< 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
 21326        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 27        {
 21328            url = textureUrl;
 21329            wrapMode = textureWrapMode;
 21330            filterMode = textureFilterMode;
 21331            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 21332            this.storeTexAsNonReadable = storeTexAsNonReadable;
 21333            maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get();
 21334            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize);
 21335            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 21336        }
 37
 8338        protected override void OnAfterLoadOrReuse() { }
 39
 14940        protected override void OnBeforeLoadOrReuse() { }
 41
 14942        protected override object GetLibraryAssetCheckId() { return idWithTexSettings; }
 43
 44        protected override void OnCancelLoading()
 45        {
 6646            if (webRequestOp != null)
 6347                webRequestOp.Dispose();
 6648        }
 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
 13553            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 54            {
 255                OnSuccess?.Invoke();
 256                return;
 57            }
 58
 13359            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 60            {
 13261                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))
 171                                OnFail?.Invoke(new Exception("The texture is a question mark"));
 72                            else
 6773                                OnSuccess?.Invoke();
 6774                        }
 75                        else
 76                        {
 077                            OnFail?.Invoke(new Exception("The texture asset is null"));
 78                        }
 079                    },
 80                    OnFail: (webRequestResult) =>
 81                    {
 5182                        OnFail?.Invoke(new Exception($"Texture promise failed: {webRequestResult?.webRequest?.error}"));
 5183                    });
 13284            }
 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        {
 69108            if (storeDefaultTextureInAdvance && !UsesDefaultWrapAndFilterMode())
 109            {
 1110                if (!library.Contains(idWithDefaultTexSettings))
 111                {
 112                    // Save default texture asset
 1113                    asset.id = idWithDefaultTexSettings;
 1114                    asset.ConfigureTexture(DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, false);
 115
 1116                    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
 1125                var defaultTexAsset = library.Get(idWithDefaultTexSettings);
 1126                asset = defaultTexAsset.Clone() as Asset_Texture;
 1127                asset.dependencyAsset = defaultTexAsset;
 1128                asset.texture = TextureHelpers.CopyTexture(defaultTexAsset.texture);
 129            }
 130
 69131            asset.id = idWithTexSettings;
 69132            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 133
 69134            if (!library.Add(asset))
 135            {
 0136                Debug.Log("add to library fail?");
 0137                return false;
 138            }
 139
 69140            PerformanceAnalytics.PromiseTextureTracker.Track();
 141
 69142            asset = library.Get(asset.id);
 69143            return true;
 144        }
 145
 225146        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
 838151            return idWithDefaultTexSettings;
 152        }
 153
 259154        public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE
 155    }
 156}