< Summary

Class:DCL.DCLTexture
Assembly:DCL.Components.Texture
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Textures/DCLTexture.cs
Covered lines:63
Uncovered lines:22
Coverable lines:85
Total lines:218
Line coverage:74.1% (63 of 85)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
DCLTexture()0%110100%
GetClassId()0%2100%
FetchFromComponent()0%12300%
FetchTextureComponent()0%5.25080%
ApplyChanges()0%50.7120057.5%
AttachTo(...)0%110100%
DetachFrom(...)0%110100%
AddReference(...)0%330100%
RemoveReference(...)0%330100%
Dispose()0%440100%

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;
 9using System.Linq;
 10
 11namespace DCL
 12{
 13    public class DCLTexture : BaseDisposable
 14    {
 15        [System.Serializable]
 16        public class Model : BaseModel
 17        {
 18            public string src;
 19            public BabylonWrapMode wrap = BabylonWrapMode.CLAMP;
 19220            public FilterMode samplingMode = FilterMode.Bilinear;
 21            public bool hasAlpha = false;
 22
 5923            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 24        }
 25
 26        public enum BabylonWrapMode
 27        {
 28            CLAMP,
 29            WRAP,
 30            MIRROR
 31        }
 32
 33        AssetPromise_Texture texturePromise = null;
 34
 7435        private Dictionary<ISharedComponent, HashSet<long>> attachedEntitiesByComponent =
 36            new Dictionary<ISharedComponent, HashSet<long>>();
 37
 38        public TextureWrapMode unityWrap;
 39        public FilterMode unitySamplingMode;
 40        public Texture2D texture;
 41
 42        protected bool isDisposed;
 1743        public float resizingFactor => texturePromise?.asset.resizingFactor ?? 1;
 44
 045        public override int GetClassId() { return (int) CLASS_ID.TEXTURE; }
 46
 22247        public DCLTexture() { model = new Model(); }
 48
 49        public static IEnumerator FetchFromComponent(IParcelScene scene, string componentId,
 50            System.Action<Texture2D> OnFinish)
 51        {
 052            yield return FetchTextureComponent(scene, componentId, (dclTexture) => { OnFinish?.Invoke(dclTexture.texture
 053        }
 54
 55        public static IEnumerator FetchTextureComponent(IParcelScene scene, string componentId,
 56            System.Action<DCLTexture> OnFinish)
 57        {
 7858            if (!scene.componentsManagerLegacy.HasSceneSharedComponent(componentId))
 59            {
 160                Debug.Log($"couldn't fetch texture, the DCLTexture component with id {componentId} doesn't exist");
 161                yield break;
 62            }
 63
 7764            DCLTexture textureComponent = scene.componentsManagerLegacy.GetSceneSharedComponent(componentId) as DCLTextu
 65
 7766            if (textureComponent == null)
 67            {
 068                Debug.Log($"couldn't fetch texture, the shared component with id {componentId} is NOT a DCLTexture");
 069                yield break;
 70            }
 71
 38172            yield return new WaitUntil(() => textureComponent.texture != null);
 73
 7774            OnFinish.Invoke(textureComponent);
 7775        }
 76
 77        public override IEnumerator ApplyChanges(BaseModel newModel)
 78        {
 11879            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 80
 81            //If the scene creates and destroy the component before our renderer has been turned on bad things happen!
 82            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 5983            if (isDisposed)
 084                yield break;
 85
 5986            Model model = (Model) newModel;
 87
 5988            unitySamplingMode = model.samplingMode;
 89
 5990            switch (model.wrap)
 91            {
 92                case BabylonWrapMode.CLAMP:
 5893                    unityWrap = TextureWrapMode.Clamp;
 5894                    break;
 95                case BabylonWrapMode.WRAP:
 096                    unityWrap = TextureWrapMode.Repeat;
 097                    break;
 98                case BabylonWrapMode.MIRROR:
 199                    unityWrap = TextureWrapMode.Mirror;
 100                    break;
 101            }
 102
 59103            if (texture == null && !string.IsNullOrEmpty(model.src))
 104            {
 57105                bool isBase64 = model.src.Contains("image/png;base64");
 106
 57107                if (isBase64)
 108                {
 0109                    string base64Data = model.src.Substring(model.src.IndexOf(',') + 1);
 110
 111                    // The used texture variable can't be null for the ImageConversion.LoadImage to work
 0112                    if (texture == null)
 113                    {
 0114                        texture = new Texture2D(1, 1);
 115                    }
 116
 0117                    if (!ImageConversion.LoadImage(texture, Convert.FromBase64String(base64Data)))
 118                    {
 0119                        Debug.LogError($"DCLTexture with id {id} couldn't parse its base64 image data.");
 120                    }
 121
 0122                    if (texture != null)
 123                    {
 0124                        texture.wrapMode = unityWrap;
 0125                        texture.filterMode = unitySamplingMode;
 126
 0127                        if (DataStore.i.textureConfig.runCompression.Get())
 0128                            texture.Compress(false);
 129
 0130                        texture.Apply(unitySamplingMode != FilterMode.Point, true);
 0131                        texture = TextureHelpers.ClampSize(texture, DataStore.i.textureConfig.generalMaxSize.Get());
 132                    }
 133                }
 134                else
 135                {
 57136                    string contentsUrl = string.Empty;
 57137                    bool isExternalURL = model.src.Contains("http://") || model.src.Contains("https://");
 138
 57139                    if (isExternalURL)
 0140                        contentsUrl = model.src;
 141                    else
 57142                        scene.contentProvider.TryGetContentsUrl(model.src, out contentsUrl);
 143
 57144                    if (!string.IsNullOrEmpty(contentsUrl))
 145                    {
 57146                        if (texturePromise != null)
 0147                            AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 148
 57149                        texturePromise = new AssetPromise_Texture(contentsUrl, unityWrap, unitySamplingMode, storeDefaul
 113150                        texturePromise.OnSuccessEvent += (x) => texture = x.texture;
 57151                        texturePromise.OnFailEvent += (x, error) => { texture = null; };
 152
 57153                        AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 57154                        yield return texturePromise;
 155                    }
 156                }
 157            }
 59158        }
 159
 160        public virtual void AttachTo(ISharedComponent component)
 161        {
 67162            AddReference(component);
 67163        }
 164
 165        public virtual void DetachFrom(ISharedComponent component)
 166        {
 41167            RemoveReference(component);
 41168        }
 169
 170        public void AddReference(ISharedComponent component)
 171        {
 77172            if (attachedEntitiesByComponent.ContainsKey(component))
 1173                return;
 174
 76175            attachedEntitiesByComponent.Add(component, new HashSet<long>());
 176
 276177            foreach (var entity in component.GetAttachedEntities())
 178            {
 62179                attachedEntitiesByComponent[component].Add(entity.entityId);
 62180                DataStore.i.sceneWorldObjects.AddTexture(scene.sceneData.sceneNumber, entity.entityId, texture);
 181            }
 76182        }
 183
 184        public void RemoveReference(ISharedComponent component)
 185        {
 55186            if (!attachedEntitiesByComponent.ContainsKey(component))
 17187                return;
 188
 148189            foreach (var entityId in attachedEntitiesByComponent[component])
 190            {
 36191                DataStore.i.sceneWorldObjects.RemoveTexture(scene.sceneData.sceneNumber, entityId, texture);
 192            }
 193
 38194            attachedEntitiesByComponent.Remove(component);
 38195        }
 196
 197        public override void Dispose()
 198        {
 52199            if (isDisposed)
 24200                return;
 201
 28202            isDisposed = true;
 203
 41204            while (attachedEntitiesByComponent.Count > 0)
 205            {
 13206                RemoveReference(attachedEntitiesByComponent.First().Key);
 207            }
 208
 28209            if (texturePromise != null)
 210            {
 27211                AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 27212                texturePromise = null;
 213            }
 214
 28215            base.Dispose();
 28216        }
 217    }
 218}