-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddonJson.cs
43 lines (36 loc) · 1.52 KB
/
AddonJson.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
namespace Gami.Core;
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class AddonJson
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web);
private static string GetJsonPath(string addon) => Path.Join(Consts.BasePluginDir, addon + ".json");
public static ValueTask<T?> LoadAsync<T>(string addon) where T : class
{
var path = GetJsonPath(addon);
if (!File.Exists(path))
return ValueTask.FromResult<T?>(null);
var content = File.Open(path, FileMode.Open);
return JsonSerializer.DeserializeAsync<T>(content, JsonSerializerOptions);
}
public static async ValueTask<T> LoadOrErrorAsync<T>(string addon) where T : class
{
var result = await LoadAsync<T>(addon);
return result == null ? throw new ApplicationException($"Plugin {addon} needs JSON at {GetJsonPath(addon)}") : result;
}
public static T? Load<T>(string addon) where T : class
{
var path = GetJsonPath(addon);
if (!File.Exists(path))
return null;
var content = File.ReadAllText(path);
return JsonSerializer.Deserialize<T>(content, JsonSerializerOptions);
}
public static async ValueTask Save<T>(T data, string addon) where T : class
{
var path = GetJsonPath(addon);
var text = JsonSerializer.Serialize(data, JsonSerializerOptions);
await File.WriteAllTextAsync(path, text);
}
}