From cd6da18687d4e2bb1fc351a434e3ead696f8183a Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sun, 17 May 2026 02:29:16 -0500 Subject: [PATCH] Add GlueModuleAutoRegister + FUN_LOAD_GLUE_SCRIPT_FUNCTIONS hook Module-self-registration pattern for the glue Lua state, mirroring the existing in-game ModuleAutoRegister. Modules opt in with a file-scope GlueModuleAutoRegister; the hook on FUN_0046ABB0 (master glue init, single caller, 35-byte linear function) fires their callbacks after the engine finishes registering its own 109 glue functions onto the freshly-created glue state. Prior attempt hooked the inner FUN_0046DDF0 60-fn batch trampoline and crashed on Octo, likely from sibling-DLL collision on a small target. The linear caller is larger and less obvious; no crash, hook fires cleanly on initial launch and every world->glue return. --- src/DllMain.cpp | 17 +++++++++++++---- src/Game.cpp | 22 ++++++++++++++++++++++ src/Game.h | 31 +++++++++++++++++++++++++++++++ src/Offsets.h | 10 ++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/DllMain.cpp b/src/DllMain.cpp index edffe00e..25463124 100644 --- a/src/DllMain.cpp +++ b/src/DllMain.cpp @@ -20,6 +20,7 @@ static Game::FrameScript_Initialize_t FrameScript_Initialize_o = nullptr; static Game::LoadScriptFunctions_t LoadScriptFunctions_o = nullptr; +static Game::LoadGlueScriptFunctions_t LoadGlueScriptFunctions_o = nullptr; // `Frame::RegisterEvent` is `__thiscall(this, eventName)`. MSVC can't emit // __thiscall on free functions, but a __fastcall function with a dummy EDX @@ -57,6 +58,11 @@ static void __fastcall LoadScriptFunctions_h() { Event::Custom::EnableWrites(); } +static void __stdcall LoadGlueScriptFunctions_h() { + LoadGlueScriptFunctions_o(); + Game::RunGlueModuleRegistrations(); +} + // Every Lua-side `frame:RegisterEvent(...)` is a chance to claim a slot // for any custom event still waiting. By this point the engine's table // is fully populated and SuperWoWhook / other DLLs have done their @@ -83,14 +89,17 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved) { if (MH_EnableHook(target) != MH_OK) return FALSE; - // Three core init hooks stay here — each runs glue logic - // (PrepareForReload, RunModuleRegistrations, EnableWrites, - // RetryClaims) that's tightly coupled to DllMain state, so - // a declarative HookAutoRegister wouldn't simplify them. + // Four core init hooks stay here — each runs glue logic + // (PrepareForReload, RunModuleRegistrations, + // RunGlueModuleRegistrations, EnableWrites, RetryClaims) + // that's tightly coupled to DllMain state, so a declarative + // HookAutoRegister wouldn't simplify them. HOOK_FUNCTION(Offsets::FUN_FRAME_SCRIPT_INITIALIZE, FrameScript_Initialize_h, FrameScript_Initialize_o); HOOK_FUNCTION(Offsets::FUN_LOAD_SCRIPT_FUNCTIONS, LoadScriptFunctions_h, LoadScriptFunctions_o); + HOOK_FUNCTION(Offsets::FUN_LOAD_GLUE_SCRIPT_FUNCTIONS, + LoadGlueScriptFunctions_h, LoadGlueScriptFunctions_o); HOOK_FUNCTION(Offsets::FUN_FRAME_REGISTER_EVENT, FrameRegisterEvent_h, FrameRegisterEvent_o); diff --git a/src/Game.cpp b/src/Game.cpp index a395ad75..07aba8d2 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -88,6 +88,17 @@ void RegisterGlobalFunction(const char *name, CFunction func) { fn(name, func); } +// Aliased to the same engine entry; the engine reads VAR_LUA_STATE +// internally to decide which state to write to, and the glue hook +// runs while that pointer is set to the glue state. Wrapped as a +// named function (rather than `using RegisterGlueFunction = ...`) so +// callers express intent at the call site. +void RegisterGlueFunction(const char *name, CFunction func) { + auto fn = reinterpret_cast( + Offsets::FUN_FRAMESCRIPT_REGISTER_FUNCTION); + fn(name, func); +} + void RegisterFrameMethods(void *context, const FrameMethodEntry *table, int count) { auto fn = reinterpret_cast(Offsets::FUN_REGISTER_FRAME_METHODS); fn(table, count, context); @@ -167,6 +178,7 @@ void SetGlobalNumber(void *L, const char *name, double value) { namespace { ModuleAutoRegister *g_moduleHead = nullptr; +GlueModuleAutoRegister *g_glueModuleHead = nullptr; HookAutoRegister *g_hookHead = nullptr; } // namespace @@ -179,6 +191,16 @@ void RunModuleRegistrations() { node->fn(); } +GlueModuleAutoRegister::GlueModuleAutoRegister(Fn f) + : fn(f), next(g_glueModuleHead) { + g_glueModuleHead = this; +} + +void RunGlueModuleRegistrations() { + for (auto *node = g_glueModuleHead; node != nullptr; node = node->next) + node->fn(); +} + HookAutoRegister::HookAutoRegister(uintptr_t target, void *hook, void **original) : target(target), hook(hook), original(original), next(g_hookHead) { g_hookHead = this; diff --git a/src/Game.h b/src/Game.h index 1266dcb4..21134308 100644 --- a/src/Game.h +++ b/src/Game.h @@ -20,6 +20,11 @@ namespace Game { using FrameScript_Initialize_t = bool(__fastcall *)(); using LoadScriptFunctions_t = void(__fastcall *)(); +// Master glue Lua-state init function. `__stdcall` (no args, no return). +// Hooked to inject our own glue-side globals after the engine finishes +// registering its 109 glue functions. See FUN_LOAD_GLUE_SCRIPT_FUNCTIONS. +using LoadGlueScriptFunctions_t = void(__stdcall *)(); + namespace Lua { using CFunction = int(__fastcall *)(void *L); @@ -124,6 +129,16 @@ void *ResolveObject(void *L, int idx); // must use the WoW Lua C function ABI: `int __fastcall(void *L)`. void RegisterGlobalFunction(const char *name, CFunction func); +// Glue-state equivalent of `RegisterGlobalFunction`. Identical wire +// (calls `FrameScript_RegisterFunction`); the engine routes the +// registration to whichever Lua state `VAR_LUA_STATE` currently +// references. Only safe to call from inside a `GlueModuleAutoRegister` +// callback — at that point the engine has just finished registering +// its own 109 glue functions, and `VAR_LUA_STATE` still points at the +// glue state. Calling outside that window would silently target the +// wrong state. +void RegisterGlueFunction(const char *name, CFunction func); + // Frame-method registration entry: { name, func } pairs walked by the engine's // per-frame-type method-table iterator. Layout matches what the engine expects // natively — name first, then function pointer. @@ -192,6 +207,22 @@ struct ModuleAutoRegister { void RunModuleRegistrations(); +// Glue-state mirror of `ModuleAutoRegister`. Modules wanting login- +// screen exposure declare a file-scope +// `static const Game::GlueModuleAutoRegister _g{&RegisterGlueFunctions};` +// alongside (or instead of) the in-game `ModuleAutoRegister`. Glue +// callbacks run from the `FUN_LOAD_GLUE_SCRIPT_FUNCTIONS` post-hook, +// once per glue boot (game launch + every world→glue return). Use +// `Game::Lua::RegisterGlueFunction` from inside the callback. +struct GlueModuleAutoRegister { + using Fn = void (*)(); + explicit GlueModuleAutoRegister(Fn fn); + Fn fn; + GlueModuleAutoRegister *next; +}; + +void RunGlueModuleRegistrations(); + // Declarative MinHook registration. Each feature module declares a // file-scope `static const Game::HookAutoRegister _hookreg{target, // &hook_fn, reinterpret_cast(&original_fn)};` and DllMain's diff --git a/src/Offsets.h b/src/Offsets.h index a2a1cc5b..9aa5a4f2 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -18,6 +18,16 @@ enum Offsets { FUN_INVALID_FUNCTION_PTR_CHECK = 0x42A320, FUN_LOAD_SCRIPT_FUNCTIONS = 0x490250, + // Master glue Lua init — clean linear caller of all 5 glue batch + // trampolines (60 main + 11 char-select + 24 char-create + 10 realm + // + 4 frame globals = 109 total). Body: 0x0046ABB0..0x0046ABD2, + // 35 bytes, single caller (FUN_0046A7B0). Runs once per glue boot: + // initial launch and every world→glue return (log out). Post-hook + // is the glue analog of `FUN_LOAD_SCRIPT_FUNCTIONS` — by the time + // it returns, `VAR_LUA_STATE` points at the freshly populated glue + // state, so `FrameScript_RegisterFunction` writes land on it. + FUN_LOAD_GLUE_SCRIPT_FUNCTIONS = 0x0046ABB0, + // GameTooltip script-method prologue helpers (used to resolve self → CFrameScriptObject*). FUN_FRAMESCRIPT_PUSH_OBJECT = 0x6F3BC0, FUN_FRAMESCRIPT_GET_OBJECT = 0x6F3740,