< 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:48
Uncovered lines:8
Coverable lines:56
Total lines:131
Line coverage:85.7% (48 of 56)
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%4.184077.78%
OnBeforeLoadOrReuse()0%110100%
OnAfterLoadOrReuse()0%110100%
LoadAssetBundleWithDeps()0%18.0918093.55%
SetAssetBundle(...)0%110100%
OnLoad(...)0%330100%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Providers;
 3using MainScripts.DCL.Controllers.AssetManager;
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using UnityEngine;
 8
 9namespace DCL
 10{
 11    public class AssetPromise_AB : AssetPromise_WithUrl<Asset_AB>
 12    {
 13        public static event Action OnDownloadingProgressUpdate;
 014        public static int queueCount => AssetPromiseKeeper_AB.i.waitingPromisesCount;
 15
 116        static HashSet<string> failedRequestUrls = new ();
 17
 4318        List<AssetPromise_AB> dependencyPromises = new ();
 19
 120        public static AssetBundlesLoader assetBundlesLoader = new ();
 21
 22        private readonly Transform containerTransform;
 23        private readonly AssetSource permittedSources;
 24
 25        private CancellationTokenSource cancellationTokenSource;
 26
 27        private Service<IAssetBundleResolver> assetBundleResolver;
 28
 29        public AssetPromise_AB(string contentUrl, string hash,
 4330            Transform containerTransform = null, AssetSource permittedSources = AssetSource.ALL) : base(contentUrl,
 31            hash)
 32        {
 4333            this.containerTransform = containerTransform;
 4334            this.permittedSources = permittedSources;
 4335            assetBundlesLoader.Start();
 4336        }
 37
 38        protected override bool AddToLibrary()
 39        {
 2240            if (!library.Add(asset))
 41            {
 042                Debug.Log("add to library fail?");
 43
 044                return false;
 45            }
 46
 2247            if (asset == null)
 48            {
 049                Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}");
 50
 051                return false;
 52            }
 53
 2254            asset = library.Get(asset.id);
 55
 2256            return true;
 57        }
 58
 59        protected override void OnCancelLoading()
 60        {
 861            cancellationTokenSource?.Cancel();
 862            cancellationTokenSource = null;
 63
 1664            foreach (var t in dependencyPromises)
 065                t.Unload();
 66
 867            dependencyPromises.Clear();
 68
 869            asset?.CancelShow();
 870        }
 71
 4372        protected override void OnBeforeLoadOrReuse() { }
 73
 3574        protected override void OnAfterLoadOrReuse() { }
 75
 76        private async UniTask LoadAssetBundleWithDeps(string baseUrl, string hash, Action onSuccess, Action<Exception> o
 77        {
 2978            var finalUrl = baseUrl + hash;
 79
 2980            if (failedRequestUrls.Contains(finalUrl))
 81            {
 082                onFail?.Invoke(new Exception($"The url {finalUrl} has failed"));
 083                return;
 84            }
 85
 86            try
 87            {
 8788                var bundle = await assetBundleResolver.Ref.GetAssetBundleAsync(permittedSources, baseUrl, hash, cancella
 2289                SetAssetBundle(bundle);
 90
 2891                var dependencies = await bundle.GetDependenciesAsync(baseUrl, hash, cancellationToken);
 92
 7093                foreach (string dependency in dependencies)
 94                {
 1395                    var promise = new AssetPromise_AB(baseUrl, dependency, containerTransform, permittedSources);
 1396                    AssetPromiseKeeper_AB.i.Keep(promise);
 1397                    dependencyPromises.Add(promise);
 98                }
 99
 70100                foreach (var dependencyPromise in dependencyPromises)
 17101                    await dependencyPromise;
 102
 22103                assetBundlesLoader.MarkAssetBundleForLoad(asset, containerTransform, onSuccess, onFail);
 22104            }
 7105            catch (Exception e)
 106            {
 7107                if (e is not OperationCanceledException)
 2108                    failedRequestUrls.Add(finalUrl);
 109
 7110                onFail?.Invoke(e);
 7111            }
 29112        }
 113
 114        private void SetAssetBundle(AssetBundle assetBundle)
 115        {
 22116            asset.SetAssetBundle(assetBundle);
 22117            asset.LoadMetrics();
 22118        }
 119
 120        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 121        {
 30122            if (cancellationTokenSource != null)
 1123                return;
 124
 29125            cancellationTokenSource = new CancellationTokenSource();
 29126            OnDownloadingProgressUpdate?.Invoke();
 127
 29128            LoadAssetBundleWithDeps(contentUrl, hash, OnSuccess, OnFail, cancellationTokenSource.Token).Forget();
 29129        }
 130    }
 131}