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