| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | namespace 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 | | { |
| 0 | 21 | | this.scene = scene; |
| 0 | 22 | | this.entity = entity; |
| 0 | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| | 26 | | private struct MarkedSharedComponentInfo |
| | 27 | | { |
| | 28 | | public ParcelScene scene; |
| | 29 | | public string componentId; |
| | 30 | |
|
| | 31 | | public MarkedSharedComponentInfo(ParcelScene scene, string componentId) |
| | 32 | | { |
| 0 | 33 | | this.scene = scene; |
| 0 | 34 | | this.componentId = componentId; |
| 0 | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| 666 | 38 | | Queue<IDCLEntity> entitiesMarkedForCleanup = new Queue<IDCLEntity>(); |
| 666 | 39 | | Queue<MarkedEntityInfo> rootEntitiesMarkedForCleanup = new Queue<MarkedEntityInfo>(); |
| 666 | 40 | | Queue<MarkedSharedComponentInfo> disposableComponentsMarkedForCleanup = new Queue<MarkedSharedComponentInfo>(); |
| | 41 | |
|
| | 42 | | Coroutine removeEntitiesCoroutine; |
| | 43 | |
|
| 1332 | 44 | | public void Start() { removeEntitiesCoroutine = CoroutineStarter.Start(CleanupEntitiesCoroutine()); } |
| | 45 | |
|
| | 46 | | public void Stop() |
| | 47 | | { |
| 686 | 48 | | if (removeEntitiesCoroutine != null) |
| 686 | 49 | | CoroutineStarter.Stop(removeEntitiesCoroutine); |
| 686 | 50 | | } |
| | 51 | |
|
| | 52 | | public void MarkForCleanup(IDCLEntity entity) |
| | 53 | | { |
| 496 | 54 | | if (entity.markedForCleanup) |
| 0 | 55 | | return; |
| | 56 | |
|
| 496 | 57 | | entity.markedForCleanup = true; |
| | 58 | |
|
| 496 | 59 | | if (entity.gameObject != null) |
| 492 | 60 | | entity.gameObject.SetActive(false); |
| | 61 | |
|
| | 62 | | #if UNITY_EDITOR |
| 496 | 63 | | if (entity.gameObject != null) |
| 492 | 64 | | entity.gameObject.name += "-MARKED-FOR-CLEANUP"; |
| | 65 | | #endif |
| | 66 | |
|
| 496 | 67 | | entitiesMarkedForCleanup.Enqueue(entity); |
| 496 | 68 | | } |
| | 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 |
| 976 | 72 | | public void MarkRootEntityForCleanup(IParcelScene scene, IDCLEntity entity) { rootEntitiesMarkedForCleanup.Enque |
| | 73 | |
|
| 1050 | 74 | | public void MarkDisposableComponentForCleanup(IParcelScene scene, string componentId) { disposableComponentsMark |
| | 75 | |
|
| | 76 | | public void ForceCleanup() |
| | 77 | | { |
| 1313 | 78 | | while (disposableComponentsMarkedForCleanup.Count > 0) |
| | 79 | | { |
| 518 | 80 | | MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue(); |
| 518 | 81 | | markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId); |
| | 82 | | } |
| | 83 | |
|
| 795 | 84 | | 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 |
| 1281 | 88 | | while (rootEntitiesMarkedForCleanup.Count > 0) |
| | 89 | | { |
| 486 | 90 | | MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue(); |
| 486 | 91 | | markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false); |
| | 92 | |
|
| 486 | 93 | | if (!scenesToRemove.Contains(markedEntityInfo.scene)) |
| 239 | 94 | | scenesToRemove.Add(markedEntityInfo.scene); |
| | 95 | | } |
| | 96 | |
|
| 1289 | 97 | | while (entitiesMarkedForCleanup.Count > 0) |
| | 98 | | { |
| 494 | 99 | | IDCLEntity entity = entitiesMarkedForCleanup.Dequeue(); |
| 494 | 100 | | entity.SetParent(null); |
| 494 | 101 | | entity.Cleanup(); |
| | 102 | | } |
| | 103 | |
|
| 2068 | 104 | | foreach (var scene in scenesToRemove) |
| | 105 | | { |
| 239 | 106 | | if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id)) |
| 239 | 107 | | Object.Destroy(scene.gameObject); |
| | 108 | | } |
| 795 | 109 | | } |
| | 110 | |
|
| | 111 | | IEnumerator CleanupEntitiesCoroutine() |
| | 112 | | { |
| 7779 | 113 | | while (true) |
| | 114 | | { |
| 8445 | 115 | | float lastTime = Time.unscaledTime; |
| | 116 | |
|
| 8452 | 117 | | while (disposableComponentsMarkedForCleanup.Count > 0) |
| | 118 | | { |
| 7 | 119 | | MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue() |
| 7 | 120 | | markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId); |
| | 121 | |
|
| 7 | 122 | | if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) |
| | 123 | | { |
| 0 | 124 | | yield return null; |
| 0 | 125 | | lastTime = Time.unscaledTime; |
| | 126 | | } |
| | 127 | | } |
| | 128 | |
|
| 8445 | 129 | | 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 |
| 8447 | 133 | | while (rootEntitiesMarkedForCleanup.Count > 0) |
| | 134 | | { |
| 2 | 135 | | MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue(); |
| 2 | 136 | | markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false); |
| | 137 | |
|
| 2 | 138 | | if (!scenesToRemove.Contains(markedEntityInfo.scene)) |
| 2 | 139 | | scenesToRemove.Add(markedEntityInfo.scene); |
| | 140 | |
|
| 2 | 141 | | if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) |
| | 142 | | { |
| 0 | 143 | | yield return null; |
| 0 | 144 | | lastTime = Time.unscaledTime; |
| | 145 | | } |
| | 146 | | } |
| | 147 | |
|
| 8447 | 148 | | while (entitiesMarkedForCleanup.Count > 0) |
| | 149 | | { |
| 2 | 150 | | IDCLEntity entity = entitiesMarkedForCleanup.Dequeue(); |
| 2 | 151 | | entity.SetParent(null); |
| 2 | 152 | | entity.Cleanup(); |
| | 153 | |
|
| 2 | 154 | | if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) |
| | 155 | | { |
| 0 | 156 | | yield return null; |
| 0 | 157 | | lastTime = Time.unscaledTime; |
| | 158 | | } |
| | 159 | | } |
| | 160 | |
|
| 16894 | 161 | | foreach (var scene in scenesToRemove) |
| | 162 | | { |
| 2 | 163 | | if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id)) |
| | 164 | | { |
| 2 | 165 | | Object.Destroy(scene.gameObject); |
| | 166 | |
|
| 2 | 167 | | if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) |
| | 168 | | { |
| 0 | 169 | | yield return null; |
| 0 | 170 | | lastTime = Time.unscaledTime; |
| | 171 | | } |
| | 172 | | } |
| | 173 | | } |
| | 174 | |
|
| 8445 | 175 | | yield return null; |
| 7779 | 176 | | } |
| | 177 | | } |
| | 178 | |
|
| | 179 | | public void Dispose() |
| | 180 | | { |
| 686 | 181 | | ForceCleanup(); |
| 686 | 182 | | Stop(); |
| 686 | 183 | | } |
| | 184 | | } |
| | 185 | | } |