< Summary

Class:DCL.CRDT.CRDTExecutor
Assembly:DCL.CRDTProtocol.Executor
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/CRDTProtocol/Executor/CRDTExecutor.cs
Covered lines:47
Uncovered lines:4
Coverable lines:51
Total lines:124
Line coverage:92.1% (47 of 51)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CRDTExecutor(...)0%110100%
Dispose()0%110100%
Execute(...)0%3.013090.91%
PutComponent(...)0%110100%
RemoveComponent(...)0%4.054085.71%
GetOrCreateEntity(...)0%220100%
RemoveEntity(...)0%2.022083.33%
GetOrCreateECSManager(...)0%220100%
OnEntityRemoved(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/CRDTProtocol/Executor/CRDTExecutor.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECSRuntime;
 3using DCL.Models;
 4
 5namespace DCL.CRDT
 6{
 7    public class CRDTExecutor : ICRDTExecutor
 8    {
 9        private readonly IParcelScene ownerScene;
 10        private readonly ECSComponentsFactory ecsComponentsFactory;
 11
 12        private ECSComponentsManager ecsManager;
 13
 014        public CRDTProtocol crdtProtocol { get; }
 15
 56816        public CRDTExecutor(IParcelScene scene)
 17        {
 56818            ownerScene = scene;
 56819            crdtProtocol = new CRDTProtocol();
 56820            ecsComponentsFactory = DataStore.i.ecs7.componentsFactory;
 56821        }
 22
 23        public void Dispose()
 24        {
 56425            DataStore.i.ecs7.componentsManagers.Remove(ownerScene);
 56426            DataStore.i.ecs7.scenes.Remove(ownerScene);
 56427        }
 28
 29        public void Execute(CRDTMessage crdtMessage)
 30        {
 931            CRDTMessage storedMessage = crdtProtocol.GetState(crdtMessage.key1, crdtMessage.key2);
 932            CRDTMessage resultMessage = crdtProtocol.ProcessMessage(crdtMessage);
 33
 34            // messages are the same so state didn't change
 935            if (storedMessage == resultMessage)
 36            {
 037                return;
 38            }
 39
 940            long entityId = resultMessage.key1;
 941            int componentId = resultMessage.key2;
 42
 43            // null data means to remove component, not null data means to update or create
 944            if (resultMessage.data != null)
 45            {
 646                PutComponent(ownerScene, entityId, componentId, resultMessage.data);
 647            }
 48            else
 49            {
 350                RemoveComponent(ownerScene, entityId, componentId);
 51            }
 352        }
 53
 54        private void PutComponent(IParcelScene scene, long entityId, int componentId, object data)
 55        {
 656            IDCLEntity entity = GetOrCreateEntity(scene, entityId);
 657            ECSComponentsManager ecsManager = GetOrCreateECSManager(scene);
 658            ecsManager.DeserializeComponent(componentId, entity, data);
 659        }
 60
 61        private void RemoveComponent(IParcelScene scene, long entityId, int componentId)
 62        {
 363            IDCLEntity entity = scene.GetEntityById(entityId);
 64
 365            if (entity == null || ecsManager == null)
 66            {
 067                return;
 68            }
 69
 370            ecsManager.RemoveComponent(componentId, entity);
 71
 72            // there is no component for this entity so we remove it
 73            // from scene
 374            if (!ecsManager.HasAnyComponent(entity))
 75            {
 276                RemoveEntity(scene, entityId);
 77            }
 378        }
 79
 80        private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId)
 81        {
 682            IDCLEntity entity = scene.GetEntityById(entityId);
 683            if (entity != null)
 84            {
 285                return entity;
 86            }
 87
 488            entity = scene.CreateEntity(entityId);
 489            entity.OnRemoved += OnEntityRemoved;
 490            return entity;
 91        }
 92
 93        private void RemoveEntity(IParcelScene scene, long entityId)
 94        {
 295            IDCLEntity entity = scene.GetEntityById(entityId);
 296            if (entity == null)
 97            {
 098                return;
 99            }
 100
 101            // we unsubscribe since this methods means that entity has no components
 102            // so there is no need to try to clean them up on removed event
 2103            entity.OnRemoved -= OnEntityRemoved;
 2104            scene.RemoveEntity(entityId);
 2105        }
 106
 107        private ECSComponentsManager GetOrCreateECSManager(IParcelScene scene)
 108        {
 6109            if (ecsManager != null)
 110            {
 2111                return ecsManager;
 112            }
 4113            ecsManager = new ECSComponentsManager(scene, ecsComponentsFactory.componentBuilders);
 4114            DataStore.i.ecs7.componentsManagers.Add(scene, ecsManager);
 4115            DataStore.i.ecs7.scenes.Add(scene);
 4116            return ecsManager;
 117        }
 118
 119        private void OnEntityRemoved(IDCLEntity entity)
 120        {
 1121            ecsManager?.RemoveAllComponents(entity);
 1122        }
 123    }
 124}