< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%110100%
OnDestroy()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
 9/// <summary>
 10/// This class recopiles all the needed information to be sent to the kernel and be able to show the feedback along the 
 11/// </summary>
 12public class LoadingFeedbackController : MonoBehaviour
 13{
 14    private class SceneLoadingStatus
 15    {
 16        public int sceneId;
 17        public int componentsLoading;
 18    }
 19
 20    private List<SceneLoadingStatus> loadedScenes;
 21
 22    private int currentComponentsLoading = 0;
 23    private int loadingComponentsPercentage = 0;
 24    private int maxLoadingComponentsRef = 0;
 25    private int maxLoadingCalculatedPercentage = 0;
 26
 27    private int totalActiveDownloads = 0;
 28    private int downloadingAssetsPercentage = 0;
 29    private int maxDownloadingAssetsRef = 0;
 30    private int maxDownloadingCalculatedPercentage = 0;
 31
 32    private void Start()
 33    {
 12334        loadedScenes = new List<SceneLoadingStatus>();
 35
 12336        Environment.i.world.sceneController.OnNewSceneAdded += SceneController_OnNewSceneAdded;
 12337        GLTFComponent.OnDownloadingProgressUpdate += GLTFComponent_OnDownloadingProgressUpdate;
 12338        AssetPromise_AB.OnDownloadingProgressUpdate += AssetPromise_AB_OnDownloadingProgressUpdate;
 12339        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 12340    }
 41
 42    private void OnDestroy()
 43    {
 12244        Environment.i.world.sceneController.OnNewSceneAdded -= SceneController_OnNewSceneAdded;
 12245        GLTFComponent.OnDownloadingProgressUpdate -= GLTFComponent_OnDownloadingProgressUpdate;
 12246        AssetPromise_AB.OnDownloadingProgressUpdate -= AssetPromise_AB_OnDownloadingProgressUpdate;
 12247        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 12248    }
 49
 50    private void SceneController_OnNewSceneAdded(IParcelScene scene)
 51    {
 2252        var parcelScene = scene as ParcelScene;
 53
 2254        if ( parcelScene == null )
 055            return;
 56
 2257        parcelScene.sceneLifecycleHandler.OnStateRefreshed += Scene_OnStateRefreshed;
 2258    }
 59
 60    private void Scene_OnStateRefreshed(ParcelScene scene)
 61    {
 062        SceneLoadingStatus refreshedScene = new SceneLoadingStatus
 63        {
 64            sceneId = scene.GetInstanceID(),
 65            componentsLoading = scene.sceneLifecycleHandler.disposableNotReadyCount
 66        };
 67
 068        switch (scene.sceneLifecycleHandler.state)
 69        {
 70            case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS:
 071                AddOrUpdateLoadedScene(refreshedScene);
 072                break;
 73            case SceneLifecycleHandler.State.READY:
 074                scene.sceneLifecycleHandler.OnStateRefreshed -= Scene_OnStateRefreshed;
 75                break;
 76        }
 77
 078        RefreshFeedbackMessage();
 079    }
 80
 81    private void AddOrUpdateLoadedScene(SceneLoadingStatus scene)
 82    {
 083        SceneLoadingStatus existingScene = loadedScenes.FirstOrDefault(x => x.sceneId == scene.sceneId);
 084        if (existingScene == null)
 085            loadedScenes.Add(scene);
 86        else
 87        {
 088            existingScene.componentsLoading = scene.componentsLoading;
 89        }
 090    }
 91
 46092    private void GLTFComponent_OnDownloadingProgressUpdate() { RefreshFeedbackMessage(); }
 93
 7694    private void AssetPromise_AB_OnDownloadingProgressUpdate() { RefreshFeedbackMessage(); }
 95
 96    private void RefreshFeedbackMessage()
 97    {
 26898        if (!DataStore.i.HUDs.loadingHUD.visible.Get())
 26899            return;
 100
 0101        string loadingText = string.Empty;
 0102        string secondLoadingText = string.Empty;
 0103        DCL.Interface.WebInterface.LoadingFeedbackMessage messageToSend = new WebInterface.LoadingFeedbackMessage();
 0104        messageToSend.loadPercentage = 0;
 105
 0106        currentComponentsLoading = loadedScenes.Sum(x => x.componentsLoading);
 0107        if (currentComponentsLoading > 0)
 108        {
 0109            loadingComponentsPercentage = GetLoadingComponentsPercentage(currentComponentsLoading);
 0110            messageToSend.loadPercentage = loadingComponentsPercentage;
 0111            DataStore.i.HUDs.loadingHUD.percentage.Set(loadingComponentsPercentage);
 0112            loadingText = string.Format("Loading scenes {0}%", loadingComponentsPercentage);
 113        }
 114
 0115        totalActiveDownloads = AssetPromiseKeeper_GLTF.i.waitingPromisesCount + AssetPromiseKeeper_AB.i.waitingPromisesC
 0116        if (totalActiveDownloads > 0)
 117        {
 0118            downloadingAssetsPercentage = GetDownloadingAssetsPercentage(totalActiveDownloads);
 0119            secondLoadingText = string.Format("Downloading images, 3D models, and sounds {0}%", downloadingAssetsPercent
 120
 0121            if (!string.IsNullOrEmpty(loadingText))
 122            {
 0123                loadingText += "\n";
 124            }
 125
 0126            loadingText += secondLoadingText;
 127        }
 128
 0129        if (!string.IsNullOrEmpty(loadingText))
 130        {
 0131            DataStore.i.HUDs.loadingHUD.message.Set(loadingText);
 0132            messageToSend.message = loadingText;
 0133            WebInterface.ScenesLoadingFeedback(messageToSend);
 134        }
 0135    }
 136
 137    private int GetLoadingComponentsPercentage(int currentComponentsLoading)
 138    {
 0139        if (currentComponentsLoading > maxLoadingComponentsRef)
 0140            maxLoadingComponentsRef = currentComponentsLoading;
 141
 0142        int result = Mathf.FloorToInt(100f - (currentComponentsLoading * 100f / maxLoadingComponentsRef));
 0143        if (result > maxLoadingCalculatedPercentage)
 0144            maxLoadingCalculatedPercentage = result;
 145
 0146        return maxLoadingCalculatedPercentage;
 147    }
 148
 149    private int GetDownloadingAssetsPercentage(int totalActiveDownloads)
 150    {
 0151        if (totalActiveDownloads > maxDownloadingAssetsRef)
 0152            maxDownloadingAssetsRef = totalActiveDownloads;
 153
 0154        int result = Mathf.FloorToInt(100f - (totalActiveDownloads * 100f / maxDownloadingAssetsRef));
 0155        if (result > maxDownloadingCalculatedPercentage)
 0156            maxDownloadingCalculatedPercentage = result;
 157
 0158        return maxDownloadingCalculatedPercentage;
 159    }
 160
 161    private void RendererState_OnChange(bool current, bool previous)
 162    {
 113163        if (!current)
 5164            return;
 165
 108166        loadedScenes.Clear();
 108167        maxLoadingComponentsRef = 0;
 108168        maxLoadingCalculatedPercentage = 0;
 108169        maxDownloadingAssetsRef = 0;
 108170        maxDownloadingCalculatedPercentage = 0;
 108171    }
 172}