| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Networking; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class AssetPromise_AB : AssetPromise_WithUrl<Asset_AB> |
| | 10 | | { |
| 1 | 11 | | public static bool VERBOSE = false; |
| 24 | 12 | | public static int MAX_CONCURRENT_REQUESTS => CommonScriptableObjects.rendererState.Get() ? 30 : 256; |
| | 13 | |
|
| 1 | 14 | | public static int concurrentRequests = 0; |
| | 15 | | public static event Action OnDownloadingProgressUpdate; |
| | 16 | |
|
| | 17 | | bool requestRegistered = false; |
| | 18 | |
|
| 0 | 19 | | public static int downloadingCount => concurrentRequests; |
| 0 | 20 | | public static int queueCount => AssetPromiseKeeper_AB.i.waitingPromisesCount; |
| | 21 | |
|
| | 22 | | Coroutine loadCoroutine; |
| 1 | 23 | | static HashSet<string> failedRequestUrls = new HashSet<string>(); |
| | 24 | |
|
| 35 | 25 | | List<AssetPromise_AB> dependencyPromises = new List<AssetPromise_AB>(); |
| | 26 | |
|
| 1 | 27 | | public static AssetBundlesLoader assetBundlesLoader = new AssetBundlesLoader(); |
| | 28 | | private Transform containerTransform; |
| | 29 | | private WebRequestAsyncOperation asyncOp; |
| | 30 | |
|
| 35 | 31 | | public AssetPromise_AB(string contentUrl, string hash, Transform containerTransform = null) : base(contentUrl, h |
| | 32 | | { |
| 35 | 33 | | this.containerTransform = containerTransform; |
| 35 | 34 | | assetBundlesLoader.Start(); |
| 35 | 35 | | } |
| | 36 | |
|
| | 37 | | protected override bool AddToLibrary() |
| | 38 | | { |
| 17 | 39 | | if (!library.Add(asset)) |
| | 40 | | { |
| 0 | 41 | | Debug.Log("add to library fail?"); |
| 0 | 42 | | return false; |
| | 43 | | } |
| | 44 | |
|
| 17 | 45 | | if (asset == null) |
| | 46 | | { |
| 0 | 47 | | Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}"); |
| 0 | 48 | | return false; |
| | 49 | | } |
| | 50 | |
|
| 17 | 51 | | asset = library.Get(asset.id); |
| 17 | 52 | | return true; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | protected override void OnCancelLoading() |
| | 56 | | { |
| 7 | 57 | | if (loadCoroutine != null) |
| | 58 | | { |
| 7 | 59 | | CoroutineStarter.Stop(loadCoroutine); |
| 7 | 60 | | loadCoroutine = null; |
| | 61 | | } |
| | 62 | |
|
| 7 | 63 | | if (asyncOp != null) |
| | 64 | | { |
| 3 | 65 | | asyncOp.Dispose(); |
| | 66 | | } |
| | 67 | |
|
| 14 | 68 | | for (int i = 0; i < dependencyPromises.Count; i++) |
| | 69 | | { |
| 0 | 70 | | dependencyPromises[i].Unload(); |
| | 71 | | } |
| | 72 | |
|
| 7 | 73 | | dependencyPromises.Clear(); |
| | 74 | |
|
| 7 | 75 | | if (asset != null) |
| | 76 | | { |
| 7 | 77 | | asset.CancelShow(); |
| | 78 | | } |
| | 79 | |
|
| 7 | 80 | | UnregisterConcurrentRequest(); |
| 7 | 81 | | } |
| | 82 | |
|
| 28 | 83 | | protected override void OnAfterLoadOrReuse() { } |
| | 84 | |
|
| 35 | 85 | | protected override void OnBeforeLoadOrReuse() { } |
| | 86 | |
|
| | 87 | | protected IEnumerator LoadAssetBundleWithDeps(string baseUrl, string hash, Action OnSuccess, Action OnFail) |
| | 88 | | { |
| 24 | 89 | | string finalUrl = baseUrl + hash; |
| | 90 | |
|
| 24 | 91 | | if (failedRequestUrls.Contains(finalUrl)) |
| | 92 | | { |
| 0 | 93 | | OnFail?.Invoke(); |
| 0 | 94 | | yield break; |
| | 95 | | } |
| | 96 | |
|
| 24 | 97 | | yield return WaitForConcurrentRequestsSlot(); |
| | 98 | |
|
| 19 | 99 | | RegisterConcurrentRequest(); |
| | 100 | | #if (UNITY_EDITOR || UNITY_STANDALONE) |
| 19 | 101 | | asyncOp = Environment.i.platform.webRequest.GetAssetBundle(url: finalUrl, hash: Hash128.Compute(hash), dispo |
| | 102 | | #else |
| | 103 | | //NOTE(Brian): Disable in build because using the asset bundle caching uses IDB. |
| | 104 | | asyncOp = Environment.i.platform.webRequest.GetAssetBundle(url: finalUrl, disposeOnCompleted: false); |
| | 105 | | #endif |
| | 106 | |
|
| 19 | 107 | | if (!DependencyMapLoadHelper.dependenciesMap.ContainsKey(hash)) |
| 5 | 108 | | CoroutineStarter.Start(DependencyMapLoadHelper.GetDepMap(baseUrl, hash)); |
| | 109 | |
|
| 19 | 110 | | yield return DependencyMapLoadHelper.WaitUntilDepMapIsResolved(hash); |
| | 111 | |
|
| 19 | 112 | | if (DependencyMapLoadHelper.dependenciesMap.ContainsKey(hash)) |
| | 113 | | { |
| 16 | 114 | | using (var it = DependencyMapLoadHelper.dependenciesMap[hash].GetEnumerator()) |
| | 115 | | { |
| 27 | 116 | | while (it.MoveNext()) |
| | 117 | | { |
| 11 | 118 | | var dep = it.Current; |
| 11 | 119 | | var promise = new AssetPromise_AB(baseUrl, dep, containerTransform); |
| 11 | 120 | | AssetPromiseKeeper_AB.i.Keep(promise); |
| 11 | 121 | | dependencyPromises.Add(promise); |
| | 122 | | } |
| 16 | 123 | | } |
| | 124 | | } |
| | 125 | |
|
| 19 | 126 | | yield return asyncOp; |
| | 127 | |
|
| 19 | 128 | | if (asyncOp.isDisposed) |
| | 129 | | { |
| 0 | 130 | | OnFail?.Invoke(); |
| 0 | 131 | | yield break; |
| | 132 | | } |
| | 133 | |
|
| 19 | 134 | | if (!asyncOp.isSucceded) |
| | 135 | | { |
| 2 | 136 | | if (VERBOSE) |
| 0 | 137 | | Debug.Log($"Request failed? {asyncOp.webRequest.error} ... {finalUrl}"); |
| 2 | 138 | | failedRequestUrls.Add(finalUrl); |
| 2 | 139 | | OnFail?.Invoke(); |
| 2 | 140 | | asyncOp.Dispose(); |
| 2 | 141 | | yield break; |
| | 142 | | } |
| | 143 | |
|
| 17 | 144 | | UnregisterConcurrentRequest(); |
| | 145 | |
|
| 56 | 146 | | foreach (var promise in dependencyPromises) |
| | 147 | | { |
| 11 | 148 | | yield return promise; |
| | 149 | | } |
| | 150 | |
|
| 17 | 151 | | AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(asyncOp.webRequest); |
| 17 | 152 | | asyncOp.Dispose(); |
| | 153 | |
|
| 17 | 154 | | if (assetBundle == null || asset == null) |
| | 155 | | { |
| 0 | 156 | | OnFail?.Invoke(); |
| | 157 | |
|
| 0 | 158 | | failedRequestUrls.Add(finalUrl); |
| 0 | 159 | | yield break; |
| | 160 | | } |
| | 161 | |
|
| 17 | 162 | | asset.ownerAssetBundle = assetBundle; |
| 17 | 163 | | asset.assetBundleAssetName = assetBundle.name; |
| | 164 | |
|
| 17 | 165 | | assetBundlesLoader.MarkAssetBundleForLoad(asset, assetBundle, containerTransform, OnSuccess, OnFail); |
| 17 | 166 | | } |
| | 167 | |
|
| | 168 | | public override string ToString() |
| | 169 | | { |
| 0 | 170 | | string result = $"AB request... loadCoroutine = {loadCoroutine} ... state = {state}\n"; |
| | 171 | |
|
| 0 | 172 | | if (asyncOp.webRequest != null) |
| 0 | 173 | | result += $"url = {asyncOp.webRequest.url} ... code = {asyncOp.webRequest.responseCode} ... progress = { |
| | 174 | | else |
| 0 | 175 | | result += $"null request for url: {contentUrl + hash}\n"; |
| | 176 | |
|
| | 177 | |
|
| 0 | 178 | | if (dependencyPromises != null && dependencyPromises.Count > 0) |
| | 179 | | { |
| 0 | 180 | | result += "Dependencies:\n\n"; |
| 0 | 181 | | foreach (var p in dependencyPromises) |
| | 182 | | { |
| 0 | 183 | | result += p.ToString() + "\n\n"; |
| | 184 | | } |
| | 185 | | } |
| | 186 | |
|
| 0 | 187 | | result += "Concurrent requests = " + concurrentRequests; |
| | 188 | |
|
| 0 | 189 | | return result; |
| | 190 | | } |
| | 191 | |
|
| 48 | 192 | | protected override void OnLoad(Action OnSuccess, Action OnFail) { loadCoroutine = CoroutineStarter.Start(LoadAss |
| | 193 | |
|
| | 194 | | IEnumerator WaitForConcurrentRequestsSlot() |
| | 195 | | { |
| 24 | 196 | | while (concurrentRequests >= MAX_CONCURRENT_REQUESTS) |
| | 197 | | { |
| 0 | 198 | | yield return null; |
| | 199 | | } |
| 24 | 200 | | } |
| | 201 | |
|
| | 202 | | void RegisterConcurrentRequest() |
| | 203 | | { |
| 19 | 204 | | if (requestRegistered) |
| 0 | 205 | | return; |
| | 206 | |
|
| 19 | 207 | | concurrentRequests++; |
| 19 | 208 | | OnDownloadingProgressUpdate?.Invoke(); |
| 19 | 209 | | requestRegistered = true; |
| 19 | 210 | | } |
| | 211 | |
|
| | 212 | | void UnregisterConcurrentRequest() |
| | 213 | | { |
| 24 | 214 | | if (!requestRegistered) |
| 5 | 215 | | return; |
| | 216 | |
|
| 19 | 217 | | concurrentRequests--; |
| 19 | 218 | | OnDownloadingProgressUpdate?.Invoke(); |
| 19 | 219 | | requestRegistered = false; |
| 19 | 220 | | } |
| | 221 | | } |
| | 222 | | } |