< 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:53
Uncovered lines:13
Coverable lines:66
Total lines:156
Line coverage:80.3% (53 of 66)
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%9.157064.71%
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
 20526        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 27        {
 20528            url = textureUrl;
 20529            wrapMode = textureWrapMode;
 20530            filterMode = textureFilterMode;
 20531            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 20532            this.storeTexAsNonReadable = storeTexAsNonReadable;
 20533            maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get();
 20534            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize);
 20535            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 20536        }
 37
 7838        protected override void OnAfterLoadOrReuse() { }
 39
 14440        protected override void OnBeforeLoadOrReuse() { }
 41
 14442        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
 13053            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 54            {
 255                OnSuccess?.Invoke();
 256                return;
 57            }
 58
 12859            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 60            {
 12761                webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture(
 62                    url: url,
 63                    OnSuccess: (webRequestResult) =>
 64                    {
 6265                        if (asset != null)
 66                        {
 6267                            Texture2D texture = DownloadHandlerTexture.GetContent(webRequestResult.webRequest);
 6268                            asset.texture = TextureHelpers.ClampSize(texture, maxTextureSize, useGPUCopy: false);
 6269                            asset.resizingFactor = Mathf.Min(1, TextureHelpers.GetScalingFactor(texture.width, texture.h
 6270                            if (TextureUtils.IsQuestionMarkPNG(asset.texture))
 071                                OnFail?.Invoke(new Exception("The texture is a question mark"));
 72                            else
 6273                                OnSuccess?.Invoke();
 74                        }
 75                        else
 76                        {
 077                            OnFail?.Invoke(new Exception("The texture asset is null"));
 78                        }
 079                    },
 80                    OnFail: (webRequestResult) =>
 81                    {
 5282                        OnFail?.Invoke(new Exception($"Texture promise failed: {webRequestResult?.webRequest?.error}"));
 5283                    });
 84            }
 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        {
 64108            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
 64131            asset.id = idWithTexSettings;
 64132            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 133
 64134            if (!library.Add(asset))
 135            {
 0136                Debug.Log("add to library fail?");
 0137                return false;
 138            }
 139
 64140            PerformanceAnalytics.PromiseTextureTracker.Track();
 141
 64142            asset = library.Get(asset.id);
 64143            return true;
 144        }
 145
 217146        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
 807151            return idWithDefaultTexSettings;
 152        }
 153
 246154        public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE
 155    }
 156}