utils for JSON files and RNG/rolling, abstracted arg parsing for commands

This commit is contained in:
Kyle Belanger
2024-02-18 23:11:58 -05:00
parent d2552b74b7
commit 7445a1f5d8
9 changed files with 232 additions and 10 deletions

View File

@@ -0,0 +1,35 @@
using System.Text.Json;
namespace BLHX.Server.Common.Utils;
public static class JSON
{
public static string ConfigPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json");
public static T Load<T>(string path) where T : new()
{
if (!File.Exists(path))
{
T obj = new T();
Save(path, obj);
}
return JsonSerializer.Deserialize<T>(File.ReadAllText(path));
}
public static void Save<T>(string path, T obj)
{
File.WriteAllText(path, JsonSerializer.Serialize(obj, new JsonSerializerOptions()
{
WriteIndented = true
}));
}
public static string Stringify<T>(T obj)
{
return JsonSerializer.Serialize(obj, new JsonSerializerOptions()
{
WriteIndented = true
});
}
}