rework Stats: they can output to StringBuilder and also use imGUI

This commit is contained in:
in0finite 2022-04-22 02:29:29 +02:00
parent b2495d3164
commit b9b311e40e
6 changed files with 92 additions and 39 deletions

View file

@ -16,15 +16,15 @@ namespace SanAndreasUnity.Stats
void Start() void Start()
{ {
Utilities.Stats.RegisterStat(new Utilities.Stats.Entry(){category = "MISC", onGUI = OnStatGUI}); Utilities.Stats.RegisterStat(new Utilities.Stats.Entry(){category = "MISC", getStatsAction = GetStats});
} }
void OnStatGUI() void GetStats(Utilities.Stats.GetStatsContext context)
{ {
m_nestingLevel = 0; m_nestingLevel = 0;
var sb = new System.Text.StringBuilder(); var sb = context.stringBuilder;
sb.AppendFormat("num peds: {0}\n", Ped.NumPeds); sb.AppendFormat("num peds: {0}\n", Ped.NumPeds);
sb.AppendFormat("num vehicles: {0}\n", Vehicle.NumVehicles); sb.AppendFormat("num vehicles: {0}\n", Vehicle.NumVehicles);
@ -218,9 +218,6 @@ namespace SanAndreasUnity.Stats
AppendStatsForBackgroundJobRunner(sb, PathfindingManager.Singleton.BackgroundJobRunner, "\t"); AppendStatsForBackgroundJobRunner(sb, PathfindingManager.Singleton.BackgroundJobRunner, "\t");
sb.AppendLine(); sb.AppendLine();
GUILayout.Label(sb.ToString());
} }
private void AddNesting(System.Text.StringBuilder sb) private void AddNesting(System.Text.StringBuilder sb)

View file

@ -11,12 +11,12 @@ namespace SanAndreasUnity.Stats
{ {
void Start() void Start()
{ {
Utilities.Stats.RegisterStat(new Utilities.Stats.Entry() { category = "NET", onGUI = OnStatGUI }); Utilities.Stats.RegisterStat(new Utilities.Stats.Entry() { category = "NET", getStatsAction = GetStats });
} }
void OnStatGUI() void GetStats(Utilities.Stats.GetStatsContext context)
{ {
var sb = new System.Text.StringBuilder(); var sb = context.stringBuilder;
AddTimeSpan(sb, "Network time", NetworkTime.time); AddTimeSpan(sb, "Network time", NetworkTime.time);
AddTimeSpan(sb, "Local network time", NetworkTime.localTime); AddTimeSpan(sb, "Local network time", NetworkTime.localTime);
@ -42,8 +42,6 @@ namespace SanAndreasUnity.Stats
} }
sb.AppendLine($"Num spawned network objects: {NetManager.NumSpawnedNetworkObjects}"); sb.AppendLine($"Num spawned network objects: {NetManager.NumSpawnedNetworkObjects}");
GUILayout.Label(sb.ToString());
} }
private static void AddTimeSpan(System.Text.StringBuilder sb, string text, double seconds) private static void AddTimeSpan(System.Text.StringBuilder sb, string text, double seconds)

View file

