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