using System; using System.IO; using System.Text.Json; using System.Threading.Tasks; namespace undead_universal_patch_il2cpp.Core; public class DediConfig { 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(data, _options == null ? new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, AllowTrailingCommas = true } : _options); loaded = true; return cached; } }