< Summary

Class:DCL.AssetPromise_AB
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/AB/AssetPromise_AB.cs
Covered lines:75
Uncovered lines:27
Coverable lines:102
Total lines:222
Line coverage:73.5% (75 of 102)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_AB()0%110100%
AssetPromise_AB(...)0%110100%
AddToLibrary()0%4.123050%
OnCancelLoading()0%5.075085.71%
OnAfterLoadOrReuse()0%110100%
OnBeforeLoadOrReuse()0%110100%
LoadAssetBundleWithDeps()0%22.420081.82%
ToString()0%30500%
OnLoad(...)0%110100%
WaitForConcurrentRequestsSlot()0%4.594066.67%
RegisterConcurrentRequest()0%3.043083.33%
UnregisterConcurrentRequest()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/AB/AssetPromise_AB.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using UnityEngine.Networking;
 6
 7namespace DCL
 8{
 9    public class AssetPromise_AB : AssetPromise_WithUrl<Asset_AB>
 10    {
 111        public static bool VERBOSE = false;
 2412        public static int MAX_CONCURRENT_REQUESTS => CommonScriptableObjects.rendererState.Get() ? 30 : 256;
 13
 114        public static int concurrentRequests = 0;
 15        public static event Action OnDownloadingProgressUpdate;
 16
 17        bool requestRegistered = false;
 18
 019        public static int downloadingCount => concurrentRequests;
 020        public static int queueCount => AssetPromiseKeeper_AB.i.waitingPromisesCount;
 21
 22        Coroutine loadCoroutine;
 123        static HashSet<string> failedRequestUrls = new HashSet<string>();
 24
 3525        List<AssetPromise_AB> dependencyPromises = new List<AssetPromise_AB>();
 26
 127        public static AssetBundlesLoader assetBundlesLoader = new AssetBundlesLoader();
 28        private Transform containerTransform;
 29        private WebRequestAsyncOperation asyncOp;
 30
 3531        public AssetPromise_AB(string contentUrl, string hash, Transform containerTransform = null) : base(contentUrl, h
 32        {
 3533            this.containerTransform = containerTransform;
 3534            assetBundlesLoader.Start();
 3535        }
 36
 37        protected override bool AddToLibrary()
 38        {
 1739            if (!library.Add(asset))
 40            {
 041                Debug.Log("add to library fail?");
 042                return false;
 43            }
 44
 1745            if (asset == null)
 46            {
 047                Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}");
 048                return false;
 49            }
 50
 1751            asset = library.Get(asset.id);
 1752            return true;
 53        }
 54
 55        protected override void OnCancelLoading()
 56        {
 757            if (loadCoroutine != null)
 58            {
 759                CoroutineStarter.Stop(loadCoroutine);
 760                loadCoroutine = null;
 61            }
 62
 763            if (asyncOp != null)
 64            {
 365                asyncOp.Dispose();
 66            }
 67
 1468            for (int i = 0; i < dependencyPromises.Count; i++)
 69            {
 070                dependencyPromises[i].Unload();
 71            }
 72
 773            dependencyPromises.Clear();
 74
 775            if (asset != null)
 76            {
 777                asset.CancelShow();
 78            }
 79
 780            UnregisterConcurrentRequest();
 781        }
 82
 2883        protected override void OnAfterLoadOrReuse() { }
 84
 3585        protected override void OnBeforeLoadOrReuse() { }
 86
 87        protected IEnumerator LoadAssetBundleWithDeps(string baseUrl, string hash, Action OnSuccess, Action OnFail)
 88        {
 2489            string finalUrl = baseUrl + hash;
 90
 2491            if (failedRequestUrls.Contains(finalUrl))
 92            {
 093                OnFail?.Invoke();
 094                yield break;
 95            }
 96
 2497            yield return WaitForConcurrentRequestsSlot();
 98
 1999            RegisterConcurrentRequest();
 100#if (UNITY_EDITOR || UNITY_STANDALONE)
 19101            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
 19107            if (!DependencyMapLoadHelper.dependenciesMap.ContainsKey(hash))
 5108                CoroutineStarter.Start(DependencyMapLoadHelper.GetDepMap(baseUrl, hash));
 109
 19110            yield return DependencyMapLoadHelper.WaitUntilDepMapIsResolved(hash);
 111
 19112            if (DependencyMapLoadHelper.dependenciesMap.ContainsKey(hash))
 113            {
 16114                using (var it = DependencyMapLoadHelper.dependenciesMap[hash].GetEnumerator())
 115                {
 27116                    while (it.MoveNext())
 117                    {
 11118                        var dep = it.Current;
 11119                        var promise = new AssetPromise_AB(baseUrl, dep, containerTransform);
 11120                        AssetPromiseKeeper_AB.i.Keep(promise);
 11121                        dependencyPromises.Add(promise);
 122                    }
 16123                }
 124            }
 125
 19126            yield return asyncOp;
 127
 19128            if (asyncOp.isDisposed)
 129            {
 0130                OnFail?.Invoke();
 0131                yield break;
 132            }
 133
 19134            if (!asyncOp.isSucceded)
 135            {
 2136                if (VERBOSE)
 0137                    Debug.Log($"Request failed? {asyncOp.webRequest.error} ... {finalUrl}");
 2138                failedRequestUrls.Add(finalUrl);
 2139                OnFail?.Invoke();
 2140                asyncOp.Dispose();
 2141                yield break;
 142            }
 143
 17144            UnregisterConcurrentRequest();
 145
 56146            foreach (var promise in dependencyPromises)
 147            {
 11148                yield return promise;
 149            }
 150
 17151            AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(asyncOp.webRequest);
 17152            asyncOp.Dispose();
 153
 17154            if (assetBundle == null || asset == null)
 155            {
 0156                OnFail?.Invoke();
 157
 0158                failedRequestUrls.Add(finalUrl);
 0159                yield break;
 160            }
 161
 17162            asset.ownerAssetBundle = assetBundle;
 17163            asset.assetBundleAssetName = assetBundle.name;
 164
 17165            assetBundlesLoader.MarkAssetBundleForLoad(asset, assetBundle, containerTransform, OnSuccess, OnFail);
 17166        }
 167
 168        public override string ToString()
 169        {
 0170            string result = $"AB request... loadCoroutine = {loadCoroutine} ... state = {state}\n";
 171
 0172            if (asyncOp.webRequest != null)
 0173                result += $"url = {asyncOp.webRequest.url} ... code = {asyncOp.webRequest.responseCode} ... progress = {
 174            else
 0175                result += $"null request for url: {contentUrl + hash}\n";
 176
 177
 0178            if (dependencyPromises != null && dependencyPromises.Count > 0)
 179            {
 0180                result += "Dependencies:\n\n";
 0181                foreach (var p in dependencyPromises)
 182                {
 0183                    result += p.ToString() + "\n\n";
 184                }
 185            }
 186
 0187            result += "Concurrent requests = " + concurrentRequests;
 188
 0189            return result;
 190        }
 191
 48192        protected override void OnLoad(Action OnSuccess, Action OnFail) { loadCoroutine = CoroutineStarter.Start(LoadAss
 193
 194        IEnumerator WaitForConcurrentRequestsSlot()
 195        {
 24196            while (concurrentRequests >= MAX_CONCURRENT_REQUESTS)
 197            {
 0198                yield return null;
 199            }
 24200        }
 201
 202        void RegisterConcurrentRequest()
 203        {
 19204            if (requestRegistered)
 0205                return;
 206
 19207            concurrentRequests++;
 19208            OnDownloadingProgressUpdate?.Invoke();
 19209            requestRegistered = true;
 19210        }
 211
 212        void UnregisterConcurrentRequest()
 213        {
 24214            if (!requestRegistered)
 5215                return;
 216
 19217            concurrentRequests--;
 19218            OnDownloadingProgressUpdate?.Invoke();
 19219            requestRegistered = false;
 19220        }
 221    }
 222}