From 68336df8686a1061c98493709a9dff87f142f5a4 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 10 Apr 2023 13:27:55 -0400 Subject: [PATCH 01/12] Made some basic start to the config stuff. More will come later. --- SrcMod/Shell/GlobalUsings.cs | 4 ++- SrcMod/Shell/ObjectModels/ShellConfig.cs | 46 ++++++++++++++++++++++++ SrcMod/Shell/Shell.cs | 13 ++++--- SrcMod/Shell/Shell.csproj | 2 ++ SrcMod/Shell/Tools.cs | 14 ++++++-- 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 SrcMod/Shell/ObjectModels/ShellConfig.cs diff --git a/SrcMod/Shell/GlobalUsings.cs b/SrcMod/Shell/GlobalUsings.cs index a26ef43..8ae847e 100644 --- a/SrcMod/Shell/GlobalUsings.cs +++ b/SrcMod/Shell/GlobalUsings.cs @@ -1,12 +1,14 @@ global using Nerd_STF.Mathematics; +global using Newtonsoft.Json; global using SharpCompress.Archives.Rar; global using SharpCompress.Archives.SevenZip; global using SharpCompress.Readers; -global using SrcMod.Shell; global using SrcMod.Shell.Interop; global using SrcMod.Shell.Modules.ObjectModels; +global using SrcMod.Shell.ObjectModels; global using System; global using System.Collections.Generic; +global using System.ComponentModel; global using System.Diagnostics; global using System.Formats.Tar; global using System.IO; diff --git a/SrcMod/Shell/ObjectModels/ShellConfig.cs b/SrcMod/Shell/ObjectModels/ShellConfig.cs new file mode 100644 index 0000000..d347d11 --- /dev/null +++ b/SrcMod/Shell/ObjectModels/ShellConfig.cs @@ -0,0 +1,46 @@ +namespace SrcMod.Shell.ObjectModels; + +public class ShellConfig +{ + public const string FilePath = "config.json"; + + public static ShellConfig Defaults => new() + { + SteamDirectories = new[] + { + "Temp" + } + }; + public static ShellConfig LoadedConfig => p_data ?? Defaults; + + private static ShellConfig? p_data; + + public string[] SteamDirectories; + + public static void LoadConfig(string basePath) + { + string fullPath = Path.Combine(basePath, FilePath); + + if (!File.Exists(fullPath)) + { + p_data = null; + return; + } + StreamReader reader = new(fullPath); + JsonTextReader jsonReader = new(reader); + p_data = Serializer.Deserialize(jsonReader); + jsonReader.Close(); + reader.Close(); + } + + public static void SaveConfig(string basePath) + { + string fullPath = Path.Combine(basePath, FilePath); + + StreamWriter writer = new(fullPath); + JsonTextWriter jsonWriter = new(writer); + Serializer.Serialize(jsonWriter, p_data); + jsonWriter.Close(); + writer.Close(); + } +} diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 3a90b88..634ab0a 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -1,12 +1,10 @@ -using System.ComponentModel; - -namespace SrcMod.Shell; +namespace SrcMod.Shell; public class Shell { public const string Author = "That_One_Nerd"; public const string Name = "SrcMod"; - public const string Version = "Alpha 0.3.2"; + public const string Version = "Alpha 0.3.3"; public readonly string? ShellDirectory; @@ -50,6 +48,10 @@ public class Shell WorkingDirectory = Directory.GetCurrentDirectory(); + // Load config. + if (ShellDirectory is null) Write("[WARNING] Could not load config from shell location. Defaults will be used."); + else ShellConfig.LoadConfig(ShellDirectory); + // Load modules and commands. List possibleAsms = new() { @@ -250,6 +252,9 @@ public class Shell activeCommand.Dispose(); activeCommand = null; } + + if (ShellDirectory is null) Write("[WARNING] Could not save config to shell location. Any changes will be ignored."); + else ShellConfig.SaveConfig(ShellDirectory); return; } } diff --git a/SrcMod/Shell/Shell.csproj b/SrcMod/Shell/Shell.csproj index d662f45..253835f 100644 --- a/SrcMod/Shell/Shell.csproj +++ b/SrcMod/Shell/Shell.csproj @@ -13,6 +13,7 @@ false Logo.ico true + true @@ -31,6 +32,7 @@ + diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index 3fea015..9de1e0f 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -1,9 +1,9 @@ -using System.Text; - -namespace SrcMod.Shell; +namespace SrcMod.Shell; public static class Tools { + public static JsonSerializer Serializer { get; private set; } + private static int loadingPosition = -1; private static int lastLoadingBufferSize = 0; private static int lastLoadingValue = -1; @@ -12,6 +12,14 @@ public static class Tools public static bool LoadingBarEnabled { get; private set; } + static Tools() + { + Serializer = JsonSerializer.Create(new() + { + Formatting = Formatting.Indented + }); + } + public static void DisplayWithPages(IEnumerable lines, ConsoleColor? color = null) { int written = 0; -- 2.49.0.windows.1 From b2bc9fa7eee90d08ad5db5f1d4c3439a13ad01de Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 10 Apr 2023 18:46:40 -0400 Subject: [PATCH 02/12] New config system. Tracks changes instead of the whole thing. --- SrcMod/Shell/ObjectModels/Config.cs | 76 ++++++++++++++++++++++ SrcMod/Shell/ObjectModels/ConfigChanges.cs | 8 +++ SrcMod/Shell/ObjectModels/ShellConfig.cs | 46 ------------- SrcMod/Shell/Shell.cs | 4 +- 4 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 SrcMod/Shell/ObjectModels/Config.cs create mode 100644 SrcMod/Shell/ObjectModels/ConfigChanges.cs delete mode 100644 SrcMod/Shell/ObjectModels/ShellConfig.cs diff --git a/SrcMod/Shell/ObjectModels/Config.cs b/SrcMod/Shell/ObjectModels/Config.cs new file mode 100644 index 0000000..87b5ed8 --- /dev/null +++ b/SrcMod/Shell/ObjectModels/Config.cs @@ -0,0 +1,76 @@ +namespace SrcMod.Shell.ObjectModels; + +public struct Config +{ + public const string FilePath = "config.json"; + + public static readonly Config Defaults; + + public static Config LoadedConfig + { + get => p_applied; + set + { + p_applied = value; + p_changes = p_applied.GetChanges(Defaults); + } + } + + private static Config p_applied; + private static ConfigChanges? p_changes; + + static Config() + { + Defaults = new(); + } + + public string[] SteamDirectories; + + public Config ApplyChanges(ConfigChanges changes) => this with + { + SteamDirectories = changes.SteamDirectories ?? SteamDirectories + }; + public ConfigChanges GetChanges(Config? baseConfig = null) + { + Config reference = baseConfig ?? Defaults; + return new() + { + SteamDirectories = reference.SteamDirectories == SteamDirectories ? null : SteamDirectories + }; + } + + public static void LoadConfig(string basePath) + { + string fullPath = Path.Combine(basePath, FilePath); + + if (!File.Exists(fullPath)) + { + p_applied = Defaults; + p_changes = null; + return; + } + StreamReader reader = new(fullPath); + JsonTextReader jsonReader = new(reader); + p_changes = Serializer.Deserialize(jsonReader); + jsonReader.Close(); + reader.Close(); + + p_applied = p_changes is null ? Defaults : Defaults.ApplyChanges(p_changes.Value); + } + public static void SaveConfig(string basePath) + { + string fullPath = Path.Combine(basePath, FilePath); + + if (p_changes is null || !p_changes.Value.HasChange) + { + if (File.Exists(fullPath)) File.Delete(fullPath); + return; + } + + StreamWriter writer = new(fullPath); + JsonTextWriter jsonWriter = new(writer); + Serializer.Serialize(jsonWriter, p_changes); + jsonWriter.Close(); + writer.Close(); + } +} diff --git a/SrcMod/Shell/ObjectModels/ConfigChanges.cs b/SrcMod/Shell/ObjectModels/ConfigChanges.cs new file mode 100644 index 0000000..909e662 --- /dev/null +++ b/SrcMod/Shell/ObjectModels/ConfigChanges.cs @@ -0,0 +1,8 @@ +namespace SrcMod.Shell.ObjectModels; + +public record struct ConfigChanges +{ + public bool HasChange => SteamDirectories is not null; + + public string[]? SteamDirectories; +} diff --git a/SrcMod/Shell/ObjectModels/ShellConfig.cs b/SrcMod/Shell/ObjectModels/ShellConfig.cs deleted file mode 100644 index d347d11..0000000 --- a/SrcMod/Shell/ObjectModels/ShellConfig.cs +++ /dev/null @@ -1,46 +0,0 @@ -namespace SrcMod.Shell.ObjectModels; - -public class ShellConfig -{ - public const string FilePath = "config.json"; - - public static ShellConfig Defaults => new() - { - SteamDirectories = new[] - { - "Temp" - } - }; - public static ShellConfig LoadedConfig => p_data ?? Defaults; - - private static ShellConfig? p_data; - - public string[] SteamDirectories; - - public static void LoadConfig(string basePath) - { - string fullPath = Path.Combine(basePath, FilePath); - - if (!File.Exists(fullPath)) - { - p_data = null; - return; - } - StreamReader reader = new(fullPath); - JsonTextReader jsonReader = new(reader); - p_data = Serializer.Deserialize(jsonReader); - jsonReader.Close(); - reader.Close(); - } - - public static void SaveConfig(string basePath) - { - string fullPath = Path.Combine(basePath, FilePath); - - StreamWriter writer = new(fullPath); - JsonTextWriter jsonWriter = new(writer); - Serializer.Serialize(jsonWriter, p_data); - jsonWriter.Close(); - writer.Close(); - } -} diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 634ab0a..78acb5a 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -50,7 +50,7 @@ public class Shell // Load config. if (ShellDirectory is null) Write("[WARNING] Could not load config from shell location. Defaults will be used."); - else ShellConfig.LoadConfig(ShellDirectory); + else Config.LoadConfig(ShellDirectory); // Load modules and commands. List possibleAsms = new() @@ -254,7 +254,7 @@ public class Shell } if (ShellDirectory is null) Write("[WARNING] Could not save config to shell location. Any changes will be ignored."); - else ShellConfig.SaveConfig(ShellDirectory); + else Config.SaveConfig(ShellDirectory); return; } } -- 2.49.0.windows.1 From 0c07c9cfe59858cbc0696a14b72fdaff058f80be Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 10 Apr 2023 19:23:46 -0400 Subject: [PATCH 03/12] 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); -- 2.49.0.windows.1 From 3742b4a230f68918d706dba06bfc3578f4e5eb7d Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Mon, 17 Apr 2023 17:58:28 -0400 Subject: [PATCH 04/12] Some config display changes. WIP --- SrcMod/Shell/Modules/ConfigModule.cs | 63 +++++++++++++++++++++++++++- SrcMod/Shell/ObjectModels/Config.cs | 19 +++++++-- SrcMod/Shell/Tools.cs | 24 ----------- 3 files changed, 76 insertions(+), 30 deletions(-) diff --git a/SrcMod/Shell/Modules/ConfigModule.cs b/SrcMod/Shell/Modules/ConfigModule.cs index 4399b6e..426fb77 100644 --- a/SrcMod/Shell/Modules/ConfigModule.cs +++ b/SrcMod/Shell/Modules/ConfigModule.cs @@ -4,8 +4,67 @@ public static class ConfigModule { [Command("display")] - public static void DisplayConfig() + public static void DisplayConfig(ConfigDisplayMode mode = ConfigDisplayMode.Color) { - // TODO + switch (mode) + { + case ConfigDisplayMode.Raw: + DisplayConfigRaw(); + break; + + case ConfigDisplayMode.Color: + DisplayConfigColor(); + break; + } + } + + private static void DisplayConfigColor() + { + Config config = Config.LoadedConfig; + List dirs = config.GameDirectories is null ? new() : new(config.GameDirectories); + dirs.Add("config"); + config.GameDirectories = dirs.ToArray(); + Config.LoadedConfig = config; + + Write("Steam Game Directories: ", null, false); + if (config.GameDirectories is null || config.GameDirectories.Length <= 0) Write("None", ConsoleColor.DarkGray); + else + { + Write("[", ConsoleColor.DarkGray); + for (int i = 0; i < config.GameDirectories.Length; i++) + { + Write(" \"", ConsoleColor.DarkGray, false); + Write(config.GameDirectories[i], ConsoleColor.White, false); + if (i < config.GameDirectories.Length - 1) Write("\",", ConsoleColor.DarkGray); + else Write("\"", ConsoleColor.DarkGray); + } + Write("]", ConsoleColor.DarkGray); + } + } + private static void DisplayConfigRaw() + { + MemoryStream ms = new(); + StreamWriter writer = new(ms, leaveOpen: true); + JsonTextWriter jsonWriter = new(writer); + + Serializer.Serialize(jsonWriter, Config.LoadedConfig); + + jsonWriter.Close(); + writer.Close(); + ms.Position = 0; + + StreamReader reader = new(ms); + string msg = reader.ReadToEnd(); + + Write(msg); + + reader.Close(); + ms.Close(); + } + + public enum ConfigDisplayMode + { + Raw, + Color } } diff --git a/SrcMod/Shell/ObjectModels/Config.cs b/SrcMod/Shell/ObjectModels/Config.cs index 5a8ad60..e76ce48 100644 --- a/SrcMod/Shell/ObjectModels/Config.cs +++ b/SrcMod/Shell/ObjectModels/Config.cs @@ -21,7 +21,15 @@ public struct Config static Config() { - Defaults = new(); + Defaults = new() + { + GameDirectories = new[] + { + "testing 1", + "testing 2" + }, + RunUnsafeCommands = AskMode.Ask + }; } public string[] GameDirectories; @@ -29,17 +37,20 @@ public struct Config public Config ApplyChanges(ConfigChanges changes) => this with { - GameDirectories = changes.GameDirectories ?? GameDirectories, + GameDirectories = GameDirectories.Union(changes.GameDirectories ?? Array.Empty()).ToArray(), RunUnsafeCommands = changes.RunUnsafeCommands ?? RunUnsafeCommands }; public ConfigChanges GetChanges(Config? baseConfig = null) { Config reference = baseConfig ?? Defaults; - return new() + ConfigChanges changes = new() { - GameDirectories = reference.GameDirectories == GameDirectories ? null : GameDirectories, + GameDirectories = reference.GameDirectories == GameDirectories ? null : + GameDirectories.Where(x => !reference.GameDirectories.Contains(x)).ToArray(), RunUnsafeCommands = reference.RunUnsafeCommands == RunUnsafeCommands ? null : RunUnsafeCommands }; + + return changes; } public static void LoadConfig(string basePath) diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index 8772ef8..713ffea 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -168,30 +168,6 @@ 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); -- 2.49.0.windows.1 From 4d342ae9585d89e92ed1e6752e972bd4f6210ed3 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Mon, 17 Apr 2023 19:11:33 -0400 Subject: [PATCH 05/12] Added some config stuff. Next is modifying the config. --- SrcMod/Shell/Modules/ConfigModule.cs | 15 ++++++++--- SrcMod/Shell/Tools.cs | 37 ++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/SrcMod/Shell/Modules/ConfigModule.cs b/SrcMod/Shell/Modules/ConfigModule.cs index 426fb77..392d4cb 100644 --- a/SrcMod/Shell/Modules/ConfigModule.cs +++ b/SrcMod/Shell/Modules/ConfigModule.cs @@ -21,10 +21,6 @@ public static class ConfigModule private static void DisplayConfigColor() { Config config = Config.LoadedConfig; - List dirs = config.GameDirectories is null ? new() : new(config.GameDirectories); - dirs.Add("config"); - config.GameDirectories = dirs.ToArray(); - Config.LoadedConfig = config; Write("Steam Game Directories: ", null, false); if (config.GameDirectories is null || config.GameDirectories.Length <= 0) Write("None", ConsoleColor.DarkGray); @@ -40,9 +36,20 @@ public static class ConfigModule } Write("]", ConsoleColor.DarkGray); } + + Write("Run Unsafe Commands: ", null, false); + ConsoleColor color = config.RunUnsafeCommands switch + { + AskMode.Never => ConsoleColor.Red, + AskMode.Always => ConsoleColor.Green, + AskMode.Ask or _ => ConsoleColor.DarkGray + }; + Write(config.RunUnsafeCommands, color); } private static void DisplayConfigRaw() { + // This is definitely a bit inefficient, but shouldn't be too much of an issue. + MemoryStream ms = new(); StreamWriter writer = new(ms, leaveOpen: true); JsonTextWriter jsonWriter = new(writer); diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index 713ffea..e9ff45b 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -170,15 +170,36 @@ public static class Tools public static bool ValidateUnsafe() { - Write("You are about to execute an unsafe command.\nProceed? > ", ConsoleColor.DarkYellow, false); + switch (Config.LoadedConfig.RunUnsafeCommands) + { + case AskMode.Always: + Write("[INFO] The shell has been configured to always run unsafe commands. " + + "This can be changed in the config.", ConsoleColor.DarkGray); + return true; - Console.ForegroundColor = ConsoleColor.Yellow; - Console.CursorVisible = true; - string result = Console.ReadLine()!.Trim().ToLower(); - Console.CursorVisible = false; - Console.ResetColor(); + case AskMode.Never: + Write("[ERROR] The shell has been configured to never run unsafe commands. " + + "This can be changed in the config.", ConsoleColor.Red); + return false; - return result == "y" || result == "yes" || result == "t" || - result == "true" || result == "p" || result == "proceed"; + case AskMode.Ask or _: + Write("You are about to execute an unsafe command.\nProceed? > ", ConsoleColor.DarkYellow, false); + Int2 start = (Console.CursorLeft, Console.CursorTop); + Write("\nTip: You can disable this dialog in the config.", ConsoleColor.DarkGray); + int finish = Console.CursorTop; + + Console.SetCursorPosition(start.x, start.y); + + Console.ForegroundColor = ConsoleColor.Yellow; + Console.CursorVisible = true; + string result = Console.ReadLine()!.Trim().ToLower(); + Console.CursorVisible = false; + Console.ResetColor(); + + Console.SetCursorPosition(0, finish); + + return result == "y" || result == "yes" || result == "t" || + result == "true" || result == "p" || result == "proceed"; + } } } -- 2.49.0.windows.1 From 8bfd8c142c3105b523bc07b3fc33baa7079f50f8 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 17 Apr 2023 19:38:31 -0400 Subject: [PATCH 06/12] Quick update, added some more config display options. --- SrcMod/Shell/Modules/ConfigModule.cs | 76 +++++++++++++++++----------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/SrcMod/Shell/Modules/ConfigModule.cs b/SrcMod/Shell/Modules/ConfigModule.cs index 392d4cb..a206493 100644 --- a/SrcMod/Shell/Modules/ConfigModule.cs +++ b/SrcMod/Shell/Modules/ConfigModule.cs @@ -4,7 +4,7 @@ public static class ConfigModule { [Command("display")] - public static void DisplayConfig(ConfigDisplayMode mode = ConfigDisplayMode.Color) + public static void DisplayConfig(ConfigDisplayMode mode = ConfigDisplayMode.All) { switch (mode) { @@ -12,39 +12,24 @@ public static class ConfigModule DisplayConfigRaw(); break; - case ConfigDisplayMode.Color: - DisplayConfigColor(); + case ConfigDisplayMode.All: + DisplayConfigAll(); + break; + + case ConfigDisplayMode.GameDirectories: + DisplayConfigGameDirectories(); + break; + + case ConfigDisplayMode.RunUnsafeCommands: + DisplayConfigUnsafeCommands(); break; } } - private static void DisplayConfigColor() + private static void DisplayConfigAll() { - Config config = Config.LoadedConfig; - - Write("Steam Game Directories: ", null, false); - if (config.GameDirectories is null || config.GameDirectories.Length <= 0) Write("None", ConsoleColor.DarkGray); - else - { - Write("[", ConsoleColor.DarkGray); - for (int i = 0; i < config.GameDirectories.Length; i++) - { - Write(" \"", ConsoleColor.DarkGray, false); - Write(config.GameDirectories[i], ConsoleColor.White, false); - if (i < config.GameDirectories.Length - 1) Write("\",", ConsoleColor.DarkGray); - else Write("\"", ConsoleColor.DarkGray); - } - Write("]", ConsoleColor.DarkGray); - } - - Write("Run Unsafe Commands: ", null, false); - ConsoleColor color = config.RunUnsafeCommands switch - { - AskMode.Never => ConsoleColor.Red, - AskMode.Always => ConsoleColor.Green, - AskMode.Ask or _ => ConsoleColor.DarkGray - }; - Write(config.RunUnsafeCommands, color); + DisplayConfigGameDirectories(); + DisplayConfigUnsafeCommands(); } private static void DisplayConfigRaw() { @@ -68,10 +53,41 @@ public static class ConfigModule reader.Close(); ms.Close(); } + private static void DisplayConfigGameDirectories() + { + Write("Steam Game Directories: ", null, false); + if (Config.LoadedConfig.GameDirectories is null || Config.LoadedConfig.GameDirectories.Length <= 0) + Write("None", ConsoleColor.DarkGray); + else + { + Write("[", ConsoleColor.DarkGray); + for (int i = 0; i < Config.LoadedConfig.GameDirectories.Length; i++) + { + Write(" \"", ConsoleColor.DarkGray, false); + Write(Config.LoadedConfig.GameDirectories[i], ConsoleColor.White, false); + if (i < Config.LoadedConfig.GameDirectories.Length - 1) Write("\",", ConsoleColor.DarkGray); + else Write("\"", ConsoleColor.DarkGray); + } + Write("]", ConsoleColor.DarkGray); + } + } + private static void DisplayConfigUnsafeCommands() + { + Write("Run Unsafe Commands: ", null, false); + ConsoleColor color = Config.LoadedConfig.RunUnsafeCommands switch + { + AskMode.Never => ConsoleColor.Red, + AskMode.Always => ConsoleColor.Green, + AskMode.Ask or _ => ConsoleColor.DarkGray + }; + Write(Config.LoadedConfig.RunUnsafeCommands, color); + } public enum ConfigDisplayMode { Raw, - Color + All, + GameDirectories, + RunUnsafeCommands } } -- 2.49.0.windows.1 From ec15e78564717d854f738d0811377359686b67cc Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 17 Apr 2023 20:12:52 -0400 Subject: [PATCH 07/12] Added some config modifier commands. Definitely not very automated, stuff to do later. --- SrcMod/Shell/Modules/ConfigModule.cs | 101 +++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/SrcMod/Shell/Modules/ConfigModule.cs b/SrcMod/Shell/Modules/ConfigModule.cs index a206493..58e88a0 100644 --- a/SrcMod/Shell/Modules/ConfigModule.cs +++ b/SrcMod/Shell/Modules/ConfigModule.cs @@ -4,6 +4,7 @@ public static class ConfigModule { [Command("display")] + [Command("list")] public static void DisplayConfig(ConfigDisplayMode mode = ConfigDisplayMode.All) { switch (mode) @@ -26,6 +27,106 @@ public static class ConfigModule } } + [Command("add")] + [Command("append")] + public static void AppendConfigVariable(string name, string value) + { + Config config = Config.LoadedConfig; + + switch (name.Trim().ToLower()) + { + case "gamedirectories": + config.GameDirectories = config.GameDirectories.Append(value).ToArray(); + break; + + case "rununsafecommands": + throw new($"The config variable \"{name}\" is a single variable and cannot be appended to."); + + default: throw new($"Unknown config variable \"{name}\""); + } + + Config.LoadedConfig = config; + } + + [Command("delete")] + [Command("remove")] + public static void RemoveConfigVariable(string name, string value) + { + Config config = Config.LoadedConfig; + + switch (name.Trim().ToLower()) + { + case "gamedirectories": + config.GameDirectories = config.GameDirectories + .Where(x => x.Trim().ToLower() != value.Trim().ToLower()) + .ToArray(); + break; + + case "rununsafecommands": + throw new($"The config variable \"{name}\" is a single variable and cannot be appended to."); + + default: throw new($"Unknown config variable \"{name}\""); + } + + Config.LoadedConfig = config; + } + + [Command("reset")] + public static void ResetConfig(string name = "all") + { + Config config = Config.LoadedConfig; + + switch (name.Trim().ToLower()) + { + case "gamedirectories": + config.GameDirectories = Config.Defaults.GameDirectories; + break; + + case "rununsafecommands": + config.RunUnsafeCommands = Config.Defaults.RunUnsafeCommands; + break; + + case "all": + config = Config.Defaults; + break; + + default: throw new($"Unknown config variable \"{name}\""); + } + + Config.LoadedConfig = config; + } + + [Command("set")] + public static void SetConfigVariable(string name, string value) + { + Config config = Config.LoadedConfig; + + switch (name.Trim().ToLower()) + { + case "gamedirectories": + throw new($"The config variable \"{name}\" is a list and must be added or removed to."); + + case "rununsafecommands": + if (int.TryParse(value, out int intRes)) + { + AskMode mode = (AskMode)intRes; + if (!Enum.IsDefined(mode)) throw new($"(AskMode){value} is not a valid AskMode."); + config.RunUnsafeCommands = mode; + } + else if (Enum.TryParse(value, true, out AskMode modeRes)) + { + if (!Enum.IsDefined(modeRes)) throw new($"\"{value}\" is not a valid AskMode."); + config.RunUnsafeCommands = modeRes; + } + else throw new($"\"{value}\" is not a valid AskMode."); + break; + + default: throw new($"Unknown config variable \"{name}\""); + } + + Config.LoadedConfig = config; + } + private static void DisplayConfigAll() { DisplayConfigGameDirectories(); -- 2.49.0.windows.1 From b7a4333e2d39a2362c6d933996bcccd87748d718 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 17 Apr 2023 20:27:40 -0400 Subject: [PATCH 08/12] Added the ability to load custom assemblies to the shell. --- SrcMod/Shell/Shell.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 3a90b88..8516305 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -95,6 +95,26 @@ public class Shell ReloadDirectoryInfo(); } + public bool LoadModule(Type moduleType) + { + if (LoadedModules.Any(x => x.Type.FullName == moduleType.FullName)) return false; + + ModuleInfo? module = ModuleInfo.FromType(moduleType); + if (module is null) return false; + + LoadedModules.Add(module); + LoadedCommands.AddRange(module.Commands); + + return true; + } + public bool LoadModule() => LoadModule(typeof(T)); + public int LoadModules(Assembly moduleAssembly) + { + int loaded = 0; + foreach (Type moduleType in moduleAssembly.GetTypes()) if (LoadModule(moduleType)) loaded++; + return loaded; + } + public void AddHistory(HistoryItem item) => History.Add(item); public void UndoItem() { -- 2.49.0.windows.1 From b869de9be869dabb3ac23bda6254d77c03898923 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 17 Apr 2023 21:02:39 -0400 Subject: [PATCH 09/12] Directory info is now reloaded after every command. One liner fix. --- SrcMod/Shell/Shell.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 8516305..08de616 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -270,6 +270,8 @@ public class Shell activeCommand.Dispose(); activeCommand = null; } + + ReloadDirectoryInfo(); return; } } -- 2.49.0.windows.1 From 4d2e98ce427ad24ca104ed8060d6c0ec1db4d303 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 18 Apr 2023 08:31:40 -0400 Subject: [PATCH 10/12] Moved the loading bar to its own class. Small fix. --- SrcMod/Shell/LoadingBar.cs | 79 +++++++++++++++++++++ SrcMod/Shell/Modules/BaseModule.cs | 12 ++-- SrcMod/Shell/Modules/CompressionModule.cs | 6 +- SrcMod/Shell/Shell.cs | 4 +- SrcMod/Shell/Tools.cs | 83 ++--------------------- 5 files changed, 94 insertions(+), 90 deletions(-) create mode 100644 SrcMod/Shell/LoadingBar.cs diff --git a/SrcMod/Shell/LoadingBar.cs b/SrcMod/Shell/LoadingBar.cs new file mode 100644 index 0000000..f658c8e --- /dev/null +++ b/SrcMod/Shell/LoadingBar.cs @@ -0,0 +1,79 @@ +namespace SrcMod.Shell; + +internal static class LoadingBar +{ + public static int position = -1; + public static int bufferSize = 0; + public static int lastValue = -1; + public static float value = 0; + public static ConsoleColor color = Console.ForegroundColor; + + public static bool Enabled { get; private set; } + + public static void End(bool clear = true) + { + if (position == -1) throw new("No loading bar is active."); + + if (clear) + { + Int2 oldPos = (Console.CursorLeft, Console.CursorTop); + + Console.CursorLeft = 0; + Console.CursorTop = position; + Console.Write(new string(' ', Console.BufferWidth)); + Console.CursorLeft = 0; + + Console.SetCursorPosition(oldPos.x, oldPos.y); + } + position = -1; + Enabled = false; + } + public static void Set(float value, ConsoleColor? color = null) + { + const string left = " --- [", + right = "] --- "; + int barSize = Console.BufferWidth - left.Length - right.Length, + filled = (int)(barSize * value); + + if (filled == lastValue) return; + lastValue = filled; + + Int2 oldPos = (Console.CursorLeft, Console.CursorTop); + + LoadingBar.value = value; + LoadingBar.color = color ?? Console.ForegroundColor; + + // Erase last bar. + Console.SetCursorPosition(0, position); + Console.Write(new string(' ', bufferSize)); + Console.CursorLeft = 0; + + // Add new bar. + bufferSize = Console.BufferWidth; + + Write(left, newLine: false); + ConsoleColor oldFore = Console.ForegroundColor; + + if (color is not null) Console.ForegroundColor = color.Value; + Write(new string('=', filled), newLine: false); + if (color is not null) Console.ForegroundColor = oldFore; + Write(new string(' ', barSize - filled), newLine: false); + Write(right, newLine: false); + + if (oldPos.y == Console.CursorTop) oldPos.y++; + while (oldPos.y >= Console.BufferHeight) + { + Console.WriteLine(); + oldPos.y--; + position--; + } + Console.SetCursorPosition(oldPos.x, oldPos.y); + } + public static void Start(float value = 0, int? position = null, ConsoleColor? color = null) + { + if (LoadingBar.position != -1) throw new("The loading bar has already been enabled."); + LoadingBar.position = position ?? Console.CursorTop; + Enabled = true; + Set(value, color); + } +} diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 0436cb5..eae1fd6 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -45,7 +45,7 @@ public static class BaseModule Write($"Copying directory \"{source}\" to \"{destination}\"..."); - LoadingBarStart(); + LoadingBar.Start(); for (int i = 0; i < files.Length; i++) { string file = files[i], @@ -53,7 +53,7 @@ public static class BaseModule destFile = Path.Combine(destination, file); Directory.CreateDirectory(Path.GetDirectoryName(destFile)!); File.Copy(sourceFile, destFile); - LoadingBarSet((i + 1) / (float)files.Length, ConsoleColor.DarkGreen); + LoadingBar.Set((i + 1) / (float)files.Length, ConsoleColor.DarkGreen); Console.CursorLeft = 0; string message = $"{sourceFile}"; int remainder = Console.BufferWidth - message.Length; @@ -63,7 +63,7 @@ public static class BaseModule Write(message, newLine: false); } - LoadingBarEnd(); + LoadingBar.End(); Console.CursorLeft = 0; Write(new string(' ', Console.BufferWidth), newLine: false); @@ -361,13 +361,13 @@ public static class BaseModule Thread.Sleep(rand.Next(500, 1000)); - LoadingBarStart(); + LoadingBar.Start(); for (int i = 0; i < files.Length; i++) { FileInfo file = new(files[i]); Thread.Sleep((int)(rand.Next(50, 100) * (file.Length >> 20))); - LoadingBarSet((i + 1) / (float)files.Length, ConsoleColor.Red); + LoadingBar.Set((i + 1) / (float)files.Length, ConsoleColor.Red); Console.CursorLeft = 0; string message = $"{files[i]}"; int remainder = Console.BufferWidth - message.Length; @@ -377,7 +377,7 @@ public static class BaseModule Write(message, newLine: false); } - LoadingBarEnd(); + LoadingBar.End(); Console.CursorLeft = 0; Write(new string(' ', Console.BufferWidth), newLine: false); diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index 43d5804..128dcf9 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -168,7 +168,7 @@ public static class CompressionModule int failed = 0; - LoadingBarStart(); + LoadingBar.Start(); for (int i = 0; i < files.Count; i++) { bool failedThisTime = false; @@ -181,7 +181,7 @@ public static class CompressionModule failedThisTime = true; failed++; } - LoadingBarSet((i + 1) / (float)files.Count, failedThisTime ? ConsoleColor.Red : ConsoleColor.DarkGreen); ; + LoadingBar.Set((i + 1) / (float)files.Count, failedThisTime ? ConsoleColor.Red : ConsoleColor.DarkGreen); ; Console.CursorLeft = 0; string message = $"{relative[i]}"; int remainder = Console.BufferWidth - message.Length; @@ -194,7 +194,7 @@ public static class CompressionModule archive.Dispose(); writer.Dispose(); - LoadingBarEnd(); + LoadingBar.End(); Console.CursorLeft = 0; Write(new string(' ', Console.BufferWidth), newLine: false); diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 08de616..58cd52e 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -247,12 +247,12 @@ public class Shell catch (TargetInvocationException ex) { Write($"[ERROR] {ex.InnerException!.Message}", ConsoleColor.Red); - if (LoadingBarEnabled) LoadingBarEnd(); + if (LoadingBar.Enabled) LoadingBar.End(); } catch (Exception ex) { Write($"[ERROR] {ex.Message}", ConsoleColor.Red); - if (LoadingBarEnabled) LoadingBarEnd(); + if (LoadingBar.Enabled) LoadingBar.End(); } #endif } diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index 3fea015..ef6cc12 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -2,16 +2,8 @@ namespace SrcMod.Shell; -public static class Tools +internal static class Tools { - private static int loadingPosition = -1; - private static int lastLoadingBufferSize = 0; - private static int lastLoadingValue = -1; - private static float loadingBarValue = 0; - private static ConsoleColor loadingBarColor = Console.ForegroundColor; - - public static bool LoadingBarEnabled { get; private set; } - public static void DisplayWithPages(IEnumerable lines, ConsoleColor? color = null) { int written = 0; @@ -73,73 +65,6 @@ public static class Tools return allFiles; } - public static void LoadingBarEnd(bool clear = true) - { - if (loadingPosition == -1) throw new("No loading bar is active."); - - if (clear) - { - Int2 oldPos = (Console.CursorLeft, Console.CursorTop); - - Console.CursorLeft = 0; - Console.CursorTop = loadingPosition; - Console.Write(new string(' ', Console.BufferWidth)); - Console.CursorLeft = 0; - - Console.SetCursorPosition(oldPos.x, oldPos.y); - } - loadingPosition = -1; - LoadingBarEnabled = false; - } - public static void LoadingBarSet(float value, ConsoleColor? color = null) - { - const string left = " --- [", - right = "] --- "; - int barSize = Console.BufferWidth - left.Length - right.Length, - filled = (int)(barSize * value); - - if (filled == lastLoadingValue) return; - lastLoadingValue = filled; - - Int2 oldPos = (Console.CursorLeft, Console.CursorTop); - - loadingBarValue = value; - loadingBarColor = color ?? Console.ForegroundColor; - - // Erase last bar. - Console.SetCursorPosition(0, loadingPosition); - Console.Write(new string(' ', lastLoadingBufferSize)); - Console.CursorLeft = 0; - - // Add new bar. - lastLoadingBufferSize = Console.BufferWidth; - - Write(left, newLine: false); - ConsoleColor oldFore = Console.ForegroundColor; - - if (color is not null) Console.ForegroundColor = color.Value; - Write(new string('=', filled), newLine: false); - if (color is not null) Console.ForegroundColor = oldFore; - Write(new string(' ', barSize - filled), newLine: false); - Write(right, newLine: false); - - if (oldPos.y == Console.CursorTop) oldPos.y++; - while (oldPos.y >= Console.BufferHeight) - { - Console.WriteLine(); - oldPos.y--; - loadingPosition--; - } - Console.SetCursorPosition(oldPos.x, oldPos.y); - } - public static void LoadingBarStart(float value = 0, int? position = null, ConsoleColor? color = null) - { - if (loadingPosition != -1) throw new("The loading bar has already been enabled."); - loadingPosition = position ?? Console.CursorTop; - LoadingBarEnabled = true; - LoadingBarSet(value, color); - } - public static void Write(object? message, ConsoleColor? col = null, bool newLine = true) { ConsoleColor prevCol = Console.ForegroundColor; @@ -150,10 +75,10 @@ public static class Tools Console.ForegroundColor = prevCol; - if (newLine && LoadingBarEnabled && Console.CursorTop >= Console.BufferHeight - 1) + if (newLine && LoadingBar.Enabled && Console.CursorTop >= Console.BufferHeight - 1) { - loadingPosition--; - LoadingBarSet(loadingBarValue, loadingBarColor); + LoadingBar.position--; + LoadingBar.Set(LoadingBar.value, LoadingBar.color); } } -- 2.49.0.windows.1 From fd4162aa32b56328806c6267bfe2408a06cfaf66 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 18 Apr 2023 08:52:04 -0400 Subject: [PATCH 11/12] How on earth did I miss this? --- SrcMod/Shell/Tools.cs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index 3747dc6..cebd629 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -4,17 +4,8 @@ namespace SrcMod.Shell; internal static class Tools { -<<<<<<< HEAD public static JsonSerializer Serializer { get; private set; } - private static int loadingPosition = -1; - private static int lastLoadingBufferSize = 0; - private static int lastLoadingValue = -1; - private static float loadingBarValue = 0; - private static ConsoleColor loadingBarColor = Console.ForegroundColor; - - public static bool LoadingBarEnabled { get; private set; } - static Tools() { Serializer = JsonSerializer.Create(new() @@ -24,8 +15,6 @@ internal static class Tools }); } -======= ->>>>>>> origin/shell-systems public static void DisplayWithPages(IEnumerable lines, ConsoleColor? color = null) { int written = 0; -- 2.49.0.windows.1 From 25c6d152a2ead697d37ab913ebc072c155a72d5a Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 18 Apr 2023 08:57:45 -0400 Subject: [PATCH 12/12] Removed some test stuff. --- SrcMod/Shell/ObjectModels/Config.cs | 6 +----- SrcMod/Shell/Tools.cs | 4 +--- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/SrcMod/Shell/ObjectModels/Config.cs b/SrcMod/Shell/ObjectModels/Config.cs index e76ce48..a14e6df 100644 --- a/SrcMod/Shell/ObjectModels/Config.cs +++ b/SrcMod/Shell/ObjectModels/Config.cs @@ -23,11 +23,7 @@ public struct Config { Defaults = new() { - GameDirectories = new[] - { - "testing 1", - "testing 2" - }, + GameDirectories = Array.Empty(), RunUnsafeCommands = AskMode.Ask }; } diff --git a/SrcMod/Shell/Tools.cs b/SrcMod/Shell/Tools.cs index cebd629..c90265d 100644 --- a/SrcMod/Shell/Tools.cs +++ b/SrcMod/Shell/Tools.cs @@ -1,6 +1,4 @@ -using System.Numerics; - -namespace SrcMod.Shell; +namespace SrcMod.Shell; internal static class Tools { -- 2.49.0.windows.1