From dd62e66871cab8c55670cba34b2c7b2d27f91c5a Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 19 Apr 2023 14:02:23 -0400 Subject: [PATCH] Added automated config setting. Next up resetting. --- SrcMod/Shell/Modules/ConfigModule.cs | 38 +++++++++------------- SrcMod/Shell/ObjectModels/Config.cs | 30 ++++++++++++----- SrcMod/Shell/ObjectModels/ConfigChanges.cs | 2 +- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/SrcMod/Shell/Modules/ConfigModule.cs b/SrcMod/Shell/Modules/ConfigModule.cs index 98608c7..ffc5ca0 100644 --- a/SrcMod/Shell/Modules/ConfigModule.cs +++ b/SrcMod/Shell/Modules/ConfigModule.cs @@ -97,32 +97,24 @@ public static class ConfigModule [Command("set")] public static void SetConfigVariable(string name, string value) { - Config config = Config.LoadedConfig; + FieldInfo[] validFields = (from field in typeof(Config).GetFields() + let isPublic = field.IsPublic + let isStatic = field.IsStatic + where isPublic && !isStatic + select field).ToArray(); - switch (name.Trim().ToLower()) - { - case "gamedirectories": - throw new($"The config variable \"{name}\" is a list and must be added or removed to."); + FieldInfo? chosenField = validFields.FirstOrDefault(x => x.Name.Trim().ToLower() == name.Trim().ToLower()); + if (chosenField is null) throw new($"No valid config variable named \"{name}\"."); + else if (chosenField.FieldType.IsArray) throw new($"The variable \"{name}\" is an array and cannot be" + + " directly set. Instead, add or remove items from it."); - 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; + object parsed = TypeParsers.ParseAll(value); + if (parsed is string parsedStr + && chosenField.FieldType.IsEnum + && Enum.TryParse(chosenField.FieldType, parsedStr, true, out object? obj)) parsed = obj; - default: throw new($"Unknown config variable \"{name}\""); - } - - Config.LoadedConfig = config; + chosenField.SetValue(Config.LoadedConfig, parsed); + DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name); } private static void DisplayConfigAll() diff --git a/SrcMod/Shell/ObjectModels/Config.cs b/SrcMod/Shell/ObjectModels/Config.cs index f66f5e7..0c272f8 100644 --- a/SrcMod/Shell/ObjectModels/Config.cs +++ b/SrcMod/Shell/ObjectModels/Config.cs @@ -1,6 +1,6 @@ namespace SrcMod.Shell.ObjectModels; -public struct Config +public class Config { public const string FilePath = "config.json"; @@ -26,16 +26,26 @@ public struct Config GameDirectories = Array.Empty(), RunUnsafeCommands = AskMode.Ask }; + p_applied = Defaults; } public string[] GameDirectories; public AskMode RunUnsafeCommands; - public Config ApplyChanges(ConfigChanges changes) => this with + internal Config() { - GameDirectories = GameDirectories.Union(changes.GameDirectories ?? Array.Empty()).ToArray(), - RunUnsafeCommands = changes.RunUnsafeCommands ?? RunUnsafeCommands - }; + GameDirectories = Array.Empty(); + } + + public Config ApplyChanges(ConfigChanges changes) + { + if (changes.GameDirectories is not null) + GameDirectories = GameDirectories.Union(changes.GameDirectories).ToArray(); + + if (changes.RunUnsafeCommands is not null) RunUnsafeCommands = changes.RunUnsafeCommands.Value; + + return this; + } public ConfigChanges GetChanges(Config? baseConfig = null) { Config reference = baseConfig ?? Defaults; @@ -65,21 +75,23 @@ public struct Config jsonReader.Close(); reader.Close(); - p_applied = p_changes is null ? Defaults : Defaults.ApplyChanges(p_changes.Value); + p_applied = p_changes is null ? Defaults : Defaults.ApplyChanges(p_changes); } public static void SaveConfig(string basePath) { string fullPath = Path.Combine(basePath, FilePath); - if (p_changes is null || !p_changes.Value.HasChange) + if (p_changes is null || !p_changes.HasChange) { if (File.Exists(fullPath)) File.Delete(fullPath); return; } StreamWriter writer = new(fullPath); - JsonTextWriter jsonWriter = new(writer); - jsonWriter.Indentation = 4; + JsonTextWriter jsonWriter = new(writer) + { + Indentation = 4 + }; Serializer.Serialize(jsonWriter, p_changes); jsonWriter.Close(); writer.Close(); diff --git a/SrcMod/Shell/ObjectModels/ConfigChanges.cs b/SrcMod/Shell/ObjectModels/ConfigChanges.cs index 8328796..e527d5f 100644 --- a/SrcMod/Shell/ObjectModels/ConfigChanges.cs +++ b/SrcMod/Shell/ObjectModels/ConfigChanges.cs @@ -1,6 +1,6 @@ namespace SrcMod.Shell.ObjectModels; -public record struct ConfigChanges +public record class ConfigChanges { [JsonIgnore] public bool HasChange => GameDirectories is not null || RunUnsafeCommands is not null;