< 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:51
Uncovered lines:11
Coverable lines:62
Total lines:152
Line coverage:82.2% (51 of 62)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CRDTExecutor(...)0%110100%
Dispose()0%4.914061.54%
Execute(...)0%4.14081.82%
ExecuteWithoutStoringState(...)0%4.184077.78%
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
 318        public CRDTProtocol crdtProtocol { get; }
 19
 14720        public CRDTExecutor(IParcelScene scene, ECSComponentsManager componentsManager)
 21        {
 14722            ownerScene = scene;
 14723            crdtProtocol = new CRDTProtocol();
 14724            ecsManager = componentsManager;
 14725            loadedScenes = DataStore.i.ecs7.scenes;
 14726        }
 27
 28        public void Dispose()
 29        {
 30#if UNITY_EDITOR
 431            if (disposed)
 032                Debug.LogWarning("CRDTExecutor::Dispose Called while disposed");
 33#endif
 34
 435            if (disposed)
 036                return;
 37
 438            disposed = true;
 439            loadedScenes.Remove(ownerScene);
 440            using (var entities = ownerScene.entities.Values.GetEnumerator())
 41            {
 442                while (entities.MoveNext())
 43                {
 044                    var entity = entities.Current;
 045                    entity.OnRemoved -= OnEntityRemoved;
 046                    ecsManager.RemoveAllComponents(ownerScene, entity);
 47                }
 448            }
 449        }
 50
 51        public void Execute(CRDTMessage crdtMessage)
 52        {
 53#if UNITY_EDITOR
 1254            if (disposed)
 055                Debug.LogWarning("CRDTExecutor::Execute Called while disposed");
 56#endif
 1257            if (!sceneAdded)
 58            {
 659                sceneAdded = true;
 660                loadedScenes.Add(ownerScene);
 61            }
 62
 1263            CRDTMessage storedMessage = crdtProtocol.GetState(crdtMessage.key1, crdtMessage.key2);
 1264            CRDTMessage resultMessage = crdtProtocol.ProcessMessage(crdtMessage);
 65
 66            // messages are the same so state didn't change
 1267            if (storedMessage == resultMessage)
 68            {
 069                return;
 70            }
 71
 72
 1273            ExecuteWithoutStoringState(crdtMessage.key1, crdtMessage.key2, crdtMessage.data);
 1274        }
 75
 76        public void ExecuteWithoutStoringState(long entityId, int componentId, object data)
 77        {
 78#if UNITY_EDITOR
 34279            if (disposed)
 080                Debug.LogWarning("CRDTExecutor::ExecuteWithoutStoringState Called while disposed");
 81#endif
 82
 34283            if (disposed)
 084                return;
 85
 86            // null data means to remove component, not null data means to update or create
 34287            if (data != null)
 88            {
 27189                PutComponent(ownerScene, entityId, componentId, data);
 27190            }
 91            else
 92            {
 7193                RemoveComponent(ownerScene, entityId, componentId);
 94            }
 7195        }
 96
 97        private void PutComponent(IParcelScene scene, long entityId, int componentId, object data)
 98        {
 27199            IDCLEntity entity = GetOrCreateEntity(scene, entityId);
 271100            ecsManager.DeserializeComponent(componentId, scene, entity, data);
 271101        }
 102
 103        private void RemoveComponent(IParcelScene scene, long entityId, int componentId)
 104        {
 71105            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 106            {
 0107                return;
 108            }
 109
 71110            ecsManager.RemoveComponent(componentId, scene, entity);
 111
 112            // there is no component for this entity so we remove it
 113            // from scene
 71114            if (!ecsManager.HasAnyComponent(scene, entity))
 115            {
 8116                RemoveEntity(scene, entityId);
 117            }
 71118        }
 119
 120        private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId)
 121        {
 271122            if (scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 123            {
 234124                return entity;
 125            }
 126
 127            // CreateEntity internally adds entity to `scene.entities`
 37128            entity = scene.CreateEntity(entityId);
 37129            entity.OnRemoved += OnEntityRemoved;
 37130            return entity;
 131        }
 132
 133        private void RemoveEntity(IParcelScene scene, long entityId)
 134        {
 8135            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
 8142            entity.OnRemoved -= OnEntityRemoved;
 8143            scene.RemoveEntity(entityId);
 8144        }
 145
 146        private void OnEntityRemoved(IDCLEntity entity)
 147        {
 1148            entity.OnRemoved -= OnEntityRemoved;
 1149            ecsManager.RemoveAllComponents(ownerScene, entity);
 1150        }
 151    }
 152}