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