< 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:56
Uncovered lines:12
Coverable lines:68
Total lines:156
Line coverage:82.3% (56 of 68)
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%8.817066.67%
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 System;
 2using DCL.Helpers;
 3using MainScripts.DCL.Analytics.PerformanceAnalytics;
 4using UnityEngine;
 5using UnityEngine.Networking;
 6
 7namespace DCL
 8{
 9    public class AssetPromise_Texture : AssetPromise<Asset_Texture>
 10    {
 11        const TextureWrapMode DEFAULT_WRAP_MODE = TextureWrapMode.Clamp;
 12        const FilterMode DEFAULT_FILTER_MODE = FilterMode.Bilinear;
 13        private const string PLAIN_BASE64_PROTOCOL = "data:text/plain;base64,";
 14
 15        string url;
 16        string idWithTexSettings;
 17        string idWithDefaultTexSettings;
 18        TextureWrapMode wrapMode;
 19        FilterMode filterMode;
 20        bool storeDefaultTextureInAdvance = false;
 21        bool storeTexAsNonReadable = false;
 22        private readonly int maxTextureSize;
 23
 24        WebRequestAsyncOperation webRequestOp = null;
 25
 21926        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 27        {
 21928            url = textureUrl;
 21929            wrapMode = textureWrapMode;
 21930            filterMode = textureFilterMode;
 21931            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 21932            this.storeTexAsNonReadable = storeTexAsNonReadable;
 21933            maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get();
 21934            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize);
 21935            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 21936        }
 37
 8238        protected override void OnAfterLoadOrReuse() { }
 39
 15540        protected override void OnBeforeLoadOrReuse() { }
 41
 15542        protected override object GetLibraryAssetCheckId() { return idWithTexSettings; }
 43
 44        protected override void OnCancelLoading()
 45        {
 7346            if (webRequestOp != null)
 7047                webRequestOp.Dispose();
 7348        }
 49
 50        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 51        {
 52            // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi
 14153            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 54            {
 255                OnSuccess?.Invoke();
 256                return;
 57            }
 58
 13959            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 60            {
 13861                webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture(
 62                    url: url,
 63                    OnSuccess: (webRequestResult) =>
 64                    {
 6865                        if (asset != null)
 66                        {
 6867                            Texture2D texture = DownloadHandlerTexture.GetContent(webRequestResult.webRequest);
 6868                            asset.texture = TextureHelpers.ClampSize(texture, maxTextureSize, useGPUCopy: false);
 6869                            asset.resizingFactor = Mathf.Min(1, TextureHelpers.GetScalingFactor(texture.width, texture.h
 6870                            if (TextureUtils.IsQuestionMarkPNG(asset.texture))
 271                                OnFail?.Invoke(new Exception("The texture is a question mark"));
 72                            else
 6673                                OnSuccess?.Invoke();
 6674                        }
 75                        else
 76                        {
 077                            OnFail?.Invoke(new Exception("The texture asset is null"));
 78                        }
 079                    },
 80                    OnFail: (webRequestResult) =>
 81                    {
 5482                        OnFail?.Invoke(new Exception($"Texture promise failed: {webRequestResult?.webRequest?.error}"));
 5483                    });
 13884            }
 85            else
 86            {
 87                //For Base64 protocols we just take the bytes and create the texture
 88                //to avoid Unity's web request issue with large URLs
 89                try {
 190                    string substring = url.Substring(PLAIN_BASE64_PROTOCOL.Length);
 191                    byte[] decodedTexture = Convert.FromBase64String(substring);
 092                    asset.texture = new Texture2D(1, 1);
 093                    asset.texture.LoadImage(decodedTexture);
 094                    asset.resizingFactor = TextureHelpers.GetScalingFactor(asset.texture.width, asset.texture.height, ma
 095                    asset.texture = TextureHelpers.ClampSize(asset.texture, maxTextureSize);
 096                    OnSuccess?.Invoke();
 097                }
 198                catch (Exception e)
 99                {
 1100                    OnFail?.Invoke(e);
 1101                }
 102
 103            }
 1104        }
 105
 106        protected override bool AddToLibrary()
 107        {
 68108            if (storeDefaultTextureInAdvance && !UsesDefaultWrapAndFilterMode())
 109            {
 17110                if (!library.Contains(idWithDefaultTexSettings))
 111                {
 112                    // Save default texture asset
 15113                    asset.id = idWithDefaultTexSettings;
 15114                    asset.ConfigureTexture(DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, false);
 115
 15116                    if (!library.Add(asset))
 117                    {
 0118                        Debug.Log("add to library fail?");
 0119                        return false;
 120                    }
 121                }
 122
 123                // By always using library.Get() for the default tex we have stored, we increase its references counter,
 124                // that will come in handy for removing that default tex when there is no one using it
 17125                var defaultTexAsset = library.Get(idWithDefaultTexSettings);
 17126                asset = defaultTexAsset.Clone() as Asset_Texture;
 17127                asset.dependencyAsset = defaultTexAsset;
 17128                asset.texture = TextureHelpers.CopyTexture(defaultTexAsset.texture);
 129            }
 130
 68131            asset.id = idWithTexSettings;
 68132            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 133
 68134            if (!library.Add(asset))
 135            {
 0136                Debug.Log("add to library fail?");
 0137                return false;
 138            }
 139
 68140            PerformanceAnalytics.PromiseTextureTracker.Track();
 141
 68142            asset = library.Get(asset.id);
 68143            return true;
 144        }
 145
 251146        string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode, int maxSize
 147
 148        public override object GetId()
 149        {
 150            // We only use the id-with-settings when storing/reading from the library
 856151            return idWithDefaultTexSettings;
 152        }
 153
 281154        public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE
 155    }
 156}