-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathAnalyzerTool.cs
More file actions
164 lines (145 loc) · 5.18 KB
/
AnalyzerTool.cs
File metadata and controls
164 lines (145 loc) · 5.18 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
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.Parsers.Models;
using UnityDataTools.Analyzer.SQLite.Writers;
using UnityDataTools.BinaryFormat;
using UnityDataTools.FileSystem;
namespace UnityDataTools.Analyzer;
public class AnalyzerTool
{
bool m_Verbose = false;
public List<ISQLiteFileParser> parsers = new List<ISQLiteFileParser>()
{
new AddressablesBuildLayoutParser(),
new SerializedFileParser(),
};
public int Analyze(
string path,
string databaseName,
string searchPattern,
bool skipReferences,
bool verbose,
bool noRecursion)
{
m_Verbose = verbose;
using SQLiteWriter writer = new(databaseName);
try
{
writer.Begin();
foreach (var parser in parsers)
{
parser.Verbose = verbose;
parser.SkipReferences = skipReferences;
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 = Directory.GetFiles(
path,
searchPattern,
noRecursion ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories);
int countFailures = 0;
int countSuccess = 0;
int countIgnored = 0;
int i = 1;
foreach (var file in files)
{
bool foundParser = false;
foreach (var parser in parsers)
{
if (parser.CanParse(file))
{
foundParser = true;
try
{
parser.Parse(file);
ReportProgress(Path.GetRelativePath(path, file), i, files.Length);
countSuccess++;
}
catch (SerializedFileOpenException e)
{
// 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();
var relativePath = Path.GetRelativePath(path, file);
Console.Error.WriteLine($"Failed to open: {relativePath}");
var hint = SerializedFileDetector.GetOpenFailureHint(e.FilePath);
if (hint != null)
Console.Error.WriteLine(hint);
countFailures++;
}
catch (Exception e)
{
// Unexpected failure (SQL error, I/O error, bug, etc.) — print full details.
EraseProgressLine();
var relativePath = Path.GetRelativePath(path, file);
Console.Error.WriteLine($"Failed to process: {relativePath}");
if (m_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_Verbose)
{
var relativePath = Path.GetRelativePath(path, file);
Console.WriteLine();
Console.WriteLine($"Ignoring {relativePath}");
}
countIgnored++;
}
++i;
}
Console.WriteLine();
Console.WriteLine($"Finalizing database. Successfully processed files: {countSuccess}, Failed files: {countFailures}, 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;
}
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)}\r");
else
Console.WriteLine();
}
}