< Summary

Class:DCL.ECSRuntime.ECSComponent[ModelType]
Assembly:DCL.ECSRuntime.Components
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ECSRuntime/Components/ECSComponent.cs
Covered lines:39
Uncovered lines:11
Coverable lines:50
Total lines:176
Line coverage:78% (39 of 50)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ECSComponent(...)0%110100%
Create(...)0%5.395075%
Remove(...)0%3.143075%
SetModel(...)0%3.333066.67%
Deserialize(...)0%110100%
HasComponent(...)0%110100%
Get(...)0%2.152066.67%
ComponentDataContainsEntity(...)0%220100%
ComponentDataAdd(...)0%220100%
ComponentDataRemove(...)0%4.594066.67%
ComponentDataTryGet(...)0%2.52050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ECSRuntime/Components/ECSComponent.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL.Controllers;
 4using DCL.Models;
 5using UnityEngine;
 6
 7namespace DCL.ECSRuntime
 8{
 9    public class ECSComponent<ModelType> : IECSComponent
 10    {
 4211        internal readonly Dictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>> componentData =
 12            new Dictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>>();
 13
 14        private readonly Func<IECSComponentHandler<ModelType>> handlerBuilder;
 15        private readonly Func<object, ModelType> deserializer;
 16
 4217        public ECSComponent(Func<object, ModelType> deserializer, Func<IECSComponentHandler<ModelType>> handlerBuilder)
 18        {
 4219            this.deserializer = deserializer;
 4220            this.handlerBuilder = handlerBuilder;
 4221        }
 22
 23        /// <summary>
 24        /// creates and add component to an entity
 25        /// </summary>
 26        /// <param name="scene">target scene</param>
 27        /// <param name="entity">target entity</param>
 28        public void Create(IParcelScene scene, IDCLEntity entity)
 29        {
 7330            var entityId = entity.entityId;
 31
 7332            if (ComponentDataContainsEntity(componentData, scene, entity))
 33            {
 034                Debug.LogError($"entity {entityId} already contains component {typeof(ModelType)}", entity.gameObject);
 035                return;
 36            }
 37
 7338            var handler = handlerBuilder?.Invoke();
 39
 7340            ComponentDataAdd(componentData, scene, entity,
 41                new ECSComponentData<ModelType>()
 42                {
 43                    entity = entity,
 44                    model = default,
 45                    scene = scene,
 46                    handler = handler
 47                }
 48            );
 49
 7350            handler?.OnComponentCreated(scene, entity);
 7351        }
 52
 53        /// <summary>
 54        /// remove component from entity
 55        /// </summary>
 56        /// <param name="scene">target scene</param>
 57        /// <param name="entity">target entity</param>
 58        /// <returns>true if component removed successfully, false if entity didn't contain component</returns>
 59        public bool Remove(IParcelScene scene, IDCLEntity entity)
 60        {
 3961            if (!ComponentDataRemove(componentData, scene, entity, out ECSComponentData<ModelType> data))
 062                return false;
 63
 3964            data.handler?.OnComponentRemoved(scene, entity);
 3965            return true;
 66        }
 67
 68        /// <summary>
 69        /// set component model for entity
 70        /// </summary>
 71        /// <param name="scene">target scene</param>
 72        /// <param name="entity">target entity</param>
 73        /// <param name="model">new model</param>
 74        public void SetModel(IParcelScene scene, IDCLEntity entity, ModelType model)
 75        {
 7076            if (ComponentDataTryGet(componentData, scene, entity, out ECSComponentData<ModelType> data))
 77            {
 7078                data.model = model;
 7079                data.handler?.OnComponentModelUpdated(scene, entity, model);
 7080            }
 81            else
 82            {
 083                Debug.LogError($"trying to update model but entity {entity.entityId} does not contains component {typeof
 84                    entity.gameObject);
 85            }
 086        }
 87
 88        /// <summary>
 89        /// deserialize message and apply a new model for an entity
 90        /// </summary>
 91        /// <param name="scene">target scene</param>
 92        /// <param name="entity">target entity</param>
 93        /// <param name="message">message</param>
 94        public void Deserialize(IParcelScene scene, IDCLEntity entity, object message)
 95        {
 3996            SetModel(scene, entity, deserializer.Invoke(message));
 3997        }
 98
 99        /// <summary>
 100        /// check if entity contains component
 101        /// </summary>
 102        /// <param name="scene">target scene</param>
 103        /// <param name="entity">target entity</param>
 104        /// <returns>true if entity contains this component</returns>
 105        public bool HasComponent(IParcelScene scene, IDCLEntity entity)
 106        {
 112107            return ComponentDataContainsEntity(componentData, scene, entity);
 108        }
 109
 110        /// <summary>
 111        /// get component data for an entity
 112        /// </summary>
 113        /// <param name="scene">target scene</param>
 114        /// <param name="entity">target entity</param>
 115        /// <returns>component data, including model</returns>
 116        public IECSReadOnlyComponentData<ModelType> Get(IParcelScene scene, IDCLEntity entity)
 117        {
 33118            if (ComponentDataTryGet(componentData, scene, entity, out ECSComponentData<ModelType> data))
 119            {
 33120                return data;
 121            }
 0122            return null;
 123        }
 124
 125        private static bool ComponentDataContainsEntity(IReadOnlyDictionary<IParcelScene, Dictionary<long, ECSComponentD
 126            IParcelScene scene, IDCLEntity entity)
 127        {
 185128            if (!componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData))
 79129                return false;
 130
 106131            return entitiesData.ContainsKey(entity.entityId);
 132        }
 133
 134        private static void ComponentDataAdd(IDictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>> co
 135            IParcelScene scene, IDCLEntity entity, ECSComponentData<ModelType> newData)
 136        {
 73137            if (!componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData))
 138            {
 34139                entitiesData = new Dictionary<long, ECSComponentData<ModelType>>();
 34140                componentData.Add(scene, entitiesData);
 141            }
 73142            entitiesData.Add(entity.entityId, newData);
 73143        }
 144
 145        private static bool ComponentDataRemove(IDictionary<IParcelScene, Dictionary<long, ECSComponentData<ModelType>>>
 146            IParcelScene scene, IDCLEntity entity, out ECSComponentData<ModelType> data)
 147        {
 39148            if (!componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData))
 149            {
 0150                data = null;
 0151                return false;
 152            }
 153
 39154            if (!entitiesData.TryGetValue(entity.entityId, out data))
 0155                return false;
 156
 39157            entitiesData.Remove(entity.entityId);
 158
 39159            if (entitiesData.Count == 0)
 160            {
 10161                componentData.Remove(scene);
 162            }
 39163            return true;
 164        }
 165
 166        private static bool ComponentDataTryGet(IReadOnlyDictionary<IParcelScene, Dictionary<long, ECSComponentData<Mode
 167            IParcelScene scene, IDCLEntity entity, out ECSComponentData<ModelType> data)
 168        {
 103169            if (componentData.TryGetValue(scene, out Dictionary<long, ECSComponentData<ModelType>> entitiesData))
 103170                return entitiesData.TryGetValue(entity.entityId, out data);
 171
 0172            data = null;
 0173            return false;
 174        }
 175    }
 176}