Files
ClassicAPI/src/frame/Modern.cpp
T
Brues b08d2fe813 refactor(frame): one wrapper for the layout sub-object's calls
Every region and frame carries a layout sub-object at +0x24 (4.3.4's
CLayoutFrame; OFF_REGION_ANCHOR, OFF_FRAME_LAYOUT_SUBOBJECT for a frame),
and three engine calls on it were wrapped by hand in sixteen files, each
with its own typedef and often its own AnchorOf / LayoutOf / FrameOf
helper:

- CLayoutFrame::GetRect (FUN_REGION_GET_RECT, 0x00768320), the
  {bottom, left, top, right} rect, false before layout;
- IsResizePending (FUN_REGION_LAYOUT_DIRTY, 0x00768310);
- CLayoutFrame::Resize (FUN_LAYOUT_RESIZE, 0x007680E0), 1 to lay out now,
  0 to mark for the layout pass.

frame/Layout.h now holds them, taking the region or frame itself, with
Of and OwnerOf for the sub-object and back, and GetResolvedRect for the
read the edge getters make (Script GetLeft 0x007A1A19: a pending resize
run now, then the rect). Frame::Subclass::FrameOfLayout is replaced by
OwnerOf.
2026-11-09 11:51:27 -06:00

583 lines
26 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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([ignoreRect])` — registered
// on the REGION base method registry, so they resolve on frames,
// buttons, textures, fontstrings — everything.
// - `region:GetWidth([ignoreRect])` / `region:GetHeight([ignoreRect])` —
// the engine's getters with the later argument. The engine answers the
// explicit size, and the rect's extent only when that size is 0; with
// ignoreRect a 0 stays 0. The engine leaves the layout scale in a rect
// extent, and vanilla addons are tuned to that, so it stays.
// - `region:GetUnscaledWidth([ignoreRect])` / `GetUnscaledHeight` /
// `GetUnscaledSize` — 4.3.4's getters, which divide a rect extent by the
// layout scale (see PushUnscaledExtent). The ported retail Lua calls
// these where retail calls GetWidth / GetHeight / GetSize.
// - `region:IsMouseOver([top, bottom, left, right])` — cursor-in-rect
// test with optional per-edge offsets, also on the REGION registry.
// - `region:GetRect()` — left, bottom, width, height, composed from the
// engine's edge getters and the unscaled extents. Also on the REGION
// registry.
// - `region:GetScaledRect()` — GetRect times the effective scale, the
// rect in UIParent-independent screen units.
// - `region:IsDragging()` — true while the region is the frame currently
// being moved/sized. Reads the engine's global drag target.
// - `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. 4.3.4's: chains after any existing handler through the
// same wrapper `hooksecurefunc` installs.
// - `texture:GetScale()` / `GetEffectiveScale()`, and the fontstring pair
// — Frame has the engine's own. A region carries no scale of its own;
// the frame's scale setter hands each region the frame's effective
// scale as its layout scale (OFF_REGION_EFFECTIVE_SCALE, benilla-re
// ui/visibility "Scale"), so the effective scale is that and the scale
// is 1.
// - `frame:GetEffectiveAlpha()` — the alpha the frame is drawn with:
// the byte at OFF_FRAME_ALPHA, which `Frame::Alpha` keeps at own ×
// the parent's drawn alpha. Frame-scoped (matches GetAlpha).
//
// 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 "HookSecureFunc.h"
#include "Offsets.h"
#include "frame/Layout.h"
#include "frame/ScriptBindings.h"
#include <cstdint>
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;
}
using LayoutExtent_t = float(__thiscall *)(void *layout);
// The explicit size of `self` in layout `slot`; 0 when none is set.
float ExplicitExtent(void *self, int slot) {
void *layout = Frame::Layout::Of(self);
auto *vtable = Game::Read<uintptr_t *>(layout, 0);
return reinterpret_cast<LayoutExtent_t>(vtable[slot / sizeof(uintptr_t)])(layout);
}
// Pushes the engine getter's answer for `self` (index 1), or 0 when
// ignoreRect is set and the explicit size in `slot` is 0 — the one case the
// engine answers from the rect instead.
void PushEngineExtent(void *L, bool ignoreRect, int slot, uintptr_t getter) {
if (ignoreRect) {
void *self = Game::Lua::ResolveRegion(L);
if (self == nullptr)
return;
if (ExplicitExtent(self, slot) == 0.0f) {
Game::Lua::PushNumber(L, 0.0);
return;
}
}
const int top = Game::Lua::GetTop(L);
Game::Lua::PushValue(L, 1);
CallScript(getter, L); // (.., self, value)
Game::Lua::Remove(L, top + 1);
}
// 4.3.4's Script_GetWidth / Script_GetHeight for `self` (index 1): the
// explicit size from layout `slot`, or when it is 0 and ignoreRect is not set,
// the rect's span over the layout scale; pushed in script units. 1.12's own
// getters (FUN_SCRIPT_REGION_GETWIDTH / _GETHEIGHT) leave out the division,
// so an anchored region under a UI scale reads scaled: a 160-high track at
// scale 0.91 answered 145.6 while its GetTop - GetBottom was 160. Their other
// steps, and the edge getters' division, are the engine's.
void PushUnscaledExtent(void *L, bool ignoreRect, int slot, bool vertical) {
void *self = Game::Lua::ResolveRegion(L);
if (self == nullptr)
return;
float extent = ExplicitExtent(self, slot);
if (extent == 0.0f && !ignoreRect) {
float rect[4] = {0.0f, 0.0f, 0.0f, 0.0f}; // {bottom, left, top, right}
if (Frame::Layout::GetResolvedRect(self, rect))
extent = (vertical ? rect[2] - rect[0] : rect[3] - rect[1]) /
Game::Read<float>(self, Offsets::OFF_REGION_EFFECTIVE_SCALE);
}
Game::Lua::PushNumber(L, Game::InternalToPixel(extent));
}
// `region:GetWidth([ignoreRect])` / `region:GetHeight([ignoreRect])`.
int __fastcall Script_GetWidth(void *L) {
const bool ignoreRect = Game::Lua::ToBoolean(L, 2) != 0;
Game::Lua::SetTop(L, 1);
PushEngineExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_WIDTH,
Offsets::FUN_SCRIPT_REGION_GETWIDTH);
return 1;
}
int __fastcall Script_GetHeight(void *L) {
const bool ignoreRect = Game::Lua::ToBoolean(L, 2) != 0;
Game::Lua::SetTop(L, 1);
PushEngineExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_HEIGHT,
Offsets::FUN_SCRIPT_REGION_GETHEIGHT);
return 1;
}
// `region:GetSize([ignoreRect])` → width, height.
int __fastcall Script_GetSize(void *L) {
const bool ignoreRect = Game::Lua::ToBoolean(L, 2) != 0;
Game::Lua::SetTop(L, 1); // (self)
PushEngineExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_WIDTH,
Offsets::FUN_SCRIPT_REGION_GETWIDTH);
PushEngineExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_HEIGHT,
Offsets::FUN_SCRIPT_REGION_GETHEIGHT);
return 2;
}
// `region:GetUnscaledWidth([ignoreRect])` / `GetUnscaledHeight` /
// `GetUnscaledSize`.
int __fastcall Script_GetUnscaledWidth(void *L) {
const bool ignoreRect = Game::Lua::ToBoolean(L, 2) != 0;
Game::Lua::SetTop(L, 1);
PushUnscaledExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_WIDTH, false);
return 1;
}
int __fastcall Script_GetUnscaledHeight(void *L) {
const bool ignoreRect = Game::Lua::ToBoolean(L, 2) != 0;
Game::Lua::SetTop(L, 1);
PushUnscaledExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_HEIGHT, true);
return 1;
}
int __fastcall Script_GetUnscaledSize(void *L) {
const bool ignoreRect = Game::Lua::ToBoolean(L, 2) != 0;
Game::Lua::SetTop(L, 1); // (self)
PushUnscaledExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_WIDTH, false);
PushUnscaledExtent(L, ignoreRect, Offsets::VTBL_LAYOUT_GET_HEIGHT, true);
return 2;
}
// ---- IsMouseOver (Region base — all region types) --------------------------
// `region:IsMouseOver([topOffset, bottomOffset, leftOffset, rightOffset])` —
// true when the cursor is within the region's rect, each edge optionally
// shifted by the matching offset (added to that edge, modern semantics:
// +top/-bottom/-left/+right enlarges). Vanilla addons have always done this
// in Lua as MouseIsOver(); we register it as the native Region method later
// clients expose.
//
// The math mirrors the engine exactly: GetLeft/Right/Top/Bottom (Region
// registry, all types) and GetCursorPosition both return values scaled by the
// same k = FUN_0041ad70()*DAT_007ffd68 factor, but the rect getters also
// divide by the region's effective scale — so dividing the cursor by that same
// effective scale (read from obj+0x7C, the field GetLeft/GetEffectiveScale
// themselves read) puts both in the region's logical space. A getter returns
// nil for an unpositioned region → we report false.
// Calls a Region getter that returns one number; false if it pushed nil/none.
bool CallRegionNumber(void *L, uintptr_t fn, double *out) {
Game::Lua::SetTop(L, 1); // (self)
CallScript(fn, L); // (self, value | nil)
if (!Game::Lua::IsNumber(L, 2))
return false;
*out = Game::Lua::ToNumber(L, 2);
return true;
}
// `IsMouseOver` and `IsDragging` go on the REGION base registry, which every
// region-family type inherits from — so a frame, texture or fontstring self
// reaches them legitimately, and a Region-only gate would reject the very
// callers the base registration exists to serve. Accepting the four ids
// states that set directly instead of resting on an assumption about which
// classes the engine's `IsA` treats as Regions. Both methods only read
// Region-base state, so the wider set is sound. A bad self raises the
// engine's own "this" errors, as 4.3.4's FrameScript_GetObjectThis does.
constexpr uintptr_t kRegionFamilyTypeIds[] = {
Offsets::VAR_REGION_LUA_TYPE_ID,
Offsets::VAR_FRAME_LUA_TYPE_ID,
Offsets::VAR_TEXTURE_LUA_TYPE_ID,
Offsets::VAR_FONTSTRING_LUA_TYPE_ID,
};
void *ResolveRegionFamily(void *L) {
return Game::Lua::ResolveTypedObjectAny(
L, 1, kRegionFamilyTypeIds,
static_cast<int>(sizeof(kRegionFamilyTypeIds) / sizeof(kRegionFamilyTypeIds[0])),
/*raiseError=*/true);
}
int __fastcall Script_IsMouseOver(void *L) {
// Offsets first — the stack gets reshaped by the delegated getters.
const double offTop = Game::Lua::IsNumber(L, 2) ? Game::Lua::ToNumber(L, 2) : 0.0;
const double offBottom = Game::Lua::IsNumber(L, 3) ? Game::Lua::ToNumber(L, 3) : 0.0;
const double offLeft = Game::Lua::IsNumber(L, 4) ? Game::Lua::ToNumber(L, 4) : 0.0;
const double offRight = Game::Lua::IsNumber(L, 5) ? Game::Lua::ToNumber(L, 5) : 0.0;
void *obj = ResolveRegionFamily(L);
const float effScale = Game::Read<float>(obj, Offsets::OFF_REGION_EFFECTIVE_SCALE);
double left, right, top, bottom;
if (effScale == 0.0f ||
!CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETLEFT, &left) ||
!CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETRIGHT, &right) ||
!CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETTOP, &top) ||
!CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETBOTTOM, &bottom)) {
Game::Lua::PushBool(L, false); // no resolved rect
return 1;
}
Game::Lua::SetTop(L, 0);
CallScript(Offsets::FUN_SCRIPT_GETCURSORPOSITION, L); // (x, y)
const double x = Game::Lua::ToNumber(L, 1) / effScale;
const double y = Game::Lua::ToNumber(L, 2) / effScale;
left += offLeft;
right += offRight;
top += offTop;
bottom += offBottom;
// Strict, as 4.3.4's CScriptRegion::IsMouseOver: an edge is outside.
const bool inside = x > left && x < right && y > bottom && y < top;
Game::Lua::PushBool(L, inside);
return 1;
}
// ---- GetRect (Region base — all region types) ------------------------------
// `region:GetRect()` → left, bottom, width, height. Composed from the
// engine's edge getters and the unscaled extents (the engine's GetWidth /
// GetHeight leave the layout scale in a rect-derived size).
// Matches retail: returns nothing for a region with no resolved rect —
// gated on GetLeft, which pushes nil when the position isn't resolved (a
// size-only region with no anchor has width/height but no rect).
int __fastcall Script_GetRect(void *L) {
double left, bottom, width, height;
if (!CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETLEFT, &left))
return 0;
CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETBOTTOM, &bottom);
CallRegionNumber(L, reinterpret_cast<uintptr_t>(&Script_GetUnscaledWidth), &width);
CallRegionNumber(L, reinterpret_cast<uintptr_t>(&Script_GetUnscaledHeight), &height);
Game::Lua::SetTop(L, 1);
Game::Lua::PushNumber(L, left);
Game::Lua::PushNumber(L, bottom);
Game::Lua::PushNumber(L, width);
Game::Lua::PushNumber(L, height);
return 4;
}
// `region:GetScaledRect()` → GetRect's four values times the effective scale.
int __fastcall Script_GetScaledRect(void *L) {
void *self = ResolveRegionFamily(L);
if (self == nullptr)
return 0;
const double scale = Game::Read<float>(self, Offsets::OFF_REGION_EFFECTIVE_SCALE);
double left, bottom, width, height;
if (!CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETLEFT, &left))
return 0;
CallRegionNumber(L, Offsets::FUN_SCRIPT_REGION_GETBOTTOM, &bottom);
CallRegionNumber(L, reinterpret_cast<uintptr_t>(&Script_GetUnscaledWidth), &width);
CallRegionNumber(L, reinterpret_cast<uintptr_t>(&Script_GetUnscaledHeight), &height);
Game::Lua::SetTop(L, 1);
Game::Lua::PushNumber(L, left * scale);
Game::Lua::PushNumber(L, bottom * scale);
Game::Lua::PushNumber(L, width * scale);
Game::Lua::PushNumber(L, height * scale);
return 4;
}
// ---- IsDragging (Region base — all region types) ---------------------------
// `region:IsDragging()` — true while this region is the frame currently
// being moved/sized (StartMoving/StartSizing active, before
// StopMovingOrSizing). The engine tracks a single drag target in the global
// UI context at +0xCFC; StopMovingOrSizing itself gates on
// `context+0xCFC == self`, which is exactly this predicate. Reading the
// global context (rather than self+0xA0) keeps it correct for non-frame
// regions — a texture/fontstring self can never match the frame stored
// there, so it reports false, as it should.
int __fastcall Script_IsDragging(void *L) {
void *self = ResolveRegionFamily(L);
void *ctx = Game::Read<void *>(static_cast<uintptr_t>(Offsets::VAR_UI_CONTEXT_PTR));
void *dragTarget =
ctx != nullptr
? Game::Read<void *>(ctx, Offsets::OFF_UI_CONTEXT_DRAG_TARGET)
: nullptr;
Game::Lua::PushBool(L, self == dragTarget);
return 1;
}
// ---- 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;
}
// ---- GetEffectiveAlpha (Frame) ---------------------------------------------
// `frame:GetEffectiveAlpha()` — the alpha the frame is drawn with, the byte
// its regions and model read (fading UIParent fades all children). With
// `Frame::Alpha` that byte is own × the parent's drawn alpha, truncated per
// level, so this is 3.3.5's Script_GetEffectiveAlpha (0x0049F900):
// `(m_alpha * m_parentAlpha / 255) / 255.0`. The engine's GetAlpha runs first
// for its `self` type check, which errors on a non-frame.
int __fastcall Script_GetEffectiveAlpha(void *L) {
Game::Lua::SetTop(L, 1); // (self)
CallScript(Offsets::FUN_SCRIPT_FRAME_GETALPHA, L); // (self, own)
const void *frame = Game::Lua::ResolveObject(L, 1);
Game::Lua::SetTop(L, 0);
if (frame == nullptr)
return 0;
Game::Lua::PushNumber(L, Game::Read<uint8_t>(frame, Offsets::OFF_FRAME_ALPHA) / 255.0);
return 1;
}
// ---- 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("type", function)` — 4.3.4's
// FrameScript_Object::HookScript, less taint:
// * no script set: the hook becomes the script itself, so GetScript
// returns it;
// * otherwise the script becomes the hooksecurefunc wrapper over (old,
// hook) — 4.3.4 installs the same FrameScript_HookWrapper for both. The
// old handler runs first with the script's arguments, then the hook with
// the same arguments under the engine's script error handler.
// The arguments are whatever the dispatch passes. Frame::ScriptArgs looks
// inside the wrapper and passes positional args when either side declares
// parameters; the engine sets the this/event/argN globals for vanilla
// handlers either way. Script lookup, the unknown-script error and storage
// stay the engine's own GetScript / SetScript, whose texts match 4.3.4's.
//
// The retail third argument, `bindingType`, hooks one of the script's
// intrinsic bindings instead (Frame::ScriptBindings); without it the hook
// goes on the extrinsic binding, which GetScript / SetScript read and write.
int __fastcall Script_HookScript(void *L) {
void *frame = Game::Lua::ResolveFrame(L);
if (frame == nullptr)
return 0;
if (!Game::Lua::IsString(L, 2) ||
Game::Lua::Type(L, 3) != Game::Lua::TYPE_FUNCTION) {
Game::Lua::Error(L, "Usage: %s:HookScript(\"type\", function)",
Game::Lua::ObjectDisplayName(frame));
return 0;
}
if (Game::Lua::IsNumber(L, 4)) {
const int binding = static_cast<int>(Game::Lua::ToNumber(L, 4));
if (binding == Frame::ScriptBindings::INTRINSIC_PRECALL ||
binding == Frame::ScriptBindings::INTRINSIC_POSTCALL) {
if (!Frame::ScriptBindings::HookIntrinsic(
L, frame, static_cast<Frame::ScriptBindings::Binding>(binding)))
Game::Lua::Error(L, "%s doesn't have a \"%s\" script",
Game::Lua::ObjectDisplayName(frame), Game::Lua::ToString(L, 2));
return 0;
}
}
Game::Lua::SetTop(L, 3); // (self, type, hook)
CallScript(Offsets::FUN_SCRIPT_FRAME_GETSCRIPT, L); // (.., old or nil)
if (Game::Lua::Type(L, 4) == Game::Lua::TYPE_FUNCTION) {
Game::Lua::Insert(L, 3); // (self, type, old, hook)
HookSecureFunc::PushWrapper(L); // (self, type, wrapper)
} else {
Game::Lua::SetTop(L, 3); // (self, type, hook)
}
CallScript(Offsets::FUN_SCRIPT_FRAME_SETSCRIPT, L);
return 0;
}
// ---- 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)};
// ---- Texture / FontString scale -----------------------------------------------
int __fastcall Script_GetRegionScale(void *L) {
if (ResolveRegionFamily(L) == nullptr)
return 0;
Game::Lua::PushNumber(L, 1.0);
return 1;
}
int __fastcall Script_GetRegionEffectiveScale(void *L) {
void *self = ResolveRegionFamily(L);
if (self == nullptr)
return 0;
Game::Lua::PushNumber(L, Game::Read<float>(self, Offsets::OFF_REGION_EFFECTIVE_SCALE));
return 1;
}
// ---- Button normal-state font ------------------------------------------------
// `button:SetNormalFontObject(font)` / `button:GetNormalFontObject()` - the
// names 3.0 gave vanilla's SetTextFontObject / GetTextFontObject (the font of
// the button's normal state; the Highlight / Disabled pair kept their names).
// Pure delegation, so arguments, validation and errors are the engine's own.
int __fastcall Script_SetNormalFontObject(void *L) {
return CallScript(Offsets::FUN_SCRIPT_BUTTON_SET_TEXT_FONT_OBJECT, L);
}
int __fastcall Script_GetNormalFontObject(void *L) {
return CallScript(Offsets::FUN_SCRIPT_BUTTON_GET_TEXT_FONT_OBJECT, L);
}
// ---- Registration ----------------------------------------------------------
const Game::Lua::FrameMethodEntry g_regionMethods[] = {
{"SetSize", &Script_SetSize},
{"GetSize", &Script_GetSize},
{"GetWidth", &Script_GetWidth},
{"GetHeight", &Script_GetHeight},
{"GetUnscaledSize", &Script_GetUnscaledSize},
{"GetUnscaledWidth", &Script_GetUnscaledWidth},
{"GetUnscaledHeight", &Script_GetUnscaledHeight},
{"IsMouseOver", &Script_IsMouseOver},
{"GetRect", &Script_GetRect},
{"GetScaledRect", &Script_GetScaledRect},
{"IsDragging", &Script_IsDragging},
};
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},
{"GetEffectiveAlpha", &Script_GetEffectiveAlpha},
};
const Game::Lua::FrameMethodEntry g_buttonMethods[] = {
{"SetNormalFontObject", &Script_SetNormalFontObject},
{"GetNormalFontObject", &Script_GetNormalFontObject},
};
const Game::Lua::FrameMethodEntry g_textureMethods[] = {
{"SetShown", &Script_SetShown<Offsets::FUN_SCRIPT_TEXTURE_SHOW,
Offsets::FUN_SCRIPT_TEXTURE_HIDE>},
{"GetScale", &Script_GetRegionScale},
{"GetEffectiveScale", &Script_GetRegionEffectiveScale},
};
const Game::Lua::FrameMethodEntry g_fontStringMethods[] = {
{"SetShown", &Script_SetShown<Offsets::FUN_SCRIPT_FONTSTRING_SHOW,
Offsets::FUN_SCRIPT_FONTSTRING_HIDE>},
{"GetScale", &Script_GetRegionScale},
{"GetEffectiveScale", &Script_GetRegionEffectiveScale},
};
void RegisterLuaFunctions() {
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_REGION_METHOD_REGISTRY), g_regionMethods);
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_FRAME_METHOD_REGISTRY), g_frameMethods);
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_BUTTON_METHOD_REGISTRY), g_buttonMethods);
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_TEXTURE_METHOD_REGISTRY), g_textureMethods);
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_FONTSTRING_METHOD_REGISTRY), g_fontStringMethods);
}
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
} // namespace Frame::Modern