< Summary

Class:DCL.Controllers.SceneLifecycleHandler
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/SceneLifecycleHandler.cs
Covered lines:29
Uncovered lines:31
Coverable lines:60
Total lines:156
Line coverage:48.3% (29 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SceneLifecycleHandler(...)0%110100%
OnAddSharedComponent(...)0%2.152066.67%
OnSceneSetData(...)0%4.184077.78%
OnDisposableReady(...)0%30500%
SetInitMessagesDone()0%14.115028.57%
SetSceneReady()0%6.25063.64%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL.Components;
 3using DCL.Models;
 4using UnityEngine;
 5
 6namespace 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
 024        public int disposableNotReadyCount => disposableNotReady.Count;
 025        public bool isReady => state == State.READY;
 26
 50327        public List<string> disposableNotReady = new List<string>();
 28        State stateValue = State.NOT_READY;
 29
 30        public State state
 31        {
 032            get { return stateValue; }
 33            set
 34            {
 147135                stateValue = value;
 147136                OnStateRefreshed?.Invoke(owner);
 037            }
 38        }
 39
 40        public event System.Action<ParcelScene> OnSceneReady;
 41        public event System.Action<ParcelScene> OnStateRefreshed;
 42
 43        private ParcelScene owner;
 44
 50345        public SceneLifecycleHandler(ParcelScene ownerScene)
 46        {
 50347            state = State.NOT_READY;
 50348            this.owner = ownerScene;
 50349            owner.OnSetData += OnSceneSetData;
 50350            owner.OnAddSharedComponent += OnAddSharedComponent;
 50351        }
 52
 53        private void OnAddSharedComponent(string id, ISharedComponent component)
 54        {
 63055            if (state != State.READY)
 56            {
 057                disposableNotReady.Add(id);
 58            }
 63059        }
 60
 61        private void OnSceneSetData(LoadParcelScenesMessage.UnityParcelScene data)
 62        {
 50063            state = State.WAITING_FOR_INIT_MESSAGES;
 50064            owner.RefreshLoadingState();
 65
 66#if UNITY_EDITOR
 50067            DebugConfig debugConfig = DataStore.i.debugConfig;
 68            //NOTE(Brian): Don't generate parcel blockers if debugScenes is active and is not the desired scene.
 50069            if (debugConfig.soloScene && debugConfig.soloSceneCoords != data.basePosition)
 70            {
 071                SetSceneReady();
 072                return;
 73            }
 74#endif
 75
 50076            if (owner.isTestScene)
 46877                SetSceneReady();
 50078        }
 79
 80        private void OnDisposableReady(ISharedComponent component)
 81        {
 082            if (owner.isReleased)
 083                return;
 84
 085            disposableNotReady.Remove(component.id);
 86
 087            if (VERBOSE)
 88            {
 089                Debug.Log($"{owner.sceneData.basePosition} Disposable objects left... {disposableNotReady.Count}");
 90            }
 91
 092            if (disposableNotReady.Count == 0)
 93            {
 094                SetSceneReady();
 95            }
 96
 097            OnStateRefreshed?.Invoke(owner);
 098            owner.RefreshLoadingState();
 099        }
 100
 101        public void SetInitMessagesDone()
 102        {
 1103            if (owner.isReleased)
 0104                return;
 105
 1106            if (state == State.READY)
 107            {
 1108                Debug.LogWarning($"Init messages done after ready?! {owner.sceneData.basePosition}", owner.gameObject);
 1109                return;
 110            }
 111
 0112            state = State.WAITING_FOR_COMPONENTS;
 0113            owner.RefreshLoadingState();
 114
 0115            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
 0121                using (var iterator = owner.disposableComponents.GetEnumerator())
 122                {
 0123                    while (iterator.MoveNext())
 124                    {
 0125                        owner.disposableComponents[iterator.Current.Value.id].CallWhenReady(OnDisposableReady);
 126                    }
 0127                }
 128            }
 129            else
 130            {
 0131                SetSceneReady();
 132            }
 0133        }
 134
 135        private void SetSceneReady()
 136        {
 137#if UNITY_STANDALONE || UNITY_EDITOR
 468138            if (DataStore.i.common.isApplicationQuitting.Get())
 0139                return;
 140#endif
 141
 468142            if (state == State.READY)
 0143                return;
 144
 468145            if (VERBOSE)
 0146                Debug.Log($"{owner.sceneData.basePosition} Scene Ready!");
 147
 468148            state = State.READY;
 149
 468150            Environment.i.world.sceneController.SendSceneReady(owner.sceneData.id);
 468151            owner.RefreshLoadingState();
 152
 468153            OnSceneReady?.Invoke(owner);
 0154        }
 155    }
 156}