Made the display system of the config data a gazillion times better.
This commit is contained in:
parent
25c6d152a2
commit
8677ff464d
@ -5,24 +5,20 @@ public static class ConfigModule
|
|||||||
{
|
{
|
||||||
[Command("display")]
|
[Command("display")]
|
||||||
[Command("list")]
|
[Command("list")]
|
||||||
public static void DisplayConfig(ConfigDisplayMode mode = ConfigDisplayMode.All)
|
public static void DisplayConfig(string display = "all")
|
||||||
{
|
{
|
||||||
switch (mode)
|
switch (display.Trim().ToLower())
|
||||||
{
|
{
|
||||||
case ConfigDisplayMode.Raw:
|
case "all":
|
||||||
DisplayConfigRaw();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ConfigDisplayMode.All:
|
|
||||||
DisplayConfigAll();
|
DisplayConfigAll();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ConfigDisplayMode.GameDirectories:
|
case "raw":
|
||||||
DisplayConfigGameDirectories();
|
DisplayConfigRaw();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ConfigDisplayMode.RunUnsafeCommands:
|
default:
|
||||||
DisplayConfigUnsafeCommands();
|
DisplayConfigName(display);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,66 +125,76 @@ public static class ConfigModule
|
|||||||
|
|
||||||
private static void DisplayConfigAll()
|
private static void DisplayConfigAll()
|
||||||
{
|
{
|
||||||
DisplayConfigGameDirectories();
|
FieldInfo[] validFields = (from field in typeof(Config).GetFields()
|
||||||
DisplayConfigUnsafeCommands();
|
let isPublic = field.IsPublic
|
||||||
|
let isStatic = field.IsStatic
|
||||||
|
where isPublic && !isStatic
|
||||||
|
select field).ToArray();
|
||||||
|
|
||||||
|
foreach (FieldInfo field in validFields)
|
||||||
|
DisplayConfigItem(field.GetValue(Config.LoadedConfig), name: field.Name);
|
||||||
}
|
}
|
||||||
private static void DisplayConfigRaw()
|
private static void DisplayConfigItem<T>(T item, int indents = 0, string name = "", bool newLine = true)
|
||||||
{
|
{
|
||||||
// This is definitely a bit inefficient, but shouldn't be too much of an issue.
|
Write(new string(' ', indents * 4), newLine: false);
|
||||||
|
if (!string.IsNullOrWhiteSpace(name)) Write($"{name}: ", newLine: false);
|
||||||
|
|
||||||
MemoryStream ms = new();
|
if (item is Array itemArray)
|
||||||
StreamWriter writer = new(ms, leaveOpen: true);
|
|
||||||
JsonTextWriter jsonWriter = new(writer);
|
|
||||||
|
|
||||||
Serializer.Serialize(jsonWriter, Config.LoadedConfig);
|
|
||||||
|
|
||||||
jsonWriter.Close();
|
|
||||||
writer.Close();
|
|
||||||
ms.Position = 0;
|
|
||||||
|
|
||||||
StreamReader reader = new(ms);
|
|
||||||
string msg = reader.ReadToEnd();
|
|
||||||
|
|
||||||
Write(msg);
|
|
||||||
|
|
||||||
reader.Close();
|
|
||||||
ms.Close();
|
|
||||||
}
|
|
||||||
private static void DisplayConfigGameDirectories()
|
|
||||||
{
|
|
||||||
Write("Steam Game Directories: ", null, false);
|
|
||||||
if (Config.LoadedConfig.GameDirectories is null || Config.LoadedConfig.GameDirectories.Length <= 0)
|
|
||||||
Write("None", ConsoleColor.DarkGray);
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
Write("[", ConsoleColor.DarkGray);
|
if (itemArray.Length < 1)
|
||||||
for (int i = 0; i < Config.LoadedConfig.GameDirectories.Length; i++)
|
|
||||||
{
|
{
|
||||||
Write(" \"", ConsoleColor.DarkGray, false);
|
Write("[]", ConsoleColor.DarkGray, newLine);
|
||||||
Write(Config.LoadedConfig.GameDirectories[i], ConsoleColor.White, false);
|
return;
|
||||||
if (i < Config.LoadedConfig.GameDirectories.Length - 1) Write("\",", ConsoleColor.DarkGray);
|
|
||||||
else Write("\"", ConsoleColor.DarkGray);
|
|
||||||
}
|
}
|
||||||
Write("]", ConsoleColor.DarkGray);
|
|
||||||
|
Write("[", ConsoleColor.DarkGray);
|
||||||
|
for (int i = 0; i < itemArray.Length; i++)
|
||||||
|
{
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
else if (item is byte itemByte) Write($"0x{itemByte:X2}", ConsoleColor.Yellow, newLine);
|
||||||
private static void DisplayConfigUnsafeCommands()
|
else if (item is sbyte or short or ushort or int or uint or long or ulong)
|
||||||
{
|
Write(item, ConsoleColor.Yellow, newLine);
|
||||||
Write("Run Unsafe Commands: ", null, false);
|
else if (item is char)
|
||||||
ConsoleColor color = Config.LoadedConfig.RunUnsafeCommands switch
|
{
|
||||||
|
Write("\'", ConsoleColor.DarkGray, false);
|
||||||
|
Write(item, ConsoleColor.Blue, false);
|
||||||
|
Write("\'", ConsoleColor.DarkGray, newLine);
|
||||||
|
}
|
||||||
|
else if (item is string)
|
||||||
|
{
|
||||||
|
Write("\"", ConsoleColor.DarkGray, false);
|
||||||
|
Write(item, ConsoleColor.DarkCyan, false);
|
||||||
|
Write("\"", ConsoleColor.DarkGray, newLine);
|
||||||
|
}
|
||||||
|
else if (item is AskMode) Write(item, item switch
|
||||||
{
|
{
|
||||||
AskMode.Never => ConsoleColor.Red,
|
AskMode.Never => ConsoleColor.Red,
|
||||||
AskMode.Always => ConsoleColor.Green,
|
AskMode.Always => ConsoleColor.Green,
|
||||||
AskMode.Ask or _ => ConsoleColor.DarkGray
|
AskMode.Ask or _ => ConsoleColor.DarkGray
|
||||||
};
|
}, newLine);
|
||||||
Write(Config.LoadedConfig.RunUnsafeCommands, color);
|
else Write(item, newLine: newLine);
|
||||||
}
|
}
|
||||||
|
private static void DisplayConfigName(string name)
|
||||||
public enum ConfigDisplayMode
|
|
||||||
{
|
{
|
||||||
Raw,
|
FieldInfo[] validFields = (from field in typeof(Config).GetFields()
|
||||||
All,
|
let isPublic = field.IsPublic
|
||||||
GameDirectories,
|
let isStatic = field.IsStatic
|
||||||
RunUnsafeCommands
|
where isPublic && !isStatic
|
||||||
|
select field).ToArray();
|
||||||
|
|
||||||
|
FieldInfo? chosenField = validFields.FirstOrDefault(x => x.Name.Trim().ToLower() == name.Trim().ToLower());
|
||||||
|
if (chosenField is null) throw new($"No config variable named \"{name}\".");
|
||||||
|
|
||||||
|
DisplayConfigItem(chosenField.GetValue(Config.LoadedConfig), name: chosenField.Name);
|
||||||
|
}
|
||||||
|
private static void DisplayConfigRaw()
|
||||||
|
{
|
||||||
|
string json = JsonConvert.SerializeObject(Config.LoadedConfig, Formatting.Indented);
|
||||||
|
Write(json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -79,6 +79,7 @@ public struct Config
|
|||||||
|
|
||||||
StreamWriter writer = new(fullPath);
|
StreamWriter writer = new(fullPath);
|
||||||
JsonTextWriter jsonWriter = new(writer);
|
JsonTextWriter jsonWriter = new(writer);
|
||||||
|
jsonWriter.Indentation = 4;
|
||||||
Serializer.Serialize(jsonWriter, p_changes);
|
Serializer.Serialize(jsonWriter, p_changes);
|
||||||
jsonWriter.Close();
|
jsonWriter.Close();
|
||||||
writer.Close();
|
writer.Close();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user