Appraisals - RequestId slot merges a pending request for the same object (PendingIdQueue), as the QueryHealth slot does. - AutoId: per-object last-sent time kept across client releases; no re-appraisal within 10 min while the data is cached, unless the server deleted the object (0xF747). Map bounded at 8192 with a prune off the create path. The sent mark is set at the real send in DrainRequestIds, not at queue time. - The selected-target priority appraisal is skipped when its data is cached and fresh. Main thread frozen - Plugin ticks pause while AC's main thread is frozen (a main-thread message box, or no beat for the hang watchdog's threshold); events keep flowing. Exposed through GetEngineStatusJson (mainThreadFrozen, mainThreadStallMs, pluginTicksPausedMs) and the heartbeat (pump=paused). Crashes - AC's OUT OF MEMORY box: one full-memory dump to <install>\Dumps before the box shows, newest 3 kept. - Native crash logger: pid, tid and date per entry; once per address; .NET Framework AVs never charged to the limit; last-chance handler; stack overflow and heap corruption codes. Build-SehTrampoline gains -NoDeploy. - ProcessExitHooks: dead fail-fast detours removed; crash exit codes pass straight through TerminateProcess/NtTerminateProcess. - CoreCLR's own crash dump is left to the loader's existing ConfigureCrashDumps (Loader.c, Logs\dumps\clr_*.dmp) and Publish-EngineCoreClr's createdump/DAC handling; this change adds no second setup. World transitions - AcMainThreadQueue holds queued game-mutating work while the player is dead, in portal space, or within 5 s after either or after a non-adjacent landblock jump. When the window ends, stale casts, uses, selects and appraisals are dropped and stops and cancels run in order. Walking across an adjacent landblock border never gates. State is re-sampled after AC's tick, before the post-tick drain. - Inventory panel actions are held during a transition too. Diagnostics - Heartbeat valargest= (largest free VA block); free and largest logged before and after each plugin load and unload. - CastSpell comments corrected to the actual main-thread paths. Tests - tools\EngineRobustnessTests and native\SehTrampoline\tests\CrashLogFilterTests.c. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
231 lines
12 KiB
C#
231 lines
12 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using RynthCore.Engine.Hooking;
|
|
|
|
namespace RynthCore.Engine.Compatibility;
|
|
|
|
/// <summary>
|
|
/// Hooks AC's top-level per-frame game-logic tick, <c>Client::UseTime</c>
|
|
/// (acclient.map 0x00010FA0 + 0x401000 = 0x00411FA0; ImageBase 0x00400000, ASLR
|
|
/// off), to drain the marshalled CAST queue on AC's MAIN thread IN THE GAME-LOGIC
|
|
/// PHASE.
|
|
///
|
|
/// Why this tick (not EndScene): a cast must run where AC processes player actions
|
|
/// and advances its cast state machine. EndScene is the RENDER phase — marshalling
|
|
/// the cast there fired IncrementBusyCount but never completed the cast (proven
|
|
/// dead-end, reverted 2026-05-19: no projectile/damage, busy leaked, cast slot
|
|
/// jammed). Client::UseTime IS the game-logic phase, so the cast completes there.
|
|
///
|
|
/// Why marshal casts at all: CastSpell is the LAST off-thread AC mutator. Run off
|
|
/// the pump thread it mutates AC's single-threaded object/animation/enchant graph
|
|
/// and corrupts it; AC then AVs LATER in its own per-tick code — CSequence::
|
|
/// update_internal (animation, captured 2026-06-03), 0x67E779 (classify), 0x0055FA24
|
|
/// (range-list). Running the cast here, on AC's own game thread, removes that race
|
|
/// at the source — the root fix for the off-thread object-graph corruption class.
|
|
///
|
|
/// Thiscall detour, same proven pattern as DbCacheTeardownHooks / LogoutLifecycleHooks.
|
|
/// DrainCasts early-returns when no cast is pending, so the detour is alloc-free in
|
|
/// the common per-frame case — the same safe profile as the existing EndScene detour.
|
|
/// </summary>
|
|
internal static class GameTickHooks
|
|
{
|
|
/// <summary>Set once the Decal-coexistence plugin pump starts (no EndScene hook in that
|
|
/// client): the UseTime detour then also runs EngineFrameController.RunHeadlessPrefetch.
|
|
/// Never set in a client without Decal.</summary>
|
|
internal static volatile bool HeadlessPrefetch;
|
|
|
|
private const int UseTimeVa = 0x00411FA0; // Client::UseTime — thiscall, returns bool in AL
|
|
|
|
// Client::UseTime returns a bool (AL): AC's main loop drives it as
|
|
// do { } while ( UseTime() != 0 );
|
|
// so the return value MUST be propagated. A void delegate discards AC's AL
|
|
// and the loop then reads whatever the managed epilogue left in EAX — when
|
|
// that is nonzero the loop never exits and the client hard-freezes (main
|
|
// thread spins in UseTime pumping messages but never rendering; bot threads
|
|
// keep running). Dump-confirmed root cause, 2026-06-05.
|
|
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
|
|
private delegate int ThisCallBoolDelegate(IntPtr thisPtr);
|
|
|
|
private static ThisCallBoolDelegate? _originalUseTime;
|
|
private static ThisCallBoolDelegate? _useTimeDetour;
|
|
private static IntPtr _targetAddress;
|
|
private static int _installed;
|
|
private static int _exitLatch;
|
|
|
|
// Verified unique + lands exactly at UseTimeVa offline (tools/pe_pattern.py).
|
|
// Replaces the prior warn-only prologue check; null = wildcard (rel32 call operand).
|
|
private static readonly byte?[] UseTimePattern = [ 0x56, 0x8B, 0xF1, 0xE8, null, null, null, null, 0xE8, null, null, null, null, 0x84, 0xC0, 0x74 ];
|
|
|
|
public static bool IsInstalled => Volatile.Read(ref _installed) != 0;
|
|
|
|
/// <summary>Client::UseTime returned 0: AC's game loop is exiting (any exit path).</summary>
|
|
public static bool GameLoopExiting => Volatile.Read(ref _exitLatch) != 0;
|
|
|
|
public static void Initialize()
|
|
{
|
|
if (Interlocked.CompareExchange(ref _installed, 1, 0) != 0)
|
|
return;
|
|
|
|
if (!AcClientModule.TryReadTextSection(out AcClientTextSection textSection))
|
|
{
|
|
Volatile.Write(ref _installed, 0);
|
|
RynthLog.Compat("GameTickHooks: acclient.exe not available.");
|
|
return;
|
|
}
|
|
|
|
HookResolver.ResolveResult resolved = HookResolver.Resolve(textSection, "GameTick.UseTime", UseTimePattern, UseTimeVa);
|
|
if (!resolved.Success)
|
|
{
|
|
Volatile.Write(ref _installed, 0);
|
|
RynthLog.Compat($"GameTickHooks: Client::UseTime unresolved (VA 0x{UseTimeVa:X8}) — cast marshalling NOT installed.");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
_targetAddress = resolved.Address;
|
|
_useTimeDetour = UseTimeDetour;
|
|
IntPtr detourPtr = Marshal.GetFunctionPointerForDelegate(_useTimeDetour);
|
|
_originalUseTime = Marshal.GetDelegateForFunctionPointer<ThisCallBoolDelegate>(
|
|
MinHook.HookCreate(_targetAddress, detourPtr));
|
|
Thread.MemoryBarrier();
|
|
MinHook.Enable(_targetAddress);
|
|
Volatile.Write(ref _installed, 1);
|
|
RynthLog.Compat($"GameTickHooks: Client::UseTime hooked @ 0x{_targetAddress.ToInt32():X8} ({resolved.Detail}) — cast drain on game-logic tick.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Volatile.Write(ref _installed, 0);
|
|
RynthLog.Compat($"GameTickHooks: install threw {ex.GetType().Name}: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private static int UseTimeDetour(IntPtr thisPtr)
|
|
{
|
|
// Hang-watchdog beat from the game-logic tick (no frame count): the
|
|
// EndScene beat alone left the watchdog blind to a main thread that
|
|
// wedges before the new generation's EndScene hook installs.
|
|
try { MainThreadHangWatchdog.MainThreadBeatNoFrame(); }
|
|
catch { }
|
|
// Diag (diag/logoff-origin): 1 Hz sample of the client's own input-idle clock.
|
|
try { LogoffOriginProbe.SampleIdleOnGameThread(); }
|
|
catch { }
|
|
// Refresh TeleportStateHooks' main-thread cache every real game tick. This
|
|
// detour (unlike EndScene) fires on AC's true main thread in EVERY mode
|
|
// including Decal-coexistence (no D3D9 hook there) — without this,
|
|
// off-thread callers of IsPortaling in coexistence mode would see a
|
|
// perpetually stale cache since nothing else ever reads it on-thread.
|
|
// The getter itself does the guarded read + cache write; discard the value.
|
|
bool portaling = false;
|
|
try { portaling = TeleportStateHooks.IsPortaling; }
|
|
catch { }
|
|
// Death / portal space / teleport lines, for lining crashes up with what
|
|
// the character was doing (PlayerLifecycleLog). Also the world-transition state the
|
|
// AcMainThreadQueue drains gate on: sampled BEFORE the cast drain below, so a cast queued
|
|
// as the player dies is held in this same tick, not run into the death.
|
|
try { PlayerLifecycleLog.SampleOnGameThread(portaling); }
|
|
catch { }
|
|
|
|
// Drain any queued cast BEFORE AC's game-logic tick: SelectItem sets AC's
|
|
// selection and the cast initiates, then THIS SAME UseTime call processes it to
|
|
// completion — mimicking AC's natural input -> UseTime flow. (Draining AFTER the
|
|
// tick landed self-buffs but not targeted casts: the selection set at the end of
|
|
// tick N was clobbered before tick N+1 read it.) Alloc-free when no cast is
|
|
// pending. Must never throw into AC's native frame.
|
|
try { AcMainThreadQueue.DrainCasts(); }
|
|
catch { }
|
|
|
|
// Run AC's own game-logic + physics + animation pass.
|
|
int result;
|
|
try { result = _originalUseTime!(thisPtr); }
|
|
catch { result = 0; }
|
|
|
|
// ── Game loop is exiting — quiesce BEFORE AC's teardown ──────────────
|
|
// AC's main loop is `do { } while (UseTime() != 0)`; a 0 return means
|
|
// this was the LAST tick and AC tears its world down next (DBCache::
|
|
// DestroyObjectCaches frees every DBObj). This is the only close signal
|
|
// that covers ALL exit paths: the in-game exit / logout-quit flow never
|
|
// delivers WM_CLOSE, so the Win32Backend/AvaloniaOverlay WM_CLOSE
|
|
// quiesce never ran and the ~63 Hz pump kept firing Move/SetAutoRun/
|
|
// object reads into freed objects -> the on-close AV at acclient
|
|
// 0x00416C86 (DBOCache::DestroyObj reading [null+0x28]). We are still
|
|
// INSIDE the final UseTime call here — AC objects are all valid — so
|
|
// the pump's in-flight frame finishes safely during the bounded join.
|
|
if (result == 0 && Interlocked.Exchange(ref _exitLatch, 1) == 0)
|
|
{
|
|
try { RynthLog.Compat("GameTickHooks: Client::UseTime returned 0 — game loop exiting; quiescing plugin pump + late input before AC teardown."); }
|
|
catch { }
|
|
// Drop the busy watchdog's cached ClientUISystem ptr (freed next).
|
|
try { BusyCountHooks.ResetSession(); }
|
|
catch { }
|
|
// Arm the late mouse/cursor swallow in BOTH game-window subclasses
|
|
// (the 0x0056547B UpdateCursorState close-AV trigger) — WM_CLOSE
|
|
// never fired on this path, so neither latch is set yet.
|
|
try { ImGuiBackend.Win32Backend.MarkCloseInFlight(); }
|
|
catch { }
|
|
try { UI.RynthOverlayWindow.MarkCloseInFlight(); }
|
|
catch { }
|
|
try { EntryPoint.StopTickPumpAndJoin(); }
|
|
catch { }
|
|
// Skip the post-tick drains/watchdog below — no engine-issued
|
|
// mutation may run once the loop has decided to exit.
|
|
return 0;
|
|
}
|
|
|
|
// Drain the marshalled-action ring AFTER AC's tick, NOT before.
|
|
// ⚠ Issuing a UseObject (corpse open) / movement / stance change starts
|
|
// or REPLACES the player's CSequence motion — and AC's animation update
|
|
// (CPhysicsObj::update_object -> CSequence::update_internal ->
|
|
// AnimSequenceNode::get_high_frame) runs inside _originalUseTime above.
|
|
// Draining BEFORE that call meant the SAME tick walked the sequence we
|
|
// had just perturbed, hitting a freed/null anim node -> hard AV at
|
|
// acclient 0x5263xx/0x5264xx (null+0xC). Live-diagnosed 2026-06-11
|
|
// during a rapid corpse-open grind (AC-only crash stack, no engine
|
|
// frames). Draining after lets AC install the new gesture's node and
|
|
// walk it cleanly on its NEXT tick. The EndScene drain (the other call
|
|
// site) is already post-physics in the frame, so it was never the
|
|
// trigger. (Casts stay PRE-tick above — their selection-timing
|
|
// requirement, and they go through the separate cast slot.)
|
|
// Re-sample the world-transition state first: a death, a portal entry or a
|
|
// landblock jump can begin INSIDE AC's tick above, and the pre-tick sample would
|
|
// let this drain run queued work into it. Edges are logged once whichever sample
|
|
// sees them.
|
|
try
|
|
{
|
|
bool portalingAfter = false;
|
|
try { portalingAfter = TeleportStateHooks.IsPortaling; }
|
|
catch { }
|
|
PlayerLifecycleLog.SampleOnGameThread(portalingAfter);
|
|
}
|
|
catch { }
|
|
try { AcMainThreadQueue.Drain(); }
|
|
catch { }
|
|
// Main-thread snapshots the pump reads instead of AC memory (player id,
|
|
// cast gate, pose, ...). Post-tick, so they reflect this tick's physics.
|
|
try { MainThreadSnapshots.Tick(); }
|
|
catch { }
|
|
// Decal clients only (no EndScene hook): the prefetches EngineFrameController
|
|
// runs per frame. False for every other client - one flag check.
|
|
if (HeadlessPrefetch)
|
|
{
|
|
try { ImGuiBackend.EngineFrameController.RunHeadlessPrefetch(); }
|
|
catch { }
|
|
}
|
|
// Busy watchdog: also post-tick now. Its decrement / cursor-refresh
|
|
// touch m_cBusy, not the motion sequence, so order is immaterial here;
|
|
// keeping all engine-issued mutations after AC's pass is the invariant.
|
|
try { BusyCountHooks.CheckWatchdog(); }
|
|
catch { }
|
|
// Flight recorder: per-second summary, stall detectors, scheduled automatic dump.
|
|
try { Net.FlightRecorder.MainTick(); }
|
|
catch { }
|
|
// AC's "server has disconnected" screen: close the client after a short wait so a
|
|
// launcher can relaunch it (EngineSettings.CloseOnServerDisconnect).
|
|
try { ServerDisconnectClose.MainTick(); }
|
|
catch { }
|
|
|
|
// MUST return Client::UseTime's own bool — AC loops do/while on it.
|
|
return result;
|
|
}
|
|
}
|