< Summary

Class:DCL.AssetPromise_Texture
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Texture/AssetPromise_Texture.cs
Covered lines:49
Uncovered lines:4
Coverable lines:53
Total lines:147
Line coverage:92.4% (49 of 53)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_Texture(...)0%330100%
OnAfterLoadOrReuse()0%110100%
OnBeforeLoadOrReuse()0%110100%
GetLibraryAssetCheckId()0%110100%
OnCancelLoading()0%220100%
OnLoad(...)0%440100%
>c__DisplayClass20_0/<<OnLoad()0%660100%
AddToLibrary()0%6.346078.95%
ConstructId(...)0%110100%
GetId()0%110100%
UsesDefaultWrapAndFilterMode()0%220100%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using DCL.Helpers;
 4using MainScripts.DCL.Analytics.PerformanceAnalytics;
 5using MainScripts.DCL.Controllers.AssetManager;
 6using System.Threading;
 7using UnityEngine;
 8
 9namespace DCL
 10{
 11    public class AssetPromise_Texture : AssetPromise<Asset_Texture>
 12    {
 13        private const TextureWrapMode DEFAULT_WRAP_MODE = TextureWrapMode.Clamp;
 14        private const FilterMode DEFAULT_FILTER_MODE = FilterMode.Bilinear;
 15
 16        private readonly string idWithTexSettings;
 17        private readonly string idWithDefaultTexSettings;
 18        private readonly TextureWrapMode wrapMode;
 19        private readonly FilterMode filterMode;
 20        private readonly bool storeDefaultTextureInAdvance = false;
 21        private readonly bool storeTexAsNonReadable = false;
 22        private readonly int maxTextureSize;
 23        private readonly AssetSource permittedSources;
 24
 25        private CancellationTokenSource cancellationTokenSource;
 26
 27        private Service<ITextureAssetResolver> textureResolver;
 28
 34229        public string url { get; }
 30
 15231        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 32            int? overrideMaxTextureSize = null, AssetSource permittedSources = AssetSource.WEB)
 33        {
 15234            url = textureUrl;
 15235            wrapMode = textureWrapMode;
 15236            filterMode = textureFilterMode;
 15237            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 15238            this.storeTexAsNonReadable = storeTexAsNonReadable;
 15239            maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get();
 15240            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize);
 15241            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 15242            this.permittedSources = permittedSources;
 15243        }
 44
 8745        protected override void OnAfterLoadOrReuse() { }
 46
 15047        protected override void OnBeforeLoadOrReuse() { }
 48
 49        protected override object GetLibraryAssetCheckId()
 50        {
 15051            return idWithTexSettings;
 52        }
 53
 54        protected override void OnCancelLoading()
 55        {
 6356            cancellationTokenSource?.Cancel();
 6357        }
 58
 59        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 60        {
 61            // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi
 13362            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 63            {
 164                onSuccess?.Invoke();
 165                return;
 66            }
 67
 13268            cancellationTokenSource = new CancellationTokenSource();
 69
 70            async UniTaskVoid LaunchRequest()
 71            {
 39472                var result = await textureResolver.Ref.GetTextureAsync(permittedSources, url,
 73                    maxTextureSize, cancellationTokenSource.Token);
 74
 13275                if (result.IsSuccess)
 76                {
 6977                    var successResponse = result.GetSuccessResponse();
 6978                    asset.texture = successResponse.Texture;
 6979                    asset.resizingFactor = successResponse.ResizingFactor;
 6980                    onSuccess?.Invoke();
 81                }
 82                else
 83                {
 6384                    onFail?.Invoke(result.GetFailResponse().Exception);
 85                }
 13286            }
 87
 13288            LaunchRequest().Forget();
 13289        }
 90
 91        protected override bool AddToLibrary()
 92        {
 7093            if (storeDefaultTextureInAdvance && !UsesDefaultWrapAndFilterMode())
 94            {
 195                if (!library.Contains(idWithDefaultTexSettings))
 96                {
 97                    // Save default texture asset
 198                    asset.id = idWithDefaultTexSettings;
 199                    asset.ConfigureTexture(DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, false);
 100
 1101                    if (!library.Add(asset))
 102                    {
 0103                        Debug.Log("add to library fail?");
 0104                        return false;
 105                    }
 106                }
 107
 108                // By always using library.Get() for the default tex we have stored, we increase its references counter,
 109                // that will come in handy for removing that default tex when there is no one using it
 1110                var defaultTexAsset = library.Get(idWithDefaultTexSettings);
 1111                asset = defaultTexAsset.Clone() as Asset_Texture;
 1112                asset.dependencyAsset = defaultTexAsset;
 1113                asset.texture = TextureHelpers.CopyTexture(defaultTexAsset.texture);
 114            }
 115
 70116            asset.id = idWithTexSettings;
 70117            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 118
 70119            if (!library.Add(asset))
 120            {
 0121                Debug.Log("add to library fail?");
 0122                return false;
 123            }
 124
 70125            PerformanceAnalytics.PromiseTextureTracker.Track();
 126
 70127            asset = library.Get(asset.id);
 70128            return true;
 129        }
 130
 131        string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode, int maxSize
 132        {
 163133            return $"{((int)textureWrapMode)}{((int)textureFilterMode)}{textureUrl}{maxSize}";
 134        }
 135
 136        public override object GetId()
 137        {
 138            // We only use the id-with-settings when storing/reading from the library
 638139            return idWithDefaultTexSettings;
 140        }
 141
 142        public bool UsesDefaultWrapAndFilterMode()
 143        {
 197144            return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTER_MODE;
 145        }
 146    }
 147}