forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyzerTool.cs
More file actions
202 lines (171 loc) · 6.37 KB
/
Copy pathAnalyzerTool.cs
File metadata and controls
202 lines (171 loc) · 6.37 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityDataTools.Analyzer.SQLite;
using UnityDataTools.FileSystem;
namespace UnityDataTools.Analyzer;
public class AnalyzerTool
{
bool m_Verbose = false;
public int Analyze(
string path,
string databaseName,
string searchPattern,
bool skipReferences,
bool verbose,
bool noRecursion)
{
m_Verbose = verbose;
using SQLiteWriter writer = new (databaseName, skipReferences);
try
{
writer.Begin();
}
catch (Exception e)
{
Console.Error.WriteLine($"Error creating database: {e.Message}");
return 1;
}
var timer = new Stopwatch();
timer.Start();
var files = Directory.GetFiles(
path,
searchPattern,
noRecursion ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories);
int i = 1;
foreach (var file in files)
{
if (ShouldIgnoreFile(file))
{
var relativePath = Path.GetRelativePath(path, file);
if (m_Verbose)
{
Console.WriteLine();
Console.WriteLine($"Ignoring {relativePath}");
}
++i;
continue;
}
ProcessFile(file, path, writer, i, files.Length);
++i;
}
Console.WriteLine();
Console.WriteLine("Finalizing database...");
writer.End();
timer.Stop();
Console.WriteLine();
Console.WriteLine($"Total time: {(timer.Elapsed.TotalMilliseconds / 1000.0):F3} s");
return 0;
}
bool ShouldIgnoreFile(string file)
{
// Unfortunately there is no standard extension for AssetBundles, and SerializedFiles often have no extension at all.
// Also there is also no distinctive signature at the start of a SerializedFile to immediately recognize it based on its first bytes.
// This makes it difficult to use the "--search-pattern" argument to only pick those files.
// Hence to reduce noise in UnityDataTool output we filter out files that we have a high confidence are
// NOT SerializedFiles or Unity Archives.
string fileName = Path.GetFileName(file);
string extension = Path.GetExtension(file);
return IgnoredFileNames.Contains(fileName) || IgnoredExtensions.Contains(extension);
}
// These lists are based on expected output files in Player, AssetBundle, Addressables and ECS builds.
// However this is by no means exhaustive.
private static readonly HashSet<string> IgnoredFileNames = new()
{
".DS_Store", "boot.config", "archive_dependencies.bin", "scene_info.bin", "app.info", "link.xml",
"catalog.bin", "catalog.hash"
};
private static readonly HashSet<string> IgnoredExtensions = new()
{
".txt", ".resS", ".resource", ".json", ".dll", ".pdb", ".exe", ".manifest", ".entities", ".entityheader"
};
void ProcessFile(string file, string rootDirectory, SQLiteWriter writer, int fileIndex, int cntFiles)
{
try
{
UnityArchive archive = null;
try
{
archive = UnityFileSystem.MountArchive(file, "archive:" + Path.DirectorySeparatorChar);
}
catch (NotSupportedException)
{
// It wasn't an AssetBundle, try to open the file as a SerializedFile.
var relativePath = Path.GetRelativePath(rootDirectory, file);
writer.WriteSerializedFile(relativePath, file, Path.GetDirectoryName(file));
ReportProgress(relativePath, fileIndex, cntFiles);
}
if (archive != null)
{
try
{
var assetBundleName = Path.GetRelativePath(rootDirectory, file);
writer.BeginAssetBundle(assetBundleName, new FileInfo(file).Length);
ReportProgress(assetBundleName, fileIndex, cntFiles);
foreach (var node in archive.Nodes)
{
if (node.Flags.HasFlag(ArchiveNodeFlags.SerializedFile))
{
try
{
writer.WriteSerializedFile(node.Path, "archive:/" + node.Path, Path.GetDirectoryName(file));
}
catch (Exception e)
{
EraseProgressLine();
Console.Error.WriteLine($"Error processing {node.Path} in archive {file}");
Console.Error.WriteLine(e);
Console.WriteLine();
}
}
}
}
finally
{
writer.EndAssetBundle();
archive.Dispose();
}
}
EraseProgressLine();
}
catch (NotSupportedException)
{
EraseProgressLine();
Console.Error.WriteLine();
//A "failed to load" error will already be logged by the UnityFileSystem library
}
catch (Exception e)
{
EraseProgressLine();
Console.Error.WriteLine();
Console.Error.WriteLine($"Error processing file: {file}");
Console.WriteLine($"{e.GetType()}: {e.Message}");
if (m_Verbose)
Console.WriteLine(e.StackTrace);
}
}
int m_LastProgressMessageLength = 0;
void ReportProgress(string relativePath, int fileIndex, int cntFiles)
{
var message = $"Processing {fileIndex * 100 / cntFiles}% ({fileIndex}/{cntFiles}) {relativePath}";
if (!m_Verbose)
{
EraseProgressLine();
Console.Write($"\r{message}");
}
else
{
Console.WriteLine();
Console.WriteLine(message);
}
m_LastProgressMessageLength = message.Length;
}
void EraseProgressLine()
{
if (!m_Verbose)
Console.Write($"\r{new string(' ', m_LastProgressMessageLength)}");
else
Console.WriteLine();
}
}