< 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
 66320        public Dictionary<string, MessagingController> messagingControllers { get; set; } = new Dictionary<string, Messa
 21
 22        private Coroutine mainCoroutine;
 23
 3024        public bool hasPendingMessages => pendingMessagesCount > 0;
 25
 66326        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
 66335        private Dictionary<string, MessagingController> globalSceneControllers = new Dictionary<string, MessagingControl
 66336        private List<MessagingController> sortedControllers = new List<MessagingController>();
 66337        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        {
 66346            messagingControllers[GLOBAL_MESSAGING_CONTROLLER] = new MessagingController(messageHandler, GLOBAL_MESSAGING
 47
 66348            if (!string.IsNullOrEmpty(GLOBAL_MESSAGING_CONTROLLER))
 66349                messagingControllers.TryGetValue(GLOBAL_MESSAGING_CONTROLLER, out globalController);
 50
 66351            Environment.i.world.sceneController.OnSortScenes += MarkBusesDirty;
 52
 66353            if (mainCoroutine == null)
 54            {
 66355                mainCoroutine = CoroutineStarter.Start(ProcessMessages());
 56            }
 66357        }
 58
 66359        bool populateBusesDirty = true;
 60
 168861        public void MarkBusesDirty() { populateBusesDirty = true; }
 62
 63        public void PopulateBusesToBeProcessed()
 64        {
 219565            IWorldState worldState = Environment.i.world.state;
 219566            string currentSceneId = worldState.currentSceneId;
 219567            List<IParcelScene> scenesSortedByDistance = worldState.scenesSortedByDistance;
 68
 219569            int count = scenesSortedByDistance.Count; // we need to retrieve list count everytime because it
 70            // may change after a yield return
 71
 219572            sortedControllers.Clear();
 73
 219574            if (!string.IsNullOrEmpty(currentSceneId) && messagingControllers.ContainsKey(currentSceneId))
 15075                currentSceneController = messagingControllers[currentSceneId];
 76
 767477            for (int i = 0; i < count; i++)
 78            {
 164279                string controllerId = scenesSortedByDistance[i].sceneData.id;
 80
 164281                if (controllerId != currentSceneId)
 82                {
 149283                    if (!messagingControllers.ContainsKey(controllerId))
 84                        continue;
 85
 149186                    sortedControllers.Add(messagingControllers[controllerId]);
 87                }
 88            }
 89
 219590            sortedControllersCount = sortedControllers.Count;
 91
 219592            bool globalSceneControllerActive = globalSceneControllers.Count > 0;
 219593            bool globalControllerActive = globalController != null && globalController.enabled;
 219594            bool currentSceneControllerActive = currentSceneController != null && currentSceneController.enabled;
 95
 219596            bool atLeastOneControllerShouldBeProcessed = globalSceneControllerActive || globalControllerActive || curren
 97
 219598            if (!atLeastOneControllerShouldBeProcessed)
 099                return;
 100
 2195101            busesToProcess.Clear();
 102            //-------------------------------------------------------------------------------------------
 103            // Global scenes
 2195104            using (var globalScenecontrollersIterator = globalSceneControllers.GetEnumerator())
 105            {
 2218106                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                }
 2195112            }
 113
 2195114            if (globalControllerActive)
 115            {
 2190116                busesToProcess.Add(globalController.initBus);
 117            }
 118
 2195119            if (currentSceneControllerActive)
 120            {
 251121                busesToProcess.Add(currentSceneController.initBus);
 251122                busesToProcess.Add(currentSceneController.uiBus);
 251123                busesToProcess.Add(currentSceneController.systemBus);
 124            }
 125
 7372126            for (int i = 0; i < sortedControllersCount; ++i)
 127            {
 1491128                MessagingController msgController = sortedControllers[i];
 129
 1491130                busesToProcess.Add(msgController.initBus);
 1491131                busesToProcess.Add(msgController.uiBus);
 132            }
 133
 7372134            for (int i = 0; i < sortedControllersCount; ++i)
 135            {
 1491136                MessagingController msgController = sortedControllers[i];
 1491137                busesToProcess.Add(msgController.systemBus);
 138            }
 139
 2195140            busesToProcessCount = busesToProcess.Count;
 2195141        }
 142
 143        public void Dispose()
 144        {
 683145            if (mainCoroutine != null)
 146            {
 663147                CoroutineStarter.Stop(mainCoroutine);
 663148                mainCoroutine = null;
 149            }
 150
 683151            using (var controllersIterator = messagingControllers.GetEnumerator())
 152            {
 2032153                while (controllersIterator.MoveNext())
 154                {
 1349155                    controllersIterator.Current.Value.Stop();
 1349156                    DisposeController(controllersIterator.Current.Value);
 157                }
 683158            }
 159
 683160            Environment.i.world.sceneController.OnSortScenes -= PopulateBusesToBeProcessed;
 161
 683162            messagingControllers.Clear();
 683163        }
 164
 701165        public bool ContainsController(string sceneId) { return messagingControllers.ContainsKey(sceneId); }
 166
 167        public void AddController(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = false)
 168        {
 701169            if (!messagingControllers.ContainsKey(sceneId))
 701170                messagingControllers[sceneId] = new MessagingController(messageHandler, sceneId);
 171
 701172            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
 701180            PopulateBusesToBeProcessed();
 701181        }
 182
 183        public void AddControllerIfNotExists(IMessageProcessHandler messageHandler, string sceneId, bool isGlobal = fals
 184        {
 701185            if (!ContainsController(sceneId))
 701186                AddController(messageHandler, sceneId, isGlobal);
 701187        }
 188
 189        public void RemoveController(string sceneId)
 190        {
 702191            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
 702202            globalSceneControllers.Remove(sceneId);
 702203        }
 204
 205        void DisposeController(MessagingController controller)
 206        {
 1364207            controller.Stop();
 1364208            controller.Dispose();
 1364209        }
 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
 661218            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            }
 661226        }
 227
 228        IEnumerator ProcessMessages()
 229        {
 7747230            while (true)
 231            {
 8410232                if (paused)
 233                {
 0234                    yield return null;
 0235                    continue;
 236                }
 237
 8410238                if (populateBusesDirty)
 239                {
 1494240                    PopulateBusesToBeProcessed();
 1494241                    populateBusesDirty = false;
 242                }
 243
 8410244                timeBudgetCounter = CommonScriptableObjects.rendererState.Get() ? MAX_GLOBAL_MSG_BUDGET : float.MaxValue
 245
 78764246                for (int i = 0; i < busesToProcessCount; ++i)
 247                {
 30972248                    MessagingBus bus = busesToProcess[i];
 249
 30972250                    if (ProcessBus(bus))
 251                        break;
 252                }
 253
 8410254                if (pendingInitMessagesCount == 0)
 255                {
 8410256                    UnityGLTF.GLTFSceneImporter.budgetPerFrameInMilliseconds = Mathf.Clamp(timeBudgetCounter, GLTF_BUDGE
 8410257                }
 258                else
 259                {
 0260                    UnityGLTF.GLTFSceneImporter.budgetPerFrameInMilliseconds = 0;
 261                }
 262
 8410263                yield return null;
 264            }
 265        }
 266
 267        bool ProcessBus(MessagingBus bus)
 268        {
 30972269            if (!bus.enabled || bus.pendingMessagesCount <= 0)
 30964270                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}