This repository has been archived by the owner on Dec 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWZFile.cs
331 lines (297 loc) · 11.8 KB
/
WZFile.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
// reWZ is copyright angelsl, 2011 to 2015 inclusive.
//
// This file (WZFile.cs) is part of reWZ.
//
// reWZ is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// reWZ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with reWZ. If not, see <http://www.gnu.org/licenses/>.
//
// Linking reWZ statically or dynamically with other modules
// is making a combined work based on reWZ. Thus, the terms and
// conditions of the GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of reWZ give you
// permission to link reWZ with independent modules to produce an
// executable, regardless of the license terms of these independent modules,
// and to copy and distribute the resulting executable under terms of your
// choice, provided that you also meet, for each linked independent module,
// the terms and conditions of the license of that module. An independent
// module is a module which is not derived from or based on reWZ.
using System;
using System.Collections.Generic;
using System.Globalization;
using reWZ.WZProperties;
using UsefulThings;
namespace reWZ {
/// <summary>A WZ file.</summary>
public sealed class WZFile : IDisposable {
internal readonly WZAES _aes;
private readonly IBytePointerObject _bpo;
internal readonly bool _encrypted;
internal readonly WZReadSelection _flag;
private readonly WZBinaryReader _r;
internal readonly WZVariant _variant;
internal uint _fstart;
/// <summary>Creates and loads a WZ file from a path. The Stream created will be disposed when the WZ file is disposed.</summary>
/// <param name="path"> The path where the WZ file is located. </param>
/// <param name="variant"> The variant of this WZ file. </param>
/// <param name="encrypted"> Whether the WZ file is encrypted outside a WZ image. </param>
/// <param name="flag"> WZ parsing flags. </param>
public unsafe WZFile(string path, WZVariant variant, bool encrypted, WZReadSelection flag = WZReadSelection.None) {
_variant = variant;
_encrypted = encrypted;
_flag = flag;
_aes = new WZAES(_variant);
_bpo = new MemoryMappedFile(path);
_r = new WZBinaryReader(_bpo.Pointer, _bpo.Length, _aes, 0);
Parse();
}
/// <summary>The root directory of the WZ file.</summary>
public WZDirectory MainDirectory { get; private set; }
/// <summary>Disposer.</summary>
~WZFile() {
Dispose(false);
}
/// <summary>Resolves a path in the form "/a/b/c/.././d/e/f/".</summary>
/// <param name="path"> The path to resolve. </param>
/// <exception cref="System.Collections.Generic.KeyNotFoundException">The path has an invalid node.</exception>
public WZObject ResolvePath(string path) {
return WZUtil.ResolvePath(MainDirectory, path);
}
private void Parse() {
_r.Seek(0);
if (_r.ReadInt32() != 0x31474B50) {
WZUtil.Die("WZ file has invalid header; file does not have magic \"PKG1\".");
}
_r.Skip(8);
_fstart = _r.ReadUInt32();
GuessVersion();
MainDirectory = new WZDirectory("", null, this, _r, _fstart + 2);
}
private void GuessVersion() {
_r.Seek(_fstart);
short ver = _r.ReadInt16();
bool success;
long offset = TryFindImageInDir(out success);
if (success && GuessVersionWithImageOffsetAt(ver, offset)) {
return;
}
for (ushort v = 0; v < ushort.MaxValue; v++) {
uint vHash;
if (!VersionHash(v, ver, out vHash)) {
continue;
}
_r.VersionHash = vHash;
if (DepthFirstImageSearch(out offset)) {
break;
}
}
if (!GuessVersionWithImageOffsetAt(ver, offset)) {
WZUtil.Die("Unable to guess WZ version.");
}
}
private bool DepthFirstImageSearch(out long offset) {
bool success = false;
offset = -1;
int count = _r.ReadWZInt();
for (int i = 0; i < count; i++) {
byte type = _r.ReadByte();
switch (type) {
case 1:
_r.Skip(10);
continue;
case 2:
int x = _r.ReadInt32();
type = _r.PeekFor(() => {
_r.Seek(x + _fstart);
return _r.ReadByte();
});
break;
case 3:
case 4:
_r.SkipWZString();
break;
default:
WZUtil.Die("Unknown object type in WzDirectory.");
break;
}
_r.ReadWZInt();
_r.ReadWZInt();
offset = _r.Position;
if (type == 4) {
success = true;
break;
}
if (type == 3) {
try {
offset = _r.PeekFor(() => {
_r.Seek(_r.ReadWZOffset(_fstart));
long o;
success = DepthFirstImageSearch(out o);
return o;
});
break;
} catch {}
}
_r.Skip(4);
}
return success;
}
private long TryFindImageInDir(out bool success) {
int count = _r.ReadWZInt();
if (count == 0) {
WZUtil.Die("WZ file has no entries!");
}
long offset = 0;
offset = TryFindImageOffset(count, offset, out success);
return offset;
}
private long TryFindImageOffset(int count, long offset, out bool success) {
success = false;
for (int i = 0; i < count; i++) {
byte type = _r.ReadByte();
switch (type) {
case 1:
_r.Skip(10);
continue;
case 2:
int x = _r.ReadInt32();
type = _r.PeekFor(() => {
_r.Seek(x + _fstart);
return _r.ReadByte();
});
break;
case 3:
case 4:
_r.SkipWZString();
break;
default:
WZUtil.Die("Unknown object type in WzDirectory.");
break;
}
_r.ReadWZInt();
_r.ReadWZInt();
offset = _r.Position;
_r.Skip(4);
if (type != 4) {
continue;
}
success = true;
break;
}
return offset;
}
private bool GuessVersionWithImageOffsetAt(short ver, long offset) {
bool success = false;
for (ushort v = 0; v < ushort.MaxValue; v++) {
uint vHash;
if (!VersionHash(v, ver, out vHash)) {
continue;
}
_r.Seek(offset);
_r.VersionHash = vHash;
try {
uint off = _r.ReadWZOffset(_fstart);
if (off + 1 > _r.Length) {
continue;
}
_r.Seek(off);
if (_r.PeekFor(() => _r.ReadWZStringBlock(true)) != "Property" &&
_r.PeekFor(() => _r.ReadWZStringBlock(false)) != "Property") {
continue;
}
success = true;
break;
} catch {
success = false;
}
}
return success;
}
internal unsafe WZBinaryReader GetSubstream(long offset, long length) {
return new WZBinaryReader(_bpo.Pointer + offset, length, _aes, _r.VersionHash);
}
internal static bool VersionHash(ushort v, short sV, out uint vHash) {
vHash = 0;
foreach (char c in v.ToString(CultureInfo.InvariantCulture)) {
vHash = 32*vHash + c + 1;
}
return (0xFF
^ ((vHash >> 24) & 0xFF)
^ ((vHash >> 16) & 0xFF)
^ ((vHash >> 8) & 0xFF)
^ (vHash & 0xFF)) == sV;
}
#region IDisposable Members
/// <summary>Disposes this WZ file.</summary>
public void Dispose() {
GC.SuppressFinalize(this);
Dispose(true);
}
private void Dispose(bool disposing) {
if (disposing) {
_bpo.Dispose();
}
MainDirectory = null;
}
#endregion
}
/// <summary>WZ reading flags.</summary>
[Flags]
public enum WZReadSelection : byte {
/// <summary>No flags are enabled, that is, lazy loading of properties and WZ images is enabled.</summary>
None = 0,
/// <summary>Set this flag to disable lazy loading of string properties.</summary>
EagerParseStrings = 1,
/// <summary>Set this flag to disable lazy loading of Audio properties.</summary>
EagerParseAudio = 2,
/// <summary>Set this flag to disable lazy loading of canvas properties.</summary>
EagerParseCanvas = 4,
/// <summary>Set this flag to completely disable loading of canvas properties.</summary>
NeverParseCanvas = 8,
/// <summary>Set this flag to disable lazy loading of string, Audio and canvas properties.</summary>
EagerParseAll = EagerParseCanvas | EagerParseAudio | EagerParseStrings,
/// <summary>Set this flag to disable lazy loading of WZ images.</summary>
EagerParseImage = 16
}
internal static class WZUtil {
internal static T Die<T>(string cause) {
throw new WZException(cause);
}
internal static void Die(string cause) {
throw new WZException(cause);
}
internal static WZObject ResolvePath(WZObject start, string path) {
try {
WZObject result = start;
string[] nodes = (path.StartsWith("/") ? path.Substring(1) : path).Split('/');
foreach (string node in nodes) {
switch (node) {
case ".":
continue;
case "..":
result = result.Parent;
continue;
}
WZUOLProperty uolNode = result as WZUOLProperty;
if (uolNode != null) {
result = uolNode.FinalTarget;
}
result = result[node];
}
return result;
} catch (KeyNotFoundException) {
return null;
}
}
}
}