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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace XUtliPoolLib
{
public class AssetBundleManager : MonoBehaviour
{
public int BundleCount
{
get
{
return this._bundleCount;
}
}
public AssetBundleDataReader depInfoReader
{
get
{
return this._depInfoReader;
}
}
public bool isCurrentLoading
{
get
{
return this._isCurrentLoading;
}
}
public static Version version = new Version(0, 1, 0);
public static AssetBundleManager Instance;
public static string NAME = "AssetBundleManager";
public static bool enableLog = false;
private const int MAX_REQUEST = 100;
private int _requestRemain = 100;
private Queue<AssetBundleLoader> _requestQueue = new Queue<AssetBundleLoader>();
private List<AssetBundleLoader> _currentLoadQueue = new List<AssetBundleLoader>();
private HashSet<AssetBundleLoader> _nonCompleteLoaderSet = new HashSet<AssetBundleLoader>();
private HashSet<AssetBundleLoader> _thisTimeLoaderSet = new HashSet<AssetBundleLoader>();
private Dictionary<uint, AssetBundleInfo> _loadedAssetBundle = new Dictionary<uint, AssetBundleInfo>();
private Dictionary<uint, AssetBundleLoader> _loaderCache = new Dictionary<uint, AssetBundleLoader>();
private bool _isCurrentLoading;
private Queue<AssetBundleInfo> _requestUnloadBundleQueue = new Queue<AssetBundleInfo>();
private AssetBundleLoadProgress _progress = new AssetBundleLoadProgress();
public AssetBundleManager.LoadProgressHandler onProgress;
public AssetBundlePathResolver pathResolver;
private AssetBundleDataReader _depInfoReader;
private int _bundleCount = 0;
private uint _defaultHash = 0u;
public delegate void LoadAssetCompleteHandler(AssetBundleInfo info, int handlerID);
public delegate void LoaderCompleteHandler(AssetBundleLoader info);
public delegate void LoadProgressHandler(AssetBundleLoadProgress progress);
public AssetBundleManager()
{
AssetBundleManager.Instance = this;
this.pathResolver = new AssetBundlePathResolver();
this._defaultHash = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(0u, "Assets.Resources.");
}
protected void Awake()
{
base.InvokeRepeating("CheckUnusedBundle", 0f, 5f);
}
public void Init()
{
this.RemoveAll();
this.LoadDepInfo();
}
public void Init(byte[] data, Action callback)
{
bool flag = data.Length > 4;
if (flag)
{
XBinaryReader xbinaryReader = XBinaryReader.Get();
xbinaryReader.InitByte(data, 0, 0);
bool flag2 = xbinaryReader.ReadChar() == 'A' && xbinaryReader.ReadChar() == 'B' && xbinaryReader.ReadChar() == 'D';
if (flag2)
{
bool flag3 = xbinaryReader.ReadChar() == 'T';
if (flag3)
{
this._depInfoReader = new AssetBundleDataReader();
}
else
{
this._depInfoReader = new AssetBundleDataBinaryReader();
}
this._depInfoReader.Read(xbinaryReader);
}
XBinaryReader.Return(xbinaryReader, false);
}
bool flag4 = callback != null;
if (flag4)
{
callback();
}
}
private void LoadDepInfo()
{
string path = string.Format("{0}/{1}", this.pathResolver.BundleCacheDir, this.pathResolver.DependFileName);
bool flag = File.Exists(path);
if (flag)
{
this.Init(File.ReadAllBytes(path), null);
}
else
{
TextAsset textAsset = Resources.Load<TextAsset>("dep");
bool flag2 = textAsset != null;
if (flag2)
{
this.Init(textAsset.bytes, null);
Resources.UnloadAsset(textAsset);
}
else
{
XSingleton<XDebug>.singleton.AddErrorLog("depFile not exist!", null, null, null, null, null);
}
}
}
private void OnDestroy()
{
this.RemoveAll();
}
public AssetBundleLoader Load(uint hash, string path, string suffix, string prefix = null, AssetBundleManager.LoadAssetCompleteHandler handler = null, int handlerID = -1)
{
bool flag = this.depInfoReader == null;
AssetBundleLoader result;
if (flag)
{
result = null;
}
else
{
AssetBundleLoader assetBundleLoader = this.CreateLoader(hash, path, suffix, prefix);
bool flag2 = assetBundleLoader == null;
if (flag2)
{
result = null;
}
else
{
assetBundleLoader.loadHandlerID = handlerID;
this._thisTimeLoaderSet.Add(assetBundleLoader);
bool isComplete = assetBundleLoader.isComplete;
if (isComplete)
{
bool flag3 = handler != null;
if (flag3)
{
handler(assetBundleLoader.bundleInfo, handlerID);
}
}
else
{
bool flag4 = handler != null;
if (flag4)
{
AssetBundleLoader assetBundleLoader2 = assetBundleLoader;
assetBundleLoader2.onComplete = (AssetBundleManager.LoadAssetCompleteHandler)Delegate.Combine(assetBundleLoader2.onComplete, handler);
}
this._isCurrentLoading = true;
bool flag5 = assetBundleLoader.state < LoadState.State_LoadingAsync;
if (flag5)
{
this._nonCompleteLoaderSet.Add(assetBundleLoader);
}
this.StartLoad();
}
result = assetBundleLoader;
}
}
return result;
}
public AssetBundleInfo LoadImm(uint hash, string path, string suffix, string prefix = null)
{
bool flag = this.depInfoReader == null;
AssetBundleInfo result;
if (flag)
{
result = null;
}
else
{
AssetBundleLoader assetBundleLoader = this.CreateLoader(hash, path, suffix, prefix);
bool flag2 = assetBundleLoader == null;
if (flag2)
{
result = null;
}
else
{
this._thisTimeLoaderSet.Add(assetBundleLoader);
bool isComplete = assetBundleLoader.isComplete;
if (isComplete)
{
result = assetBundleLoader.bundleInfo;
}
else
{
this._isCurrentLoading = true;
bool flag3 = assetBundleLoader.state < LoadState.State_Loading;
if (flag3)
{
this._nonCompleteLoaderSet.Add(assetBundleLoader);
}
this.StartLoadImm();
result = assetBundleLoader.bundleInfo;
}
}
}
return result;
}
public bool CheckInDep(uint hash)
{
bool flag = this.depInfoReader == null;
return !flag && this._depInfoReader.GetAssetBundleInfo(hash) != null;
}
internal AssetBundleLoader CreateLoader(uint abFileName, string location = null, string suffix = null, string prefix = null)
{
bool flag = this._loaderCache.ContainsKey(abFileName);
AssetBundleLoader assetBundleLoader;
if (flag)
{
assetBundleLoader = this._loaderCache[abFileName];
}
else
{
AssetBundleData assetBundleData = this._depInfoReader.GetAssetBundleInfo(abFileName);
bool flag2 = assetBundleData == null;
if (flag2)
{
bool flag3 = prefix != null;
uint num;
if (flag3)
{
num = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(0u, prefix);
}
else
{
num = this._defaultHash;
}
bool flag4 = location != null;
if (flag4)
{
num = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(num, location);
}
bool flag5 = suffix != null;
if (flag5)
{
num = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(num, suffix);
}
assetBundleData = this._depInfoReader.GetAssetBundleInfoByShortName(num);
}
bool flag6 = assetBundleData == null;
if (flag6)
{
return null;
}
assetBundleLoader = this.CreateLoader();
assetBundleLoader.bundleManager = this;
assetBundleLoader.bundleData = assetBundleData;
assetBundleLoader.bundleName = assetBundleData.fullName;
this._loaderCache[abFileName] = assetBundleLoader;
}
return assetBundleLoader;
}
protected virtual AssetBundleLoader CreateLoader()
{
RuntimePlatform platform = Application.platform;
AssetBundleLoader result;
if ((int)platform != 8)
{
if ((int)platform != 11)
{
result = new MobileAssetBundleLoader();
}
else
{
result = new AndroidAssetBundleLoader();
}
}
else
{
result = new IOSAssetBundleLoader();
}
return result;
}
private void StartLoad()
{
bool flag = this._nonCompleteLoaderSet.Count > 0;
if (flag)
{
List<AssetBundleLoader> list = ListPool<AssetBundleLoader>.Get();
list.AddRange(this._nonCompleteLoaderSet);
this._nonCompleteLoaderSet.Clear();
foreach (AssetBundleLoader item in list)
{
this._currentLoadQueue.Add(item);
}
this._progress = new AssetBundleLoadProgress();
this._progress.total = this._currentLoadQueue.Count;
foreach (AssetBundleLoader assetBundleLoader in list)
{
assetBundleLoader.Load();
}
ListPool<AssetBundleLoader>.Release(list);
}
}
private void StartLoadImm()
{
bool flag = this._nonCompleteLoaderSet.Count > 0;
if (flag)
{
bool flag2 = this._nonCompleteLoaderSet.Count == 1;
if (flag2)
{
HashSet<AssetBundleLoader>.Enumerator enumerator = this._nonCompleteLoaderSet.GetEnumerator();
enumerator.MoveNext();
this._currentLoadQueue.Add(enumerator.Current);
this._nonCompleteLoaderSet.Clear();
this._progress.percent = 0f;
this._progress.complete = 0;
this._progress.loader = null;
this._progress.total = this._currentLoadQueue.Count;
enumerator.Current.LoadImm();
}
else
{
List<AssetBundleLoader> list = ListPool<AssetBundleLoader>.Get();
list.AddRange(this._nonCompleteLoaderSet);
this._nonCompleteLoaderSet.Clear();
foreach (AssetBundleLoader item in list)
{
this._currentLoadQueue.Add(item);
}
this._progress.percent = 0f;
this._progress.complete = 0;
this._progress.loader = null;
this._progress.total = this._currentLoadQueue.Count;
foreach (AssetBundleLoader assetBundleLoader in list)
{
assetBundleLoader.LoadImm();
}
ListPool<AssetBundleLoader>.Release(list);
}
}
}
public void RemoveAll()
{
this._currentLoadQueue.Clear();
this._requestQueue.Clear();
foreach (KeyValuePair<uint, AssetBundleInfo> keyValuePair in this._loadedAssetBundle)
{
keyValuePair.Value.Dispose();
}
this._loadedAssetBundle.Clear();
this._loaderCache.Clear();
this._requestUnloadBundleQueue.Clear();
}
public AssetBundleInfo GetBundleInfo(uint key)
{
foreach (KeyValuePair<uint, AssetBundleInfo> keyValuePair in this._loadedAssetBundle)
{
AssetBundleInfo value = keyValuePair.Value;
bool flag = value.bundleName == key;
if (flag)
{
return value;
}
}
return null;
}
internal void RequestLoadBundle(AssetBundleLoader loader)
{
bool flag = this._requestRemain < 0;
if (flag)
{
this._requestRemain = 0;
}
bool flag2 = this._requestRemain == 0;
if (flag2)
{
this._requestQueue.Enqueue(loader);
}
else
{
this.LoadBundle(loader);
}
}
internal void RequestLoadBundleImm(AssetBundleLoader loader)
{
bool flag = this._requestRemain < 0;
if (flag)
{
this._requestRemain = 0;
}
bool flag2 = this._requestRemain == 0;
if (flag2)
{
this._requestQueue.Enqueue(loader);
}
else
{
this.LoadBundleImm(loader);
}
}
private void CheckRequestList()
{
while (this._requestRemain > 0 && this._requestQueue.Count > 0)
{
AssetBundleLoader loader = this._requestQueue.Dequeue();
this.LoadBundle(loader);
}
}
private void LoadBundle(AssetBundleLoader loader)
{
bool flag = !loader.isComplete;
if (flag)
{
loader.LoadBundle();
this._requestRemain--;
}
}
private void LoadBundleImm(AssetBundleLoader loader)
{
bool flag = !loader.isComplete;
if (flag)
{
loader.LoadBundleImm();
this._requestRemain--;
}
}
internal void LoadError(AssetBundleLoader loader)
{
this.LoadComplete(loader);
}
internal void LoadComplete(AssetBundleLoader loader)
{
this._requestRemain++;
this._currentLoadQueue.Remove(loader);
bool flag = this.onProgress != null;
if (flag)
{
this._progress.loader = loader;
this._progress.complete = this._progress.total - this._currentLoadQueue.Count;
this.onProgress(this._progress);
}
bool flag2 = this._currentLoadQueue.Count == 0 && this._nonCompleteLoaderSet.Count == 0;
if (flag2)
{
this._isCurrentLoading = false;
foreach (AssetBundleLoader assetBundleLoader in this._thisTimeLoaderSet)
{
bool flag3 = assetBundleLoader.bundleInfo != null;
if (flag3)
{
assetBundleLoader.bundleInfo.ResetLifeTime();
}
}
this._thisTimeLoaderSet.Clear();
}
else
{
this.CheckRequestList();
}
}
internal AssetBundleInfo CreateBundleInfo(AssetBundleLoader loader, AssetBundleInfo abi = null, AssetBundle assetBundle = null)
{
bool flag = abi == null;
if (flag)
{
abi = new AssetBundleInfo();
}
abi.bundleName = loader.bundleName;
abi.bundle = assetBundle;
abi.data = loader.bundleData;
this._loadedAssetBundle[abi.bundleName] = abi;
this._bundleCount++;
return abi;
}
public void DeleteBundleCount()
{
this._bundleCount--;
}
internal void RemoveBundleInfo(AssetBundleInfo abi)
{
abi.Dispose();
this._loadedAssetBundle.Remove(abi.bundleName);
}
private void CheckUnusedBundle()
{
this.UnloadUnusedBundle(false);
}
public void UnloadUnusedBundle(bool force = false)
{
bool flag = (!this._isCurrentLoading && !XSingleton<XResourceLoaderMgr>.singleton.isCurrentLoading) || force;
bool flag2 = flag;
if (flag2)
{
List<uint> list = ListPool<uint>.Get();
list.AddRange(this._loadedAssetBundle.Keys);
int num = force ? 100000 : 20;
int num2 = 0;
bool flag3;
do
{
flag3 = false;
int num3 = 0;
while (num3 < list.Count && flag && num2 < num)
{
uint key = list[num3];
AssetBundleInfo assetBundleInfo = this._loadedAssetBundle[key];
bool isUnused = assetBundleInfo.isUnused;
if (isUnused)
{
flag3 = true;
num2++;
this.RemoveBundleInfo(assetBundleInfo);
list.RemoveAt(num3);
num3--;
}
num3++;
}
}
while (flag3 && flag && num2 < num);
ListPool<uint>.Release(list);
while (this._requestUnloadBundleQueue.Count > 0 && flag && num2 < num)
{
AssetBundleInfo assetBundleInfo2 = this._requestUnloadBundleQueue.Dequeue();
bool flag4 = assetBundleInfo2 != null;
if (flag4)
{
assetBundleInfo2.UnloadBundle();
num2++;
}
}
}
}
public void UnloadNotUsedLoader()
{
List<uint> list = ListPool<uint>.Get();
list.AddRange(this._loadedAssetBundle.Keys);
List<AssetBundleLoader> list2 = ListPool<AssetBundleLoader>.Get();
list2.AddRange(this._nonCompleteLoaderSet);
list2.AddRange(this._currentLoadQueue);
foreach (AssetBundleLoader assetBundleLoader in list2)
{
this.RemoveLoadingLoader(assetBundleLoader.bundleName, list);
}
for (int i = 0; i < list.Count; i++)
{
AssetBundleInfo abi = this._loadedAssetBundle[list[i]];
this.RemoveBundleInfo(abi);
list.RemoveAt(i);
i--;
}
ListPool<uint>.Release(list);
ListPool<AssetBundleLoader>.Release(list2);
}
private void RemoveLoadingLoader(uint hash, List<uint> list)
{
AssetBundleData assetBundleInfo = this._depInfoReader.GetAssetBundleInfo(hash);
list.Remove(assetBundleInfo.fullName);
bool flag = assetBundleInfo.dependencies != null;
if (flag)
{
for (int i = 0; i < assetBundleInfo.dependencies.Length; i++)
{
this.RemoveLoadingLoader(assetBundleInfo.dependencies[i], list);
}
}
}
public void AddUnloadBundleQueue(AssetBundleInfo info)
{
this._requestUnloadBundleQueue.Enqueue(info);
bool flag = this._requestUnloadBundleQueue.Count > 3;
if (flag)
{
this.UnloadUnusedBundle(false);
}
}
public void RemoveBundle(uint key)
{
AssetBundleInfo bundleInfo = this.GetBundleInfo(key);
bool flag = bundleInfo != null;
if (flag)
{
this.RemoveBundleInfo(bundleInfo);
}
}
}
}
|