Add radar/chat suppression hooks and custom-widget API

- ChatHooks: captures gmMainChatUI via ListenToElementMessage and hides it
  via UIElement::SetVisible(false), re-asserted every frame from EndSceneHook.
  Suppression toggle restores visibility when flipped off.
- RadarHooks: also suppresses gmRadarUI::DrawChildren (bezel/compass/coords)
  alongside DrawObjects so the whole retail radar goes blank.
- PluginContract v55: SetRadarSuppressed + SetChatSuppressed fn pointers
  exposed to plugins via RynthCoreHost.
This commit is contained in:
tombohar 2026-04-23 21:08:26 -04:00
commit 01c7a549bd
8 changed files with 312 additions and 20 deletions

View file

@ -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;
/// <summary>
/// When true, TickHide() calls UIElement::SetVisible(false) on the captured
/// chat widget each frame, hiding the retail chatbox. Re-asserted every frame.
/// </summary>
public static bool SuppressOriginalChat;
/// <summary>The captured gmMainChatUI singleton. Zero until the user interacts
/// with the chat at least once (hover, click, message dispatch).</summary>
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<ListenToElementMessageDelegate>(
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);
}
/// <summary>True once we've issued SetVisible(false) — used so we know to
/// restore visibility one frame after the suppress flag flips off.</summary>
private static bool _isHiddenAsserted;
/// <summary>
/// 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.
/// </summary>
public static unsafe void TickHide()
{
IntPtr inst = _gmMainChatInstance;
if (inst == IntPtr.Zero) return;
if (SuppressOriginalChat)
{
try
{
((delegate* unmanaged[Thiscall]<IntPtr, int, void>)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]<IntPtr, int, void>)UIElementSetVisibleVa)(inst, 1);
}
catch { /* best-effort */ }
_isHiddenAsserted = false;
}
}
}

View file

@ -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;
/// <summary>
/// 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.
/// </summary>
public static bool SuppressOriginalDraw;
/// <summary>
/// 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<GmRadarUIDrawChildrenDelegate>(
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);
}
/// <summary>
/// Returns the retail radar element's current screen rect (x0,y0,x1,y1 in
/// pixels, exclusive on x1/y1). Calls UIElement::GetSurfaceBox on the

View file

@ -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;

View file

@ -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);

View file

@ -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+.</summary>
public IntPtr GetRadarRectFn;
/// <summary>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+.</summary>
public IntPtr SetRadarSuppressedFn;
/// <summary>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+.</summary>
public IntPtr SetChatSuppressedFn;
}
@ -492,7 +504,7 @@ internal struct RynthCoreAPI
/// <summary>Current API version. Bump when adding fields to RynthCoreAPI.</summary>
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);

View file

@ -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));