< 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:40
Uncovered lines:26
Coverable lines:66
Total lines:158
Line coverage:60.6% (40 of 66)
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%9.314030.77%
SetSceneReady()0%5.585071.43%

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;
 49726        public float loadingProgress => sceneResourcesLoadTracker.loadingProgress;
 027        public bool isReady => state == State.READY;
 28
 29        State stateValue = State.NOT_READY;
 30
 31        public State state
 32        {
 033            get { return stateValue; }
 34            set
 35            {
 156336                stateValue = value;
 156337                OnStateRefreshed?.Invoke(owner);
 038            }
 39        }
 40
 107441        public SceneResourcesLoadTracker sceneResourcesLoadTracker { get; }
 42
 43        public event Action<ParcelScene> OnSceneReady;
 44        public event Action<ParcelScene> OnStateRefreshed;
 45
 46        private ParcelScene owner;
 47
 53748        public SceneLifecycleHandler(ParcelScene ownerScene)
 49        {
 53750            state = State.NOT_READY;
 53751            this.owner = ownerScene;
 53752            owner.OnSetData += OnSceneSetData;
 53
 53754            sceneResourcesLoadTracker = new SceneResourcesLoadTracker();
 53755            sceneResourcesLoadTracker.Track(owner.componentsManagerLegacy, Environment.i.world.state);
 53756            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
 53761            DataStore.i.ecs7.scenes.OnAdded += ChangeTrackingSystem;
 53762        }
 63
 64        private void ChangeTrackingSystem(IParcelScene scene)
 65        {
 266            if (scene.sceneData.id != owner.sceneData.id)
 067                return;
 68
 269            DataStore.i.ecs7.scenes.OnAdded -= ChangeTrackingSystem;
 70
 271            sceneResourcesLoadTracker.Dispose();
 272            sceneResourcesLoadTracker.Track(scene.sceneData.id);
 273        }
 74
 75        private void OnSceneSetData(LoadParcelScenesMessage.UnityParcelScene data)
 76        {
 52977            state = State.WAITING_FOR_INIT_MESSAGES;
 52978            owner.RefreshLoadingState();
 79
 80#if UNITY_EDITOR
 52981            DebugConfig debugConfig = DataStore.i.debugConfig;
 82            //NOTE(Brian): Don't generate parcel blockers if debugScenes is active and is not the desired scene.
 52983            if (debugConfig.soloScene && debugConfig.soloSceneCoords != data.basePosition)
 84            {
 085                SetSceneReady();
 086                return;
 87            }
 88#endif
 89
 52990            if (owner.isTestScene)
 49791                SetSceneReady();
 52992        }
 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        {
 1110            if (owner.isReleased)
 0111                return;
 112
 1113            if (state == State.READY)
 114            {
 1115                Debug.LogWarning($"Init messages done after ready?! {owner.sceneData.basePosition}", owner.gameObject);
 1116                return;
 117            }
 118
 0119            state = State.WAITING_FOR_COMPONENTS;
 0120            owner.RefreshLoadingState();
 121
 0122            if (sceneResourcesLoadTracker.ShouldWaitForPendingResources())
 123            {
 0124                sceneResourcesLoadTracker.OnResourcesLoaded -= SetSceneReady;
 0125                sceneResourcesLoadTracker.OnResourcesLoaded += SetSceneReady;
 0126            }
 127            else
 128            {
 0129                SetSceneReady();
 130            }
 0131        }
 132
 133        private void SetSceneReady()
 134        {
 135#if UNITY_STANDALONE || UNITY_EDITOR
 497136            if (DataStore.i.common.isApplicationQuitting.Get())
 0137                return;
 138#endif
 139
 497140            if (state == State.READY)
 0141                return;
 142
 497143            if (VERBOSE)
 0144                Debug.Log($"{owner.sceneData.basePosition} Scene Ready!");
 145
 497146            state = State.READY;
 147
 497148            Environment.i.world.sceneController.SendSceneReady(owner.sceneData.id);
 497149            owner.RefreshLoadingState();
 150
 497151            sceneResourcesLoadTracker.OnResourcesLoaded -= SetSceneReady;
 497152            sceneResourcesLoadTracker.OnResourcesStatusUpdate -= OnResourcesStatusUpdated;
 497153            sceneResourcesLoadTracker.Dispose();
 154
 497155            OnSceneReady?.Invoke(owner);
 0156        }
 157    }
 158}