< 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:103
Uncovered lines:1
Coverable lines:104
Total lines:338
Line coverage:99% (103 of 104)
Covered branches:0
Total branches:0
Covered methods:17
Total methods:17
Method coverage:100% (17 of 17)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ECSComponentsManager(...)0%110100%
GetComponent(...)0%110100%
GetOrCreateComponent(...)0%330100%
GetOrCreateComponent(...)0%330100%
DeserializeComponent(...)0%220100%
SignalComponentCreated(...)0%110100%
SignalComponentUpdated(...)0%110100%
RemoveComponent(...)0%3.043083.33%
RemoveAllComponents(...)0%440100%
HasAnyComponent(...)0%330100%
CreateComponentGroup[T1, T2](...)0%110100%
CreateComponentGroup[T1, T2, T3](...)0%110100%
CreateComponentGroupWithoutComponent[T1](...)0%110100%
CreateComponentGroupWithoutComponent[T1, T2](...)0%110100%
SetGroupsOnComponentCreated(...)0%990100%
SetGroupsOnComponentRemoved(...)0%990100%
UpdateComponentGroups(...)0%330100%

File(s)

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

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Models;
 3using System;
 4using System.Collections.Generic;
 5using UnityEngine;
 6
 7namespace DCL.ECSRuntime
 8{
 9    public class ECSComponentsManager
 10    {
 33111        internal readonly KeyValueSet<int, IECSComponent> loadedComponents = new KeyValueSet<int, IECSComponent>();
 33112        internal readonly IList<IECSComponentsGroup> componentsGroups = new List<IECSComponentsGroup>();
 33113        internal readonly Dictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGroups =
 14            new Dictionary<IDCLEntity, List<IECSComponentsGroup>>();
 15        private readonly IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders;
 16
 33117        public ECSComponentsManager(IReadOnlyDictionary<int, ECSComponentsFactory.ECSComponentBuilder> componentBuilders
 18        {
 33119            this.componentBuilders = componentBuilders;
 33120        }
 21
 22        /// <summary>
 23        /// get a component instance using it id
 24        /// </summary>
 25        /// <param name="componentId"></param>
 26        /// <returns>component instance of null if it does not exist</returns>
 27        public IECSComponent GetComponent(int componentId)
 28        {
 129            loadedComponents.TryGetValue(componentId, out IECSComponent component);
 130            return component;
 31        }
 32
 33        /// <summary>
 34        /// get or create a component
 35        /// </summary>
 36        /// <param name="componentId"></param>
 37        /// <returns>the instance of existing or newly created component</returns>
 38        public IECSComponent GetOrCreateComponent(int componentId)
 39        {
 503440            if (loadedComponents.TryGetValue(componentId, out IECSComponent component))
 25141                return component;
 42
 478343            if (!componentBuilders.TryGetValue(componentId, out ECSComponentsFactory.ECSComponentBuilder componentBuilde
 1344                return null;
 45
 477046            component = componentBuilder.Invoke();
 477047            loadedComponents.Add(componentId, component);
 48
 477049            return component;
 50        }
 51
 52        /// <summary>
 53        /// get or create a component for an entity
 54        /// </summary>
 55        /// <param name="componentId"></param>
 56        /// <param name="scene"></param>
 57        /// <param name="entity"></param>
 58        /// <returns>the instance of existing or newly created component</returns>
 59        public IECSComponent GetOrCreateComponent(int componentId, IParcelScene scene, IDCLEntity entity)
 60        {
 8561            IECSComponent component = GetOrCreateComponent(componentId);
 62
 8563            if (component == null || component.HasComponent(scene, entity))
 2064                return component;
 65
 6566            component.Create(scene, entity);
 6567            SetGroupsOnComponentCreated(component, scene, entity, componentsGroups, entitiesGroups);
 6568            return component;
 69        }
 70
 71        /// <summary>
 72        /// deserialize data for a component. it will create the component if it does not exists
 73        /// </summary>
 74        /// <param name="componentId"></param>
 75        /// <param name="scene"></param>
 76        /// <param name="entity"></param>
 77        /// <param name="message"></param>
 78        public void DeserializeComponent(int componentId, IParcelScene scene, IDCLEntity entity, object message)
 79        {
 2480            var component = GetOrCreateComponent(componentId, scene, entity);
 81
 2482            if (component != null)
 83            {
 1984                component.Deserialize(scene, entity, message);
 1985                UpdateComponentGroups(component, scene, entity, entitiesGroups);
 86            }
 2487        }
 88
 89        public void SignalComponentCreated(IParcelScene scene, IDCLEntity entity, IECSComponent component)
 90        {
 70091            SetGroupsOnComponentCreated(component, scene, entity, componentsGroups, entitiesGroups);
 70092        }
 93
 94        public void SignalComponentUpdated(IParcelScene scene, IDCLEntity entity, IECSComponent component)
 95        {
 119396            UpdateComponentGroups(component, scene, entity, entitiesGroups);
 119397        }
 98
 99        /// <summary>
 100        /// remove a component from an entity
 101        /// </summary>
 102        /// <param name="componentId"></param>
 103        /// <param name="scene"></param>
 104        /// <param name="entity"></param>
 105        /// <returns>true if component removed successfully, false if entity didn't contain component</returns>
 106        public bool RemoveComponent(int componentId, IParcelScene scene, IDCLEntity entity)
 107        {
 191108            if (!loadedComponents.TryGetValue(componentId, out IECSComponent component))
 0109                return false;
 110
 191111            if (!component.Remove(scene, entity))
 12112                return false;
 113
 179114            SetGroupsOnComponentRemoved(component, scene, entity, componentsGroups, entitiesGroups);
 115
 179116            return true;
 117        }
 118
 119        /// <summary>
 120        /// remove all components of a given entity
 121        /// </summary>
 122        /// <param name="scene"></param>
 123        /// <param name="entity"></param>
 124        public void RemoveAllComponents(IParcelScene scene, IDCLEntity entity)
 125        {
 516126            int count = loadedComponents.Count;
 127
 20176128            for (int i = 0; i < count; i++)
 129            {
 9572130                loadedComponents.Pairs[i].value.Remove(scene, entity);
 131            }
 132
 516133            if (entitiesGroups.TryGetValue(entity, out List<IECSComponentsGroup> entityInGroups))
 134            {
 60135                for (int i = 0; i < entityInGroups.Count; i++)
 136                {
 15137                    entityInGroups[i].Remove(entity);
 138                }
 139
 15140                entitiesGroups.Remove(entity);
 141            }
 516142        }
 143
 144        /// <summary>
 145        /// get if entity has any component
 146        /// </summary>
 147        /// <param name="scene"></param>
 148        /// <param name="entity"></param>
 149        /// <returns></returns>
 150        public bool HasAnyComponent(IParcelScene scene, IDCLEntity entity)
 151        {
 180152            int count = loadedComponents.Count;
 153
 5882154            for (int i = 0; i < count; i++)
 155            {
 2903156                if (loadedComponents.Pairs[i].value.HasComponent(scene, entity))
 157                {
 142158                    return true;
 159                }
 160            }
 161
 38162            return false;
 163        }
 164
 165        /// <summary>
 166        /// creates a components group
 167        /// </summary>
 168        /// <param name="componentId1">id of one of the components</param>
 169        /// <param name="componentId2">id of the other component</param>
 170        /// <typeparam name="T1">model type of `componentId1`</typeparam>
 171        /// <typeparam name="T2">model type of `componentId2`</typeparam>
 172        /// <returns></returns>
 173        public IECSReadOnlyComponentsGroup<T1, T2> CreateComponentGroup<T1, T2>(int componentId1, int componentId2)
 174        {
 41175            var compGroup = new ECSComponentsGroup<T1, T2>(GetOrCreateComponent(componentId1), GetOrCreateComponent(comp
 41176            componentsGroups.Add(compGroup);
 41177            return compGroup;
 178        }
 179
 180        /// <summary>
 181        /// creates a components group
 182        /// </summary>
 183        /// <param name="componentId1">id of one of the components</param>
 184        /// <param name="componentId2">id of the other component</param>
 185        /// <param name="componentId3">id of the other component</param>
 186        /// <typeparam name="T1">model type of `componentId1`</typeparam>
 187        /// <typeparam name="T2">model type of `componentId2`</typeparam>
 188        /// <typeparam name="T3">model type of `componentId3`</typeparam>
 189        /// <returns></returns>
 190        public IECSReadOnlyComponentsGroup<T1, T2, T3> CreateComponentGroup<T1, T2, T3>(int componentId1, int componentI
 191        {
 13192            var compGroup = new ECSComponentsGroup<T1, T2, T3>(
 193                GetOrCreateComponent(componentId1),
 194                GetOrCreateComponent(componentId2),
 195                GetOrCreateComponent(componentId3));
 196
 13197            componentsGroups.Add(compGroup);
 13198            return compGroup;
 199        }
 200
 201        /// <summary>
 202        /// creates a components group of components without `excludeComponentId`
 203        /// </summary>
 204        /// <param name="componentId">id of component that must be present</param>
 205        /// <param name="excludeComponentId">id of component that must not be present</param>
 206        /// <typeparam name="T1">type of component that must be present</typeparam>
 207        /// <returns></returns>
 208        public IECSReadOnlyComponentsGroup<T1> CreateComponentGroupWithoutComponent<T1>(int componentId, int excludeComp
 209        {
 16210            var compGroup = new ECSComponentsGroupWithout<T1>(GetOrCreateComponent(componentId),
 211                GetOrCreateComponent(excludeComponentId));
 212
 16213            componentsGroups.Add(compGroup);
 16214            return compGroup;
 215        }
 216
 217        /// <summary>
 218        /// creates a components group of components without `excludeComponentId`
 219        /// </summary>
 220        /// <param name="componentId1">id of component that must be present</param>
 221        /// <param name="componentId2">id of component that must be present</param>
 222        /// <param name="excludeComponentId">id of component that must not be present</param>
 223        /// <typeparam name="T1">model type of `componentId1`</typeparam>
 224        /// <typeparam name="T2">model type of `componentId2`</typeparam>
 225        /// <returns></returns>
 226        public IECSReadOnlyComponentsGroup<T1, T2> CreateComponentGroupWithoutComponent<T1, T2>(int componentId1, int co
 227        {
 26228            var compGroup = new ECSComponentsGroupWithout<T1, T2>(
 229                GetOrCreateComponent(componentId1),
 230                GetOrCreateComponent(componentId2),
 231                GetOrCreateComponent(excludeComponentId));
 232
 26233            componentsGroups.Add(compGroup);
 26234            return compGroup;
 235        }
 236
 237        private static void SetGroupsOnComponentCreated(IECSComponent component, IParcelScene scene, IDCLEntity entity,
 238            IList<IECSComponentsGroup> componentsGroups, IDictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGrou
 239        {
 765240            List<IECSComponentsGroup> entityInGroups = null;
 241
 765242            if (entitiesGroups.TryGetValue(entity, out entityInGroups))
 243            {
 48244                for (int i = entityInGroups.Count - 1; i >= 0; i--)
 245                {
 12246                    IECSComponentsGroup compGroup = entityInGroups[i];
 247
 12248                    if (compGroup.ShouldRemoveOnComponentAdd(component))
 249                    {
 10250                        compGroup.Remove(entity);
 10251                        entityInGroups.RemoveAt(i);
 252                    }
 253                }
 254
 12255                if (entityInGroups.Count == 0)
 256                {
 10257                    entitiesGroups.Remove(entity);
 10258                    entityInGroups = null;
 259                }
 260            }
 261
 1994262            for (int i = 0; i < componentsGroups.Count; i++)
 263            {
 232264                IECSComponentsGroup compGroup = componentsGroups[i];
 265
 232266                if (compGroup.ShouldAddOnComponentAdd(component) && compGroup.MatchEntity(scene, entity))
 267                {
 43268                    compGroup.Add(scene, entity);
 269
 43270                    if (entityInGroups == null)
 271                    {
 43272                        entityInGroups = new List<IECSComponentsGroup>();
 43273                        entitiesGroups[entity] = entityInGroups;
 274                    }
 275
 43276                    entityInGroups.Add(compGroup);
 277                }
 278            }
 765279        }
 280
 281        private static void SetGroupsOnComponentRemoved(IECSComponent component, IParcelScene scene, IDCLEntity entity,
 282            IList<IECSComponentsGroup> componentsGroups, Dictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGroup
 283        {
 179284            List<IECSComponentsGroup> entityInGroups = null;
 285
 179286            if (entitiesGroups.TryGetValue(entity, out entityInGroups))
 287            {
 40288                for (int i = entityInGroups.Count - 1; i >= 0; i--)
 289                {
 10290                    IECSComponentsGroup compGroup = entityInGroups[i];
 291
 10292                    if (compGroup.ShouldRemoveOnComponentRemove(component))
 293                    {
 10294                        compGroup.Remove(entity);
 10295                        entityInGroups.RemoveAt(i);
 296                    }
 297                }
 298
 10299                if (entityInGroups.Count == 0)
 300                {
 10301                    entitiesGroups.Remove(entity);
 10302                    entityInGroups = null;
 303                }
 304            }
 305
 410306            for (int i = 0; i < componentsGroups.Count; i++)
 307            {
 26308                IECSComponentsGroup compGroup = componentsGroups[i];
 309
 26310                if (compGroup.ShouldAddOnComponentRemove(component) && compGroup.MatchEntity(scene, entity))
 311                {
 3312                    compGroup.Add(scene, entity);
 313
 3314                    if (entityInGroups == null)
 315                    {
 3316                        entityInGroups = new List<IECSComponentsGroup>();
 3317                        entitiesGroups[entity] = entityInGroups;
 318                    }
 319
 3320                    entityInGroups.Add(compGroup);
 321                }
 322            }
 179323        }
 324
 325        private static void UpdateComponentGroups(IECSComponent component, IParcelScene scene, IDCLEntity entity,
 326            IDictionary<IDCLEntity, List<IECSComponentsGroup>> entitiesGroups)
 327        {
 1212328            if (entitiesGroups.TryGetValue(entity, out var entityInGroups))
 329            {
 348330                for (int i = 0; i < entityInGroups.Count; i++)
 331                {
 87332                    IECSComponentsGroup compGroup = entityInGroups[i];
 87333                    compGroup.Update(scene, entity, component);
 334                }
 335            }
 1212336        }
 337    }
 338}

Methods/Properties

ECSComponentsManager(System.Collections.Generic.IReadOnlyDictionary[Int32,ECSComponentBuilder])
GetComponent(System.Int32)
GetOrCreateComponent(System.Int32)
GetOrCreateComponent(System.Int32, DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity)
DeserializeComponent(System.Int32, DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity, System.Object)
SignalComponentCreated(DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity, DCL.ECSRuntime.IECSComponent)
SignalComponentUpdated(DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity, DCL.ECSRuntime.IECSComponent)
RemoveComponent(System.Int32, DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity)
RemoveAllComponents(DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity)
HasAnyComponent(DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity)
CreateComponentGroup[T1, T2](System.Int32, System.Int32)
CreateComponentGroup[T1, T2, T3](System.Int32, System.Int32, System.Int32)
CreateComponentGroupWithoutComponent[T1](System.Int32, System.Int32)
CreateComponentGroupWithoutComponent[T1, T2](System.Int32, System.Int32, System.Int32)
SetGroupsOnComponentCreated(DCL.ECSRuntime.IECSComponent, DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity, System.Collections.Generic.IList[IECSComponentsGroup], System.Collections.Generic.IDictionary[IDCLEntity,List`1])
SetGroupsOnComponentRemoved(DCL.ECSRuntime.IECSComponent, DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity, System.Collections.Generic.IList[IECSComponentsGroup], System.Collections.Generic.Dictionary[IDCLEntity,List`1])
UpdateComponentGroups(DCL.ECSRuntime.IECSComponent, DCL.Controllers.IParcelScene, DCL.Models.IDCLEntity, System.Collections.Generic.IDictionary[IDCLEntity,List`1])