New config system. Tracks changes instead of the whole thing.

This commit is contained in:
That-One-Nerd 2023-04-10 18:46:40 -04:00
parent 68336df868
commit b2bc9fa7ee
4 changed files with 86 additions and 48 deletions

View File

@ -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<ConfigChanges?>(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();
}
}

View File

@ -0,0 +1,8 @@
namespace SrcMod.Shell.ObjectModels;
public record struct ConfigChanges
{
public bool HasChange => SteamDirectories is not null;
public string[]? SteamDirectories;
}

View File

@ -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<ShellConfig>(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();
}
}

View File

@ -50,7 +50,7 @@ public class Shell
// Load config. // Load config.
if (ShellDirectory is null) Write("[WARNING] Could not load config from shell location. Defaults will be used."); 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. // Load modules and commands.
List<Assembly?> possibleAsms = new() List<Assembly?> 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."); 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; return;
} }
} }