mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-22 03:53:04 +00:00
rework Stats: they can output to StringBuilder and also use imGUI
This commit is contained in:
parent
b2495d3164
commit
b9b311e40e
6 changed files with 92 additions and 39 deletions
|
@ -16,15 +16,15 @@ namespace SanAndreasUnity.Stats
|
|||
|
||||
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;
|
||||
|
||||
var sb = new System.Text.StringBuilder();
|
||||
var sb = context.stringBuilder;
|
||||
|
||||
sb.AppendFormat("num peds: {0}\n", Ped.NumPeds);
|
||||
sb.AppendFormat("num vehicles: {0}\n", Vehicle.NumVehicles);
|
||||
|
@ -218,9 +218,6 @@ namespace SanAndreasUnity.Stats
|
|||
AppendStatsForBackgroundJobRunner(sb, PathfindingManager.Singleton.BackgroundJobRunner, "\t");
|
||||
sb.AppendLine();
|
||||
|
||||
|
||||
GUILayout.Label(sb.ToString());
|
||||
|
||||
}
|
||||
|
||||
private void AddNesting(System.Text.StringBuilder sb)
|
||||
|
|
|
@ -11,12 +11,12 @@ namespace SanAndreasUnity.Stats
|
|||
{
|
||||
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, "Local network time", NetworkTime.localTime);
|
||||
|
@ -42,8 +42,6 @@ namespace SanAndreasUnity.Stats
|
|||
}
|
||||
|
||||
sb.AppendLine($"Num spawned network objects: {NetManager.NumSpawnedNetworkObjects}");
|
||||
|
||||
GUILayout.Label(sb.ToString());
|
||||
}
|
||||
|
||||
private static void AddTimeSpan(System.Text.StringBuilder sb, string text, double seconds)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using SanAndreasUnity.Net;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SanAndreasUnity.Stats
|
||||
{
|
||||
|
@ -17,15 +18,18 @@ namespace SanAndreasUnity.Stats
|
|||
public const string ColumnNamesKey = "player_stats_column_names";
|
||||
public const string ColumnWidthsKey = "player_stats_column_widths";
|
||||
|
||||
private readonly List<string> m_textsForPlayer = new List<string>();
|
||||
|
||||
|
||||
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;
|
||||
|
||||
string[] dataKeys = SyncedServerData.Data.GetStringArray(ColumnDataKeysKey) ?? new string[0];
|
||||
|
@ -40,38 +44,66 @@ namespace SanAndreasUnity.Stats
|
|||
return;
|
||||
|
||||
// columns
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
if (context.isOnGui)
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
m_currentIndex = 0;
|
||||
for (int i=0; i < m_currentColumnNames.Length; i++)
|
||||
GUILayout.Button(m_currentColumnNames[i], GUILayout.Width(GetWidth()));
|
||||
GUILayout.EndHorizontal();
|
||||
{
|
||||
if (context.isOnGui)
|
||||
GUILayout.Button(m_currentColumnNames[i], GUILayout.Width(GetWidth()));
|
||||
else
|
||||
sb.Append(m_currentColumnNames[i].PadRight(GetWidthForText()));
|
||||
}
|
||||
|
||||
if (context.isOnGui)
|
||||
GUILayout.EndHorizontal();
|
||||
else
|
||||
sb.AppendLine();
|
||||
|
||||
foreach (var p in Player.AllPlayersEnumerable)
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
if (context.isOnGui)
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
m_currentIndex = 0;
|
||||
GUILayout.Label(p.PlayerName, GUILayout.Width(GetWidth()));
|
||||
GUILayout.Label(p.CachedIpAddress, GUILayout.Width(GetWidth()));
|
||||
GUILayout.Label(p.netId.ToString(), GUILayout.Width(GetWidth()));
|
||||
GUILayout.Label(p.OwnedPed != null ? p.OwnedPed.netId.ToString() : "", GUILayout.Width(GetWidth()));
|
||||
GUILayout.Label(p.OwnedPed != null && p.OwnedPed.PedDef != null ? p.OwnedPed.PedDef.ModelName : "", GUILayout.Width(GetWidth()));
|
||||
GUILayout.Label(p.OwnedPed != null && p.OwnedPed.CurrentState != null ? p.OwnedPed.CurrentState.GetType().Name : "", GUILayout.Width(GetWidth()));
|
||||
GUILayout.Label(p.OwnedPed != null ? p.OwnedPed.Health.ToString() : "", GUILayout.Width(GetWidth()));
|
||||
GUILayout.Label(p.OwnedPed != null && p.OwnedPed.CurrentWeapon != null && p.OwnedPed.CurrentWeapon.Definition != null ? p.OwnedPed.CurrentWeapon.Definition.ModelName : "", GUILayout.Width(GetWidth()));
|
||||
m_textsForPlayer.Clear();
|
||||
|
||||
m_textsForPlayer.Add(p.PlayerName);
|
||||
m_textsForPlayer.Add(p.CachedIpAddress);
|
||||
m_textsForPlayer.Add(p.netId.ToString());
|
||||
m_textsForPlayer.Add(p.OwnedPed != null ? p.OwnedPed.netId.ToString() : "");
|
||||
m_textsForPlayer.Add(p.OwnedPed != null && p.OwnedPed.PedDef != null ? p.OwnedPed.PedDef.ModelName : "");
|
||||
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)
|
||||
{
|
||||
string data = p.ExtraData.GetString(dataKey) ?? string.Empty;
|
||||
GUILayout.Label(data, GUILayout.Width(GetWidth()));
|
||||
m_textsForPlayer.Add(data);
|
||||
}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
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();
|
||||
else
|
||||
sb.AppendLine();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
float GetWidth() => m_currentWidths[m_currentIndex++];
|
||||
|
||||
int GetWidthForText() => Mathf.RoundToInt(this.GetWidth() / 3f);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,18 +7,14 @@ namespace SanAndreasUnity.Stats
|
|||
{
|
||||
public class WorldStats : MonoBehaviour
|
||||
{
|
||||
private readonly System.Text.StringBuilder _stringBuilder = new System.Text.StringBuilder();
|
||||
|
||||
|
||||
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;
|
||||
sb.Clear();
|
||||
var sb = context.stringBuilder;
|
||||
|
||||
var cell = Cell.Instance;
|
||||
|
||||
|
@ -75,7 +71,6 @@ namespace SanAndreasUnity.Stats
|
|||
sb.Append($"World not loaded\n");
|
||||
}
|
||||
|
||||
GUILayout.Label(sb.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace SanAndreasUnity.UI
|
|||
{
|
||||
int m_tabIndex = 0;
|
||||
Vector2 m_scrollViewPos = Vector2.zero;
|
||||
readonly Utilities.Stats.GetStatsContext m_statsContext = new Utilities.Stats.GetStatsContext(true);
|
||||
|
||||
|
||||
StatsWindow()
|
||||
|
@ -43,8 +44,11 @@ namespace SanAndreasUnity.UI
|
|||
{
|
||||
if (!string.IsNullOrEmpty(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();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SanAndreasUnity.Utilities
|
||||
{
|
||||
|
@ -9,7 +10,33 @@ namespace SanAndreasUnity.Utilities
|
|||
{
|
||||
public string category = "";
|
||||
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>>();
|
||||
|
|
Loading…
Reference in a new issue