Skip to content

Commit

Permalink
Added status command
Browse files Browse the repository at this point in the history
  • Loading branch information
features-not-bugs committed Feb 19, 2021
1 parent 9d1d210 commit 55509e3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/RustServerMetrics/Harmony/TimeWarning/Dispose_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace RustServerMetrics.Harmony.TimeWarning
[HarmonyPatch(typeof(global::TimeWarning), nameof(global::TimeWarning.Dispose))]
public static class Dispose_Patch
{
/// <summary>
/// Disabled as this patch doesn't apply with Harmony v1, will enable with Harmony v2 upgrade
/// </summary>
[HarmonyPrepare]
public static bool Prepare(HarmonyInstance harmonyInstance) => false;

[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> originalInstructions)
{
Expand Down
6 changes: 6 additions & 0 deletions src/RustServerMetrics/Harmony/TimeWarning/New_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace RustServerMetrics.Harmony.TimeWarning
[HarmonyPatch(typeof(global::TimeWarning), nameof(global::TimeWarning.New))]
public static class New_Patch
{
/// <summary>
/// Disabled as this patch doesn't apply with Harmony v1, will enable with Harmony v2 upgrade
/// </summary>
[HarmonyPrepare]
public static bool Prepare(HarmonyInstance harmonyInstance) => false;

[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> originalInstructions)
{
Expand Down
43 changes: 32 additions & 11 deletions src/RustServerMetrics/MetricsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,43 @@ void RegisterCommands()
Variable = false,
Call = new Action<ConsoleSystem.Arg>(ReloadCommand)
};

ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "reload"] = reloadCommand;
var allCommands = ConsoleSystem.Index.All;
Array.Resize(ref allCommands, allCommands.Length + 1);
allCommands[allCommands.Length - 1] = reloadCommand;

ConsoleSystem.Command statusCommand = new ConsoleSystem.Command()
{
Name = "status",
Parent = commandPrefix,
FullName = commandPrefix + "." + "status",
ServerAdmin = true,
Variable = false,
Call = new Action<ConsoleSystem.Arg>(StatusCommand)
};
ConsoleSystem.Index.Server.Dict[commandPrefix + "." + "status"] = statusCommand;

ConsoleSystem.Command[] allCommands = ConsoleSystem.Index.All.Concat(new ConsoleSystem.Command[] { reloadCommand, statusCommand }).ToArray();
// Would be nice if this had a public setter, or better yet, a register command helper
typeof(ConsoleSystem.Index)
.GetProperty(nameof(ConsoleSystem.Index.All), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
.SetValue(null, allCommands);
}

private void ReloadCommand(ConsoleSystem.Arg arg)
void StatusCommand(ConsoleSystem.Arg arg)
{
_stringBuilder.Clear();
_stringBuilder.AppendLine("[ServerMetrics]: Status Overview");
_stringBuilder.Append("\tReady: "); _stringBuilder.Append(_ready); _stringBuilder.AppendLine();
_stringBuilder.AppendLine("\tReport Uploader:");
_stringBuilder.Append("\t\tRunning: "); _stringBuilder.Append(_reportUploader.IsRunning); _stringBuilder.AppendLine();
_stringBuilder.Append("\t\tIn Buffer: "); _stringBuilder.Append(_reportUploader.BufferSize); _stringBuilder.AppendLine();

arg.ReplyWith(_stringBuilder.ToString());
}

void ReloadCommand(ConsoleSystem.Arg arg)
{
LoadConfiguration();
if (!ValidateConfiguration() || _configuration.enabled == false)
{
if (!_configuration.enabled)
{
Debug.LogWarning("[ServerMetrics]: Metrics gathering has been disabled in the configuration");
return;
}

_ready = false;
CancelInvoke(LogNetworkUpdates);
foreach (var player in _playerStatsActions)
Expand All @@ -111,6 +126,12 @@ private void ReloadCommand(ConsoleSystem.Arg arg)
basePlayer.CancelInvoke(player.Value);
}
_reportUploader.Stop();

if (!_configuration.enabled)
{
Debug.LogWarning("[ServerMetrics]: Metrics gathering has been disabled in the configuration");
return;
}
}
else if (!_ready)
{
Expand Down
3 changes: 3 additions & 0 deletions src/RustServerMetrics/ReportUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ReportUploader : MonoBehaviour
Uri _uri = null;
MetricsLogger _metricsLogger;

public bool IsRunning => _isRunning;
public int BufferSize => _sendBuffer.Count;

void Awake()
{
_metricsLogger = GetComponent<MetricsLogger>();
Expand Down

0 comments on commit 55509e3

Please sign in to comment.