< 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:35
Uncovered lines:5
Coverable lines:40
Total lines:100
Line coverage:87.5% (35 of 40)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TextureAssetResolver(...)0%110100%
GetTextureAsync()0%15.115092.31%
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
 69721        public TextureAssetResolver(IReadOnlyDictionary<AssetSource, ITextureAssetProvider> providers, DataStore_Feature
 22        {
 69723            this.providers = providers;
 69724        }
 25
 26        public async UniTask<TextureResponse> GetTextureAsync(AssetSource permittedSources, string url, int maxTextureSi
 27        {
 13228            Exception lastException = null;
 13229            Texture2D texture = null;
 30
 13231            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 32            {
 13133                using var permittedSourcesRent = GetPermittedProviders(this.providers, permittedSources);
 13134                var permittedProviders = permittedSourcesRent.GetList();
 35
 53836                foreach (var provider in permittedProviders)
 37                {
 38                    try
 39                    {
 53440                        texture = await provider.GetTextureAsync(url, cancellationToken);
 41
 11642                        if (texture)
 43                        {
 6944                            if (TextureUtils.IsQuestionMarkPNG(texture))
 45                            {
 046                                lastException = new Exception("The texture is a question mark");
 047                                continue;
 48                            }
 49
 50                            // The valid texture is retrieved
 6951                            AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provi
 6952                            break;
 53                        }
 54
 4755                        AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provider}
 4756                    }
 6257                    catch (Exception e)
 58                    {
 6259                        lastException = e;
 60
 6261                        AssetResolverLogger.LogVerbose(featureFlags, e);
 62
 63                        // Propagate `OperationCanceledException` further as there is no reason to iterate
 6264                        if (e is OperationCanceledException)
 1165                            break;
 5166                    }
 9867                }
 13168            }
 69            else
 70            {
 171                try { texture = LoadFromBase64(url); }
 372                catch (Exception e) { lastException = e; }
 73            }
 74
 13275            if (texture)
 76            {
 6977                var clampedTexture = TextureHelpers.ClampSize(texture, maxTextureSize, useGPUCopy: false);
 78
 6979                var resizingFactor = Mathf.Min(1,
 80                    TextureHelpers.GetScalingFactor(texture.width, texture.height, maxTextureSize));
 81
 6982                return new TextureResponse(new TextureSuccessResponse(clampedTexture, resizingFactor));
 83            }
 84
 6385            lastException ??= new AssetNotFoundException(permittedSources, url);
 6386            return new TextureResponse(new TextureFailResponse(lastException));
 13287        }
 88
 89        //For Base64 protocols we just take the bytes and create the texture
 90        //to avoid Unity's web request issue with large URLs
 91        private Texture2D LoadFromBase64(string url)
 92        {
 193            string substring = url.Substring(PLAIN_BASE64_PROTOCOL.Length);
 194            byte[] decodedTexture = Convert.FromBase64String(substring);
 095            var texture = new Texture2D(1, 1);
 096            texture.LoadImage(decodedTexture);
 097            return texture;
 98        }
 99    }
 100}