-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathAssetsManager.cs
More file actions
564 lines (497 loc) · 21.3 KB
/
AssetsManager.cs
File metadata and controls
564 lines (497 loc) · 21.3 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace AssetsTools.NET.Extra
{
public class AssetsManager
{
public bool updateAfterLoad = true;
public bool useTemplateFieldCache = false;
public bool useThreadSafeReader = true;
public ClassDatabasePackage classPackage;
public ClassDatabaseFile classFile;
public List<AssetsFileInstance> files = new List<AssetsFileInstance>();
public List<BundleFileInstance> bundles = new List<BundleFileInstance>();
private Dictionary<uint, AssetTypeTemplateField> templateFieldCache = new Dictionary<uint, AssetTypeTemplateField>();
private Dictionary<string, AssetTypeTemplateField> monoTemplateFieldCache = new Dictionary<string, AssetTypeTemplateField>();
#region assets files
public AssetsFileInstance LoadAssetsFile(Stream stream, string path, bool loadDeps, string root = "", BundleFileInstance bunInst = null)
{
AssetsFileInstance instance;
int index = files.FindIndex(f => f.path.ToLower() == Path.GetFullPath(path).ToLower());
if (index == -1)
{
var reader = AssetsFileReaderHelper.createReader(stream, useThreadSafeReader, true);
instance = new AssetsFileInstance(reader, path, root);
instance.parentBundle = bunInst;
files.Add(instance);
}
else
{
instance = files[index];
}
if (loadDeps)
{
if (bunInst == null)
LoadDependencies(instance, Path.GetDirectoryName(path));
else
LoadBundleDependencies(instance, bunInst, Path.GetDirectoryName(path));
}
if (updateAfterLoad)
UpdateDependencies(instance);
return instance;
}
public AssetsFileInstance LoadAssetsFile(FileStream stream, bool loadDeps, string root = "")
{
return LoadAssetsFile(stream, stream.Name, loadDeps, root);
}
public AssetsFileInstance LoadAssetsFile(string path, bool loadDeps, string root = "")
{
return LoadAssetsFile(File.OpenRead(path), loadDeps, root);
}
public bool UnloadAssetsFile(string path)
{
int index = files.FindIndex(f => f.path.ToLower() == Path.GetFullPath(path).ToLower());
if (index != -1)
{
AssetsFileInstance assetsInst = files[index];
assetsInst.file.Close();
files.Remove(assetsInst);
return true;
}
return false;
}
public bool UnloadAllAssetsFiles(bool clearCache = false)
{
if (clearCache)
{
templateFieldCache.Clear();
monoTemplateFieldCache.Clear();
}
if (files.Count != 0)
{
foreach (AssetsFileInstance assetsInst in files)
{
assetsInst.file.Close();
}
files.Clear();
return true;
}
return false;
}
public void UnloadAll(bool unloadClassData = false)
{
UnloadAllAssetsFiles(true);
UnloadAllBundleFiles();
if (unloadClassData)
{
classPackage = null;
classFile = null;
}
}
#endregion
#region bundle files
public BundleFileInstance LoadBundleFile(Stream stream, string path, bool unpackIfPacked = true)
{
BundleFileInstance bunInst;
int index = bundles.FindIndex(f => f.path.ToLower() == path.ToLower());
if (index == -1)
{
bunInst = new BundleFileInstance(stream, path, "", unpackIfPacked);
bundles.Add(bunInst);
}
else
{
bunInst = bundles[index];
}
return bunInst;
}
public BundleFileInstance LoadBundleFile(FileStream stream, bool unpackIfPacked = true)
{
return LoadBundleFile(stream, Path.GetFullPath(stream.Name), unpackIfPacked);
}
public BundleFileInstance LoadBundleFile(string path, bool unpackIfPacked = true)
{
return LoadBundleFile(File.OpenRead(path), unpackIfPacked);
}
public bool UnloadBundleFile(string path)
{
int index = bundles.FindIndex(f => f.path.ToLower() == Path.GetFullPath(path).ToLower());
if (index != -1)
{
BundleFileInstance bunInst = bundles[index];
bunInst.file.Close();
foreach (AssetsFileInstance assetsInst in bunInst.loadedAssetsFiles)
{
assetsInst.file.Close();
}
bundles.Remove(bunInst);
return true;
}
return false;
}
public bool UnloadAllBundleFiles()
{
if (bundles.Count != 0)
{
foreach (BundleFileInstance bunInst in bundles)
{
bunInst.file.Close();
foreach (AssetsFileInstance assetsInst in bunInst.loadedAssetsFiles)
{
assetsInst.file.Close();
}
}
bundles.Clear();
return true;
}
return false;
}
public AssetsFileInstance LoadAssetsFileFromBundle(BundleFileInstance bunInst, int index, bool loadDeps = false)
{
string assetMemPath = Path.Combine(bunInst.path, bunInst.file.GetFileName(index));
int listIndex = files.FindIndex(f => f.path.ToLower() == Path.GetFullPath(assetMemPath).ToLower());
if (listIndex == -1)
{
if (bunInst.file.IsAssetsFile(index))
{
bunInst.file.GetFileRange(index, out long offset, out long length);
SegmentStream stream = new SegmentStream(bunInst.BundleStream, offset, length);
AssetsFileInstance assetsInst = LoadAssetsFile(stream, assetMemPath, loadDeps, bunInst: bunInst);
bunInst.loadedAssetsFiles.Add(assetsInst);
return assetsInst;
}
}
else
{
return files[listIndex];
}
return null;
}
public AssetsFileInstance LoadAssetsFileFromBundle(BundleFileInstance bunInst, string name, bool loadDeps = false)
{
int index = bunInst.file.GetFileIndex(name);
if (index < 0)
return null;
return LoadAssetsFileFromBundle(bunInst, index, loadDeps);
}
#endregion
#region dependencies
public void UpdateDependencies(AssetsFileInstance ofFile)
{
var depList = ofFile.file.dependencies;
for (int i = 0; i < depList.dependencyCount; i++)
{
AssetsFileDependency dep = depList.dependencies[i];
int index = files.FindIndex(f => Path.GetFileName(dep.assetPath.ToLower()) == Path.GetFileName(f.path.ToLower()));
if (index != -1)
{
ofFile.dependencies[i] = files[index];
}
}
}
public void UpdateDependencies()
{
foreach (AssetsFileInstance file in files)
{
UpdateDependencies(file);
}
}
public void LoadDependencies(AssetsFileInstance ofFile, string path)
{
for (int i = 0; i < ofFile.dependencies.Count; i++)
{
string depPath = ofFile.file.dependencies.dependencies[i].assetPath;
if (depPath == string.Empty)
{
continue;
}
if (files.FindIndex(f => Path.GetFileName(f.path).ToLower() == Path.GetFileName(depPath).ToLower()) == -1)
{
string absPath = Path.Combine(path, depPath);
string localAbsPath = Path.Combine(path, Path.GetFileName(depPath));
if (File.Exists(absPath))
{
LoadAssetsFile(File.OpenRead(absPath), true);
}
else if (File.Exists(localAbsPath))
{
LoadAssetsFile(File.OpenRead(localAbsPath), true);
}
}
}
}
public void LoadBundleDependencies(AssetsFileInstance ofFile, BundleFileInstance ofBundle, string path)
{
for (int i = 0; i < ofFile.dependencies.Count; i++)
{
string depPath = ofFile.file.dependencies.dependencies[i].assetPath;
if (files.FindIndex(f => Path.GetFileName(f.path).ToLower() == Path.GetFileName(depPath).ToLower()) == -1)
{
string bunPath = Path.GetFileName(depPath);
int bunIndex = Array.FindIndex(ofBundle.file.bundleInf6.dirInf, d => Path.GetFileName(d.name) == bunPath);
//by default, the directory of an assets file is the bundle's file path (somepath\bundle.unity3d\file.assets)
//we back out again to get the directory the bundle is in
string noBunPath = Path.Combine(path, "..");
string nbAbsPath = Path.Combine(noBunPath, depPath);
string nbLocalAbsPath = Path.Combine(noBunPath, Path.GetFileName(depPath));
//if the user chose to set the path to the directory the bundle is in,
//we need to check for that as well
string absPath = Path.Combine(path, depPath);
string localAbsPath = Path.Combine(path, Path.GetFileName(depPath));
if (bunIndex != -1)
{
LoadAssetsFileFromBundle(ofBundle, bunIndex, true);
}
else if (File.Exists(absPath))
{
LoadAssetsFile(File.OpenRead(absPath), true);
}
else if (File.Exists(localAbsPath))
{
LoadAssetsFile(File.OpenRead(localAbsPath), true);
}
else if (File.Exists(nbAbsPath))
{
LoadAssetsFile(File.OpenRead(nbAbsPath), true);
}
else if (File.Exists(nbLocalAbsPath))
{
LoadAssetsFile(File.OpenRead(nbLocalAbsPath), true);
}
}
}
}
#endregion
#region asset resolving
public AssetExternal GetExtAsset(AssetsFileInstance relativeTo, int fileId, long pathId, bool onlyGetInfo = false, bool forceFromCldb = false)
{
AssetExternal ext = new AssetExternal
{
info = null,
instance = null,
file = null
};
if (fileId == 0 && pathId == 0)
{
return ext;
}
else if (fileId != 0)
{
AssetsFileInstance dep = relativeTo.GetDependency(this, fileId - 1);
if (dep == null)
return ext;
ext.file = dep;
ext.info = dep.table.GetAssetInfo(pathId);
if (ext.info == null)
return ext;
if (!onlyGetInfo)
ext.instance = GetTypeInstance(dep.file, ext.info, forceFromCldb);
else
ext.instance = null;
return ext;
}
else
{
ext.file = relativeTo;
ext.info = relativeTo.table.GetAssetInfo(pathId);
if (ext.info == null)
return ext;
if (!onlyGetInfo)
ext.instance = GetTypeInstance(relativeTo.file, ext.info, forceFromCldb);
else
ext.instance = null;
return ext;
}
}
public AssetExternal GetExtAsset(AssetsFileInstance relativeTo, AssetTypeValueField atvf, bool onlyGetInfo = false, bool forceFromCldb = false)
{
int fileId = atvf.Get("m_FileID").GetValue().AsInt();
long pathId = atvf.Get("m_PathID").GetValue().AsInt64();
return GetExtAsset(relativeTo, fileId, pathId, onlyGetInfo, forceFromCldb);
}
public AssetTypeInstance GetTypeInstance(AssetsFileInstance inst, AssetFileInfoEx info, bool forceFromCldb = false)
{
return GetTypeInstance(inst.file, info, forceFromCldb);
}
public AssetTypeInstance GetTypeInstance(AssetsFile file, AssetFileInfoEx info, bool forceFromCldb = false)
{
return new AssetTypeInstance(GetTemplateBaseField(file, info, forceFromCldb), file.reader, info.absoluteFilePos);
}
[Obsolete("Renamed to GetTypeInstance")]
public AssetTypeInstance GetATI(AssetsFile file, AssetFileInfoEx info, bool forceFromCldb = false)
{
return GetTypeInstance(file, info, forceFromCldb);
}
#endregion
#region deserialization
public AssetTypeTemplateField GetTemplateBaseField(AssetsFile file, AssetFileInfoEx info, bool forceFromCldb = false)
{
ushort scriptIndex = AssetHelper.GetScriptIndex(file, info);
uint fixedId = AssetHelper.FixAudioID(info.curFileType);
bool hasTypeTree = file.typeTree.hasTypeTree;
AssetTypeTemplateField baseField;
if (useTemplateFieldCache && templateFieldCache.ContainsKey(fixedId))
{
baseField = templateFieldCache[fixedId];
}
else
{
baseField = new AssetTypeTemplateField();
if (hasTypeTree && !forceFromCldb)
{
baseField.From0D(AssetHelper.FindTypeTreeTypeByID(file.typeTree, fixedId, scriptIndex), 0);
}
else
{
baseField.FromClassDatabase(classFile, AssetHelper.FindAssetClassByID(classFile, fixedId), 0);
}
}
return baseField;
}
public AssetTypeValueField GetMonoBaseFieldCached(AssetsFileInstance inst, AssetFileInfoEx info, string managedPath)
{
AssetsFile file = inst.file;
ushort scriptIndex = AssetHelper.GetScriptIndex(file, info);
if (scriptIndex == 0xFFFF)
return null;
string scriptName;
if (!inst.monoIdToName.ContainsKey(scriptIndex))
{
AssetTypeInstance scriptAti = GetExtAsset(inst, GetTypeInstance(inst.file, info).GetBaseField().Get("m_Script")).instance;
//couldn't find asset
if (scriptAti == null)
return null;
scriptName = scriptAti.GetBaseField().Get("m_Name").GetValue().AsString();
string scriptNamespace = scriptAti.GetBaseField().Get("m_Namespace").GetValue().AsString();
string assemblyName = scriptAti.GetBaseField().Get("m_AssemblyName").GetValue().AsString();
if (scriptNamespace == string.Empty)
{
scriptNamespace = "-";
}
scriptName = $"{assemblyName}.{scriptNamespace}.{scriptName}";
inst.monoIdToName[scriptIndex] = scriptName;
}
else
{
scriptName = inst.monoIdToName[scriptIndex];
}
if (monoTemplateFieldCache.ContainsKey(scriptName))
{
AssetTypeTemplateField baseTemplateField = monoTemplateFieldCache[scriptName];
AssetTypeInstance baseAti = new AssetTypeInstance(baseTemplateField, file.reader, info.absoluteFilePos);
return baseAti.GetBaseField();
}
else
{
AssetTypeValueField baseValueField = MonoDeserializer.GetMonoBaseField(this, inst, info, managedPath);
monoTemplateFieldCache[scriptName] = baseValueField.templateField;
return baseValueField;
}
}
#endregion
#region class database
public ClassDatabaseFile LoadClassDatabase(Stream stream)
{
classFile = new ClassDatabaseFile();
classFile.Read(new AssetsFileReader(stream));
return classFile;
}
public ClassDatabaseFile LoadClassDatabase(string path)
{
return LoadClassDatabase(File.OpenRead(path));
}
public ClassDatabaseFile LoadClassDatabaseFromPackage(string version, bool specific = false)
{
if (classPackage == null)
throw new Exception("No class package loaded!");
if (specific)
{
if (!version.StartsWith("U"))
version = "U" + version;
int index = classPackage.header.files.FindIndex(f => f.name == version);
if (index == -1)
return null;
classFile = classPackage.files[index];
return classFile;
}
else
{
List<ClassDatabaseFile> matchingFiles = new List<ClassDatabaseFile>();
List<UnityVersion> matchingVersions = new List<UnityVersion>();
if (version.StartsWith("U"))
version = version.Substring(1);
UnityVersion versionParsed = new UnityVersion(version);
for (int i = 0; i < classPackage.files.Count; i++)
{
ClassDatabaseFile file = classPackage.files[i];
for (int j = 0; j < file.header.unityVersions.Length; j++)
{
string unityVersion = file.header.unityVersions[j];
if (version == unityVersion)
{
classFile = file;
return classFile;
}
else if (WildcardMatches(version, unityVersion))
{
string fullUnityVersion = unityVersion;
if (fullUnityVersion.EndsWith("*"))
fullUnityVersion = file.header.unityVersions[1 - j];
matchingFiles.Add(file);
matchingVersions.Add(new UnityVersion(fullUnityVersion));
}
}
}
if (matchingFiles.Count == 1)
{
classFile = matchingFiles[0];
return classFile;
}
else if (matchingFiles.Count > 0)
{
int selectedIndex = 0;
int patchNumToMatch = versionParsed.patch;
int highestMatchingPatchNum = matchingVersions[selectedIndex].patch;
for (int i = 1; i < selectedIndex; i++)
{
int thisPatchNum = matchingVersions[selectedIndex].patch;
if (thisPatchNum > highestMatchingPatchNum && thisPatchNum <= patchNumToMatch)
{
selectedIndex = i;
highestMatchingPatchNum = thisPatchNum;
}
}
classFile = matchingFiles[selectedIndex];
return classFile;
}
return null;
}
}
private bool WildcardMatches(string test, string pattern)
{
return Regex.IsMatch(test, "^" + Regex.Escape(pattern).Replace("\\*", ".*") + "$");
}
public ClassDatabasePackage LoadClassPackage(Stream stream)
{
classPackage = new ClassDatabasePackage();
classPackage.Read(new AssetsFileReader(stream));
return classPackage;
}
public ClassDatabasePackage LoadClassPackage(string path)
{
return LoadClassPackage(File.OpenRead(path));
}
#endregion
}
public struct AssetExternal
{
public AssetFileInfoEx info;
public AssetTypeInstance instance;
public AssetsFileInstance file;
}
}