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