< Summary

Class:DCL.Controllers.SceneLifecycleHandler
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/SceneLifecycleHandler.cs
Covered lines:48
Uncovered lines:17
Coverable lines:65
Total lines:158
Line coverage:73.8% (48 of 65)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SceneLifecycleHandler(...)0%110100%
ChangeTrackingSystem(...)0%2.022083.33%
OnSceneSetData(...)0%4.184077.78%
OnResourcesStatusUpdated()0%20400%
SetInitMessagesDone()0%4.254075%
SetSceneReady()0%5.255078.57%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.ECSRuntime;
 3using DCL.Models;
 4using DCL.WorldRuntime;
 5using UnityEngine;
 6
 7namespace DCL.Controllers
 8{
 9    /// <summary>
 10    /// This handler is in charge of handling the scene lifecycle events
 11    /// and upkeep the scene lifecycle state.
 12    /// </summary>
 13    public class SceneLifecycleHandler
 14    {
 15        public static bool VERBOSE = false;
 16
 17        public enum State
 18        {
 19            NOT_READY,
 20            WAITING_FOR_INIT_MESSAGES,
 21            WAITING_FOR_COMPONENTS,
 22            READY,
 23        }
 24
 225        public int pendingResourcesCount => sceneResourcesLoadTracker.pendingResourcesCount;
 35226        public float loadingProgress => sceneResourcesLoadTracker.loadingProgress;
 19527        public bool isReady => state == State.READY;
 28
 29        State stateValue = State.NOT_READY;
 30
 31        public State state
 32        {
 3621433            get { return stateValue; }
 34            set
 35            {
 112236                stateValue = value;
 112237                OnStateRefreshed?.Invoke(owner);
 038            }
 39        }
 40
 219641        public SceneResourcesLoadTracker sceneResourcesLoadTracker { get; }
 42
 43        public event Action<ParcelScene> OnSceneReady;
 44        public event Action<ParcelScene> OnStateRefreshed;
 45
 46        private ParcelScene owner;
 47
 38948        public SceneLifecycleHandler(ParcelScene ownerScene)
 49        {
 38950            state = State.NOT_READY;
 38951            this.owner = ownerScene;
 38952            owner.OnSetData += OnSceneSetData;
 53
 38954            sceneResourcesLoadTracker = new SceneResourcesLoadTracker();
 38955            sceneResourcesLoadTracker.Track(owner.componentsManagerLegacy, Environment.i.world.state);
 38956            sceneResourcesLoadTracker.OnResourcesStatusUpdate += OnResourcesStatusUpdated;
 57
 58            // This is done while the two ECS are living together, if we detect that a component from the new ECS has in
 59            // resource for the scene, we changed the track since that means that this scene is from the new ECS.
 60            // This should disappear when the old ecs is removed from the project. This should be the default track
 38961            DataStore.i.ecs7.scenes.OnAdded += ChangeTrackingSystem;
 38962        }
 63
 64        private void ChangeTrackingSystem(IParcelScene scene)
 65        {
 466            if (scene.sceneData.sceneNumber != owner.sceneData.sceneNumber)
 067                return;
 68
 469            DataStore.i.ecs7.scenes.OnAdded -= ChangeTrackingSystem;
 70
 471            sceneResourcesLoadTracker.Dispose();
 472            sceneResourcesLoadTracker.Track(scene.sceneData.sceneNumber);
 473        }
 74
 75        private void OnSceneSetData(LoadParcelScenesMessage.UnityParcelScene data)
 76        {
 38177            state = State.WAITING_FOR_INIT_MESSAGES;
 38178            owner.RefreshLoadingState();
 79
 80#if UNITY_EDITOR
 38181            DebugConfig debugConfig = DataStore.i.debugConfig;
 82            //NOTE(Brian): Don't generate parcel blockers if debugScenes is active and is not the desired scene.
 38183            if (debugConfig.soloScene && debugConfig.soloSceneCoords != data.basePosition)
 84            {
 085                SetSceneReady();
 086                return;
 87            }
 88#endif
 89
 38190            if (owner.isTestScene)
 34891                SetSceneReady();
 38192        }
 93
 94        private void OnResourcesStatusUpdated()
 95        {
 096            if (owner.isReleased)
 097                return;
 98
 099            if (VERBOSE)
 100            {
 0101                Debug.Log($"{owner.sceneData.basePosition} Disposable objects left... {sceneResourcesLoadTracker.pending
 102            }
 103
 0104            OnStateRefreshed?.Invoke(owner);
 0105            owner.RefreshLoadingState();
 0106        }
 107
 108        public void SetInitMessagesDone()
 109        {
 3110            if (owner.isReleased)
 0111                return;
 112
 3113            if (state == State.READY)
 114            {
 1115                Debug.LogWarning($"Init messages done after ready?! {owner.sceneData.basePosition}", owner.gameObject);
 1116                return;
 117            }
 118
 2119            state = State.WAITING_FOR_COMPONENTS;
 2120            owner.RefreshLoadingState();
 121
 2122            if (sceneResourcesLoadTracker.ShouldWaitForPendingResources())
 123            {
 0124                sceneResourcesLoadTracker.OnResourcesLoaded -= SetSceneReady;
 0125                sceneResourcesLoadTracker.OnResourcesLoaded += SetSceneReady;
 126            }
 127            else
 128            {
 2129                SetSceneReady();
 130            }
 2131        }
 132
 133        private void SetSceneReady()
 134        {
 135#if UNITY_STANDALONE || UNITY_EDITOR
 350136            if (DataStore.i.common.isApplicationQuitting.Get())
 0137                return;
 138#endif
 139
 350140            if (state == State.READY)
 0141                return;
 142
 350143            if (VERBOSE)
 0144                Debug.Log($"{owner.sceneData.basePosition} Scene Ready!");
 145
 350146            state = State.READY;
 147
 350148            Environment.i.world.sceneController.SendSceneReady(owner.sceneData.sceneNumber);
 350149            owner.RefreshLoadingState();
 150
 350151            sceneResourcesLoadTracker.OnResourcesLoaded -= SetSceneReady;
 350152            sceneResourcesLoadTracker.OnResourcesStatusUpdate -= OnResourcesStatusUpdated;
 350153            sceneResourcesLoadTracker.Dispose();
 154
 350155            OnSceneReady?.Invoke(owner);
 1156        }
 157    }
 158}