< 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
 62920        public Dictionary<string, MessagingController> messagingControllers { get; set; } = new Dictionary<string, Messa
 21
 22        private Coroutine mainCoroutine;
 23
 2824        public bool hasPendingMessages => pendingMessagesCount > 0;
 25
 62926        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
 62935        private Dictionary<string, MessagingController> globalSceneControllers = new Dictionary<string, MessagingControl
 62936        private List<MessagingController> sortedControllers = new List<MessagingController>();
 62937        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
 62946        public MessagingControllersManager (IMessageProcessHandler messageHandler = null)
 47        {
 62948            this.messageHandler = messageHandler;
 62949        }
 50
 51        public void Initialize()
 52        {
 62953            if ( messageHandler == null )
 62954                messageHandler = Environment.i.world.sceneController;
 55
 62956            messagingControllers[GLOBAL_MESSAGING_CONTROLLER] = new MessagingController(this, messageHandler, GLOBAL_MES
 57
 62958            if (!string.IsNullOrEmpty(GLOBAL_MESSAGING_CONTROLLER))
 62959                messagingControllers.TryGetValue(GLOBAL_MESSAGING_CONTROLLER, out globalController);
 60
 62961            Environment.i.world.sceneController.OnSortScenes += MarkBusesDirty;
 62
 62963            if (mainCoroutine == null)
 64            {
 62965                mainCoroutine = CoroutineStarter.Start(ProcessMessages());
 66            }
 62967        }
 68
 62969        bool populateBusesDirty = true;
 70
 122471        public void MarkBusesDirty() { populateBusesDirty = true; }
 72
 73        public void PopulateBusesToBeProcessed()
 74        {
 124675            IWorldState worldState = Environment.i.world.state;
 124676            string currentSceneId = worldState.currentSceneId;
 124677            List<IParcelScene> scenesSortedByDistance = worldState.scenesSortedByDistance;
 78
 124679            int count = scenesSortedByDistance.Count; // we need to retrieve list count everytime because it
 80            // may change after a yield return
 81
 124682            sortedControllers.Clear();
 83
 124684            if (!string.IsNullOrEmpty(currentSceneId) && messagingControllers.ContainsKey(currentSceneId))
 085                currentSceneController = messagingControllers[currentSceneId];
 86
 287887            for (int i = 0; i < count; i++)
 88            {
 19389                string controllerId = scenesSortedByDistance[i].sceneData.id;
 90
 19391                if (controllerId != currentSceneId)
 92                {
 18593                    if (!messagingControllers.ContainsKey(controllerId))
 94                        continue;
 95
 15596                    sortedControllers.Add(messagingControllers[controllerId]);
 97                }
 98            }
 99
 1246100            sortedControllersCount = sortedControllers.Count;
 101
 1246102            bool globalSceneControllerActive = globalSceneControllers.Count > 0;
 1246103            bool globalControllerActive = globalController != null && globalController.enabled;
 1246104            bool currentSceneControllerActive = currentSceneController != null && currentSceneController.enabled;
 105
 1246106            bool atLeastOneControllerShouldBeProcessed = globalSceneControllerActive || globalControllerActive || curren
 107
 1246108            if (!atLeastOneControllerShouldBeProcessed)
 0109                return;
 110
 1246111            busesToProcess.Clear();
 112            //-------------------------------------------------------------------------------------------
 113            // Global scenes
 1246114            using (var globalScenecontrollersIterator = globalSceneControllers.GetEnumerator())
 115            {
 1248116                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                }
 1246122            }
 123
 1246124            if (globalControllerActive)
 125            {
 1243126                busesToProcess.Add(globalController.initBus);
 127            }
 128
 1246129            if (currentSceneControllerActive)
 130            {
 0131                busesToProcess.Add(currentSceneController.initBus);
 0132                busesToProcess.Add(currentSceneController.uiBus);
 0133                busesToProcess.Add(currentSceneController.systemBus);
 134            }
 135
 2802136            for (int i = 0; i < sortedControllersCount; ++i)
 137            {
 155138                MessagingController msgController = sortedControllers[i];
 139
 155140                busesToProcess.Add(msgController.initBus);
 155141                busesToProcess.Add(msgController.uiBus);
 142            }
 143
 2802144            for (int i = 0; i < sortedControllersCount; ++i)
 145            {
 155146                MessagingController msgController = sortedControllers[i];
 155147                busesToProcess.Add(msgController.systemBus);
 148            }
 149
 1246150            busesToProcessCount = busesToProcess.Count;
 1246151        }
 152
 153        public void Dispose()
 154        {
 629155            if (mainCoroutine != null)
 156            {
 629157                CoroutineStarter.Stop(mainCoroutine);
 629158                mainCoroutine = null;
 159            }
 160
 629161            using (var controllersIterator = messagingControllers.GetEnumerator())
 162            {
 1258163                while (controllersIterator.MoveNext())
 164                {
 629165                    controllersIterator.Current.Value.Stop();
 629166                    DisposeController(controllersIterator.Current.Value);
 167                }
 629168            }
 169
 629170            Environment.i.world.sceneController.OnSortScenes -= PopulateBusesToBeProcessed;
 171
 629172            messagingControllers.Clear();
 629173        }
 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        {
 384201            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
 384212            globalSceneControllers.Remove(sceneId);
 384213        }
 214
 215        void DisposeController(MessagingController controller)
 216        {
 656217            controller.Stop();
 656218            controller.Dispose();
 656219        }
 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
 368228            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            }
 368236        }
 237
 238        IEnumerator ProcessMessages()
 239        {
 6940240            while (true)
 241            {
 7569242                if (paused)
 243                {
 0244                    yield return null;
 0245                    continue;
 246                }
 247
 7569248                if (populateBusesDirty)
 249                {
 1219250                    PopulateBusesToBeProcessed();
 1219251                    populateBusesDirty = false;
 252                }
 253
 7569254                timeBudgetCounter = CommonScriptableObjects.rendererState.Get() ? MAX_GLOBAL_MSG_BUDGET : float.MaxValue
 255
 30550256                for (int i = 0; i < busesToProcessCount; ++i)
 257                {
 7708258                    MessagingBus bus = busesToProcess[i];
 259
 7708260                    if (ProcessBus(bus))
 261                        break;
 262                }
 263
 7569264                if (pendingInitMessagesCount == 0)
 265                {
 7567266                    AssetPromiseKeeper_GLTF.i.throttlingCounter.budgetPerFrameInMilliseconds = Mathf.Clamp(timeBudgetCou
 7567267                }
 268                else
 269                {
 2270                    AssetPromiseKeeper_GLTF.i.throttlingCounter.budgetPerFrameInMilliseconds = 0;
 271                }
 272
 7569273                yield return null;
 274            }
 275        }
 276
 277        bool ProcessBus(MessagingBus bus)
 278        {
 7708279            if (!bus.enabled || bus.pendingMessagesCount <= 0)
 7699280                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}