< 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:52
Uncovered lines:15
Coverable lines:67
Total lines:155
Line coverage:77.6% (52 of 67)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_Texture()0%110100%
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%
IsQuestionMarkPNG(...)0%6.226081.82%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Texture/AssetPromise_Texture.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.Networking;
 4
 5namespace DCL
 6{
 7    public class AssetPromise_Texture : AssetPromise<Asset_Texture>
 8    {
 19        static readonly byte[] questionMarkPNG = { 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0
 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
 23        WebRequestAsyncOperation webRequestOp = null;
 24
 35625        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 26        {
 35627            url = textureUrl;
 35628            wrapMode = textureWrapMode;
 35629            filterMode = textureFilterMode;
 35630            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 35631            this.storeTexAsNonReadable = storeTexAsNonReadable;
 35632            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE);
 35633            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 35634        }
 35
 5236        protected override void OnAfterLoadOrReuse() { }
 37
 9538        protected override void OnBeforeLoadOrReuse() { }
 39
 9540        protected override object GetLibraryAssetCheckId() { return idWithTexSettings; }
 41
 42        protected override void OnCancelLoading()
 43        {
 4044            if (webRequestOp != null)
 3945                webRequestOp.Dispose();
 4046        }
 47
 48        protected override void OnLoad(Action OnSuccess, Action OnFail)
 49        {
 50            // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi
 9051            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 52            {
 053                OnSuccess?.Invoke();
 054                return;
 55            }
 56
 9057            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 58            {
 9059                webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture(
 60                    url: url,
 61                    OnSuccess: (webRequestResult) =>
 62                    {
 4863                        if (asset != null)
 64                        {
 4865                            asset.texture = DownloadHandlerTexture.GetContent(webRequestResult);
 4866                            if (IsQuestionMarkPNG(asset.texture))
 167                                OnFail?.Invoke();
 68                            else
 4769                                OnSuccess?.Invoke();
 4770                        }
 71                        else
 72                        {
 073                            OnFail?.Invoke();
 74                        }
 075                    },
 76                    OnFail: (webRequestResult) =>
 77                    {
 2678                        OnFail?.Invoke();
 2679                    });
 9080            }
 81            else
 82            {
 83                //For Base64 protocols we just take the bytes and create the texture
 84                //to avoid Unity's web request issue with large URLs
 085                byte[] decodedTexture = Convert.FromBase64String(url.Substring(PLAIN_BASE64_PROTOCOL.Length));
 086                asset.texture = new Texture2D(1, 1);
 087                asset.texture.LoadImage(decodedTexture);
 088                OnSuccess?.Invoke();
 89            }
 090        }
 91
 92        protected override bool AddToLibrary()
 93        {
 4794            if (storeDefaultTextureInAdvance && !UsesDefaultWrapAndFilterMode())
 95            {
 196                if (!library.Contains(idWithDefaultTexSettings))
 97                {
 98                    // Save default texture asset
 199                    asset.id = idWithDefaultTexSettings;
 1100                    asset.ConfigureTexture(DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, false);
 101
 1102                    if (!library.Add(asset))
 103                    {
 0104                        Debug.Log("add to library fail?");
 0105                        return false;
 106                    }
 107                }
 108
 109                // By always using library.Get() for the default tex we have stored, we increase its references counter,
 110                // that will come in handy for removing that default tex when there is no one using it
 1111                var defaultTexAsset = library.Get(idWithDefaultTexSettings);
 1112                asset = defaultTexAsset.Clone() as Asset_Texture;
 1113                asset.dependencyAsset = defaultTexAsset;
 1114                asset.texture = TextureHelpers.CopyTexture(defaultTexAsset.texture);
 115            }
 116
 47117            asset.id = idWithTexSettings;
 47118            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 119
 47120            if (!library.Add(asset))
 121            {
 0122                Debug.Log("add to library fail?");
 0123                return false;
 124            }
 125
 47126            asset = library.Get(asset.id);
 47127            return true;
 128        }
 129
 363130        string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode) { return ((
 131
 132        public override object GetId()
 133        {
 134            // We only use the id-with-settings when storing/reading from the library
 936135            return idWithDefaultTexSettings;
 136        }
 137
 387138        public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE
 139
 140        internal bool IsQuestionMarkPNG(Texture tex)
 141        {
 48142            if (!tex)
 0143                return true;
 144
 48145            if (tex.width != 8 || tex.height != 8)
 47146                return false;
 147
 1148            byte[] png1 = (tex as Texture2D).EncodeToPNG();
 246149            for (int i = 0; i < questionMarkPNG.Length; i++)
 122150                if (!png1[i].Equals(questionMarkPNG[i]))
 0151                    return false;
 1152            return true;
 153        }
 154    }
 155}