| | 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 | |
|
| 0 | 63 | | private void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { OnKernelConfigChange |
| 0 | 64 | | private void OnKernelConfigChanged(KernelConfigModel kernelConfig) { currentNetwork = kernelConfig.network ?? st |
| | 65 | |
|
| 0 | 66 | | private void OnOtherPlayersModified(string playerName, Player player) { lastPlayerCount = otherPlayers.Count(); |
| | 67 | |
|
| | 68 | | private void OnDisable() |
| | 69 | | { |
| 0 | 70 | | otherPlayers.OnAdded -= OnOtherPlayersModified; |
| 0 | 71 | | otherPlayers.OnRemoved -= OnOtherPlayersModified; |
| 0 | 72 | | currentRealm.OnChange -= UpdateRealm; |
| 0 | 73 | | kernelConfigPromise.Dispose(); |
| 0 | 74 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| | 75 | |
|
| 0 | 76 | | StopAllCoroutines(); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | private IEnumerator UpdateLabelLoop() |
| | 80 | | { |
| 0 | 81 | | while (true) |
| | 82 | | { |
| 0 | 83 | | UpdateLabel(); |
| 0 | 84 | | UpdateBackgroundSize(); |
| | 85 | |
|
| 0 | 86 | | yield return new WaitForSeconds(REFRESH_SECONDS); |
| | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private void UpdateLabel() |
| | 91 | | { |
| 0 | 92 | | float dt = Time.unscaledDeltaTime; |
| | 93 | |
|
| 0 | 94 | | float fps = performanceData.Get().fpsCount; |
| | 95 | |
|
| 0 | 96 | | string fpsFormatted = fps.ToString("##"); |
| 0 | 97 | | string msFormatted = (dt * 1000).ToString("##"); |
| | 98 | |
|
| 0 | 99 | | string targetText = string.Empty; |
| | 100 | |
|
| 0 | 101 | | string NO_DECIMALS = "##"; |
| 0 | 102 | | string TWO_DECIMALS = "##.00"; |
| | 103 | |
|
| 0 | 104 | | Color fpsColor = FPSColoring.GetDisplayColor(fps); |
| | 105 | |
|
| 0 | 106 | | targetText += GetColor(GetHexColor(Color.white)); |
| 0 | 107 | | targetText += GetTitle("Skybox"); |
| 0 | 108 | | targetText += GetLine($"Config: {DataStore.i.skyboxConfig.configToLoad.Get()}"); |
| 0 | 109 | | targetText += GetLine($"Duration: {DataStore.i.skyboxConfig.lifecycleDuration.Get()}"); |
| 0 | 110 | | targetText += GetLine($"Game Time: {DataStore.i.skyboxConfig.currentVirtualTime.Get().ToString(TWO_DECIMALS) |
| 0 | 111 | | targetText += GetLine($"UTC Time: {DataStore.i.worldTimer.GetCurrentTime().ToString()}"); |
| 0 | 112 | | targetText += GetEmptyLine(); |
| | 113 | |
|
| 0 | 114 | | targetText += GetTitle("General"); |
| 0 | 115 | | targetText += GetLine($"Network: {currentNetwork.ToUpper()}"); |
| 0 | 116 | | targetText += GetLine($"Realm: {currentRealmValue.ToUpper()}"); |
| 0 | 117 | | targetText += GetLine($"Nearby players: {lastPlayerCount}"); |
| 0 | 118 | | targetText += GetEmptyLine(); |
| | 119 | |
|
| 0 | 120 | | targetText += GetColor(GetHexColor(fpsColor)); |
| 0 | 121 | | targetText += GetTitle("FPS"); |
| 0 | 122 | | targetText += GetLine($"Hiccups in the last 1000 frames: {performanceData.Get().hiccupCount}"); |
| 0 | 123 | | targetText += GetLine($"Hiccup loss: {(100.0f * performanceData.Get().hiccupSum / performanceData.Get().tota |
| 0 | 124 | | targetText += GetLine($"Bad Frames Percentile: {((performanceData.Get().hiccupCount) / 10.0f).ToString(NO_DE |
| 0 | 125 | | targetText += GetLine($"Current {msFormatted} ms (fps: {fpsFormatted})"); |
| | 126 | |
|
| 0 | 127 | | if (label.text != targetText) |
| | 128 | | { |
| 0 | 129 | | label.text = targetText; |
| | 130 | | } |
| 0 | 131 | | } |
| | 132 | | private void UpdateBackgroundSize() |
| | 133 | | { |
| 0 | 134 | | var labelSize = label.GetPreferredValues(); |
| 0 | 135 | | var tempMinSize = labelSize + labelPadding; |
| 0 | 136 | | tempMinSize.x = Mathf.Max(tempMinSize.x, minSize.x); |
| 0 | 137 | | tempMinSize.y = Mathf.Max(tempMinSize.y, minSize.y); |
| | 138 | |
|
| 0 | 139 | | if (tempMinSize != minSize) |
| | 140 | | { |
| 0 | 141 | | background.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempMinSize.x); |
| 0 | 142 | | background.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, tempMinSize.y); |
| | 143 | |
|
| 0 | 144 | | minSize = tempMinSize; |
| | 145 | | } |
| 0 | 146 | | } |
| 0 | 147 | | private static string GetHexColor(Color color) { return $"#{ColorUtility.ToHtmlStringRGB(color)}"; } |
| | 148 | |
|
| 0 | 149 | | private string GetColor(string color) { return $"<color={color}>"; } |
| | 150 | |
|
| 0 | 151 | | private string GetTitle(string text) { return $"<voffset=0.1em><u><size=110%>{text}<size=100%></u></voffset><br> |
| | 152 | |
|
| 0 | 153 | | private string GetLine(string text) { return $"{text}<br>"; } |
| | 154 | |
|
| 0 | 155 | | private string GetEmptyLine() { return "<br>"; } |
| | 156 | | } |
| | 157 | | } |