< Summary

Class:DCL.Providers.AssetBundleWebLoader
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/AB/Providers/AssetBundleWebLoader.cs
Covered lines:26
Uncovered lines:2
Coverable lines:28
Total lines:83
Line coverage:92.8% (26 of 28)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetBundleWebLoader(...)0%110100%
GetAssetBundleAsync()0%12.0512092.86%
ComputeHash(...)0%33092.31%
WaitForConcurrentRequestsSlot()0%110100%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using MainScripts.DCL.Controllers.AssetManager;
 3using System;
 4using System.Text;
 5using System.Threading;
 6using UnityEngine;
 7using UnityEngine.Pool;
 8
 9namespace DCL.Providers
 10{
 11    public class AssetBundleWebLoader : AssetBundleWebRequestBasedProvider, IAssetBundleProvider
 12    {
 13        private const string CACHING_FEATURE_FLAG = "ab_caching";
 14
 15        private Service<IWebRequestController> webRequestController;
 16        private readonly DataStore_FeatureFlag featureFlags;
 17        private readonly DataStore_Performance performance;
 18
 2919        private static int maxConcurrentRequests => CommonScriptableObjects.rendererState.Get() ? 30 : 256;
 5820        private bool cachingEnabled => featureFlags.flags.Get().IsFeatureEnabled(CACHING_FEATURE_FLAG);
 21
 69722        public AssetBundleWebLoader(DataStore_FeatureFlag featureFlags, DataStore_Performance performance)
 23        {
 69724            this.featureFlags = featureFlags;
 69725            this.performance = performance;
 69726        }
 27
 28        public async UniTask<AssetBundle> GetAssetBundleAsync(string contentUrl, string hash, CancellationToken cancella
 29        {
 8730            await WaitForConcurrentRequestsSlot();
 2931            performance.concurrentABRequests.Set(performance.concurrentABRequests.Get() + 1);
 32
 33            try
 34            {
 2935                var url = contentUrl + hash;
 2936                var hash128 = ComputeHash(contentUrl, hash);
 37
 2938                if (cachingEnabled)
 039                    AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Asset Bundle {hash} is cached: {Caching.
 40
 2941                using var webRequest = cachingEnabled
 42                    ? webRequestController.Ref.GetAssetBundle(url, hash: hash128, disposeOnCompleted: false)
 43                    : webRequestController.Ref.GetAssetBundle(url, disposeOnCompleted: false);
 44
 7945                return await FromWebRequestAsync(webRequest, url, cancellationToken);
 46            }
 47            finally
 48            {
 2949                performance.concurrentABRequests.Set(performance.concurrentABRequests.Get() - 1);
 50            }
 2251        }
 52
 53        // According to https://adr.decentraland.org/adr/ADR-11
 54        // we use /vX for versioning so caching system should respect it
 55        internal static Hash128 ComputeHash(string contentUrl, string hash)
 56        {
 2957            var hashBuilder = GenericPool<StringBuilder>.Get();
 2958            hashBuilder.Clear();
 2959            hashBuilder.Append(hash);
 60
 2961            var span = contentUrl.AsSpan();
 62
 63            // content URL always ends with '/'
 64            int indexOfVersionStart;
 65
 75466            for (indexOfVersionStart = span.Length - 2; span[indexOfVersionStart] != '/'; indexOfVersionStart--) { }
 67
 2968            indexOfVersionStart++;
 69
 2970            if (span[indexOfVersionStart] == 'v')
 071                hashBuilder.Insert(0, span.Slice(indexOfVersionStart, span.Length - indexOfVersionStart - 1));
 72
 2973            var hash128 = Hash128.Compute(hashBuilder.ToString());
 2974            GenericPool<StringBuilder>.Release(hashBuilder);
 2975            return hash128;
 76        }
 77
 78        private UniTask WaitForConcurrentRequestsSlot()
 79        {
 5880            return UniTask.WaitUntil(() => performance.concurrentABRequests.Get() < maxConcurrentRequests);
 81        }
 82    }
 83}