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>
88 lines
3.4 KiB
C#
88 lines
3.4 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}");
|
|
|
|
PluginManager.QueueCreateObject(objectId);
|
|
AutoIdService.Enqueue(objectId);
|
|
return result;
|
|
}
|
|
}
|