Made the config remove system automated.
This commit is contained in:
parent
409817f208
commit
b446feefe7
@ -59,23 +59,32 @@ public static class ConfigModule
|
|||||||
[Command("remove")]
|
[Command("remove")]
|
||||||
public static void RemoveConfigVariable(string name, string value)
|
public static void RemoveConfigVariable(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 \"{chosenField.Name}\" is not an array" +
|
||||||
config.GameDirectories = config.GameDirectories
|
" and cannot have data added or removed from it." +
|
||||||
.Where(x => x.Trim().ToLower() != value.Trim().ToLower())
|
" Instead, set or reset the variable.");
|
||||||
.ToArray();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "rununsafecommands":
|
object parsed = TypeParsers.ParseAll(value);
|
||||||
throw new($"The config variable \"{name}\" is a single variable and cannot be appended to.");
|
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}\"");
|
Type arrayType = chosenField.FieldType.GetElementType()!;
|
||||||
}
|
|
||||||
|
|
||||||
Config.LoadedConfig = config;
|
Array arrayValue = (Array)chosenField.GetValue(Config.LoadedConfig)!;
|
||||||
|
ArrayList collection = new(arrayValue);
|
||||||
|
if (!collection.Contains(parsed)) throw new($"The value \"{value}\" is not contained in this variable.");
|
||||||
|
collection.Remove(parsed);
|
||||||
|
|
||||||
|
chosenField.SetValue(Config.LoadedConfig, collection.ToArray()!.CastArray(arrayType));
|
||||||
|
DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("reset")]
|
[Command("reset")]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user