< 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:46
Uncovered lines:3
Coverable lines:49
Total lines:168
Line coverage:93.8% (46 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%3.033085.71%
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;
 9910        internal readonly KeyValueSet<int, IECSComponent> loadedComponents = new KeyValueSet<int, IECSComponent>();
 9911        internal readonly IList<IECSComponentsGroup> componentsGroups = new List<IECSComponentsGroup>();
 12
 9913        public ECSComponentsManager(IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders
 14        {
 9915            this.componentBuilders = componentBuilders;
 9916        }
 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        {
 125            loadedComponents.TryGetValue(componentId, out IECSComponent component);
 126            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        {
 71636            if (loadedComponents.TryGetValue(componentId, out IECSComponent component))
 28437                return component;
 38
 43239            if (!componentBuilders.TryGetValue(componentId, out ECSComponentsFactory.ECSComponentBuilder componentBuilde
 040                return null;
 41
 43242            component = componentBuilder.Invoke();
 43243            loadedComponents.Add(componentId, component);
 44
 43245            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        {
 28657            IECSComponent component = GetOrCreateComponent(componentId);
 28658            if (component == null || component.HasComponent(scene, entity))
 8159                return component;
 60
 20561            component.Create(scene, entity);
 62
 50663            for (int i = 0; i < componentsGroups.Count; i++)
 64            {
 4865                IECSComponentsGroup compGroup = componentsGroups[i];
 4866                if (compGroup.Match(component) && compGroup.Match(scene, entity))
 67                {
 1568                    compGroup.Add(scene, entity);
 69                }
 70            }
 71
 20572            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        {
 26884            var component = GetOrCreateComponent(componentId, scene, entity);
 26885            component?.Deserialize(scene, entity, message);
 26886        }
 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        {
 7197            if (!loadedComponents.TryGetValue(componentId, out IECSComponent component))
 098                return false;
 99
 71100            if (!component.Remove(scene, entity))
 0101                return false;
 102
 146103            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
 71112            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        {
 2122            int count = loadedComponents.Count;
 12123            for (int i = 0; i < count; i++)
 124            {
 4125                loadedComponents.Pairs[i].value.Remove(scene, entity);
 126            }
 127
 6128            for (int i = 0; i < componentsGroups.Count; i++)
 129            {
 1130                componentsGroups[i].Remove(entity);
 131            }
 2132        }
 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        {
 68142            int count = loadedComponents.Count;
 644143            for (int i = 0; i < count; i++)
 144            {
 317145                if (loadedComponents.Pairs[i].value.HasComponent(scene, entity))
 146                {
 63147                    return true;
 148                }
 149            }
 5150            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        {
 21163            var compGroup = new ECSComponentsGroup<T1, T2>(GetOrCreateComponent(componentId1), GetOrCreateComponent(comp
 21164            componentsGroups.Add(compGroup);
 21165            return compGroup;
 166        }
 167    }
 168}