66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using LibHac.Common.Keys;
|
|
using TinfoilVibeServer.Models;
|
|
|
|
namespace TinfoilVibeServer.Services;
|
|
|
|
/// <summary>
|
|
/// Reads the JSON config file on startup, watches it for changes, and also
|
|
/// loads the KeySet from the file specified in the config.
|
|
/// </summary>
|
|
public class ConfigManager
|
|
{
|
|
public AppSettings? Settings { get; private set; }
|
|
public event Action<AppSettings?>? OnChange;
|
|
|
|
private readonly string _configPath;
|
|
private readonly FileSystemWatcher _watcher;
|
|
private readonly Lock _sync = new();
|
|
|
|
public ConfigManager()
|
|
{
|
|
_configPath = Path.Combine(AppContext.BaseDirectory, "config", "appsettings.json");
|
|
Load();
|
|
|
|
_watcher = new FileSystemWatcher
|
|
{
|
|
Path = Path.GetDirectoryName(_configPath)!,
|
|
Filter = Path.GetFileName(_configPath),
|
|
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.Attributes
|
|
};
|
|
_watcher.Changed += (_, _) => Reload();
|
|
_watcher.EnableRaisingEvents = true;
|
|
}
|
|
|
|
private void Load()
|
|
{
|
|
if (!File.Exists(_configPath))
|
|
{
|
|
Settings = new AppSettings(
|
|
RootDirectories: [],
|
|
WhitelistExtensions: [],
|
|
RomExtensions: [],
|
|
CredentialsFile: "credentials.json",
|
|
FingerprintsFile: "fingerprints.json",
|
|
BlacklistFile: "blacklist.json",
|
|
MaxFailedAttempts: 5
|
|
);
|
|
return;
|
|
}
|
|
|
|
var txt = File.ReadAllText(_configPath);
|
|
Settings = JsonSerializer.Deserialize<AppSettings>(txt, new JsonSerializerOptions { PropertyNameCaseInsensitive = true })!;
|
|
}
|
|
|
|
private void Reload()
|
|
{
|
|
lock (_sync)
|
|
{
|
|
Load();
|
|
OnChange?.Invoke(Settings);
|
|
}
|
|
}
|
|
} |