RynthServers/Plugins/RynthCore.Plugin.RynthILT/Hub/IltHubStore.cs

494 lines
23 KiB
C#

// IltHubStore.cs — Disk persistence for the ILT Hub.
// Originally SilentKelpie's ILT Hub store in RynthAi; moved to RynthILT 2026-10-13.
//
// Files (RynthILT's folder, %APPDATA%\RynthCore\RynthILT; <char> = <world>\<character>):
// <char>\hub.json live Hub state (IltHubState)
// <char>\bank-transactions.json bank transaction log (capped)
// <char>\profiles\<n>.json named per-character Hub profiles
// Profiles\<n>.json named profiles shared by every character
// They were ilt-hub.json, ilt-hub-transactions.json, ilt-hub-profiles\ (RynthAi's per-character
// settings folder) and RynthAi\IltHubProfiles\; IltMigration copies them over on the first login.
// Saves are dirty-checked (the serialized JSON is compared to what was last written) so
// the periodic save in Tick costs nothing when nothing changed. A one-time import pulls
// UtilityBelt's bankdata sidecars so players keep their balances and history.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using RynthCore.Plugin.Shared;
using RynthCore.PluginSdk;
namespace RynthCore.Plugin.RynthILT.Hub;
internal sealed class IltHubStore
{
/// <summary>Maximum bank transactions kept on disk (oldest dropped first).</summary>
public const int MaxTransactions = 1000;
/// <summary>RynthILT's folder: shared profiles, aug costs and equipment suits (overridable for the tests).</summary>
public static string SharedRoot = RynthILTPlugin.SettingsRoot;
public const string StateFile = "hub.json";
public const string TransactionsFile = "bank-transactions.json";
public const string ProfilesFolder = "profiles";
private readonly RynthCoreHost _host;
private readonly string _charFolder;
private string _lastStateJson = string.Empty;
private string _lastTxJson = string.Empty;
public IltHubStore(RynthCoreHost host, string charFolder)
{
_host = host;
_charFolder = charFolder ?? string.Empty;
}
/// <summary>
/// True while this character's copy from RynthAi is unfinished (IltMigration): the state, the
/// bank log and per-character profiles are not written, so no fresh file shadows a RynthAi
/// original that hasn't been copied yet. Shared profiles still save.
/// </summary>
public Func<bool> WritesBlocked = static () => false;
/// <summary>
/// The character's RynthAi folder while its copy is unfinished: a file RynthILT doesn't have yet
/// is read from there (read only) when it can be; otherwise the Hub runs from the defaults.
/// </summary>
public string? LegacyFolder;
private bool Blocked()
{
try { return WritesBlocked(); }
catch { return true; }
}
/// <summary>A RynthAi original read without locking it (it may be held open elsewhere). Null when it can't be.</summary>
private string? ReadLegacy(string fileName)
{
if (string.IsNullOrEmpty(LegacyFolder)) return null;
string path = Path.Combine(LegacyFolder, fileName);
try
{
if (!File.Exists(path)) return null;
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var sr = new StreamReader(fs);
return sr.ReadToEnd();
}
catch (Exception ex)
{
RynthLog.Write(LogCat.IltStore, $"RynthAi's {path} can't be read yet ({ex.Message}); running from the defaults until it is copied.");
return null;
}
}
public string CharFolder => _charFolder;
private string StatePath => Path.Combine(_charFolder, StateFile);
private string TxPath => Path.Combine(_charFolder, TransactionsFile);
private string CharProfileDir => Path.Combine(_charFolder, ProfilesFolder);
internal static string SharedProfileDir => Path.Combine(SharedRoot, "Profiles");
// ── State ───────────────────────────────────────────────────────────────
/// <summary>Loads the live state, returning defaults when the file is missing or unreadable.</summary>
public IltHubState LoadState()
{
try
{
if (_charFolder.Length > 0 && File.Exists(StatePath))
{
string json = File.ReadAllText(StatePath);
var s = JsonSerializer.Deserialize(json, IltHubJsonContext.Default.IltHubState);
if (s != null)
{
Normalize(s);
_lastStateJson = json;
return s;
}
}
else if (ReadLegacy("ilt-hub.json") is string legacyJson)
{
var s = JsonSerializer.Deserialize(legacyJson, IltHubJsonContext.Default.IltHubState);
if (s != null) { Normalize(s); return s; }
}
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"load {StatePath}"); }
return new IltHubState();
}
/// <summary>Writes the state only when its JSON differs from the last write.</summary>
public void SaveStateIfDirty(IltHubState state)
{
if (_charFolder.Length == 0) return;
try
{
string json = JsonSerializer.Serialize(state, IltHubJsonContext.Default.IltHubState);
if (json == _lastStateJson) return;
if (Blocked()) return; // the copy from RynthAi isn't finished: never shadow its hub.json
// A failure is said in chat once per folder (on ILT) and logged; the next periodic save retries.
if (DataWriteGuard.TrySave("the ILT Hub state", StatePath, () =>
{
Directory.CreateDirectory(_charFolder);
WriteAtomic(StatePath, json);
}))
_lastStateJson = json;
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"save {StatePath}"); }
}
/// <summary>Repairs null collections / out-of-range values after deserialization.</summary>
private static void Normalize(IltHubState s)
{
s.ServerOptions ??= new IltServerOptionsSnapshot();
s.ServerOptions.Bits = new Dictionary<string, int>(s.ServerOptions.Bits ?? new(), StringComparer.OrdinalIgnoreCase);
s.Bank ??= new IltBankState();
s.Pet ??= new IltPetState();
s.Pet.PetLog ??= new();
s.Pet.ShinyLog ??= new();
s.Pet.Breeding ??= new();
s.Pet.Breeding.Rules ??= new();
s.Pet.Breeding.StageNames ??= new();
s.Pet.Breeding.Log ??= new();
s.Pet.Breeding.LastDebug ??= new();
s.Pet.CaptureLens = Math.Clamp(s.Pet.CaptureLens, 0, PetCaptureRules.Lenses.Length - 1);
s.Pet.Breeding.Rules.RemoveAll(r => r == null || string.IsNullOrEmpty(r.Key));
s.Pet.Breeding.Log.RemoveAll(e => e == null);
for (int i = 0; i < s.Pet.Breeding.StageNames.Count; i++) s.Pet.Breeding.StageNames[i] ??= string.Empty;
if (s.Pet.Breeding.Log.Count > IltBreedingRules.MaxEvents)
s.Pet.Breeding.Log.RemoveRange(0, s.Pet.Breeding.Log.Count - IltBreedingRules.MaxEvents);
s.Character ??= new IltCharacterState();
s.Character.ItemConversions ??= new();
s.Character.AugTargets = new Dictionary<string, int>(s.Character.AugTargets ?? new(), StringComparer.OrdinalIgnoreCase);
s.Character.AttrOrder = IltAttributeRaiser.RepairOrder(s.Character.AttrOrder);
s.Character.AttrRaiseStats = IltAttributeRaiser.RepairSelection(s.Character.AttrRaiseStats);
s.Character.AttrAutoRaiseMinutes = Math.Clamp(s.Character.AttrAutoRaiseMinutes, 1, 1440);
s.Character.AttrRaiseMode = Math.Clamp(s.Character.AttrRaiseMode, 0, 2);
s.Character.AttrKeepXp = Math.Max(0, s.Character.AttrKeepXp);
s.Character.CharmsSeen = new Dictionary<string, IltCharmSeen>(s.Character.CharmsSeen ?? new(), StringComparer.OrdinalIgnoreCase);
s.Gear ??= new IltGearState();
s.Gear.DispelInclusions ??= new();
s.Gear.DispelExclusions ??= new();
s.Gear.RingRuns = new Dictionary<string, int>(s.Gear.RingRuns ?? new(), StringComparer.OrdinalIgnoreCase);
s.Games ??= new IltGamesState();
s.Combat ??= new IltCombatState();
s.T11 ??= new IltT11State();
s.Remote ??= new IltRemoteState();
if (s.T11.ItemAugsOverride < -1) s.T11.ItemAugsOverride = -1;
s.Guardian ??= new IltGuardianState();
s.Guardian.TurnInCounts = new Dictionary<string, int>(s.Guardian.TurnInCounts ?? new(), StringComparer.OrdinalIgnoreCase);
s.Guardian.CooldownUntilUtc = new Dictionary<string, DateTime>(s.Guardian.CooldownUntilUtc ?? new(), StringComparer.OrdinalIgnoreCase);
s.Guardian.CooldownHoursAssumed = Math.Clamp(s.Guardian.CooldownHoursAssumed, 1, 72);
s.SelectedTab = Math.Clamp(s.SelectedTab, 0, 4);
s.Gear.SplitArrowTarget = Math.Clamp(s.Gear.SplitArrowTarget, 0, 10);
}
// ── Transactions ────────────────────────────────────────────────────────
public List<IltBankTransaction> LoadTransactions()
{
try
{
if (_charFolder.Length > 0 && File.Exists(TxPath))
{
string json = File.ReadAllText(TxPath);
var list = JsonSerializer.Deserialize(json, IltHubJsonContext.Default.IltBankTransactionList);
if (list != null) { _lastTxJson = json; return list; }
}
else if (ReadLegacy("ilt-hub-transactions.json") is string legacyJson)
{
var list = JsonSerializer.Deserialize(legacyJson, IltHubJsonContext.Default.IltBankTransactionList);
if (list != null) return list;
}
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"load {TxPath}"); }
return new List<IltBankTransaction>();
}
public void SaveTransactionsIfDirty(List<IltBankTransaction> list)
{
if (_charFolder.Length == 0) return;
try
{
if (list.Count > MaxTransactions)
list.RemoveRange(0, list.Count - MaxTransactions);
string json = JsonSerializer.Serialize(list, IltHubJsonContext.Default.IltBankTransactionList);
if (json == _lastTxJson) return;
if (Blocked()) return; // as SaveStateIfDirty
if (DataWriteGuard.TrySave("the bank transaction log", TxPath, () =>
{
Directory.CreateDirectory(_charFolder);
WriteAtomic(TxPath, json);
}))
_lastTxJson = json;
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"save {TxPath}"); }
}
// ── Profiles ────────────────────────────────────────────────────────────
/// <summary>Lists profile names. Shared ones are prefixed "shared:".</summary>
public List<string> ListProfiles()
{
var names = new List<string>();
try
{
if (Directory.Exists(CharProfileDir))
names.AddRange(Directory.GetFiles(CharProfileDir, "*.json").Select(Path.GetFileNameWithoutExtension).OfType<string>());
if (Directory.Exists(SharedProfileDir))
names.AddRange(Directory.GetFiles(SharedProfileDir, "*.json").Select(p => "shared:" + Path.GetFileNameWithoutExtension(p)));
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"list profiles"); }
names.Sort(StringComparer.OrdinalIgnoreCase);
return names;
}
/// <summary>Saves the preference parts of <paramref name="state"/> as a named profile.</summary>
public bool SaveProfile(IltHubState state, string name, bool shared, out string error)
{
error = string.Empty;
string safe = SanitizeName(name);
if (safe.Length == 0) { error = "Profile name is empty."; return false; }
if (!shared && Blocked())
{
error = "this character's Hub data is still being copied from RynthAi (see the log); save it as a shared profile, or try again later.";
return false;
}
try
{
string dir = shared ? SharedProfileDir : CharProfileDir;
var copy = Clone(state);
StripRuntime(copy);
string json = JsonSerializer.Serialize(copy, IltHubJsonContext.Default.IltHubState);
string path = Path.Combine(dir, safe + ".json");
// The player asked for this save: the caller shows the reason (and the fix) itself.
return DataWriteGuard.TrySave("the Hub profile", path, () =>
{
Directory.CreateDirectory(dir);
WriteAtomic(path, json);
}, out error, callerShows: true);
}
catch (Exception ex) { error = ex.Message; return false; }
}
/// <summary>
/// Loads a profile onto <paramref name="target"/>, keeping runtime data (balances,
/// server bits, rosters, import flag) from the current state.
/// </summary>
public bool LoadProfile(IltHubState target, string name, out string error)
{
error = string.Empty;
bool shared = name.StartsWith("shared:", StringComparison.OrdinalIgnoreCase);
string safe = SanitizeName(shared ? name.Substring(7) : name);
string path = Path.Combine(shared ? SharedProfileDir : CharProfileDir, safe + ".json");
try
{
if (!File.Exists(path)) { error = $"Profile not found: {name}"; return false; }
var p = JsonSerializer.Deserialize(File.ReadAllText(path), IltHubJsonContext.Default.IltHubState);
if (p == null) { error = "Profile file is empty."; return false; }
Normalize(p);
target.Pet = KeepRosters(p.Pet, target.Pet);
target.Character = p.Character;
target.Gear = p.Gear;
target.Games = p.Games;
target.Bank.AutoRefreshSeconds = p.Bank.AutoRefreshSeconds;
target.ForceLeaftideFeatures = p.ForceLeaftideFeatures;
target.GamesHudVisible = p.GamesHudVisible;
return true;
}
catch (Exception ex) { error = ex.Message; return false; }
}
private static IltPetState KeepRosters(IltPetState incoming, IltPetState current)
{
incoming.PetLog = current.PetLog;
incoming.ShinyLog = current.ShinyLog;
// The breeding tracker holds this object; it is the character's own (not a profile setting).
incoming.Breeding = current.Breeding;
return incoming;
}
private static IltHubState Clone(IltHubState s)
=> JsonSerializer.Deserialize(JsonSerializer.Serialize(s, IltHubJsonContext.Default.IltHubState),
IltHubJsonContext.Default.IltHubState) ?? new IltHubState();
/// <summary>Removes character-specific runtime data before a profile is written.</summary>
private static void StripRuntime(IltHubState s)
{
s.ServerOptions = new IltServerOptionsSnapshot();
var keepRefresh = s.Bank.AutoRefreshSeconds;
s.Bank = new IltBankState { AutoRefreshSeconds = keepRefresh };
s.Pet.PetLog = new();
s.Pet.ShinyLog = new();
s.Pet.Breeding = new();
s.UbSidecarsImported = false;
s.WindowVisible = false;
}
private static string SanitizeName(string name)
{
var invalid = Path.GetInvalidFileNameChars();
return new string((name ?? string.Empty).Trim().Where(c => !invalid.Contains(c)).ToArray());
}
// ── UtilityBelt sidecar import ──────────────────────────────────────────
/// <summary>
/// One-time import of UB's bankdata (balances, transactions) and item conversions.
/// Searches Documents\Decal Plugins\UtilityBelt\&lt;world&gt;\&lt;char&gt;; when the world is
/// unknown every world folder containing the character is tried. Returns a summary.
/// </summary>
public string ImportUbSidecars(IltHubState state, List<IltBankTransaction> txLog, string world, string charName)
{
if (string.IsNullOrWhiteSpace(charName)) return "character name unknown";
string ubRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Decal Plugins", "UtilityBelt");
if (!Directory.Exists(ubRoot)) return "no UtilityBelt folder";
var charDirs = new List<string>();
if (!string.IsNullOrWhiteSpace(world))
charDirs.Add(Path.Combine(ubRoot, world, charName));
try
{
foreach (string w in Directory.GetDirectories(ubRoot))
{
string d = Path.Combine(w, charName);
if (!charDirs.Contains(d, StringComparer.OrdinalIgnoreCase)) charDirs.Add(d);
}
}
catch { /* unreadable Documents subfolder: fall through with what we have */ }
foreach (string dir in charDirs)
{
string bankDir = Path.Combine(dir, "bankdata");
if (!Directory.Exists(bankDir) && !Directory.Exists(dir)) continue;
int parts = 0;
parts += ImportBalances(Path.Combine(bankDir, "bankbalances.json"), state.Bank) ? 1 : 0;
int tx = ImportTransactions(Path.Combine(bankDir, "transactions.json"), txLog);
int conv = ImportConversions(new[]
{
Path.Combine(bankDir, "itemconversions.json"),
Path.Combine(dir, "itemconversions.json"),
}, state.Character.ItemConversions);
if (parts + tx + conv > 0)
return $"imported from {dir}: balances {(parts > 0 ? "yes" : "no")}, {tx} transactions, {conv} conversions";
}
return "no UtilityBelt bank data found for " + charName;
}
private bool ImportBalances(string path, IltBankState bank)
{
if (!File.Exists(path)) return false;
try
{
using var doc = JsonDocument.Parse(File.ReadAllText(path));
var root = doc.RootElement;
if (root.ValueKind != JsonValueKind.Object) return false;
bank.Pyreals = ReadLong(root, "Pyreals");
bank.Luminance = ReadLong(root, "Luminance");
bank.LegendaryKeys = ReadLong(root, "LegendaryKeys");
bank.MythicalKeys = ReadLong(root, "MythicalKeys");
bank.EnlightenedCoins = ReadLong(root, "EnlightenedCoins");
bank.WeaklyEnlightenedCoins = ReadLong(root, "WeaklyEnlightenedCoins");
if (TryGetProp(root, "LastUpdated", out var lu) && lu.ValueKind == JsonValueKind.String && lu.TryGetDateTime(out var dt))
bank.LastUpdated = dt;
return true;
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"UB balances import"); return false; }
}
private int ImportTransactions(string path, List<IltBankTransaction> txLog)
{
if (!File.Exists(path)) return 0;
try
{
using var doc = JsonDocument.Parse(File.ReadAllText(path));
if (doc.RootElement.ValueKind != JsonValueKind.Array) return 0;
int added = 0;
foreach (var e in doc.RootElement.EnumerateArray())
{
if (e.ValueKind != JsonValueKind.Object) continue;
var t = new IltBankTransaction
{
Type = ReadString(e, "Type"),
Currency = ReadString(e, "Currency"),
Amount = ReadLong(e, "Amount"),
OtherPlayer = ReadString(e, "OtherPlayer"),
Description = ReadString(e, "Description"),
};
if (TryGetProp(e, "Timestamp", out var ts) && ts.ValueKind == JsonValueKind.String && ts.TryGetDateTime(out var dt))
t.Timestamp = dt;
// Skip exact duplicates so re-running the import can't double the log.
if (txLog.Any(x => x.Timestamp == t.Timestamp && x.Amount == t.Amount && x.OtherPlayer == t.OtherPlayer)) continue;
txLog.Add(t);
added++;
}
txLog.Sort((a, b) => a.Timestamp.CompareTo(b.Timestamp));
return added;
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"UB transactions import"); return 0; }
}
private int ImportConversions(string[] paths, List<IltItemConversion> table)
{
foreach (string path in paths)
{
if (!File.Exists(path)) continue;
try
{
using var doc = JsonDocument.Parse(File.ReadAllText(path));
if (doc.RootElement.ValueKind != JsonValueKind.Array) continue;
int added = 0;
foreach (var e in doc.RootElement.EnumerateArray())
{
string name = ReadString(e, "ItemName");
if (name.Length == 0) continue;
var existing = table.FirstOrDefault(c => c.ItemName.Equals(name, StringComparison.OrdinalIgnoreCase));
if (existing == null) { existing = new IltItemConversion { ItemName = name }; table.Add(existing); added++; }
existing.ItemsPerConversion = Math.Max(1, (int)ReadLong(e, "ItemsPerConversion"));
existing.CoinsPerConversion = (int)ReadLong(e, "CoinsPerConversion");
}
return added;
}
catch (Exception ex) { RynthLog.Exception(LogCat.IltStore, ex, $"UB conversions import"); }
}
return 0;
}
// ── JSON helpers (case-insensitive property access) ─────────────────────
private static bool TryGetProp(JsonElement obj, string name, out JsonElement value)
{
foreach (var p in obj.EnumerateObject())
if (p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) { value = p.Value; return true; }
value = default;
return false;
}
private static long ReadLong(JsonElement obj, string name)
{
if (!TryGetProp(obj, name, out var v)) return 0;
if (v.ValueKind == JsonValueKind.Number && v.TryGetInt64(out long l)) return l;
if (v.ValueKind == JsonValueKind.String) return IltParse.ParseLeadingLong(v.GetString() ?? string.Empty);
return 0;
}
private static string ReadString(JsonElement obj, string name)
{
if (!TryGetProp(obj, name, out var v)) return string.Empty;
return v.ValueKind switch
{
JsonValueKind.String => v.GetString() ?? string.Empty,
JsonValueKind.Number => v.GetRawText(),
_ => string.Empty,
};
}
/// <summary>Write-then-rename so a crash mid-write never leaves a truncated file.</summary>
private static void WriteAtomic(string path, string contents)
{
string tmp = path + ".tmp";
File.WriteAllText(tmp, contents);
File.Move(tmp, path, overwrite: true);
}
}