| | 1 | | using DCL; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityGLTF; |
| | 8 | |
|
| | 9 | | namespace 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 | |
|
| 652 | 34 | | public LoadingFeedbackController () |
| | 35 | | { |
| 652 | 36 | | loadedScenes = new List<SceneLoadingStatus>(); |
| | 37 | |
|
| 652 | 38 | | Environment.i.world.sceneController.OnNewSceneAdded += SceneController_OnNewSceneAdded; |
| 652 | 39 | | GLTFComponent.OnDownloadingProgressUpdate += GLTFComponent_OnDownloadingProgressUpdate; |
| 652 | 40 | | AssetPromise_AB.OnDownloadingProgressUpdate += AssetPromise_AB_OnDownloadingProgressUpdate; |
| 652 | 41 | | CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange; |
| 652 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Dispose() |
| | 45 | | { |
| 652 | 46 | | Environment.i.world.sceneController.OnNewSceneAdded -= SceneController_OnNewSceneAdded; |
| 652 | 47 | | GLTFComponent.OnDownloadingProgressUpdate -= GLTFComponent_OnDownloadingProgressUpdate; |
| 652 | 48 | | AssetPromise_AB.OnDownloadingProgressUpdate -= AssetPromise_AB_OnDownloadingProgressUpdate; |
| 652 | 49 | | CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange; |
| 652 | 50 | | } |
| | 51 | |
|
| | 52 | | private void SceneController_OnNewSceneAdded(IParcelScene scene) |
| | 53 | | { |
| 27 | 54 | | var parcelScene = scene as ParcelScene; |
| | 55 | |
|
| 27 | 56 | | if ( parcelScene == null ) |
| 0 | 57 | | return; |
| | 58 | |
|
| 27 | 59 | | parcelScene.sceneLifecycleHandler.OnStateRefreshed += Scene_OnStateRefreshed; |
| 27 | 60 | | } |
| | 61 | |
|
| | 62 | | private void Scene_OnStateRefreshed(ParcelScene scene) |
| | 63 | | { |
| 0 | 64 | | SceneLoadingStatus refreshedScene = new SceneLoadingStatus |
| | 65 | | { |
| | 66 | | sceneId = scene.GetInstanceID(), |
| | 67 | | componentsLoading = scene.sceneLifecycleHandler.disposableNotReadyCount |
| | 68 | | }; |
| | 69 | |
|
| 0 | 70 | | switch (scene.sceneLifecycleHandler.state) |
| | 71 | | { |
| | 72 | | case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS: |
| 0 | 73 | | AddOrUpdateLoadedScene(refreshedScene); |
| 0 | 74 | | break; |
| | 75 | | case SceneLifecycleHandler.State.READY: |
| 0 | 76 | | scene.sceneLifecycleHandler.OnStateRefreshed -= Scene_OnStateRefreshed; |
| | 77 | | break; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | RefreshFeedbackMessage(); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void AddOrUpdateLoadedScene(SceneLoadingStatus scene) |
| | 84 | | { |
| 0 | 85 | | SceneLoadingStatus existingScene = loadedScenes.FirstOrDefault(x => x.sceneId == scene.sceneId); |
| 0 | 86 | | if (existingScene == null) |
| 0 | 87 | | loadedScenes.Add(scene); |
| | 88 | | else |
| | 89 | | { |
| 0 | 90 | | existingScene.componentsLoading = scene.componentsLoading; |
| | 91 | | } |
| 0 | 92 | | } |
| | 93 | |
|
| 296 | 94 | | private void GLTFComponent_OnDownloadingProgressUpdate() { RefreshFeedbackMessage(); } |
| | 95 | |
|
| 108 | 96 | | private void AssetPromise_AB_OnDownloadingProgressUpdate() { RefreshFeedbackMessage(); } |
| | 97 | |
|
| | 98 | | private void RefreshFeedbackMessage() |
| | 99 | | { |
| 202 | 100 | | if (!DataStore.i.HUDs.loadingHUD.visible.Get()) |
| 202 | 101 | | return; |
| | 102 | |
|
| 0 | 103 | | string loadingText = string.Empty; |
| 0 | 104 | | string secondLoadingText = string.Empty; |
| 0 | 105 | | DCL.Interface.WebInterface.LoadingFeedbackMessage messageToSend = new WebInterface.LoadingFeedbackMessage(); |
| 0 | 106 | | messageToSend.loadPercentage = 0; |
| | 107 | |
|
| 0 | 108 | | currentComponentsLoading = loadedScenes.Sum(x => x.componentsLoading); |
| 0 | 109 | | if (currentComponentsLoading > 0) |
| | 110 | | { |
| 0 | 111 | | loadingComponentsPercentage = GetLoadingComponentsPercentage(currentComponentsLoading); |
| 0 | 112 | | messageToSend.loadPercentage = loadingComponentsPercentage; |
| 0 | 113 | | DataStore.i.HUDs.loadingHUD.percentage.Set(loadingComponentsPercentage); |
| 0 | 114 | | loadingText = string.Format("Loading scenes {0}%", loadingComponentsPercentage); |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | totalActiveDownloads = AssetPromiseKeeper_GLTF.i.waitingPromisesCount + AssetPromiseKeeper_AB.i.waitingPromi |
| 0 | 118 | | if (totalActiveDownloads > 0) |
| | 119 | | { |
| 0 | 120 | | downloadingAssetsPercentage = GetDownloadingAssetsPercentage(totalActiveDownloads); |
| 0 | 121 | | secondLoadingText = string.Format("Downloading images, 3D models, and sounds {0}%", downloadingAssetsPer |
| | 122 | |
|
| 0 | 123 | | if (!string.IsNullOrEmpty(loadingText)) |
| | 124 | | { |
| 0 | 125 | | loadingText += "\n"; |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | loadingText += secondLoadingText; |
| | 129 | | } |
| | 130 | |
|
| 0 | 131 | | if (!string.IsNullOrEmpty(loadingText)) |
| | 132 | | { |
| 0 | 133 | | DataStore.i.HUDs.loadingHUD.message.Set(loadingText); |
| 0 | 134 | | messageToSend.message = loadingText; |
| 0 | 135 | | WebInterface.ScenesLoadingFeedback(messageToSend); |
| | 136 | | } |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | private int GetLoadingComponentsPercentage(int currentComponentsLoading) |
| | 140 | | { |
| 0 | 141 | | if (currentComponentsLoading > maxLoadingComponentsRef) |
| 0 | 142 | | maxLoadingComponentsRef = currentComponentsLoading; |
| | 143 | |
|
| 0 | 144 | | int result = Mathf.FloorToInt(100f - (currentComponentsLoading * 100f / maxLoadingComponentsRef)); |
| 0 | 145 | | if (result > maxLoadingCalculatedPercentage) |
| 0 | 146 | | maxLoadingCalculatedPercentage = result; |
| | 147 | |
|
| 0 | 148 | | return maxLoadingCalculatedPercentage; |
| | 149 | | } |
| | 150 | |
|
| | 151 | | private int GetDownloadingAssetsPercentage(int totalActiveDownloads) |
| | 152 | | { |
| 0 | 153 | | if (totalActiveDownloads > maxDownloadingAssetsRef) |
| 0 | 154 | | maxDownloadingAssetsRef = totalActiveDownloads; |
| | 155 | |
|
| 0 | 156 | | int result = Mathf.FloorToInt(100f - (totalActiveDownloads * 100f / maxDownloadingAssetsRef)); |
| 0 | 157 | | if (result > maxDownloadingCalculatedPercentage) |
| 0 | 158 | | maxDownloadingCalculatedPercentage = result; |
| | 159 | |
|
| 0 | 160 | | return maxDownloadingCalculatedPercentage; |
| | 161 | | } |
| | 162 | |
|
| | 163 | | private void RendererState_OnChange(bool current, bool previous) |
| | 164 | | { |
| 2 | 165 | | if (!current) |
| 1 | 166 | | return; |
| | 167 | |
|
| 1 | 168 | | loadedScenes.Clear(); |
| 1 | 169 | | maxLoadingComponentsRef = 0; |
| 1 | 170 | | maxLoadingCalculatedPercentage = 0; |
| 1 | 171 | | maxDownloadingAssetsRef = 0; |
| 1 | 172 | | maxDownloadingCalculatedPercentage = 0; |
| 1 | 173 | | } |
| | 174 | | } |
| | 175 | | } |