From 0c07c9cfe59858cbc0696a14b72fdaff058f80be Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 10 Apr 2023 19:23:46 -0400 Subject: [PATCH] Some tiny stuff. More tomorrow --- SrcMod/Shell/Modules/ConfigModule.cs | 11 ++++++++ SrcMod/Shell/ObjectModels/AskMode.cs | 8 ++++++ SrcMod/Shell/ObjectModels/Config.cs | 9 ++++--- SrcMod/Shell/ObjectModels/ConfigChanges.cs | 6 +++-- SrcMod/Shell/Tools.cs | 31 ++++++++++++++++++++-- 5 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 SrcMod/Shell/Modules/ConfigModule.cs create mode 100644 SrcMod/Shell/ObjectModels/AskMode.cs diff --git a/SrcMod/Shell/Modules/ConfigModule.cs b/SrcMod/Shell/Modules/ConfigModule.cs new file mode 100644 index 0000000..4399b6e --- /dev/null +++ b/SrcMod/Shell/Modules/ConfigModule.cs @@ -0,0 +1,11 @@ +namespace SrcMod.Shell.Modules; + +[Module("config")] +public static class ConfigModule +{ + [Command("display")] + public static void DisplayConfig() + { + // TODO + } +} diff --git a/SrcMod/Shell/ObjectModels/AskMode.cs b/SrcMod/Shell/ObjectModels/AskMode.cs new file mode 100644 index 0000000..e425960 --- /dev/null +++ b/SrcMod/Shell/ObjectModels/AskMode.cs @@ -0,0 +1,8 @@ +namespace SrcMod.Shell.ObjectModels; + +public enum AskMode : sbyte +{ + Never = -1, + Ask = 0, + Always = 1 +} diff --git a/SrcMod/Shell/ObjectModels/Config.cs b/SrcMod/Shell/ObjectModels/Config.cs index 87b5ed8..5a8ad60 100644 --- a/SrcMod/Shell/ObjectModels/Config.cs +++ b/SrcMod/Shell/ObjectModels/Config.cs @@ -24,18 +24,21 @@ public struct Config Defaults = new(); } - public string[] SteamDirectories; + public string[] GameDirectories; + public AskMode RunUnsafeCommands; public Config ApplyChanges(ConfigChanges changes) => this with { - SteamDirectories = changes.SteamDirectories ?? SteamDirectories + GameDirectories = changes.GameDirectories ?? GameDirectories, + RunUnsafeCommands = changes.RunUnsafeCommands ?? RunUnsafeCommands }; public ConfigChanges GetChanges(Config? baseConfig = null) { Config reference = baseConfig ?? Defaults; return new() { - SteamDirectories = reference.SteamDirectories == SteamDirectories ? null : SteamDirectories + GameDirectories = reference.GameDirectories == GameDirectories ? null : GameDirectories, + RunUnsafeCommands = reference.RunUnsafeCommands == RunUnsafeCommands ? null : RunUnsafeCommands }; } diff --git a/SrcMod/Shell/ObjectModels/ConfigChanges.cs b/SrcMod/Shell/ObjectModels/ConfigChanges.cs index 909e662..8328796 100644 --- a/SrcMod/Shell/ObjectModels/ConfigChanges.cs +++ b/SrcMod/Shell/ObjectModels/ConfigChanges.cs @@ -2,7 +2,9 @@ public record struct ConfigChanges { - public bool HasChange => SteamDirectories is not null; + [JsonIgnore] + public bool HasChange => GameDirectories is not null || RunUnsafeCommands is not null; - public string[]? SteamDirectories; + public string[]? GameDirectories; + public AskMode? RunUnsafeCommands; } diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index 9de1e0f..8772ef8 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -1,4 +1,6 @@ -namespace SrcMod.Shell; +using System.Numerics; + +namespace SrcMod.Shell; public static class Tools { @@ -16,7 +18,8 @@ public static class Tools { Serializer = JsonSerializer.Create(new() { - Formatting = Formatting.Indented + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore }); } @@ -165,6 +168,30 @@ public static class Tools } } + public static void WriteAutoColor(object? message, bool newLine = true) + { + ConsoleColor? col = null; + if (message is null) col = ConsoleColor.DarkGray; + else if (message is bool typeBool) + { + if (typeBool) col = ConsoleColor.Green; + else col = ConsoleColor.Red; + } + else if (message is sbyte || message is byte || message is short || message is ushort || + message is int || message is uint || message is long || message is ulong || + message is float || message is double || message is decimal) col = ConsoleColor.DarkGreen; + else if (message is char) col = ConsoleColor.DarkYellow; + else if (message is AskMode typeAskMode) col = typeAskMode switch + { + AskMode.Never => ConsoleColor.Red, + AskMode.Ask => ConsoleColor.DarkGray, + AskMode.Always => ConsoleColor.Green, + _ => null + }; + + Write(message, col, newLine); + } + public static bool ValidateUnsafe() { Write("You are about to execute an unsafe command.\nProceed? > ", ConsoleColor.DarkYellow, false);