feat: port aelrynth 2026.9.27.2 snapshot onto GitHub history

Overlay upstream/main (9b5e276) onto BlankenshipG main without rewriting
history. Keep origin-only Decal/plugin/docs trees. Re-apply aelrynth.com
and sibling-fork URL fixes that aelrynth git still has as tombohar links.

Co-authored-by: BlankenshipG <BlankenshipG@users.noreply.github.com>
This commit is contained in:
Cursor Agent 2026-10-04 15:43:50 +00:00
commit 4ddbb52eb2
No known key found for this signature in database
244 changed files with 59710 additions and 3067 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using RynthCore.Engine.Hooking;
namespace RynthCore.Engine.Compatibility;
@ -25,14 +26,36 @@ namespace RynthCore.Engine.Compatibility;
internal static class TimeSyncHooks
{
private const int HandleTimeSynchVa = 0x005448F0;
// Verified unique + lands exactly at HandleTimeSynchVa offline (tools/pe_pattern.py).
private static readonly byte?[] HandleTimeSynchPattern = [ 0x55, 0x8B, 0xEC, 0x83, 0xE4, 0xF8, 0x83, 0xEC, 0x10, 0x8B ];
private const int TimeSyncHeaderMTimeOffset = 24;
private static IntPtr _originalHandleTimeSynch;
private static bool _initialized;
// Server-time / wall-clock reference pair, updated each time sync arrives.
private static double _lastServerTime;
private static long _lastWallClockTicks;
// Deep-audit finding #19 (2026-06-18): these used to be two plain fields
// (double + long) written on the net thread and read on the pump thread
// with no atomicity — on x86 a 64-bit load/store isn't atomic, so a
// reader could observe a torn _lastWallClockTicks (worst case a
// mis-timed rebuff from a wildly wrong elapsed time). Publishing both as
// one immutable snapshot object, swapped via Volatile.Write/Read, fixes
// that AND the subtler cross-field issue a per-field Interlocked fix
// wouldn't: a reader can no longer see a NEW _lastServerTime paired with
// the OLD _lastWallClockTicks (or vice versa) — the reference swap is
// all-or-nothing.
private sealed class TimeSyncSnapshot
{
public readonly double ServerTime;
public readonly long WallClockTicks;
public TimeSyncSnapshot(double serverTime, long wallClockTicks)
{
ServerTime = serverTime;
WallClockTicks = wallClockTicks;
}
}
private static TimeSyncSnapshot? _snapshot;
public static bool IsInitialized => _initialized;
@ -42,12 +65,12 @@ internal static class TimeSyncHooks
/// </summary>
public static double GetCurrentServerTime()
{
long wallTicks = _lastWallClockTicks;
if (wallTicks == 0)
TimeSyncSnapshot? snap = Volatile.Read(ref _snapshot);
if (snap == null || snap.WallClockTicks == 0)
return 0;
double elapsed = (DateTime.UtcNow.Ticks - wallTicks) / (double)TimeSpan.TicksPerSecond;
return _lastServerTime + elapsed;
double elapsed = (DateTime.UtcNow.Ticks - snap.WallClockTicks) / (double)TimeSpan.TicksPerSecond;
return snap.ServerTime + elapsed;
}
public static void Initialize()
@ -55,10 +78,15 @@ internal static class TimeSyncHooks
if (_initialized)
return;
var ptr = new IntPtr(HandleTimeSynchVa);
if (!SmartBoxLocator.IsPointerInModule(ptr))
if (!AcClientModule.TryReadTextSection(out AcClientTextSection text))
{
RynthLog.Compat($"Compat: time-sync hook pointer looks invalid (0x{HandleTimeSynchVa:X8})");
RynthLog.Compat("Compat: time-sync hook - acclient .text not readable.");
return;
}
HookResolver.ResolveResult resolved = HookResolver.Resolve(text, "TimeSync.HandleTimeSynch", HandleTimeSynchPattern, HandleTimeSynchVa);
if (!resolved.Success)
{
RynthLog.Compat($"Compat: time-sync hook unresolved (0x{HandleTimeSynchVa:X8})");
return;
}
@ -67,11 +95,11 @@ internal static class TimeSyncHooks
unsafe
{
delegate* unmanaged[Thiscall]<IntPtr, IntPtr, IntPtr, void> detour = &HandleTimeSynchDetour;
MinHook.Hook(ptr, (IntPtr)detour, out _originalHandleTimeSynch);
MinHook.Hook(resolved.Address, (IntPtr)detour, out _originalHandleTimeSynch);
}
_initialized = true;
RynthLog.Verbose($"Compat: time-sync hook ready - HandleTimeSynch=0x{HandleTimeSynchVa:X8}");
RynthLog.Verbose($"Compat: time-sync hook ready - HandleTimeSynch=0x{resolved.Address.ToInt32():X8} ({resolved.Detail})");
}
catch (Exception ex)
{
@ -90,8 +118,7 @@ internal static class TimeSyncHooks
double serverTime = BitConverter.Int64BitsToDouble(bits);
if (serverTime > 0)
{
_lastServerTime = serverTime;
_lastWallClockTicks = DateTime.UtcNow.Ticks;
Volatile.Write(ref _snapshot, new TimeSyncSnapshot(serverTime, DateTime.UtcNow.Ticks));
}
}
}