< 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:29
Uncovered lines:4
Coverable lines:33
Total lines:148
Line coverage:87.8% (29 of 33)
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%330100%
SetModel(...)0%3.583060%
Deserialize(...)0%110100%
HasComponent(...)0%110100%
Get(...)0%220100%
Get(...)0%220100%
Get()0%110100%

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    {
 91011        internal readonly DualKeyValueSet<IParcelScene, long, ECSComponentData<ModelType>> componentData =
 12            new DualKeyValueSet<IParcelScene, long, ECSComponentData<ModelType>>(50);
 13
 14        private readonly Func<IECSComponentHandler<ModelType>> handlerBuilder;
 15        private readonly Func<object, ModelType> deserializer;
 16
 91017        public ECSComponent(Func<object, ModelType> deserializer, Func<IECSComponentHandler<ModelType>> handlerBuilder)
 18        {
 91019            this.deserializer = deserializer;
 91020            this.handlerBuilder = handlerBuilder;
 91021        }
 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        {
 28830            var entityId = entity.entityId;
 31
 28832            if (componentData.ContainsKey(scene, entityId))
 33            {
 034                Debug.LogError($"entity {entityId} already contains component {typeof(ModelType)}", entity.gameObject);
 035                return;
 36            }
 37
 28838            var handler = handlerBuilder?.Invoke();
 39
 28840            componentData.Add(scene, entityId, new ECSComponentData<ModelType>()
 41            {
 42                entity = entity,
 43                model = default,
 44                scene = scene,
 45                handler = handler
 46            });
 47
 28848            handler?.OnComponentCreated(scene, entity);
 28849        }
 50
 51        /// <summary>
 52        /// remove component from entity
 53        /// </summary>
 54        /// <param name="scene">target scene</param>
 55        /// <param name="entity">target entity</param>
 56        /// <returns>true if component removed successfully, false if entity didn't contain component</returns>
 57        public bool Remove(IParcelScene scene, IDCLEntity entity)
 58        {
 19759            if (!componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data))
 8060                return false;
 61
 11762            data.handler?.OnComponentRemoved(scene, entity);
 11763            componentData.Remove(scene, entity.entityId);
 11764            return true;
 65        }
 66
 67        /// <summary>
 68        /// set component model for entity
 69        /// </summary>
 70        /// <param name="scene">target scene</param>
 71        /// <param name="entity">target entity</param>
 72        /// <param name="model">new model</param>
 73        public void SetModel(IParcelScene scene, IDCLEntity entity, ModelType model)
 74        {
 40475            if (componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data))
 76            {
 40477                data.model = model;
 40478                data.handler?.OnComponentModelUpdated(scene, entity, model);
 79            }
 80            else
 81            {
 082                Debug.LogError($"trying to update model but entity {entity.entityId} does not contains component {typeof
 83                    entity.gameObject);
 84            }
 085        }
 86
 87        /// <summary>
 88        /// deserialize message and apply a new model for an entity
 89        /// </summary>
 90        /// <param name="scene">target scene</param>
 91        /// <param name="entity">target entity</param>
 92        /// <param name="message">message</param>
 93        public void Deserialize(IParcelScene scene, IDCLEntity entity, object message)
 94        {
 37095            SetModel(scene, entity, deserializer.Invoke(message));
 37096        }
 97
 98        /// <summary>
 99        /// check if entity contains component
 100        /// </summary>
 101        /// <param name="scene">target scene</param>
 102        /// <param name="entity">target entity</param>
 103        /// <returns>true if entity contains this component</returns>
 104        public bool HasComponent(IParcelScene scene, IDCLEntity entity)
 105        {
 930106            return componentData.ContainsKey(scene, entity.entityId);
 107        }
 108
 109        /// <summary>
 110        /// get component data for an entity
 111        /// </summary>
 112        /// <param name="scene">target scene</param>
 113        /// <param name="entity">target entity</param>
 114        /// <returns>component data, including model</returns>
 115        public IECSReadOnlyComponentData<ModelType> Get(IParcelScene scene, IDCLEntity entity)
 116        {
 388117            if (componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data))
 118            {
 236119                return data;
 120            }
 152121            return null;
 122        }
 123
 124        /// <summary>
 125        /// get component data for an entity
 126        /// </summary>
 127        /// <param name="scene">target scene</param>
 128        /// <param name="entityId">target entity id</param>
 129        /// <returns>component data, including model</returns>
 130        public IECSReadOnlyComponentData<ModelType> Get(IParcelScene scene, long entityId)
 131        {
 101132            if (componentData.TryGetValue(scene, entityId, out ECSComponentData<ModelType> data))
 133            {
 61134                return data;
 135            }
 40136            return null;
 137        }
 138
 139        /// <summary>
 140        /// get component data for all entities
 141        /// </summary>
 142        /// <returns>list of component's data</returns>
 143        public IReadOnlyList<KeyValueSetTriplet<IParcelScene, long, ECSComponentData<ModelType>>> Get()
 144        {
 127145            return componentData.Pairs;
 146        }
 147    }
 148}