< Summary

Class:DCL.LoadingFeedbackController
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/LoadingFeedbackController.cs
Covered lines:28
Uncovered lines:47
Coverable lines:75
Total lines:175
Line coverage:37.3% (28 of 75)
Covered branches:0
Total branches:0

Metrics

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

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.Controllers;
 3using DCL.Interface;
 4using System.Collections.Generic;
 5using System.Linq;
 6using UnityEngine;
 7using UnityGLTF;
 8
 9namespace DCL
 10{
 11    /// <summary>
 12    /// This class recopiles all the needed information to be sent to the kernel and be able to show the feedback along 
 13    /// </summary>
 14    public class LoadingFeedbackController
 15    {
 16        private class SceneLoadingStatus
 17        {
 18            public int sceneId;
 19            public int componentsLoading;
 20        }
 21
 22        private List<SceneLoadingStatus> loadedScenes;
 23
 24        private int currentComponentsLoading = 0;
 25        private int loadingComponentsPercentage = 0;
 26        private int maxLoadingComponentsRef = 0;
 27        private int maxLoadingCalculatedPercentage = 0;
 28
 29        private int totalActiveDownloads = 0;
 30        private int downloadingAssetsPercentage = 0;
 31        private int maxDownloadingAssetsRef = 0;
 32        private int maxDownloadingCalculatedPercentage = 0;
 33
 65234        public LoadingFeedbackController ()
 35        {
 65236            loadedScenes = new List<SceneLoadingStatus>();
 37
 65238            Environment.i.world.sceneController.OnNewSceneAdded += SceneController_OnNewSceneAdded;
 65239            GLTFComponent.OnDownloadingProgressUpdate += GLTFComponent_OnDownloadingProgressUpdate;
 65240            AssetPromise_AB.OnDownloadingProgressUpdate += AssetPromise_AB_OnDownloadingProgressUpdate;
 65241            CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 65242        }
 43
 44        public void Dispose()
 45        {
 65246            Environment.i.world.sceneController.OnNewSceneAdded -= SceneController_OnNewSceneAdded;
 65247            GLTFComponent.OnDownloadingProgressUpdate -= GLTFComponent_OnDownloadingProgressUpdate;
 65248            AssetPromise_AB.OnDownloadingProgressUpdate -= AssetPromise_AB_OnDownloadingProgressUpdate;
 65249            CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 65250        }
 51
 52        private void SceneController_OnNewSceneAdded(IParcelScene scene)
 53        {
 2754            var parcelScene = scene as ParcelScene;
 55
 2756            if ( parcelScene == null )
 057                return;
 58
 2759            parcelScene.sceneLifecycleHandler.OnStateRefreshed += Scene_OnStateRefreshed;
 2760        }
 61
 62        private void Scene_OnStateRefreshed(ParcelScene scene)
 63        {
 064            SceneLoadingStatus refreshedScene = new SceneLoadingStatus
 65            {
 66                sceneId = scene.GetInstanceID(),
 67                componentsLoading = scene.sceneLifecycleHandler.disposableNotReadyCount
 68            };
 69
 070            switch (scene.sceneLifecycleHandler.state)
 71            {
 72                case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS:
 073                    AddOrUpdateLoadedScene(refreshedScene);
 074                    break;
 75                case SceneLifecycleHandler.State.READY:
 076                    scene.sceneLifecycleHandler.OnStateRefreshed -= Scene_OnStateRefreshed;
 77                    break;
 78            }
 79
 080            RefreshFeedbackMessage();
 081        }
 82
 83        private void AddOrUpdateLoadedScene(SceneLoadingStatus scene)
 84        {
 085            SceneLoadingStatus existingScene = loadedScenes.FirstOrDefault(x => x.sceneId == scene.sceneId);
 086            if (existingScene == null)
 087                loadedScenes.Add(scene);
 88            else
 89            {
 090                existingScene.componentsLoading = scene.componentsLoading;
 91            }
 092        }
 93
 29694        private void GLTFComponent_OnDownloadingProgressUpdate() { RefreshFeedbackMessage(); }
 95
 10896        private void AssetPromise_AB_OnDownloadingProgressUpdate() { RefreshFeedbackMessage(); }
 97
 98        private void RefreshFeedbackMessage()
 99        {
 202100            if (!DataStore.i.HUDs.loadingHUD.visible.Get())
 202101                return;
 102
 0103            string loadingText = string.Empty;
 0104            string secondLoadingText = string.Empty;
 0105            DCL.Interface.WebInterface.LoadingFeedbackMessage messageToSend = new WebInterface.LoadingFeedbackMessage();
 0106            messageToSend.loadPercentage = 0;
 107
 0108            currentComponentsLoading = loadedScenes.Sum(x => x.componentsLoading);
 0109            if (currentComponentsLoading > 0)
 110            {
 0111                loadingComponentsPercentage = GetLoadingComponentsPercentage(currentComponentsLoading);
 0112                messageToSend.loadPercentage = loadingComponentsPercentage;
 0113                DataStore.i.HUDs.loadingHUD.percentage.Set(loadingComponentsPercentage);
 0114                loadingText = string.Format("Loading scenes {0}%", loadingComponentsPercentage);
 115            }
 116
 0117            totalActiveDownloads = AssetPromiseKeeper_GLTF.i.waitingPromisesCount + AssetPromiseKeeper_AB.i.waitingPromi
 0118            if (totalActiveDownloads > 0)
 119            {
 0120                downloadingAssetsPercentage = GetDownloadingAssetsPercentage(totalActiveDownloads);
 0121                secondLoadingText = string.Format("Downloading images, 3D models, and sounds {0}%", downloadingAssetsPer
 122
 0123                if (!string.IsNullOrEmpty(loadingText))
 124                {
 0125                    loadingText += "\n";
 126                }
 127
 0128                loadingText += secondLoadingText;
 129            }
 130
 0131            if (!string.IsNullOrEmpty(loadingText))
 132            {
 0133                DataStore.i.HUDs.loadingHUD.message.Set(loadingText);
 0134                messageToSend.message = loadingText;
 0135                WebInterface.ScenesLoadingFeedback(messageToSend);
 136            }
 0137        }
 138
 139        private int GetLoadingComponentsPercentage(int currentComponentsLoading)
 140        {
 0141            if (currentComponentsLoading > maxLoadingComponentsRef)
 0142                maxLoadingComponentsRef = currentComponentsLoading;
 143
 0144            int result = Mathf.FloorToInt(100f - (currentComponentsLoading * 100f / maxLoadingComponentsRef));
 0145            if (result > maxLoadingCalculatedPercentage)
 0146                maxLoadingCalculatedPercentage = result;
 147
 0148            return maxLoadingCalculatedPercentage;
 149        }
 150
 151        private int GetDownloadingAssetsPercentage(int totalActiveDownloads)
 152        {
 0153            if (totalActiveDownloads > maxDownloadingAssetsRef)
 0154                maxDownloadingAssetsRef = totalActiveDownloads;
 155
 0156            int result = Mathf.FloorToInt(100f - (totalActiveDownloads * 100f / maxDownloadingAssetsRef));
 0157            if (result > maxDownloadingCalculatedPercentage)
 0158                maxDownloadingCalculatedPercentage = result;
 159
 0160            return maxDownloadingCalculatedPercentage;
 161        }
 162
 163        private void RendererState_OnChange(bool current, bool previous)
 164        {
 2165            if (!current)
 1166                return;
 167
 1168            loadedScenes.Clear();
 1169            maxLoadingComponentsRef = 0;
 1170            maxLoadingCalculatedPercentage = 0;
 1171            maxDownloadingAssetsRef = 0;
 1172            maxDownloadingCalculatedPercentage = 0;
 1173        }
 174    }
 175}