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