Ready for alpha 0.4.0 (the final alpha version (hopefully)) #72

Merged
That-One-Nerd merged 16 commits from shell-configuration into main 2023-04-25 17:42:32 -04:00
4 changed files with 54 additions and 26 deletions
Showing only changes of commit 2420e16fa4 - Show all commits

View File

@ -0,0 +1,20 @@
namespace SrcMod.Shell.Extensions;
public static class ConversionExtension
{
public static T Cast<T>(this object obj) => (T)Cast(obj, typeof(T));
public static object Cast(this object obj, Type newType) => Convert.ChangeType(obj, newType);
public static object CastArray(this object[] obj, Type newElementType)
{
Array result = Array.CreateInstance(newElementType, obj.Length);
for (int i = 0; i < obj.Length; i++) result.SetValue(obj[i].Cast(newElementType), i);
return result;
}
public static T[] CastArray<T>(this object[] obj)
{
Array result = Array.CreateInstance(typeof(T), obj.Length);
for (int i = 0; i < obj.Length; i++) result.SetValue(obj[i].Cast<T>(), i);
return (T[])result;
}
}

View File

@ -3,6 +3,7 @@ global using Newtonsoft.Json;
global using SharpCompress.Archives.Rar;
global using SharpCompress.Archives.SevenZip;
global using SharpCompress.Readers;
global using SrcMod.Shell.Extensions;
global using SrcMod.Shell.Interop;
global using SrcMod.Shell.Modules.ObjectModels;
global using SrcMod.Shell.ObjectModels;

View File

@ -29,21 +29,30 @@ public static class ConfigModule
[Command("append")]
public static void AppendConfigVariable(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":
config.GameDirectories = config.GameDirectories.Append(value).ToArray();
break;
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 \"{chosenField.Name}\" is not an array" +
" and cannot have data added or removed from it." +
" Instead, set or reset the variable.");
case "rununsafecommands":
throw new($"The config variable \"{name}\" is a single variable and cannot be appended to.");
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}\"");
}
Type arrayType = chosenField.FieldType.GetElementType()!;
Config.LoadedConfig = config;
Array arrayValue = (Array)chosenField.GetValue(Config.LoadedConfig)!;
ArrayList collection = new(arrayValue) { parsed };
chosenField.SetValue(Config.LoadedConfig, collection.ToArray()!.CastArray(arrayType));
DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
}
[Command("delete")]
@ -96,8 +105,9 @@ public static class ConfigModule
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.");
else if (chosenField.FieldType.IsArray) throw new($"The variable \"{chosenField.Name}\" is an array and" +
" cannot be directly set. Instead, add or remove items" +
" from it.");
object parsed = TypeParsers.ParseAll(value);
if (parsed is string parsedStr
@ -124,27 +134,20 @@ public static class ConfigModule
Write(new string(' ', indents * 4), newLine: false);
if (!string.IsNullOrWhiteSpace(name)) Write($"{name}: ", newLine: false);
if (item is IEnumerable itemEnumerable)
if (item is null) Write("null", ConsoleColor.DarkRed, newLine);
else if (item is Array itemArray)
{
// This is a bit inefficient.
int count = 0;
foreach (object _ in itemEnumerable) count++;
object[] itemData = new object[count];
count = 0;
foreach (object obj in itemEnumerable) itemData[count] = obj;
if (itemData.Length < 1)
if (itemArray.Length < 1)
{
Write("[]", ConsoleColor.DarkGray, newLine);
return;
}
Write("[", ConsoleColor.DarkGray);
for (int i = 0; i < itemData.Length; i++)
for (int i = 0; i < itemArray.Length; i++)
{
DisplayConfigItem(itemData.GetValue(i), indents + 1, newLine: false);
if (i < itemData.Length - 1) Write(',', newLine: false);
DisplayConfigItem(itemArray.GetValue(i), indents + 1, newLine: false);
if (i < itemArray.Length - 1) Write(',', newLine: false);
Write('\n', newLine: false);
}
Write(new string(' ', indents * 4) + "]", ConsoleColor.DarkGray, newLine);

View File

@ -36,4 +36,8 @@
<PackageReference Include="SharpCompress" Version="0.33.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="NewFolder\" />
</ItemGroup>
</Project>