< Summary

Class:DCL.ECSRuntime.ECSComponentsManager
Assembly:DCL.ECSRuntime.ComponentsManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ECSRuntime/ComponentsManager/ECSComponentsManager.cs
Covered lines:47
Uncovered lines:2
Coverable lines:49
Total lines:168
Line coverage:95.9% (47 of 49)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ECSComponentsManager(...)0%110100%
GetComponent(...)0%110100%
GetOrCreateComponent(...)0%330100%
GetOrCreateComponent(...)0%660100%
DeserializeComponent(...)0%220100%
RemoveComponent(...)0%5.155081.82%
RemoveAllComponents(...)0%330100%
HasAnyComponent(...)0%330100%
CreateComponentGroup[T1, T2](...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ECSRuntime/ComponentsManager/ECSComponentsManager.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Controllers;
 3using DCL.Models;
 4
 5namespace DCL.ECSRuntime
 6{
 7    public class ECSComponentsManager
 8    {
 9        private readonly IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders;
 16310        internal readonly KeyValueSet<int, IECSComponent> loadedComponents = new KeyValueSet<int, IECSComponent>();
 16311        internal readonly IList<IECSComponentsGroup> componentsGroups = new List<IECSComponentsGroup>();
 12
 16313        public ECSComponentsManager(IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders
 14        {
 16315            this.componentBuilders = componentBuilders;
 16316        }
 17
 18        /// <summary>
 19        /// get a component instance using it id
 20        /// </summary>
 21        /// <param name="componentId"></param>
 22        /// <returns>component instance of null if it does not exist</returns>
 23        public IECSComponent GetComponent(int componentId)
 24        {
 325            loadedComponents.TryGetValue(componentId, out IECSComponent component);
 326            return component;
 27        }
 28
 29        /// <summary>
 30        /// get or create a component
 31        /// </summary>
 32        /// <param name="componentId"></param>
 33        /// <returns>the instance of existing or newly created component</returns>
 34        public IECSComponent GetOrCreateComponent(int componentId)
 35        {
 129936            if (loadedComponents.TryGetValue(componentId, out IECSComponent component))
 38937                return component;
 38
 91039            if (!componentBuilders.TryGetValue(componentId, out ECSComponentsFactory.ECSComponentBuilder componentBuilde
 440                return null;
 41
 90642            component = componentBuilder.Invoke();
 90643            loadedComponents.Add(componentId, component);
 44
 90645            return component;
 46        }
 47
 48        /// <summary>
 49        /// get or create a component for an entity
 50        /// </summary>
 51        /// <param name="componentId"></param>
 52        /// <param name="scene"></param>
 53        /// <param name="entity"></param>
 54        /// <returns>the instance of existing or newly created component</returns>
 55        public IECSComponent GetOrCreateComponent(int componentId, IParcelScene scene, IDCLEntity entity)
 56        {
 39157            IECSComponent component = GetOrCreateComponent(componentId);
 39158            if (component == null || component.HasComponent(scene, entity))
 14159                return component;
 60
 25061            component.Create(scene, entity);
 62
 60663            for (int i = 0; i < componentsGroups.Count; i++)
 64            {
 5365                IECSComponentsGroup compGroup = componentsGroups[i];
 5366                if (compGroup.Match(component) && compGroup.Match(scene, entity))
 67                {
 1668                    compGroup.Add(scene, entity);
 69                }
 70            }
 71
 25072            return component;
 73        }
 74
 75        /// <summary>
 76        /// deserialize data for a component. it will create the component if it does not exists
 77        /// </summary>
 78        /// <param name="componentId"></param>
 79        /// <param name="scene"></param>
 80        /// <param name="entity"></param>
 81        /// <param name="message"></param>
 82        public void DeserializeComponent(int componentId, IParcelScene scene, IDCLEntity entity, object message)
 83        {
 37384            var component = GetOrCreateComponent(componentId, scene, entity);
 37385            component?.Deserialize(scene, entity, message);
 36986        }
 87
 88        /// <summary>
 89        /// remove a component from an entity
 90        /// </summary>
 91        /// <param name="componentId"></param>
 92        /// <param name="scene"></param>
 93        /// <param name="entity"></param>
 94        /// <returns>true if component removed successfully, false if entity didn't contain component</returns>
 95        public bool RemoveComponent(int componentId, IParcelScene scene, IDCLEntity entity)
 96        {
 7597            if (!loadedComponents.TryGetValue(componentId, out IECSComponent component))
 098                return false;
 99
 75100            if (!component.Remove(scene, entity))
 0101                return false;
 102
 154103            for (int i = 0; i < componentsGroups.Count; i++)
 104            {
 2105                IECSComponentsGroup compGroup = componentsGroups[i];
 2106                if (compGroup.Match(component))
 107                {
 2108                    compGroup.Remove(entity);
 109                }
 110            }
 111
 75112            return true;
 113        }
 114
 115        /// <summary>
 116        /// remove all components of a given entity
 117        /// </summary>
 118        /// <param name="scene"></param>
 119        /// <param name="entity"></param>
 120        public void RemoveAllComponents(IParcelScene scene, IDCLEntity entity)
 121        {
 9122            int count = loadedComponents.Count;
 206123            for (int i = 0; i < count; i++)
 124            {
 94125                loadedComponents.Pairs[i].value.Remove(scene, entity);
 126            }
 127
 44128            for (int i = 0; i < componentsGroups.Count; i++)
 129            {
 13130                componentsGroups[i].Remove(entity);
 131            }
 9132        }
 133
 134        /// <summary>
 135        /// get if entity has any component
 136        /// </summary>
 137        /// <param name="scene"></param>
 138        /// <param name="entity"></param>
 139        /// <returns></returns>
 140        public bool HasAnyComponent(IParcelScene scene, IDCLEntity entity)
 141        {
 72142            int count = loadedComponents.Count;
 956143            for (int i = 0; i < count; i++)
 144            {
 469145                if (loadedComponents.Pairs[i].value.HasComponent(scene, entity))
 146                {
 63147                    return true;
 148                }
 149            }
 9150            return false;
 151        }
 152
 153        /// <summary>
 154        /// creates a components group
 155        /// </summary>
 156        /// <param name="componentId1">id of one of the components</param>
 157        /// <param name="componentId2">id of the other component</param>
 158        /// <typeparam name="T1">model type of `componentId1`</typeparam>
 159        /// <typeparam name="T2">model type of `componentId2`</typeparam>
 160        /// <returns></returns>
 161        public IECSReadOnlyComponentsGroup<T1, T2> CreateComponentGroup<T1, T2>(int componentId1, int componentId2)
 162        {
 11163            var compGroup = new ECSComponentsGroup<T1, T2>(GetOrCreateComponent(componentId1), GetOrCreateComponent(comp
 11164            componentsGroups.Add(compGroup);
 11165            return compGroup;
 166        }
 167    }
 168}