< Summary

Class:DCL.MessagingControllersManager
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/Messaging/MessagingControllersManager.cs
Covered lines:126
Uncovered lines:16
Coverable lines:142
Total lines:314
Line coverage:88.7% (126 of 142)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MessagingControllersManager(...)0%110100%
Initialize()0%440100%
MarkBusesDirty()0%110100%
PopulateBusesToBeProcessed()0%17.3517089.36%
Dispose()0%330100%
ContainsController(...)0%110100%
AddController(...)0%550100%
AddControllerIfNotExists(...)0%220100%
RemoveController(...)0%220100%
DisposeController(...)0%110100%
Enqueue(...)0%2100%
ForceEnqueueToGlobal(...)0%110100%
SetSceneReady(...)0%3.192033.33%
ProcessMessages()0%11.1711088.89%
ProcessBus(...)0%440100%
RefreshControllerEnabledState(...)0%6.976070%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Scene/Messaging/MessagingControllersManager.cs

#LineLine coverage
 1using DCL.Controllers;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6namespace 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
 63020        public Dictionary<string, MessagingController> messagingControllers { get; set; } = new Dictionary<string, Messa
 21
 22        private Coroutine mainCoroutine;
 23
 2824        public bool hasPendingMessages => pendingMessagesCount > 0;
 25
 63026        public float timeBudgetCounter = MAX_GLOBAL_MSG_BUDGET;
 5427        public long processedInitMessagesCount { get; set; }
 10828        public int pendingMessagesCount { get; set;  }
 10829        public int pendingInitMessagesCount { get; set; }
 30
 031        public bool isRunning { get { return mainCoroutine != null; } }
 32
 4233        public bool paused { get; set; }
 34
 63035        private Dictionary<string, MessagingController> globalSceneControllers = new Dictionary<string, MessagingControl
 63036        private List<MessagingController> sortedControllers = new List<MessagingController>();
 63037        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
 63046        public MessagingControllersManager (IMessageProcessHandler messageHandler = null)
 47        {
 63048            this.messageHandler = messageHandler;
 63049        }
 50
 51        public void Initialize()
 52        {
 63053            if ( messageHandler == null )
 63054                messageHandler = Environment.i.world.sceneController;
 55
 63056            messagingControllers[GLOBAL_MESSAGING_CONTROLLER] = new MessagingController(this, messageHandler, GLOBAL_MES
 57
 63058            if (!string.IsNullOrEmpty(GLOBAL_MESSAGING_CONTROLLER))
 63059                messagingControllers.TryGetValue(GLOBAL_MESSAGING_CONTROLLER, out globalController);
 60
 63061            Environment.i.world.sceneController.OnSortScenes += MarkBusesDirty;
 62
 63063            if (mainCoroutine == null)
 64            {
 63065                mainCoroutine = CoroutineStarter.Start(ProcessMessages());
 66            }
 63067        }
 68
 63069        bool populateBusesDirty = true;
 70
 122671        public void MarkBusesDirty() { populateBusesDirty = true; }
 72
 73        public void PopulateBusesToBeProcessed()
 74        {
 124875            IWorldState worldState = Environment.i.world.state;
 124876            string currentSceneId = worldState.currentSceneId;
 124877            List<IParcelScene> scenesSortedByDistance = worldState.scenesSortedByDistance;
 78
 124879            int count = scenesSortedByDistance.Count; // we need to retrieve list count everytime because it
 80            // may change after a yield return
 81
 124882            sortedControllers.Clear();
 83
 124884            if (!string.IsNullOrEmpty(currentSceneId) && messagingControllers.ContainsKey(currentSceneId))
 085                currentSceneController = messagingControllers[currentSceneId];
 86
 289287            for (int i = 0; i < count; i++)
 88            {
 19889                string controllerId = scenesSortedByDistance[i].sceneData.id;
 90
 19891                if (controllerId != currentSceneId)
 92                {
 19193                    if (!messagingControllers.ContainsKey(controllerId))
 94                        continue;
 95
 15896                    sortedControllers.Add(messagingControllers[controllerId]);
 97                }
 98            }
 99
 1248100            sortedControllersCount = sortedControllers.Count;
 101
 1248102            bool globalSceneControllerActive = globalSceneControllers.Count > 0;
 1248103            bool globalControllerActive = globalController != null && globalController.enabled;
 1248104            bool currentSceneControllerActive = currentSceneController != null && currentSceneController.enabled;
 105
 1248106            bool atLeastOneControllerShouldBeProcessed = globalSceneControllerActive || globalControllerActive || curren
 107
 1248108            if (!atLeastOneControllerShouldBeProcessed)
 0109                return;
 110
 1248111            busesToProcess.Clear();
 112            //-------------------------------------------------------------------------------------------
 113            // Global scenes
 1248114            using (var globalScenecontrollersIterator = globalSceneControllers.GetEnumerator())
 115            {
 1250116                while (globalScenecontrollersIterator.MoveNext())
 117                {
 2118                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.uiBus);
 2119                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.initBus);
 2120                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.systemBus);
 121                }
 1248122            }
 123
 1248124            if (globalControllerActive)
 125            {
 1245126                busesToProcess.Add(globalController.initBus);
 127            }
 128
 1248129            if (currentSceneControllerActive)
 130            {
 0131                busesToProcess.Add(currentSceneController.initBus);
 0132                busesToProcess.Add(currentSceneController.uiBus);
 0133                busesToProcess.Add(currentSceneController.systemBus);
 134            }
 135
 2812136            for (int i = 0; i < sortedControllersCount; ++i)
 137            {
 158138                MessagingController msgController = sortedControllers[i];
 139
 158140                busesToProcess.Add(msgController.initBus);
 158141                busesToProcess.Add(msgController.uiBus);
 142            }
 143
 2812144            for (int i = 0; i < sortedControllersCount; ++i)
 145            {
 158146                MessagingController msgController = sortedControllers[i];
 158147                busesToProcess.Add(msgController.systemBus);
 148            }
 149
 1248150            busesToProcessCount = busesToProcess.Count;
 1248151        }
 152
 153        public void Dispose()
 154        {
 630155            if (mainCoroutine != null)
 156            {
 630157                CoroutineStarter.Stop(mainCoroutine);
 630158                mainCoroutine = null;
 159            }
 160
 630161            using (var controllersIterator = messagingControllers.GetEnumerator())
 162            {
 1260163                while (controllersIterator.MoveNext())
 164                {
 630165                    controllersIterator.Current.Value.Stop();
 630166                    DisposeController(controllersIterator.Current.Value);
 167                }
 630168            }
 169
 630170            Environment.i.world.sceneController.OnSortScenes -= PopulateBusesToBeProcessed;
 171
 630172            messagingControllers.Clear();
 630173        }
 174
 27175        public bool ContainsController(string sceneId) { return messagingControllers.ContainsKey(sceneId); }
 176
 177        public void AddController(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = false)
 178        {
 27179            if (!messagingControllers.ContainsKey(sceneId))
 27180                messagingControllers[sceneId] = new MessagingController(this, messageHandler, sceneId);
 181
 27182            if (isGlobal && !string.IsNullOrEmpty(sceneId))
 183            {
 1184                messagingControllers.TryGetValue(sceneId, out MessagingController newGlobalSceneController);
 185
 1186                if (!globalSceneControllers.ContainsKey(sceneId))
 1187                    globalSceneControllers.Add(sceneId, newGlobalSceneController);
 188            }
 189
 27190            PopulateBusesToBeProcessed();
 27191        }
 192
 193        public void AddControllerIfNotExists(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = fals
 194        {
 27195            if (!ContainsController(sceneId))
 27196                AddController(messageHandler, sceneId, isGlobal);
 27197        }
 198
 199        public void RemoveController(string sceneId)
 200        {
 383201            if (messagingControllers.ContainsKey(sceneId))
 202            {
 203                // In case there is any pending message from a scene being unloaded we decrease the count accordingly
 27204                pendingMessagesCount -= messagingControllers[sceneId].messagingBuses[MessagingBusType.INIT].pendingMessa
 205                                        messagingControllers[sceneId].messagingBuses[MessagingBusType.UI].pendingMessage
 206                                        messagingControllers[sceneId].messagingBuses[MessagingBusType.SYSTEM].pendingMes
 207
 27208                DisposeController(messagingControllers[sceneId]);
 27209                messagingControllers.Remove(sceneId);
 210            }
 211
 383212            globalSceneControllers.Remove(sceneId);
 383213        }
 214
 215        void DisposeController(MessagingController controller)
 216        {
 657217            controller.Stop();
 657218            controller.Dispose();
 657219        }
 220
 0221        public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage) { messagingControllers[queuedMessage.s
 222
 54223        public void ForceEnqueueToGlobal(MessagingBusType busId, QueuedSceneMessage queuedMessage) { messagingController
 224
 225        public void SetSceneReady(string sceneId)
 226        {
 227            // Start processing SYSTEM queue
 367228            if (messagingControllers.ContainsKey(sceneId))
 229            {
 230                // Start processing SYSTEM queue
 0231                MessagingController sceneMessagingController = messagingControllers[sceneId];
 0232                sceneMessagingController.StartBus(MessagingBusType.SYSTEM);
 0233                sceneMessagingController.StartBus(MessagingBusType.UI);
 0234                sceneMessagingController.StopBus(MessagingBusType.INIT);
 235            }
 367236        }
 237
 238        IEnumerator ProcessMessages()
 239        {
 8544240            while (true)
 241            {
 9174242                if (paused)
 243                {
 0244                    yield return null;
 0245                    continue;
 246                }
 247
 9174248                if (populateBusesDirty)
 249                {
 1221250                    PopulateBusesToBeProcessed();
 1221251                    populateBusesDirty = false;
 252                }
 253
 9174254                timeBudgetCounter = CommonScriptableObjects.rendererState.Get() ? MAX_GLOBAL_MSG_BUDGET : float.MaxValue
 255
 36970256                for (int i = 0; i < busesToProcessCount; ++i)
 257                {
 9313258                    MessagingBus bus = busesToProcess[i];
 259
 9313260                    if (ProcessBus(bus))
 261                        break;
 262                }
 263
 9174264                if (pendingInitMessagesCount == 0)
 265                {
 9172266                    AssetPromiseKeeper_GLTF.i.throttlingCounter.budgetPerFrameInMilliseconds = Mathf.Clamp(timeBudgetCou
 9172267                }
 268                else
 269                {
 2270                    AssetPromiseKeeper_GLTF.i.throttlingCounter.budgetPerFrameInMilliseconds = 0;
 271                }
 272
 9174273                yield return null;
 274            }
 275        }
 276
 277        bool ProcessBus(MessagingBus bus)
 278        {
 9313279            if (!bus.enabled || bus.pendingMessagesCount <= 0)
 9304280                return false;
 281
 9282            float startTime = Time.realtimeSinceStartup;
 283
 9284            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.
 9288            bus.ProcessQueue(timeBudget, out _);
 9289            RefreshControllerEnabledState(bus.owner);
 290
 9291            timeBudgetCounter -= Time.realtimeSinceStartup - startTime;
 292
 9293            if (timeBudgetCounter <= 0)
 2294                return true;
 295
 7296            return false;
 297        }
 298
 299        private void RefreshControllerEnabledState(MessagingController controller)
 300        {
 9301            if (controller == null || !controller.enabled)
 0302                return;
 303
 9304            if (controller.uiBus.pendingMessagesCount != 0)
 0305                return;
 9306            if (controller.initBus.pendingMessagesCount != 0)
 2307                return;
 7308            if (controller.systemBus.pendingMessagesCount != 0)
 0309                return;
 310
 7311            controller.enabled = false;
 7312        }
 313    }
 314}