< 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:55
Uncovered lines:6
Coverable lines:61
Total lines:152
Line coverage:90.1% (55 of 61)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CRDTExecutor(...)0%110100%
Dispose()0%440100%
Execute(...)0%4.14081.82%
ExecuteWithoutStoringState(...)0%4.254075%
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;
 5using UnityEngine;
 6
 7namespace DCL.CRDT
 8{
 9    public class CRDTExecutor : ICRDTExecutor
 10    {
 11        private readonly IParcelScene ownerScene;
 12        private readonly ECSComponentsManager ecsManager;
 13
 14        private bool sceneAdded = false;
 15        private bool disposed = false;
 16        private readonly IList<IParcelScene> loadedScenes;
 17
 3118        public CRDTProtocol crdtProtocol { get; }
 19
 16520        public CRDTExecutor(IParcelScene scene, ECSComponentsManager componentsManager)
 21        {
 16522            ownerScene = scene;
 16523            crdtProtocol = new CRDTProtocol();
 16524            ecsManager = componentsManager;
 16525            loadedScenes = DataStore.i.ecs7.scenes;
 16526        }
 27
 28        public void Dispose()
 29        {
 30#if UNITY_EDITOR
 831            if (disposed)
 232                Debug.LogWarning("CRDTExecutor::Dispose Called while disposed");
 33#endif
 34
 835            if (disposed)
 236                return;
 37
 638            disposed = true;
 639            loadedScenes.Remove(ownerScene);
 640            using (var entities = ownerScene.entities.Values.GetEnumerator())
 41            {
 842                while (entities.MoveNext())
 43                {
 244                    var entity = entities.Current;
 245                    entity.OnRemoved -= OnEntityRemoved;
 246                    ecsManager.RemoveAllComponents(ownerScene, entity);
 47                }
 648            }
 649        }
 50
 51        public void Execute(CRDTMessage crdtMessage)
 52        {
 53#if UNITY_EDITOR
 1454            if (disposed)
 055                Debug.LogWarning("CRDTExecutor::Execute Called while disposed");
 56#endif
 1457            if (!sceneAdded)
 58            {
 859                sceneAdded = true;
 860                loadedScenes.Add(ownerScene);
 61            }
 62
 1463            CRDTMessage storedMessage = crdtProtocol.GetState(crdtMessage.key1, crdtMessage.key2);
 1464            CRDTMessage resultMessage = crdtProtocol.ProcessMessage(crdtMessage);
 65
 66            // messages are the same so state didn't change
 1467            if (storedMessage == resultMessage)
 68            {
 069                return;
 70            }
 71
 72
 1473            ExecuteWithoutStoringState(crdtMessage.key1, crdtMessage.key2, crdtMessage.data);
 1474        }
 75
 76        public void ExecuteWithoutStoringState(long entityId, int componentId, object data)
 77        {
 78#if UNITY_EDITOR
 38579            if (disposed)
 080                Debug.LogWarning("CRDTExecutor::ExecuteWithoutStoringState Called while disposed");
 81#endif
 82
 38583            if (disposed)
 084                return;
 85
 86            // null data means to remove component, not null data means to update or create
 38587            if (data != null)
 88            {
 31389                PutComponent(ownerScene, entityId, componentId, data);
 90            }
 91            else
 92            {
 7293                RemoveComponent(ownerScene, entityId, componentId);
 94            }
 7295        }
 96
 97        private void PutComponent(IParcelScene scene, long entityId, int componentId, object data)
 98        {
 31399            IDCLEntity entity = GetOrCreateEntity(scene, entityId);
 313100            ecsManager.DeserializeComponent(componentId, scene, entity, data);
 313101        }
 102
 103        private void RemoveComponent(IParcelScene scene, long entityId, int componentId)
 104        {
 72105            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 106            {
 0107                return;
 108            }
 109
 72110            ecsManager.RemoveComponent(componentId, scene, entity);
 111
 112            // there is no component for this entity so we remove it
 113            // from scene
 72114            if (!ecsManager.HasAnyComponent(scene, entity))
 115            {
 9116                RemoveEntity(scene, entityId);
 117            }
 72118        }
 119
 120        private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId)
 121        {
 313122            if (scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 123            {
 267124                return entity;
 125            }
 126
 127            // CreateEntity internally adds entity to `scene.entities`
 46128            entity = scene.CreateEntity(entityId);
 46129            entity.OnRemoved += OnEntityRemoved;
 46130            return entity;
 131        }
 132
 133        private void RemoveEntity(IParcelScene scene, long entityId)
 134        {
 9135            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 136            {
 0137                return;
 138            }
 139
 140            // we unsubscribe since this methods means that entity has no components
 141            // so there is no need to try to clean them up on removed event
 9142            entity.OnRemoved -= OnEntityRemoved;
 9143            scene.RemoveEntity(entityId);
 9144        }
 145
 146        private void OnEntityRemoved(IDCLEntity entity)
 147        {
 1148            entity.OnRemoved -= OnEntityRemoved;
 1149            ecsManager.RemoveAllComponents(ownerScene, entity);
 1150        }
 151    }
 152}