< 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:52
Uncovered lines:4
Coverable lines:56
Total lines:155
Line coverage:92.8% (52 of 56)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:12
Method coverage:100% (12 of 12)

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__DisplayClass23_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 bool? overrideCompression;
 23        private readonly bool linear;
 24        private readonly bool useGPUCopy;
 25        private readonly int maxTextureSize;
 26        private readonly AssetSource permittedSources;
 27
 28        private CancellationTokenSource cancellationTokenSource;
 29
 30        private Service<ITextureAssetResolver> textureResolver;
 31
 30132        public string url { get; }
 33
 16734        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 35            bool storeDefaultTextureInAdvance = false, bool storeTexAsNonReadable = true,
 36            int? overrideMaxTextureSize = null, AssetSource permittedSources = AssetSource.WEB,
 37            bool? overrideCompression = null, bool linear = false, bool useGPUCopy = false)
 38        {
 16739            url = textureUrl;
 16740            wrapMode = textureWrapMode;
 16741            filterMode = textureFilterMode;
 16742            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 16743            this.storeTexAsNonReadable = storeTexAsNonReadable;
 16744            this.overrideCompression = overrideCompression;
 16745            this.linear = linear;
 16746            this.useGPUCopy = useGPUCopy;
 16747            maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get();
 16748            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize);
 16749            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 16750            this.permittedSources = permittedSources;
 16751        }
 52
 12153        protected override void OnAfterLoadOrReuse() { }
 54
 14155        protected override void OnBeforeLoadOrReuse() { }
 56
 57        protected override object GetLibraryAssetCheckId()
 58        {
 14159            return idWithTexSettings;
 60        }
 61
 62        protected override void OnCancelLoading()
 63        {
 2064            cancellationTokenSource?.Cancel();
 2065        }
 66
 67        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 68        {
 69            // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi
 12470            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 71            {
 172                onSuccess?.Invoke();
 173                return;
 74            }
 75
 12376            cancellationTokenSource = new CancellationTokenSource();
 77
 78            async UniTaskVoid LaunchRequest()
 79            {
 35980                MainScripts.DCL.Controllers.AssetManager.Texture.TextureResponse result = await textureResolver.Ref.GetT
 81                    maxTextureSize, linear, useGPUCopy, cancellationTokenSource.Token);
 82
 11983                if (result.IsSuccess)
 84                {
 10385                    var successResponse = result.GetSuccessResponse();
 10386                    asset.texture = successResponse.Texture;
 10387                    asset.resizingFactor = successResponse.ResizingFactor;
 10388                    onSuccess?.Invoke();
 89                }
 90                else
 91                {
 1692                    onFail?.Invoke(result.GetFailResponse().Exception);
 93                }
 11994            }
 95
 12396            LaunchRequest().Forget();
 12397        }
 98
 99        protected override bool AddToLibrary()
 100        {
 104101            if (storeDefaultTextureInAdvance && !UsesDefaultWrapAndFilterMode())
 102            {
 1103                if (!library.Contains(idWithDefaultTexSettings))
 104                {
 105                    // Save default texture asset
 1106                    asset.id = idWithDefaultTexSettings;
 1107                    asset.ConfigureTexture(DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, overrideCompression, false);
 108
 1109                    if (!library.Add(asset))
 110                    {
 0111                        Debug.Log("add to library fail?");
 0112                        return false;
 113                    }
 114                }
 115
 116                // By always using library.Get() for the default tex we have stored, we increase its references counter,
 117                // that will come in handy for removing that default tex when there is no one using it
 1118                var defaultTexAsset = library.Get(idWithDefaultTexSettings);
 1119                asset = defaultTexAsset.Clone() as Asset_Texture;
 1120                asset.dependencyAsset = defaultTexAsset;
 1121                asset.texture = TextureHelpers.CopyTexture(defaultTexAsset.texture);
 122            }
 123
 104124            asset.id = idWithTexSettings;
 104125            asset.ConfigureTexture(wrapMode, filterMode, overrideCompression, storeTexAsNonReadable);
 126
 104127            if (!library.Add(asset))
 128            {
 0129                Debug.Log("add to library fail?");
 0130                return false;
 131            }
 132
 104133            PerformanceAnalytics.PromiseTextureTracker.Track();
 134
 104135            asset = library.Get(asset.id);
 104136            return true;
 137        }
 138
 139        string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode, int maxSize
 140        {
 178141            return $"{((int)textureWrapMode)}{((int)textureFilterMode)}{textureUrl}{maxSize}";
 142        }
 143
 144        public override object GetId()
 145        {
 146            // We only use the id-with-settings when storing/reading from the library
 683147            return idWithDefaultTexSettings;
 148        }
 149
 150        public bool UsesDefaultWrapAndFilterMode()
 151        {
 212152            return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTER_MODE;
 153        }
 154    }
 155}