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(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; } }