< 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:67
Uncovered lines:7
Coverable lines:74
Total lines:180
Line coverage:90.5% (67 of 74)
Covered branches:0
Total branches:0
Covered methods:12
Total methods:12
Method coverage:100% (12 of 12)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CRDTExecutor(...)0%110100%
Dispose()0%4.074083.33%
Execute(...)0%5.125083.33%
ExecuteWithoutStoringState(...)0%4.254075%
GenerateInitialEntities()0%110100%
PutComponent[T](...)0%440100%
RemoveComponent(...)0%5.025090%
DeserializeComponent(...)0%110100%
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.ECS7;
 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 disposed = false;
 15
 2116        public CRDTProtocol crdtProtocol { get; }
 17
 33618        public CRDTExecutor(IParcelScene scene, ECSComponentsManager componentsManager)
 19        {
 33620            ownerScene = scene;
 33621            crdtProtocol = new CRDTProtocol();
 33622            ecsManager = componentsManager;
 33623        }
 24
 25        public void Dispose()
 26        {
 27#if UNITY_EDITOR
 32728            if (disposed)
 029                Debug.LogWarning("CRDTExecutor::Dispose Called while disposed");
 30#endif
 31
 32732            if (disposed)
 033                return;
 34
 32735            disposed = true;
 36
 32737            using (var entities = ownerScene.entities.Values.GetEnumerator())
 38            {
 84039                while (entities.MoveNext())
 40                {
 51341                    var entity = entities.Current;
 51342                    entity.OnRemoved -= OnEntityRemoved;
 51343                    ecsManager.RemoveAllComponents(ownerScene, entity);
 44                }
 32745            }
 32746        }
 47
 48        public void Execute(CrdtMessage crdtMessage)
 49        {
 50#if UNITY_EDITOR
 1451            if (disposed)
 052                Debug.LogWarning("CRDTExecutor::Execute Called while disposed");
 53#endif
 54
 1455            CRDTProtocol.ProcessMessageResultType resultType = crdtProtocol.ProcessMessage(crdtMessage);
 56
 57            // If the message change the state
 1458            if (resultType == CRDTProtocol.ProcessMessageResultType.StateUpdatedData ||
 59                resultType == CRDTProtocol.ProcessMessageResultType.StateUpdatedTimestamp ||
 60                resultType == CRDTProtocol.ProcessMessageResultType.EntityWasDeleted)
 61            {
 1462                ExecuteWithoutStoringState(crdtMessage.EntityId, crdtMessage.ComponentId, crdtMessage.Data);
 63            }
 1464        }
 65
 66        public void ExecuteWithoutStoringState(long entityId, int componentId, object data)
 67        {
 68#if UNITY_EDITOR
 1969            if (disposed)
 070                Debug.LogWarning($"CRDTExecutor::ExecuteWithoutStoringState Called while disposed scene {ownerScene.scen
 71#endif
 72
 1973            if (disposed)
 074                return;
 75
 76            // null data means to remove component, not null data means to update or create
 1977            if (data != null)
 78            {
 1579                DeserializeComponent(ownerScene, entityId, componentId, data);
 80            }
 81            else
 82            {
 483                RemoveComponent(entityId, componentId);
 84            }
 485        }
 86
 87        public void GenerateInitialEntities()
 88        {
 489            var cameraEntity = GetOrCreateEntity(ownerScene, SpecialEntityId.CAMERA_ENTITY);
 490            var playerEntity = GetOrCreateEntity(ownerScene, SpecialEntityId.PLAYER_ENTITY);
 491            ecsManager.GetOrCreateComponent(ComponentID.TRANSFORM, ownerScene, cameraEntity);
 492            ecsManager.GetOrCreateComponent(ComponentID.TRANSFORM, ownerScene, playerEntity);
 493        }
 94
 95        public void PutComponent<T>(long entityId, ECSComponent<T> component, T model)
 96        {
 97#if UNITY_EDITOR
 119798            if (disposed)
 499                Debug.LogWarning($"CRDTExecutor::PutComponent Called while disposed scene {ownerScene.sceneData.sceneNum
 100#endif
 101
 1197102            if (disposed)
 4103                return;
 104
 1193105            IDCLEntity entity = GetOrCreateEntity(ownerScene, entityId);
 106
 1193107            if (!component.HasComponent(ownerScene, entity))
 108            {
 700109                component.Create(ownerScene, entity);
 700110                ecsManager.SignalComponentCreated(ownerScene, entity, component);
 111            }
 112
 1193113            component.SetModel(ownerScene, entity, model);
 1193114            ecsManager.SignalComponentUpdated(ownerScene, entity, component);
 1193115        }
 116
 117        public void RemoveComponent(long entityId, int componentId)
 118        {
 119#if UNITY_EDITOR
 184120            if (disposed)
 4121                Debug.LogWarning($"CRDTExecutor::RemoveComponent Called while disposed scene {ownerScene.sceneData.scene
 122#endif
 123
 184124            if (disposed)
 4125                return;
 126
 180127            if (!ownerScene.entities.TryGetValue(entityId, out IDCLEntity entity))
 128            {
 0129                return;
 130            }
 131
 180132            ecsManager.RemoveComponent(componentId, ownerScene, entity);
 133
 134            // there is no component for this entity so we remove it
 135            // from scene
 180136            if (!ecsManager.HasAnyComponent(ownerScene, entity))
 137            {
 38138                RemoveEntity(ownerScene, entityId);
 139            }
 180140        }
 141
 142        private void DeserializeComponent(IParcelScene scene, long entityId, int componentId, object data)
 143        {
 15144            IDCLEntity entity = GetOrCreateEntity(scene, entityId);
 15145            ecsManager.DeserializeComponent(componentId, scene, entity, data);
 15146        }
 147
 148        private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId)
 149        {
 1216150            if (scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 151            {
 1098152                return entity;
 153            }
 154
 155            // CreateEntity internally adds entity to `scene.entities`
 118156            entity = scene.CreateEntity(entityId);
 118157            entity.OnRemoved += OnEntityRemoved;
 118158            return entity;
 159        }
 160
 161        private void RemoveEntity(IParcelScene scene, long entityId)
 162        {
 38163            if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity))
 164            {
 0165                return;
 166            }
 167
 168            // we unsubscribe since this methods means that entity has no components
 169            // so there is no need to try to clean them up on removed event
 38170            entity.OnRemoved -= OnEntityRemoved;
 38171            scene.RemoveEntity(entityId);
 38172        }
 173
 174        private void OnEntityRemoved(IDCLEntity entity)
 175        {
 1176            entity.OnRemoved -= OnEntityRemoved;
 1177            ecsManager.RemoveAllComponents(ownerScene, entity);
 1178        }
 179    }
 180}