< Summary

Class:DCL.PreviewSceneLimitsWarning
Assembly:PreviewSceneLimitsWarning
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/Debugging/Panels/PreviewSceneLimitsWarning/PreviewSceneLimitsWarning.cs
Covered lines:60
Uncovered lines:13
Coverable lines:73
Total lines:174
Line coverage:82.1% (60 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%3.013088.89%

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            }
 651            if (!active && isActive)
 52            {
 153                KernelConfig.i.OnChange -= OnKernelConfigChanged;
 154                StopChecking();
 55            }
 656            isActive = active;
 657        }
 58
 59        private void StopChecking()
 60        {
 861            CoroutineStarter.Stop(updateRoutine);
 862            ShowNotification(false);
 863        }
 64
 65        internal void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous)
 66        {
 667            sceneId = current.debugConfig.sceneLimitsWarningSceneId;
 668            if (string.IsNullOrEmpty(sceneId))
 69            {
 170                StopChecking();
 71            }
 672        }
 73
 74        // NOTE: we are doing the check inside a coroutine since scene might be unloaded and loaded again
 75        private IEnumerator UpdateRoutine()
 76        {
 077            while (true)
 78            {
 579                HandleWarningNotification();
 580                yield return WaitForSecondsCache.Get(CHECK_INTERVAL);
 81            }
 82        }
 83
 84        internal void HandleWarningNotification()
 85        {
 1186            bool isLimitReached = false;
 1187            bool isLimitReachedAndMessageChanged = false;
 88
 1189            if (!string.IsNullOrEmpty(sceneId))
 90            {
 691                worldState.loadedScenes.TryGetValue(sceneId, out IParcelScene scene);
 692                ISceneMetricsCounter metricsController = scene?.metricsCounter;
 693                SceneMetricsModel currentMetrics = metricsController?.GetModel();
 694                SceneMetricsModel limit = metricsController?.GetLimits();
 95
 696                string warningMessage = null;
 697                isLimitReached = IsLimitReached(currentMetrics, limit, ref warningMessage);
 698                if (isLimitReached)
 99                {
 5100                    string prevMessage = limitReachedNotification.message;
 5101                    string newMessage = string.Format(NOTIFICATION_MESSAGE, warningMessage);
 5102                    limitReachedNotification.message = newMessage;
 5103                    isLimitReachedAndMessageChanged = prevMessage != newMessage;
 104                }
 105            }
 106
 11107            if (isShowingNotification != isLimitReached || isLimitReachedAndMessageChanged)
 108            {
 6109                ShowNotification(isLimitReached);
 110            }
 11111        }
 112
 113        private static bool IsLimitReached(SceneMetricsModel currentMetrics, SceneMetricsModel limit, ref string message
 114        {
 6115            if (currentMetrics == null || limit == null)
 0116                return false;
 117
 6118            if (currentMetrics.materials > limit.materials)
 119            {
 0120                message = $"Materials ({currentMetrics.materials}/{limit.materials})";
 0121                return true;
 122            }
 123
 6124            if (currentMetrics.triangles > limit.triangles)
 125            {
 0126                message = $"Triangles ({currentMetrics.triangles}/{limit.triangles})";
 0127                return true;
 128            }
 129
 6130            if (currentMetrics.meshes > limit.meshes)
 131            {
 0132                message = $"Meshes ({currentMetrics.meshes}/{limit.meshes})";
 0133                return true;
 134            }
 135
 6136            if (currentMetrics.entities > limit.entities)
 137            {
 5138                message = $"Entities ({currentMetrics.entities}/{limit.entities})";
 5139                return true;
 140            }
 141
 1142            if (currentMetrics.bodies > limit.bodies)
 143            {
 0144                message = $"Bodies ({currentMetrics.bodies}/{limit.bodies})";
 0145                return true;
 146            }
 147
 1148            if (currentMetrics.sceneHeight > limit.sceneHeight)
 149            {
 0150                message = $"Height ({currentMetrics.sceneHeight}/{limit.sceneHeight})";
 0151                return true;
 152            }
 153
 1154            return false;
 155        }
 156
 157        private void ShowNotification(bool show)
 158        {
 14159            isShowingNotification = show;
 160
 14161            var notificationsController = NotificationsController.i;
 162
 14163            if (notificationsController == null)
 0164                return;
 165
 14166            if (show)
 167            {
 5168                notificationsController.ShowNotification(limitReachedNotification);
 5169                return;
 170            }
 9171            notificationsController.DismissAllNotifications(limitReachedNotification.groupID);
 9172        }
 173    }
 174}