Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
427 lines (353 loc) · 10.8 KB
/
Copy pathindex.ts
File metadata and controls
427 lines (353 loc) · 10.8 KB
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
import * as fzstd from "./lib/fzstd.js";
class BinaryReader {
private readonly view: DataView;
private offset: number;
constructor(data: Uint8Array) {
this.view = new DataView(data.buffer);
this.offset = data.byteOffset;
}
readU8() {
const result = this.view.getUint8(this.offset);
this.offset += 1;
return result;
}
readU16() {
const result = this.view.getUint16(this.offset, true)
this.offset += 2;
return result;
}
readU32() {
const result = this.view.getUint32(this.offset, true);
this.offset += 4;
return result;
}
readU64() {
const result = this.view.getBigUint64(this.offset, true);
this.offset += 8;
return result;
}
readI8() {
const result = this.view.getInt8(this.offset);
this.offset += 1;
return result;
}
readI16() {
const result = this.view.getInt16(this.offset, true);
this.offset += 2;
return result;
}
readI32() {
const result = this.view.getInt32(this.offset, true);
this.offset += 4;
return result;
}
readI64() {
const result = this.view.getBigUint64(this.offset, true);
this.offset += 8;
return result;
}
readAsciiString(length: number) {
let result = "";
for (let i = 0; i < length; i++) {
const charCode = this.readU8();
result += String.fromCharCode(charCode);
}
return result;
}
readName(nameMap: readonly string[]) {
const index = this.readI32();
return 0 <= index && index < nameMap.length ? nameMap[index] : null;
}
getRemainingBytes() {
return new Uint8Array(this.view.buffer, this.offset);
}
}
/* -------- */
enum UsmapVersion {
Initial,
PackageVersioning,
LongFName,
LargeEnums,
ExplicitEnumValues,
LatestPlusOne,
Latest = LatestPlusOne - 1
}
enum UsmapCompressionMethod {
None,
Oodle,
Brotli,
Zstandard
}
/* -------- */
type Guid = [number, number, number, number];
type PackageFileVersion = {
fileVersionUE4: number;
fileVersionUE5: number;
}
type CustomVersion = {
key: Guid;
version: number;
}
type PackageVersioning = {
packageVersion: PackageFileVersion;
customVersions: CustomVersion[];
netCL: number;
}
/* -------- */
enum PropertyTypeKind {
ByteProperty,
BoolProperty,
IntProperty,
FloatProperty,
ObjectProperty,
NameProperty,
DelegateProperty,
DoubleProperty,
ArrayProperty,
StructProperty,
StrProperty,
TextProperty,
InterfaceProperty,
MulticastDelegateProperty,
WeakObjectProperty,
LazyObjectProperty,
AssetObjectProperty,
SoftObjectProperty,
UInt64Property,
UInt32Property,
UInt16Property,
Int64Property,
Int16Property,
Int8Property,
MapProperty,
SetProperty,
EnumProperty,
FieldPathProperty,
OptionalProperty,
Utf8StrProperty,
AnsiStrProperty
}
type EnumMember = {
name: string;
value: number | bigint;
}
type Enum = {
name: string;
members: EnumMember[];
}
type AtomicPropertyType = string;
type EnumPropertyType = {
type: string;
enumName: string;
innerType: PropertyType;
}
type StructPropertyType = {
type: string;
structType: string;
}
type SequencePropertyType = {
type: string;
innerType: PropertyType;
}
type MapPropertyType = {
type: string;
innerType: PropertyType;
valueType: PropertyType;
}
type PropertyType =
| AtomicPropertyType
| EnumPropertyType
| StructPropertyType
| SequencePropertyType
| MapPropertyType;
type PropertyInfo = {
index: number;
name: string;
arraySize: number;
type: PropertyType;
}
type Struct = {
name: string;
superType: string | null;
propertyCount: number;
properties: PropertyInfo[];
}
type Usmap = {
packageVersioning: PackageVersioning | null;
names: string[];
enums: Enum[];
structs: Struct[];
}
const MAGIC = 0x30C4;
/* -------- */
function readGuid(reader: BinaryReader): Guid {
const a = reader.readU32();
const b = reader.readU32();
const c = reader.readU32();
const d = reader.readU32();
return [a, b, c, d];
}
function readCustomVersion(reader: BinaryReader): CustomVersion {
const key = readGuid(reader);
const version = reader.readI32();
return {key, version};
}
function readPackageVersioning(reader: BinaryReader): PackageVersioning {
const fileVersionUE4 = reader.readI32();
const fileVersionUE5 = reader.readI32();
const packageVersion = {fileVersionUE4, fileVersionUE5};
const numCustomVersions = reader.readI32();
const customVersions: CustomVersion[] = [];
for (let i = 0; i < numCustomVersions; i++) {
customVersions.push(readCustomVersion(reader));
}
const netCL = reader.readU32();
return {packageVersion, customVersions, netCL};
}
/* -------- */
function readPropertyType(reader: BinaryReader, nameMap: readonly string[]): PropertyType {
const kind = reader.readU8();
const type = PropertyTypeKind[kind];
switch (kind) {
case PropertyTypeKind.EnumProperty: {
const innerType = readPropertyType(reader, nameMap);
const enumName = reader.readName(nameMap)!;
return {type, enumName, innerType};
}
case PropertyTypeKind.StructProperty: {
const structType = reader.readName(nameMap)!;
return {type, structType};
}
case PropertyTypeKind.SetProperty:
case PropertyTypeKind.ArrayProperty:
case PropertyTypeKind.OptionalProperty: {
const innerType = readPropertyType(reader, nameMap);
return {type, innerType};
}
case PropertyTypeKind.MapProperty: {
const innerType = readPropertyType(reader, nameMap);
const valueType = readPropertyType(reader, nameMap);
return {type, innerType, valueType};
}
default:
return type;
}
}
function readPropertyInfo(reader: BinaryReader, nameMap: readonly string[]): PropertyInfo {
const index = reader.readU16();
const arraySize = reader.readU8();
const name = reader.readName(nameMap)!;
const type = readPropertyType(reader, nameMap);
return {index, name, arraySize, type};
}
function readStruct(reader: BinaryReader, nameMap: readonly string[]): Struct {
const name = reader.readName(nameMap)!;
const superType = reader.readName(nameMap);
const propertyCount = reader.readU16();
const serializablePropertyCount = reader.readU16();
const properties: PropertyInfo[] = [];
for (let i = 0; i < serializablePropertyCount; i++) {
properties.push(readPropertyInfo(reader, nameMap));
}
return {name, superType, propertyCount, properties};
}
function parseUsmapBody(version: UsmapVersion, data: Uint8Array): Usmap {
const reader = new BinaryReader(data);
const numNames = reader.readU32();
const nameMap: string[] = [];
for (let i = 0; i < numNames; i++) {
const nameLength = version >= UsmapVersion.LongFName ? reader.readU16() : reader.readU8();
nameMap.push(reader.readAsciiString(nameLength));
}
const numEnums = reader.readU32();
const enums = new Map<string, Enum>();
for (let i = 0; i < numEnums; i++) {
const enumName = reader.readName(nameMap)!;
const numMembers = version >= UsmapVersion.LargeEnums ? reader.readU16() : reader.readU8();
const members: EnumMember[] = [];
if (version >= UsmapVersion.ExplicitEnumValues) {
for (let i = 0; i < numMembers; i++) {
const value = reader.readU64();
const name = reader.readName(nameMap)!;
members.push({name, value});
}
} else {
for (let i = 0; i < numMembers; i++) {
const memberName = reader.readName(nameMap)!;
members.push({name: memberName, value: i });
}
}
if (!enums.has(enumName)) {
enums.set(enumName, {name: enumName, members});
}
}
const numStructs = reader.readU32();
const structs = new Map<string, Struct>();
for (let i = 0; i < numStructs; i++) {
const struct = readStruct(reader, nameMap);
structs.set(struct.name, struct);
}
return {
packageVersioning: null,
names: nameMap,
enums: [...enums.values()],
structs: [...structs.values()]
}
}
function parseUsmap(data: Uint8Array): Usmap {
const reader = new BinaryReader(data);
const magic = reader.readU16();
if (magic !== MAGIC) {
throw new Error("Usmap has invalid magic: " + magic);
}
const usmapVersion = reader.readU8();
if (usmapVersion > UsmapVersion.Latest) {
throw new Error("Usmap has invalid version: " + usmapVersion);
}
let hasVersioning = false;
if (usmapVersion >= UsmapVersion.PackageVersioning) {
const b = reader.readI32();
hasVersioning = b === 1;
}
const packageVersioning = hasVersioning ? readPackageVersioning(reader) : null;
const compressionMethod = reader.readU8();
const sizeCompressed = reader.readU32();
const sizeDecompressed = reader.readU32();
const body = reader.getRemainingBytes();
let uncompressed: Uint8Array;
switch (compressionMethod) {
case UsmapCompressionMethod.None:
if (sizeCompressed !== sizeDecompressed) {
throw new Error("No compression specified but sizeCompressed != sizeDecompressed");
}
uncompressed = body.slice();
break;
case UsmapCompressionMethod.Oodle:
throw new Error("Usmap uses Oodle compression, which is unsupported");
case UsmapCompressionMethod.Brotli:
uncompressed = (window as any).brotli.decompress(body);
break;
case UsmapCompressionMethod.Zstandard:
uncompressed = fzstd.decompress(body, null);
break;
default:
throw new Error("Unsupported compression method: " + compressionMethod);
}
const result = parseUsmapBody(usmapVersion, uncompressed);
result.packageVersioning = packageVersioning;
return result;
}
function jsonifyUsmap(usmap: Usmap): string {
const replacer = (key: any, value: any) => {
if (typeof value === "bigint") {
if (Number.MIN_SAFE_INTEGER <= value && value <= Number.MAX_SAFE_INTEGER) {
return Number(value);
} else {
return value.toString();
}
}
return value;
};
return JSON.stringify({ enums: usmap.enums, structs: usmap.structs }, replacer, 2);
}
export { parseUsmap, jsonifyUsmap };