From fb67fcce5a1da73eadfe41cc1409f99c55cb58a8 Mon Sep 17 00:00:00 2001 From: Luke Date: Sat, 5 Mar 2022 22:51:07 +1000 Subject: [PATCH] Create module to reduce console spam - fixes #2560 --- TShockAPI/Modules/ReduceConsoleSpam.cs | 66 ++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 TShockAPI/Modules/ReduceConsoleSpam.cs diff --git a/TShockAPI/Modules/ReduceConsoleSpam.cs b/TShockAPI/Modules/ReduceConsoleSpam.cs new file mode 100644 index 00000000..bc33655c --- /dev/null +++ b/TShockAPI/Modules/ReduceConsoleSpam.cs @@ -0,0 +1,66 @@ +/* +TShock, a server mod for Terraria +Copyright (C) 2011-2019 Pryaxis & TShock Contributors + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +using System; + +namespace TShockAPI.Modules +{ + public class ReduceConsoleSpam : Module + { + public override void Initialise() => + OTAPI.Hooks.Main.StatusTextChange += OnMainStatusTextChange; + + public override void Dispose() => + OTAPI.Hooks.Main.StatusTextChange -= OnMainStatusTextChange; + + /// + /// Holds the last status text value, to determine if there is a suitable change to report. + /// + private string _lastStatusText = null; + + /// + /// Aims to reduce the amount of console spam by filtering out load/save progress + /// + /// + /// OTAPI event + private void OnMainStatusTextChange(object sender, OTAPI.Hooks.Main.StatusTextChangeArgs e) + { + bool replace(string text) + { + if (e.Value.StartsWith(text)) + { + var segment = e.Value.Substring(0, text.Length); + if (_lastStatusText != segment) + { + Console.WriteLine(segment); // write it manually instead of terraria which causes double writes + _lastStatusText = segment; + } + e.Value = ""; + return true; + } + return false; + } + + _ = replace("Resetting game objects") + || replace("Settling liquids") + || replace("Loading world data") + || replace("Saving world data") + || replace("Validating world save"); + } + } +} +