diff --git a/src/RynthCore.Engine/Compatibility/ChatHooks.cs b/src/RynthCore.Engine/Compatibility/ChatHooks.cs new file mode 100644 index 0000000..a1a569e --- /dev/null +++ b/src/RynthCore.Engine/Compatibility/ChatHooks.cs @@ -0,0 +1,145 @@ +// ============================================================================ +// RynthCore.Engine - Compatibility/ChatHooks.cs +// +// Hooks gmMainChatUI::ListenToElementMessage purely to capture the widget's +// 'this' pointer on first UI dispatch. Per-frame visibility assertion is +// driven from EndSceneHook (ChatHooks.TickHide), which avoids the unsafe +// UseTime trampoline that was crashing with 0xC000001D — UseTime's prologue +// contains a relative jump MinHook on x86 mis-relocated. ListenToElementMessage +// is a large function with a standard MSVC prologue, safe to hook. +// +// VA derivation (map offset + 0x00401000 = live VA): +// 000CD6F0 gmMainChatUI::ListenToElementMessage(UIElementMessageInfo const &) +// → 0x004CE6F0 +// 00061390 UIElement::SetVisible(bool) → 0x00462390 +// ============================================================================ + +using System; +using System.Runtime.InteropServices; +using System.Threading; +using RynthCore.Engine.Hooking; + +namespace RynthCore.Engine.Compatibility; + +internal static class ChatHooks +{ + private const int GmMainChatUIListenMsgVa = 0x004CE6F0; + private const int UIElementSetVisibleVa = 0x00462390; + + [UnmanagedFunctionPointer(CallingConvention.ThisCall)] + private delegate void ListenToElementMessageDelegate(IntPtr thisPtr, IntPtr msgInfo); + + private static ListenToElementMessageDelegate? _originalListen; + private static ListenToElementMessageDelegate? _listenDetour; // held alive to prevent GC + + private static IntPtr _gmMainChatInstance; + private static bool _hookInstalled; + private static string _statusMessage = "Not initialized."; + + public static bool IsInstalled => _hookInstalled; + public static string StatusMessage => _statusMessage; + + /// + /// When true, TickHide() calls UIElement::SetVisible(false) on the captured + /// chat widget each frame, hiding the retail chatbox. Re-asserted every frame. + /// + public static bool SuppressOriginalChat; + + /// The captured gmMainChatUI singleton. Zero until the user interacts + /// with the chat at least once (hover, click, message dispatch). + public static IntPtr GmMainChatInstance => _gmMainChatInstance; + + public static void Initialize() + { + if (_hookInstalled) + return; + + if (!AcClientModule.TryReadTextSection(out AcClientTextSection textSection)) + { + _statusMessage = "acclient.exe not available."; + return; + } + + int funcOff = GmMainChatUIListenMsgVa - textSection.TextBaseVa; + if (funcOff < 0 || funcOff >= textSection.Bytes.Length) + { + _statusMessage = $"ListenToElementMessage VA out of range @ 0x{GmMainChatUIListenMsgVa:X8}."; + RynthLog.Compat($"Compat: chat hook failed - {_statusMessage}"); + return; + } + + byte firstByte = textSection.Bytes[funcOff]; + if (firstByte is 0x00 or 0xCC or 0xC3) + { + _statusMessage = $"ListenToElementMessage looks invalid @ 0x{GmMainChatUIListenMsgVa:X8} (opcode 0x{firstByte:X2})."; + RynthLog.Compat($"Compat: chat hook failed - {_statusMessage}"); + return; + } + + try + { + IntPtr targetAddress = new IntPtr(textSection.TextBaseVa + funcOff); + _listenDetour = ListenDetour; + IntPtr detourPtr = Marshal.GetFunctionPointerForDelegate(_listenDetour); + _originalListen = Marshal.GetDelegateForFunctionPointer( + MinHook.HookCreate(targetAddress, detourPtr)); + Thread.MemoryBarrier(); + MinHook.Enable(targetAddress); + + _hookInstalled = true; + _statusMessage = $"Hooked gmMainChatUI::ListenToElementMessage @ 0x{targetAddress.ToInt32():X8}."; + RynthLog.Verbose($"Compat: chat hook ready @ 0x{targetAddress.ToInt32():X8}, firstByte=0x{firstByte:X2}"); + } + catch (Exception ex) + { + _statusMessage = ex.Message; + RynthLog.Compat($"Compat: chat hook failed - {ex.Message}"); + } + } + + private static void ListenDetour(IntPtr thisPtr, IntPtr msgInfo) + { + if (thisPtr != IntPtr.Zero) + _gmMainChatInstance = thisPtr; + + _originalListen!(thisPtr, msgInfo); + } + + /// True once we've issued SetVisible(false) — used so we know to + /// restore visibility one frame after the suppress flag flips off. + private static bool _isHiddenAsserted; + + /// + /// Per-frame visibility assertion — called from EndSceneHook every frame. + /// While suppression is on, calls SetVisible(false) every frame so the game + /// can't sneak the chat back on. When suppression flips off, calls + /// SetVisible(true) one time to restore the retail chatbox. + /// + public static unsafe void TickHide() + { + IntPtr inst = _gmMainChatInstance; + if (inst == IntPtr.Zero) return; + + if (SuppressOriginalChat) + { + try + { + ((delegate* unmanaged[Thiscall])UIElementSetVisibleVa)(inst, 0); + } + catch { /* best-effort */ } + _isHiddenAsserted = true; + return; + } + + // Suppression is off — if we previously hid it, show it again now. + if (_isHiddenAsserted) + { + try + { + ((delegate* unmanaged[Thiscall])UIElementSetVisibleVa)(inst, 1); + } + catch { /* best-effort */ } + _isHiddenAsserted = false; + } + } +} diff --git a/src/RynthCore.Engine/Compatibility/RadarHooks.cs b/src/RynthCore.Engine/Compatibility/RadarHooks.cs index df49db8..f1591bc 100644 --- a/src/RynthCore.Engine/Compatibility/RadarHooks.cs +++ b/src/RynthCore.Engine/Compatibility/RadarHooks.cs @@ -1,14 +1,18 @@ // ============================================================================ // RynthCore.Engine - Compatibility/RadarHooks.cs // -// Hooks gmRadarUI::DrawObjects to capture the singleton 'this' pointer. +// Hooks gmRadarUI::DrawObjects (blips) and gmRadarUI::DrawChildren (bezel, +// compass tokens, coord readouts) to capture the singleton 'this' pointer +// and, when SuppressOriginalDraw is set, blank the vanilla radar entirely +// so a plugin can own that rect. +// // Exposes TryGetRadarRect, which asks UIElement::GetSurfaceBox for the -// radar element's current screen rect (used to overlay dungeon walls on -// the retail radar). +// radar element's current screen rect. // // VA derivation (map offset + 0x00401000 = live VA): -// 000D8FE0 gmRadarUI::DrawObjects(UISurface*) → 0x004D9FE0 -// 00061220 UIElement::GetSurfaceBox(Box2D*) → 0x00462220 +// 000D8FE0 gmRadarUI::DrawObjects(UISurface*) → 0x004D9FE0 +// 000D9380 gmRadarUI::DrawChildren(Box2D&,Box2D&,SmartArray&,UISurface*) → 0x004DA380 +// 00061220 UIElement::GetSurfaceBox(Box2D*) → 0x00462220 // ============================================================================ using System; @@ -20,14 +24,21 @@ namespace RynthCore.Engine.Compatibility; internal static class RadarHooks { - private const int GmRadarUIDrawObjectsVa = 0x004D9FE0; + private const int GmRadarUIDrawObjectsVa = 0x004D9FE0; + private const int GmRadarUIDrawChildrenVa = 0x004DA380; private const int UIElementGetSurfaceBoxVa = 0x00462220; [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void GmRadarUIDrawObjectsDelegate(IntPtr thisPtr, IntPtr uiSurface); - private static GmRadarUIDrawObjectsDelegate? _originalDrawObjects; - private static GmRadarUIDrawObjectsDelegate? _drawObjectsDetour; // held alive to prevent GC + [UnmanagedFunctionPointer(CallingConvention.ThisCall)] + private delegate void GmRadarUIDrawChildrenDelegate(IntPtr thisPtr, IntPtr clipRect, IntPtr clipInside, IntPtr boxArray, IntPtr uiSurface); + + private static GmRadarUIDrawObjectsDelegate? _originalDrawObjects; + private static GmRadarUIDrawObjectsDelegate? _drawObjectsDetour; // held alive to prevent GC + private static GmRadarUIDrawChildrenDelegate? _originalDrawChildren; + private static GmRadarUIDrawChildrenDelegate? _drawChildrenDetour; + private static IntPtr _gmRadarUIInstance; private static bool _hookInstalled; private static string _statusMessage = "Not initialized."; @@ -35,6 +46,13 @@ internal static class RadarHooks public static bool IsInstalled => _hookInstalled; public static string StatusMessage => _statusMessage; + /// + /// When true, the detours skip the original gmRadarUI draw calls, blanking + /// the vanilla radar so a custom renderer can own that rect. The UIElement + /// is still captured on entry, so GetSurfaceBox keeps working. + /// + public static bool SuppressOriginalDraw; + /// /// The captured gmRadarUI singleton. Zero until the radar has rendered at /// least once this session (usually within the first frame after login). @@ -52,20 +70,36 @@ internal static class RadarHooks return; } + bool drawObjectsOk = TryInstallDrawObjectsHook(textSection); + bool drawChildrenOk = TryInstallDrawChildrenHook(textSection); + + if (drawObjectsOk && drawChildrenOk) + { + _hookInstalled = true; + _statusMessage = "Hooked gmRadarUI::DrawObjects + DrawChildren."; + RynthLog.Verbose("Compat: radar hooks ready (DrawObjects + DrawChildren)."); + } + else + { + _statusMessage = $"Partial install — DrawObjects={drawObjectsOk}, DrawChildren={drawChildrenOk}."; + RynthLog.Compat($"Compat: radar hook {_statusMessage}"); + } + } + + private static bool TryInstallDrawObjectsHook(AcClientTextSection textSection) + { int funcOff = GmRadarUIDrawObjectsVa - textSection.TextBaseVa; if (funcOff < 0 || funcOff >= textSection.Bytes.Length) { - _statusMessage = $"gmRadarUI::DrawObjects VA out of range @ 0x{GmRadarUIDrawObjectsVa:X8}."; - RynthLog.Compat($"Compat: radar hook failed - {_statusMessage}"); - return; + RynthLog.Compat($"Compat: DrawObjects VA out of range @ 0x{GmRadarUIDrawObjectsVa:X8}."); + return false; } byte firstByte = textSection.Bytes[funcOff]; if (firstByte is 0x00 or 0xCC or 0xC3) { - _statusMessage = $"gmRadarUI::DrawObjects looks invalid @ 0x{GmRadarUIDrawObjectsVa:X8} (opcode 0x{firstByte:X2})."; - RynthLog.Compat($"Compat: radar hook failed - {_statusMessage}"); - return; + RynthLog.Compat($"Compat: DrawObjects looks invalid @ 0x{GmRadarUIDrawObjectsVa:X8} (opcode 0x{firstByte:X2})."); + return false; } try @@ -77,15 +111,46 @@ internal static class RadarHooks MinHook.HookCreate(targetAddress, detourPtr)); Thread.MemoryBarrier(); MinHook.Enable(targetAddress); - - _hookInstalled = true; - _statusMessage = $"Hooked gmRadarUI::DrawObjects @ 0x{targetAddress.ToInt32():X8}."; - RynthLog.Verbose($"Compat: radar hook ready @ 0x{targetAddress.ToInt32():X8}, firstByte=0x{firstByte:X2}"); + return true; } catch (Exception ex) { - _statusMessage = ex.Message; - RynthLog.Compat($"Compat: radar hook failed - {ex.Message}"); + RynthLog.Compat($"Compat: DrawObjects hook failed - {ex.Message}"); + return false; + } + } + + private static bool TryInstallDrawChildrenHook(AcClientTextSection textSection) + { + int funcOff = GmRadarUIDrawChildrenVa - textSection.TextBaseVa; + if (funcOff < 0 || funcOff >= textSection.Bytes.Length) + { + RynthLog.Compat($"Compat: DrawChildren VA out of range @ 0x{GmRadarUIDrawChildrenVa:X8}."); + return false; + } + + byte firstByte = textSection.Bytes[funcOff]; + if (firstByte is 0x00 or 0xCC or 0xC3) + { + RynthLog.Compat($"Compat: DrawChildren looks invalid @ 0x{GmRadarUIDrawChildrenVa:X8} (opcode 0x{firstByte:X2})."); + return false; + } + + try + { + IntPtr targetAddress = new IntPtr(textSection.TextBaseVa + funcOff); + _drawChildrenDetour = DrawChildrenDetour; + IntPtr detourPtr = Marshal.GetFunctionPointerForDelegate(_drawChildrenDetour); + _originalDrawChildren = Marshal.GetDelegateForFunctionPointer( + MinHook.HookCreate(targetAddress, detourPtr)); + Thread.MemoryBarrier(); + MinHook.Enable(targetAddress); + return true; + } + catch (Exception ex) + { + RynthLog.Compat($"Compat: DrawChildren hook failed - {ex.Message}"); + return false; } } @@ -94,9 +159,23 @@ internal static class RadarHooks if (thisPtr != IntPtr.Zero) _gmRadarUIInstance = thisPtr; + if (SuppressOriginalDraw) + return; + _originalDrawObjects!(thisPtr, uiSurface); } + private static void DrawChildrenDetour(IntPtr thisPtr, IntPtr clipRect, IntPtr clipInside, IntPtr boxArray, IntPtr uiSurface) + { + if (thisPtr != IntPtr.Zero) + _gmRadarUIInstance = thisPtr; + + if (SuppressOriginalDraw) + return; + + _originalDrawChildren!(thisPtr, clipRect, clipInside, boxArray, uiSurface); + } + /// /// Returns the retail radar element's current screen rect (x0,y0,x1,y1 in /// pixels, exclusive on x1/y1). Calls UIElement::GetSurfaceBox on the diff --git a/src/RynthCore.Engine/D3D9/EndSceneHook.cs b/src/RynthCore.Engine/D3D9/EndSceneHook.cs index 8be4cf3..1b7faf4 100644 --- a/src/RynthCore.Engine/D3D9/EndSceneHook.cs +++ b/src/RynthCore.Engine/D3D9/EndSceneHook.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; +using RynthCore.Engine.Compatibility; using RynthCore.Engine.Hooking; using RynthCore.Engine.ImGuiBackend; @@ -205,6 +206,9 @@ internal static class EndSceneHook _renderCount++; _frameCount++; + // Per-frame chatbox visibility assertion (no-op unless plugin enables suppression). + try { ChatHooks.TickHide(); } catch { /* never let this bring down EndScene */ } + if (_renderCount == 1 && _offscreenFilterDisabled && _skipCount > 0) { long installElapsedMs = Environment.TickCount64 - _installTick; diff --git a/src/RynthCore.Engine/EntryPoint.cs b/src/RynthCore.Engine/EntryPoint.cs index 65c6050..ea332dc 100644 --- a/src/RynthCore.Engine/EntryPoint.cs +++ b/src/RynthCore.Engine/EntryPoint.cs @@ -274,6 +274,7 @@ public static class EntryPoint RunInitStep("teleport-state hooks", TeleportStateHooks.Initialize); RunInitStep("salvage hooks", SalvageHooks.Initialize); RunInitStep("radar hooks", RadarHooks.Initialize); + RunInitStep("chat hooks", ChatHooks.Initialize); RunInitStep("do-motion hooks", DoMotionHooks.Initialize); RunInitStep("smartbox-setstate hooks", SmartBoxSetStateHooks.Initialize); RunInitStep("appraisal hooks", AppraisalHooks.Initialize); diff --git a/src/RynthCore.Engine/Plugins/PluginContract.cs b/src/RynthCore.Engine/Plugins/PluginContract.cs index febc423..5e45d76 100644 --- a/src/RynthCore.Engine/Plugins/PluginContract.cs +++ b/src/RynthCore.Engine/Plugins/PluginContract.cs @@ -485,6 +485,18 @@ internal struct RynthCoreAPI /// (x0,y0 top-left, x1,y1 bottom-right exclusive). Returns 1 on success, 0 /// if the radar has not rendered yet this session. Requires API v53+. public IntPtr GetRadarRectFn; + + /// Function pointer: void SetRadarSuppressed(int enabled) + /// When enabled=1, the engine skips the original gmRadarUI::DrawObjects call + /// so the vanilla radar stops rendering and a plugin can own that rect. + /// Requires API v54+. + public IntPtr SetRadarSuppressedFn; + + /// Function pointer: void SetChatSuppressed(int enabled) + /// When enabled=1, the engine calls UIElement::SetVisible(false) on the + /// gmMainChatUI each frame, hiding the retail chatbox entirely. + /// Requires API v55+. + public IntPtr SetChatSuppressedFn; } @@ -492,7 +504,7 @@ internal struct RynthCoreAPI /// Current API version. Bump when adding fields to RynthCoreAPI. internal static class PluginContractVersion { - public const uint Current = 53; + public const uint Current = 55; } internal static class ClientActionHookFlags @@ -891,3 +903,9 @@ internal unsafe delegate int GetObjectPalettesCallbackDelegate(uint objectId, ui [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal unsafe delegate int GetRadarRectCallbackDelegate(int* x0, int* y0, int* x1, int* y1); + +[UnmanagedFunctionPointer(CallingConvention.Cdecl)] +internal delegate void SetRadarSuppressedCallbackDelegate(int enabled); + +[UnmanagedFunctionPointer(CallingConvention.Cdecl)] +internal delegate void SetChatSuppressedCallbackDelegate(int enabled); diff --git a/src/RynthCore.Engine/Plugins/PluginManager.cs b/src/RynthCore.Engine/Plugins/PluginManager.cs index 310116d..75b4894 100644 --- a/src/RynthCore.Engine/Plugins/PluginManager.cs +++ b/src/RynthCore.Engine/Plugins/PluginManager.cs @@ -117,6 +117,8 @@ internal static class PluginManager private static DoJumpCallbackDelegate? _doJumpCallback; private static LaunchJumpWithMotionCallbackDelegate? _launchJumpWithMotionCallback; private static GetRadarRectCallbackDelegate? _getRadarRectCallback; + private static SetRadarSuppressedCallbackDelegate? _setRadarSuppressedCallback; + private static SetChatSuppressedCallbackDelegate? _setChatSuppressedCallback; private static SetMotionCallbackDelegate? _setMotionCallback; private static StopCompletelyCallbackDelegate? _stopCompletelyCallback; private static TurnToHeadingCallbackDelegate? _turnToHeadingCallback; @@ -1679,6 +1681,8 @@ internal static class PluginManager _doJumpCallback ??= DoJump; _launchJumpWithMotionCallback ??= LaunchJumpWithMotion; _getRadarRectCallback ??= GetRadarRect; + _setRadarSuppressedCallback ??= SetRadarSuppressed; + _setChatSuppressedCallback ??= SetChatSuppressed; _setMotionCallback ??= SetMotion; _stopCompletelyCallback ??= StopCompletely; _turnToHeadingCallback ??= TurnToHeading; @@ -1851,6 +1855,8 @@ internal static class PluginManager _api.DoJumpFn = Marshal.GetFunctionPointerForDelegate(_doJumpCallback); _api.LaunchJumpWithMotionFn = Marshal.GetFunctionPointerForDelegate(_launchJumpWithMotionCallback); _api.GetRadarRectFn = Marshal.GetFunctionPointerForDelegate(_getRadarRectCallback); + _api.SetRadarSuppressedFn = Marshal.GetFunctionPointerForDelegate(_setRadarSuppressedCallback); + _api.SetChatSuppressedFn = Marshal.GetFunctionPointerForDelegate(_setChatSuppressedCallback); } private static void ProbeClientHooks() @@ -2222,6 +2228,16 @@ internal static class PluginManager return 1; } + private static void SetRadarSuppressed(int enabled) + { + RadarHooks.SuppressOriginalDraw = enabled != 0; + } + + private static void SetChatSuppressed(int enabled) + { + ChatHooks.SuppressOriginalChat = enabled != 0; + } + private static int SetMotion(uint motion, int enabled) { return ToAbiBool(ClientActionHooks.SetMotion(motion, enabled != 0)); diff --git a/src/RynthCore.PluginSdk/RynthCoreApiNative.cs b/src/RynthCore.PluginSdk/RynthCoreApiNative.cs index 9331c38..b7cf770 100644 --- a/src/RynthCore.PluginSdk/RynthCoreApiNative.cs +++ b/src/RynthCore.PluginSdk/RynthCoreApiNative.cs @@ -104,4 +104,6 @@ public struct RynthCoreApiNative public IntPtr DoJumpFn; public IntPtr LaunchJumpWithMotionFn; public IntPtr GetRadarRectFn; + public IntPtr SetRadarSuppressedFn; + public IntPtr SetChatSuppressedFn; } diff --git a/src/RynthCore.PluginSdk/RynthCoreHost.cs b/src/RynthCore.PluginSdk/RynthCoreHost.cs index ea5c07a..c0298e9 100644 --- a/src/RynthCore.PluginSdk/RynthCoreHost.cs +++ b/src/RynthCore.PluginSdk/RynthCoreHost.cs @@ -104,6 +104,8 @@ public readonly unsafe struct RynthCoreHost public bool HasDoJump => _api.Version >= 51 && _api.DoJumpFn != IntPtr.Zero; public bool HasLaunchJumpWithMotion => _api.Version >= 52 && _api.LaunchJumpWithMotionFn != IntPtr.Zero; public bool HasGetRadarRect => _api.Version >= 53 && _api.GetRadarRectFn != IntPtr.Zero; + public bool HasSetRadarSuppressed => _api.Version >= 54 && _api.SetRadarSuppressedFn != IntPtr.Zero; + public bool HasSetChatSuppressed => _api.Version >= 55 && _api.SetChatSuppressedFn != IntPtr.Zero; // ─── Methods ──────────────────────────────────────────────────────────── @@ -282,6 +284,31 @@ public readonly unsafe struct RynthCoreHost } } + /// + /// When enabled, the engine suppresses the vanilla gmRadarUI::DrawObjects + /// call so the radar rect is blank and a plugin can own it entirely. + /// + public void SetRadarSuppressed(bool enabled) + { + if (_api.SetRadarSuppressedFn == IntPtr.Zero) + return; + + ((delegate* unmanaged[Cdecl])_api.SetRadarSuppressedFn)(enabled ? 1 : 0); + } + + /// + /// When enabled, the engine hides the retail gmMainChatUI each frame via + /// UIElement::SetVisible(false). Re-asserted every frame so the game can't + /// sneak it back on. + /// + public void SetChatSuppressed(bool enabled) + { + if (_api.SetChatSuppressedFn == IntPtr.Zero) + return; + + ((delegate* unmanaged[Cdecl])_api.SetChatSuppressedFn)(enabled ? 1 : 0); + } + public bool SetMotion(uint motion, bool enabled) { return _api.SetMotionFn != IntPtr.Zero &&