< Summary

Class:DCL.FPSDisplay.FPSDisplay
Assembly:FPSDisplay
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/FPSDisplay/FPSDisplay.cs
Covered lines:0
Uncovered lines:86
Coverable lines:86
Total lines:188
Line coverage:0% (0 of 86)
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/DCL/WorldRuntime/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
 63        private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous)
 64        {
 065            OnKernelConfigChanged(current);
 066        }
 67
 68        private void OnKernelConfigChanged(KernelConfigModel kernelConfig)
 69        {
 070            currentNetwork = kernelConfig.network ?? string.Empty;
 071        }
 72
 73        private void OnOtherPlayersModified(string playerName, Player player)
 74        {
 075            lastPlayerCount = otherPlayers.Count();
 076        }
 77
 78        private void OnDisable()
 79        {
 080            otherPlayers.OnAdded -= OnOtherPlayersModified;
 081            otherPlayers.OnRemoved -= OnOtherPlayersModified;
 082            currentRealm.OnChange -= UpdateRealm;
 083            kernelConfigPromise.Dispose();
 084            KernelConfig.i.OnChange -= OnKernelConfigChanged;
 85
 086            StopAllCoroutines();
 087        }
 88
 89        private IEnumerator UpdateLabelLoop()
 90        {
 091            while (true)
 92            {
 093                UpdateLabel();
 094                UpdateBackgroundSize();
 95
 096                yield return new WaitForSeconds(REFRESH_SECONDS);
 97            }
 98        }
 99
 100        private void UpdateLabel()
 101        {
 0102            float dt = Time.unscaledDeltaTime;
 103
 0104            float fps = performanceData.Get().fpsCount;
 105
 0106            string fpsFormatted = fps.ToString("##");
 0107            string msFormatted = (dt * 1000).ToString("##");
 108
 0109            string targetText = string.Empty;
 110
 0111            string NO_DECIMALS = "##";
 0112            string TWO_DECIMALS = "##.00";
 113
 0114            Color fpsColor = FPSColoring.GetDisplayColor(fps);
 115
 0116            targetText += GetColor(GetHexColor(Color.white));
 0117            targetText += GetTitle("Skybox");
 0118            targetText += GetLine($"Config: {DataStore.i.skyboxConfig.configToLoad.Get()}");
 0119            targetText += GetLine($"Duration: {DataStore.i.skyboxConfig.lifecycleDuration.Get()}");
 0120            targetText +=
 121                GetLine($"Game Time: {DataStore.i.skyboxConfig.currentVirtualTime.Get().ToString(TWO_DECIMALS)}");
 0122            targetText += GetLine($"UTC Time: {DataStore.i.worldTimer.GetCurrentTime().ToString()}");
 0123            targetText += GetEmptyLine();
 124
 0125            targetText += GetTitle("General");
 0126            targetText += GetLine($"Network: {currentNetwork.ToUpper()}");
 0127            targetText += GetLine($"Realm: {currentRealmValue.ToUpper()}");
 0128            targetText += GetLine($"Nearby players: {lastPlayerCount}");
 0129            targetText += GetEmptyLine();
 130
 0131            targetText += GetColor(GetHexColor(fpsColor));
 0132            targetText += GetTitle("FPS");
 0133            targetText += GetLine($"Hiccups in the last 1000 frames: {performanceData.Get().hiccupCount}");
 0134            targetText += GetLine(
 135                $"Hiccup loss: {(100.0f * performanceData.Get().hiccupSum / performanceData.Get().totalSeconds).ToString
 0136            targetText +=
 137                GetLine(
 138                    $"Bad Frames Percentile: {((performanceData.Get().hiccupCount) / 10.0f).ToString(NO_DECIMALS)}%");
 0139            targetText += GetLine($"Current {msFormatted} ms (fps: {fpsFormatted})");
 140
 0141            if (label.text != targetText)
 142            {
 0143                label.text = targetText;
 144            }
 0145        }
 146
 147        private void UpdateBackgroundSize()
 148        {
 0149            var labelSize = label.GetPreferredValues();
 0150            var tempMinSize = labelSize + labelPadding;
 0151            tempMinSize.x = Mathf.Max(tempMinSize.x, minSize.x);
 0152            tempMinSize.y = Mathf.Max(tempMinSize.y, minSize.y);
 153
 0154            if (tempMinSize != minSize)
 155            {
 0156                background.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempMinSize.x);
 0157                background.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, tempMinSize.y);
 158
 0159                minSize = tempMinSize;
 160            }
 0161        }
 162
 163        private static string GetHexColor(Color color)
 164        {
 0165            return $"#{ColorUtility.ToHtmlStringRGB(color)}";
 166        }
 167
 168        private string GetColor(string color)
 169        {
 0170            return $"<color={color}>";
 171        }
 172
 173        private string GetTitle(string text)
 174        {
 0175            return $"<voffset=0.1em><u><size=110%>{text}<size=100%></u></voffset><br>";
 176        }
 177
 178        private string GetLine(string text)
 179        {
 0180            return $"{text}<br>";
 181        }
 182
 183        private string GetEmptyLine()
 184        {
 0185            return "<br>";
 186        }
 187    }
 188}