< Summary

Class:DCL.TextureAssetResolver
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Texture/Providers/TextureAssetResolver.cs
Covered lines:33
Uncovered lines:7
Coverable lines:40
Total lines:99
Line coverage:82.5% (33 of 40)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:3
Method coverage:100% (3 of 3)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TextureAssetResolver(...)0%110100%
GetTextureAsync()0%15.4715087.18%
LoadFromBase64(...)0%1.221040%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Texture/Providers/TextureAssetResolver.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using MainScripts.DCL.Controllers.AssetManager;
 3using MainScripts.DCL.Controllers.AssetManager.Texture;
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using UnityEngine;
 8
 9namespace DCL
 10{
 11    /// <summary>
 12    /// A higher level abstraction to load a texture from either source and apply additional constraints
 13    /// </summary>
 14    public class TextureAssetResolver : AssetResolverBase, ITextureAssetResolver
 15    {
 16        private const string PLAIN_BASE64_PROTOCOL = "data:text/plain;base64,";
 17
 18        private readonly IReadOnlyDictionary<AssetSource, ITextureAssetProvider> providers;
 19        private readonly BaseVariable<bool> isDebugMode;
 20
 74121        public TextureAssetResolver(IReadOnlyDictionary<AssetSource, ITextureAssetProvider> providers, DataStore_Feature
 22        {
 74123            this.providers = providers;
 74124        }
 25
 26        public async UniTask<TextureResponse> GetTextureAsync(AssetSource permittedSources, string url, int maxTextureSi
 27            CancellationToken cancellationToken = default)
 28        {
 11929            Exception lastException = null;
 11930            Texture2D texture = null;
 31
 11932            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 33            {
 11834                using var permittedSourcesRent = GetPermittedProviders(this.providers, permittedSources);
 11835                var permittedProviders = permittedSourcesRent.GetList();
 36
 35837                foreach (var provider in permittedProviders)
 38                {
 39                    try
 40                    {
 35441                        texture = await provider.GetTextureAsync(url, cancellationToken);
 42
 10343                        if (texture)
 44                        {
 10345                            if (TextureUtils.IsQuestionMarkPNG(texture))
 46                            {
 047                                lastException = new Exception("The texture is a question mark");
 048                                continue;
 49                            }
 50
 51                            // The valid texture is retrieved
 10352                            AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provi
 10353                            break;
 54                        }
 55
 056                        AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provider}
 057                    }
 1558                    catch (Exception e)
 59                    {
 1560                        lastException = e;
 61
 1562                        AssetResolverLogger.LogVerbose(featureFlags, e);
 63
 64                        // Propagate `OperationCanceledException` further as there is no reason to iterate
 1565                        if (e is OperationCanceledException)
 1166                            break;
 467                    }
 468                }
 11869            }
 70            else
 71            {
 172                try { texture = LoadFromBase64(url); }
 373                catch (Exception e) { lastException = e; }
 74            }
 75
 11976            if (texture)
 77            {
 10378                Texture2D clampedTexture = TextureHelpers.ClampSize(texture, maxTextureSize, linear: linear, useGPUCopy:
 10379                float resizingFactor = Mathf.Min(1, TextureHelpers.GetScalingFactor(texture.width, texture.height, maxTe
 80
 10381                return new TextureResponse(new TextureSuccessResponse(clampedTexture, resizingFactor));
 82            }
 83
 1684            lastException ??= new AssetNotFoundException(permittedSources, url);
 1685            return new TextureResponse(new TextureFailResponse(lastException));
 11986        }
 87
 88        //For Base64 protocols we just take the bytes and create the texture
 89        //to avoid Unity's web request issue with large URLs
 90        private Texture2D LoadFromBase64(string url)
 91        {
 192            string substring = url.Substring(PLAIN_BASE64_PROTOCOL.Length);
 193            byte[] decodedTexture = Convert.FromBase64String(substring);
 094            var texture = new Texture2D(1, 1);
 095            texture.LoadImage(decodedTexture);
 096            return texture;
 97        }
 98    }
 99}