< 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
 35324        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 25        {
 35326            url = textureUrl;
 35327            wrapMode = textureWrapMode;
 35328            filterMode = textureFilterMode;
 35329            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 35330            this.storeTexAsNonReadable = storeTexAsNonReadable;
 35331            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE);
 35332            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 35333        }
 34
 5335        protected override void OnAfterLoadOrReuse() { }
 36
 9137        protected override void OnBeforeLoadOrReuse() { }
 38
 9139        protected override object GetLibraryAssetCheckId() { return idWithTexSettings; }
 40
 41        protected override void OnCancelLoading()
 42        {
 3543            if (webRequestOp != null)
 3444                webRequestOp.Dispose();
 3545        }
 46
 47        protected override void OnLoad(Action OnSuccess, Action OnFail)
 48        {
 49            // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi
 8650            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 51            {
 052                OnSuccess?.Invoke();
 053                return;
 54            }
 55
 8656            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 57            {
 8658                webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture(
 59                    url: url,
 60                    OnSuccess: (webRequestResult) =>
 61                    {
 4962                        if (asset != null)
 63                        {
 4964                            asset.texture = DownloadHandlerTexture.GetContent(webRequestResult);
 4965                            if (TextureUtils.IsQuestionMarkPNG(asset.texture))
 166                                OnFail?.Invoke();
 67                            else
 4868                                OnSuccess?.Invoke();
 4869                        }
 70                        else
 71                        {
 072                            OnFail?.Invoke();
 73                        }
 074                    },
 75                    OnFail: (webRequestResult) =>
 76                    {
 2077                        OnFail?.Invoke();
 2078                    });
 8679            }
 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        {
 4893            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
 48116            asset.id = idWithTexSettings;
 48117            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 118
 48119            if (!library.Add(asset))
 120            {
 0121                Debug.Log("add to library fail?");
 0122                return false;
 123            }
 124
 48125            asset = library.Get(asset.id);
 48126            return true;
 127        }
 128
 360129        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
 944134            return idWithDefaultTexSettings;
 135        }
 136
 384137        public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE
 138    }
 139}