< Summary

Class:DCL.PreviewSceneLimitsWarning
Assembly:PreviewSceneLimitsWarning
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/Debugging/Panels/PreviewSceneLimitsWarning/PreviewSceneLimitsWarning.cs
Covered lines:56
Uncovered lines:17
Coverable lines:73
Total lines:177
Line coverage:76.7% (56 of 73)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PreviewSceneLimitsWarning()0%110100%
PreviewSceneLimitsWarning(...)0%110100%
Dispose()0%110100%
SetActive(...)0%550100%
StopChecking()0%110100%
OnKernelConfigChanged(...)0%220100%
UpdateRoutine()0%3.333066.67%
HandleWarningNotification()0%10100100%
IsLimitReached(...)0%20.649047.62%
ShowNotification(...)0%4.543044.44%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/Debugging/Panels/PreviewSceneLimitsWarning/PreviewSceneLimitsWarning.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Controllers;
 4using DCL.NotificationModel;
 5using UnityEngine;
 6using Type = DCL.NotificationModel.Type;
 7
 8namespace DCL
 9{
 10    public class PreviewSceneLimitsWarning : IDisposable
 11    {
 12        private const string NOTIFICATION_GROUP = "SceneLimitationExceeded";
 13        private const string NOTIFICATION_MESSAGE = "Scene's limits exceeded: {0}";
 14
 15        internal const float CHECK_INTERVAL = 0.2f;
 16
 17        private string sceneId;
 18        private Coroutine updateRoutine;
 19        private bool isActive = false;
 20
 21        internal bool isShowingNotification = false;
 22
 23        private readonly IWorldState worldState;
 24
 125        private static readonly Model limitReachedNotification = new Model()
 26        {
 27            type = Type.WARNING,
 28            groupID = NOTIFICATION_GROUP,
 29            message = NOTIFICATION_MESSAGE
 30        };
 31
 532        public PreviewSceneLimitsWarning(IWorldState worldState)
 33        {
 534            this.worldState = worldState;
 535        }
 36
 37        public void Dispose()
 38        {
 639            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 640            StopChecking();
 641        }
 42
 43        public void SetActive(bool active)
 44        {
 645            if (active && !isActive)
 46            {
 547                sceneId = KernelConfig.i.Get().debugConfig.sceneLimitsWarningSceneId;
 548                KernelConfig.i.OnChange += OnKernelConfigChanged;
 549                updateRoutine = CoroutineStarter.Start(UpdateRoutine());
 50            }
 51
 652            if (!active && isActive)
 53            {
 154                KernelConfig.i.OnChange -= OnKernelConfigChanged;
 155                StopChecking();
 56            }
 57
 658            isActive = active;
 659        }
 60
 61        private void StopChecking()
 62        {
 863            CoroutineStarter.Stop(updateRoutine);
 864            ShowNotification(false);
 865        }
 66
 67        internal void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous)
 68        {
 669            sceneId = current.debugConfig.sceneLimitsWarningSceneId;
 670            if (string.IsNullOrEmpty(sceneId))
 71            {
 172                StopChecking();
 73            }
 674        }
 75
 76        // NOTE: we are doing the check inside a coroutine since scene might be unloaded and loaded again
 77        private IEnumerator UpdateRoutine()
 78        {
 079            while (true)
 80            {
 581                HandleWarningNotification();
 582                yield return WaitForSecondsCache.Get(CHECK_INTERVAL);
 83            }
 84        }
 85
 86        internal void HandleWarningNotification()
 87        {
 1188            bool isLimitReached = false;
 1189            bool isLimitReachedAndMessageChanged = false;
 90
 1191            if (!string.IsNullOrEmpty(sceneId))
 92            {
 693                worldState.loadedScenes.TryGetValue(sceneId, out IParcelScene scene);
 694                ISceneMetricsCounter metricsController = scene?.metricsCounter;
 695                SceneMetricsModel currentMetrics = metricsController?.currentCount;
 696                SceneMetricsModel limit = metricsController?.maxCount;
 97
 698                string warningMessage = null;
 699                isLimitReached = IsLimitReached(currentMetrics, limit, ref warningMessage);
 6100                if (isLimitReached)
 101                {
 5102                    string prevMessage = limitReachedNotification.message;
 5103                    string newMessage = string.Format(NOTIFICATION_MESSAGE, warningMessage);
 5104                    limitReachedNotification.message = newMessage;
 5105                    isLimitReachedAndMessageChanged = prevMessage != newMessage;
 106                }
 107            }
 108
 11109            if (isShowingNotification != isLimitReached || isLimitReachedAndMessageChanged)
 110            {
 6111                ShowNotification(isLimitReached);
 112            }
 11113        }
 114
 115        private static bool IsLimitReached(SceneMetricsModel currentMetrics, SceneMetricsModel limit, ref string message
 116        {
 6117            if (currentMetrics == null || limit == null)
 0118                return false;
 119
 6120            if (currentMetrics.materials > limit.materials)
 121            {
 0122                message = $"Materials ({currentMetrics.materials}/{limit.materials})";
 0123                return true;
 124            }
 125
 6126            if (currentMetrics.triangles > limit.triangles)
 127            {
 0128                message = $"Triangles ({currentMetrics.triangles}/{limit.triangles})";
 0129                return true;
 130            }
 131
 6132            if (currentMetrics.meshes > limit.meshes)
 133            {
 0134                message = $"Meshes ({currentMetrics.meshes}/{limit.meshes})";
 0135                return true;
 136            }
 137
 6138            if (currentMetrics.entities > limit.entities)
 139            {
 5140                message = $"Entities ({currentMetrics.entities}/{limit.entities})";
 5141                return true;
 142            }
 143
 1144            if (currentMetrics.bodies > limit.bodies)
 145            {
 0146                message = $"Bodies ({currentMetrics.bodies}/{limit.bodies})";
 0147                return true;
 148            }
 149
 1150            if (currentMetrics.sceneHeight > limit.sceneHeight)
 151            {
 0152                message = $"Height ({currentMetrics.sceneHeight}/{limit.sceneHeight})";
 0153                return true;
 154            }
 155
 1156            return false;
 157        }
 158
 159        private void ShowNotification(bool show)
 160        {
 14161            isShowingNotification = show;
 162
 14163            var notificationsController = NotificationsController.i;
 164
 14165            if (notificationsController == null)
 14166                return;
 167
 0168            if (show)
 169            {
 0170                notificationsController.ShowNotification(limitReachedNotification);
 0171                return;
 172            }
 173
 0174            notificationsController.DismissAllNotifications(limitReachedNotification.groupID);
 0175        }
 176    }
 177}