< 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:50
Uncovered lines:6
Coverable lines:56
Total lines:142
Line coverage:89.2% (50 of 56)
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%3.143075%
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 DCL.Controllers;
 2using DCL.ECSRuntime;
 3using DCL.Models;
 4using UnityEngine;
 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 disposed = false;
 14
 6115        public CRDTProtocol crdtProtocol { get; }
 16
 17117        public CRDTExecutor(IParcelScene scene, ECSComponentsManager componentsManager)
 18        {
 17119            ownerScene = scene;
 17120            crdtProtocol = new CRDTProtocol();
 17121            ecsManager = componentsManager;
 17122        }
 23
 24        public void Dispose()
 25        {
 26#if UNITY_EDITOR
 1127            if (disposed)
 328                Debug.LogWarning("CRDTExecutor::Dispose Called while disposed");
 29#endif
 30
 1131            if (disposed)
 332                return;
 33
 834            disposed = true;
 35
 836            using (var entities = ownerScene.entities.Values.GetEnumerator())
 37            {
 1538                while (entities.MoveNext())
 39                {
 740                    var entity = entities.Current;
 741                    entity.OnRemoved -= OnEntityRemoved;
 742                    ecsManager.RemoveAllComponents(ownerScene, entity);
 43                }
 844            }
 845        }
 46
 47        public void Execute(CRDTMessage crdtMessage)
 48        {
 49#if UNITY_EDITOR
 1750            if (disposed)
 051                Debug.LogWarning("CRDTExecutor::Execute Called while disposed");
 52#endif
 53
 1754            CRDTMessage storedMessage = crdtProtocol.GetState(crdtMessage.key1, crdtMessage.key2);
 1755            CRDTMessage resultMessage = crdtProtocol.ProcessMessage(crdtMessage);
 56
 57            // messages are the same so state didn't change
 1758            if (storedMessage == resultMessage)
 59            {
 060                return;
 61            }
 62
 1763            ExecuteWithoutStoringState(crdtMessage.key1, crdtMessage.key2, crdtMessage.data);
 1764        }
 65
 66        public void ExecuteWithoutStoringState(long entityId, int componentId, object data)
 67        {
 68#if UNITY_EDITOR
 43069            if (disposed)
 070                Debug.LogWarning("CRDTExecutor::ExecuteWithoutStoringState Called while disposed");
 71#endif
 72
 43073            if (disposed)
 074                return;
 75
 76            // null data means to remove component, not null data means to update or create
 43077            if (data != null)
 78            {
 35879                PutComponent(ownerScene, entityId, componentId, data);
 80            }
 81            else
 82            {
 7283                RemoveComponent(ownerScene, entityId, componentId);
 84            }
 7285        }
 86
 87        private void PutComponent(IParcelScene scene, long entityId, int componentId, object data)
 88        {
 35889            IDCLEntity entity = GetOrCreateEntity(scene, entityId);
 35890            ecsManager.DeserializeComponent(componentId, scene, entity, data);
 35891        }
 92
 93        private void RemoveComponent(IParcelScene scene, long entityId, int componentId)
 94        {
 7295            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 96            {
 097                return;
 98            }
 99
 72100            ecsManager.RemoveComponent(componentId, scene, entity);
 101
 102            // there is no component for this entity so we remove it
 103            // from scene
 72104            if (!ecsManager.HasAnyComponent(scene, entity))
 105            {
 9106                RemoveEntity(scene, entityId);
 107            }
 72108        }
 109
 110        private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId)
 111        {
 358112            if (scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 113            {
 305114                return entity;
 115            }
 116
 117            // CreateEntity internally adds entity to `scene.entities`
 53118            entity = scene.CreateEntity(entityId);
 53119            entity.OnRemoved += OnEntityRemoved;
 53120            return entity;
 121        }
 122
 123        private void RemoveEntity(IParcelScene scene, long entityId)
 124        {
 9125            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 126            {
 0127                return;
 128            }
 129
 130            // we unsubscribe since this methods means that entity has no components
 131            // so there is no need to try to clean them up on removed event
 9132            entity.OnRemoved -= OnEntityRemoved;
 9133            scene.RemoveEntity(entityId);
 9134        }
 135
 136        private void OnEntityRemoved(IDCLEntity entity)
 137        {
 1138            entity.OnRemoved -= OnEntityRemoved;
 1139            ecsManager.RemoveAllComponents(ownerScene, entity);
 1140        }
 141    }
 142}