From 92eebc7de14d50228f5ca85fe98dda22bca390de Mon Sep 17 00:00:00 2001 From: Twirlbug Date: Wed, 28 Jun 2023 00:26:21 -0500 Subject: [PATCH] Extended the Log to filter through the displayed responced using console --- SRMP/Console/Console Commands.txt | 8 +- SRMP/Console/ConsoleInput.cs | 65 ++++++++++-- SRMP/Console/SRMPConsole.cs | 163 +++++++++++++++++++++++++----- SRMP/MultiplayerUI.cs | 4 +- 4 files changed, 203 insertions(+), 37 deletions(-) diff --git a/SRMP/Console/Console Commands.txt b/SRMP/Console/Console Commands.txt index 2ea5f3e..1c4a065 100644 --- a/SRMP/Console/Console Commands.txt +++ b/SRMP/Console/Console Commands.txt @@ -10,6 +10,7 @@ cheat allgadgets Unlocks all of the gagets in the game cheat spawn [id] ([amount]) + spawns given item for the given ammount tp [DestinationPlayer] Teleports the player that entered in the commant to the player entered @@ -17,4 +18,9 @@ listplayers Prints the list of current players to the console sleep [Hours] - Sleep command followed by the time in in game hours to "sleep"". Fast forwards the game the alotted hours. \ No newline at end of file + Sleep command followed by the time in in game hours to "sleep"". Fast forwards the game the alotted hours. + +console [enable/disable] [logtype] + Sets whether a specific log type should be displaying in the console. + All type default to enabled. + Log Types: Error, Assert, Warning, Log, Exception \ No newline at end of file diff --git a/SRMP/Console/ConsoleInput.cs b/SRMP/Console/ConsoleInput.cs index cf52165..29221f5 100644 --- a/SRMP/Console/ConsoleInput.cs +++ b/SRMP/Console/ConsoleInput.cs @@ -9,7 +9,7 @@ namespace SRMultiplayer { public string inputString = ""; public event Action OnInputText; - + public void ClearLine() { Console.CursorLeft = 0; @@ -17,7 +17,7 @@ namespace SRMultiplayer Console.CursorTop--; Console.CursorLeft = 0; } - + public void RedrawInputLine() { bool flag = Console.CursorLeft > 0; @@ -29,7 +29,7 @@ namespace SRMultiplayer Console.Write("> "); Console.Write(this.inputString); } - + internal void OnBackspace() { bool flag = this.inputString.Length <= 0; @@ -39,14 +39,14 @@ namespace SRMultiplayer this.RedrawInputLine(); } } - + internal void OnEscape() { this.ClearLine(); this.inputString = ""; this.RedrawInputLine(); } - + internal void OnEnter() { this.ClearLine(); @@ -57,10 +57,45 @@ namespace SRMultiplayer bool flag = this.OnInputText != null; if (flag) { + //on text inputed reset the search loc and cycle the search tree + searchLoc = -1; //searchLoc set to -1 to always go to place 0 on first arrow + AddToCommandTree(obj); + this.OnInputText(obj); } } - + + List cmdTree = new List(); + internal void AddToCommandTree(string cmdText) + { + cmdTree.Insert(0,cmdText); + + //if tree gets larger than 10 remove the 11th item + if (cmdTree.Count > 10) { cmdTree.RemoveAt(10); }; + } + + + //handle internal cycling of last 10 commands + int searchLoc = -1; + internal void GetCommand(int diff) + { + if (cmdTree.Count > 0) + { + searchLoc = searchLoc + diff; + //prevent below 0 or over max position + if (searchLoc > (cmdTree.Count - 1)) searchLoc = (cmdTree.Count - 1); + if (searchLoc < 0) searchLoc = 0; + + //if a new location is found enter the search text in the input and redraw it. + this.inputString = cmdTree[searchLoc]; + this.RedrawInputLine(); + } + else + { + Console.WriteLine("cmdTree is empty"); + } + } + public void Update() { bool flag = !Console.KeyAvailable; @@ -94,6 +129,24 @@ namespace SRMultiplayer this.inputString += consoleKeyInfo.KeyChar.ToString(); this.RedrawInputLine(); } + else + { + //handle up pressed to get previous lines + bool flag6 = consoleKeyInfo.Key == ConsoleKey.UpArrow; + if (flag6) + { + this.GetCommand(1); + } + else + { + //handle down pressed to get next lines + bool flag7 = consoleKeyInfo.Key == ConsoleKey.DownArrow; + if (flag7) + { + this.GetCommand(-1); + } + } + } } } } diff --git a/SRMP/Console/SRMPConsole.cs b/SRMP/Console/SRMPConsole.cs index a776d24..9f6ab5b 100644 --- a/SRMP/Console/SRMPConsole.cs +++ b/SRMP/Console/SRMPConsole.cs @@ -1,4 +1,5 @@ -using MonomiPark.SlimeRancher.Regions; +using JetBrains.Annotations; +using MonomiPark.SlimeRancher.Regions; using SRMultiplayer.Networking; using System; using System.Collections.Generic; @@ -30,27 +31,98 @@ namespace SRMultiplayer SRMP.Log("Console Started"); } + //used to prevent duplicate messages displaying and list what number duplicate it is + string LastMessage = ""; + int duplicateCount = 0; + + //types of console types that can be enabled/disabled + //server automatically starts with all active + List blockMessages = new List(); //keeps a list of message types that have been disabled private void Application_logMessageReceived(string condition, string stackTrace, LogType type) { - if (type == LogType.Warning) - Console.ForegroundColor = ConsoleColor.Yellow; - else if (type == LogType.Error) - Console.ForegroundColor = ConsoleColor.Red; - else - Console.ForegroundColor = ConsoleColor.White; - // We're half way through typing something, so clear this line .. if (Console.CursorLeft != 0) input.ClearLine(); - Console.WriteLine(condition); + //construct message + string message = condition; if (!string.IsNullOrEmpty(stackTrace)) - Console.WriteLine(stackTrace); + { + //add stack strace if included + message += Environment.NewLine + stackTrace; + } + + if (message == LastMessage) + { + //do not process duplicate marks if the last item was not written + if(duplicateCount >0) duplicateCount++; + } + else + { + //add write line for duplicate notices if necessary + if (duplicateCount > 0) + { + Console.ForegroundColor = ConsoleColor.Gray; + Console.WriteLine("Output Duplicated: " + duplicateCount); + } + + //format color for message + if (type == LogType.Warning) + Console.ForegroundColor = ConsoleColor.Yellow; + else if (type == LogType.Error) + Console.ForegroundColor = ConsoleColor.Red; + else + Console.ForegroundColor = ConsoleColor.White; + + // mark new message + LastMessage = message; + + //check data for inner types + string data = condition; + + //remove the srmp tag and the time to check inner tags + if (type == LogType.Log) data = data.Substring(17); + + //only write the message type if its not blocked + //always allow console replay to display + if (!blockMessages.Contains(type) || data.StartsWith("[Console]")) + { + //write the new line if not blocked + duplicateCount = 0; + Console.WriteLine(message); + + } + else + { + //for testing log disabled display + //Console.ForegroundColor = ConsoleColor.Magenta; + Console.WriteLine(type.ToString() + " Dismissed"); + + //mark dupilcate count to -1 + //prevent duplicate count for supressed messages from displaying + duplicateCount = -1; + } + } + + // If we were typing something re-add it. input.RedrawInputLine(); } + public static void ClearCurrentConsoleLine() + { + int currentLineCursor = Console.CursorTop; + Console.SetCursorPosition(0, Console.CursorTop); + Console.Write(new string(' ', Console.BufferWidth)); + Console.SetCursorPosition(0, currentLineCursor); + } + + //create a log call that marks all console sent replys + void ConsoleLog(string message) + { + SRMP.Log(message, "Console"); + } @@ -74,17 +146,17 @@ namespace SRMultiplayer { if (args.Length != 2 || !int.TryParse(args[1], out int money)) { - SRMP.Log("Usage: cheat money "); + ConsoleLog("Usage: cheat money "); } else { - if(money > 0) + if (money > 0) SRSingleton.Instance.PlayerState.AddCurrency(money); else SRSingleton.Instance.PlayerState.SpendCurrency(money); } } - else if(args[0].Equals("enable")) + else if (args[0].Equals("enable")) { //TestUI.Instance.cheat = !TestUI.Instance.cheat; } @@ -92,7 +164,7 @@ namespace SRMultiplayer { if (args.Length != 2 || !int.TryParse(args[1], out int value)) { - SRMP.Log("Usage: cheat keys "); + ConsoleLog("Usage: cheat keys "); } else { @@ -123,7 +195,7 @@ namespace SRMultiplayer } else { - SRMP.Log(id + " can not be spawned"); + ConsoleLog(id + " can not be spawned"); } } else @@ -132,7 +204,7 @@ namespace SRMultiplayer SRMP.Log(args[1] + " not found. " + (data.Count() > 0 ? " Did you mean one of these?" : "")); foreach (var name in data) { - SRMP.Log(name); + ConsoleLog(name); } } } @@ -156,26 +228,26 @@ namespace SRMultiplayer else { var data = Enum.GetNames(typeof(Identifiable.Id)).Where(n => n.ToLower().Contains(args[1].ToLower())); - SRMP.Log(args[1] + " not found. " + (data.Count() > 0 ? " Did you mean one of these?" : "")); + ConsoleLog(args[1] + " not found. " + (data.Count() > 0 ? " Did you mean one of these?" : "")); foreach (var name in data) { - SRMP.Log(name); + ConsoleLog(name); } } } else { - SRMP.Log("Usage: cheat spawn ()"); + ConsoleLog("Usage: cheat spawn ()"); } } } else { - SRMP.Log("Available sub commands:"); - SRMP.Log("cheat money "); - SRMP.Log("cheat keys "); - SRMP.Log("cheat spawn ()"); - SRMP.Log("cheat allgadgets"); + ConsoleLog("Available sub commands:"); + ConsoleLog("cheat money "); + ConsoleLog("cheat keys "); + ConsoleLog("cheat spawn ()"); + ConsoleLog("cheat allgadgets"); } } break; @@ -190,18 +262,18 @@ namespace SRMultiplayer } else { - SRMP.Log("Player not found"); + ConsoleLog("Player not found"); } } else { - SRMP.Log("Usage: tp "); + ConsoleLog("Usage: tp "); } } break; case "listplayers": { - SRMP.Log("Players:"); + ConsoleLog("Players:"); foreach (var player in Globals.Players.Values) { SRMP.Log(player.Username); @@ -213,10 +285,45 @@ namespace SRMultiplayer if (args.Length == 1) { SRSingleton.Instance.TimeDirector.FastForwardTo(SRSingleton.Instance.TimeDirector.HoursFromNow(float.Parse(args[0]))); + ConsoleLog("Sleeoing for " + args[0] + " hours"); + } + else + { + ConsoleLog("Usage: sleep "); } } break; - + case "console": //add toggle option for turning on and off logging types + + if (args.Length > 1 && (args[0] == "enable"|| args[0] == "disable")) + { + bool enable = args[0] == "enable"; + //double check type + if (LogType.TryParse(args[1], true, out LogType logType)) + { + if (enable) + { + if (!blockMessages.Contains(logType)) blockMessages.Remove(logType); + ConsoleLog(logType.ToString() + " Messages Enabled"); + } + else + { + if (blockMessages.Contains(logType)) blockMessages.Add(logType); + ConsoleLog("[Console] " + logType.ToString() + " Messages Disabled"); + } + } + else + { + ConsoleLog("Invalid Feed back Type"); + ConsoleLog("Suggestions: " + string.Join(", ", logType)); + } + } + else + { + ConsoleLog("Usage: console "); + } + break; + } } diff --git a/SRMP/MultiplayerUI.cs b/SRMP/MultiplayerUI.cs index 1023c61..0409731 100644 --- a/SRMP/MultiplayerUI.cs +++ b/SRMP/MultiplayerUI.cs @@ -133,14 +133,14 @@ public class MultiplayerUI : SRSingleton } private void FunctionKeys() { - GUILayout.Label("Press Button or Corresponding Key To Change Menu"); + GUILayout.Label("Press Button or Key To Change Style"); GUILayout.BeginHorizontal(); if (GUILayout.Button(menuOpen == 1 ? "F3 - Full" : "F3 - Mini")) { menuOpen = menuOpen == 1 ? 2 : 1; } GUILayout.FlexibleSpace(); - if (GUILayout.Button(menuOpen == 0 ? "F4 - Full" : "F4 - Closed")) + if (GUILayout.Button(menuOpen == 0 ? "F4 - Full" : "F4 - Colapsed")) { menuOpen = menuOpen == 0 ? 2 : 0; }