< 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
 2724        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        {
 217365            IWorldState worldState = Environment.i.world.state;
 217366            string currentSceneId = worldState.currentSceneId;
 217367            List<IParcelScene> scenesSortedByDistance = worldState.scenesSortedByDistance;
 68
 217369            int count = scenesSortedByDistance.Count; // we need to retrieve list count everytime because it
 70            // may change after a yield return
 71
 217372            sortedControllers.Clear();
 73
 217374            if (!string.IsNullOrEmpty(currentSceneId) && messagingControllers.ContainsKey(currentSceneId))
 14975                currentSceneController = messagingControllers[currentSceneId];
 76
 757677            for (int i = 0; i < count; i++)
 78            {
 161579                string controllerId = scenesSortedByDistance[i].sceneData.id;
 80
 161581                if (controllerId != currentSceneId)
 82                {
 146683                    if (!messagingControllers.ContainsKey(controllerId))
 84                        continue;
 85
 146586                    sortedControllers.Add(messagingControllers[controllerId]);
 87                }
 88            }
 89
 217390            sortedControllersCount = sortedControllers.Count;
 91
 217392            bool globalSceneControllerActive = globalSceneControllers.Count > 0;
 217393            bool globalControllerActive = globalController != null && globalController.enabled;
 217394            bool currentSceneControllerActive = currentSceneController != null && currentSceneController.enabled;
 95
 217396            bool atLeastOneControllerShouldBeProcessed = globalSceneControllerActive || globalControllerActive || curren
 97
 217398            if (!atLeastOneControllerShouldBeProcessed)
 099                return;
 100
 2173101            busesToProcess.Clear();
 102            //-------------------------------------------------------------------------------------------
 103            // Global scenes
 2173104            using (var globalScenecontrollersIterator = globalSceneControllers.GetEnumerator())
 105            {
 2197106                while (globalScenecontrollersIterator.MoveNext())
 107                {
 24108                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.uiBus);
 24109                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.initBus);
 24110                    busesToProcess.Add(globalScenecontrollersIterator.Current.Value.systemBus);
 111                }
 2173112            }
 113
 2173114            if (globalControllerActive)
 115            {
 2169116                busesToProcess.Add(globalController.initBus);
 117            }
 118
 2173119            if (currentSceneControllerActive)
 120            {
 240121                busesToProcess.Add(currentSceneController.initBus);
 240122                busesToProcess.Add(currentSceneController.uiBus);
 240123                busesToProcess.Add(currentSceneController.systemBus);
 124            }
 125
 7276126            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
 7276134            for (int i = 0; i < sortedControllersCount; ++i)
 135            {
 1465136                MessagingController msgController = sortedControllers[i];
 1465137                busesToProcess.Add(msgController.systemBus);
 138            }
 139
 2173140            busesToProcessCount = busesToProcess.Count;
 2173141        }
 142
 143        public void Dispose()
 144        {
 687145            if (mainCoroutine != null)
 146            {
 666147                CoroutineStarter.Stop(mainCoroutine);
 666148                mainCoroutine = null;
 149            }
 150
 687151            using (var controllersIterator = messagingControllers.GetEnumerator())
 152            {
 2044153                while (controllersIterator.MoveNext())
 154                {
 1357155                    controllersIterator.Current.Value.Stop();
 1357156                    DisposeController(controllersIterator.Current.Value);
 157                }
 687158            }
 159
 687160            Environment.i.world.sceneController.OnSortScenes -= PopulateBusesToBeProcessed;
 161
 687162            messagingControllers.Clear();
 687163        }
 164
 705165        public bool ContainsController(string sceneId) { return messagingControllers.ContainsKey(sceneId); }
 166
 167        public void AddController(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = false)
 168        {
 705169            if (!messagingControllers.ContainsKey(sceneId))
 705170                messagingControllers[sceneId] = new MessagingController(messageHandler, sceneId);
 171
 705172            if (isGlobal && !string.IsNullOrEmpty(sceneId))
 173            {
 22174                messagingControllers.TryGetValue(sceneId, out MessagingController newGlobalSceneController);
 175
 22176                if (!globalSceneControllers.ContainsKey(sceneId))
 22177                    globalSceneControllers.Add(sceneId, newGlobalSceneController);
 178            }
 179
 705180            PopulateBusesToBeProcessed();
 705181        }
 182
 183        public void AddControllerIfNotExists(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = fals
 184        {
 705185            if (!ContainsController(sceneId))
 705186                AddController(messageHandler, sceneId, isGlobal);
 705187        }
 188
 189        public void RemoveController(string sceneId)
 190        {
 706191            if (messagingControllers.ContainsKey(sceneId))
 192            {
 193                // In case there is any pending message from a scene being unloaded we decrease the count accordingly
 14194                pendingMessagesCount -= messagingControllers[sceneId].messagingBuses[MessagingBusType.INIT].pendingMessa
 195                                        messagingControllers[sceneId].messagingBuses[MessagingBusType.UI].pendingMessage
 196                                        messagingControllers[sceneId].messagingBuses[MessagingBusType.SYSTEM].pendingMes
 197
 14198                DisposeController(messagingControllers[sceneId]);
 14199                messagingControllers.Remove(sceneId);
 200            }
 201
 706202            globalSceneControllers.Remove(sceneId);
 706203        }
 204
 205        void DisposeController(MessagingController controller)
 206        {
 1371207            controller.Stop();
 1371208            controller.Dispose();
 1371209        }
 210
 0211        public void Enqueue(bool isUiBus, QueuedSceneMessage_Scene queuedMessage) { messagingControllers[queuedMessage.s
 212
 54213        public void ForceEnqueueToGlobal(MessagingBusType busId, QueuedSceneMessage queuedMessage) { messagingController
 214
 215        public void SetSceneReady(string sceneId)
 216        {
 217            // Start processing SYSTEM queue
 665218            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            }
 665226        }
 227
 228        IEnumerator ProcessMessages()
 229        {
 10493230            while (true)
 231            {
 11159232                if (paused)
 233                {
 0234                    yield return null;
 0235                    continue;
 236                }
 237
 11159238                if (populateBusesDirty)
 239                {
 1468240                    PopulateBusesToBeProcessed();
 1468241                    populateBusesDirty = false;
 242                }
 243
 11159244                timeBudgetCounter = CommonScriptableObjects.rendererState.Get() ? MAX_GLOBAL_MSG_BUDGET : float.MaxValue
 245
 107864246                for (int i = 0; i < busesToProcessCount; ++i)
 247                {
 42773248                    MessagingBus bus = busesToProcess[i];
 249
 42773250                    if (ProcessBus(bus))
 251                        break;
 252                }
 253
 11159254                if (pendingInitMessagesCount == 0)
 255                {
 11159256                    UnityGLTF.GLTFSceneImporter.budgetPerFrameInMilliseconds = Mathf.Clamp(timeBudgetCounter, GLTF_BUDGE
 11159257                }
 258                else
 259                {
 0260                    UnityGLTF.GLTFSceneImporter.budgetPerFrameInMilliseconds = 0;
 261                }
 262
 11159263                yield return null;
 264            }
 265        }
 266
 267        bool ProcessBus(MessagingBus bus)
 268        {
 42773269            if (!bus.enabled || bus.pendingMessagesCount <= 0)
 42766270                return false;
 271
 7272            float startTime = Time.realtimeSinceStartup;
 273
 7274            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.
 7278            bus.ProcessQueue(timeBudget, out _);
 7279            RefreshControllerEnabledState(bus.owner);
 280
 7281            timeBudgetCounter -= Time.realtimeSinceStartup - startTime;
 282
 7283            if (timeBudgetCounter <= 0)
 0284                return true;
 285
 7286            return false;
 287        }
 288
 289        private void RefreshControllerEnabledState(MessagingController controller)
 290        {
 7291            if (controller == null || !controller.enabled)
 0292                return;
 293
 7294            if (controller.uiBus.pendingMessagesCount != 0)
 0295                return;
 7296            if (controller.initBus.pendingMessagesCount != 0)
 0297                return;
 7298            if (controller.systemBus.pendingMessagesCount != 0)
 0299                return;
 300
 7301            controller.enabled = false;
 7302        }
 303    }
 304}