< 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
 32225        public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t
 26        {
 32227            url = textureUrl;
 32228            wrapMode = textureWrapMode;
 32229            filterMode = textureFilterMode;
 32230            this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance;
 32231            this.storeTexAsNonReadable = storeTexAsNonReadable;
 32232            idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE);
 32233            idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f
 32234        }
 35
 3636        protected override void OnAfterLoadOrReuse() { }
 37
 7138        protected override void OnBeforeLoadOrReuse() { }
 39
 7140        protected override object GetLibraryAssetCheckId() { return idWithTexSettings; }
 41
 42        protected override void OnCancelLoading()
 43        {
 3344            if (webRequestOp != null)
 3345                webRequestOp.Dispose();
 3346        }
 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
 6651            if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode())
 52            {
 053                OnSuccess?.Invoke();
 054                return;
 55            }
 56
 6657            if (!url.StartsWith(PLAIN_BASE64_PROTOCOL))
 58            {
 6659                webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture(
 60                    url: url,
 61                    OnSuccess: (webRequestResult) =>
 62                    {
 3263                        if (asset != null)
 64                        {
 3265                            asset.texture = DownloadHandlerTexture.GetContent(webRequestResult);
 3266                            if (IsQuestionMarkPNG(asset.texture))
 167                                OnFail?.Invoke();
 68                            else
 3169                                OnSuccess?.Invoke();
 3170                        }
 71                        else
 72                        {
 073                            OnFail?.Invoke();
 74                        }
 075                    },
 76                    OnFail: (webRequestResult) =>
 77                    {
 2078                        OnFail?.Invoke();
 2079                    });
 6680            }
 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        {
 3194            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
 31117            asset.id = idWithTexSettings;
 31118            asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable);
 119
 31120            if (!library.Add(asset))
 121            {
 0122                Debug.Log("add to library fail?");
 0123                return false;
 124            }
 125
 31126            asset = library.Get(asset.id);
 31127            return true;
 128        }
 129
 329130        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
 825135            return idWithDefaultTexSettings;
 136        }
 137
 340138        public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE
 139
 140        internal bool IsQuestionMarkPNG(Texture tex)
 141        {
 32142            if (!tex)
 0143                return true;
 144
 32145            if (tex.width != 8 || tex.height != 8)
 31146                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}