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