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