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; } }