Fixed the config not saving to its file.

This commit is contained in:
That_One_Nerd 2023-04-20 08:20:18 -04:00
parent b446feefe7
commit 35885b8724
3 changed files with 26 additions and 25 deletions

View File

@ -52,6 +52,7 @@ public static class ConfigModule
ArrayList collection = new(arrayValue) { parsed }; ArrayList collection = new(arrayValue) { parsed };
chosenField.SetValue(Config.LoadedConfig, collection.ToArray()!.CastArray(arrayType)); chosenField.SetValue(Config.LoadedConfig, collection.ToArray()!.CastArray(arrayType));
Config.UpdateChanges();
DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name); DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
} }
@ -84,6 +85,7 @@ public static class ConfigModule
collection.Remove(parsed); collection.Remove(parsed);
chosenField.SetValue(Config.LoadedConfig, collection.ToArray()!.CastArray(arrayType)); chosenField.SetValue(Config.LoadedConfig, collection.ToArray()!.CastArray(arrayType));
Config.UpdateChanges();
DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name); DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
} }
@ -124,6 +126,7 @@ public static class ConfigModule
&& Enum.TryParse(chosenField.FieldType, parsedStr, true, out object? obj)) parsed = obj; && Enum.TryParse(chosenField.FieldType, parsedStr, true, out object? obj)) parsed = obj;
chosenField.SetValue(Config.LoadedConfig, parsed); chosenField.SetValue(Config.LoadedConfig, parsed);
Config.UpdateChanges();
DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name); DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
} }
@ -216,6 +219,7 @@ public static class ConfigModule
if (chosenField is null) throw new($"No valid config variable named \"{name}\"."); if (chosenField is null) throw new($"No valid config variable named \"{name}\".");
chosenField.SetValue(Config.LoadedConfig, chosenField.GetValue(Config.Defaults)); chosenField.SetValue(Config.LoadedConfig, chosenField.GetValue(Config.Defaults));
Config.UpdateChanges();
DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name); DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
} }
} }

View File

@ -4,7 +4,7 @@ public class Config
{ {
public const string FilePath = "config.json"; public const string FilePath = "config.json";
public static Config Defaults => (Config)p_defaults.MemberwiseClone(); public static Config Defaults => new();
public static Config LoadedConfig public static Config LoadedConfig
{ {
@ -12,22 +12,15 @@ public class Config
set set
{ {
p_applied = value; p_applied = value;
p_changes = p_applied.GetChanges(Defaults); UpdateChanges();
} }
} }
private static Config p_applied; private static Config p_applied;
private static ConfigChanges? p_changes; private static Changes? p_changes;
private static readonly Config p_defaults;
static Config() static Config()
{ {
p_defaults = new()
{
GameDirectories = Array.Empty<string>(),
RunUnsafeCommands = AskMode.Ask
};
p_applied = Defaults; p_applied = Defaults;
} }
@ -37,9 +30,10 @@ public class Config
internal Config() internal Config()
{ {
GameDirectories = Array.Empty<string>(); GameDirectories = Array.Empty<string>();
RunUnsafeCommands = AskMode.Ask;
} }
public Config ApplyChanges(ConfigChanges changes) public Config ApplyChanges(Changes changes)
{ {
if (changes.GameDirectories is not null) if (changes.GameDirectories is not null)
GameDirectories = GameDirectories.Union(changes.GameDirectories).ToArray(); GameDirectories = GameDirectories.Union(changes.GameDirectories).ToArray();
@ -48,10 +42,10 @@ public class Config
return this; return this;
} }
public ConfigChanges GetChanges(Config? baseConfig = null) public Changes GetChanges(Config? baseConfig = null)
{ {
Config reference = baseConfig ?? Defaults; Config reference = baseConfig ?? Defaults;
ConfigChanges changes = new() Changes changes = new()
{ {
GameDirectories = reference.GameDirectories == GameDirectories ? null : GameDirectories = reference.GameDirectories == GameDirectories ? null :
GameDirectories.Where(x => !reference.GameDirectories.Contains(x)).ToArray(), GameDirectories.Where(x => !reference.GameDirectories.Contains(x)).ToArray(),
@ -73,7 +67,7 @@ public class Config
} }
StreamReader reader = new(fullPath); StreamReader reader = new(fullPath);
JsonTextReader jsonReader = new(reader); JsonTextReader jsonReader = new(reader);
p_changes = Serializer.Deserialize<ConfigChanges?>(jsonReader); p_changes = Serializer.Deserialize<Changes?>(jsonReader);
jsonReader.Close(); jsonReader.Close();
reader.Close(); reader.Close();
@ -83,7 +77,7 @@ public class Config
{ {
string fullPath = Path.Combine(basePath, FilePath); string fullPath = Path.Combine(basePath, FilePath);
if (p_changes is null || !p_changes.HasChange) if (p_changes is null || !p_changes.Any())
{ {
if (File.Exists(fullPath)) File.Delete(fullPath); if (File.Exists(fullPath)) File.Delete(fullPath);
return; return;
@ -98,4 +92,17 @@ public class Config
jsonWriter.Close(); jsonWriter.Close();
writer.Close(); writer.Close();
} }
public static void UpdateChanges()
{
p_changes = p_applied.GetChanges(Defaults);
}
public class Changes
{
public string[]? GameDirectories;
public AskMode? RunUnsafeCommands;
public bool Any() => typeof(Changes).GetFields().Any(x => x.GetValue(this) is not null);
}
} }

View File

@ -1,10 +0,0 @@
namespace SrcMod.Shell.ObjectModels;
public record class ConfigChanges
{
[JsonIgnore]
public bool HasChange => GameDirectories is not null || RunUnsafeCommands is not null;
public string[]? GameDirectories;
public AskMode? RunUnsafeCommands;
}