< 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:44
Uncovered lines:13
Coverable lines:57
Total lines:139
Line coverage:77.1% (44 of 57)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_Texture(...)0%220100%
OnAfterLoadOrReuse()0%110100%
OnBeforeLoadOrReuse()0%110100%
GetLibraryAssetCheckId()0%110100%
OnCancelLoading()0%220100%
OnLoad(...)0%15.286036.36%
AddToLibrary()0%6.396077.78%
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 UnityEngine;
 4using UnityEngine.Networking;
 5
 6namespace DCL
 7{
 8    public class AssetPromise_Texture : AssetPromise<Asset_Texture>
 9    {
 10        const TextureWrapMode DEFAULT_WRAP_MODE = TextureWrapMode.Clamp;
 11        const FilterMode DEFAULT_FILTER_MODE = FilterMode.Bilinear;
 12        private const string PLAIN_BASE64_PROTOCOL = "data:text/plain;base64,";
 13
 14        string url;
 15        string idWithTexSettings;
 16        string idWithDefaultTexSettings;
 17        TextureWrapMode wrapMode;
 18        FilterMode filterMode;
 19        bool storeDefaultTextureInAdvance = false;
 20        bool storeTexAsNonReadable = false;
 21
 22        WebRequestAsyncOperation webRequestOp = null;
 23
 27624        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 25        {
 27626            url = textureUrl;
 27627            wrapMode = textureWrapMode;
 27628            filterMode = textureFilterMode;
 27629            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 27630            this.storeTexAsNonReadable = storeTexAsNonReadable;
 27631            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE);
 27632            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 27633        }
 34
 6735        protected override void OnAfterLoadOrReuse() { }
 36
 12537        protected override void OnBeforeLoadOrReuse() { }
 38
 12539        protected override object GetLibraryAssetCheckId() { return idWithTexSettings; }
 40
 41        protected override void OnCancelLoading()
 42        {
 5843            if (webRequestOp != null)
 5644                webRequestOp.Dispose();
 5845        }
 46
 47        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 48        {
 49            // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi
 11050            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 51            {
 052                OnSuccess?.Invoke();
 053                return;
 54            }
 55
 11056            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 57            {
 11058                webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture(
 59                    url: url,
 60                    OnSuccess: (webRequestResult) =>
 61                    {
 5362                        if (asset != null)
 63                        {
 5364                            asset.texture = DownloadHandlerTexture.GetContent(webRequestResult.webRequest);
 5365                            if (TextureUtils.IsQuestionMarkPNG(asset.texture))
 166                                OnFail?.Invoke(new Exception("The texture is a question mark"));
 67                            else
 5268                                OnSuccess?.Invoke();
 5269                        }
 70                        else
 71                        {
 072                            OnFail?.Invoke(new Exception("The texture asset is null"));
 73                        }
 074                    },
 75                    OnFail: (webRequestResult) =>
 76                    {
 4977                        OnFail?.Invoke(new Exception($"Texture promise failed: {webRequestResult?.webRequest?.error}"));
 4978                    });
 11079            }
 80            else
 81            {
 82                //For Base64 protocols we just take the bytes and create the texture
 83                //to avoid Unity's web request issue with large URLs
 084                byte[] decodedTexture = Convert.FromBase64String(url.Substring(PLAIN_BASE64_PROTOCOL.Length));
 085                asset.texture = new Texture2D(1, 1);
 086                asset.texture.LoadImage(decodedTexture);
 087                OnSuccess?.Invoke();
 88            }
 089        }
 90
 91        protected override bool AddToLibrary()
 92        {
 5293            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
 52116            asset.id = idWithTexSettings;
 52117            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 118
 52119            if (!library.Add(asset))
 120            {
 0121                Debug.Log("add to library fail?");
 0122                return false;
 123            }
 124
 52125            asset = library.Get(asset.id);
 52126            return true;
 127        }
 128
 283129        string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode) { return ((
 130
 131        public override object GetId()
 132        {
 133            // We only use the id-with-settings when storing/reading from the library
 894134            return idWithDefaultTexSettings;
 135        }
 136
 320137        public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE
 138    }
 139}