< Summary

Class:DCL.LoadingFeedbackController
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/LoadingFeedbackController.cs
Covered lines:30
Uncovered lines:53
Coverable lines:83
Total lines:202
Line coverage:36.1% (30 of 83)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadingFeedbackController()0%110100%
FeatureFlagsSet(...)0%330100%
Dispose()0%220100%
SceneController_OnNewSceneAdded(...)0%6200%
Scene_OnStateRefreshed(...)0%12300%
AddOrUpdateLoadedScene(...)0%6200%
GLTFComponent_OnDownloadingProgressUpdate()0%110100%
AssetPromise_AB_OnDownloadingProgressUpdate()0%2100%
RefreshFeedbackMessage()0%57.3808.33%
GetLoadingComponentsPercentage(...)0%12300%
GetDownloadingAssetsPercentage(...)0%12300%
RendererState_OnChange(...)0%220100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.Controllers;
 3using DCL.Interface;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8using UnityGLTF;
 9
 10namespace DCL
 11{
 12    /// <summary>
 13    /// This class recopiles all the needed information to be sent to the kernel and be able to show the feedback along 
 14    /// </summary>
 15    public class LoadingFeedbackController
 16    {
 17        private readonly DataStoreRef<DataStore_LoadingScreen> dataStoreLoadingScreen;
 18        private  bool isDecoupledLoadingScreenEnabled;
 19
 20        private class SceneLoadingStatus
 21        {
 22            public int sceneInstanceId;
 23            public int componentsLoading;
 24        }
 25
 26        private List<SceneLoadingStatus> loadedScenes;
 27
 28        private int currentComponentsLoading = 0;
 29        private int loadingComponentsPercentage = 0;
 30        private int maxLoadingComponentsRef = 0;
 31        private int maxLoadingCalculatedPercentage = 0;
 32
 33        private int totalActiveDownloads = 0;
 34        private int downloadingAssetsPercentage = 0;
 35        private int maxDownloadingAssetsRef = 0;
 36        private int maxDownloadingCalculatedPercentage = 0;
 37
 51238        public LoadingFeedbackController()
 39        {
 51240            DataStore.i.featureFlags.flags.OnChange += FeatureFlagsSet;
 51241        }
 42
 43        private void FeatureFlagsSet(FeatureFlag current, FeatureFlag _)
 44        {
 5945            DataStore.i.featureFlags.flags.OnChange -= FeatureFlagsSet;
 46
 5947            isDecoupledLoadingScreenEnabled = current.IsFeatureEnabled(DataStore.i.featureFlags.DECOUPLED_LOADING_SCREEN
 5948            if (!isDecoupledLoadingScreenEnabled)
 49            {
 5950                loadedScenes = new List<SceneLoadingStatus>();
 51
 52                //Add this null check is quite bad. But we are deleting this class soon, so it does not generate any tec
 7853                if(Environment.i.world.sceneController != null) Environment.i.world.sceneController.OnNewSceneAdded += S
 5954                GLTFComponent.OnDownloadingProgressUpdate += GLTFComponent_OnDownloadingProgressUpdate;
 5955                AssetPromise_AB.OnDownloadingProgressUpdate += AssetPromise_AB_OnDownloadingProgressUpdate;
 5956                CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 57            }
 5958        }
 59
 60        public void Dispose()
 61        {
 51262            if (!isDecoupledLoadingScreenEnabled)
 63            {
 51264                Environment.i.world.sceneController.OnNewSceneAdded -= SceneController_OnNewSceneAdded;
 51265                GLTFComponent.OnDownloadingProgressUpdate -= GLTFComponent_OnDownloadingProgressUpdate;
 51266                AssetPromise_AB.OnDownloadingProgressUpdate -= AssetPromise_AB_OnDownloadingProgressUpdate;
 51267                CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 68            }
 51269        }
 70
 71        private void SceneController_OnNewSceneAdded(IParcelScene scene)
 72        {
 073            var parcelScene = scene as ParcelScene;
 74
 075            if (parcelScene == null)
 076                return;
 77
 078            parcelScene.sceneLifecycleHandler.OnStateRefreshed += Scene_OnStateRefreshed;
 079        }
 80
 81        private void Scene_OnStateRefreshed(ParcelScene scene)
 82        {
 083            SceneLoadingStatus refreshedScene = new SceneLoadingStatus
 84            {
 85                sceneInstanceId = scene.GetInstanceID(),
 86                componentsLoading = scene.sceneLifecycleHandler.sceneResourcesLoadTracker.pendingResourcesCount
 87            };
 88
 089            switch (scene.sceneLifecycleHandler.state)
 90            {
 91                case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS:
 092                    AddOrUpdateLoadedScene(refreshedScene);
 093                    break;
 94                case SceneLifecycleHandler.State.READY:
 095                    scene.sceneLifecycleHandler.OnStateRefreshed -= Scene_OnStateRefreshed;
 96                    break;
 97            }
 98
 099            RefreshFeedbackMessage();
 0100        }
 101
 102        private void AddOrUpdateLoadedScene(SceneLoadingStatus scene)
 103        {
 0104            SceneLoadingStatus existingScene = loadedScenes.FirstOrDefault(x => x.sceneInstanceId == scene.sceneInstance
 0105            if (existingScene == null)
 0106                loadedScenes.Add(scene);
 107            else
 108            {
 0109                existingScene.componentsLoading = scene.componentsLoading;
 110            }
 0111        }
 112
 113        private void GLTFComponent_OnDownloadingProgressUpdate()
 114        {
 6632115            RefreshFeedbackMessage();
 6632116        }
 117
 118        private void AssetPromise_AB_OnDownloadingProgressUpdate()
 119        {
 0120            RefreshFeedbackMessage();
 0121        }
 122
 123        private void RefreshFeedbackMessage()
 124        {
 6632125            if (!dataStoreLoadingScreen.Ref.loadingHUD.fadeIn.Get() && !dataStoreLoadingScreen.Ref.loadingHUD.visible.Ge
 6632126                return;
 127
 0128            string loadingText = string.Empty;
 0129            string secondLoadingText = string.Empty;
 0130            DCL.Interface.WebInterface.LoadingFeedbackMessage messageToSend = new WebInterface.LoadingFeedbackMessage();
 0131            messageToSend.loadPercentage = 0;
 132
 0133            currentComponentsLoading = loadedScenes.Sum(x => x.componentsLoading);
 0134            if (currentComponentsLoading > 0)
 135            {
 0136                loadingComponentsPercentage = GetLoadingComponentsPercentage(currentComponentsLoading);
 0137                messageToSend.loadPercentage = loadingComponentsPercentage;
 0138                dataStoreLoadingScreen.Ref.loadingHUD.percentage.Set(loadingComponentsPercentage);
 0139                loadingText = string.Format("Loading scenes {0}%", loadingComponentsPercentage);
 140            }
 141
 0142            totalActiveDownloads = AssetPromiseKeeper_GLTF.i.waitingPromisesCount +
 143                                   AssetPromiseKeeper_AB.i.waitingPromisesCount;
 0144            if (totalActiveDownloads > 0)
 145            {
 0146                downloadingAssetsPercentage = GetDownloadingAssetsPercentage(totalActiveDownloads);
 0147                secondLoadingText = string.Format("Downloading images, 3D models, and sounds {0}%",
 148                    downloadingAssetsPercentage);
 149
 0150                if (!string.IsNullOrEmpty(loadingText))
 151                {
 0152                    loadingText += "\n";
 153                }
 154
 0155                loadingText += secondLoadingText;
 156            }
 157
 0158            if (!string.IsNullOrEmpty(loadingText))
 159            {
 0160                dataStoreLoadingScreen.Ref.loadingHUD.message.Set(loadingText);
 0161                messageToSend.message = loadingText;
 0162                WebInterface.ScenesLoadingFeedback(messageToSend);
 163            }
 0164        }
 165
 166        private int GetLoadingComponentsPercentage(int currentComponentsLoading)
 167        {
 0168            if (currentComponentsLoading > maxLoadingComponentsRef)
 0169                maxLoadingComponentsRef = currentComponentsLoading;
 170
 0171            int result = Mathf.FloorToInt(100f - (currentComponentsLoading * 100f / maxLoadingComponentsRef));
 0172            if (result > maxLoadingCalculatedPercentage)
 0173                maxLoadingCalculatedPercentage = result;
 174
 0175            return maxLoadingCalculatedPercentage;
 176        }
 177
 178        private int GetDownloadingAssetsPercentage(int totalActiveDownloads)
 179        {
 0180            if (totalActiveDownloads > maxDownloadingAssetsRef)
 0181                maxDownloadingAssetsRef = totalActiveDownloads;
 182
 0183            int result = Mathf.FloorToInt(100f - (totalActiveDownloads * 100f / maxDownloadingAssetsRef));
 0184            if (result > maxDownloadingCalculatedPercentage)
 0185                maxDownloadingCalculatedPercentage = result;
 186
 0187            return maxDownloadingCalculatedPercentage;
 188        }
 189
 190        private void RendererState_OnChange(bool current, bool previous)
 191        {
 88192            if (!current)
 48193                return;
 194
 40195            loadedScenes.Clear();
 40196            maxLoadingComponentsRef = 0;
 40197            maxLoadingCalculatedPercentage = 0;
 40198            maxDownloadingAssetsRef = 0;
 40199            maxDownloadingCalculatedPercentage = 0;
 40200        }
 201    }
 202}