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