< Summary

Class:DCL.FPSDisplay.FPSDisplay
Assembly:FPSDisplay
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/Debugging/FPSDisplay/FPSDisplay.cs
Covered lines:0
Uncovered lines:83
Coverable lines:83
Total lines:157
Line coverage:0% (0 of 83)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FPSDisplay()0%2100%
OnEnable()0%12300%
SetupRealm()0%2100%
SetupKernelConfig()0%2100%
UpdateRealm(...)0%12300%
OnKernelConfigChanged(...)0%2100%
OnKernelConfigChanged(...)0%6200%
OnOtherPlayersModified(...)0%2100%
OnDisable()0%2100%
UpdateLabelLoop()0%12300%
UpdateLabel()0%6200%
UpdateBackgroundSize()0%6200%
GetHexColor(...)0%2100%
GetColor(...)0%2100%
GetTitle(...)0%2100%
GetLine(...)0%2100%
GetEmptyLine()0%2100%

File(s)

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

#LineLine coverage
 1using System.Collections;
 2using DCL.Helpers;
 3using TMPro;
 4using UnityEngine;
 5using Variables.RealmsInfo;
 6
 7namespace DCL.FPSDisplay
 8{
 9    public class FPSDisplay : MonoBehaviour
 10    {
 11        private const float REFRESH_SECONDS = 0.1f;
 012        [SerializeField] private Vector2 labelPadding = new Vector2(12, 12);
 13
 14        [SerializeField] private TextMeshProUGUI label;
 15        [SerializeField] private RectTransform background;
 16        [SerializeField] private PerformanceMetricsDataVariable performanceData;
 17
 018        private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 19        private int lastPlayerCount;
 020        private CurrentRealmVariable currentRealm => DataStore.i.realm.playerRealm;
 21
 22        private Promise<KernelConfigModel> kernelConfigPromise;
 023        private string currentNetwork = string.Empty;
 024        private string currentRealmValue = string.Empty;
 25
 026        private Vector2 minSize = Vector2.zero;
 27
 28        private void OnEnable()
 29        {
 030            if (label == null || performanceData == null)
 031                return;
 32
 033            lastPlayerCount = otherPlayers.Count();
 034            otherPlayers.OnAdded += OnOtherPlayersModified;
 035            otherPlayers.OnRemoved += OnOtherPlayersModified;
 36
 037            SetupKernelConfig();
 038            SetupRealm();
 39
 040            StartCoroutine(UpdateLabelLoop());
 041        }
 42
 43        private void SetupRealm()
 44        {
 045            currentRealm.OnChange += UpdateRealm;
 046            UpdateRealm(currentRealm.Get(), null);
 047        }
 48
 49        private void SetupKernelConfig()
 50        {
 051            kernelConfigPromise = KernelConfig.i.EnsureConfigInitialized();
 052            kernelConfigPromise.Catch(Debug.Log);
 053            kernelConfigPromise.Then(OnKernelConfigChanged);
 054            KernelConfig.i.OnChange += OnKernelConfigChanged;
 055        }
 56
 57        private void UpdateRealm(CurrentRealmModel current, CurrentRealmModel previous)
 58        {
 059            if (current == null) return;
 060            currentRealmValue = current.serverName ?? string.Empty;
 061        }
 62
 063        private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { OnKernelConfigChange
 064        private void OnKernelConfigChanged(KernelConfigModel kernelConfig) { currentNetwork = kernelConfig.network ?? st
 65
 066        private void OnOtherPlayersModified(string playerName, Player player) { lastPlayerCount = otherPlayers.Count(); 
 67
 68        private void OnDisable()
 69        {
 070            otherPlayers.OnAdded -= OnOtherPlayersModified;
 071            otherPlayers.OnRemoved -= OnOtherPlayersModified;
 072            currentRealm.OnChange -= UpdateRealm;
 073            kernelConfigPromise.Dispose();
 074            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 75
 076            StopAllCoroutines();
 077        }
 78
 79        private IEnumerator UpdateLabelLoop()
 80        {
 081            while (true)
 82            {
 083                UpdateLabel();
 084                UpdateBackgroundSize();
 85
 086                yield return new WaitForSeconds(REFRESH_SECONDS);
 87            }
 88        }
 89
 90        private void UpdateLabel()
 91        {
 092            float dt = Time.unscaledDeltaTime;
 93
 094            float fps = performanceData.Get().fpsCount;
 95
 096            string fpsFormatted = fps.ToString("##");
 097            string msFormatted = (dt * 1000).ToString("##");
 98
 099            string targetText = string.Empty;
 100
 0101            string NO_DECIMALS = "##";
 0102            string TWO_DECIMALS = "##.00";
 103
 0104            Color fpsColor = FPSColoring.GetDisplayColor(fps);
 105
 0106            targetText += GetColor(GetHexColor(Color.white));
 0107            targetText += GetTitle("Skybox");
 0108            targetText += GetLine($"Config: {DataStore.i.skyboxConfig.configToLoad.Get()}");
 0109            targetText += GetLine($"Duration: {DataStore.i.skyboxConfig.lifecycleDuration.Get()}");
 0110            targetText += GetLine($"Game Time: {DataStore.i.skyboxConfig.currentVirtualTime.Get().ToString(TWO_DECIMALS)
 0111            targetText += GetLine($"UTC Time: {DataStore.i.worldTimer.GetCurrentTime().ToString()}");
 0112            targetText += GetEmptyLine();
 113
 0114            targetText += GetTitle("General");
 0115            targetText += GetLine($"Network: {currentNetwork.ToUpper()}");
 0116            targetText += GetLine($"Realm: {currentRealmValue.ToUpper()}");
 0117            targetText += GetLine($"Nearby players: {lastPlayerCount}");
 0118            targetText += GetEmptyLine();
 119
 0120            targetText += GetColor(GetHexColor(fpsColor));
 0121            targetText += GetTitle("FPS");
 0122            targetText += GetLine($"Hiccups in the last 1000 frames: {performanceData.Get().hiccupCount}");
 0123            targetText += GetLine($"Hiccup loss: {(100.0f * performanceData.Get().hiccupSum / performanceData.Get().tota
 0124            targetText += GetLine($"Bad Frames Percentile: {((performanceData.Get().hiccupCount) / 10.0f).ToString(NO_DE
 0125            targetText += GetLine($"Current {msFormatted} ms (fps: {fpsFormatted})");
 126
 0127            if (label.text != targetText)
 128            {
 0129                label.text = targetText;
 130            }
 0131        }
 132        private void UpdateBackgroundSize()
 133        {
 0134            var labelSize = label.GetPreferredValues();
 0135            var tempMinSize = labelSize + labelPadding;
 0136            tempMinSize.x = Mathf.Max(tempMinSize.x, minSize.x);
 0137            tempMinSize.y = Mathf.Max(tempMinSize.y, minSize.y);
 138
 0139            if (tempMinSize != minSize)
 140            {
 0141                background.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempMinSize.x);
 0142                background.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, tempMinSize.y);
 143
 0144                minSize = tempMinSize;
 145            }
 0146        }
 0147        private static string GetHexColor(Color color) { return $"#{ColorUtility.ToHtmlStringRGB(color)}"; }
 148
 0149        private string GetColor(string color) { return $"<color={color}>"; }
 150
 0151        private string GetTitle(string text) { return $"<voffset=0.1em><u><size=110%>{text}<size=100%></u></voffset><br>
 152
 0153        private string GetLine(string text) { return $"{text}<br>"; }
 154
 0155        private string GetEmptyLine() { return "<br>"; }
 156    }
 157}