mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 23:49:02 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace RadicadeServer.Auth;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static class SteamWhitelist
|
||||
{
|
||||
public sealed class WhitelistFile
|
||||
{
|
||||
public bool Enabled { get; set; }
|
||||
public List<string> SteamIds { get; set; } = new();
|
||||
}
|
||||
|
||||
private static readonly JsonSerializerOptions CaseInsensitive = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
private static readonly object Lock = new();
|
||||
private static WhitelistFile? _data;
|
||||
private static string _filePath = "";
|
||||
private static bool _loaded;
|
||||
private static DateTime _lastWriteUtc = DateTime.MinValue;
|
||||
|
||||
|
||||
public static string FilePath => _filePath;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static bool CreatedEmptyConfig { get; private set; }
|
||||
|
||||
|
||||
public static void Load()
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
if (_loaded) return;
|
||||
|
||||
_filePath = Path.GetFullPath(Path.Join(DBConfig.PrivateData, "steamwhitelist.json"));
|
||||
_loaded = true;
|
||||
CreatedEmptyConfig = !File.Exists(_filePath);
|
||||
|
||||
try
|
||||
{
|
||||
if (CreatedEmptyConfig)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
_data = new WhitelistFile { Enabled = false, SteamIds = new List<string>() };
|
||||
}
|
||||
else
|
||||
{
|
||||
var raw = File.ReadAllText(_filePath);
|
||||
_data = JsonSerializer.Deserialize<WhitelistFile>(raw, CaseInsensitive) ?? new WhitelistFile();
|
||||
_data.SteamIds ??= new List<string>();
|
||||
_lastWriteUtc = File.GetLastWriteTimeUtc(_filePath);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_data = new WhitelistFile { Enabled = false, SteamIds = new List<string>() };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static bool Enabled
|
||||
{
|
||||
get { EnsureLoaded(); lock (Lock) return _data!.Enabled; }
|
||||
set { EnsureLoaded(); lock (Lock) { _data!.Enabled = value; SaveLocked(); } }
|
||||
}
|
||||
|
||||
|
||||
public static IReadOnlyCollection<string> SteamIds
|
||||
{
|
||||
get { EnsureLoaded(); lock (Lock) return _data!.SteamIds.ToList(); }
|
||||
}
|
||||
|
||||
|
||||
public static void SetSteamIds(IEnumerable<string> steamIds)
|
||||
{
|
||||
EnsureLoaded();
|
||||
lock (Lock)
|
||||
{
|
||||
_data!.SteamIds = steamIds
|
||||
.Where(id => !string.IsNullOrWhiteSpace(id))
|
||||
.Select(id => id.Trim())
|
||||
.Distinct()
|
||||
.ToList();
|
||||
SaveLocked();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static bool Contains(string steamId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(steamId)) return false;
|
||||
EnsureLoaded();
|
||||
lock (Lock) return _data!.SteamIds.Contains(steamId.Trim());
|
||||
}
|
||||
|
||||
|
||||
public static bool Add(string steamId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(steamId)) return false;
|
||||
steamId = steamId.Trim();
|
||||
|
||||
EnsureLoaded();
|
||||
lock (Lock)
|
||||
{
|
||||
if (_data!.SteamIds.Contains(steamId)) return false;
|
||||
_data.SteamIds.Add(steamId);
|
||||
SaveLocked();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static bool Remove(string steamId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(steamId)) return false;
|
||||
steamId = steamId.Trim();
|
||||
|
||||
EnsureLoaded();
|
||||
lock (Lock)
|
||||
{
|
||||
var removed = _data!.SteamIds.Remove(steamId);
|
||||
if (removed) SaveLocked();
|
||||
return removed;
|
||||
}
|
||||
}
|
||||
|
||||
private static void EnsureLoaded()
|
||||
{
|
||||
if (!_loaded) { Load(); return; }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
lock (Lock)
|
||||
{
|
||||
DateTime lastWriteUtc;
|
||||
try { lastWriteUtc = File.GetLastWriteTimeUtc(_filePath); }
|
||||
catch { return; }
|
||||
|
||||
if (lastWriteUtc > _lastWriteUtc)
|
||||
{
|
||||
try
|
||||
{
|
||||
var raw = File.ReadAllText(_filePath);
|
||||
var reloaded = JsonSerializer.Deserialize<WhitelistFile>(raw, CaseInsensitive);
|
||||
if (reloaded != null)
|
||||
{
|
||||
reloaded.SteamIds ??= new List<string>();
|
||||
_data = reloaded;
|
||||
_lastWriteUtc = lastWriteUtc;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void SaveLocked()
|
||||
{
|
||||
try
|
||||
{
|
||||
var dir = Path.GetDirectoryName(_filePath);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
File.WriteAllText(_filePath, JsonSerializer.Serialize(_data, options));
|
||||
_lastWriteUtc = File.GetLastWriteTimeUtc(_filePath);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user