From 68336df8686a1061c98493709a9dff87f142f5a4 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Mon, 10 Apr 2023 13:27:55 -0400 Subject: [PATCH] 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;