From 61db68dd7fb24a8ae0e7a6a1a67474b6f66b2e19 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Fri, 11 Sep 2026 14:12:40 -0500 Subject: [PATCH] fix(lua): gate frame-method `self` with the engine's own type check Every engine frame method validates its `self` in three steps before touching it: lua_type == table, resolve to the CFrameScriptObject, then the IsA(typeId) vmethod at vtable+0x10. We did the first two and skipped the third, so ResolveObject handed back a pointer for ANY frame and the method cast it to whatever type it expected. That was reachable from plain Lua, not just through a mirror or a hook. The per-type method dispatcher is an __index handler that rawgeti's the method closure out of the Lua registry, so frame methods are ordinary first-class values, and GameTooltip.SetSpellByID(ShapeshiftButton1, 133) reached the tooltip builder with a Button, writing tooltip fields through Button memory. The engine refuses that same call with "Wrong object type for member function". Game::Lua::ResolveTypedObject runs the engine's prologue and raises the engine's own three error strings, read from .data rather than reinvented. FrameScriptTypeId mirrors the lazy id assignment, so a check is valid before any stock method of the type has run. Seven per-type wrappers stop a call site pairing a method with the wrong class's id, and ResolveTypedObjectAny covers a method that more than one type reaches. 45 call sites across 19 files now gate self. model/DisplayInfo.cpp had already worked this out for Model alone and loses its private copy. Type ids are per CLASS, one shared by all of that class's methods. Each was read from the prologue of a method that can only belong to its class: a base-class method registered into several tables carries the BASE id, so sampling GetObjectType or GetDrawLayer would have produced a gate that accepts unrelated types. Frame comes from Script_RegisterEvent and Texture from Script_SetDesaturated, both cross-checked against the first entry of their table. Deliberately left ungated: the binding `owner` in bindings/Api.cpp and the three co-hook sites in texture/Atlas.cpp. Each only uses the pointer as an identity key and never casts it, so a gate would tighten the contract for no safety gain -- and raising inside a co-hook would fire before the original ran. Verified in-game: the Button-as-tooltip call now raises the engine's error; a Button still reaches a Frame-registry method (button:SetAttribute round-trips), so IsA does accept derived types; and fontstring:GetRotation() still answers -- that reader is registered on both the Texture and FontString registries and needs the id pair rather than the Texture id alone. --- src/Game.cpp | 75 ++++++++++++++++++++++++++++++++++++ src/Game.h | 43 +++++++++++++++++++++ src/Offsets.h | 34 ++++++++++++++++ src/editbox/Methods.cpp | 18 ++++----- src/equipmentset/Tooltip.cpp | 2 +- src/fontstring/Metrics.cpp | 4 +- src/frame/Attributes.cpp | 4 +- src/frame/Modern.cpp | 26 ++++++++++++- src/frame/UnitEvent.cpp | 4 +- src/gameobject/Tooltip.cpp | 4 +- src/item/Tooltip.cpp | 4 +- src/model/DisplayInfo.cpp | 35 ++++------------- src/spell/Tooltip.cpp | 8 ++-- src/texture/Atlas.cpp | 6 +-- src/texture/Desaturation.cpp | 2 +- src/texture/Mask.cpp | 14 +++---- src/texture/Transform.cpp | 25 ++++++++---- src/tooltip/CompareItem.cpp | 9 +++-- src/tooltip/Owner.cpp | 2 +- src/tooltip/SetAction.cpp | 2 +- src/totem/Tracker.cpp | 2 +- src/unit/Tooltip.cpp | 4 +- 22 files changed, 247 insertions(+), 80 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 32642647..498bb6c0 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -99,6 +99,81 @@ void *ResolveObject(void *L, int idx) { return result; } +namespace { +using FrameScriptIsA_t = char(__thiscall *)(void *self, int typeId); + +const char *EngineString(uintptr_t address) { + return reinterpret_cast(address); +} + +// Shared body: the engine's prologue with N accepted type ids. +void *ResolveChecked(void *L, int idx, const uintptr_t *typeIdVars, int count, + bool raiseError) { + if (Type(L, idx) != TYPE_TABLE) { + if (raiseError) + Error(L, EngineString(Offsets::STR_FS_THIS_NON_TABLE)); + return nullptr; + } + void *obj = ResolveObject(L, idx); + if (obj == nullptr) { + if (raiseError) + Error(L, EngineString(Offsets::STR_FS_THIS_NON_OBJECT)); + return nullptr; + } + auto **vtable = *reinterpret_cast(obj); + auto IsA = reinterpret_cast( + vtable[Offsets::OFF_VMT_FRAMESCRIPT_ISA / sizeof(void *)]); + for (int i = 0; i < count; ++i) { + if (IsA(obj, FrameScriptTypeId(typeIdVars[i]))) + return obj; + } + if (raiseError) + Error(L, EngineString(Offsets::STR_FS_WRONG_OBJECT_TYPE)); + return nullptr; +} +} // namespace + +int FrameScriptTypeId(uintptr_t typeIdVar) { + auto &typeId = Game::Ref(typeIdVar); + if (typeId == 0) { + auto &counter = Game::Ref(Offsets::VAR_FRAMESCRIPT_TYPE_ID_COUNTER); + counter += 1; + typeId = counter; + } + return typeId; +} + +void *ResolveTypedObject(void *L, int idx, uintptr_t typeIdVar, bool raiseError) { + return ResolveChecked(L, idx, &typeIdVar, 1, raiseError); +} + +void *ResolveTypedObjectAny(void *L, int idx, const uintptr_t *typeIdVars, + int count, bool raiseError) { + return ResolveChecked(L, idx, typeIdVars, count, raiseError); +} + +void *ResolveTooltip(void *L, int idx, bool raiseError) { + return ResolveTypedObject(L, idx, Offsets::VAR_GAMETOOLTIP_LUA_TYPE_ID, raiseError); +} +void *ResolveFrame(void *L, int idx, bool raiseError) { + return ResolveTypedObject(L, idx, Offsets::VAR_FRAME_LUA_TYPE_ID, raiseError); +} +void *ResolveRegion(void *L, int idx, bool raiseError) { + return ResolveTypedObject(L, idx, Offsets::VAR_REGION_LUA_TYPE_ID, raiseError); +} +void *ResolveTexture(void *L, int idx, bool raiseError) { + return ResolveTypedObject(L, idx, Offsets::VAR_TEXTURE_LUA_TYPE_ID, raiseError); +} +void *ResolveFontString(void *L, int idx, bool raiseError) { + return ResolveTypedObject(L, idx, Offsets::VAR_FONTSTRING_LUA_TYPE_ID, raiseError); +} +void *ResolveEditBox(void *L, int idx, bool raiseError) { + return ResolveTypedObject(L, idx, Offsets::VAR_EDITBOX_LUA_TYPE_ID, raiseError); +} +void *ResolveModel(void *L, int idx, bool raiseError) { + return ResolveTypedObject(L, idx, Offsets::VAR_MODEL_LUA_TYPE_ID, raiseError); +} + // Looks up `_G[name]`. If absent, creates a fresh table and binds it. // Leaves the resulting table on top of the stack. namespace { diff --git a/src/Game.h b/src/Game.h index d57a69e8..a396adb3 100644 --- a/src/Game.h +++ b/src/Game.h @@ -216,6 +216,49 @@ void *State(); // the `self` slot of any `frame:method(...)` invocation. void *ResolveObject(void *L, int idx); +// Same resolve, then the type gate every engine frame method applies to its +// `self` before touching it: `lua_type == table`, resolve to the object, then +// the `IsA(typeId)` vmethod at vtable+0x10. `ResolveObject` alone is not +// enough for a method that casts the result — it hands back a +// `CFrameScriptObject *` for ANY frame, so `GameTooltip.SetSpellByID(button)` +// would reach the tooltip builder with a Button. Frame methods are ordinary +// first-class Lua values (the per-type dispatcher is an `__index` handler +// that returns the method closure), so that call is reachable from plain Lua +// and the gate is not optional. +// +// `typeIdVar` is the class's `VAR_*_LUA_TYPE_ID`, which must match the +// registry the method was registered on. The id is assigned lazily by +// whichever method of the class runs first, so these mirror that assignment +// and work even before any stock method of the type has been called. +// +// Raises the engine's own three errors and returns null. Pass +// `raiseError = false` for an optional object ARGUMENT, where the caller +// wants a silent null rather than a "this" error naming the wrong slot. +// Returns null either way, so callers always check. +void *ResolveTypedObject(void *L, int idx, uintptr_t typeIdVar, + bool raiseError = true); + +// For a method registered on SEVERAL registries, where any of those types is +// a legitimate `self`. Accepts the object if `IsA` passes for any of the +// listed ids — which is exactly what being in all of those registries means, +// and avoids having to pin down the shared base class. +void *ResolveTypedObjectAny(void *L, int idx, const uintptr_t *typeIdVars, + int count, bool raiseError = true); + +// The class's live type id, assigning it if the engine has not yet. Needed +// directly only when calling a type-specific vtable slot. +int FrameScriptTypeId(uintptr_t typeIdVar); + +// Per-type sugar over `ResolveTypedObject`, so a call site cannot pair a +// method with the wrong class's id. `idx` defaults to the `self` slot. +void *ResolveTooltip(void *L, int idx = 1, bool raiseError = true); +void *ResolveFrame(void *L, int idx = 1, bool raiseError = true); +void *ResolveRegion(void *L, int idx = 1, bool raiseError = true); +void *ResolveTexture(void *L, int idx = 1, bool raiseError = true); +void *ResolveFontString(void *L, int idx = 1, bool raiseError = true); +void *ResolveEditBox(void *L, int idx = 1, bool raiseError = true); +void *ResolveModel(void *L, int idx = 1, bool raiseError = true); + // Every name these registrars bind is ALSO bound under `_G.ClassicAPI` — // `ClassicAPI.GetSpellInfo`, `ClassicAPI.C_Item.IsBound` — as an escape // hatch for names something else replaces later, and by value so diff --git a/src/Offsets.h b/src/Offsets.h index 7ada4c28..c50f7eb6 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -2196,6 +2196,40 @@ enum Offsets { // every frame type. VAR_MODEL_LUA_TYPE_ID = 0x00CF0C5C, VAR_FRAMESCRIPT_TYPE_ID_COUNTER = 0x00CEEF6C, + + // Frame-script type ids for the other classes we register methods on — + // same lazy-assign mechanism as VAR_MODEL_LUA_TYPE_ID, one id per CLASS + // shared by all of its methods (VAR_GAMETOOLTIP_LUA_TYPE_ID is read or + // written by ~45 functions in the GameTooltip method block). + // + // Each was read from the prologue of a method that can only belong to + // that class. That matters: a BASE-class method registered into several + // tables carries the base's id, so sampling `GetObjectType` or + // `GetDrawLayer` risks a gate that accepts unrelated types. Frame is from + // `Script_RegisterEvent` (FUN_00774A40) and Texture from + // `Script_SetDesaturated` (FUN_0079C1E0), both cross-checked against the + // first entry of their table; GameTooltip from + // `Script_GameTooltip_SetSpell` (FUN_00532D10). Pairs 1:1 with the + // VAR_*_METHOD_REGISTRY values above. + VAR_GAMETOOLTIP_LUA_TYPE_ID = 0x00C0D414, + VAR_FRAME_LUA_TYPE_ID = 0x00CF0C10, + VAR_REGION_LUA_TYPE_ID = 0x00CF0C3C, + VAR_TEXTURE_LUA_TYPE_ID = 0x00CF4CDC, + VAR_FONTSTRING_LUA_TYPE_ID = 0x00CF2CCC, + VAR_EDITBOX_LUA_TYPE_ID = 0x00CF4DB4, + + // CFrameScriptObject vtable slot 4 — the `IsA(typeId)` predicate every + // frame class implements, and the gate every engine method applies to its + // `self` before touching it. `char __thiscall(void *self, int typeId)`. + OFF_VMT_FRAMESCRIPT_ISA = 0x10, + + // The three errors an engine frame method raises for a bad `self`, as + // .data string literals. None contains a `%`, so they are safe to hand + // straight to the variadic `lua_error`, and reading the engine's own + // strings keeps our diagnostics identical to the stock ones. + STR_FS_THIS_NON_TABLE = 0x00847EF8, + STR_FS_THIS_NON_OBJECT = 0x00847EC0, + STR_FS_WRONG_OBJECT_TYPE = 0x00847E98, // FUN_0076cfe0(model /*ecx*/, int replaceableType, const char *path) — // loads `path` as a texture and binds it to the model's replaceable-texture // slot `replaceableType`. Worker behind `Model:ReplaceIconTexture` (which diff --git a/src/editbox/Methods.cpp b/src/editbox/Methods.cpp index b19064f7..cbc91846 100644 --- a/src/editbox/Methods.cpp +++ b/src/editbox/Methods.cpp @@ -64,7 +64,7 @@ uint32_t ColorByte(void *L, int idx, double dflt) { } int __fastcall Script_SetCursorPosition(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr || !Game::Lua::IsNumber(L, 2)) { Game::Lua::Error(L, "Usage: EditBox:SetCursorPosition(position)"); return 0; @@ -82,7 +82,7 @@ int __fastcall Script_SetCursorPosition(void *L) { } int __fastcall Script_GetCursorPosition(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr) { Game::Lua::Error(L, "Usage: EditBox:GetCursorPosition()"); return 0; @@ -96,7 +96,7 @@ int __fastcall Script_GetCursorPosition(void *L) { // vs GetCursorPosition's byte offset. Equal for ASCII; differs once the text // holds multibyte characters. int __fastcall Script_GetUTF8CursorPosition(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr) { Game::Lua::Error(L, "Usage: EditBox:GetUTF8CursorPosition()"); return 0; @@ -113,7 +113,7 @@ int __fastcall Script_GetUTF8CursorPosition(void *L) { // helper does; vanilla can only clear a selection via HighlightText(0, 0), // which yanks it to offset 0 instead of leaving the caret in place. int __fastcall Script_ClearHighlightText(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr) { Game::Lua::Error(L, "Usage: EditBox:ClearHighlightText()"); return 0; @@ -128,7 +128,7 @@ int __fastcall Script_ClearHighlightText(void *L) { // focused box in one global (written by SetFocus / cleared by ClearFocus), so // focus is just pointer identity against it. int __fastcall Script_HasFocus(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr) { Game::Lua::PushBool(L, false); return 1; @@ -140,7 +140,7 @@ int __fastcall Script_HasFocus(void *L) { // True when the box holds any text (its byte length is non-zero). int __fastcall Script_HasText(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr) { Game::Lua::PushBool(L, false); return 1; @@ -156,7 +156,7 @@ int __fastcall Script_HasText(void *L) { // it on all three via the engine's own region color setter. Colors are 0..1; // alpha defaults to 1. Takes effect on the next selection paint. int __fastcall Script_SetHighlightColor(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr || !Game::Lua::IsNumber(L, 2) || !Game::Lua::IsNumber(L, 3) || !Game::Lua::IsNumber(L, 4)) { Game::Lua::Error(L, "Usage: EditBox:SetHighlightColor(r, g, b [, a])"); @@ -181,7 +181,7 @@ int __fastcall Script_SetHighlightColor(void *L) { // GetHighlightColor() -> r, g, b, a (each 0..1). Reads the color off the first // highlight region; a region with no explicit color reads back white (opaque). int __fastcall Script_GetHighlightColor(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr) { Game::Lua::Error(L, "Usage: EditBox:GetHighlightColor()"); return 0; @@ -205,7 +205,7 @@ int __fastcall Script_GetHighlightColor(void *L) { // drops every entry and resets the write position. No-op when history was // never enabled (max 0). int __fastcall Script_ClearHistory(void *L) { - void *eb = Game::Lua::ResolveObject(L, 1); + void *eb = Game::Lua::ResolveEditBox(L); if (eb == nullptr) { Game::Lua::Error(L, "Usage: EditBox:ClearHistory()"); return 0; diff --git a/src/equipmentset/Tooltip.cpp b/src/equipmentset/Tooltip.cpp index 397a11cc..db538d32 100644 --- a/src/equipmentset/Tooltip.cpp +++ b/src/equipmentset/Tooltip.cpp @@ -160,7 +160,7 @@ int __fastcall Script_GameTooltipSetEquipmentSet(void *L) { if (s == nullptr) return 0; - void *self = Game::Lua::ResolveObject(L, 1); + void *self = Game::Lua::ResolveTooltip(L); if (self == nullptr) return 0; diff --git a/src/fontstring/Metrics.cpp b/src/fontstring/Metrics.cpp index 0dafd074..46ee1e8d 100644 --- a/src/fontstring/Metrics.cpp +++ b/src/fontstring/Metrics.cpp @@ -151,7 +151,7 @@ float WrapBudget(void *fs) { void *ResolveSelf(void *L, const char *usage) { void *fs = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - fs = Game::Lua::ResolveObject(L, 1); + fs = Game::Lua::ResolveFontString(L, 1, /*raiseError=*/false); if (fs == nullptr) Game::Lua::Error(L, "%s", usage); return fs; @@ -362,7 +362,7 @@ int __fastcall Script_GetMaxLines(void *L) { int __fastcall Script_SetFormattedText(void *L) { void *fs = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - fs = Game::Lua::ResolveObject(L, 1); + fs = Game::Lua::ResolveFontString(L, 1, /*raiseError=*/false); if (fs == nullptr || !Game::Lua::IsString(L, 2)) { Game::Lua::Error(L, "Usage: fontstring:SetFormattedText(\"format\"[, ...])"); return 0; diff --git a/src/frame/Attributes.cpp b/src/frame/Attributes.cpp index 99ccdf4a..1aadc60a 100644 --- a/src/frame/Attributes.cpp +++ b/src/frame/Attributes.cpp @@ -990,7 +990,7 @@ int DoSet(void *L, bool fireHandler) { // (self, name, value) // (the key the mouse-focus poll uses) and enable its mouse so it can be // hovered. if (std::strcmp(lname, "unit") == 0) { - void *obj = Game::Lua::ResolveObject(L, 1); + void *obj = Game::Lua::ResolveFrame(L); if (obj != nullptr) { if (isString) { const char *tok = Game::Lua::ToString(L, 3); @@ -1018,7 +1018,7 @@ int DoSet(void *L, bool fireHandler) { // (self, name, value) } if (fireHandler) - FireAttributeChanged(L, Game::Lua::ResolveObject(L, 1), lname); + FireAttributeChanged(L, Game::Lua::ResolveFrame(L), lname); return 0; } diff --git a/src/frame/Modern.cpp b/src/frame/Modern.cpp index a38d6a9e..b00a19b7 100644 --- a/src/frame/Modern.cpp +++ b/src/frame/Modern.cpp @@ -119,6 +119,28 @@ bool CallRegionNumber(void *L, uintptr_t fn, double *out) { 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. Non-raising: each reports +// false for an unusable self, the contract they already had. +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(sizeof(kRegionFamilyTypeIds) / sizeof(kRegionFamilyTypeIds[0])), + /*raiseError=*/false); +} + 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; @@ -126,7 +148,7 @@ int __fastcall Script_IsMouseOver(void *L) { 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 = Game::Lua::ResolveObject(L, 1); + void *obj = ResolveRegionFamily(L); if (obj == nullptr) { Game::Lua::PushBool(L, false); return 1; @@ -191,7 +213,7 @@ int __fastcall Script_GetRect(void *L) { // 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 = Game::Lua::ResolveObject(L, 1); + void *self = ResolveRegionFamily(L); void *ctx = Game::Read(static_cast(Offsets::VAR_UI_CONTEXT_PTR)); void *dragTarget = ctx != nullptr diff --git a/src/frame/UnitEvent.cpp b/src/frame/UnitEvent.cpp index bd819a8d..a0e16549 100644 --- a/src/frame/UnitEvent.cpp +++ b/src/frame/UnitEvent.cpp @@ -175,7 +175,7 @@ int __fastcall Script_RegisterUnitEvent(void *L) { Game::Lua::Error(L, "Usage: frame:RegisterUnitEvent(\"event\" [, \"unit1\", ...])"); return 0; } - void *frame = Game::Lua::ResolveObject(L, 1); + void *frame = Game::Lua::ResolveFrame(L); if (frame == nullptr) { // Not a frame — hand it to the engine so it raises its own error, so // the message stays the engine's rather than a copy of it. @@ -231,7 +231,7 @@ int __fastcall Script_IsEventRegistered(void *L) { Game::Lua::Error(L, "Usage: frame:IsEventRegistered(\"event\")"); return 0; } - void *frame = Game::Lua::ResolveObject(L, 1); + void *frame = Game::Lua::ResolveFrame(L); const char *eventName = Game::Lua::ToString(L, 2); const uint8_t *entry = (frame != nullptr) ? FindEventEntry(eventName) : nullptr; const bool registered = entry != nullptr && ChainHas(entry, frame); diff --git a/src/gameobject/Tooltip.cpp b/src/gameobject/Tooltip.cpp index a99e4cab..7726b25c 100644 --- a/src/gameobject/Tooltip.cpp +++ b/src/gameobject/Tooltip.cpp @@ -58,7 +58,7 @@ static int __fastcall Script_GameTooltipGetGameObject(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:GetGameObject()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) return 0; @@ -105,7 +105,7 @@ static int __fastcall Script_GameTooltipHasGameObject(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:HasGameObject()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) { Game::Lua::PushBool(L, false); return 1; diff --git a/src/item/Tooltip.cpp b/src/item/Tooltip.cpp index f5613a38..fa8a05d5 100644 --- a/src/item/Tooltip.cpp +++ b/src/item/Tooltip.cpp @@ -188,7 +188,7 @@ static int __fastcall Script_GameTooltipGetItem(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:GetItem()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) return 0; auto *base = static_cast(tooltipObj); @@ -274,7 +274,7 @@ static int __fastcall Script_GameTooltipHasItem(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:HasItem()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) { Game::Lua::PushBool(L, 0); return 1; diff --git a/src/model/DisplayInfo.cpp b/src/model/DisplayInfo.cpp index 9b42d7ef..dca9e371 100644 --- a/src/model/DisplayInfo.cpp +++ b/src/model/DisplayInfo.cpp @@ -49,13 +49,10 @@ namespace Model::DisplayInfo { namespace { -// The CFrameScriptObject "is-a" vmethod, at vtable +0x10 for every frame type, -// and the Model model-load worker at vtable +0x94 (the one Script_SetModel -// invokes after resolving self). Both single-use here, so kept local. -constexpr int kVmtIsA = 0x10; +// The Model model-load worker at vtable +0x94 — the one Script_SetModel +// invokes after resolving self. Single-use here, so kept local. constexpr int kVmtLoadModel = 0x94; -using IsA_t = char(__thiscall *)(void *self, int typeId); using LoadModel_t = void(__thiscall *)(void *self, const char *path); using SetReplaceableTexture_t = void(__thiscall *)(void *model, int type, const char *path); @@ -65,30 +62,12 @@ Fn Vmethod(void *obj, int byteOffset) { return *reinterpret_cast(vtbl + byteOffset); } -// The Model frame-script type id. The engine assigns it lazily on the first -// Model method call; mirror that assignment (FUN_0076d950's prologue) so a -// type-check is valid even if no stock Model method has run yet. -int ModelTypeId() { - auto &typeId = Game::Ref(Offsets::VAR_MODEL_LUA_TYPE_ID); - if (typeId == 0) { - auto &counter = Game::Ref(Offsets::VAR_FRAMESCRIPT_TYPE_ID_COUNTER); - counter += 1; - typeId = counter; - } - return typeId; -} - -// Resolve arg1 to a Model object, or nullptr if it is not a Model. Mirrors the -// resolve-then-typecheck prologue every Model method uses: generic object -// resolve, then the IsA vmethod. The type-check matters here because we call a -// Model vtable slot directly — a non-Model region would dispatch a wrong method. +// Resolve arg1 to a Model object, or nullptr if it is not a Model. The +// type-check matters especially here because we call a Model vtable slot +// directly — a non-Model region would dispatch a wrong method. Non-raising, +// which is the contract the callers below already had. void *ResolveModel(void *L) { - void *obj = Game::Lua::ResolveObject(L, 1); - if (obj == nullptr) - return nullptr; - if (!Vmethod(obj, kVmtIsA)(obj, ModelTypeId())) - return nullptr; - return obj; + return Game::Lua::ResolveModel(L, 1, /*raiseError=*/false); } // Apply a creature display's TextureVariation skins to the just-loaded model. diff --git a/src/spell/Tooltip.cpp b/src/spell/Tooltip.cpp index b159f671..c88011a8 100644 --- a/src/spell/Tooltip.cpp +++ b/src/spell/Tooltip.cpp @@ -31,7 +31,7 @@ using BuildSpellTooltip_t = void(__thiscall *)(void *thisObj, int spellID, int a void ShowByID(void *L, int spellID) { if (spellID <= 0) return; - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) return; auto BuildSpellTooltip = @@ -70,7 +70,7 @@ static int __fastcall Script_GameTooltipSetSpellByID(void *L) { void AppendByID(void *L, int spellID) { if (spellID <= 0) return; - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) return; auto *tt = static_cast(tooltipObj); @@ -137,7 +137,7 @@ static int __fastcall Script_GameTooltipGetSpell(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:GetSpell()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) return 0; @@ -176,7 +176,7 @@ static int __fastcall Script_GameTooltipHasSpell(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:HasSpell()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) { Game::Lua::PushBool(L, 0); return 1; diff --git a/src/texture/Atlas.cpp b/src/texture/Atlas.cpp index 18523652..4b584bb0 100644 --- a/src/texture/Atlas.cpp +++ b/src/texture/Atlas.cpp @@ -293,7 +293,7 @@ int __fastcall Script_RegisterAtlas(void *L) { // filter or wrap mode to set, and dropping them silently is what lets modern call // sites pass through unchanged. int __fastcall Script_SetAtlas(void *L) { - void *region = Game::Lua::ResolveObject(L, 1); + void *region = Game::Lua::ResolveTexture(L); if (region == nullptr) return 0; @@ -350,7 +350,7 @@ int __fastcall Script_SetAtlas(void *L) { // `texture:GetAtlas()` — the atlas last set on this texture, or nil if the // texture has been pointed somewhere else since. int __fastcall Script_GetAtlas(void *L) { - void *region = Game::Lua::ResolveObject(L, 1); + void *region = Game::Lua::ResolveTexture(L); if (region == nullptr) return 0; const Applied *entry = LiveEntry(region); @@ -374,7 +374,7 @@ int __fastcall Script_GetAtlas(void *L) { // the addon's coordinates in sprite space, so identity means the whole sprite // for an atlas'd texture and the whole file for any other. int __fastcall Script_ResetTexCoord(void *L) { - void *region = Game::Lua::ResolveObject(L, 1); + void *region = Game::Lua::ResolveTexture(L); if (region == nullptr) return 0; diff --git a/src/texture/Desaturation.cpp b/src/texture/Desaturation.cpp index 9348e650..a7ba38e8 100644 --- a/src/texture/Desaturation.cpp +++ b/src/texture/Desaturation.cpp @@ -311,7 +311,7 @@ int LevelOf(void *region) { void *ResolveSelf(void *L) { if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) return nullptr; - return Game::Lua::ResolveObject(L, 1); + return Game::Lua::ResolveTexture(L, 1, /*raiseError=*/false); } // --- Lua surface ------------------------------------------------------------ diff --git a/src/texture/Mask.cpp b/src/texture/Mask.cpp index 5d23925d..d9bd37d7 100644 --- a/src/texture/Mask.cpp +++ b/src/texture/Mask.cpp @@ -441,7 +441,7 @@ static const Game::HookAutoRegister _ctorHook{Offsets::FUN_SIMPLETEXTURE_CTOR, // texture:SetMask("path") sets the mask; SetMask(nil) / SetMask("") clears it. int __fastcall Script_SetMask(void *L) { - void *region = Game::Lua::ResolveObject(L, 1); + void *region = Game::Lua::ResolveTexture(L); if (region == nullptr) return 0; const char *path = (Game::Lua::GetTop(L) >= 2 && Game::Lua::Type(L, 2) == Game::Lua::TYPE_STRING) @@ -485,8 +485,8 @@ int __fastcall Script_CreateMaskTexture(void *L) { // calls add further masks (drawn on units 1..7; extras past the device's stage // budget are stored but not applied). Adding the same mask twice is a no-op. int __fastcall Script_AddMaskTexture(void *L) { - void *base = Game::Lua::ResolveObject(L, 1); - void *mask = Game::Lua::ResolveObject(L, 2); + void *base = Game::Lua::ResolveTexture(L); + void *mask = Game::Lua::ResolveTexture(L, 2, /*raiseError=*/false); if (base == nullptr || mask == nullptr || base == mask) return 0; auto &v = g_baseMasks[base]; @@ -499,10 +499,10 @@ int __fastcall Script_AddMaskTexture(void *L) { // texture:RemoveMaskTexture([mask]) — drop the given mask, or all if omitted. int __fastcall Script_RemoveMaskTexture(void *L) { - void *base = Game::Lua::ResolveObject(L, 1); + void *base = Game::Lua::ResolveTexture(L); if (base == nullptr) return 0; - void *mask = Game::Lua::ResolveObject(L, 2); + void *mask = Game::Lua::ResolveTexture(L, 2, /*raiseError=*/false); auto it = g_baseMasks.find(base); if (it == g_baseMasks.end()) return 0; @@ -519,7 +519,7 @@ int __fastcall Script_RemoveMaskTexture(void *L) { // texture:GetNumMaskTextures() -> count. int __fastcall Script_GetNumMaskTextures(void *L) { - void *base = Game::Lua::ResolveObject(L, 1); + void *base = Game::Lua::ResolveTexture(L); int n = 0; if (base != nullptr) { auto it = g_baseMasks.find(base); @@ -532,7 +532,7 @@ int __fastcall Script_GetNumMaskTextures(void *L) { // texture:GetMaskTexture(index) -> the index-th mask region object (or nil). int __fastcall Script_GetMaskTexture(void *L) { - void *base = Game::Lua::ResolveObject(L, 1); + void *base = Game::Lua::ResolveTexture(L); const int index = (Game::Lua::GetTop(L) >= 2) ? static_cast(Game::Lua::ToNumber(L, 2)) : 1; if (base != nullptr && index >= 1) { diff --git a/src/texture/Transform.cpp b/src/texture/Transform.cpp index 43c8ca42..f50c3815 100644 --- a/src/texture/Transform.cpp +++ b/src/texture/Transform.cpp @@ -246,7 +246,7 @@ void __fastcall StoreCorners_h(void *region, void *edx, float *rect) { int __fastcall Script_SetRotation(void *L) { void *tex = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - tex = Game::Lua::ResolveObject(L, 1); + tex = Game::Lua::ResolveTexture(L, 1, /*raiseError=*/false); if (tex == nullptr || !Game::Lua::IsNumber(L, 2)) { Game::Lua::Error(L, "Usage: texture:SetRotation(angle [, cx, cy])"); return 0; @@ -261,10 +261,21 @@ int __fastcall Script_SetRotation(void *L) { return 0; } +// Registered on BOTH registries below (the reader is shared; only SetRotation +// differs per type), so either is a legitimate self and the gate must accept +// both — hence the id pair rather than `ResolveTexture`. +constexpr uintptr_t kRotatableTypeIds[] = { + Offsets::VAR_TEXTURE_LUA_TYPE_ID, + Offsets::VAR_FONTSTRING_LUA_TYPE_ID, +}; + int __fastcall Script_GetRotation(void *L) { void *tex = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - tex = Game::Lua::ResolveObject(L, 1); + tex = Game::Lua::ResolveTypedObjectAny( + L, 1, kRotatableTypeIds, + static_cast(sizeof(kRotatableTypeIds) / sizeof(kRotatableTypeIds[0])), + /*raiseError=*/false); if (tex == nullptr) { Game::Lua::Error(L, "Usage: region:GetRotation()"); return 0; @@ -500,7 +511,7 @@ void ReapplyFontStringRotation(void *fs) { int __fastcall Script_SetRotationFS(void *L) { void *fs = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - fs = Game::Lua::ResolveObject(L, 1); + fs = Game::Lua::ResolveFontString(L, 1, /*raiseError=*/false); if (fs == nullptr || !Game::Lua::IsNumber(L, 2)) { Game::Lua::Error(L, "Usage: fontstring:SetRotation(angle [, cx, cy])"); return 0; @@ -522,7 +533,7 @@ int __fastcall Script_SetRotationFS(void *L) { int __fastcall Script_SetVertexOffset(void *L) { void *tex = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - tex = Game::Lua::ResolveObject(L, 1); + tex = Game::Lua::ResolveTexture(L, 1, /*raiseError=*/false); const int slot = Game::Lua::IsNumber(L, 2) ? VertexSlot(static_cast(Game::Lua::ToNumber(L, 2))) : -1; @@ -547,7 +558,7 @@ int __fastcall Script_SetVertexOffset(void *L) { int __fastcall Script_GetVertexOffset(void *L) { void *tex = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - tex = Game::Lua::ResolveObject(L, 1); + tex = Game::Lua::ResolveTexture(L, 1, /*raiseError=*/false); const int slot = Game::Lua::IsNumber(L, 2) ? VertexSlot(static_cast(Game::Lua::ToNumber(L, 2))) : -1; @@ -568,7 +579,7 @@ int __fastcall Script_GetVertexOffset(void *L) { int __fastcall Script_GetCorners(void *L) { void *tex = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - tex = Game::Lua::ResolveObject(L, 1); + tex = Game::Lua::ResolveTexture(L, 1, /*raiseError=*/false); if (tex == nullptr) { Game::Lua::Error(L, "Usage: texture:GetCorners()"); return 0; @@ -596,7 +607,7 @@ int __fastcall Script_GetCorners(void *L) { int __fastcall Script_GetGlyphVerts(void *L) { void *fs = nullptr; if (Game::Lua::Type(L, 1) == Game::Lua::TYPE_TABLE) - fs = Game::Lua::ResolveObject(L, 1); + fs = Game::Lua::ResolveFontString(L, 1, /*raiseError=*/false); if (fs == nullptr) { Game::Lua::Error(L, "Usage: fontstring:GetGlyphVerts()"); return 0; diff --git a/src/tooltip/CompareItem.cpp b/src/tooltip/CompareItem.cpp index 8f51bd07..f016c4e1 100644 --- a/src/tooltip/CompareItem.cpp +++ b/src/tooltip/CompareItem.cpp @@ -525,7 +525,10 @@ int __fastcall Script_SetHyperlinkCompareItem(void *L) { int hoveredID = Item::Arg::ResolveItemID(L, 2); int hoveredSuffix = Item::StatAccum::ParseRandomSuffixFromLink(L, 2); if (hoveredID <= 0 && Game::Lua::Type(L, 5) == Game::Lua::TYPE_TABLE) { - if (void *cmp = Game::Lua::ResolveObject(L, 5)) { + // Typed: `CurrentID` reads tooltip fields off this object, so a + // non-tooltip here would read them out of unrelated memory. Silent — + // the argument is optional and we already fall through without it. + if (void *cmp = Game::Lua::ResolveTooltip(L, 5, /*raiseError=*/false)) { uint64_t g = 0; hoveredID = Item::TooltipItem::CurrentID(cmp, &g); if (g != 0) { @@ -581,7 +584,7 @@ int __fastcall Script_SetHyperlinkCompareItem(void *L) { // The tooltip's underlying CFrameScriptObject — the `this` for the // engine's builder / add-line thiscalls. - void *self = Game::Lua::ResolveObject(L, 1); + void *self = Game::Lua::ResolveTooltip(L); if (self == nullptr) return bail(0); @@ -630,7 +633,7 @@ int __fastcall Script_IsEquippedItem(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:IsEquippedItem()"); return 0; } - void *self = Game::Lua::ResolveObject(L, 1); + void *self = Game::Lua::ResolveTooltip(L); const int itemID = self != nullptr ? Item::TooltipItem::CurrentID(self, nullptr) : 0; bool equipped = false; diff --git a/src/tooltip/Owner.cpp b/src/tooltip/Owner.cpp index 421a7776..02051f26 100644 --- a/src/tooltip/Owner.cpp +++ b/src/tooltip/Owner.cpp @@ -49,7 +49,7 @@ static int __fastcall Script_GameTooltipGetOwner(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:GetOwner()"); return 0; } - void *tooltip = Game::Lua::ResolveObject(L, 1); + void *tooltip = Game::Lua::ResolveTooltip(L); if (tooltip == nullptr) return 0; diff --git a/src/tooltip/SetAction.cpp b/src/tooltip/SetAction.cpp index 0412950c..44ded978 100644 --- a/src/tooltip/SetAction.cpp +++ b/src/tooltip/SetAction.cpp @@ -117,7 +117,7 @@ int __fastcall Script_SetAction(void *L) { return CallScript(Offsets::FUN_SCRIPT_GAMETOOLTIP_SET_ACTION, L); if (info.target == Macro::ShowTooltip::Target::Spell) { - void *tooltip = Game::Lua::ResolveObject(L, 1); + void *tooltip = Game::Lua::ResolveTooltip(L); if (tooltip == nullptr) return CallScript(Offsets::FUN_SCRIPT_GAMETOOLTIP_SET_ACTION, L); return ShowSpell(L, tooltip, slot0, static_cast(info.spellID)); diff --git a/src/totem/Tracker.cpp b/src/totem/Tracker.cpp index fb0f2b7a..de793bcb 100644 --- a/src/totem/Tracker.cpp +++ b/src/totem/Tracker.cpp @@ -530,7 +530,7 @@ int __fastcall Script_GameTooltipSetTotem(void *L) { if (!t.active || t.spellID == 0) return 0; - void *self = Game::Lua::ResolveObject(L, 1); + void *self = Game::Lua::ResolveTooltip(L); if (self == nullptr) return 0; diff --git a/src/unit/Tooltip.cpp b/src/unit/Tooltip.cpp index 135115ee..06438de7 100644 --- a/src/unit/Tooltip.cpp +++ b/src/unit/Tooltip.cpp @@ -151,7 +151,7 @@ static int __fastcall Script_GameTooltipGetUnitGUID(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:GetUnitGUID()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) return 0; @@ -184,7 +184,7 @@ static int __fastcall Script_GameTooltipHasUnit(void *L) { Game::Lua::Error(L, "Usage: GameTooltip:HasUnit()"); return 0; } - void *tooltipObj = Game::Lua::ResolveObject(L, 1); + void *tooltipObj = Game::Lua::ResolveTooltip(L); if (tooltipObj == nullptr) { Game::Lua::PushBool(L, false); return 1;