@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using SanAndreasUnity.Net; using SanAndreasUnity.Net;
using System.Collections.Generic;
namespace SanAndreasUnity.Stats namespace SanAndreasUnity.Stats
{ {
@ -17,15 +18,18 @@ namespace SanAndreasUnity.Stats
public const string ColumnNamesKey = "player_stats_column_names"; public const string ColumnNamesKey = "player_stats_column_names";
public const string ColumnWidthsKey = "player_stats_column_widths"; public const string ColumnWidthsKey = "player_stats_column_widths";
private readonly List<string> m_textsForPlayer = new List<string>();
void Start() void Start()
{ {
Utilities.Stats.RegisterStat(new Utilities.Stats.Entry(){category = "PLAYERS", onGUI = OnStatGUI}); Utilities.Stats.RegisterStat(new Utilities.Stats.Entry(){category = "PLAYERS", getStatsAction = GetStats});
} }
void OnStatGUI() void GetStats(Utilities.Stats.GetStatsContext context)
{ {
var sb = context.stringBuilder;
bool isServer = NetStatus.IsServer; bool isServer = NetStatus.IsServer;
string[] dataKeys = SyncedServerData.Data.GetStringArray(ColumnDataKeysKey) ?? new string[0]; string[] dataKeys = SyncedServerData.Data.GetStringArray(ColumnDataKeysKey) ?? new string[0];
@ -40,38 +44,66 @@ namespace SanAndreasUnity.Stats
return; return;
// columns // columns
if (context.isOnGui)
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
m_currentIndex = 0; m_currentIndex = 0;
for (int i=0; i < m_currentColumnNames.Length; i++) for (int i=0; i < m_currentColumnNames.Length; i++)
{
if (context.isOnGui)
GUILayout.Button(m_currentColumnNames[i], GUILayout.Width(GetWidth())); GUILayout.Button(m_currentColumnNames[i], GUILayout.Width(GetWidth()));
else
sb.Append(m_currentColumnNames[i].PadRight(GetWidthForText()));
}
if (context.isOnGui)
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
else
sb.AppendLine();
foreach (var p in Player.AllPlayersEnumerable) foreach (var p in Player.AllPlayersEnumerable)
{ {
if (context.isOnGui)
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
m_currentIndex = 0; m_currentIndex = 0;
GUILayout.Label(p.PlayerName, GUILayout.Width(GetWidth())); m_textsForPlayer.Clear();
GUILayout.Label(p.CachedIpAddress, GUILayout.Width(GetWidth()));
GUILayout.Label(p.netId.ToString(), GUILayout.Width(GetWidth())); m_textsForPlayer.Add(p.PlayerName);
GUILayout.Label(p.OwnedPed != null ? p.OwnedPed.netId.ToString() : "", GUILayout.Width(GetWidth())); m_textsForPlayer.Add(p.CachedIpAddress);
GUILayout.Label(p.OwnedPed != null && p.OwnedPed.PedDef != null ? p.OwnedPed.PedDef.ModelName : "", GUILayout.Width(GetWidth())); m_textsForPlayer.Add(p.netId.ToString());
GUILayout.Label(p.OwnedPed != null && p.OwnedPed.CurrentState != null ? p.OwnedPed.CurrentState.GetType().Name : "", GUILayout.Width(GetWidth())); m_textsForPlayer.Add(p.OwnedPed != null ? p.OwnedPed.netId.ToString() : "");
GUILayout.Label(p.OwnedPed != null ? p.OwnedPed.Health.ToString() : "", GUILayout.Width(GetWidth())); m_textsForPlayer.Add(p.OwnedPed != null && p.OwnedPed.PedDef != null ? p.OwnedPed.PedDef.ModelName : "");
GUILayout.Label(p.OwnedPed != null && p.OwnedPed.CurrentWeapon != null && p.OwnedPed.CurrentWeapon.Definition != null ? p.OwnedPed.CurrentWeapon.Definition.ModelName : "", GUILayout.Width(GetWidth())); m_textsForPlayer.Add(p.OwnedPed != null && p.OwnedPed.CurrentState != null ? p.OwnedPed.CurrentState.GetType().Name : "");
m_textsForPlayer.Add(p.OwnedPed != null ? p.OwnedPed.Health.ToString() : "");
m_textsForPlayer.Add(p.OwnedPed != null && p.OwnedPed.CurrentWeapon != null && p.OwnedPed.CurrentWeapon.Definition != null ? p.OwnedPed.CurrentWeapon.Definition.ModelName : "");
foreach (string dataKey in dataKeys) foreach (string dataKey in dataKeys)
{ {
string data = p.ExtraData.GetString(dataKey) ?? string.Empty; string data = p.ExtraData.GetString(dataKey) ?? string.Empty;
GUILayout.Label(data, GUILayout.Width(GetWidth())); m_textsForPlayer.Add(data);
} }
foreach (string text in m_textsForPlayer)
{
if (context.isOnGui)
GUILayout.Label(text, GUILayout.Width(GetWidth()));
else
sb.Append(text.PadRight(GetWidthForText()));
}
if (context.isOnGui)
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
else
sb.AppendLine();
} }
} }
float GetWidth() => m_currentWidths[m_currentIndex++]; float GetWidth() => m_currentWidths[m_currentIndex++];
int GetWidthForText() => Mathf.RoundToInt(this.GetWidth() / 3f);
} }
} }

