< Summary

Class:DCL.ParcelScenesCleaner
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ParcelScenesCleaner.cs
Covered lines:61
Uncovered lines:15
Coverable lines:76
Total lines:185
Line coverage:80.2% (61 of 76)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MarkedEntityInfo(...)0%2100%
MarkedSharedComponentInfo(...)0%2100%
ParcelScenesCleaner()0%110100%
Start()0%110100%
Stop()0%220100%
MarkForCleanup(...)0%4.024088.89%
MarkRootEntityForCleanup(...)0%110100%
MarkDisposableComponentForCleanup(...)0%110100%
ForceCleanup()0%880100%
CleanupEntitiesCoroutine()0%22.2218076.47%
Dispose()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ParcelScenesCleaner.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Models;
 3using System.Collections;
 4using System.Collections.Generic;
 5using UnityEngine;
 6using Object = UnityEngine.Object;
 7
 8namespace DCL
 9{
 10    public class ParcelScenesCleaner : IParcelScenesCleaner
 11    {
 12        const float MAX_TIME_BUDGET = 0.01f;
 13
 14        private struct MarkedEntityInfo
 15        {
 16            public ParcelScene scene;
 17            public IDCLEntity entity;
 18
 19            public MarkedEntityInfo(ParcelScene scene, IDCLEntity entity)
 20            {
 021                this.scene = scene;
 022                this.entity = entity;
 023            }
 24        }
 25
 26        private struct MarkedSharedComponentInfo
 27        {
 28            public ParcelScene scene;
 29            public string componentId;
 30
 31            public MarkedSharedComponentInfo(ParcelScene scene, string componentId)
 32            {
 033                this.scene = scene;
 034                this.componentId = componentId;
 035            }
 36        }
 37
 66638        Queue<IDCLEntity> entitiesMarkedForCleanup = new Queue<IDCLEntity>();
 66639        Queue<MarkedEntityInfo> rootEntitiesMarkedForCleanup = new Queue<MarkedEntityInfo>();
 66640        Queue<MarkedSharedComponentInfo> disposableComponentsMarkedForCleanup = new Queue<MarkedSharedComponentInfo>();
 41
 42        Coroutine removeEntitiesCoroutine;
 43
 133244        public void Start() { removeEntitiesCoroutine = CoroutineStarter.Start(CleanupEntitiesCoroutine()); }
 45
 46        public void Stop()
 47        {
 68648            if (removeEntitiesCoroutine != null)
 68649                CoroutineStarter.Stop(removeEntitiesCoroutine);
 68650        }
 51
 52        public void MarkForCleanup(IDCLEntity entity)
 53        {
 49654            if (entity.markedForCleanup)
 055                return;
 56
 49657            entity.markedForCleanup = true;
 58
 49659            if (entity.gameObject != null)
 49260                entity.gameObject.SetActive(false);
 61
 62#if UNITY_EDITOR
 49663            if (entity.gameObject != null)
 49264                entity.gameObject.name += "-MARKED-FOR-CLEANUP";
 65#endif
 66
 49667            entitiesMarkedForCleanup.Enqueue(entity);
 49668        }
 69
 70        // When removing all entities from a scene, we need to separate the root entities, as stated in ParcelScene,
 71        // to avoid traversing a lot of child entities in the same frame and other problems
 97672        public void MarkRootEntityForCleanup(IParcelScene scene, IDCLEntity entity) { rootEntitiesMarkedForCleanup.Enque
 73
 105074        public void MarkDisposableComponentForCleanup(IParcelScene scene, string componentId) { disposableComponentsMark
 75
 76        public void ForceCleanup()
 77        {
 131378            while (disposableComponentsMarkedForCleanup.Count > 0)
 79            {
 51880                MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue();
 51881                markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId);
 82            }
 83
 79584            HashSet<ParcelScene> scenesToRemove = new HashSet<ParcelScene>();
 85
 86            // If we have root entities queued for removal, we call Parcel Scene's RemoveEntity()
 87            // so that the child entities end up recursively in the entitiesMarkedForCleanup queue
 128188            while (rootEntitiesMarkedForCleanup.Count > 0)
 89            {
 48690                MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue();
 48691                markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false);
 92
 48693                if (!scenesToRemove.Contains(markedEntityInfo.scene))
 23994                    scenesToRemove.Add(markedEntityInfo.scene);
 95            }
 96
 128997            while (entitiesMarkedForCleanup.Count > 0)
 98            {
 49499                IDCLEntity entity = entitiesMarkedForCleanup.Dequeue();
 494100                entity.SetParent(null);
 494101                entity.Cleanup();
 102            }
 103
 2068104            foreach (var scene in scenesToRemove)
 105            {
 239106                if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id))
 239107                    Object.Destroy(scene.gameObject);
 108            }
 795109        }
 110
 111        IEnumerator CleanupEntitiesCoroutine()
 112        {
 7779113            while (true)
 114            {
 8445115                float lastTime = Time.unscaledTime;
 116
 8452117                while (disposableComponentsMarkedForCleanup.Count > 0)
 118                {
 7119                    MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue()
 7120                    markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId);
 121
 7122                    if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
 123                    {
 0124                        yield return null;
 0125                        lastTime = Time.unscaledTime;
 126                    }
 127                }
 128
 8445129                HashSet<ParcelScene> scenesToRemove = new HashSet<ParcelScene>();
 130
 131                // If we have root entities queued for removal, we call Parcel Scene's RemoveEntity()
 132                // so that the child entities end up recursively in the entitiesMarkedForCleanup queue
 8447133                while (rootEntitiesMarkedForCleanup.Count > 0)
 134                {
 2135                    MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue();
 2136                    markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false);
 137
 2138                    if (!scenesToRemove.Contains(markedEntityInfo.scene))
 2139                        scenesToRemove.Add(markedEntityInfo.scene);
 140
 2141                    if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
 142                    {
 0143                        yield return null;
 0144                        lastTime = Time.unscaledTime;
 145                    }
 146                }
 147
 8447148                while (entitiesMarkedForCleanup.Count > 0)
 149                {
 2150                    IDCLEntity entity = entitiesMarkedForCleanup.Dequeue();
 2151                    entity.SetParent(null);
 2152                    entity.Cleanup();
 153
 2154                    if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
 155                    {
 0156                        yield return null;
 0157                        lastTime = Time.unscaledTime;
 158                    }
 159                }
 160
 16894161                foreach (var scene in scenesToRemove)
 162                {
 2163                    if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id))
 164                    {
 2165                        Object.Destroy(scene.gameObject);
 166
 2167                        if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET)
 168                        {
 0169                            yield return null;
 0170                            lastTime = Time.unscaledTime;
 171                        }
 172                    }
 173                }
 174
 8445175                yield return null;
 7779176            }
 177        }
 178
 179        public void Dispose()
 180        {
 686181            ForceCleanup();
 686182            Stop();
 686183        }
 184    }
 185}