48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace undead_universal_patch_il2cpp.Core;
|
|
|
|
public class DediConfig<T>
|
|
{
|
|
private readonly string _default;
|
|
public readonly string _name;
|
|
public readonly string path;
|
|
private T? cached;
|
|
private bool loaded = false;
|
|
private readonly JsonSerializerOptions? _options;
|
|
|
|
public DediConfig(string name, string def, JsonSerializerOptions? options)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Name cannot be invalid");
|
|
|
|
_name = name;
|
|
path = $"BepInEx/config/Undead.{name}.json";
|
|
_default = def;
|
|
_options = options;
|
|
}
|
|
|
|
private string GetAlways() {
|
|
if (!File.Exists(path))
|
|
{
|
|
File.WriteAllText(path, _default);
|
|
return _default;
|
|
}
|
|
return File.ReadAllText(path);
|
|
}
|
|
public T Get()
|
|
{
|
|
if (loaded) return cached;
|
|
|
|
string data = GetAlways();
|
|
cached = JsonSerializer.Deserialize<T>(data, _options == null ? new JsonSerializerOptions()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
AllowTrailingCommas = true
|
|
} : _options);
|
|
|
|
loaded = true;
|
|
return cached;
|
|
}
|
|
} |