| | 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; |
| 30 | 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 | |
|
| 44 | 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 | |
|
| | 31 | | public AssetPromise_AB(string contentUrl, string hash, |
| 44 | 32 | | Transform containerTransform = null) : base(contentUrl, |
| | 33 | | hash) |
| | 34 | | { |
| 44 | 35 | | this.containerTransform = containerTransform; |
| 44 | 36 | | assetBundlesLoader.Start(); |
| 44 | 37 | | } |
| | 38 | |
|
| | 39 | | protected override bool AddToLibrary() |
| | 40 | | { |
| 22 | 41 | | if (!library.Add(asset)) |
| | 42 | | { |
| 0 | 43 | | Debug.Log("add to library fail?"); |
| 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()}"); |
| 0 | 50 | | return false; |
| | 51 | | } |
| | 52 | |
|
| 22 | 53 | | asset = library.Get(asset.id); |
| 22 | 54 | | return true; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | protected override void OnCancelLoading() |
| | 58 | | { |
| 8 | 59 | | if (loadCoroutine != null) |
| | 60 | | { |
| 8 | 61 | | CoroutineStarter.Stop(loadCoroutine); |
| 8 | 62 | | loadCoroutine = null; |
| | 63 | | } |
| | 64 | |
|
| 8 | 65 | | if (asyncOp != null) |
| | 66 | | { |
| 8 | 67 | | asyncOp.Dispose(); |
| | 68 | | } |
| | 69 | |
|
| 18 | 70 | | for (int i = 0; i < dependencyPromises.Count; i++) |
| | 71 | | { |
| 1 | 72 | | dependencyPromises[i].Unload(); |
| | 73 | | } |
| | 74 | |
|
| 8 | 75 | | dependencyPromises.Clear(); |
| | 76 | |
|
| 8 | 77 | | if (asset != null) |
| | 78 | | { |
| 8 | 79 | | asset.CancelShow(); |
| | 80 | | } |
| | 81 | |
|
| 8 | 82 | | UnregisterConcurrentRequest(); |
| 8 | 83 | | } |
| | 84 | |
|
| | 85 | | protected override void OnBeforeLoadOrReuse() |
| | 86 | | { |
| 44 | 87 | | } |
| | 88 | |
|
| | 89 | | protected override void OnAfterLoadOrReuse() |
| | 90 | | { |
| 36 | 91 | | } |
| | 92 | |
|
| | 93 | | protected IEnumerator LoadAssetBundleWithDeps(string baseUrl, string hash, Action OnSuccess, Action<Exception> O |
| | 94 | | { |
| 30 | 95 | | string finalUrl = baseUrl + hash; |
| | 96 | |
|
| 30 | 97 | | if (failedRequestUrls.Contains(finalUrl)) |
| | 98 | | { |
| 0 | 99 | | OnFail?.Invoke(new Exception($"The url {finalUrl} has failed")); |
| 0 | 100 | | yield break; |
| | 101 | | } |
| | 102 | |
|
| 30 | 103 | | yield return WaitForConcurrentRequestsSlot(); |
| | 104 | |
|
| 30 | 105 | | RegisterConcurrentRequest(); |
| | 106 | | #if (UNITY_EDITOR || UNITY_STANDALONE) |
| 30 | 107 | | asyncOp = Environment.i.platform.webRequest.GetAssetBundle(url: finalUrl, hash: Hash128.Compute(hash), |
| | 108 | | disposeOnCompleted: false); |
| | 109 | | #else |
| | 110 | | //NOTE(Brian): Disable in build because using the asset bundle caching uses IDB. |
| | 111 | | asyncOp = Environment.i.platform.webRequest.GetAssetBundle(url: finalUrl, disposeOnCompleted: false); |
| | 112 | | #endif |
| | 113 | |
|
| | 114 | | // 1. Download asset bundle, but don't load its objects yet |
| 30 | 115 | | yield return asyncOp; |
| | 116 | |
|
| 25 | 117 | | if (asyncOp.isDisposed) |
| | 118 | | { |
| 0 | 119 | | OnFail?.Invoke(new Exception("Operation is disposed")); |
| 0 | 120 | | yield break; |
| | 121 | | } |
| | 122 | |
|
| 25 | 123 | | if (!asyncOp.isSucceded) |
| | 124 | | { |
| 2 | 125 | | if (VERBOSE) |
| 0 | 126 | | Debug.Log($"Request failed? {asyncOp.webRequest.error} ... {finalUrl}"); |
| 2 | 127 | | failedRequestUrls.Add(finalUrl); |
| 2 | 128 | | OnFail?.Invoke(new Exception($"Request failed? {asyncOp.webRequest.error} ... {finalUrl}")); |
| 2 | 129 | | asyncOp.Dispose(); |
| 2 | 130 | | yield break; |
| | 131 | | } |
| | 132 | |
|
| 23 | 133 | | if (!LoadAssetBundle(OnFail, finalUrl)) |
| 0 | 134 | | yield break; |
| | 135 | |
|
| | 136 | | // 2. Check internal metadata file (dependencies, version, timestamp) and if it doesn't exist, fetch the ext |
| 23 | 137 | | TextAsset metadata = asset.GetMetadata(); |
| | 138 | |
|
| 23 | 139 | | if (metadata != null) |
| | 140 | | { |
| 0 | 141 | | AssetBundleDepMapLoadHelper.LoadDepMapFromJSON(metadata.text, hash); |
| 0 | 142 | | } |
| | 143 | | else |
| | 144 | | { |
| 23 | 145 | | if (!AssetBundleDepMapLoadHelper.dependenciesMap.ContainsKey(hash)) |
| 4 | 146 | | CoroutineStarter.Start(AssetBundleDepMapLoadHelper.LoadExternalDepMap(baseUrl, hash)); |
| | 147 | |
|
| 23 | 148 | | yield return AssetBundleDepMapLoadHelper.WaitUntilExternalDepMapIsResolved(hash); |
| | 149 | | } |
| | 150 | |
|
| | 151 | | // 3. Resolve dependencies |
| 23 | 152 | | if (AssetBundleDepMapLoadHelper.dependenciesMap.ContainsKey(hash)) |
| | 153 | | { |
| 21 | 154 | | using (var it = AssetBundleDepMapLoadHelper.dependenciesMap[hash].GetEnumerator()) |
| | 155 | | { |
| 35 | 156 | | while (it.MoveNext()) |
| | 157 | | { |
| 14 | 158 | | var dep = it.Current; |
| 14 | 159 | | var promise = new AssetPromise_AB(baseUrl, dep, containerTransform); |
| 14 | 160 | | AssetPromiseKeeper_AB.i.Keep(promise); |
| 14 | 161 | | dependencyPromises.Add(promise); |
| | 162 | | } |
| 21 | 163 | | } |
| | 164 | | } |
| | 165 | |
|
| 23 | 166 | | UnregisterConcurrentRequest(); |
| | 167 | |
|
| 74 | 168 | | foreach (var promise in dependencyPromises) |
| | 169 | | { |
| 14 | 170 | | yield return promise; |
| | 171 | | } |
| | 172 | |
|
| 23 | 173 | | assetBundlesLoader.MarkAssetBundleForLoad(asset, containerTransform, OnSuccess, OnFail); |
| 23 | 174 | | } |
| | 175 | | private bool LoadAssetBundle(Action<Exception> OnFail, string finalUrl) |
| | 176 | | { |
| 23 | 177 | | var assetBundle = DownloadHandlerAssetBundle.GetContent(asyncOp.webRequest); |
| 23 | 178 | | asyncOp.Dispose(); |
| | 179 | |
|
| 23 | 180 | | if (assetBundle == null || asset == null) |
| | 181 | | { |
| 0 | 182 | | OnFail?.Invoke(new Exception("Asset bundle or asset is null")); |
| 0 | 183 | | failedRequestUrls.Add(finalUrl); |
| | 184 | |
|
| 0 | 185 | | return false; |
| | 186 | | } |
| | 187 | |
|
| 23 | 188 | | asset.SetAssetBundle(assetBundle); |
| 23 | 189 | | asset.LoadMetrics(); |
| | 190 | |
|
| 23 | 191 | | return true; |
| | 192 | | } |
| | 193 | |
|
| | 194 | | public override string ToString() |
| | 195 | | { |
| 0 | 196 | | string result = $"AB request... loadCoroutine = {loadCoroutine} ... state = {state}\n"; |
| | 197 | |
|
| 0 | 198 | | if (asyncOp.webRequest != null) |
| 0 | 199 | | result += |
| | 200 | | $"url = {asyncOp.webRequest.url} ... code = {asyncOp.webRequest.responseCode} ... progress = {asyncO |
| | 201 | | else |
| 0 | 202 | | result += $"null request for url: {contentUrl + hash}\n"; |
| | 203 | |
|
| | 204 | |
|
| 0 | 205 | | if (dependencyPromises != null && dependencyPromises.Count > 0) |
| | 206 | | { |
| 0 | 207 | | result += "Dependencies:\n\n"; |
| 0 | 208 | | foreach (var p in dependencyPromises) |
| | 209 | | { |
| 0 | 210 | | result += p.ToString() + "\n\n"; |
| | 211 | | } |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | result += "Concurrent requests = " + concurrentRequests; |
| | 215 | |
|
| 0 | 216 | | return result; |
| | 217 | | } |
| | 218 | |
|
| | 219 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 220 | | { |
| 30 | 221 | | loadCoroutine = CoroutineStarter.Start(DCLCoroutineRunner.Run( |
| | 222 | | LoadAssetBundleWithDeps(contentUrl, hash, OnSuccess, OnFail), |
| 0 | 223 | | exception => OnFail?.Invoke(exception))); |
| 30 | 224 | | } |
| | 225 | |
|
| | 226 | | IEnumerator WaitForConcurrentRequestsSlot() |
| | 227 | | { |
| 30 | 228 | | while (concurrentRequests >= MAX_CONCURRENT_REQUESTS) |
| | 229 | | { |
| 0 | 230 | | yield return null; |
| | 231 | | } |
| 30 | 232 | | } |
| | 233 | |
|
| | 234 | | void RegisterConcurrentRequest() |
| | 235 | | { |
| 30 | 236 | | if (requestRegistered) |
| 0 | 237 | | return; |
| | 238 | |
|
| 30 | 239 | | concurrentRequests++; |
| 30 | 240 | | OnDownloadingProgressUpdate?.Invoke(); |
| 30 | 241 | | requestRegistered = true; |
| 30 | 242 | | } |
| | 243 | |
|
| | 244 | | void UnregisterConcurrentRequest() |
| | 245 | | { |
| 31 | 246 | | if (!requestRegistered) |
| 1 | 247 | | return; |
| | 248 | |
|
| 30 | 249 | | concurrentRequests--; |
| 30 | 250 | | OnDownloadingProgressUpdate?.Invoke(); |
| 30 | 251 | | requestRegistered = false; |
| 30 | 252 | | } |
| | 253 | | } |
| | 254 | | } |