| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using Variables.RealmsInfo; |
| | 7 | |
|
| | 8 | | namespace DCL.FPSDisplay |
| | 9 | | { |
| | 10 | | public class FPSDisplay : MonoBehaviour |
| | 11 | | { |
| | 12 | | private const float REFRESH_SECONDS = 0.1f; |
| 0 | 13 | | [SerializeField] private Vector2 labelPadding = new Vector2(12, 12); |
| | 14 | |
|
| | 15 | | [SerializeField] private TextMeshProUGUI label; |
| | 16 | | [SerializeField] private RectTransform background; |
| | 17 | | [SerializeField] private PerformanceMetricsDataVariable performanceData; |
| | 18 | |
|
| 0 | 19 | | public string versions = "Kernel: Unity renderer:"; |
| | 20 | |
|
| 0 | 21 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 22 | | private int lastPlayerCount; |
| 0 | 23 | | private CurrentRealmVariable currentRealm => DataStore.i.realm.playerRealm; |
| | 24 | |
|
| | 25 | | private Promise<KernelConfigModel> kernelConfigPromise; |
| 0 | 26 | | private string currentNetwork = string.Empty; |
| 0 | 27 | | private string currentRealmValue = string.Empty; |
| | 28 | |
|
| 0 | 29 | | private Vector2 minSize = Vector2.zero; |
| | 30 | |
|
| | 31 | | private void OnEnable() |
| | 32 | | { |
| 0 | 33 | | if (label == null || performanceData == null) |
| 0 | 34 | | return; |
| | 35 | |
|
| 0 | 36 | | lastPlayerCount = otherPlayers.Count(); |
| 0 | 37 | | otherPlayers.OnAdded += OnOtherPlayersModified; |
| 0 | 38 | | otherPlayers.OnRemoved += OnOtherPlayersModified; |
| 0 | 39 | | DataStore.i.debugConfig.versions.OnChange += SetVersion; |
| | 40 | |
|
| 0 | 41 | | SetupKernelConfig(); |
| 0 | 42 | | SetupRealm(); |
| | 43 | |
|
| 0 | 44 | | StartCoroutine(UpdateLabelLoop()); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | private void SetupRealm() |
| | 48 | | { |
| 0 | 49 | | currentRealm.OnChange += UpdateRealm; |
| 0 | 50 | | UpdateRealm(currentRealm.Get(), null); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private void SetupKernelConfig() |
| | 54 | | { |
| 0 | 55 | | kernelConfigPromise = KernelConfig.i.EnsureConfigInitialized(); |
| 0 | 56 | | kernelConfigPromise.Catch(Debug.Log); |
| 0 | 57 | | kernelConfigPromise.Then(OnKernelConfigChanged); |
| 0 | 58 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void UpdateRealm(CurrentRealmModel current, CurrentRealmModel previous) |
| | 62 | | { |
| 0 | 63 | | if (current == null) return; |
| 0 | 64 | | currentRealmValue = current.serverName ?? string.Empty; |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { OnKernelConfigChange |
| 0 | 68 | | private void OnKernelConfigChanged(KernelConfigModel kernelConfig) { currentNetwork = kernelConfig.network ?? st |
| | 69 | |
|
| 0 | 70 | | private void OnOtherPlayersModified(string playerName, Player player) { lastPlayerCount = otherPlayers.Count(); |
| | 71 | |
|
| | 72 | | private void SetVersion(string current, string previous) |
| | 73 | | { |
| 0 | 74 | | versions = current; |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | private void OnDisable() |
| | 78 | | { |
| 0 | 79 | | otherPlayers.OnAdded -= OnOtherPlayersModified; |
| 0 | 80 | | otherPlayers.OnRemoved -= OnOtherPlayersModified; |
| 0 | 81 | | currentRealm.OnChange -= UpdateRealm; |
| 0 | 82 | | kernelConfigPromise.Dispose(); |
| 0 | 83 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 0 | 84 | | DataStore.i.debugConfig.versions.OnChange -= SetVersion; |
| 0 | 85 | | StopAllCoroutines(); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | private IEnumerator UpdateLabelLoop() |
| | 89 | | { |
| 0 | 90 | | while (true) |
| | 91 | | { |
| 0 | 92 | | UpdateLabel(); |
| 0 | 93 | | UpdateBackgroundSize(); |
| | 94 | |
|
| 0 | 95 | | yield return new WaitForSeconds(REFRESH_SECONDS); |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| | 99 | | private void UpdateLabel() |
| | 100 | | { |
| 0 | 101 | | float dt = Time.unscaledDeltaTime; |
| | 102 | |
|
| 0 | 103 | | float fps = performanceData.Get().fpsCount; |
| | 104 | |
|
| 0 | 105 | | string fpsFormatted = fps.ToString("##"); |
| 0 | 106 | | string msFormatted = (dt * 1000).ToString("##"); |
| | 107 | |
|
| 0 | 108 | | string targetText = string.Empty; |
| | 109 | |
|
| 0 | 110 | | string NO_DECIMALS = "##"; |
| 0 | 111 | | string TWO_DECIMALS = "##.00"; |
| | 112 | |
|
| 0 | 113 | | Color fpsColor = FPSColoring.GetDisplayColor(fps); |
| | 114 | |
|
| 0 | 115 | | targetText += GetColor(GetHexColor(Color.white)); |
| 0 | 116 | | targetText += GetTitle("Version"); |
| 0 | 117 | | targetText += GetLine($"{versions}"); |
| 0 | 118 | | targetText += GetTitle("Skybox"); |
| 0 | 119 | | targetText += GetLine($"Config: {DataStore.i.skyboxConfig.configToLoad.Get()}"); |
| 0 | 120 | | targetText += GetLine($"Duration: {DataStore.i.skyboxConfig.lifecycleDuration.Get()}"); |
| 0 | 121 | | targetText += GetLine($"Game Time: {DataStore.i.skyboxConfig.currentVirtualTime.Get().ToString(TWO_DECIMALS) |
| 0 | 122 | | targetText += GetLine($"UTC Time: {DataStore.i.worldTimer.GetCurrentTime().ToString()}"); |
| 0 | 123 | | targetText += GetEmptyLine(); |
| | 124 | |
|
| 0 | 125 | | targetText += GetTitle("General"); |
| 0 | 126 | | targetText += GetLine($"Network: {currentNetwork.ToUpper()}"); |
| 0 | 127 | | targetText += GetLine($"Realm: {currentRealmValue.ToUpper()}"); |
| 0 | 128 | | targetText += GetLine($"Nearby players: {lastPlayerCount}"); |
| 0 | 129 | | targetText += GetEmptyLine(); |
| | 130 | |
|
| 0 | 131 | | targetText += GetColor(GetHexColor(fpsColor)); |
| 0 | 132 | | targetText += GetTitle("FPS"); |
| 0 | 133 | | targetText += GetLine($"Hiccups in the last 1000 frames: {performanceData.Get().hiccupCount}"); |
| 0 | 134 | | targetText += GetLine($"Hiccup loss: {(100.0f * performanceData.Get().hiccupSum / performanceData.Get().tota |
| 0 | 135 | | targetText += GetLine($"Bad Frames Percentile: {((performanceData.Get().hiccupCount) / 10.0f).ToString(NO_DE |
| 0 | 136 | | targetText += GetLine($"Current {msFormatted} ms (fps: {fpsFormatted})"); |
| | 137 | |
|
| 0 | 138 | | if (label.text != targetText) |
| | 139 | | { |
| 0 | 140 | | label.text = targetText; |
| | 141 | | } |
| 0 | 142 | | } |
| | 143 | | private void UpdateBackgroundSize() |
| | 144 | | { |
| 0 | 145 | | var labelSize = label.GetPreferredValues(); |
| 0 | 146 | | var tempMinSize = labelSize + labelPadding; |
| 0 | 147 | | tempMinSize.x = Mathf.Max(tempMinSize.x, minSize.x); |
| 0 | 148 | | tempMinSize.y = Mathf.Max(tempMinSize.y, minSize.y); |
| | 149 | |
|
| 0 | 150 | | if (tempMinSize != minSize) |
| | 151 | | { |
| 0 | 152 | | background.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempMinSize.x); |
| 0 | 153 | | background.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, tempMinSize.y); |
| | 154 | |
|
| 0 | 155 | | minSize = tempMinSize; |
| | 156 | | } |
| 0 | 157 | | } |
| 0 | 158 | | private static string GetHexColor(Color color) { return $"#{ColorUtility.ToHtmlStringRGB(color)}"; } |
| | 159 | |
|
| 0 | 160 | | private string GetColor(string color) { return $"<color={color}>"; } |
| | 161 | |
|
| 0 | 162 | | private string GetTitle(string text) { return $"<voffset=0.1em><u><size=110%>{text}<size=100%></u></voffset><br> |
| | 163 | |
|
| 0 | 164 | | private string GetLine(string text) { return $"{text}<br>"; } |
| | 165 | |
|
| 0 | 166 | | private string GetEmptyLine() { return "<br>"; } |
| | 167 | | } |
| | 168 | | } |