< Summary

Class:DCL.Models.DecentralandEntity
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/DecentralandEntity.cs
Covered lines:98
Uncovered lines:5
Coverable lines:103
Total lines:234
Line coverage:95.1% (98 of 103)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DecentralandEntity()0%110100%
GetSharedComponents()0%2100%
AddChild(...)0%220100%
RemoveChild(...)0%220100%
SetParent(...)0%660100%
EnsureMeshGameObject(...)0%440100%
ResetRelease()0%2100%
Cleanup()0%12120100%
AddSharedComponent(...)0%2.032080%
RemoveSharedComponent(...)0%4.054085.71%
TryGetComponent[T]()0%550100%
TryGetBaseComponent(...)0%110100%
TryGetSharedComponent(...)0%330100%
GetSharedComponent(...)0%2.152066.67%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/DecentralandEntity.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8
 9namespace DCL.Models
 10{
 11    [Serializable]
 12    public class DecentralandEntity : IDCLEntity
 13    {
 89714        public IParcelScene scene { get; set; }
 130515        public bool markedForCleanup { get; set; } = false;
 8316        public bool isInsideBoundaries { get; set; } = false;
 17
 123818        public Dictionary<string, IDCLEntity> children { get; private set; } = new Dictionary<string, IDCLEntity>();
 47419        public IDCLEntity parent { get; private set; }
 20
 149421        public Dictionary<CLASS_ID_COMPONENT, IEntityComponent> components { get; private set; } = new Dictionary<CLASS_
 73722        public Dictionary<System.Type, ISharedComponent> sharedComponents { get; private set; } = new Dictionary<System.
 23
 455624        public GameObject gameObject { get; set; }
 141525        public string entityId { get; set; }
 489926        public MeshesInfo meshesInfo { get; set; }
 314127        public GameObject meshRootGameObject => meshesInfo.meshRootGameObject;
 128        public Renderer[] renderers => meshesInfo.renderers;
 29
 114330        public System.Action<IDCLEntity> OnShapeUpdated { get; set; }
 13231        public System.Action<object> OnNameChange { get; set; }
 19432        public System.Action<object> OnTransformChange { get; set; }
 122633        public System.Action<IDCLEntity> OnRemoved { get; set; }
 100234        public System.Action<IDCLEntity> OnMeshesInfoUpdated { get; set; }
 100235        public System.Action<IDCLEntity> OnMeshesInfoCleaned { get; set; }
 36
 242837        public System.Action<ICleanableEventDispatcher> OnCleanupEvent { get; set; }
 38
 39        const string MESH_GAMEOBJECT_NAME = "Mesh";
 40
 41        bool isReleased = false;
 42
 68143        public DecentralandEntity()
 44        {
 68145            meshesInfo = new MeshesInfo();
 114346            OnShapeUpdated += (entity) => meshesInfo.UpdateRenderersCollection();
 115847            meshesInfo.OnUpdated += () => OnMeshesInfoUpdated?.Invoke(this);
 88748            meshesInfo.OnCleanup += () => OnMeshesInfoCleaned?.Invoke(this);
 68149        }
 50
 051        public Dictionary<System.Type, ISharedComponent> GetSharedComponents() { return sharedComponents; }
 52
 53        public void AddChild(IDCLEntity entity)
 54        {
 1355            if (!children.ContainsKey(entity.entityId))
 56            {
 1357                children.Add(entity.entityId, entity);
 58            }
 1359        }
 60
 61        public void RemoveChild(IDCLEntity entity)
 62        {
 1263            if (children.ContainsKey(entity.entityId))
 64            {
 1265                children.Remove(entity.entityId);
 66            }
 1267        }
 68
 69        public void SetParent(IDCLEntity entity)
 70        {
 44471            if (parent != null)
 72            {
 1273                parent.RemoveChild(this);
 74            }
 75
 44476            if (entity != null)
 77            {
 1378                entity.AddChild(this);
 79
 1380                if (entity.gameObject && gameObject)
 1381                    gameObject.transform.SetParent(entity.gameObject.transform, false);
 1382            }
 43183            else if (gameObject)
 84            {
 42785                gameObject.transform.SetParent(null, false);
 86            }
 87
 44488            parent = entity;
 44489        }
 90
 91        public void EnsureMeshGameObject(string gameObjectName = null)
 92        {
 20593            if (meshesInfo.meshRootGameObject == null)
 94            {
 20395                meshesInfo.meshRootGameObject = new GameObject();
 20396                meshesInfo.meshRootGameObject.name = gameObjectName == null ? MESH_GAMEOBJECT_NAME : gameObjectName;
 20397                meshesInfo.meshRootGameObject.transform.SetParent(gameObject.transform);
 20398                Utils.ResetLocalTRS(meshesInfo.meshRootGameObject.transform);
 99            }
 205100        }
 101
 0102        public void ResetRelease() { isReleased = false; }
 103
 104        public void Cleanup()
 105        {
 106            // Don't do anything if this object was already released
 459107            if (isReleased)
 1108                return;
 109
 458110            OnRemoved?.Invoke(this);
 111
 112            // This will release the poolable objects of the mesh and the entity
 458113            OnCleanupEvent?.Invoke(this);
 114
 1334115            foreach (var kvp in components)
 116            {
 209117                if (kvp.Value == null)
 118                    continue;
 119
 209120                if (kvp.Value is ICleanable cleanableComponent)
 209121                    cleanableComponent.Cleanup();
 122
 209123                if (!(kvp.Value is IPoolableObjectContainer poolableContainer))
 124                    continue;
 125
 65126                if (poolableContainer.poolableObject == null)
 127                    continue;
 128
 1129                poolableContainer.poolableObject.Release();
 130            }
 131
 458132            components.Clear();
 133
 458134            if (meshesInfo.meshRootGameObject)
 135            {
 8136                Utils.SafeDestroy(meshesInfo.meshRootGameObject);
 8137                meshesInfo.CleanReferences();
 138            }
 139
 458140            if (gameObject)
 141            {
 454142                int childCount = gameObject.transform.childCount;
 143
 144                // Destroy any other children
 1444145                for (int i = 0; i < childCount; i++)
 146                {
 268147                    Utils.SafeDestroy(gameObject.transform.GetChild(i).gameObject);
 148                }
 149
 150                //NOTE(Brian): This will prevent any component from storing/querying invalid gameObject references.
 454151                gameObject = null;
 152            }
 153
 458154            OnTransformChange = null;
 458155            isReleased = true;
 458156        }
 157
 158        public void AddSharedComponent(System.Type componentType, ISharedComponent component)
 159        {
 280160            if (component == null)
 161            {
 0162                return;
 163            }
 164
 280165            RemoveSharedComponent(componentType);
 166
 280167            sharedComponents.Add(componentType, component);
 280168        }
 169
 170        public void RemoveSharedComponent(Type targetType, bool triggerDetaching = true)
 171        {
 592172            if (sharedComponents.TryGetValue(targetType, out ISharedComponent component))
 173            {
 275174                if (component == null)
 0175                    return;
 176
 275177                sharedComponents.Remove(targetType);
 178
 275179                if (triggerDetaching)
 15180                    component.DetachFrom(this, targetType);
 181            }
 592182        }
 183
 184        /// <summary>
 185        /// This function is designed to get interfaces implemented by diverse components.
 186        ///
 187        /// If you want to get the component itself please use TryGetBaseComponent or TryGetSharedComponent.
 188        /// </summary>
 189        /// <typeparam name="T"></typeparam>
 190        /// <returns></returns>
 191        public T TryGetComponent<T>() where T : class
 192        {
 193            //Note (Adrian): If you are going to call this function frequently, please refactor it to avoid using LinQ f
 103194            T component = components.Values.FirstOrDefault(x => x is T) as T;
 195
 103196            if (component != null)
 1197                return component;
 198
 102199            component = sharedComponents.Values.FirstOrDefault(x => x is T) as T;
 200
 102201            if (component != null)
 25202                return component;
 203
 77204            return null;
 205        }
 206
 4207        public bool TryGetBaseComponent(CLASS_ID_COMPONENT componentId, out IEntityComponent component) { return compone
 208
 209        public bool TryGetSharedComponent(CLASS_ID componentId, out ISharedComponent component)
 210        {
 310211            foreach (KeyValuePair<Type, ISharedComponent> keyValuePairBaseDisposable in sharedComponents)
 212            {
 75213                if (keyValuePairBaseDisposable.Value.GetClassId() == (int) componentId)
 214                {
 38215                    component = keyValuePairBaseDisposable.Value;
 38216                    return true;
 217                }
 218            }
 219
 61220            component = null;
 61221            return false;
 38222        }
 223
 224        public ISharedComponent GetSharedComponent(System.Type targetType)
 225        {
 42226            if (sharedComponents.TryGetValue(targetType, out ISharedComponent component))
 227            {
 42228                return component;
 229            }
 230
 0231            return null;
 232        }
 233    }
 234}

Methods/Properties

scene()
scene(DCL.Controllers.IParcelScene)
markedForCleanup()
markedForCleanup(System.Boolean)
isInsideBoundaries()
isInsideBoundaries(System.Boolean)
children()
children(System.Collections.Generic.Dictionary[String,IDCLEntity])
DecentralandEntity()
parent()
parent(DCL.Models.IDCLEntity)
components()
components(System.Collections.Generic.Dictionary[CLASS_ID_COMPONENT,IEntityComponent])
sharedComponents()
sharedComponents(System.Collections.Generic.Dictionary[Type,ISharedComponent])
gameObject()
gameObject(UnityEngine.GameObject)
entityId()
entityId(System.String)
meshesInfo()
meshesInfo(DCL.Models.MeshesInfo)
meshRootGameObject()
renderers()
OnShapeUpdated()
OnShapeUpdated(System.Action[IDCLEntity])
OnNameChange()
OnNameChange(System.Action[Object])
OnTransformChange()
OnTransformChange(System.Action[Object])
OnRemoved()
OnRemoved(System.Action[IDCLEntity])
OnMeshesInfoUpdated()
OnMeshesInfoUpdated(System.Action[IDCLEntity])
OnMeshesInfoCleaned()
OnMeshesInfoCleaned(System.Action[IDCLEntity])
OnCleanupEvent()
OnCleanupEvent(System.Action[ICleanableEventDispatcher])
GetSharedComponents()
AddChild(DCL.Models.IDCLEntity)
RemoveChild(DCL.Models.IDCLEntity)
SetParent(DCL.Models.IDCLEntity)
EnsureMeshGameObject(System.String)
ResetRelease()
Cleanup()
AddSharedComponent(System.Type, DCL.Components.ISharedComponent)
RemoveSharedComponent(System.Type, System.Boolean)
TryGetComponent[T]()
TryGetBaseComponent(DCL.Models.CLASS_ID_COMPONENT, DCL.Components.IEntityComponent&)
TryGetSharedComponent(DCL.Models.CLASS_ID, DCL.Components.ISharedComponent&)
GetSharedComponent(System.Type)