< 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:123
Uncovered lines:11
Coverable lines:134
Total lines:304
Line coverage:91.7% (123 of 134)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MessagingControllersManager()0%110100%
Initialize(...)0%330100%
MarkBusesDirty()0%110100%
PopulateBusesToBeProcessed()0%1717097.87%
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%220100%
ProcessMessages()0%11.5611083.33%
ProcessBus(...)0%4.024090%
RefreshControllerEnabledState(...)0%8.36060%

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
 66620        public Dictionary<string, MessagingController> messagingControllers { get; set; } = new Dictionary<string, Messa
 21
 22        private Coroutine mainCoroutine;
 23
 3024        public bool hasPendingMessages => pendingMessagesCount > 0;
 25
 66626        public float timeBudgetCounter = MAX_GLOBAL_MSG_BUDGET;
 27        public int pendingMessagesCount;
 28        public int pendingInitMessagesCount;
 29        public long processedInitMessagesCount;
 30
 031        public bool isRunning { get { return mainCoroutine != null; } }
 32
 4033        public bool paused { get; set; }
 34
 66635        private Dictionary<string, MessagingController> globalSceneControllers = new Dictionary<string, MessagingControl
 66636        private List<MessagingController> sortedControllers = new List<MessagingController>();
 66637        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        {
 66646            messagingControllers[GLOBAL_MESSAGING_CONTROLLER] = new MessagingController(messageHandler, GLOBAL_MESSAGING
 47
 66648            if (!string.IsNullOrEmpty(GLOBAL_MESSAGING_CONTROLLER))
 66649                messagingControllers.TryGetValue(GLOBAL_MESSAGING_CONTROLLER, out globalController);
 50
 66651            Environment.i.world.sceneController.OnSortScenes += MarkBusesDirty;
 52
 66653            if (mainCoroutine == null)
 54            {
 66655                mainCoroutine = CoroutineStarter.Start(ProcessMessages());
 56            }
 66657        }
 58
 66659        bool populateBusesDirty = true;
 60
 163061        public void MarkBusesDirty() { populateBusesDirty = true; }
 62
 63        public void PopulateBusesToBeProcessed()
 64        {
 217265            IWorldState worldState = Environment.i.world.state;
 217266            string currentSceneId = worldState.currentSceneId;
 217267            List<IParcelScene> scenesSortedByDistance = worldState.scenesSortedByDistance;
 68
 217269            int count = scenesSortedByDistance.Count; // we need to retrieve list count everytime because it
 70            // may change after a yield return
 71
 217272            sortedControllers.Clear();
 73
 217274            if (!string.IsNullOrEmpty(currentSceneId) && messagingControllers.ContainsKey(currentSceneId))
 15075                currentSceneController = messagingControllers[currentSceneId];
 76
 757677            for (int i = 0; i < count; i++)
 78            {
 161679                string controllerId = scenesSortedByDistance[i].sceneData.id;
 80
 161681                if (controllerId != currentSceneId)
 82                {
 146683                    if (!messagingControllers.ContainsKey(controllerId))
 84                        continue;
 85
 146586                    sortedControllers.Add(messagingControllers[controllerId]);
 87                }
 88            }
 89
 217290            sortedControllersCount = sortedControllers.Count;
 91
 217292            bool globalSceneControllerActive = globalSceneControllers.Count > 0;
 217293            bool globalControllerActive = globalController != null && globalController.enabled;
 217294            bool currentSceneControllerActive = currentSceneController != null && currentSceneController.enabled;
 95
 217296            bool atLeastOneControllerShouldBeProcessed = globalSceneControllerActive || globalControllerActive || curren
 97
 217298            if (!atLeastOneControllerShouldBeProcessed)
 099                return;
 100
 2172101            busesToProcess.Clear();
 102            //-------------------------------------------------------------------------------------------
 103            // Global scenes
 2172104            using (var globalScenecontrollersIterator = globalSceneControllers.GetEnumerator())
 105            {
 2195106                while (globalScenecontrollersIterator.MoveNext())
 107                {
 23108                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.uiBus);
 23109                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.initBus);
 23110                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.systemBus);
 111                }
 2172112            }
 113
 2172114            if (globalControllerActive)
 115            {
 2167116                busesToProcess.Add(globalController.initBus);
 117            }
 118
 2172119            if (currentSceneControllerActive)
 120            {
 251121                busesToProcess.Add(currentSceneController.initBus);
 251122                busesToProcess.Add(currentSceneController.uiBus);
 251123                busesToProcess.Add(currentSceneController.systemBus);
 124            }
 125
 7274126            for (int i = 0; i < sortedControllersCount; ++i)
 127            {
 1465128                MessagingController msgController = sortedControllers[i];
 129
 1465130                busesToProcess.Add(msgController.initBus);
 1465131                busesToProcess.Add(msgController.uiBus);
 132            }
 133
 7274134            for (int i = 0; i < sortedControllersCount; ++i)
 135            {
 1465136                MessagingController msgController = sortedControllers[i];
 1465137                busesToProcess.Add(msgController.systemBus);
 138            }
 139
 2172140            busesToProcessCount = busesToProcess.Count;
 2172141        }
 142
 143        public void Dispose()
 144        {
 686145            if (mainCoroutine != null)
 146            {
 666147                CoroutineStarter.Stop(mainCoroutine);
 666148                mainCoroutine = null;
 149            }
 150
 686151            using (var controllersIterator = messagingControllers.GetEnumerator())
 152            {
 2041153                while (controllersIterator.MoveNext())
 154                {
 1355155                    controllersIterator.Current.Value.Stop();
 1355156                    DisposeController(controllersIterator.Current.Value);
 157                }
 686158            }
 159
 686160            Environment.i.world.sceneController.OnSortScenes -= PopulateBusesToBeProcessed;
 161
 686162            messagingControllers.Clear();
 686163        }
 164
 704165        public bool ContainsController(string sceneId) { return messagingControllers.ContainsKey(sceneId); }
 166
 167        public void AddController(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = false)
 168        {
 704169            if (!messagingControllers.ContainsKey(sceneId))
 704170                messagingControllers[sceneId] = new MessagingController(messageHandler, sceneId);
 171
 704172            if (isGlobal && !string.IsNullOrEmpty(sceneId))
 173            {
 21174                messagingControllers.TryGetValue(sceneId, out MessagingController newGlobalSceneController);
 175
 21176                if (!globalSceneControllers.ContainsKey(sceneId))
 21177                    globalSceneControllers.Add(sceneId, newGlobalSceneController);
 178            }
 179
 704180            PopulateBusesToBeProcessed();
 704181        }
 182
 183        public void AddControllerIfNotExists(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = fals
 184        {
 704185            if (!ContainsController(sceneId))
 704186                AddController(messageHandler, sceneId, isGlobal);
 704187        }
 188
 189        public void RemoveController(string sceneId)
 190        {
 705191            if (messagingControllers.ContainsKey(sceneId))
 192            {
 193                // In case there is any pending message from a scene being unloaded we decrease the count accordingly
 15194                pendingMessagesCount -= messagingControllers[sceneId].messagingBuses[MessagingBusType.INIT].pendingMessa
 195                                        messagingControllers[sceneId].messagingBuses[MessagingBusType.UI].pendingMessage
 196                                        messagingControllers[sceneId].messagingBuses[MessagingBusType.SYSTEM].pendingMes
 197
 15198                DisposeController(messagingControllers[sceneId]);
 15199                messagingControllers.Remove(sceneId);
 200            }
 201
 705202            globalSceneControllers.Remove(sceneId);
 705203        }
 204
 205        void DisposeController(MessagingController controller)
 206        {
 1370207            controller.Stop();
 1370208            controller.Dispose();
 1370209        }
 210
 0211        public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage) { messagingControllers[queuedMessage.s
 212
 56213        public void ForceEnqueueToGlobal(MessagingBusType busId, QueuedSceneMessage queuedMessage) { messagingController
 214
 215        public void SetSceneReady(string sceneId)
 216        {
 217            // Start processing SYSTEM queue
 664218            if (messagingControllers.ContainsKey(sceneId))
 219            {
 220                // Start processing SYSTEM queue
 8221                MessagingController sceneMessagingController = messagingControllers[sceneId];
 8222                sceneMessagingController.StartBus(MessagingBusType.SYSTEM);
 8223                sceneMessagingController.StartBus(MessagingBusType.UI);
 8224                sceneMessagingController.StopBus(MessagingBusType.INIT);
 225            }
 664226        }
 227
 228        IEnumerator ProcessMessages()
 229        {
 7779230            while (true)
 231            {
 8445232                if (paused)
 233                {
 0234                    yield return null;
 0235                    continue;
 236                }
 237
 8445238                if (populateBusesDirty)
 239                {
 1468240                    PopulateBusesToBeProcessed();
 1468241                    populateBusesDirty = false;
 242                }
 243
 8445244                timeBudgetCounter = CommonScriptableObjects.rendererState.Get() ? MAX_GLOBAL_MSG_BUDGET : float.MaxValue
 245
 79284246                for (int i = 0; i < busesToProcessCount; ++i)
 247                {
 31197248                    MessagingBus bus = busesToProcess[i];
 249
 31197250                    if (ProcessBus(bus))
 251                        break;
 252                }
 253
 8445254                if (pendingInitMessagesCount == 0)
 255                {
 8445256                    UnityGLTF.GLTFSceneImporter.budgetPerFrameInMilliseconds = Mathf.Clamp(timeBudgetCounter, GLTF_BUDGE
 8445257                }
 258                else
 259                {
 0260                    UnityGLTF.GLTFSceneImporter.budgetPerFrameInMilliseconds = 0;
 261                }
 262
 8445263                yield return null;
 264            }
 265        }
 266
 267        bool ProcessBus(MessagingBus bus)
 268        {
 31197269            if (!bus.enabled || bus.pendingMessagesCount <= 0)
 31189270                return false;
 271
 8272            float startTime = Time.realtimeSinceStartup;
 273
 8274            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.
 8278            bus.ProcessQueue(timeBudget, out _);
 8279            RefreshControllerEnabledState(bus.owner);
 280
 8281            timeBudgetCounter -= Time.realtimeSinceStartup - startTime;
 282
 8283            if (timeBudgetCounter <= 0)
 0284                return true;
 285
 8286            return false;
 287        }
 288
 289        private void RefreshControllerEnabledState(MessagingController controller)
 290        {
 8291            if (controller == null || !controller.enabled)
 0292                return;
 293
 8294            if (controller.uiBus.pendingMessagesCount != 0)
 0295                return;
 8296            if (controller.initBus.pendingMessagesCount != 0)
 0297                return;
 8298            if (controller.systemBus.pendingMessagesCount != 0)
 0299                return;
 300
 8301            controller.enabled = false;
 8302        }
 303    }
 304}