// ============================================================================ // RynthSuite - Plugins/Shared/SessionStatsTracker.cs // Feeds a SessionStats from the game: the player's properties every 0.5 s, // kill notices, chat, corpses, and the player's health and death counter // every tick, and keeps the session across plugin reloads // (TrackerSessionStore). RynthTracker and RynthAi's Mini Remote each own one, // so both show the same numbers (2026-10-11). // // Threading: every call except RequestReset and View runs on the pump // thread. RequestReset only sets a flag (any thread); View is an immutable // record swapped in after each poll (any thread). // // The bank's luminance where the client has no bank property (2026-11-02): // a server that keeps earned luminance in its bank out of the client's sight // has its server plugin read the bank and publish it in its profile // ("RynthServer.Profile" values, RynthServerProfile.ValueBankLuminance; // RynthDreamWeave's every-minute /b). Read every few seconds from the active // server's owner (ServerStateClient) and only while ACECustom's 9005 isn't // readable, it goes in as that bank's reading: the same baseline (the // session file's "bank"), so rises are earned luminance and drops aren't. // Needs, linked into the plugin: ServerStateClient.cs, EngineServerState.cs, // RynthServerProfileApi.cs. // ============================================================================ using System; using System.Collections.Generic; using RynthCore.PluginSdk; namespace RynthCore.Plugin.Shared; internal sealed class SessionStatsTracker { // The player's own Int64 properties (the engine serves them to the pump since 2026.9.29.3). private const uint Int64TotalExperience = 1; private const uint Int64AvailableLuminance = 6; private const uint Int64BankedLuminance = 9005; // ACECustom bank private const uint Int64RadianceEarned = 9101; // Aelrynth Bank mod session totals private const uint Int64LuminanceBanked = 9102; private const uint Int64LuminanceDrawn = 9103; // The server's death counter (PropertyInt). The engine serves the player's own record to any thread. private const uint IntNumDeaths = 43; private const long PollIntervalMs = 500; private static readonly TimeSpan SaveInterval = TimeSpan.FromSeconds(30); // Corpses waiting for their name (the client may not have it on the create tick). private const double CorpseNameWaitSec = 3.0; private const int MaxPendingCorpses = 256; private const int MaxCorpseLogLines = 40; private readonly RynthCoreHost _host; private readonly string _tag; private readonly string _saveDirectory; private readonly SessionStats _stats = new(); private readonly List<(uint Id, DateTime Seen)> _pendingCorpses = new(); private readonly ServerStateClient _server = new(); private long? _profileBankLum; // the active server profile's bank luminance (null = none) private long _nextProfileMs; private uint _playerId; private bool _loggedIn; // between OnLoginComplete and OnLogout private bool _active; // logged in and the session started (player id known) private volatile bool _pendingReset; private volatile SessionStatsView _view = SessionStatsView.Empty; private long _nextPollMs; private DateTime _nextSaveUtc; private bool _saveFailedLogged; private int _corpseLogCount; /// Log prefix, e.g. "[RynthTracker]". /// This plugin's own session folder (TrackerSessionStore.DirectoryFor). public SessionStatsTracker(RynthCoreHost host, string tag, string saveDirectory) { _host = host; _tag = tag; _saveDirectory = saveDirectory; } /// The latest numbers (any thread). Zeros before login and after logout. public SessionStatsView View => _view; /// True while a session is running (pump thread). public bool Active => _active; /// /// How often the active server profile's bank luminance is re-read (a server plugin reads its bank /// once a minute at most). Tests set it lower. /// internal long ProfileIntervalMs { get; init; } = 5000; /// Starts a fresh session on the next tick (any thread). public void RequestReset() => _pendingReset = true; // ── Lifecycle ──────────────────────────────────────────────────────────── /// /// The engine calls OnLoginComplete on a real login AND on a freshly (re)loaded plugin while /// already in game, so the saved file is what tells a reload from a new login. A player id /// that isn't readable yet starts the session on the first tick that reads it. /// public void OnLoginComplete() { _loggedIn = true; _active = false; _playerId = _host.GetPlayerId(); if (_playerId != 0) Start(); } /// A real logout ends the session: the next login starts fresh, never picks this up. public void OnLogout() { if (_active) DeleteSavedSession(); _loggedIn = false; _active = false; _pendingCorpses.Clear(); _server.Clear(); _profileBankLum = null; _nextProfileMs = 0; _view = SessionStatsView.Empty; _host.Log($"{_tag} Session ended."); } /// /// Plugin unload (hot reload, RynthSuite update, client shutting down). Saves only while a /// session runs, never deletes: a copy unloaded before its OnLoginComplete must not wipe the /// save its predecessor left. /// public void Shutdown() { if (_active) SaveSession(); } private void Start() { DateTime now = DateTime.UtcNow; if (TryRestoreSession()) { _host.Log($"{_tag} Session carried over from before the reload."); } else { _stats.Reset(now); CleanupSavedSessions(); _host.Log($"{_tag} Session started."); } _active = true; _nextSaveUtc = now + SaveInterval; _nextPollMs = 0; _view = _stats.View(now); } // ── Events (pump thread) ───────────────────────────────────────────────── /// The server's killer notification (not chat): you landed the killing blow. public void OnKillNotification(string? deathMessage) { if (_active) _stats.OnKillNotice(deathMessage, DateTime.UtcNow); } /// Damage lines (multi-target hits) and the luminance lines. public void OnChatWindowText(string? text) { if (_active) _stats.OnChat(text, DateTime.UtcNow); } /// Corpses are read on the tick (their name may not be readable yet on the create). public void OnCreateObject(uint objectId) { if (!_active || objectId == 0) return; if (_pendingCorpses.Count >= MaxPendingCorpses) _pendingCorpses.RemoveAt(0); _pendingCorpses.Add((objectId, DateTime.UtcNow)); } public void OnDeleteObject(uint objectId) { for (int i = _pendingCorpses.Count - 1; i >= 0; i--) if (_pendingCorpses[i].Id == objectId) _pendingCorpses.RemoveAt(i); } // Deaths are read every tick (CheckDeaths), not from OnUpdateHealth: the engine never sends the // player's own health through it (that is a target's health: 0x01C0 and creature vitals), so the // death count stayed 0 (2026-10-30). // ── Tick (pump thread) ─────────────────────────────────────────────────── public void Tick() { DateTime now = DateTime.UtcNow; if (_pendingReset) { _pendingReset = false; _stats.Reset(now); _pendingCorpses.Clear(); _view = _active ? _stats.View(now) : SessionStatsView.Empty; // Save the zeros now, so a reload right after a reset can't bring the old totals back. if (_active) { SaveSession(); _nextSaveUtc = now + SaveInterval; } _host.Log($"{_tag} Session reset."); } if (_loggedIn && !_active) { _playerId = _host.GetPlayerId(); if (_playerId == 0) return; Start(); } if (!_active) return; CheckDeaths(); CheckPendingCorpses(now); long nowMs = Environment.TickCount64; if (nowMs < _nextPollMs) return; _nextPollMs = nowMs + PollIntervalMs; Poll(now, nowMs); _view = _stats.View(now); // Saved right after a poll, so totals and baselines in the file always match. if (now >= _nextSaveUtc) { _nextSaveUtc = now + SaveInterval; SaveSession(); } } private void Poll(DateTime now, long nowMs) { if (_playerId == 0 || !_host.HasGetObjectQuadProperty) return; _stats.Sample(new SessionSample( Read(Int64TotalExperience), Read(Int64AvailableLuminance), Read(Int64BankedLuminance) ?? ProfileBankLum(nowMs), Read(Int64LuminanceBanked), Read(Int64LuminanceDrawn), Read(Int64RadianceEarned)), now); } private long? Read(uint stype) => _host.TryGetObjectQuadProperty(_playerId, stype, out long value) ? value : null; /// /// The active server profile's bank luminance (RynthServerProfile.ValueBankLuminance), re-read every /// few seconds and held in between; null while the profile has none (or the engine is older than v86). /// private long? ProfileBankLum(long nowMs) { if (nowMs < _nextProfileMs) return _profileBankLum; _nextProfileMs = nowMs + ProfileIntervalMs; try { _server.Poll(_host, nowMs, force: true); // throttled here, not by the client's own 2 s double v = ServerProfileView.Value(_server.ProfileJson, RynthServerProfile.ValueBankLuminance, double.NaN); _profileBankLum = v >= 0 && v < long.MaxValue ? (long)v : null; } catch { _profileBankLum = null; } return _profileBankLum; } /// /// The player's health from the engine's vitals (the same reading RynthAi's death hold uses), /// then the server's death counter, in the order the server sends them (SessionStats counts /// each death once). Every tick: the health reads 0 for a few seconds only. /// private void CheckDeaths() { int before = _stats.Deaths; if (_host.HasGetPlayerVitals && _host.TryGetPlayerVitals(out uint health, out uint maxHealth, out _, out _, out _, out _)) _stats.OnPlayerHealth(health, maxHealth); if (_host.HasGetObjectIntProperty && _host.TryGetObjectIntProperty(_playerId, IntNumDeaths, out int numDeaths)) _stats.OnDeathCount(numDeaths); if (_stats.Deaths != before) { _view = _stats.View(DateTime.UtcNow); // shown at once, not at the next poll _host.Log($"{_tag} Death counted ({_stats.Deaths} this session)."); } } /// Reads the names of newly created objects; a "Corpse of X" goes to the kill counter. private void CheckPendingCorpses(DateTime now) { if (_pendingCorpses.Count == 0) return; if (!_host.HasGetObjectName) { _pendingCorpses.Clear(); return; } for (int i = _pendingCorpses.Count - 1; i >= 0; i--) { var (id, seen) = _pendingCorpses[i]; if (!_host.TryGetObjectName(id, out string name) || string.IsNullOrEmpty(name)) { if ((now - seen).TotalSeconds > CorpseNameWaitSec) _pendingCorpses.RemoveAt(i); continue; } _pendingCorpses.RemoveAt(i); string? monster = CombatText.CorpseOwnerName(name); if (monster == null) continue; var result = _stats.OnCorpse(monster, now); if ((result == KillCounter.CorpseResult.Counted || result == KillCounter.CorpseResult.HitNotCounted) && _corpseLogCount < MaxCorpseLogLines) { _corpseLogCount++; _host.Log(result == KillCounter.CorpseResult.Counted ? $"{_tag} Kill counted from its corpse (no kill notice): '{name}' 0x{id:X8}, hit by a multi-target spell. Kills {_stats.Kills}." : $"{_tag} Not counted: '{name}' 0x{id:X8} - you hit it, single-target, and got no kill notice (someone else's kill?)."); } } } // ── Session persistence ────────────────────────────────────────────────── // // Reload-gap rule: the Rise baselines are restored as saved, so the first read after a // reload counts everything from the last save up to now, which includes what was earned // while the plugin was unloaded. Totals and baselines are saved together after the same // poll, so that span is counted exactly once. (Kills in the gap aren't seen, so the gap's // XP joins the totals and XP/hour but not XP per kill.) // The client's start time, so a new client that happens to get a closed one's process // id never picks up its save. 0 if Windows won't say (both sides then match on id alone). private static long? _processStartTicks; private static long ProcessStartTicks { get { if (_processStartTicks is long t) return t; try { using var p = System.Diagnostics.Process.GetCurrentProcess(); t = p.StartTime.ToUniversalTime().Ticks; } catch { t = 0; } _processStartTicks = t; return t; } } private void SaveSession() { if (_playerId == 0) return; try { DateTime now = DateTime.UtcNow; var s = new TrackerSessionState { ProcessId = Environment.ProcessId, ProcessStartUtcTicks = ProcessStartTicks, CharacterId = _playerId, SavedUtcTicks = now.Ticks, }; _stats.CaptureInto(s, now); TrackerSessionStore.Save(_saveDirectory, s); _saveFailedLogged = false; } catch (Exception ex) { // Every 30 s otherwise; one line until a save works again. if (!_saveFailedLogged) { _saveFailedLogged = true; _host.Log($"{_tag} Couldn't save the session: {ex.GetType().Name}: {ex.Message}"); } } } private bool TryRestoreSession() { if (_playerId == 0) return false; try { int pid = Environment.ProcessId; if (!TrackerSessionStore.TryLoad(_saveDirectory, pid, _playerId, out TrackerSessionState s)) return false; if (!TrackerSessionStore.ShouldRestore(s, pid, ProcessStartTicks, _playerId, DateTime.UtcNow)) return false; _stats.ApplyFrom(s); return true; } catch (Exception ex) { _host.Log($"{_tag} Couldn't read the saved session, starting fresh: {ex.GetType().Name}: {ex.Message}"); return false; } } private void DeleteSavedSession() { if (_playerId == 0) return; try { TrackerSessionStore.Delete(_saveDirectory, Environment.ProcessId, _playerId); } catch (Exception ex) { _host.Log($"{_tag} Couldn't delete the saved session: {ex.GetType().Name}: {ex.Message}"); } } /// A fresh session: this client's older saves (any character) and files left by clients closed long ago go. private void CleanupSavedSessions() { try { TrackerSessionStore.Cleanup(_saveDirectory, Environment.ProcessId, DateTime.UtcNow); } catch { } } }