forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextDumperTool.cs
433 lines (351 loc) · 13.8 KB
/
TextDumperTool.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
using System;
using System.IO;
using System.Text;
using UnityDataTools.FileSystem;
namespace UnityDataTools.TextDumper;
public class TextDumperTool
{
StringBuilder m_StringBuilder = new StringBuilder(1024);
bool m_SkipLargeArrays;
UnityFileReader m_Reader;
SerializedFile m_SerializedFile;
StreamWriter m_Writer;
public int Dump(string path, string outputPath, bool skipLargeArrays)
{
m_SkipLargeArrays = skipLargeArrays;
try
{
try
{
using var archive = UnityFileSystem.MountArchive(path, "/");
foreach (var node in archive.Nodes)
{
Console.WriteLine($"Processing {node.Path} {node.Size} {node.Flags}");
if (node.Flags.HasFlag(ArchiveNodeFlags.SerializedFile))
{
using (m_Writer = new StreamWriter(Path.Combine(outputPath, Path.GetFileName(node.Path) + ".txt"), false))
{
OutputSerializedFile("/" + node.Path);
}
}
}
}
catch (NotSupportedException)
{
// Try as SerializedFile
using (m_Writer = new StreamWriter(Path.Combine(outputPath, Path.GetFileName(path) + ".txt"), false))
{
OutputSerializedFile(path);
}
}
}
catch (Exception e)
{
Console.WriteLine("Error!");
Console.Write($"{e.GetType()}: ");
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return 1;
}
return 0;
}
void RecursiveDump(TypeTreeNode node, ref long offset, int level, int arrayIndex = -1)
{
bool skipChildren = false;
if (!node.IsArray)
{
m_StringBuilder.Append(' ', level * 2);
if (level != 0)
{
m_StringBuilder.Append(node.Name);
if (arrayIndex >= 0)
{
m_StringBuilder.Append('[');
m_StringBuilder.Append(arrayIndex);
m_StringBuilder.Append(']');
}
m_StringBuilder.Append(' ');
m_StringBuilder.Append('(');
m_StringBuilder.Append(node.Type);
m_StringBuilder.Append(')');
}
else
{
m_StringBuilder.Append(node.Type);
}
// Basic data type.
if (node.IsBasicType)
{
m_StringBuilder.Append(' ');
m_StringBuilder.Append(ReadValue(node, offset));
offset += node.Size;
}
else if (node.Type == "string")
{
var stringSize = m_Reader.ReadInt32(offset);
m_StringBuilder.Append(' ');
m_StringBuilder.Append(m_Reader.ReadString(offset + 4, stringSize));
offset += stringSize + 4;
// Skip child nodes as they were already processed here.
skipChildren = true;
}
m_Writer.WriteLine(m_StringBuilder);
m_StringBuilder.Clear();
if (node.IsManagedReferenceRegistry)
{
DumpManagedReferenceRegistry(node, ref offset, level + 1);
// Skip child nodes as they were already processed here.
skipChildren = true;
}
}
else
{
DumpArray(node, ref offset, level);
// Skip child nodes as they were already processed here.
skipChildren = true;
}
if (!skipChildren)
{
foreach (var child in node.Children)
{
RecursiveDump(child, ref offset, level + 1);
}
}
if (
((int)node.MetaFlags & (int)TypeTreeMetaFlags.AlignBytes) != 0 ||
((int)node.MetaFlags & (int)TypeTreeMetaFlags.AnyChildUsesAlignBytes) != 0
)
{
offset = (offset + 3) & ~(3);
}
}
void DumpArray(TypeTreeNode node, ref long offset, int level)
{
// First child contains array size.
var sizeNode = node.Children[0];
// Second child contains array type information.
var dataNode = node.Children[1];
if (sizeNode.Size != 4 || !sizeNode.IsLeaf)
throw new Exception("Invalid array size");
var arraySize = m_Reader.ReadInt32(offset);
offset += 4;
m_StringBuilder.Append(' ', level * 2);
m_StringBuilder.Append("Array");
m_StringBuilder.Append('<');
m_StringBuilder.Append(dataNode.Type);
m_StringBuilder.Append(">[");
m_StringBuilder.Append(arraySize);
m_StringBuilder.Append(']');
m_Writer.WriteLine(m_StringBuilder);
m_StringBuilder.Clear();
if (arraySize > 0)
{
if (dataNode.IsBasicType)
{
m_StringBuilder.Append(' ', (level + 1) * 2);
if (arraySize > 256 && m_SkipLargeArrays)
{
m_StringBuilder.Append("<Skipped>");
offset += dataNode.Size * arraySize;
}
else
{
var array = ReadBasicTypeArray(dataNode, offset, arraySize);
offset += dataNode.Size * arraySize;
m_StringBuilder.Append(array.GetValue(0));
for (int i = 1; i < arraySize; ++i)
{
m_StringBuilder.Append(", ");
m_StringBuilder.Append(array.GetValue(i));
}
}
m_Writer.WriteLine(m_StringBuilder);
m_StringBuilder.Clear();
}
else
{
++level;
for (int i = 0; i < arraySize; ++i)
{
RecursiveDump(dataNode, ref offset, level, i);
}
}
}
}
void DumpManagedReferenceRegistry(TypeTreeNode node, ref long offset, int level)
{
if (node.Children.Count < 2)
throw new Exception("Invalid ManagedReferenceRegistry");
// First child is version number.
var version = m_Reader.ReadInt32(offset);
RecursiveDump(node.Children[0], ref offset, level);
TypeTreeNode refTypeNode;
TypeTreeNode refObjData;
if (version == 1)
{
// Second child is the ReferencedObject.
var refObjNode = node.Children[1];
// And its children are the referenced type and data nodes.
refTypeNode = refObjNode.Children[0];
refObjData = refObjNode.Children[1];
int i = 0;
while (DumpManagedReferenceData(refTypeNode, refObjData, ref offset, level, i++))
{}
}
else if (version == 2)
{
// Second child is the RefIds vector.
var refIdsVectorNode = node.Children[1];
if (refIdsVectorNode.Children.Count < 1 || refIdsVectorNode.Name != "RefIds")
throw new Exception("Invalid ManagedReferenceRegistry RefIds vector");
var refIdsArrayNode = refIdsVectorNode.Children[0];
if (refIdsArrayNode.Children.Count != 2 || !refIdsArrayNode.IsArray)
throw new Exception("Invalid ManagedReferenceRegistry RefIds array");
// First child is the array size.
int arraySize = m_Reader.ReadInt32(offset);
offset += 4;
// Second child is the ReferencedObject.
var refObjNode = refIdsArrayNode.Children[1];
for (int i = 0; i < arraySize; ++i)
{
// First child is the rid.
long rid = m_Reader.ReadInt64(offset);
offset += 8;
// And the next children are the referenced type and data nodes.
refTypeNode = refObjNode.Children[1];
refObjData = refObjNode.Children[2];
DumpManagedReferenceData(refTypeNode, refObjData, ref offset, level, rid);
}
}
else
{
throw new Exception("Unsupported ManagedReferenceRegistry version");
}
}
bool DumpManagedReferenceData(TypeTreeNode refTypeNode, TypeTreeNode referencedTypeDataNode, ref long offset, int level, long id)
{
if (refTypeNode.Children.Count < 3)
throw new Exception("Invalid ReferencedManagedType");
m_StringBuilder.Append(' ', level * 2);
m_StringBuilder.Append($"rid(");
m_StringBuilder.Append(id);
m_StringBuilder.Append(") ReferencedObject");
m_Writer.WriteLine(m_StringBuilder);
m_StringBuilder.Clear();
++level;
var refTypeOffset = offset;
var stringSize = m_Reader.ReadInt32(offset);
var className = m_Reader.ReadString(offset + 4, stringSize);
offset += stringSize + 4;
offset = (offset + 3) & ~(3);
stringSize = m_Reader.ReadInt32(offset);
var namespaceName = m_Reader.ReadString(offset + 4, stringSize);
offset += stringSize + 4;
offset = (offset + 3) & ~(3);
stringSize = m_Reader.ReadInt32(offset);
var assemblyName = m_Reader.ReadString(offset + 4, stringSize);
offset += stringSize + 4;
offset = (offset + 3) & ~(3);
if (className == "Terminus" && namespaceName == "UnityEngine.DMAT" && assemblyName == "FAKE_ASM")
return false;
// Not the most efficient way, but it simplifies the code.
RecursiveDump(refTypeNode, ref refTypeOffset, level);
m_StringBuilder.Append(' ', level * 2);
m_StringBuilder.Append(referencedTypeDataNode.Name);
m_StringBuilder.Append(' ');
m_StringBuilder.Append(referencedTypeDataNode.Type);
m_StringBuilder.Append(' ');
m_Writer.WriteLine(m_StringBuilder);
m_StringBuilder.Clear();
if (id == -1 || id == -2)
{
m_StringBuilder.Append(' ', level * 2);
m_StringBuilder.Append(id == -1 ? " unknown" : " null");
m_Writer.WriteLine(m_StringBuilder);
m_StringBuilder.Clear();
return true;
}
var refTypeRoot = m_SerializedFile.GetRefTypeTypeTreeRoot(className, namespaceName, assemblyName);
// Dump the ReferencedObject using its own TypeTree, but skip the root.
foreach (var child in refTypeRoot.Children)
{
RecursiveDump(child, ref offset, level + 1);
}
return true;
}
void OutputSerializedFile(string path)
{
using (m_Reader = new UnityFileReader(path, 64 * 1024 * 1024))
using (m_SerializedFile = UnityFileSystem.OpenSerializedFile(path))
{
var i = 1;
m_Writer.WriteLine("External References");
foreach (var extRef in m_SerializedFile.ExternalReferences)
{
m_Writer.WriteLine($"path({i}): \"{extRef.Path}\" GUID: {extRef.Guid} Type: {(int)extRef.Type}");
++i;
}
m_Writer.WriteLine();
foreach (var obj in m_SerializedFile.Objects)
{
var root = m_SerializedFile.GetTypeTreeRoot(obj.Id);
var offset = obj.Offset;
m_Writer.Write($"ID: {obj.Id} (ClassID: {obj.TypeId}) ");
RecursiveDump(root, ref offset, 0);
m_Writer.WriteLine();
}
}
}
string ReadValue(TypeTreeNode node, long offset)
{
switch (Type.GetTypeCode(node.CSharpType))
{
case TypeCode.Int32:
return m_Reader.ReadInt32(offset).ToString();
case TypeCode.UInt32:
return m_Reader.ReadUInt32(offset).ToString();
case TypeCode.Single:
return m_Reader.ReadFloat(offset).ToString();
case TypeCode.Double:
return m_Reader.ReadDouble(offset).ToString();
case TypeCode.Int16:
return m_Reader.ReadInt16(offset).ToString();
case TypeCode.UInt16:
return m_Reader.ReadUInt16(offset).ToString();
case TypeCode.Int64:
return m_Reader.ReadInt64(offset).ToString();
case TypeCode.UInt64:
return m_Reader.ReadUInt64(offset).ToString();
case TypeCode.SByte:
return m_Reader.ReadUInt8(offset).ToString();
case TypeCode.Byte:
case TypeCode.Char:
return m_Reader.ReadUInt8(offset).ToString();
case TypeCode.Boolean:
return (m_Reader.ReadUInt8(offset) != 0).ToString();
default:
throw new Exception($"Can't get value of {node.Type} type");
}
}
Array ReadBasicTypeArray(TypeTreeNode node, long offset, int arraySize)
{
// Special case for boolean arrays.
if (node.CSharpType == typeof(bool))
{
var tmpArray = new byte[arraySize];
var boolArray = new bool[arraySize];
m_Reader.ReadArray(offset, arraySize * node.Size, tmpArray);
for (int i = 0; i < arraySize; ++i)
{
boolArray[i] = tmpArray[i] != 0;
}
return boolArray;
}
else
{
var array = Array.CreateInstance(node.CSharpType, arraySize);
m_Reader.ReadArray(offset, arraySize * node.Size, array);
return array;
}
}
}