RynthSuite as of 2026-09-05 (internal a5ff5c2)
A snapshot of the working tree for aelrynth.com/git/: the code, build files and public docs. Internal review notes and agent instructions are left out. MIT licensed; see LICENSE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
0a4b611a90
166 changed files with 186537 additions and 0 deletions
79
Plugins/RynthCore.Plugin.RynthTracker/PluginExports.cs
Normal file
79
Plugins/RynthCore.Plugin.RynthTracker/PluginExports.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using RynthCore.PluginCore;
|
||||
using RynthCore.PluginSdk;
|
||||
|
||||
namespace RynthCore.Plugin.RynthTracker;
|
||||
|
||||
public static unsafe class PluginExports
|
||||
{
|
||||
private static readonly RynthPluginRuntime<RynthTrackerPlugin> Runtime = new();
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginInit", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
private static int Init(RynthCoreApiNative* api) => Runtime.Init(api);
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginShutdown", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void Shutdown() => Runtime.Shutdown();
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginName", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static IntPtr GetName() => RynthTrackerPlugin.NamePointer;
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginVersion", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static IntPtr GetVersion() => RynthTrackerPlugin.VersionPointer;
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginTick", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void Tick() => Runtime.OnTick();
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginRender", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void Render() => Runtime.OnRender();
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginOnLoginComplete", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void OnLoginComplete() => Runtime.OnLoginComplete();
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginOnLogout", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void OnLogout() => Runtime.OnLogout();
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginOnChatWindowText", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void OnChatWindowText(IntPtr textUtf16, int chatType, IntPtr eatFlag)
|
||||
=> Runtime.OnChatWindowText(textUtf16, chatType, eatFlag);
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginOnUpdateHealth", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void OnUpdateHealth(uint targetId, float healthRatio, uint currentHealth, uint maxHealth)
|
||||
=> Runtime.OnUpdateHealth(targetId, healthRatio, currentHealth, maxHealth);
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthPluginOnDeleteObject", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void OnDeleteObject(uint objectId) => Runtime.OnDeleteObject(objectId);
|
||||
|
||||
// ── Snapshot bridge ──────────────────────────────────────────────────────
|
||||
// Panel polls RynthTrackerGetSnapshotJson every 500ms.
|
||||
// Alloc-new → swap → free-old keeps the polling thread safe without a lock.
|
||||
|
||||
private static IntPtr _snapshotPtr = IntPtr.Zero;
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthTrackerGetSnapshotJson", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static IntPtr GetSnapshotJson()
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = Runtime.Plugin?.GetSnapshot()
|
||||
?? "{\"ss\":0.0,\"xt\":0,\"xh\":0,\"lt\":0,\"lh\":0,\"kt\":0,\"kh\":0.0,\"xk\":0,\"dt\":0}";
|
||||
IntPtr newPtr = Marshal.StringToHGlobalAnsi(json);
|
||||
IntPtr oldPtr = Interlocked.Exchange(ref _snapshotPtr, newPtr);
|
||||
if (oldPtr != IntPtr.Zero) Marshal.FreeHGlobal(oldPtr);
|
||||
return newPtr;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthTrackerReset", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void Reset()
|
||||
{
|
||||
try { Runtime.Plugin?.Reset(); }
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0-windows</TargetFramework>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
|
||||
<OutputType>Library</OutputType>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishAot>true</PublishAot>
|
||||
<NativeLib>Shared</NativeLib>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<IlcOptimizationPreference>Size</IlcOptimizationPreference>
|
||||
<StripSymbols>false</StripSymbols>
|
||||
<RootNamespace>RynthCore.Plugin.RynthTracker</RootNamespace>
|
||||
<AssemblyName>RynthCore.Plugin.RynthTracker</AssemblyName>
|
||||
<Platforms>x86</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\RynthCore\src\RynthCore.PluginCore\RynthCore.PluginCore.csproj" />
|
||||
<ProjectReference Include="..\..\..\RynthCore\src\RynthCore.PluginSdk\RynthCore.PluginSdk.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
185
Plugins/RynthCore.Plugin.RynthTracker/RynthTrackerPlugin.cs
Normal file
185
Plugins/RynthCore.Plugin.RynthTracker/RynthTrackerPlugin.cs
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using RynthCore.PluginCore;
|
||||
|
||||
namespace RynthCore.Plugin.RynthTracker;
|
||||
|
||||
public sealed class RynthTrackerPlugin : RynthPluginBase
|
||||
{
|
||||
internal static readonly IntPtr NamePointer = Marshal.StringToHGlobalAnsi("RynthTracker");
|
||||
internal static readonly IntPtr VersionPointer = Marshal.StringToHGlobalAnsi("0.1.0");
|
||||
|
||||
private uint _playerId;
|
||||
private bool _loginComplete;
|
||||
private DateTime _sessionStart;
|
||||
|
||||
private long _xpTotal;
|
||||
private long _lumTotal;
|
||||
private int _killsTotal;
|
||||
private int _deathsTotal;
|
||||
|
||||
// Deduplication: track IDs already counted as dead this session to prevent
|
||||
// double-counting if the server sends multiple health=0 packets for the same object.
|
||||
private readonly HashSet<uint> _killedIds = new();
|
||||
|
||||
private int _tickCounter;
|
||||
|
||||
// Deep-audit finding #21 (2026-06-18): the RynthTrackerReset export used to
|
||||
// call ResetSession() directly, which Clear()s _killedIds from the
|
||||
// Avalonia UI thread (no Dispatcher hop) while the AC pump thread
|
||||
// concurrently Add()s/Remove()s the same HashSet in
|
||||
// OnUpdateHealth/OnDeleteObject — HashSet<uint> isn't concurrency-safe;
|
||||
// a racing Clear() can throw or corrupt its internal arrays. Mirrors the
|
||||
// engine's DispatchQueued* idiom: the export just sets a flag, OnTick
|
||||
// (pump thread, same thread as the HashSet's other mutators) performs
|
||||
// the actual reset.
|
||||
private volatile bool _pendingReset;
|
||||
|
||||
// Written on the game thread every ~0.5s, read on the Avalonia UI thread.
|
||||
// Reference reads are atomic on x86 so volatile is sufficient; no lock needed.
|
||||
private volatile string _snapshot = EmptySnapshot;
|
||||
|
||||
private const string EmptySnapshot =
|
||||
"{\"ss\":0.0,\"xt\":0,\"xh\":0,\"lt\":0,\"lh\":0,\"kt\":0,\"kh\":0.0,\"xk\":0,\"dt\":0}";
|
||||
|
||||
public override int Initialize()
|
||||
{
|
||||
Host.Log("[RynthTracker] Plugin initialized.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override void OnLoginComplete()
|
||||
{
|
||||
_playerId = Host.GetPlayerId();
|
||||
ResetSession();
|
||||
_loginComplete = true;
|
||||
Host.Log("[RynthTracker] Session started.");
|
||||
}
|
||||
|
||||
public override void OnLogout()
|
||||
{
|
||||
_loginComplete = false;
|
||||
Host.Log("[RynthTracker] Session ended.");
|
||||
}
|
||||
|
||||
public override void OnChatWindowText(string? text, int chatType, ref int eat)
|
||||
{
|
||||
if (!_loginComplete || string.IsNullOrEmpty(text)) return;
|
||||
|
||||
// XP: "You've earned N experience."
|
||||
// Lum: "You've earned N Luminance."
|
||||
int earnedIdx = text.IndexOf("earned ", StringComparison.OrdinalIgnoreCase);
|
||||
if (earnedIdx < 0) return;
|
||||
|
||||
int numStart = earnedIdx + 7; // "earned ".Length
|
||||
long amount = ParseLeadingNumber(text, numStart);
|
||||
if (amount <= 0) return;
|
||||
|
||||
int afterNum = SkipPastNumber(text, numStart);
|
||||
if (text.IndexOf("experience", afterNum, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
_xpTotal += amount;
|
||||
else if (text.IndexOf("luminance", afterNum, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
_lumTotal += amount;
|
||||
}
|
||||
|
||||
public override void OnUpdateHealth(uint targetId, float healthRatio, uint currentHealth, uint maxHealth)
|
||||
{
|
||||
if (!_loginComplete) return;
|
||||
|
||||
if (targetId == _playerId)
|
||||
{
|
||||
// maxHealth > 0 guards against spurious 0/0 packets during login.
|
||||
if (currentHealth == 0 && maxHealth > 0)
|
||||
_deathsTotal++;
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentHealth == 0 && maxHealth > 0 && _killedIds.Add(targetId))
|
||||
_killsTotal++;
|
||||
}
|
||||
|
||||
public override void OnDeleteObject(uint objectId)
|
||||
{
|
||||
_killedIds.Remove(objectId);
|
||||
}
|
||||
|
||||
public override void OnTick()
|
||||
{
|
||||
if (_pendingReset)
|
||||
{
|
||||
_pendingReset = false;
|
||||
ResetSession();
|
||||
Host.Log("[RynthTracker] Session reset.");
|
||||
}
|
||||
|
||||
if (++_tickCounter < 30) return; // rebuild every ~0.5s at 60Hz
|
||||
_tickCounter = 0;
|
||||
_snapshot = BuildSnapshot();
|
||||
}
|
||||
|
||||
internal string GetSnapshot() => _snapshot;
|
||||
|
||||
/// <summary>Called from the RynthTrackerReset export (Avalonia UI thread) —
|
||||
/// just flags a reset; OnTick performs it on the pump thread.</summary>
|
||||
internal void Reset() => _pendingReset = true;
|
||||
|
||||
private void ResetSession()
|
||||
{
|
||||
_sessionStart = DateTime.UtcNow;
|
||||
_xpTotal = 0;
|
||||
_lumTotal = 0;
|
||||
_killsTotal = 0;
|
||||
_deathsTotal = 0;
|
||||
_killedIds.Clear();
|
||||
_snapshot = EmptySnapshot;
|
||||
}
|
||||
|
||||
private string BuildSnapshot()
|
||||
{
|
||||
double ss = _loginComplete ? (DateTime.UtcNow - _sessionStart).TotalSeconds : 0.0;
|
||||
double hrs = ss / 3600.0;
|
||||
long xpHr = hrs > 0.0 ? (long)(_xpTotal / hrs) : 0;
|
||||
long lumHr = hrs > 0.0 ? (long)(_lumTotal / hrs) : 0;
|
||||
double killHr = hrs > 0.0 ? _killsTotal / hrs : 0.0;
|
||||
long xpKill = _killsTotal > 0 ? _xpTotal / _killsTotal : 0;
|
||||
|
||||
var ic = System.Globalization.CultureInfo.InvariantCulture;
|
||||
return "{\"ss\":" + ss.ToString("F1", ic)
|
||||
+ ",\"xt\":" + _xpTotal
|
||||
+ ",\"xh\":" + xpHr
|
||||
+ ",\"lt\":" + _lumTotal
|
||||
+ ",\"lh\":" + lumHr
|
||||
+ ",\"kt\":" + _killsTotal
|
||||
+ ",\"kh\":" + killHr.ToString("F1", ic)
|
||||
+ ",\"xk\":" + xpKill
|
||||
+ ",\"dt\":" + _deathsTotal
|
||||
+ "}";
|
||||
}
|
||||
|
||||
// Parse the leading run of digit+comma characters starting at 'start'.
|
||||
private static long ParseLeadingNumber(string text, int start)
|
||||
{
|
||||
long result = 0;
|
||||
bool hasDigit = false;
|
||||
for (int i = start; i < text.Length; i++)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c >= '0' && c <= '9') { result = result * 10 + (c - '0'); hasDigit = true; }
|
||||
else if (c == ',' && hasDigit) { } // skip thousands separator
|
||||
else if (hasDigit) break; // first non-number char ends the parse
|
||||
}
|
||||
return hasDigit ? result : 0;
|
||||
}
|
||||
|
||||
// Return the index of the first character after the leading number at 'start'.
|
||||
private static int SkipPastNumber(string text, int start)
|
||||
{
|
||||
for (int i = start; i < text.Length; i++)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c != ',' && (c < '0' || c > '9')) return i;
|
||||
}
|
||||
return text.Length;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue