| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Models; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Controllers |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// This handler is in charge of handling the scene lifecycle events |
| | 10 | | /// and upkeep the scene lifecycle state. |
| | 11 | | /// </summary> |
| | 12 | | public class SceneLifecycleHandler |
| | 13 | | { |
| | 14 | | public static bool VERBOSE = false; |
| | 15 | |
|
| | 16 | | public enum State |
| | 17 | | { |
| | 18 | | NOT_READY, |
| | 19 | | WAITING_FOR_INIT_MESSAGES, |
| | 20 | | WAITING_FOR_COMPONENTS, |
| | 21 | | READY, |
| | 22 | | } |
| | 23 | |
|
| 0 | 24 | | public int disposableNotReadyCount => disposableNotReady.Count; |
| 0 | 25 | | public bool isReady => state == State.READY; |
| | 26 | |
|
| 503 | 27 | | public List<string> disposableNotReady = new List<string>(); |
| | 28 | | State stateValue = State.NOT_READY; |
| | 29 | |
|
| | 30 | | public State state |
| | 31 | | { |
| 0 | 32 | | get { return stateValue; } |
| | 33 | | set |
| | 34 | | { |
| 1471 | 35 | | stateValue = value; |
| 1471 | 36 | | OnStateRefreshed?.Invoke(owner); |
| 0 | 37 | | } |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public event System.Action<ParcelScene> OnSceneReady; |
| | 41 | | public event System.Action<ParcelScene> OnStateRefreshed; |
| | 42 | |
|
| | 43 | | private ParcelScene owner; |
| | 44 | |
|
| 503 | 45 | | public SceneLifecycleHandler(ParcelScene ownerScene) |
| | 46 | | { |
| 503 | 47 | | state = State.NOT_READY; |
| 503 | 48 | | this.owner = ownerScene; |
| 503 | 49 | | owner.OnSetData += OnSceneSetData; |
| 503 | 50 | | owner.OnAddSharedComponent += OnAddSharedComponent; |
| 503 | 51 | | } |
| | 52 | |
|
| | 53 | | private void OnAddSharedComponent(string id, ISharedComponent component) |
| | 54 | | { |
| 630 | 55 | | if (state != State.READY) |
| | 56 | | { |
| 0 | 57 | | disposableNotReady.Add(id); |
| | 58 | | } |
| 630 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnSceneSetData(LoadParcelScenesMessage.UnityParcelScene data) |
| | 62 | | { |
| 500 | 63 | | state = State.WAITING_FOR_INIT_MESSAGES; |
| 500 | 64 | | owner.RefreshLoadingState(); |
| | 65 | |
|
| | 66 | | #if UNITY_EDITOR |
| 500 | 67 | | DebugConfig debugConfig = DataStore.i.debugConfig; |
| | 68 | | //NOTE(Brian): Don't generate parcel blockers if debugScenes is active and is not the desired scene. |
| 500 | 69 | | if (debugConfig.soloScene && debugConfig.soloSceneCoords != data.basePosition) |
| | 70 | | { |
| 0 | 71 | | SetSceneReady(); |
| 0 | 72 | | return; |
| | 73 | | } |
| | 74 | | #endif |
| | 75 | |
|
| 500 | 76 | | if (owner.isTestScene) |
| 468 | 77 | | SetSceneReady(); |
| 500 | 78 | | } |
| | 79 | |
|
| | 80 | | private void OnDisposableReady(ISharedComponent component) |
| | 81 | | { |
| 0 | 82 | | if (owner.isReleased) |
| 0 | 83 | | return; |
| | 84 | |
|
| 0 | 85 | | disposableNotReady.Remove(component.id); |
| | 86 | |
|
| 0 | 87 | | if (VERBOSE) |
| | 88 | | { |
| 0 | 89 | | Debug.Log($"{owner.sceneData.basePosition} Disposable objects left... {disposableNotReady.Count}"); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | if (disposableNotReady.Count == 0) |
| | 93 | | { |
| 0 | 94 | | SetSceneReady(); |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | OnStateRefreshed?.Invoke(owner); |
| 0 | 98 | | owner.RefreshLoadingState(); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public void SetInitMessagesDone() |
| | 102 | | { |
| 1 | 103 | | if (owner.isReleased) |
| 0 | 104 | | return; |
| | 105 | |
|
| 1 | 106 | | if (state == State.READY) |
| | 107 | | { |
| 1 | 108 | | Debug.LogWarning($"Init messages done after ready?! {owner.sceneData.basePosition}", owner.gameObject); |
| 1 | 109 | | return; |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | state = State.WAITING_FOR_COMPONENTS; |
| 0 | 113 | | owner.RefreshLoadingState(); |
| | 114 | |
|
| 0 | 115 | | if (disposableNotReadyCount > 0) |
| | 116 | | { |
| | 117 | | //NOTE(Brian): Here, we have to split the iterations. If not, we will get repeated calls of |
| | 118 | | // SetSceneReady(), as the disposableNotReady count is 1 and gets to 0 |
| | 119 | | // in each OnDisposableReady() call. |
| | 120 | |
|
| 0 | 121 | | using (var iterator = owner.disposableComponents.GetEnumerator()) |
| | 122 | | { |
| 0 | 123 | | while (iterator.MoveNext()) |
| | 124 | | { |
| 0 | 125 | | owner.disposableComponents[iterator.Current.Value.id].CallWhenReady(OnDisposableReady); |
| | 126 | | } |
| 0 | 127 | | } |
| | 128 | | } |
| | 129 | | else |
| | 130 | | { |
| 0 | 131 | | SetSceneReady(); |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | private void SetSceneReady() |
| | 136 | | { |
| | 137 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 468 | 138 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 139 | | return; |
| | 140 | | #endif |
| | 141 | |
|
| 468 | 142 | | if (state == State.READY) |
| 0 | 143 | | return; |
| | 144 | |
|
| 468 | 145 | | if (VERBOSE) |
| 0 | 146 | | Debug.Log($"{owner.sceneData.basePosition} Scene Ready!"); |
| | 147 | |
|
| 468 | 148 | | state = State.READY; |
| | 149 | |
|
| 468 | 150 | | Environment.i.world.sceneController.SendSceneReady(owner.sceneData.id); |
| 468 | 151 | | owner.RefreshLoadingState(); |
| | 152 | |
|
| 468 | 153 | | OnSceneReady?.Invoke(owner); |
| 0 | 154 | | } |
| | 155 | | } |
| | 156 | | } |