< 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:40
Uncovered lines:14
Coverable lines:54
Total lines:134
Line coverage:74% (40 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CRDTExecutor(...)0%110100%
Dispose()0%6200%
Execute(...)0%3.013088.89%
ExecuteWithoutStoringState(...)0%3.033085.71%
PutComponent(...)0%110100%
RemoveComponent(...)0%3.043083.33%
GetOrCreateEntity(...)0%220100%
RemoveEntity(...)0%2.032080%
OnEntityRemoved(...)0%110100%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Controllers;
 3using DCL.ECSRuntime;
 4using DCL.Models;
 5
 6namespace DCL.CRDT
 7{
 8    public class CRDTExecutor : ICRDTExecutor
 9    {
 10        private readonly IParcelScene ownerScene;
 11        private readonly ECSComponentsManager ecsManager;
 12
 13        private bool sceneAdded = false;
 14        private bool disposed = false;
 15        private readonly IList<IParcelScene> loadedScenes;
 16
 017        public CRDTProtocol crdtProtocol { get; }
 18
 5519        public CRDTExecutor(IParcelScene scene, ECSComponentsManager componentsManager)
 20        {
 5521            ownerScene = scene;
 5522            crdtProtocol = new CRDTProtocol();
 5523            ecsManager = componentsManager;
 5524            loadedScenes = DataStore.i.ecs7.scenes;
 5525        }
 26
 27        public void Dispose()
 28        {
 029            disposed = true;
 030            loadedScenes.Remove(ownerScene);
 031            using (var entities = ownerScene.entities.Values.GetEnumerator())
 32            {
 033                while (entities.MoveNext())
 34                {
 035                    var entity = entities.Current;
 036                    entity.OnRemoved -= OnEntityRemoved;
 037                    ecsManager.RemoveAllComponents(ownerScene, entity);
 38                }
 039            }
 040        }
 41
 42        public void Execute(CRDTMessage crdtMessage)
 43        {
 2644            if (!sceneAdded)
 45            {
 846                sceneAdded = true;
 847                loadedScenes.Add(ownerScene);
 48            }
 49
 2650            CRDTMessage storedMessage = crdtProtocol.GetState(crdtMessage.key1, crdtMessage.key2);
 2651            CRDTMessage resultMessage = crdtProtocol.ProcessMessage(crdtMessage);
 52
 53            // messages are the same so state didn't change
 2654            if (storedMessage == resultMessage)
 55            {
 056                return;
 57            }
 58
 59
 2660            ExecuteWithoutStoringState(crdtMessage.key1, crdtMessage.key2, crdtMessage.data);
 2661        }
 62
 63        public void ExecuteWithoutStoringState(long entityId, int componentId, object data)
 64        {
 12165            if (disposed)
 066                return;
 67
 68            // null data means to remove component, not null data means to update or create
 12169            if (data != null)
 70            {
 9771                PutComponent(ownerScene, entityId, componentId, data);
 9772            }
 73            else
 74            {
 2475                RemoveComponent(ownerScene, entityId, componentId);
 76            }
 2477        }
 78
 79        private void PutComponent(IParcelScene scene, long entityId, int componentId, object data)
 80        {
 9781            IDCLEntity entity = GetOrCreateEntity(scene, entityId);
 9782            ecsManager.DeserializeComponent(componentId, scene, entity, data);
 9783        }
 84
 85        private void RemoveComponent(IParcelScene scene, long entityId, int componentId)
 86        {
 2487            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 88            {
 089                return;
 90            }
 91
 2492            ecsManager.RemoveComponent(componentId, scene, entity);
 93
 94            // there is no component for this entity so we remove it
 95            // from scene
 2496            if (!ecsManager.HasAnyComponent(scene, entity))
 97            {
 298                RemoveEntity(scene, entityId);
 99            }
 24100        }
 101
 102        private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId)
 103        {
 97104            if (scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 105            {
 83106                return entity;
 107            }
 108
 109            // CreateEntity internally adds entity to `scene.entities`
 14110            entity = scene.CreateEntity(entityId);
 14111            entity.OnRemoved += OnEntityRemoved;
 14112            return entity;
 113        }
 114
 115        private void RemoveEntity(IParcelScene scene, long entityId)
 116        {
 2117            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 118            {
 0119                return;
 120            }
 121
 122            // we unsubscribe since this methods means that entity has no components
 123            // so there is no need to try to clean them up on removed event
 2124            entity.OnRemoved -= OnEntityRemoved;
 2125            scene.RemoveEntity(entityId);
 2126        }
 127
 128        private void OnEntityRemoved(IDCLEntity entity)
 129        {
 1130            entity.OnRemoved -= OnEntityRemoved;
 1131            ecsManager.RemoveAllComponents(ownerScene, entity);
 1132        }
 133    }
 134}