< Summary

Class:DCL.DCLTexture
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Textures/DCLTexture.cs
Covered lines:47
Uncovered lines:24
Coverable lines:71
Total lines:184
Line coverage:66.1% (47 of 71)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
GetClassId()0%2100%
DCLTexture()0%110100%
FetchFromComponent()0%12300%
FetchTextureComponent()0%5.25080%
ApplyChanges()0%43.9419058.97%
AttachTo(...)0%110100%
AttachTo(...)0%110100%
AttachTo(...)0%110100%
DetachFrom(...)0%2100%
DetachFrom(...)0%110100%
DetachFrom(...)0%110100%
AddRefCount()0%2100%
RemoveRefCount()0%2.152066.67%
Dispose()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Textures/DCLTexture.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Controllers;
 3using DCL.Models;
 4using System;
 5using System.Collections;
 6using DCL.Helpers;
 7using UnityEngine;
 8using System.Collections.Generic;
 9
 10namespace DCL
 11{
 12    public class DCLTexture : BaseDisposable
 13    {
 14        [System.Serializable]
 15        public class Model : BaseModel
 16        {
 17            public string src;
 18            public BabylonWrapMode wrap = BabylonWrapMode.CLAMP;
 3619            public FilterMode samplingMode = FilterMode.Bilinear;
 20            public bool hasAlpha = false;
 21
 3422            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 23        }
 24
 25        public enum BabylonWrapMode
 26        {
 27            CLAMP,
 28            WRAP,
 29            MIRROR
 30        }
 31
 32        AssetPromise_Texture texturePromise = null;
 33
 34        public TextureWrapMode unityWrap;
 35        public FilterMode unitySamplingMode;
 36        public Texture2D texture;
 37        protected bool isDisposed;
 38
 039        public override int GetClassId() { return (int) CLASS_ID.TEXTURE; }
 40
 14741        public DCLTexture() { model = new Model(); }
 42
 43        public static IEnumerator FetchFromComponent(IParcelScene scene, string componentId,
 44            System.Action<Texture2D> OnFinish)
 45        {
 046            yield return FetchTextureComponent(scene, componentId, (dclTexture) => { OnFinish?.Invoke(dclTexture.texture
 047        }
 48
 49        public static IEnumerator FetchTextureComponent(IParcelScene scene, string componentId,
 50            System.Action<DCLTexture> OnFinish)
 51        {
 4452            if (!scene.disposableComponents.ContainsKey(componentId))
 53            {
 154                Debug.Log($"couldn't fetch texture, the DCLTexture component with id {componentId} doesn't exist");
 155                yield break;
 56            }
 57
 4358            DCLTexture textureComponent = scene.disposableComponents[componentId] as DCLTexture;
 59
 4360            if (textureComponent == null)
 61            {
 062                Debug.Log($"couldn't fetch texture, the shared component with id {componentId} is NOT a DCLTexture");
 063                yield break;
 64            }
 65
 8966            yield return new WaitUntil(() => textureComponent.texture != null);
 67
 4368            OnFinish.Invoke(textureComponent);
 4369        }
 70
 71        public override IEnumerator ApplyChanges(BaseModel newModel)
 72        {
 6873            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 74
 75            //If the scene creates and destroy the component before our renderer has been turned on bad things happen!
 76            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 3477            if (isDisposed)
 078                yield break;
 79
 3480            Model model = (Model) newModel;
 81
 3482            unitySamplingMode = model.samplingMode;
 83
 3484            switch (model.wrap)
 85            {
 86                case BabylonWrapMode.CLAMP:
 3387                    unityWrap = TextureWrapMode.Clamp;
 3388                    break;
 89                case BabylonWrapMode.WRAP:
 090                    unityWrap = TextureWrapMode.Repeat;
 091                    break;
 92                case BabylonWrapMode.MIRROR:
 193                    unityWrap = TextureWrapMode.Mirror;
 94                    break;
 95            }
 96
 3497            if (texture == null && !string.IsNullOrEmpty(model.src))
 98            {
 3299                bool isBase64 = model.src.Contains("image/png;base64");
 100
 32101                if (isBase64)
 102                {
 0103                    string base64Data = model.src.Substring(model.src.IndexOf(',') + 1);
 104
 105                    // The used texture variable can't be null for the ImageConversion.LoadImage to work
 0106                    if (texture == null)
 107                    {
 0108                        texture = new Texture2D(1, 1);
 109                    }
 110
 0111                    if (!ImageConversion.LoadImage(texture, Convert.FromBase64String(base64Data)))
 112                    {
 0113                        Debug.LogError($"DCLTexture with id {id} couldn't parse its base64 image data.");
 114                    }
 115
 0116                    if (texture != null)
 117                    {
 0118                        texture.wrapMode = unityWrap;
 0119                        texture.filterMode = unitySamplingMode;
 0120                        texture.Compress(false);
 0121                        texture.Apply(unitySamplingMode != FilterMode.Point, true);
 122                    }
 0123                }
 124                else
 125                {
 32126                    string contentsUrl = string.Empty;
 32127                    bool isExternalURL = model.src.Contains("http://") || model.src.Contains("https://");
 128
 32129                    if (isExternalURL)
 0130                        contentsUrl = model.src;
 131                    else
 32132                        scene.contentProvider.TryGetContentsUrl(model.src, out contentsUrl);
 133
 32134                    if (!string.IsNullOrEmpty(contentsUrl))
 135                    {
 32136                        if (texturePromise != null)
 0137                            AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 138
 32139                        texturePromise = new AssetPromise_Texture(contentsUrl, unityWrap, unitySamplingMode, storeDefaul
 63140                        texturePromise.OnSuccessEvent += (x) => texture = x.texture;
 32141                        texturePromise.OnFailEvent += (x) => { texture = null; };
 142
 32143                        AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 32144                        yield return texturePromise;
 145                    }
 146                }
 147            }
 34148        }
 149
 150        private int refCount;
 151
 38152        public virtual void AttachTo(PBRMaterial material) { AddRefCount(); }
 153
 10154        public virtual void AttachTo(BasicMaterial material) { AddRefCount(); }
 155
 18156        public virtual void AttachTo(UIImage image) { AddRefCount(); }
 157
 0158        public virtual void DetachFrom(PBRMaterial material) { RemoveRefCount(); }
 159
 32160        public virtual void DetachFrom(BasicMaterial material) { RemoveRefCount(); }
 161
 18162        public virtual void DetachFrom(UIImage image) { RemoveRefCount(); }
 163
 0164        public void AddRefCount() { refCount++; }
 165
 166        public void RemoveRefCount()
 167        {
 25168            if (refCount == 0)
 0169                Dispose();
 25170        }
 171
 172        public override void Dispose()
 173        {
 51174            isDisposed = true;
 51175            if (texturePromise != null)
 176            {
 32177                AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 32178                texturePromise = null;
 179            }
 180
 51181            base.Dispose();
 51182        }
 183    }
 184}