| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Providers; |
| | 3 | | using MainScripts.DCL.Controllers.AssetManager; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class AssetPromise_AB : AssetPromise_WithUrl<Asset_AB> |
| | 12 | | { |
| | 13 | | public static event Action OnDownloadingProgressUpdate; |
| 0 | 14 | | public static int queueCount => AssetPromiseKeeper_AB.i.waitingPromisesCount; |
| | 15 | |
|
| 1 | 16 | | static HashSet<string> failedRequestUrls = new (); |
| | 17 | |
|
| 43 | 18 | | List<AssetPromise_AB> dependencyPromises = new (); |
| | 19 | |
|
| 1 | 20 | | public static AssetBundlesLoader assetBundlesLoader = new (); |
| | 21 | |
|
| | 22 | | private readonly Transform containerTransform; |
| | 23 | | private readonly AssetSource permittedSources; |
| | 24 | |
|
| | 25 | | private CancellationTokenSource cancellationTokenSource; |
| | 26 | |
|
| | 27 | | private Service<IAssetBundleResolver> assetBundleResolver; |
| | 28 | |
|
| | 29 | | public AssetPromise_AB(string contentUrl, string hash, |
| 43 | 30 | | Transform containerTransform = null, AssetSource permittedSources = AssetSource.ALL) : base(contentUrl, |
| | 31 | | hash) |
| | 32 | | { |
| 43 | 33 | | this.containerTransform = containerTransform; |
| 43 | 34 | | this.permittedSources = permittedSources; |
| 43 | 35 | | assetBundlesLoader.Start(); |
| 43 | 36 | | } |
| | 37 | |
|
| | 38 | | protected override bool AddToLibrary() |
| | 39 | | { |
| 22 | 40 | | if (!library.Add(asset)) |
| | 41 | | { |
| 0 | 42 | | Debug.Log("add to library fail?"); |
| | 43 | |
|
| 0 | 44 | | return false; |
| | 45 | | } |
| | 46 | |
|
| 22 | 47 | | if (asset == null) |
| | 48 | | { |
| 0 | 49 | | Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}"); |
| | 50 | |
|
| 0 | 51 | | return false; |
| | 52 | | } |
| | 53 | |
|
| 22 | 54 | | asset = library.Get(asset.id); |
| | 55 | |
|
| 22 | 56 | | return true; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | protected override void OnCancelLoading() |
| | 60 | | { |
| 8 | 61 | | cancellationTokenSource?.Cancel(); |
| 8 | 62 | | cancellationTokenSource = null; |
| | 63 | |
|
| 16 | 64 | | foreach (var t in dependencyPromises) |
| 0 | 65 | | t.Unload(); |
| | 66 | |
|
| 8 | 67 | | dependencyPromises.Clear(); |
| | 68 | |
|
| 8 | 69 | | asset?.CancelShow(); |
| 8 | 70 | | } |
| | 71 | |
|
| 43 | 72 | | protected override void OnBeforeLoadOrReuse() { } |
| | 73 | |
|
| 35 | 74 | | protected override void OnAfterLoadOrReuse() { } |
| | 75 | |
|
| | 76 | | private async UniTask LoadAssetBundleWithDeps(string baseUrl, string hash, Action onSuccess, Action<Exception> o |
| | 77 | | { |
| 29 | 78 | | var finalUrl = baseUrl + hash; |
| | 79 | |
|
| 29 | 80 | | if (failedRequestUrls.Contains(finalUrl)) |
| | 81 | | { |
| 0 | 82 | | onFail?.Invoke(new Exception($"The url {finalUrl} has failed")); |
| 0 | 83 | | return; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | try |
| | 87 | | { |
| 87 | 88 | | var bundle = await assetBundleResolver.Ref.GetAssetBundleAsync(permittedSources, baseUrl, hash, cancella |
| 22 | 89 | | SetAssetBundle(bundle); |
| | 90 | |
|
| 28 | 91 | | var dependencies = await bundle.GetDependenciesAsync(baseUrl, hash, cancellationToken); |
| | 92 | |
|
| 70 | 93 | | foreach (string dependency in dependencies) |
| | 94 | | { |
| 13 | 95 | | var promise = new AssetPromise_AB(baseUrl, dependency, containerTransform, permittedSources); |
| 13 | 96 | | AssetPromiseKeeper_AB.i.Keep(promise); |
| 13 | 97 | | dependencyPromises.Add(promise); |
| | 98 | | } |
| | 99 | |
|
| 70 | 100 | | foreach (var dependencyPromise in dependencyPromises) |
| 17 | 101 | | await dependencyPromise; |
| | 102 | |
|
| 22 | 103 | | assetBundlesLoader.MarkAssetBundleForLoad(asset, containerTransform, onSuccess, onFail); |
| 22 | 104 | | } |
| 7 | 105 | | catch (Exception e) |
| | 106 | | { |
| 7 | 107 | | if (e is not OperationCanceledException) |
| 2 | 108 | | failedRequestUrls.Add(finalUrl); |
| | 109 | |
|
| 7 | 110 | | onFail?.Invoke(e); |
| 7 | 111 | | } |
| 29 | 112 | | } |
| | 113 | |
|
| | 114 | | private void SetAssetBundle(AssetBundle assetBundle) |
| | 115 | | { |
| 22 | 116 | | asset.SetAssetBundle(assetBundle); |
| 22 | 117 | | asset.LoadMetrics(); |
| 22 | 118 | | } |
| | 119 | |
|
| | 120 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 121 | | { |
| 30 | 122 | | if (cancellationTokenSource != null) |
| 1 | 123 | | return; |
| | 124 | |
|
| 29 | 125 | | cancellationTokenSource = new CancellationTokenSource(); |
| 29 | 126 | | OnDownloadingProgressUpdate?.Invoke(); |
| | 127 | |
|
| 29 | 128 | | LoadAssetBundleWithDeps(contentUrl, hash, OnSuccess, OnFail, cancellationTokenSource.Token).Forget(); |
| 29 | 129 | | } |
| | 130 | | } |
| | 131 | | } |