-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXcpUnpack.cs
303 lines (278 loc) · 11 KB
/
XcpUnpack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
namespace XCPpackage
{
using Ionic.Zlib;
using JasonNS.EventArguments;
using JasonNS.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
public class XcpUnpack : IDisposable
{
private FileInfo _fileIn;
private long _lastStream;
private long _srcsize;
public event EventHandler SplitCompleted;
public event EventHandler<ProgressChangedEventArgs> SplitProgressChanged;
public event EventHandler UnpackAndSplitComplete;
public event EventHandler<ProgressChangedEventArgs> UnpackAndSplitProgressChanged;
public event EventHandler UnpackCompleted;
public event EventHandler<ProgressChangedEventArgs> UnpackProgressChanged;
public XcpUnpack(FileInfo fileIn)
{
this._fileIn = fileIn;
}
public XcpUnpack(string fileIn) : this(new FileInfo(fileIn))
{
}
public List<FileInfo> DecompressAndSplit(bool cleanup = false, string directoryOut = null)
{
List<FileInfo> list;
try
{
this.Reinit();
this._fileIn = this.DecompressXcp(cleanup, directoryOut);
this.OnUnpackAndSplitComplete(new EventArgs());
list = this.SplitXcp(cleanup, directoryOut);
}
catch (Exception exception1)
{
Console.Write(exception1.ToString());
throw;
}
return list;
}
public FileInfo DecompressXcp(bool cleanup = false, string directoryOut = null)
{
FileInfo info;
FileInfo info3;
if (!this._fileIn.Exists)
{
throw new FileNotFoundException("File Not Found", this._fileIn.FullName);
}
FileOptions options = cleanup ? FileOptions.DeleteOnClose : FileOptions.None;
if (directoryOut == null)
{
info = new FileInfo(this._fileIn.DirectoryName + @"\" + Path.GetFileNameWithoutExtension(this._fileIn.Name) + ".xup");
}
else
{
if (!Directory.Exists(directoryOut + @"\"))
{
Directory.CreateDirectory(directoryOut + @"\");
}
info = new FileInfo(directoryOut + @"\" + Path.GetFileNameWithoutExtension(this._fileIn.Name) + ".xup");
}
try
{
FileInfo info2;
using (FILE file = new FILE(this._fileIn.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 0x4000, options))
{
if (info.Exists)
{
info.Delete();
}
info2 = this.first_pass(file, info.FullName);
}
this.OnUnpackCompleted(new EventArgs());
this._fileIn = info2;
info3 = info2;
}
catch (Exception exception1)
{
info.Delete();
Console.WriteLine(exception1.ToString());
throw;
}
return info3;
}
public void Dispose()
{
this.UnpackAndSplitProgressChanged = null;
this.UnpackProgressChanged = null;
this.SplitProgressChanged = null;
this.UnpackAndSplitComplete = null;
this.UnpackCompleted = null;
this.SplitCompleted = null;
}
private FileInfo first_pass(FILE inXcp, string outpath)
{
FileInfo fi;
this.Reinit();
this._srcsize = inXcp.FileSize;
inXcp.Position = 0L;
Console.Write("\r\nUnpacking XCP\r\n");
using (FILE file = new FILE(outpath, FileMode.Create, FileAccess.Write, FileShare.Read, 0x4000, FileOptions.None))
{
fi = file.Fi;
while ((this._srcsize - this._lastStream) > 0L)
{
this.Inflate(inXcp, file);
}
}
Console.Write("\r\nUnpack Sucess\r\n");
return fi;
}
private void Inflate(FILE source, FILE dest)
{
byte[] buffer = new byte[16384];
ZlibStream zlibStream = new ZlibStream((Stream)source, CompressionMode.Decompress, true)
{
BufferSize = 16384,
FlushMode = FlushType.Full
};
short num1 = 0;
while (zlibStream.Read(buffer, 0, buffer.Length) != 0)
{
dest.Write(buffer, 0, buffer.Length);
short num2 = (short)Math.Ceiling(((double)this._lastStream + (double)zlibStream.TotalIn) / (double)this._srcsize * 100.0);
if ((int)num1 != (int)num2)
{
num1 = num2;
this.OnUnpackProgressChanged(new ProgressChangedEventArgs((int)num2));
this.OnUnpackAndSplitProgressChanged(new ProgressChangedEventArgs((int)num2 / 2));
}
}
this._lastStream += zlibStream.TotalIn;
source.Position = this._lastStream;
zlibStream.Dispose();
}
protected virtual void OnSplitCompleted(EventArgs e)
{
EventHandler splitCompleted = this.SplitCompleted;
if (splitCompleted != null)
{
splitCompleted(this, e);
}
}
protected virtual void OnSplitProgressChanged(ProgressChangedEventArgs e)
{
EventHandler<ProgressChangedEventArgs> splitProgressChanged = this.SplitProgressChanged;
if (splitProgressChanged != null)
{
splitProgressChanged(this, e);
}
}
protected virtual void OnUnpackAndSplitComplete(EventArgs e)
{
EventHandler unpackAndSplitComplete = this.UnpackAndSplitComplete;
if (unpackAndSplitComplete != null)
{
unpackAndSplitComplete(this, e);
}
}
protected virtual void OnUnpackAndSplitProgressChanged(ProgressChangedEventArgs e)
{
EventHandler<ProgressChangedEventArgs> unpackAndSplitProgressChanged = this.UnpackAndSplitProgressChanged;
if (unpackAndSplitProgressChanged != null)
{
unpackAndSplitProgressChanged(this, e);
}
}
protected virtual void OnUnpackCompleted(EventArgs e)
{
EventHandler unpackCompleted = this.UnpackCompleted;
if (unpackCompleted != null)
{
unpackCompleted(this, e);
}
}
protected virtual void OnUnpackProgressChanged(ProgressChangedEventArgs e)
{
EventHandler<ProgressChangedEventArgs> unpackProgressChanged = this.UnpackProgressChanged;
if (unpackProgressChanged != null)
{
unpackProgressChanged(this, e);
}
}
private void Reinit()
{
this._lastStream = 0L;
this._srcsize = 0L;
}
private List<FileInfo> second_pass(FILE unpackedXcp, string outDir)
{
this.Reinit();
List<FileInfo> fileInfoList = new List<FileInfo>();
Console.Write("\r\nSplitting extracted XCP in GOD format\r\n");
long length = unpackedXcp.Length;
byte[] buffer1 = new byte[45057];
string str = new string(new char[256]);
unpackedXcp.Read(buffer1, 0, 45056);
string filePath = outDir + "\\" + Path.GetFileName(outDir);
long num1;
using (FILE file = new FILE(filePath, FileMode.Create, FileAccess.Write, FileShare.Read, 16384))
{
file.Write(buffer1, 0, 45056);
num1 = 45056L;
fileInfoList.Add(file.Fi);
}
int num2 = 0;
byte[] buffer2 = new byte[16384];
short num3 = 0;
while (num1 < length)
{
if (!Directory.Exists(string.Format("{0}.data", (object)filePath)))
Directory.CreateDirectory(string.Format("{0}.data", (object)filePath));
using (FILE file = new FILE(string.Format("{0}.data\\Data{1:D4}", (object)filePath, (object)num2), FileMode.Create, FileAccess.Write, FileShare.Read, 16384))
{
for (int index = 0; index < 10404; ++index)
{
int count = num1 + 16384L >= length ? (int)(length - num1) : 16384;
unpackedXcp.Read(buffer2, 0, count);
file.Write(buffer2, 0, count);
num1 += (long)count;
short num4 = (short)Math.Ceiling((double)num1 / (double)length * 100.0);
if ((int)num4 != (int)num3)
{
num3 = num4;
this.OnSplitProgressChanged(new ProgressChangedEventArgs((int)num4));
this.OnUnpackAndSplitProgressChanged(new ProgressChangedEventArgs((int)num4 / 2 + 50));
}
}
fileInfoList.Add(file.Fi);
++num2;
}
}
Console.Write("\r\nFile splitting Ok\r\n");
return fileInfoList;
}
public List<FileInfo> SplitXcp(bool cleanup = false, string directoryOut = null)
{
List<FileInfo> list2;
FileOptions options = cleanup ? FileOptions.DeleteOnClose : FileOptions.None;
DirectoryInfo info = (directoryOut != null) ? new DirectoryInfo(directoryOut) : new DirectoryInfo(this._fileIn.DirectoryName + @"\" + Path.GetFileNameWithoutExtension(this._fileIn.FullName));
try
{
List<FileInfo> list;
using (FILE file = new FILE(this._fileIn.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 0x4000, options))
{
if (!info.Exists)
{
info.Create();
}
list = this.second_pass(file, info.FullName);
}
this.OnSplitCompleted(new EventArgs());
list2 = list;
}
catch (Exception exception1)
{
Console.WriteLine(exception1.ToString());
throw;
}
return list2;
}
public string DefaultPathOut =>
this._fileIn.DirectoryName + @"\" + Path.GetFileNameWithoutExtension(this._fileIn.Name) + ".xup";
private static class UnpackConstant
{
internal const int Chunk = 0x4000;
internal const int HeaderSize = 0xb000;
internal const int OptBuffSize = 0x4000;
internal const int DataSize = 0xa290000;
}
}
}