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