< 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:25
Uncovered lines:6
Coverable lines:31
Total lines:133
Line coverage:80.6% (25 of 31)
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.073080%
SetModel(...)0%3.333066.67%
Deserialize(...)0%110100%
HasComponent(...)0%110100%
Get(...)0%220100%
Get()0%2100%

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    {
 15211        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
 15217        public ECSComponent(Func<object, ModelType> deserializer, Func<IECSComponentHandler<ModelType>> handlerBuilder)
 18        {
 15219            this.deserializer = deserializer;
 15220            this.handlerBuilder = handlerBuilder;
 15221        }
 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        {
 14030            var entityId = entity.entityId;
 31
 14032            if (componentData.ContainsKey(scene, entityId))
 33            {
 034                Debug.LogError($"entity {entityId} already contains component {typeof(ModelType)}", entity.gameObject);
 035                return;
 36            }
 37
 14038            var handler = handlerBuilder?.Invoke();
 39
 14040            componentData.Add(scene, entityId, new ECSComponentData<ModelType>()
 41            {
 42                entity = entity,
 43                model = default,
 44                scene = scene,
 45                handler = handler
 46            });
 47
 14048            handler?.OnComponentCreated(scene, entity);
 14049        }
 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        {
 5959            if (!componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data))
 060                return false;
 61
 5962            data.handler?.OnComponentRemoved(scene, entity);
 5963            componentData.Remove(scene, entity.entityId);
 5964            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        {
 14675            if (componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data))
 76            {
 14677                data.model = model;
 14678                data.handler?.OnComponentModelUpdated(scene, entity, model);
 9579            }
 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        {
 11595            SetModel(scene, entity, deserializer.Invoke(message));
 11596        }
 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        {
 274106            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        {
 141117            if (componentData.TryGetValue(scene, entity.entityId, out ECSComponentData<ModelType> data))
 118            {
 93119                return data;
 120            }
 48121            return null;
 122        }
 123
 124        /// <summary>
 125        /// get component data for all entities
 126        /// </summary>
 127        /// <returns>list of component's data</returns>
 128        public IReadOnlyList<KeyValueSetTriplet<IParcelScene, long, ECSComponentData<ModelType>>> Get()
 129        {
 0130            return componentData.Pairs;
 131        }
 132    }
 133}