View file

@ -7,18 +7,14 @@ namespace SanAndreasUnity.Stats
{ {
public class WorldStats : MonoBehaviour public class WorldStats : MonoBehaviour
{ {
private readonly System.Text.StringBuilder _stringBuilder = new System.Text.StringBuilder();
void Start() void Start()
{ {
Utilities.Stats.RegisterStat(new Utilities.Stats.Entry(){category = "WORLD", onGUI = OnStatGUI}); Utilities.Stats.RegisterStat(new Utilities.Stats.Entry(){ category = "WORLD", getStatsAction = GetStats });
} }
void OnStatGUI() void GetStats(Utilities.Stats.GetStatsContext context)
{ {
var sb = _stringBuilder; var sb = context.stringBuilder;
sb.Clear();
var cell = Cell.Instance; var cell = Cell.Instance;
@ -75,7 +71,6 @@ namespace SanAndreasUnity.Stats
sb.Append($"World not loaded\n"); sb.Append($"World not loaded\n");
} }
GUILayout.Label(sb.ToString());
} }
} }

View file

@ -10,6 +10,7 @@ namespace SanAndreasUnity.UI
{ {
int m_tabIndex = 0; int m_tabIndex = 0;
Vector2 m_scrollViewPos = Vector2.zero; Vector2 m_scrollViewPos = Vector2.zero;
readonly Utilities.Stats.GetStatsContext m_statsContext = new Utilities.Stats.GetStatsContext(true);
StatsWindow() StatsWindow()
@ -43,8 +44,11 @@ namespace SanAndreasUnity.UI
{ {
if (!string.IsNullOrEmpty(stat.text)) if (!string.IsNullOrEmpty(stat.text))
GUILayout.Label(stat.text); GUILayout.Label(stat.text);
if (stat.onGUI != null)
stat.onGUI(); m_statsContext.stringBuilder.Clear();
stat.getStatsAction?.Invoke(m_statsContext);
if (m_statsContext.stringBuilder.Length > 0)
GUILayout.Label(m_statsContext.stringBuilder.ToString());
} }
GUILayout.EndScrollView(); GUILayout.EndScrollView();
} }

View file

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
namespace SanAndreasUnity.Utilities namespace SanAndreasUnity.Utilities
{ {
@ -9,7 +10,33 @@ namespace SanAndreasUnity.Utilities
{ {
public string category = ""; public string category = "";
public string text = null; public string text = null;
public System.Action onGUI = null; public System.Action<GetStatsContext> getStatsAction = null;
}
public class GetStatsContext
{
/// <summary>
/// This is where the stats should be stored.
/// </summary>
public readonly StringBuilder stringBuilder = new StringBuilder();
/// <summary>
/// If true, stats can be drawn using imGui, for slightly nicer output.
/// </summary>
public readonly bool isOnGui = false;
public GetStatsContext()
{
}
public GetStatsContext(bool isOnGui)
{
this.isOnGui = isOnGui;
}
public void AppendLine(string text) => this.stringBuilder.AppendLine(text);
public void AppendLine() => this.stringBuilder.AppendLine();
public void Append(string text) => this.stringBuilder.Append(text);
} }
static Dictionary<string, List<Entry>> s_entries = new Dictionary<string, List<Entry>>(); static Dictionary<string, List<Entry>> s_entries = new Dictionary<string, List<Entry>>();