-
Notifications
You must be signed in to change notification settings - Fork 131
/
Wz_Image.cs
471 lines (425 loc) · 16.8 KB
/
Wz_Image.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Runtime.InteropServices;
namespace WzComparerR2.WzLib
{
public class Wz_Image
{
public Wz_Image(string name, int size, int cs32, uint hashOff, uint hashPos, Wz_File wz_f)
{
this.Name = name;
this.WzFile = wz_f;
this.Size = size;
this.Checksum = cs32;
this.HashedOffset = hashOff;
this.HashedOffsetPosition = hashPos;
this.Node = new Wz_ImageNode(name, this);
this.extr = false;
this.chec = false;
this.checEnc = false;
}
private bool extr;
private bool chec;
private bool checEnc;
private Wz_CryptoKeyType encType;
public string Name { get; set; }
public Wz_File WzFile { get; set; }
public int Size { get; set; }
public int Checksum { get; set; }
public uint HashedOffset { get; set; }
public uint HashedOffsetPosition { get; set; }
public long Offset { get; set; }
public Wz_Node Node { get; private set; }
public Wz_Node OwnerNode { get; set; }
public bool IsChecksumChecked
{
get { return this.chec; }
internal set { this.chec = value; }
}
public bool IsLuaImage
{
get { return this.Name.EndsWith(".lua"); }
}
public Wz_Crypto.Wz_CryptoKey EncKeys
{
get
{
var crypto = this.WzFile.WzStructure.encryption;
if (this.checEnc && this.encType != default)
{
return crypto.GetKeys(this.encType);
}
return crypto.keys;
}
}
public bool TryExtract()
{
Exception ex;
return TryExtract(out ex);
}
public bool TryExtract(out Exception e)
{
if (!this.extr)
{
bool disabledChec = this.WzFile?.WzStructure?.ImgCheckDisabled ?? false;
if (!disabledChec && !this.chec)
{
if (this.Checksum != CalcCheckSum())
{
e = new ArgumentException("checksum error");
return false;
}
this.chec = true;
}
if (!this.checEnc)
{
if (!this.IsLuaImage)
{
try
{
this.TryDetectEnc();
if (!this.checEnc)
{
e = null;
return false;
}
}
catch (Exception ex)
{
e = ex;
this.Unextract();
return false;
}
}
}
try
{
lock (this.WzFile.ReadLock)
{
this.WzFile.FileStream.Position = this.Offset;
if (!this.IsLuaImage)
{
ExtractImg(this.Offset, this.Node);
this.WzFile.stringTable.Clear();
}
else
{
ExtractLua();
}
this.extr = true;
}
}
catch (Exception ex)
{
e = ex;
this.Unextract();
return false;
}
}
e = null;
return true;
}
public void Unextract()
{
this.extr = false;
this.Node.Nodes.Clear();
}
public unsafe int CalcCheckSum()
{
lock (this.WzFile.ReadLock)
{
this.WzFile.FileStream.Position = this.Offset;
int cs = 0;
byte[] buffer = new byte[4096];
int count;
int size = this.Size;
while ((count = this.WzFile.FileStream.Read(buffer, 0, Math.Min(size, buffer.Length))) > 0)
{
fixed (byte* pBuffer = buffer)
{
int* p = (int*)pBuffer;
int i, j = count / 4;
for (i = 0; i < j; i++)
{
int data = *(p + i);
cs += (data & 0xff) + (data >> 8 & 0xff) + (data >> 16 & 0xff) + (data >> 24 & 0xff);
}
for (i = i * 4; i < count; i++)
{
cs += buffer[i];
}
}
size -= count;
}
return cs;
}
}
private void ExtractImg(long offset, Wz_Node parent)
{
int entries = 0;
string tag = this.WzFile.ReadString(offset, this.EncKeys);
switch (tag)
{
case "Property":
this.WzFile.FileStream.Position += 2;
entries = this.WzFile.ReadInt32();
for (int i = 0; i < entries; i++)
{
ExtractValue(offset, parent);
}
break;
case "Shape2D#Vector2D":
parent.Value = new Wz_Vector(this.WzFile.ReadInt32(), this.WzFile.ReadInt32());
break;
case "Canvas":
this.WzFile.FileStream.Position++;
if (this.WzFile.BReader.ReadByte() == 0x01)
{
this.WzFile.FileStream.Position += 2;
entries = this.WzFile.ReadInt32();
for (int i = 0; i < entries; i++)
{
ExtractValue(offset, parent);
}
}
int w = this.WzFile.ReadInt32();
int h = this.WzFile.ReadInt32();
int form = this.WzFile.ReadInt32();
this.WzFile.FileStream.Position += 5;
int dataLen = this.WzFile.BReader.ReadInt32();
parent.Value = new Wz_Png(w, h, dataLen, form, (uint)this.WzFile.FileStream.Position, this);
this.WzFile.FileStream.Position += dataLen;
break;
case "Shape2D#Convex2D":
entries = this.WzFile.ReadInt32();
Wz_Vector[] points = new Wz_Vector[entries];
Wz_Node virtualNode = new Wz_Node();
for (int i = 0; i < entries; i++)
{
ExtractImg(offset, virtualNode);
if (virtualNode.Value is Wz_Vector point)
{
points[i] = point;
}
else
{
throw new Exception("Convex2D contains non vector2D items.");
}
}
parent.Value = new Wz_Convex(points);
break;
case "Sound_DX8":
this.WzFile.FileStream.Position++;
dataLen = this.WzFile.ReadInt32();
int duration = this.WzFile.ReadInt32();
int soundDecl = this.WzFile.BReader.ReadByte();
var mediaType = new Interop.AM_MEDIA_TYPE();
mediaType.MajorType = new Guid(this.WzFile.BReader.ReadBytes(16));
mediaType.SubType = new Guid(this.WzFile.BReader.ReadBytes(16));
mediaType.FixedSizeSamples = this.WzFile.BReader.ReadByte() != 0;
mediaType.TemporalCompression = this.WzFile.BReader.ReadByte() != 0;
mediaType.FormatType = new Guid(this.WzFile.BReader.ReadBytes(16));
switch(soundDecl)
{
case 2:
int fmtExLen = this.WzFile.ReadInt32();
var fmtExData = this.WzFile.BReader.ReadBytes(fmtExLen);
this.EncKeys.Decrypt(fmtExData, 0, fmtExData.Length);
GCHandle gcHandle = GCHandle.Alloc(fmtExData, GCHandleType.Pinned);
mediaType.CbFormat = (uint)fmtExLen;
try
{
var waveFormatEx = Marshal.PtrToStructure<Interop.WAVEFORMATEX>(gcHandle.AddrOfPinnedObject());
if (fmtExLen != waveFormatEx.CbSize + Marshal.SizeOf<Interop.WAVEFORMATEX>())
{
throw new Exception($"Failed to parse WAVEFORMATEX struct at offset {this.WzFile.FileStream.Position}.");
}
switch (waveFormatEx.FormatTag)
{
case Interop.WAVE_FORMAT_PCM:
mediaType.PbFormat = waveFormatEx;
break;
case Interop.WAVE_FORMAT_MPEGLAYER3:
mediaType.PbFormat = Marshal.PtrToStructure<Interop.MPEGLAYER3WAVEFORMAT>(gcHandle.AddrOfPinnedObject());
break;
default:
throw new Exception($"Unknown WAVEFORMATEX.FormatTag {waveFormatEx.FormatTag} at offset {this.WzFile.FileStream.Position}.");
}
}
finally
{
gcHandle.Free();
}
break;
}
parent.Value = new Wz_Sound((uint)this.WzFile.FileStream.Position, dataLen, duration, mediaType, this);
this.WzFile.FileStream.Position += dataLen;
break;
case "UOL":
this.WzFile.FileStream.Position++;
parent.Value = new Wz_Uol(this.WzFile.ReadString(offset, this.EncKeys));
break;
case "RawData": // introduced in GMS v243
this.WzFile.FileStream.Position++;
int rawDataLen = this.WzFile.ReadInt32();
uint rawDataOffset = (uint)this.WzFile.FileStream.Position;
parent.Value = new Wz_RawData(rawDataOffset, rawDataLen, this);
this.WzFile.FileStream.Position += rawDataLen;
break;
default:
throw new Exception("unknown wz tag: " + tag);
}
}
private void TryDetectEnc()
{
this.encType = default;
this.checEnc = false;
var wzsEncType = this.WzFile.WzStructure.encryption.EncType;
if (wzsEncType != default)
{
if (this.IsIllegalTag(wzsEncType))
{
this.encType = wzsEncType;
this.checEnc = true;
return;
}
}
foreach (var enc in new[] {
Wz_CryptoKeyType.BMS,
Wz_CryptoKeyType.KMS,
Wz_CryptoKeyType.GMS,
})
{
if (this.IsIllegalTag(enc))
{
this.encType = enc;
this.checEnc = true;
return;
}
}
}
private bool IsIllegalTag(Wz_CryptoKeyType keyType)
{
this.WzFile.FileStream.Position = this.Offset;
this.WzFile.stringTable.Remove(Offset);
switch (this.WzFile.ReadString(Offset, keyType))
{
case "Property":
case "Shape2D#Vector2D":
case "Canvas":
case "Shape2D#Convex2D":
case "Sound_DX8":
case "UOL":
return true;
default:
return false;
}
}
private void ExtractValue(long offset, Wz_Node parent)
{
parent = parent.Nodes.Add(this.WzFile.ReadString(offset, this.EncKeys));
byte flag = this.WzFile.BReader.ReadByte();
switch (flag)
{
case 0x00:
parent.Value = null;
break;
case 0x02:
case 0x0B:
parent.Value = this.WzFile.BReader.ReadInt16();
break;
case 0x03:
case 0x13:
// case 0x14:
parent.Value = this.WzFile.ReadInt32();
break;
case 0x14:
parent.Value = this.WzFile.ReadInt64();
break;
case 0x04:
parent.Value = this.WzFile.ReadSingle();
break;
case 0x05:
parent.Value = this.WzFile.BReader.ReadDouble();
break;
case 0x08:
parent.Value = this.WzFile.ReadString(offset, this.EncKeys);
break;
case 0x09:
int objDataLen = this.WzFile.BReader.ReadInt32();
long eob = this.WzFile.FileStream.Position + objDataLen;
ExtractImg(offset, parent);
if (this.WzFile.FileStream.Position != eob)
{
throw new Exception($"Object is not fully loaded at offset {this.WzFile.FileStream.Position}.");
}
break;
default:
throw new Exception($"Unknown value type {flag} at offset {this.WzFile.FileStream.Position}.");
}
}
private void ExtractLua()
{
while(this.WzFile.FileStream.Position < this.Offset + this.Size)
{
var flag = this.WzFile.BReader.ReadByte();
switch (flag)
{
case 0x01:
ExtractLuaValue(this.Node);
break;
default:
throw new Exception("读取Lua错误." + flag + " at Offset: " + this.WzFile.FileStream.Position);
}
}
}
private void ExtractLuaValue(Wz_Node parent)
{
int len = this.WzFile.ReadInt32();
byte[] data = this.WzFile.BReader.ReadBytes(len);
if (!this.checEnc)
{
TryDetectLuaEnc(data);
}
this.EncKeys.Decrypt(data, 0, data.Length);
string luaCode = Encoding.UTF8.GetString(data);
parent.Value = luaCode;
}
private void TryDetectLuaEnc(byte[] luaBinary)
{
byte[] tempBuffer = new byte[Math.Min(luaBinary.Length, 64)];
char[] tempStr = new char[tempBuffer.Length];
//测试各种加密方式 判断符合度最高的
int maxCharCount = 0;
var maxCharEnc = Wz_CryptoKeyType.Unknown;
foreach (var enc in new[] {
Wz_CryptoKeyType.BMS,
Wz_CryptoKeyType.KMS,
Wz_CryptoKeyType.GMS,
})
{
Buffer.BlockCopy(luaBinary, 0, tempBuffer, 0, tempBuffer.Length);
this.WzFile.WzStructure.encryption.GetKeys(enc).Decrypt(tempBuffer, 0, tempBuffer.Length);
int count = Encoding.UTF8.GetChars(tempBuffer, 0, tempBuffer.Length, tempStr, 0);
int asciiCount = tempStr.Take(count).Count(chr => 32 <= chr && chr <= 127);
if (maxCharCount < asciiCount)
{
maxCharEnc = enc;
maxCharCount = asciiCount;
}
}
this.encType = maxCharEnc;
this.checEnc = true;
}
internal class Wz_ImageNode : Wz_Node
{
public Wz_ImageNode(string nodeText, Wz_Image image) : base(nodeText)
{
this.Image = image;
}
public Wz_Image Image { get; private set; }
}
}
}