| | 1 | | using DCL.Controllers; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class MessagingControllersManager : IMessagingControllersManager |
| | 9 | | { |
| | 10 | | public static bool VERBOSE = false; |
| | 11 | |
|
| | 12 | | private const float MAX_GLOBAL_MSG_BUDGET = 0.006f; |
| | 13 | | private const float MAX_SYSTEM_MSG_BUDGET_FOR_FAR_SCENES = 0.003f; |
| | 14 | |
|
| | 15 | | private const float GLTF_BUDGET_MAX = 0.033f; |
| | 16 | | private const float GLTF_BUDGET_MIN = 0.008f; |
| | 17 | |
|
| | 18 | | public const string GLOBAL_MESSAGING_CONTROLLER = "global_messaging_controller"; |
| | 19 | |
|
| 629 | 20 | | public Dictionary<string, MessagingController> messagingControllers { get; set; } = new Dictionary<string, Messa |
| | 21 | |
|
| | 22 | | private Coroutine mainCoroutine; |
| | 23 | |
|
| 28 | 24 | | public bool hasPendingMessages => pendingMessagesCount > 0; |
| | 25 | |
|
| 629 | 26 | | public float timeBudgetCounter = MAX_GLOBAL_MSG_BUDGET; |
| 54 | 27 | | public long processedInitMessagesCount { get; set; } |
| 108 | 28 | | public int pendingMessagesCount { get; set; } |
| 108 | 29 | | public int pendingInitMessagesCount { get; set; } |
| | 30 | |
|
| 0 | 31 | | public bool isRunning { get { return mainCoroutine != null; } } |
| | 32 | |
|
| 42 | 33 | | public bool paused { get; set; } |
| | 34 | |
|
| 629 | 35 | | private Dictionary<string, MessagingController> globalSceneControllers = new Dictionary<string, MessagingControl |
| 629 | 36 | | private List<MessagingController> sortedControllers = new List<MessagingController>(); |
| 629 | 37 | | private List<MessagingBus> busesToProcess = new List<MessagingBus>(); |
| | 38 | | private int busesToProcessCount = 0; |
| | 39 | | private int sortedControllersCount = 0; |
| | 40 | |
|
| | 41 | | private MessagingController globalController = null; |
| | 42 | | private MessagingController currentSceneController = null; |
| | 43 | |
|
| | 44 | | private IMessageProcessHandler messageHandler; |
| | 45 | |
|
| 629 | 46 | | public MessagingControllersManager (IMessageProcessHandler messageHandler = null) |
| | 47 | | { |
| 629 | 48 | | this.messageHandler = messageHandler; |
| 629 | 49 | | } |
| | 50 | |
|
| | 51 | | public void Initialize() |
| | 52 | | { |
| 629 | 53 | | if ( messageHandler == null ) |
| 629 | 54 | | messageHandler = Environment.i.world.sceneController; |
| | 55 | |
|
| 629 | 56 | | messagingControllers[GLOBAL_MESSAGING_CONTROLLER] = new MessagingController(this, messageHandler, GLOBAL_MES |
| | 57 | |
|
| 629 | 58 | | if (!string.IsNullOrEmpty(GLOBAL_MESSAGING_CONTROLLER)) |
| 629 | 59 | | messagingControllers.TryGetValue(GLOBAL_MESSAGING_CONTROLLER, out globalController); |
| | 60 | |
|
| 629 | 61 | | Environment.i.world.sceneController.OnSortScenes += MarkBusesDirty; |
| | 62 | |
|
| 629 | 63 | | if (mainCoroutine == null) |
| | 64 | | { |
| 629 | 65 | | mainCoroutine = CoroutineStarter.Start(ProcessMessages()); |
| | 66 | | } |
| 629 | 67 | | } |
| | 68 | |
|
| 629 | 69 | | bool populateBusesDirty = true; |
| | 70 | |
|
| 1224 | 71 | | public void MarkBusesDirty() { populateBusesDirty = true; } |
| | 72 | |
|
| | 73 | | public void PopulateBusesToBeProcessed() |
| | 74 | | { |
| 1246 | 75 | | IWorldState worldState = Environment.i.world.state; |
| 1246 | 76 | | string currentSceneId = worldState.currentSceneId; |
| 1246 | 77 | | List<IParcelScene> scenesSortedByDistance = worldState.scenesSortedByDistance; |
| | 78 | |
|
| 1246 | 79 | | int count = scenesSortedByDistance.Count; // we need to retrieve list count everytime because it |
| | 80 | | // may change after a yield return |
| | 81 | |
|
| 1246 | 82 | | sortedControllers.Clear(); |
| | 83 | |
|
| 1246 | 84 | | if (!string.IsNullOrEmpty(currentSceneId) && messagingControllers.ContainsKey(currentSceneId)) |
| 0 | 85 | | currentSceneController = messagingControllers[currentSceneId]; |
| | 86 | |
|
| 2878 | 87 | | for (int i = 0; i < count; i++) |
| | 88 | | { |
| 193 | 89 | | string controllerId = scenesSortedByDistance[i].sceneData.id; |
| | 90 | |
|
| 193 | 91 | | if (controllerId != currentSceneId) |
| | 92 | | { |
| 185 | 93 | | if (!messagingControllers.ContainsKey(controllerId)) |
| | 94 | | continue; |
| | 95 | |
|
| 155 | 96 | | sortedControllers.Add(messagingControllers[controllerId]); |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| 1246 | 100 | | sortedControllersCount = sortedControllers.Count; |
| | 101 | |
|
| 1246 | 102 | | bool globalSceneControllerActive = globalSceneControllers.Count > 0; |
| 1246 | 103 | | bool globalControllerActive = globalController != null && globalController.enabled; |
| 1246 | 104 | | bool currentSceneControllerActive = currentSceneController != null && currentSceneController.enabled; |
| | 105 | |
|
| 1246 | 106 | | bool atLeastOneControllerShouldBeProcessed = globalSceneControllerActive || globalControllerActive || curren |
| | 107 | |
|
| 1246 | 108 | | if (!atLeastOneControllerShouldBeProcessed) |
| 0 | 109 | | return; |
| | 110 | |
|
| 1246 | 111 | | busesToProcess.Clear(); |
| | 112 | | //------------------------------------------------------------------------------------------- |
| | 113 | | // Global scenes |
| 1246 | 114 | | using (var globalScenecontrollersIterator = globalSceneControllers.GetEnumerator()) |
| | 115 | | { |
| 1248 | 116 | | while (globalScenecontrollersIterator.MoveNext()) |
| | 117 | | { |
| 2 | 118 | | busesToProcess.Add(globalScenecontrollersIterator.Current.Value.uiBus); |
| 2 | 119 | | busesToProcess.Add(globalScenecontrollersIterator.Current.Value.initBus); |
| 2 | 120 | | busesToProcess.Add(globalScenecontrollersIterator.Current.Value.systemBus); |
| | 121 | | } |
| 1246 | 122 | | } |
| | 123 | |
|
| 1246 | 124 | | if (globalControllerActive) |
| | 125 | | { |
| 1243 | 126 | | busesToProcess.Add(globalController.initBus); |
| | 127 | | } |
| | 128 | |
|
| 1246 | 129 | | if (currentSceneControllerActive) |
| | 130 | | { |
| 0 | 131 | | busesToProcess.Add(currentSceneController.initBus); |
| 0 | 132 | | busesToProcess.Add(currentSceneController.uiBus); |
| 0 | 133 | | busesToProcess.Add(currentSceneController.systemBus); |
| | 134 | | } |
| | 135 | |
|
| 2802 | 136 | | for (int i = 0; i < sortedControllersCount; ++i) |
| | 137 | | { |
| 155 | 138 | | MessagingController msgController = sortedControllers[i]; |
| | 139 | |
|
| 155 | 140 | | busesToProcess.Add(msgController.initBus); |
| 155 | 141 | | busesToProcess.Add(msgController.uiBus); |
| | 142 | | } |
| | 143 | |
|
| 2802 | 144 | | for (int i = 0; i < sortedControllersCount; ++i) |
| | 145 | | { |
| 155 | 146 | | MessagingController msgController = sortedControllers[i]; |
| 155 | 147 | | busesToProcess.Add(msgController.systemBus); |
| | 148 | | } |
| | 149 | |
|
| 1246 | 150 | | busesToProcessCount = busesToProcess.Count; |
| 1246 | 151 | | } |
| | 152 | |
|
| | 153 | | public void Dispose() |
| | 154 | | { |
| 629 | 155 | | if (mainCoroutine != null) |
| | 156 | | { |
| 629 | 157 | | CoroutineStarter.Stop(mainCoroutine); |
| 629 | 158 | | mainCoroutine = null; |
| | 159 | | } |
| | 160 | |
|
| 629 | 161 | | using (var controllersIterator = messagingControllers.GetEnumerator()) |
| | 162 | | { |
| 1258 | 163 | | while (controllersIterator.MoveNext()) |
| | 164 | | { |
| 629 | 165 | | controllersIterator.Current.Value.Stop(); |
| 629 | 166 | | DisposeController(controllersIterator.Current.Value); |
| | 167 | | } |
| 629 | 168 | | } |
| | 169 | |
|
| 629 | 170 | | Environment.i.world.sceneController.OnSortScenes -= PopulateBusesToBeProcessed; |
| | 171 | |
|
| 629 | 172 | | messagingControllers.Clear(); |
| 629 | 173 | | } |
| | 174 | |
|
| 27 | 175 | | public bool ContainsController(string sceneId) { return messagingControllers.ContainsKey(sceneId); } |
| | 176 | |
|
| | 177 | | public void AddController(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = false) |
| | 178 | | { |
| 27 | 179 | | if (!messagingControllers.ContainsKey(sceneId)) |
| 27 | 180 | | messagingControllers[sceneId] = new MessagingController(this, messageHandler, sceneId); |
| | 181 | |
|
| 27 | 182 | | if (isGlobal && !string.IsNullOrEmpty(sceneId)) |
| | 183 | | { |
| 1 | 184 | | messagingControllers.TryGetValue(sceneId, out MessagingController newGlobalSceneController); |
| | 185 | |
|
| 1 | 186 | | if (!globalSceneControllers.ContainsKey(sceneId)) |
| 1 | 187 | | globalSceneControllers.Add(sceneId, newGlobalSceneController); |
| | 188 | | } |
| | 189 | |
|
| 27 | 190 | | PopulateBusesToBeProcessed(); |
| 27 | 191 | | } |
| | 192 | |
|
| | 193 | | public void AddControllerIfNotExists(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = fals |
| | 194 | | { |
| 27 | 195 | | if (!ContainsController(sceneId)) |
| 27 | 196 | | AddController(messageHandler, sceneId, isGlobal); |
| 27 | 197 | | } |
| | 198 | |
|
| | 199 | | public void RemoveController(string sceneId) |
| | 200 | | { |
| 384 | 201 | | if (messagingControllers.ContainsKey(sceneId)) |
| | 202 | | { |
| | 203 | | // In case there is any pending message from a scene being unloaded we decrease the count accordingly |
| 27 | 204 | | pendingMessagesCount -= messagingControllers[sceneId].messagingBuses[MessagingBusType.INIT].pendingMessa |
| | 205 | | messagingControllers[sceneId].messagingBuses[MessagingBusType.UI].pendingMessage |
| | 206 | | messagingControllers[sceneId].messagingBuses[MessagingBusType.SYSTEM].pendingMes |
| | 207 | |
|
| 27 | 208 | | DisposeController(messagingControllers[sceneId]); |
| 27 | 209 | | messagingControllers.Remove(sceneId); |
| | 210 | | } |
| | 211 | |
|
| 384 | 212 | | globalSceneControllers.Remove(sceneId); |
| 384 | 213 | | } |
| | 214 | |
|
| | 215 | | void DisposeController(MessagingController controller) |
| | 216 | | { |
| 656 | 217 | | controller.Stop(); |
| 656 | 218 | | controller.Dispose(); |
| 656 | 219 | | } |
| | 220 | |
|
| 0 | 221 | | public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage) { messagingControllers[queuedMessage.s |
| | 222 | |
|
| 54 | 223 | | public void ForceEnqueueToGlobal(MessagingBusType busId, QueuedSceneMessage queuedMessage) { messagingController |
| | 224 | |
|
| | 225 | | public void SetSceneReady(string sceneId) |
| | 226 | | { |
| | 227 | | // Start processing SYSTEM queue |
| 368 | 228 | | if (messagingControllers.ContainsKey(sceneId)) |
| | 229 | | { |
| | 230 | | // Start processing SYSTEM queue |
| 0 | 231 | | MessagingController sceneMessagingController = messagingControllers[sceneId]; |
| 0 | 232 | | sceneMessagingController.StartBus(MessagingBusType.SYSTEM); |
| 0 | 233 | | sceneMessagingController.StartBus(MessagingBusType.UI); |
| 0 | 234 | | sceneMessagingController.StopBus(MessagingBusType.INIT); |
| | 235 | | } |
| 368 | 236 | | } |
| | 237 | |
|
| | 238 | | IEnumerator ProcessMessages() |
| | 239 | | { |
| 6940 | 240 | | while (true) |
| | 241 | | { |
| 7569 | 242 | | if (paused) |
| | 243 | | { |
| 0 | 244 | | yield return null; |
| 0 | 245 | | continue; |
| | 246 | | } |
| | 247 | |
|
| 7569 | 248 | | if (populateBusesDirty) |
| | 249 | | { |
| 1219 | 250 | | PopulateBusesToBeProcessed(); |
| 1219 | 251 | | populateBusesDirty = false; |
| | 252 | | } |
| | 253 | |
|
| 7569 | 254 | | timeBudgetCounter = CommonScriptableObjects.rendererState.Get() ? MAX_GLOBAL_MSG_BUDGET : float.MaxValue |
| | 255 | |
|
| 30550 | 256 | | for (int i = 0; i < busesToProcessCount; ++i) |
| | 257 | | { |
| 7708 | 258 | | MessagingBus bus = busesToProcess[i]; |
| | 259 | |
|
| 7708 | 260 | | if (ProcessBus(bus)) |
| | 261 | | break; |
| | 262 | | } |
| | 263 | |
|
| 7569 | 264 | | if (pendingInitMessagesCount == 0) |
| | 265 | | { |
| 7567 | 266 | | AssetPromiseKeeper_GLTF.i.throttlingCounter.budgetPerFrameInMilliseconds = Mathf.Clamp(timeBudgetCou |
| 7567 | 267 | | } |
| | 268 | | else |
| | 269 | | { |
| 2 | 270 | | AssetPromiseKeeper_GLTF.i.throttlingCounter.budgetPerFrameInMilliseconds = 0; |
| | 271 | | } |
| | 272 | |
|
| 7569 | 273 | | yield return null; |
| | 274 | | } |
| | 275 | | } |
| | 276 | |
|
| | 277 | | bool ProcessBus(MessagingBus bus) |
| | 278 | | { |
| 7708 | 279 | | if (!bus.enabled || bus.pendingMessagesCount <= 0) |
| 7699 | 280 | | return false; |
| | 281 | |
|
| 9 | 282 | | float startTime = Time.realtimeSinceStartup; |
| | 283 | |
|
| 9 | 284 | | float timeBudget = timeBudgetCounter; |
| | 285 | |
|
| | 286 | | //TODO(Brian): We should use the returning yieldReturn IEnumerator and MoveNext() it manually each frame to |
| | 287 | | // account the coroutine processing into the budget. Until we do that we just skip it. |
| 9 | 288 | | bus.ProcessQueue(timeBudget, out _); |
| 9 | 289 | | RefreshControllerEnabledState(bus.owner); |
| | 290 | |
|
| 9 | 291 | | timeBudgetCounter -= Time.realtimeSinceStartup - startTime; |
| | 292 | |
|
| 9 | 293 | | if (timeBudgetCounter <= 0) |
| 2 | 294 | | return true; |
| | 295 | |
|
| 7 | 296 | | return false; |
| | 297 | | } |
| | 298 | |
|
| | 299 | | private void RefreshControllerEnabledState(MessagingController controller) |
| | 300 | | { |
| 9 | 301 | | if (controller == null || !controller.enabled) |
| 0 | 302 | | return; |
| | 303 | |
|
| 9 | 304 | | if (controller.uiBus.pendingMessagesCount != 0) |
| 0 | 305 | | return; |
| 9 | 306 | | if (controller.initBus.pendingMessagesCount != 0) |
| 2 | 307 | | return; |
| 7 | 308 | | if (controller.systemBus.pendingMessagesCount != 0) |
| 0 | 309 | | return; |
| | 310 | |
|
| 7 | 311 | | controller.enabled = false; |
| 7 | 312 | | } |
| | 313 | | } |
| | 314 | | } |