RynthCore/src/RynthCore.Engine/Compatibility/CreateObjectHooks.cs
BlankenshipG a7cdbee135 fix(engine): seed object name/type at create; fatal-only native crash logger
CreateObject hook reads _name/_type from the PublicWeenieDesc so off-thread lookups work before the 500 ms identity snapshot resolves names (empty for ~10 s after login).

SehTrampoline v2: stack sweep bounded by StackBase, per-thread guards for self-faults and SEH_* wrapper AVs, unhandled-exception filter for fatal crashes, NativeAOT-module AVs logged immediately, other first-chance AVs deferred to a ring flushed only if the process dies.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-04 17:58:22 -04:00

91 lines
3.7 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Threading;
using RynthCore.Engine.Hooking;
using RynthCore.Engine.Plugins;
namespace RynthCore.Engine.Compatibility;
internal static class CreateObjectHooks
{
private const int CreateObjectVa = 0x005594B0;
// Verified unique + lands at 0x005594B0 offline (tools/pe_pattern.py).
private static readonly byte?[] CreateObjectPattern =
[
0x55, 0x8B, 0x6C, 0x24, 0x08, 0x56, 0x8B, 0xF1, 0x8B, 0x8E
];
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr CreateObjectDelegate(IntPtr thisPtr, uint objectId, IntPtr visualDesc, IntPtr physicsDesc, IntPtr weenieDesc);
private static CreateObjectDelegate? _originalCreateObject;
private static CreateObjectDelegate? _createObjectDetour;
private static IntPtr _targetAddress;
private static string _statusMessage = "Not probed yet.";
private static int _dispatchCount;
public static bool IsInstalled { get; private set; }
public static string StatusMessage => _statusMessage;
public static int DispatchCount => _dispatchCount;
public static void Initialize()
{
if (IsInstalled)
return;
if (!AcClientModule.TryReadTextSection(out AcClientTextSection textSection))
{
_statusMessage = "acclient.exe not available.";
return;
}
HookResolver.ResolveResult resolved = HookResolver.Resolve(textSection, "CreateObject.CreateObject", CreateObjectPattern, CreateObjectVa);
if (!resolved.Success)
{
_statusMessage = $"ACCObjectMaint::CreateObject unresolved (VA 0x{CreateObjectVa:X8}).";
RynthLog.Compat($"Compat: create-object hook failed - {_statusMessage}");
return;
}
try
{
_targetAddress = resolved.Address;
_createObjectDetour = CreateObjectDetour;
IntPtr detourPtr = Marshal.GetFunctionPointerForDelegate(_createObjectDetour);
_originalCreateObject = Marshal.GetDelegateForFunctionPointer<CreateObjectDelegate>(MinHook.HookCreate(_targetAddress, detourPtr));
Thread.MemoryBarrier();
MinHook.Enable(_targetAddress);
IsInstalled = true;
_statusMessage = $"Hooked ACCObjectMaint::CreateObject @ 0x{_targetAddress.ToInt32():X8}.";
RynthLog.Verbose($"Compat: create-object hook ready - CreateObject=0x{_targetAddress.ToInt32():X8}");
}
catch (Exception ex)
{
_statusMessage = ex.Message;
RynthLog.Compat($"Compat: create-object hook failed - {ex.Message}");
}
}
private static IntPtr CreateObjectDetour(IntPtr thisPtr, uint objectId, IntPtr visualDesc, IntPtr physicsDesc, IntPtr weenieDesc)
{
RecursionGuard.Tick("CreateObjectHooks.CreateObject");
if (objectId != 0 && visualDesc != IntPtr.Zero)
PaletteCache.ReadFromObjDesc(objectId, visualDesc);
IntPtr result = _originalCreateObject!(thisPtr, objectId, visualDesc, physicsDesc, weenieDesc);
if (result == IntPtr.Zero || objectId == 0)
return result;
int count = Interlocked.Increment(ref _dispatchCount);
if (count <= 0)
RynthLog.Verbose($"Compat: create object #{count} id=0x{objectId:X8} ptr=0x{result.ToInt32():X8}");
// Capture name/type from the create descriptor BEFORE the plugin is told about the
// object, so its off-thread classifier can read them on the very first attempt.
ClientObjectHooks.SeedIdentityFromCreate(objectId, weenieDesc);
PluginManager.QueueCreateObject(objectId);
AutoIdService.Enqueue(objectId);
return result;
}
}