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