Ready for alpha 0.4.0 (the final alpha version (hopefully)) #72
@ -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()
|
||||
|
||||
@ -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<string>(),
|
||||
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<string>()).ToArray(),
|
||||
RunUnsafeCommands = changes.RunUnsafeCommands ?? RunUnsafeCommands
|
||||
};
|
||||
GameDirectories = Array.Empty<string>();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user