-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathAnalyzerTool.cs
More file actions
207 lines (184 loc) · 7.29 KB
/
Copy pathAnalyzerTool.cs
File metadata and controls
207 lines (184 loc) · 7.29 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityDataTools.Analyzer.SQLite.Handlers;
using UnityDataTools.Analyzer.SQLite.Parsers;
using UnityDataTools.Analyzer.SQLite.Writers;
using UnityDataTools.Models;
using UnityDataTools.FileSystem;
namespace UnityDataTools.Analyzer;
public class AnalyzerTool
{
AnalyzeOptions m_Options;
public List<ISQLiteFileParser> parsers = new List<ISQLiteFileParser>()
{
new AddressablesBuildLayoutParser(),
new SerializedFileParser(),
};
public class AnalyzeOptions
{
// Each entry is a file or a directory. Directories are scanned using SearchPattern and
// NoRecursion; files are always included regardless of SearchPattern.
public IReadOnlyList<string> Paths { get; init; }
public string DatabaseName { get; init; }
public string SearchPattern { get; init; } = "*";
public bool SkipReferences { get; init; }
public bool SkipCrc { get; init; }
public bool Verbose { get; init; }
public bool NoRecursion { get; init; }
}
public int Analyze(AnalyzeOptions options)
{
m_Options = options;
using SQLiteWriter writer = new(m_Options.DatabaseName);
try
{
writer.Begin();
foreach (var parser in parsers)
{
parser.Verbose = m_Options.Verbose;
parser.SkipReferences = m_Options.SkipReferences;
parser.SkipCrc = m_Options.SkipCrc;
parser.Init(writer.Connection);
}
}
catch (Exception e)
{
Console.Error.WriteLine($"Error creating database: {e.Message}");
return 1;
}
var timer = new Stopwatch();
timer.Start();
var files = CollectFiles();
int countFailures = 0;
int countSuccess = 0;
int countIgnored = 0;
int countNoTypeTrees = 0;
int i = 1;
foreach (var (file, displayRoot) in files)
{
var relativePath = Path.GetRelativePath(displayRoot, file);
bool foundParser = false;
foreach (var parser in parsers)
{
if (parser.CanParse(file))
{
foundParser = true;
try
{
parser.Parse(file);
ReportProgress(relativePath, i, files.Count);
countSuccess++;
}
catch (SerializedFileOpenException e) when (e.MissingTypeTrees)
{
// The file has no TypeTrees and was rejected before opening. This is an
// expected, distinct outcome — reported and counted separately so a large
// run can tell these apart from genuine failures.
EraseProgressLine();
Console.Error.WriteLine($"Skipped (no TypeTrees): {relativePath}");
countNoTypeTrees++;
}
catch (SerializedFileOpenException)
{
// Expected failure — the file content could not be parsed.
// Don't print a stack trace; it adds no value for this known failure mode.
EraseProgressLine();
Console.Error.WriteLine($"Failed to open: {relativePath}");
countFailures++;
}
catch (Exception e)
{
// Unexpected failure (SQL error, I/O error, bug, etc.) — print full details.
EraseProgressLine();
Console.Error.WriteLine($"Failed to process: {relativePath}");
if (m_Options.Verbose)
{
Console.Error.WriteLine($" Exception: {e.GetType().Name}: {e.Message}");
if (e.InnerException != null)
Console.Error.WriteLine($" Inner: {e.InnerException.Message}");
Console.Error.WriteLine(e.StackTrace);
}
countFailures++;
}
}
}
if (!foundParser)
{
if (m_Options.Verbose)
{
Console.WriteLine();
Console.WriteLine($"Ignoring {relativePath}");
}
countIgnored++;
}
++i;
}
Console.WriteLine();
Console.WriteLine($"Finalizing database. Successfully processed files: {countSuccess}, Failed files: {countFailures}, Files without TypeTrees: {countNoTypeTrees}, Ignored files: {countIgnored}");
writer.End();
foreach (var parser in parsers)
{
parser.Dispose();
}
timer.Stop();
Console.WriteLine();
Console.WriteLine($"Total time: {(timer.Elapsed.TotalMilliseconds / 1000.0):F3} s");
return 0;
}
// Expands the input paths into the concrete files to analyze. Each result pairs the file with the
// root used to render its relative path in progress/error messages: the scanned directory for files
// found by scanning, or the file's own directory for explicitly-named files. Duplicates reached via
// more than one input are analyzed once.
List<(string FullPath, string DisplayRoot)> CollectFiles()
{
var searchOption = m_Options.NoRecursion ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories;
var collected = new List<(string FullPath, string DisplayRoot)>();
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var inputPath in m_Options.Paths)
{
if (Directory.Exists(inputPath))
{
foreach (var file in Directory.GetFiles(inputPath, m_Options.SearchPattern, searchOption))
{
if (seen.Add(Path.GetFullPath(file)))
collected.Add((file, inputPath));
}
}
else if (File.Exists(inputPath))
{
if (seen.Add(Path.GetFullPath(inputPath)))
collected.Add((inputPath, Path.GetDirectoryName(Path.GetFullPath(inputPath))));
}
else
{
Console.Error.WriteLine($"Warning: path not found, skipping: {inputPath}");
}
}
return collected;
}
int m_LastProgressMessageLength = 0;
void ReportProgress(string relativePath, int fileIndex, int cntFiles)
{
var message = $"Processing {fileIndex * 100 / cntFiles}% ({fileIndex}/{cntFiles}) {relativePath}";
if (!m_Options.Verbose)
{
EraseProgressLine();
Console.Write($"\r{message}");
}
else
{
Console.WriteLine();
Console.WriteLine(message);
}
m_LastProgressMessageLength = message.Length;
}
void EraseProgressLine()
{
if (!m_Options.Verbose)
Console.Write($"\r{new string(' ', m_LastProgressMessageLength)}\r");
else
Console.WriteLine();
}
}