Added automated config setting. Next up resetting.

This commit is contained in:
That_One_Nerd 2023-04-19 14:02:23 -04:00
parent 647993ef1f
commit dd62e66871
3 changed files with 37 additions and 33 deletions

View File

@ -97,32 +97,24 @@ public static class ConfigModule
[Command("set")] [Command("set")]
public static void SetConfigVariable(string name, string value) 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()) FieldInfo? chosenField = validFields.FirstOrDefault(x => x.Name.Trim().ToLower() == name.Trim().ToLower());
{ if (chosenField is null) throw new($"No valid config variable named \"{name}\".");
case "gamedirectories": else if (chosenField.FieldType.IsArray) throw new($"The variable \"{name}\" is an array and cannot be" +
throw new($"The config variable \"{name}\" is a list and must be added or removed to."); " directly set. Instead, add or remove items from it.");
case "rununsafecommands": object parsed = TypeParsers.ParseAll(value);
if (int.TryParse(value, out int intRes)) if (parsed is string parsedStr
{ && chosenField.FieldType.IsEnum
AskMode mode = (AskMode)intRes; && Enum.TryParse(chosenField.FieldType, parsedStr, true, out object? obj)) parsed = obj;
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;
default: throw new($"Unknown config variable \"{name}\""); chosenField.SetValue(Config.LoadedConfig, parsed);
} DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
Config.LoadedConfig = config;
} }
private static void DisplayConfigAll() private static void DisplayConfigAll()

View File

@ -1,6 +1,6 @@
namespace SrcMod.Shell.ObjectModels; namespace SrcMod.Shell.ObjectModels;
public struct Config public class Config
{ {
public const string FilePath = "config.json"; public const string FilePath = "config.json";
@ -26,16 +26,26 @@ public struct Config
GameDirectories = Array.Empty<string>(), GameDirectories = Array.Empty<string>(),
RunUnsafeCommands = AskMode.Ask RunUnsafeCommands = AskMode.Ask
}; };
p_applied = Defaults;
} }
public string[] GameDirectories; public string[] GameDirectories;
public AskMode RunUnsafeCommands; public AskMode RunUnsafeCommands;
public Config ApplyChanges(ConfigChanges changes) => this with internal Config()
{ {
GameDirectories = GameDirectories.Union(changes.GameDirectories ?? Array.Empty<string>()).ToArray(), GameDirectories = Array.Empty<string>();
RunUnsafeCommands = changes.RunUnsafeCommands ?? RunUnsafeCommands }
};
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) public ConfigChanges GetChanges(Config? baseConfig = null)
{ {
Config reference = baseConfig ?? Defaults; Config reference = baseConfig ?? Defaults;
@ -65,21 +75,23 @@ public struct Config
jsonReader.Close(); jsonReader.Close();
reader.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) public static void SaveConfig(string basePath)
{ {
string fullPath = Path.Combine(basePath, FilePath); 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); if (File.Exists(fullPath)) File.Delete(fullPath);
return; return;
} }
StreamWriter writer = new(fullPath); StreamWriter writer = new(fullPath);
JsonTextWriter jsonWriter = new(writer); JsonTextWriter jsonWriter = new(writer)
jsonWriter.Indentation = 4; {
Indentation = 4
};
Serializer.Serialize(jsonWriter, p_changes); Serializer.Serialize(jsonWriter, p_changes);
jsonWriter.Close(); jsonWriter.Close();
writer.Close(); writer.Close();

View File

@ -1,6 +1,6 @@
namespace SrcMod.Shell.ObjectModels; namespace SrcMod.Shell.ObjectModels;
public record struct ConfigChanges public record class ConfigChanges
{ {
[JsonIgnore] [JsonIgnore]
public bool HasChange => GameDirectories is not null || RunUnsafeCommands is not null; public bool HasChange => GameDirectories is not null || RunUnsafeCommands is not null;