| | 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 | |
|
| 666 | 44 | | public ParcelScenesCleaner () |
| | 45 | | { |
| 666 | 46 | | removeEntitiesCoroutine = CoroutineStarter.Start(CleanupEntitiesCoroutine()); |
| 666 | 47 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; |
| 666 | 48 | | } |
| | 49 | |
|
| | 50 | | public void Initialize() |
| | 51 | | { |
| 666 | 52 | | Environment.i.platform.memoryManager.OnCriticalMemory += CleanMarkedEntities; |
| 666 | 53 | | } |
| | 54 | |
|
| | 55 | | private void OnRendererStateChange(bool isEnable, bool prevState) |
| | 56 | | { |
| 104 | 57 | | if (!isEnable) |
| | 58 | | { |
| 1 | 59 | | CleanMarkedEntities(); |
| 1 | 60 | | Resources.UnloadUnusedAssets(); |
| | 61 | | } |
| 104 | 62 | | } |
| | 63 | |
|
| | 64 | | public void MarkForCleanup(IDCLEntity entity) |
| | 65 | | { |
| 493 | 66 | | if (entity.markedForCleanup) |
| 0 | 67 | | return; |
| | 68 | |
|
| 493 | 69 | | entity.markedForCleanup = true; |
| | 70 | |
|
| 493 | 71 | | if (entity.gameObject != null) |
| 489 | 72 | | entity.gameObject.SetActive(false); |
| | 73 | |
|
| | 74 | | #if UNITY_EDITOR |
| 493 | 75 | | if (entity.gameObject != null) |
| 489 | 76 | | entity.gameObject.name += "-MARKED-FOR-CLEANUP"; |
| | 77 | | #endif |
| | 78 | |
|
| 493 | 79 | | entitiesMarkedForCleanup.Enqueue(entity); |
| 493 | 80 | | } |
| | 81 | |
|
| | 82 | | // When removing all entities from a scene, we need to separate the root entities, as stated in ParcelScene, |
| | 83 | | // to avoid traversing a lot of child entities in the same frame and other problems |
| 970 | 84 | | public void MarkRootEntityForCleanup(IParcelScene scene, IDCLEntity entity) { rootEntitiesMarkedForCleanup.Enque |
| | 85 | |
|
| 1032 | 86 | | public void MarkDisposableComponentForCleanup(IParcelScene scene, string componentId) { disposableComponentsMark |
| | 87 | |
|
| | 88 | | public void CleanMarkedEntities() |
| | 89 | | { |
| 692 | 90 | | CleanMarkedEntitiesAsync(true).MoveNext(); |
| 692 | 91 | | } |
| | 92 | |
|
| | 93 | | public IEnumerator CleanMarkedEntitiesAsync(bool immediate = false) |
| | 94 | | { |
| 6378 | 95 | | float lastTime = Time.unscaledTime; |
| | 96 | |
|
| 6894 | 97 | | while (disposableComponentsMarkedForCleanup.Count > 0) |
| | 98 | | { |
| 516 | 99 | | MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue(); |
| 516 | 100 | | markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId); |
| | 101 | |
|
| 516 | 102 | | if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET && !immediate) |
| | 103 | | { |
| 0 | 104 | | yield return null; |
| 0 | 105 | | lastTime = Time.unscaledTime; |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| 6378 | 109 | | HashSet<ParcelScene> scenesToRemove = new HashSet<ParcelScene>(); |
| | 110 | |
|
| | 111 | | // If we have root entities queued for removal, we call Parcel Scene's RemoveEntity() |
| | 112 | | // so that the child entities end up recursively in the entitiesMarkedForCleanup queue |
| 6863 | 113 | | while (rootEntitiesMarkedForCleanup.Count > 0) |
| | 114 | | { |
| 485 | 115 | | MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue(); |
| 485 | 116 | | markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false); |
| | 117 | |
|
| 485 | 118 | | if (!scenesToRemove.Contains(markedEntityInfo.scene)) |
| 239 | 119 | | scenesToRemove.Add(markedEntityInfo.scene); |
| | 120 | |
|
| 485 | 121 | | if (!immediate && DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) |
| | 122 | | { |
| 0 | 123 | | yield return null; |
| 0 | 124 | | lastTime = Time.unscaledTime; |
| | 125 | | } |
| | 126 | | } |
| | 127 | |
|
| 6871 | 128 | | while (entitiesMarkedForCleanup.Count > 0) |
| | 129 | | { |
| 493 | 130 | | IDCLEntity entity = entitiesMarkedForCleanup.Dequeue(); |
| 493 | 131 | | entity.SetParent(null); |
| 493 | 132 | | entity.Cleanup(); |
| | 133 | |
|
| 493 | 134 | | if (!immediate && DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) |
| | 135 | | { |
| 0 | 136 | | yield return null; |
| 0 | 137 | | lastTime = Time.unscaledTime; |
| | 138 | | } |
| | 139 | | } |
| | 140 | |
|
| 13234 | 141 | | foreach (var scene in scenesToRemove) |
| | 142 | | { |
| 239 | 143 | | if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id)) |
| 239 | 144 | | Object.Destroy(scene.gameObject); |
| | 145 | |
|
| 239 | 146 | | if (!immediate && DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) |
| | 147 | | { |
| 0 | 148 | | yield return null; |
| 0 | 149 | | lastTime = Time.unscaledTime; |
| | 150 | | } |
| | 151 | | } |
| 6378 | 152 | | } |
| | 153 | |
|
| | 154 | | IEnumerator CleanupEntitiesCoroutine() |
| | 155 | | { |
| 5020 | 156 | | while (true) |
| | 157 | | { |
| 5686 | 158 | | yield return CleanMarkedEntitiesAsync(); |
| 5473 | 159 | | yield return null; |
| | 160 | | } |
| | 161 | | } |
| | 162 | |
|
| | 163 | | public void Dispose() |
| | 164 | | { |
| 687 | 165 | | CleanMarkedEntities(); |
| | 166 | |
|
| 687 | 167 | | if (removeEntitiesCoroutine != null) |
| 687 | 168 | | CoroutineStarter.Stop(removeEntitiesCoroutine); |
| | 169 | |
|
| 687 | 170 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange; |
| 687 | 171 | | Environment.i.platform.memoryManager.OnCriticalMemory -= CleanMarkedEntities; |
| 687 | 172 | | } |
| | 173 | | } |
| | 174 | | } |