< 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:47
Coverable lines:77
Total lines:183
Line coverage:38.9% (30 of 77)
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%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.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
 68434        public LoadingFeedbackController()
 35        {
 68436            loadedScenes = new List<SceneLoadingStatus>();
 37
 68438            Environment.i.world.sceneController.OnNewSceneAdded += SceneController_OnNewSceneAdded;
 68439            GLTFComponent.OnDownloadingProgressUpdate += GLTFComponent_OnDownloadingProgressUpdate;
 68440            AssetPromise_AB.OnDownloadingProgressUpdate += AssetPromise_AB_OnDownloadingProgressUpdate;
 68441            CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 68442        }
 43
 44        public void Dispose()
 45        {
 68446            Environment.i.world.sceneController.OnNewSceneAdded -= SceneController_OnNewSceneAdded;
 68447            GLTFComponent.OnDownloadingProgressUpdate -= GLTFComponent_OnDownloadingProgressUpdate;
 68448            AssetPromise_AB.OnDownloadingProgressUpdate -= AssetPromise_AB_OnDownloadingProgressUpdate;
 68449            CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 68450        }
 51
 52        private void SceneController_OnNewSceneAdded(IParcelScene scene)
 53        {
 3154            var parcelScene = scene as ParcelScene;
 55
 3156            if (parcelScene == null)
 057                return;
 58
 3159            parcelScene.sceneLifecycleHandler.OnStateRefreshed += Scene_OnStateRefreshed;
 3160        }
 61
 62        private void Scene_OnStateRefreshed(ParcelScene scene)
 63        {
 064            SceneLoadingStatus refreshedScene = new SceneLoadingStatus
 65            {
 66                sceneId = scene.GetInstanceID(),
 67                componentsLoading = scene.sceneLifecycleHandler.sceneResourcesLoadTracker.pendingResourcesCount
 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
 94        private void GLTFComponent_OnDownloadingProgressUpdate()
 95        {
 16096            RefreshFeedbackMessage();
 16097        }
 98
 99        private void AssetPromise_AB_OnDownloadingProgressUpdate()
 100        {
 60101            RefreshFeedbackMessage();
 60102        }
 103
 104        private void RefreshFeedbackMessage()
 105        {
 220106            if (!DataStore.i.HUDs.loadingHUD.fadeIn.Get() && !DataStore.i.HUDs.loadingHUD.visible.Get())
 220107                return;
 108
 0109            string loadingText = string.Empty;
 0110            string secondLoadingText = string.Empty;
 0111            DCL.Interface.WebInterface.LoadingFeedbackMessage messageToSend = new WebInterface.LoadingFeedbackMessage();
 0112            messageToSend.loadPercentage = 0;
 113
 0114            currentComponentsLoading = loadedScenes.Sum(x => x.componentsLoading);
 0115            if (currentComponentsLoading > 0)
 116            {
 0117                loadingComponentsPercentage = GetLoadingComponentsPercentage(currentComponentsLoading);
 0118                messageToSend.loadPercentage = loadingComponentsPercentage;
 0119                DataStore.i.HUDs.loadingHUD.percentage.Set(loadingComponentsPercentage);
 0120                loadingText = string.Format("Loading scenes {0}%", loadingComponentsPercentage);
 121            }
 122
 0123            totalActiveDownloads = AssetPromiseKeeper_GLTF.i.waitingPromisesCount +
 124                                   AssetPromiseKeeper_AB.i.waitingPromisesCount;
 0125            if (totalActiveDownloads > 0)
 126            {
 0127                downloadingAssetsPercentage = GetDownloadingAssetsPercentage(totalActiveDownloads);
 0128                secondLoadingText = string.Format("Downloading images, 3D models, and sounds {0}%",
 129                    downloadingAssetsPercentage);
 130
 0131                if (!string.IsNullOrEmpty(loadingText))
 132                {
 0133                    loadingText += "\n";
 134                }
 135
 0136                loadingText += secondLoadingText;
 137            }
 138
 0139            if (!string.IsNullOrEmpty(loadingText))
 140            {
 0141                DataStore.i.HUDs.loadingHUD.message.Set(loadingText);
 0142                messageToSend.message = loadingText;
 0143                WebInterface.ScenesLoadingFeedback(messageToSend);
 144            }
 0145        }
 146
 147        private int GetLoadingComponentsPercentage(int currentComponentsLoading)
 148        {
 0149            if (currentComponentsLoading > maxLoadingComponentsRef)
 0150                maxLoadingComponentsRef = currentComponentsLoading;
 151
 0152            int result = Mathf.FloorToInt(100f - (currentComponentsLoading * 100f / maxLoadingComponentsRef));
 0153            if (result > maxLoadingCalculatedPercentage)
 0154                maxLoadingCalculatedPercentage = result;
 155
 0156            return maxLoadingCalculatedPercentage;
 157        }
 158
 159        private int GetDownloadingAssetsPercentage(int totalActiveDownloads)
 160        {
 0161            if (totalActiveDownloads > maxDownloadingAssetsRef)
 0162                maxDownloadingAssetsRef = totalActiveDownloads;
 163
 0164            int result = Mathf.FloorToInt(100f - (totalActiveDownloads * 100f / maxDownloadingAssetsRef));
 0165            if (result > maxDownloadingCalculatedPercentage)
 0166                maxDownloadingCalculatedPercentage = result;
 167
 0168            return maxDownloadingCalculatedPercentage;
 169        }
 170
 171        private void RendererState_OnChange(bool current, bool previous)
 172        {
 575173            if (!current)
 574174                return;
 175
 1176            loadedScenes.Clear();
 1177            maxLoadingComponentsRef = 0;
 1178            maxLoadingCalculatedPercentage = 0;
 1179            maxDownloadingAssetsRef = 0;
 1180            maxDownloadingCalculatedPercentage = 0;
 1181        }
 182    }
 183}