From d87cc7f1e70dc145897d925324c270673ca1e61f Mon Sep 17 00:00:00 2001 From: John Gross Date: Thu, 12 Apr 2018 09:51:52 -0700 Subject: [PATCH] Truncate log in UI, add ability to cancel restart --- Torch.Server/FlowDocumentTarget.cs | 5 +++++ Torch.Server/Managers/InstanceManager.cs | 4 ---- Torch/Commands/TorchCommands.cs | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Torch.Server/FlowDocumentTarget.cs b/Torch.Server/FlowDocumentTarget.cs index 353dcf7..26a1fba 100644 --- a/Torch.Server/FlowDocumentTarget.cs +++ b/Torch.Server/FlowDocumentTarget.cs @@ -18,6 +18,7 @@ namespace Torch.Server { private FlowDocument _document = new FlowDocument { Background = new SolidColorBrush(Colors.Black) }; private readonly Paragraph _paragraph = new Paragraph(); + private readonly int _maxLines = 500; public FlowDocument Document => _document; @@ -33,6 +34,10 @@ namespace Torch.Server { var message = $"{Layout.Render(logEvent)}\n"; _paragraph.Inlines.Add(new Run(message) {Foreground = LogLevelColors[logEvent.Level]}); + + // A massive paragraph slows the UI down + if (_paragraph.Inlines.Count > _maxLines) + _paragraph.Inlines.Remove(_paragraph.Inlines.FirstInline); }); } diff --git a/Torch.Server/Managers/InstanceManager.cs b/Torch.Server/Managers/InstanceManager.cs index e5b796d..c6b1d3b 100644 --- a/Torch.Server/Managers/InstanceManager.cs +++ b/Torch.Server/Managers/InstanceManager.cs @@ -133,10 +133,6 @@ namespace Torch.Server.Managers return; } - var sb = new StringBuilder(); - foreach (var mod in checkpoint.Mods) - sb.AppendLine(mod.PublishedFileId.ToString()); - DedicatedConfig.Mods = checkpoint.Mods.Select(x => x.PublishedFileId).ToList(); Log.Debug("Loaded mod list from world"); diff --git a/Torch/Commands/TorchCommands.cs b/Torch/Commands/TorchCommands.cs index 90ebea0..9e904ee 100644 --- a/Torch/Commands/TorchCommands.cs +++ b/Torch/Commands/TorchCommands.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; +using Sandbox.Game.Multiplayer; using Sandbox.ModAPI; using SteamSDK; using Torch; @@ -22,6 +23,9 @@ namespace Torch.Commands { public class TorchCommands : CommandModule { + private static bool _restartPending = false; + private static bool _cancelRestart = false; + [Command("whatsmyip")] [Permission(MyPromoteLevel.None)] public void GetIP(ulong steamId = 0) @@ -148,6 +152,7 @@ namespace Torch.Commands [Command("restart", "Restarts the server.")] public void Restart(int countdownSeconds = 10, bool save = true) { + _restartPending = true; Task.Run(() => { var countdown = RestartCountdown(countdownSeconds, save).GetEnumerator(); @@ -158,10 +163,29 @@ namespace Torch.Commands }); } + [Command("restart cancel", "Cancel a pending restart.")] + public void CancelRestart() + { + if (_restartPending) + _cancelRestart = true; + else + Context.Respond("A restart is not pending."); + } + private IEnumerable RestartCountdown(int countdown, bool save) { for (var i = countdown; i >= 0; i--) { + if (_cancelRestart) + { + Context.Torch.CurrentSession.Managers.GetManager() + .SendMessageAsSelf($"Restart cancelled."); + + _restartPending = false; + _cancelRestart = false; + yield break; + } + if (i >= 60 && i % 60 == 0) { Context.Torch.CurrentSession.Managers.GetManager()