-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathAnalyzerTool.cs
122 lines (103 loc) · 4.46 KB
/
AnalyzerTool.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
using System;
using System.Diagnostics;
using System.IO;
using UnityDataTools.Analyzer.SQLite;
using UnityDataTools.FileSystem;
namespace UnityDataTools.Analyzer;
public class AnalyzerTool
{
public int Analyze(string path, string databaseName, string searchPattern, bool skipReferences)
{
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, SearchOption.AllDirectories);
int i = 1;
int lastLength = 0;
foreach (var file in files)
{
// Automatically ignore these annoying OS X style files meta files.
if (Path.GetFileName(file) == ".DS_Store")
continue;
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(path, file);
writer.WriteSerializedFile(relativePath, file, Path.GetDirectoryName(file));
var message = $"Processing {i * 100 / files.Length}% ({i}/{files.Length}) {file}";
Console.Write($"\rProcessing {i * 100 / files.Length}% ({i}/{files.Length}) {file}");
lastLength = message.Length;
}
if (archive != null)
{
try
{
var assetBundleName = Path.GetRelativePath(path, file);
writer.BeginAssetBundle(assetBundleName, new FileInfo(file).Length);
var message = $"Processing {i * 100 / files.Length}% ({i}/{files.Length}) {assetBundleName}";
Console.Write($"\r{message}{new string(' ', Math.Max(0, lastLength - message.Length))}");
lastLength = message.Length;
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)
{
Console.Error.WriteLine($"\rError processing {node.Path} in archive {file}{new string(' ', Math.Max(0, lastLength - message.Length))}");
Console.Error.WriteLine(e);
Console.WriteLine();
}
}
}
}
finally
{
Console.Write($"\r{new string(' ', lastLength)}");
writer.EndAssetBundle();
archive.Dispose();
}
}
}
catch(NotSupportedException) {
Console.Error.WriteLine();
//Console.Error.WriteLine($"File not supported: {file}"); // This is commented out because another codepath will output "failed to load"
}
catch (Exception e)
{
Console.Error.WriteLine();
Console.Error.WriteLine($"Error processing file: {file}");
Console.Write($"{e.GetType()}: ");
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
++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;
}
}