mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
Vanilla has RegisterEvent / UnregisterEvent as frame methods but not the query IsEventRegistered (added in a later expansion). There's no engine Script_* to delegate to, so we replicate the membership check already embedded in Frame::RegisterEvent (FUN_00702140): find the event by name in the live event table (SStrCmpI), then walk that entry's subscriber chain (head@+0xC, next@+4, frame@+8, low-bit/null sentinel) for this frame. self is resolved via Game::Lua::ResolveObject, yielding the same CFrameScriptObject* the chain stores. Registered on the Frame method registry (VAR_FRAME_METHOD_REGISTRY, alongside RegisterEvent) so it resolves on any frame. Pure read, no new hooks. Returns a boolean, matching the 3.3.5-era contract; verified in-game (true for a registered event, false otherwise). Documented in docs/API.md and README.md.
322 lines
13 KiB
C++
322 lines
13 KiB
C++
// This file is part of ClassicAPI.
|
|
//
|
|
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
|
|
// of the GNU General Public License as published by the Free Software Foundation, either
|
|
// version 3 of the License, or (at your option) any later version.
|
|
//
|
|
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
// PURPOSE. See the GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along with
|
|
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
// Modern Region/Frame method backports.
|
|
//
|
|
// Modern addon ports routinely call these methods unconditionally —
|
|
// they're the C-level frame surface later clients take for granted.
|
|
// This module supplies the ones vanilla lacks (surfaced via
|
|
// AI_VoiceOver's modern code path, but generic to every port):
|
|
//
|
|
// - `region:SetPoint("RIGHT")` — the one-arg form. Vanilla's parser
|
|
// accepts every other modern form but errors when arg 3 is fully
|
|
// omitted; a co-hook on Script_SetPoint normalizes it to
|
|
// `(point, 0, 0)` (parent-relative, zero offsets — the exact modern
|
|
// semantic).
|
|
// - `region:SetSize(w, h)` / `region:GetSize()` — registered on the
|
|
// REGION base method registry, so they resolve on frames, buttons,
|
|
// textures, fontstrings — everything.
|
|
// - `frame:SetShown(shown)` — Show/Hide are per-branch script functions
|
|
// (each with its own object typecheck), so SetShown is registered
|
|
// separately on Frame / Texture / FontString with matching delegates.
|
|
// - `frame:SetResizeBounds(minW, minH [, maxW, maxH])` — the modern
|
|
// rename of vanilla's SetMinResize/SetMaxResize pair.
|
|
// - `frame:HookScript(scriptType, handler)` — absent in vanilla
|
|
// entirely. Chains after any existing handler and invokes `handler`
|
|
// with modern positional args (see the implementation comment).
|
|
//
|
|
// Implementation style: each new method delegates to the ENGINE's own
|
|
// Script_* implementations by reshaping the Lua stack and calling them
|
|
// (`int __fastcall(void *L)`, self at index 1) — no re-derived setters,
|
|
// so pixel/UI-scale conversion, object resolution, and type checking are
|
|
// all the engine's own code. Values are read out BEFORE any stack
|
|
// reshaping.
|
|
|
|
#include "Game.h"
|
|
#include "Offsets.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
|
|
namespace Frame::Modern {
|
|
|
|
namespace {
|
|
|
|
using ScriptFn_t = int(__fastcall *)(void *L);
|
|
|
|
int CallScript(uintptr_t fn, void *L) {
|
|
return reinterpret_cast<ScriptFn_t>(fn)(L);
|
|
}
|
|
|
|
// ---- SetSize / GetSize (Region base — all region types) -------------------
|
|
|
|
// `region:SetSize(width, height)` → SetWidth + SetHeight.
|
|
int __fastcall Script_SetSize(void *L) {
|
|
if (!Game::Lua::IsNumber(L, 2) || !Game::Lua::IsNumber(L, 3)) {
|
|
Game::Lua::Error(L, "Usage: region:SetSize(width, height)");
|
|
return 0;
|
|
}
|
|
const double h = Game::Lua::ToNumber(L, 3);
|
|
Game::Lua::SetTop(L, 2); // (self, width)
|
|
CallScript(Offsets::FUN_SCRIPT_REGION_SETWIDTH, L);
|
|
Game::Lua::SetTop(L, 1); // (self)
|
|
Game::Lua::PushNumber(L, h);
|
|
CallScript(Offsets::FUN_SCRIPT_REGION_SETHEIGHT, L);
|
|
return 0;
|
|
}
|
|
|
|
// `region:GetSize()` → width, height.
|
|
int __fastcall Script_GetSize(void *L) {
|
|
Game::Lua::SetTop(L, 1); // (self)
|
|
CallScript(Offsets::FUN_SCRIPT_REGION_GETWIDTH, L); // (self, w)
|
|
CallScript(Offsets::FUN_SCRIPT_REGION_GETHEIGHT, L); // (self, w, h)
|
|
return 2;
|
|
}
|
|
|
|
// ---- SetShown (per-branch Show/Hide delegates) -----------------------------
|
|
|
|
// `object:SetShown(shown)` — any falsy value hides, truthy shows,
|
|
// matching modern semantics.
|
|
template <uintptr_t ShowFn, uintptr_t HideFn>
|
|
int __fastcall Script_SetShown(void *L) {
|
|
const bool shown = Game::Lua::ToBoolean(L, 2) != 0;
|
|
Game::Lua::SetTop(L, 1); // (self) — Show/Hide take no args
|
|
CallScript(shown ? ShowFn : HideFn, L);
|
|
return 0;
|
|
}
|
|
|
|
// ---- SetResizeBounds (Frame) -----------------------------------------------
|
|
|
|
// `frame:SetResizeBounds(minWidth, minHeight [, maxWidth, maxHeight])` —
|
|
// modern rename of the vanilla SetMinResize/SetMaxResize pair.
|
|
int __fastcall Script_SetResizeBounds(void *L) {
|
|
if (!Game::Lua::IsNumber(L, 2) || !Game::Lua::IsNumber(L, 3)) {
|
|
Game::Lua::Error(L, "Usage: frame:SetResizeBounds(minWidth, "
|
|
"minHeight [, maxWidth, maxHeight])");
|
|
return 0;
|
|
}
|
|
const bool haveMax = Game::Lua::IsNumber(L, 4) && Game::Lua::IsNumber(L, 5);
|
|
const double maxW = haveMax ? Game::Lua::ToNumber(L, 4) : 0.0;
|
|
const double maxH = haveMax ? Game::Lua::ToNumber(L, 5) : 0.0;
|
|
Game::Lua::SetTop(L, 3); // (self, minW, minH)
|
|
CallScript(Offsets::FUN_SCRIPT_FRAME_SETMINRESIZE, L);
|
|
if (haveMax) {
|
|
Game::Lua::SetTop(L, 1); // (self)
|
|
Game::Lua::PushNumber(L, maxW);
|
|
Game::Lua::PushNumber(L, maxH);
|
|
CallScript(Offsets::FUN_SCRIPT_FRAME_SETMAXRESIZE, L);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// ---- HookScript (Frame) ----------------------------------------------------
|
|
|
|
// `frame:HookScript(scriptType, handler)` — vanilla has no HookScript at
|
|
// all. Chains onto any existing handler: the old handler runs first,
|
|
// vanilla-style (no args — it reads the `this`/`event`/`argN` globals like
|
|
// every 1.12 handler), then `handler` is invoked MODERN-style with
|
|
// positional arguments `(frame, event, arg1..arg9)` for OnEvent and
|
|
// `(frame, arg1..arg9)` otherwise — mirroring the compat convention
|
|
// modern addon ports expect (and ship themselves when they detect a
|
|
// legacy client). The chain is a C closure over (handler, old, isOnEvent).
|
|
|
|
// Pushes _G[name].
|
|
void PushGlobal(void *L, const char *name) {
|
|
Game::Lua::PushString(L, name);
|
|
Game::Lua::GetTable(L, Game::Lua::GLOBALS_INDEX);
|
|
}
|
|
|
|
// The chained handler the engine actually invokes (with no args, like any
|
|
// vanilla script handler). Upvalues: 1 = hook handler, 2 = old handler
|
|
// (or nil), 3 = isOnEvent (number). Errors inside lua_call propagate to
|
|
// the engine's own protected script dispatch.
|
|
int __fastcall HookThunk(void *L) {
|
|
if (Game::Lua::Type(L, Game::Lua::UpvalueIndex(2)) ==
|
|
Game::Lua::TYPE_FUNCTION) {
|
|
Game::Lua::PushValue(L, Game::Lua::UpvalueIndex(2));
|
|
Game::Lua::Call(L, 0, 0);
|
|
}
|
|
const bool isOnEvent =
|
|
Game::Lua::ToNumber(L, Game::Lua::UpvalueIndex(3)) != 0;
|
|
Game::Lua::PushValue(L, Game::Lua::UpvalueIndex(1));
|
|
PushGlobal(L, "this");
|
|
if (isOnEvent)
|
|
PushGlobal(L, "event");
|
|
static const char *const kArgs[] = {"arg1", "arg2", "arg3", "arg4",
|
|
"arg5", "arg6", "arg7", "arg8",
|
|
"arg9"};
|
|
for (const char *a : kArgs)
|
|
PushGlobal(L, a);
|
|
Game::Lua::Call(L, (isOnEvent ? 2 : 1) + 9, 0);
|
|
return 0;
|
|
}
|
|
|
|
int __fastcall Script_HookScript(void *L) {
|
|
if (!Game::Lua::IsString(L, 2) ||
|
|
Game::Lua::Type(L, 3) != Game::Lua::TYPE_FUNCTION) {
|
|
Game::Lua::Error(L, "Usage: frame:HookScript(\"type\", function)");
|
|
return 0;
|
|
}
|
|
const char *scriptName = Game::Lua::ToString(L, 2);
|
|
const bool isOnEvent =
|
|
scriptName != nullptr && std::strcmp(scriptName, "OnEvent") == 0;
|
|
|
|
Game::Lua::SetTop(L, 3); // (self, name, handler)
|
|
// GetScript reads self/name at 1/2 and pushes the current handler
|
|
// (or nil) — extra stack entries above are ignored by its reads.
|
|
CallScript(Offsets::FUN_SCRIPT_FRAME_GETSCRIPT, L); // (.., old)
|
|
// Closure upvalues, in upvalue order: handler, old, isOnEvent.
|
|
Game::Lua::PushValue(L, 3); // handler
|
|
Game::Lua::PushValue(L, 4); // old
|
|
Game::Lua::PushNumber(L, isOnEvent ? 1.0 : 0.0); // flag
|
|
Game::Lua::PushCClosure(L, &HookThunk, 3); // (self,name,handler,old,closure)
|
|
Game::Lua::Remove(L, 4); // drop old
|
|
Game::Lua::Remove(L, 3); // drop handler → (self,name,closure)
|
|
CallScript(Offsets::FUN_SCRIPT_FRAME_SETSCRIPT, L);
|
|
return 0;
|
|
}
|
|
|
|
// ---- IsEventRegistered (Frame) ---------------------------------------------
|
|
|
|
// `frame:IsEventRegistered("event")` — vanilla has RegisterEvent /
|
|
// UnregisterEvent but not the query. There's no engine Script_* to
|
|
// delegate to, so we replicate the membership check that lives inside
|
|
// Frame::RegisterEvent (FUN_00702140): find the event by name in the live
|
|
// event table, then walk that entry's subscriber chain for this frame.
|
|
// The frame pointer we compare against is the same CFrameScriptObject*
|
|
// the engine stores (Game::Lua::ResolveObject resolves self exactly as
|
|
// Script_RegisterEvent does).
|
|
using SStrCmpI_t = int(__stdcall *)(const char *a, const char *b, int n);
|
|
|
|
bool FrameHasEvent(void *frame, const char *eventName) {
|
|
const uint32_t count = *reinterpret_cast<const uint32_t *>(
|
|
static_cast<uintptr_t>(Offsets::VAR_EVENT_TABLE_COUNT));
|
|
auto *base = *reinterpret_cast<const uint8_t *const *>(
|
|
static_cast<uintptr_t>(Offsets::VAR_EVENT_TABLE_BASE_PTR));
|
|
if (count == 0 || base == nullptr)
|
|
return false;
|
|
|
|
auto sstrcmpi = reinterpret_cast<SStrCmpI_t>(
|
|
static_cast<uintptr_t>(Offsets::FUN_SSTR_CMP_I));
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
const uint8_t *entry = base + i * Offsets::EVENT_ENTRY_STRIDE;
|
|
const char *name = *reinterpret_cast<const char *const *>(
|
|
entry + Offsets::OFF_EVENT_ENTRY_NAME);
|
|
if (name == nullptr)
|
|
continue;
|
|
if (sstrcmpi(name, eventName, 0x7FFFFFFF) != 0)
|
|
continue;
|
|
// Names are unique, so this is the one entry — walk its chain.
|
|
uintptr_t node = *reinterpret_cast<const uintptr_t *>(
|
|
entry + Offsets::OFF_EVENT_ENTRY_HEAD);
|
|
while ((node & 1) == 0 && node != 0) {
|
|
if (*reinterpret_cast<void *const *>(
|
|
node + Offsets::OFF_EVENT_NODE_FRAME) == frame)
|
|
return true;
|
|
node = *reinterpret_cast<const uintptr_t *>(
|
|
node + Offsets::OFF_EVENT_NODE_NEXT);
|
|
}
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int __fastcall Script_IsEventRegistered(void *L) {
|
|
if (!Game::Lua::IsString(L, 2)) {
|
|
Game::Lua::Error(L, "Usage: frame:IsEventRegistered(\"event\")");
|
|
return 0;
|
|
}
|
|
void *frame = Game::Lua::ResolveObject(L, 1);
|
|
const char *eventName = Game::Lua::ToString(L, 2);
|
|
const bool registered =
|
|
frame != nullptr && eventName != nullptr && FrameHasEvent(frame, eventName);
|
|
Game::Lua::PushBool(L, registered);
|
|
return 1;
|
|
}
|
|
|
|
// ---- SetPoint one-arg form (Script_SetPoint co-hook) -----------------------
|
|
|
|
// Vanilla's Script_SetPoint accepts every modern form except the bare
|
|
// one-arg call (`region:SetPoint("RIGHT")`) — a fully-omitted arg 3 is
|
|
// LUA_TNONE, which fails its string/table/number/nil validation and
|
|
// raises the usage error. Normalize to the vanilla (point, x, y) form
|
|
// with zero offsets, which anchors relative to the parent at the same
|
|
// point — exactly the modern one-arg semantic. All other call shapes
|
|
// pass through untouched.
|
|
ScriptFn_t g_origSetPoint = nullptr;
|
|
|
|
int __fastcall SetPoint_h(void *L) {
|
|
if (Game::Lua::GetTop(L) == 2 && Game::Lua::IsString(L, 2)) {
|
|
Game::Lua::PushNumber(L, 0.0);
|
|
Game::Lua::PushNumber(L, 0.0);
|
|
}
|
|
return g_origSetPoint(L);
|
|
}
|
|
|
|
const Game::HookAutoRegister _setPointHook{
|
|
Offsets::FUN_SCRIPT_REGION_SETPOINT,
|
|
reinterpret_cast<void *>(&SetPoint_h),
|
|
reinterpret_cast<void **>(&g_origSetPoint)};
|
|
|
|
// ---- Registration ----------------------------------------------------------
|
|
|
|
const Game::Lua::FrameMethodEntry g_regionMethods[] = {
|
|
{"SetSize", &Script_SetSize},
|
|
{"GetSize", &Script_GetSize},
|
|
};
|
|
|
|
const Game::Lua::FrameMethodEntry g_frameMethods[] = {
|
|
{"SetShown", &Script_SetShown<Offsets::FUN_SCRIPT_FRAME_SHOW,
|
|
Offsets::FUN_SCRIPT_FRAME_HIDE>},
|
|
{"SetResizeBounds", &Script_SetResizeBounds},
|
|
{"HookScript", &Script_HookScript},
|
|
{"IsEventRegistered", &Script_IsEventRegistered},
|
|
};
|
|
|
|
const Game::Lua::FrameMethodEntry g_textureMethods[] = {
|
|
{"SetShown", &Script_SetShown<Offsets::FUN_SCRIPT_TEXTURE_SHOW,
|
|
Offsets::FUN_SCRIPT_TEXTURE_HIDE>},
|
|
};
|
|
|
|
const Game::Lua::FrameMethodEntry g_fontStringMethods[] = {
|
|
{"SetShown", &Script_SetShown<Offsets::FUN_SCRIPT_FONTSTRING_SHOW,
|
|
Offsets::FUN_SCRIPT_FONTSTRING_HIDE>},
|
|
};
|
|
|
|
void RegisterLuaFunctions() {
|
|
Game::Lua::RegisterFrameMethods(
|
|
reinterpret_cast<void *>(Offsets::VAR_REGION_METHOD_REGISTRY),
|
|
g_regionMethods,
|
|
static_cast<int>(sizeof(g_regionMethods) / sizeof(g_regionMethods[0])));
|
|
Game::Lua::RegisterFrameMethods(
|
|
reinterpret_cast<void *>(Offsets::VAR_FRAME_METHOD_REGISTRY),
|
|
g_frameMethods,
|
|
static_cast<int>(sizeof(g_frameMethods) / sizeof(g_frameMethods[0])));
|
|
Game::Lua::RegisterFrameMethods(
|
|
reinterpret_cast<void *>(Offsets::VAR_TEXTURE_METHOD_REGISTRY),
|
|
g_textureMethods,
|
|
static_cast<int>(sizeof(g_textureMethods) / sizeof(g_textureMethods[0])));
|
|
Game::Lua::RegisterFrameMethods(
|
|
reinterpret_cast<void *>(Offsets::VAR_FONTSTRING_METHOD_REGISTRY),
|
|
g_fontStringMethods,
|
|
static_cast<int>(sizeof(g_fontStringMethods) /
|
|
sizeof(g_fontStringMethods[0])));
|
|
}
|
|
|
|
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
|
|
|
|
} // namespace
|
|
|
|
} // namespace Frame::Modern
|