-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathProgram.cs
More file actions
423 lines (376 loc) · 18.9 KB
/
Copy pathProgram.cs
File metadata and controls
423 lines (376 loc) · 18.9 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
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using UnityDataTools.Analyzer;
using UnityDataTools.Archive;
using UnityDataTools.FileSystem;
using UnityDataTools.ReferenceFinder;
using UnityDataTools.SerializedFile;
using UnityDataTools.TextDumper;
namespace UnityDataTools.UnityDataTool;
public static class Program
{
const string TypeTreeDataDescription = "Path to an external TypeTree data file to load before processing bundles";
public static async Task<int> Main(string[] args)
{
UnityFileSystem.Init();
var rootCommand = new RootCommand(BuildRootDescription());
rootCommand.AddCommand(BuildAnalyzeCommand());
rootCommand.AddCommand(BuildFindRefsCommand());
rootCommand.AddCommand(BuildDumpCommand());
rootCommand.AddCommand(BuildArchiveCommand());
rootCommand.AddCommand(BuildSerializedFileCommand());
var r = await rootCommand.InvokeAsync(args);
UnityFileSystem.Cleanup();
return r;
}
static string BuildRootDescription()
{
var version = Assembly.GetExecutingAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? Assembly.GetExecutingAssembly().GetName().Version?.ToString()
?? "unknown";
// Strip the SourceLink build-metadata suffix (e.g. "1.3.5+<commit>").
var plusIndex = version.IndexOf('+');
if (plusIndex >= 0)
version = version.Substring(0, plusIndex);
// Release builds have a bare version (e.g. "2.1.0") matching a git tag, so their
// documentation link can be pinned to the matching docs. Dev builds carry a pre-release
// suffix (e.g. "2.2.0-dev") and link to the latest docs on main instead.
var docsRef = version.Contains('-') || version == "unknown" ? "main" : $"v{version}";
var documentationUrl =
$"https://github.com/Unity-Technologies/UnityDataTools/blob/{docsRef}/Documentation/unitydatatool.md";
return
"UnityDataTool inspects and analyzes Unity file formats, for example the content formats for AssetBundles, " +
"Player and content directory builds. It can build a database of the Unity objects and their " +
"references for analysis, dump objects as text, and examine " +
"archive and SerializedFile internals.\n\n" +
"Run 'UnityDataTool [command] --help' for detailed help on a specific command.\n\n" +
$"Documentation: {documentationUrl}\n" +
$"Version: {version}";
}
static Command BuildAnalyzeCommand()
{
var pathArg = new Argument<FileSystemInfo[]>("paths",
"One or more files or directories to analyze. Directories are scanned (see --search-pattern and --no-recurse); "
+ "files are analyzed directly. Combine paths to include files from multiple locations, e.g. a build output "
+ "directory and a build report file.")
{
Arity = ArgumentArity.OneOrMore
}.ExistingOnly();
var oOpt = new Option<string>(aliases: new[] { "--output-file", "-o" }, description: "Filename of the output database", getDefaultValue: () => "database.db");
var sOpt = new Option<bool>(aliases: new[] { "--skip-references", "-s" }, description: "Do not extract references");
var scOpt = new Option<bool>(aliases: new[] { "--skip-crc" }, description: "Skip CRC checksum calculation");
var rOpt = new Option<bool>(aliases: new[] { "--extract-references", "-r" }) { IsHidden = true };
var pOpt = new Option<string>(aliases: new[] { "--search-pattern", "-p" }, description: "File search pattern applied when scanning directories", getDefaultValue: () => "*");
var vOpt = new Option<bool>(aliases: new[] { "--verbose", "-v" }, description: "Verbose output");
var recurseOpt = new Option<bool>(aliases: new[] { "--no-recurse" }, description: "Do not analyze contents of subdirectories inside scanned directories");
var dOpt = new Option<FileInfo>(aliases: new[] { "--typetree-data", "-d" }, description: TypeTreeDataDescription);
var bhOpt = new Option<DirectoryInfo>(aliases: new[] { "--build-history" },
description: "Build history folder of the project (e.g. Library/BuildHistory). The build folder matching the "
+ "analyzed build is located automatically and its ContentLayout.json and build report are included in the analysis.")
.ExistingOnly();
var analyzeCommand = new Command("analyze", "Analyze AssetBundles, SerializedFiles and build reports into a database.")
{
pathArg,
oOpt,
sOpt,
scOpt,
rOpt,
pOpt,
vOpt,
recurseOpt,
dOpt,
bhOpt
};
analyzeCommand.AddAlias("analyse");
// Bound via InvocationContext because the option count exceeds the strongly-typed
// SetHandler overloads.
analyzeCommand.SetHandler((InvocationContext context) =>
{
var d = context.ParseResult.GetValueForOption(dOpt);
var ttResult = LoadTypeTreeDataFile(d);
if (ttResult != 0)
{
context.ExitCode = ttResult;
return;
}
context.ExitCode = HandleAnalyze(
context.ParseResult.GetValueForArgument(pathArg),
context.ParseResult.GetValueForOption(oOpt),
context.ParseResult.GetValueForOption(sOpt),
context.ParseResult.GetValueForOption(scOpt),
context.ParseResult.GetValueForOption(rOpt),
context.ParseResult.GetValueForOption(pOpt),
context.ParseResult.GetValueForOption(vOpt),
context.ParseResult.GetValueForOption(recurseOpt),
context.ParseResult.GetValueForOption(bhOpt));
});
return analyzeCommand;
}
static Command BuildFindRefsCommand()
{
var pathArg = new Argument<FileInfo>("databasePath", "The path to the database generated by the 'analyze' command").ExistingOnly();
var oOpt = new Option<string>(aliases: new[] { "--output-file", "-o" }, description: "Output file", getDefaultValue: () => "references.txt");
var iOpt = new Option<long?>(aliases: new[] { "--object-id", "-i" }, description: "Object id ('id' column in the database)");
var nOpt = new Option<string>(aliases: new[] { "--object-name", "-n" }, description: "Object name");
var tOpt = new Option<string>(aliases: new[] { "--object-type", "-t" }, description: "Optional object type when searching by name");
var aOpt = new Option<bool>(aliases: new[] { "--find-all", "-a" }, description: "Find all reference chains originating from the same asset (instead of only one), can be very slow");
var stdoutOpt = new Option<bool>(aliases: new[] { "--stdout" }, description: "Write the reference chains to stdout instead of a file.");
var findRefsCommand = new Command("find-refs",
"Find reference chains to specified object(s) (experimental: results are incomplete for some build types).")
{
pathArg,
oOpt,
aOpt,
nOpt,
tOpt,
iOpt,
stdoutOpt,
};
findRefsCommand.AddValidator(commandResult =>
{
var stdoutResult = commandResult.FindResultFor(stdoutOpt);
var oResult = commandResult.FindResultFor(oOpt);
bool stdoutSet = stdoutResult is { IsImplicit: false };
bool oExplicit = oResult is { IsImplicit: false };
if (stdoutSet && oExplicit)
{
commandResult.ErrorMessage = "--stdout and -o/--output-file are mutually exclusive.";
}
});
findRefsCommand.SetHandler(
(FileInfo fi, string o, long? i, string n, string t, bool a, bool toStdout) => Task.FromResult(HandleFindReferences(fi, o, i, n, t, a, toStdout)),
pathArg, oOpt, iOpt, nOpt, tOpt, aOpt, stdoutOpt);
return findRefsCommand;
}
static Command BuildDumpCommand()
{
var pathArg = new Argument<FileInfo>("filename", "The path of the file to dump").ExistingOnly();
var fOpt = new Option<TextDumperTool.DumpFormat>(aliases: new[] { "--output-format", "-f" }, description: "Output format", getDefaultValue: () => TextDumperTool.DumpFormat.Text);
var aOpt = new Option<bool>(aliases: new[] { "--show-large-arrays", "-a" }, description: "Dump the full content of large arrays of basic data types, instead of summarizing them with a hash");
var xOpt = new Option<bool>(aliases: new[] { "--hexfloat", "-x" }, description: "Print the bit-exact hexadecimal representation after each float and double value, e.g. 0.25(0x3e800000)");
// Former option, kept so that existing scripts don't break. Ignored because skipping
// large arrays (with a hash) is now the default behavior.
var sOpt = new Option<bool>(aliases: new[] { "--skip-large-arrays", "-s" }) { IsHidden = true };
var oOpt = new Option<DirectoryInfo>(aliases: new[] { "--output-path", "-o" }, description: "Output folder", getDefaultValue: () => new DirectoryInfo(Environment.CurrentDirectory));
var objectIdOpt = new Option<long>(aliases: new[] { "--objectid", "-i" }, () => 0, "Only dump the object with this signed 64-bit id (default: 0, dump all objects)");
var typeOpt = new Option<string>(aliases: new[] { "--type", "-t" }, description: "Filter by object type (ClassID number or type name)");
var stdoutOpt = new Option<bool>(aliases: new[] { "--stdout" }, description: "Write the dump to stdout instead of a file. Refused for archives that contain more than one SerializedFile.");
var dOpt = new Option<FileInfo>(aliases: new[] { "--typetree-data", "-d" }, description: TypeTreeDataDescription);
var dumpCommand = new Command("dump",
"Dump serialized objects from a SerializedFile as text.\nFor an archive, dumps the objects from each SerializedFile inside;\nother archive content is ignored (use archive extract for that).")
{
pathArg,
fOpt,
aOpt,
xOpt,
sOpt,
oOpt,
objectIdOpt,
typeOpt,
dOpt,
stdoutOpt,
};
dumpCommand.AddValidator(commandResult =>
{
var stdoutResult = commandResult.FindResultFor(stdoutOpt);
var oResult = commandResult.FindResultFor(oOpt);
bool stdoutSet = stdoutResult is { IsImplicit: false };
bool oExplicit = oResult is { IsImplicit: false };
if (stdoutSet && oExplicit)
{
commandResult.ErrorMessage = "--stdout and -o/--output-path are mutually exclusive.";
}
});
dumpCommand.SetHandler(
(FileInfo fi, TextDumperTool.DumpFormat f, bool a, bool x, DirectoryInfo o, long objectId, string type, FileInfo d, bool toStdout) =>
{
var ttResult = LoadTypeTreeDataFile(d);
if (ttResult != 0) return Task.FromResult(ttResult);
var options = new TextDumperTool.DumpOptions
{
Format = f,
Path = fi.FullName,
OutputPath = o.FullName,
ShowLargeArrays = a,
HexFloat = x,
ObjectId = objectId,
TypeFilter = type,
ToStdout = toStdout,
};
return Task.FromResult(HandleDump(options));
},
pathArg, fOpt, aOpt, xOpt, oOpt, objectIdOpt, typeOpt, dOpt, stdoutOpt);
return dumpCommand;
}
static Command BuildArchiveCommand()
{
var pathArg = new Argument<FileInfo>("filename", "The path of the archive file").ExistingOnly();
var oOpt = new Option<DirectoryInfo>(aliases: new[] { "--output-path", "-o" }, description: "Output directory of the extracted archive", getDefaultValue: () => new DirectoryInfo("archive"));
var filterOpt = new Option<string>(aliases: new[] { "--filter" }, description: "Case-insensitive substring filter on file paths inside the archive");
var extractArchiveCommand = new Command("extract", "Extract an AssetBundle or .data file.")
{
pathArg,
oOpt,
filterOpt,
};
extractArchiveCommand.SetHandler(
(FileInfo fi, DirectoryInfo o, string filter) => Task.FromResult(ArchiveTool.ExtractContent(fi, o, filter)),
pathArg, oOpt, filterOpt);
var fOpt = new Option<ArchiveTool.OutputFormat>(aliases: new[] { "--format", "-f" }, description: "Output format", getDefaultValue: () => ArchiveTool.OutputFormat.Text);
var listArchiveCommand = new Command("list", "List the contents of an AssetBundle or .data file.")
{
pathArg,
fOpt,
};
listArchiveCommand.SetHandler(
(FileInfo fi, ArchiveTool.OutputFormat f) => Task.FromResult(ArchiveTool.ListContent(fi, f)),
pathArg, fOpt);
var headerArchiveCommand = new Command("header", "Display the header of a Unity Archive file.")
{
pathArg,
fOpt,
};
headerArchiveCommand.SetHandler(
(FileInfo fi, ArchiveTool.OutputFormat f) => Task.FromResult(ArchiveTool.PrintHeader(fi, f)),
pathArg, fOpt);
var blocksArchiveCommand = new Command("blocks", "Display the block list of a Unity Archive file.")
{
pathArg,
fOpt,
};
blocksArchiveCommand.SetHandler(
(FileInfo fi, ArchiveTool.OutputFormat f) => Task.FromResult(ArchiveTool.ListBlocks(fi, f)),
pathArg, fOpt);
var infoArchiveCommand = new Command("info", "Display a high-level summary of a Unity Archive file.")
{
pathArg,
fOpt,
};
infoArchiveCommand.SetHandler(
(FileInfo fi, ArchiveTool.OutputFormat f) => Task.FromResult(ArchiveTool.PrintSummary(fi, f)),
pathArg, fOpt);
return new Command("archive", "Inspect or extract the contents of a Unity archive (AssetBundle or web platform .data file).")
{
extractArchiveCommand,
listArchiveCommand,
headerArchiveCommand,
blocksArchiveCommand,
infoArchiveCommand,
};
}
static Command BuildSerializedFileCommand()
{
var pathArg = new Argument<FileInfo>("filename", "The path of the SerializedFile").ExistingOnly();
var fOpt = new Option<SerializedFileTool.OutputFormat>(aliases: new[] { "--format", "-f" }, description: "Output format", getDefaultValue: () => SerializedFileTool.OutputFormat.Text);
var externalRefsCommand = new Command("externalrefs", "List external file references in a SerializedFile.")
{
pathArg,
fOpt,
};
externalRefsCommand.SetHandler(
(FileInfo fi, SerializedFileTool.OutputFormat f) => Task.FromResult(SerializedFileTool.ListExternalRefs(fi, f)),
pathArg, fOpt);
var objectListCommand = new Command("objectlist", "List all objects in a SerializedFile.")
{
pathArg,
fOpt,
};
objectListCommand.SetHandler(
(FileInfo fi, SerializedFileTool.OutputFormat f) => Task.FromResult(SerializedFileTool.ListObjects(fi, f)),
pathArg, fOpt);
var headerCommand = new Command("header", "Show SerializedFile header information.")
{
pathArg,
fOpt,
};
headerCommand.SetHandler(
(FileInfo fi, SerializedFileTool.OutputFormat f) => Task.FromResult(SerializedFileTool.PrintHeader(fi, f)),
pathArg, fOpt);
var metadataCommand = new Command("metadata", "Show information from the metadata section of the SerializedFile (use `-f Json` for detailed information).")
{
pathArg,
fOpt,
};
metadataCommand.SetHandler(
(FileInfo fi, SerializedFileTool.OutputFormat f) => Task.FromResult(SerializedFileTool.PrintMetadata(fi, f)),
pathArg, fOpt);
var serializedFileCommand = new Command("serialized-file", "Inspect a SerializedFile (scene, assets, etc.).")
{
externalRefsCommand,
objectListCommand,
headerCommand,
metadataCommand,
};
serializedFileCommand.AddAlias("sf");
return serializedFileCommand;
}
static int LoadTypeTreeDataFile(FileInfo typeTreeDataFile)
{
if (typeTreeDataFile == null)
return 0;
try
{
UnityFileSystem.AddTypeTreeSourceFromFile(typeTreeDataFile.FullName);
}
catch (EntryPointNotFoundException)
{
Console.Error.WriteLine("Error: The version of UnityFileSystemApi does not support external TypeTree data files. Please use a version from Unity 6.5 or newer.");
return 1;
}
return 0;
}
static int HandleAnalyze(
FileSystemInfo[] paths,
string outputFile,
bool skipReferences,
bool skipCrc,
bool extractReferences,
string searchPattern,
bool verbose,
bool noRecurse,
DirectoryInfo buildHistory)
{
var analyzer = new AnalyzerTool();
if (extractReferences)
{
Console.WriteLine("WARNING: --extract-references, -r option is deprecated (references are now extracted by default)");
}
return analyzer.Analyze(new AnalyzerTool.AnalyzeOptions
{
Paths = Array.ConvertAll(paths, p => p.FullName),
DatabaseName = outputFile,
SearchPattern = searchPattern,
SkipReferences = skipReferences,
SkipCrc = skipCrc,
Verbose = verbose,
NoRecursion = noRecurse,
BuildHistoryPath = buildHistory?.FullName,
});
}
static int HandleFindReferences(FileInfo databasePath, string outputFile, long? objectId, string objectName, string objectType, bool findAll, bool toStdout)
{
var finder = new ReferenceFinderTool();
if ((objectId != null && objectName != null) || (objectId == null && objectName == null))
{
Console.Error.WriteLine("A value must be provided for either --object-id or --object-name.");
return 1;
}
if (objectId != null)
{
return finder.FindReferences(objectId.Value, databasePath.FullName, outputFile, findAll, toStdout);
}
else
{
return finder.FindReferences(objectName, objectType, databasePath.FullName, outputFile, findAll, toStdout);
}
}
static int HandleDump(TextDumperTool.DumpOptions options)
{
return new TextDumperTool().Dump(options);
}
}