forked from nesrak1/UABEA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
272 lines (243 loc) · 10.7 KB
/
Extensions.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
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
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using AssetsTools.NET.Extra.Decompressors.LZ4;
using Avalonia.Platform.Storage;
using SevenZip.Compression.LZMA;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace UABEAvalonia
{
public static class Extensions
{
// cheap * search check
public static bool WildcardMatches(string test, string pattern, bool caseSensitive = true)
{
RegexOptions options = 0;
if (!caseSensitive)
options |= RegexOptions.IgnoreCase;
return Regex.IsMatch(test, "^" + Regex.Escape(pattern).Replace("\\*", ".*") + "$", options);
}
// codeflow needs work but should be fine for now
public static void GetUABENameFast(AssetWorkspace workspace, AssetContainer cont, bool usePrefix, out string assetName, out string typeName)
{
assetName = "Unnamed asset";
typeName = "Unknown type";
try
{
ClassDatabaseFile cldb = workspace.am.ClassDatabase;
AssetsFile file = cont.FileInstance.file;
AssetsFileReader reader = cont.FileReader;
long filePosition = cont.FilePosition;
int classId = cont.ClassId;
ushort monoId = cont.MonoId;
ClassDatabaseType type = cldb.FindAssetClassByID(classId);
if (file.Metadata.TypeTreeEnabled)
{
TypeTreeType ttType;
if (classId == 0x72 || classId < 0)
ttType = file.Metadata.FindTypeTreeTypeByScriptIndex(monoId);
else
ttType = file.Metadata.FindTypeTreeTypeByID(classId);
if (ttType != null && ttType.Nodes.Count > 0)
{
typeName = ttType.Nodes[0].GetTypeString(ttType.StringBuffer);
if (ttType.Nodes.Count > 1 && ttType.Nodes[1].GetNameString(ttType.StringBuffer) == "m_Name")
{
reader.Position = filePosition;
assetName = reader.ReadCountStringInt32();
if (assetName == "")
assetName = "Unnamed asset";
return;
}
else if (typeName == "GameObject")
{
reader.Position = filePosition;
int size = reader.ReadInt32();
int componentSize = file.Header.Version > 0x10 ? 0x0c : 0x10;
reader.Position += size * componentSize;
reader.Position += 0x04;
assetName = reader.ReadCountStringInt32();
if (usePrefix)
assetName = $"GameObject {assetName}";
return;
}
else if (typeName == "MonoBehaviour")
{
reader.Position = filePosition;
reader.Position += 0x1c;
assetName = reader.ReadCountStringInt32();
if (assetName == "")
{
assetName = GetMonoBehaviourNameFast(workspace, cont);
if (assetName == "")
assetName = "Unnamed asset";
}
return;
}
assetName = "Unnamed asset";
return;
}
}
if (type == null)
{
typeName = $"0x{classId:X8}";
assetName = "Unnamed asset";
return;
}
typeName = cldb.GetString(type.Name);
List<ClassDatabaseTypeNode> cldbNodes = type.GetPreferredNode(false).Children;
if (cldbNodes.Count == 0)
{
assetName = "Unnamed asset";
return;
}
if (cldbNodes.Count > 1 && cldb.GetString(cldbNodes[0].FieldName) == "m_Name")
{
reader.Position = filePosition;
assetName = reader.ReadCountStringInt32();
if (assetName == "")
assetName = "Unnamed asset";
return;
}
else if (typeName == "GameObject")
{
reader.Position = filePosition;
int size = reader.ReadInt32();
int componentSize = file.Header.Version > 0x10 ? 0x0c : 0x10;
reader.Position += size * componentSize;
reader.Position += 0x04;
assetName = reader.ReadCountStringInt32();
if (usePrefix)
assetName = $"GameObject {assetName}";
return;
}
else if (typeName == "MonoBehaviour")
{
reader.Position = filePosition;
reader.Position += 0x1c;
assetName = reader.ReadCountStringInt32();
if (assetName == "")
{
assetName = GetMonoBehaviourNameFast(workspace, cont);
if (assetName == "")
assetName = "Unnamed asset";
}
return;
}
assetName = "Unnamed asset";
}
catch
{
}
}
// not very fast but w/e at least it's stable
public static string GetMonoBehaviourNameFast(AssetWorkspace workspace, AssetContainer cont)
{
try
{
if (cont.ClassId != (uint)AssetClassID.MonoBehaviour && cont.ClassId >= 0)
return string.Empty;
AssetTypeValueField monoBf;
if (cont.HasValueField)
{
monoBf = cont.BaseValueField;
}
else
{
// this is a bad idea. this directly calls am.GetTemplateField
// which won't look for new MonoScripts from UABEA.
// hasTypeTree is set to false to ignore type tree (to prevent
// reading the entire MonoBehaviour if type trees are provided)
// it might be a better idea to just temporarily remove the extra
// fields from a single MonoBehaviour so we don't have to read
// from the cldb (especially so for stripped versions of bundles)
bool wasUsingCache = workspace.am.UseTemplateFieldCache;
workspace.am.UseTemplateFieldCache = false;
AssetTypeTemplateField monoTemp = workspace.GetTemplateField(cont, true, true);
workspace.am.UseTemplateFieldCache = wasUsingCache;
monoBf = monoTemp.MakeValue(cont.FileReader, cont.FilePosition);
}
AssetContainer monoScriptCont = workspace.GetAssetContainer(cont.FileInstance, monoBf["m_Script"], false);
if (monoScriptCont == null)
return string.Empty;
AssetTypeValueField scriptBaseField = monoScriptCont.BaseValueField;
string scriptClassName = scriptBaseField["m_ClassName"].AsString;
return scriptClassName;
}
catch
{
return string.Empty;
}
}
public static string GetAssetsFileDirectory(AssetsFileInstance fileInst)
{
if (fileInst.parentBundle != null)
{
string dir = Path.GetDirectoryName(fileInst.parentBundle.path)!;
// addressables
string? upDir = Path.GetDirectoryName(dir);
string? upDir2 = Path.GetDirectoryName(upDir ?? string.Empty);
if (upDir != null && upDir2 != null)
{
if (Path.GetFileName(upDir) == "aa" && Path.GetFileName(upDir2) == "StreamingAssets")
{
dir = Path.GetDirectoryName(upDir2)!;
}
}
return dir;
}
else
{
string dir = Path.GetDirectoryName(fileInst.path)!;
if (fileInst.name == "unity default resources" || fileInst.name == "unity_builtin_extra")
{
dir = Path.GetDirectoryName(dir)!;
}
return dir;
}
}
// https://stackoverflow.com/a/23182807
public static string ReplaceInvalidPathChars(string filename)
{
return string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));
}
private static string[] byteSizeSuffixes = new string[] { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
public static string GetFormattedByteSize(long size)
{
int log = (int)Math.Log(size, 1024);
double div = log == 0 ? 1 : Math.Pow(1024, log);
double num = size / div;
return $"{num:f2}{byteSizeSuffixes[log]}";
}
public static List<string> GetFilesInDirectory(string path, List<string> extensions)
{
List<string> files = new List<string>();
foreach (string extension in extensions)
{
files.AddRange(Directory.GetFiles(path, "*." + extension));
}
return files;
}
public static string GetFilePathWithoutExtension(string path)
{
return Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
}
public static string[] GetOpenFileDialogFiles(IReadOnlyList<IStorageFile> files)
{
return files.Select(sf => sf.TryGetLocalPath()).Where(p => p != null).ToArray()!;
}
public static string? GetSaveFileDialogFile(IStorageFile? file)
{
return file?.TryGetLocalPath();
}
public static string[] GetOpenFolderDialogFiles(IReadOnlyList<IStorageFolder> folders)
{
return folders.Select(sf => sf.TryGetLocalPath()).Where(p => p != null).ToArray()!;
}
}
}