backend refactor yay!!! 1.4.0
This commit is contained in:
49
Core/DediConfig.cs
Normal file
49
Core/DediConfig.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user