diff --git a/README.md b/README.md index a03fc589..047a9829 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ reference in **[docs/API.md](docs/API.md)**. | [GameObject](docs/API.md#gameobject) | `C_GameObjectInfo.GetGameObjectInfoByID`, `C_GameObjectInfo.RequestLoadGameObjectByID`, `ClosestGameObjectPosition` | | [Glue](docs/API.md#glue) | `C_Glue.IsOnGlueScreen` | | [Gossip](docs/API.md#gossip) | `C_GossipInfo.CloseGossip`, `C_GossipInfo.GetActiveQuests`, `C_GossipInfo.GetAvailableQuests`, `C_GossipInfo.GetNumActiveQuests`, `C_GossipInfo.GetNumAvailableQuests`, `C_GossipInfo.GetNumOptions`, `C_GossipInfo.GetOptions`, `C_GossipInfo.GetText`, `C_GossipInfo.SelectActiveQuest`, `C_GossipInfo.SelectAvailableQuest`, `C_GossipInfo.SelectOption`, `C_GossipInfo.SelectOptionByIndex` | -| [GameTooltip](docs/API.md#gametooltip) | `GameTooltip:AddSpellByID`, `GameTooltip:AddTexture`, `GameTooltip:GetGameObject`, `GameTooltip:GetItem`, `GameTooltip:GetOwner`, `GameTooltip:GetSpell`, `GameTooltip:GetUnit`, `GameTooltip:GetUnitGUID`, `GameTooltip:HasGameObject`, `GameTooltip:HasItem`, `GameTooltip:HasSpell`, `GameTooltip:HasUnit`, `GameTooltip:IsEquippedItem`, `GameTooltip:SetEquipmentSet`, `GameTooltip:SetHyperlink` (full links, `spell:`, `quest:`), `GameTooltip:SetHyperlinkCompareItem`, `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByGUID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetTotem`, `GameTooltip:SetUnitAura`, `OnTooltipSetItem`, `OnTooltipSetSpell`, `OnTooltipSetUnit`, `OnTooltipSetGameObject` (scripts) | +| [GameTooltip](docs/API.md#gametooltip) | `GameTooltip:AddSpellByID`, `GameTooltip:AddTexture`, `GameTooltip:GetGameObject`, `GameTooltip:GetItem`, `GameTooltip:GetOwner`, `GameTooltip:GetSpell`, `GameTooltip:GetUnit`, `GameTooltip:GetUnitGUID`, `GameTooltip:HasGameObject`, `GameTooltip:HasItem`, `GameTooltip:HasSpell`, `GameTooltip:HasUnit`, `GameTooltip:IsEquippedItem`, `GameTooltip:SetEquipmentSet`, `GameTooltip:SetFrameStack`, `GameTooltip:SetHyperlink` (full links, `spell:`, `quest:`), `GameTooltip:SetHyperlinkCompareItem`, `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByGUID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetTotem`, `GameTooltip:SetUnitAura`, `OnTooltipSetItem`, `OnTooltipSetSpell`, `OnTooltipSetUnit`, `OnTooltipSetGameObject`, `OnTooltipSetFrameStack` (scripts) | | [Hooks](docs/API.md#hooks) | `hooksecurefunc` | | [Input](docs/API.md#input) | `GetMouseButtonClicked`, `IsLeftAltKeyDown`, `IsLeftControlKeyDown`, `IsLeftShiftKeyDown`, `IsModifierKeyDown`, `IsMouseButtonDown`, `IsRightAltKeyDown`, `IsRightControlKeyDown`, `IsRightShiftKeyDown` | | [Instance](docs/API.md#instance) | `GetInstanceInfo`, `IsActiveBattlefieldArena` | diff --git a/TODO.md b/TODO.md index 820c5fc7..47a9ee40 100644 --- a/TODO.md +++ b/TODO.md @@ -951,88 +951,15 @@ See [src/unit/Tooltip.cpp](src/unit/Tooltip.cpp) and the `FUN_SCRIPT_GAMETOOLTIP_SET_UNIT_*` block in [src/Offsets.h](src/Offsets.h). -## 46. `GameTooltip:SetFrameStack([showHidden])` — deferred (Lua-side fix in place) +## ~~46. `GameTooltip:SetFrameStack([showHidden])`~~ — DONE -Renders a tooltip listing the chain of frames at the mouse cursor, -sorted by strata + level. The 3.0+ debug-tooling primitive used by -Blizzard's `Blizzard_DebugTools` and every layout-debugging addon. - -### Current state in this project - -Our bundled `DebugTools/` backport ships its own `FrameStackTooltip` -in [DebugTools/DebugTools.lua](DebugTools/DebugTools.lua), driven -by `_CollectFramesUnderMouse`. As of the EnumerateFrames switch, -the iterator walks the engine's own frame list — fast (typically -~hundreds of entries) and includes anonymous frames the engine -sees but a `_G` walk would miss. - -What's still missing relative to the real engine `SetFrameStack`: - -- **Anonymous-frame display name.** We currently show - `` for frames where `frame:GetName()` returns nil. - The engine's method shows the frame's pointer + the Lua-side - registry-table address. To get there we'd need a Lua-callable - shim that exposes a numeric handle per frame (vftable + Lua-ref - lookup), which is non-trivial. -- **Single-tooltip semantics.** Modern addons can call - `GameTooltip:SetFrameStack(...)` directly on any tooltip; - ours requires the `FrameStackTooltip` we ship in `DebugTools/`. - Most consumers (`Blizzard_DebugTools` ports, layout addons via - `/framestack`) don't care, but a strict polyfill would. - -### C++ port from 3.3.5 — kept as a research note - -What `Script_GameTooltip_SetFrameStack` -actually does in 3.3.5 (Frostmourne client, registration entry at -VA 0x00AD2CF0 → function VA 0x00626560 → inner builder -`FUN_006230d0`): - -- Pulls cursor (in frame space) via `FUN_0047BFF0` from a render- - state global at `[DAT_00B499A8 + 0x1224 / +0x1228]`. -- Walks an intrusive linked list at `[DAT_00B499A8 + 0xCD4]`. Each - frame node reads: - - `[+0x00]` — vftable; `vftable[+0x10]` = "is the frame visible - enough to hit-test" predicate. - - `[+0x0E0]` (`node[0x38]`) = strata; gates `(strata != 0 || - showHidden)`. - - `[+0x0D0]..[+0x0DC]` (`node[0x34..0x37]`) = frame rect for - hit-test. - - `[+0x0D8]` (`node[0x36]`) flags, bit 2 → visible. - - `[+0x288]` (`node[0xA2]`) = next pointer. -- Per-frame name via `vftable[+0x04]` (a `GetDebugName()`-style - virtual). If the frame is Lua-side (`vftable[+0x10]` returns - type code 5), an extra `lua_rawgeti(L, REGISTRY, frame->luaRef)` - pulls the Lua-table reference for the `"%s: %p (%s)"` format; - otherwise it's `"%s: %p"`. -- Builds into a scratch sort array at `DAT_00C5D370` (entry stride - `0x410`), `qsort` against comparator `FUN_0061A5B0`. -- Header line via `FUN_00819D40("DEBUG_FRAMESTACK", ...)` (Lua - `GetGlobalString`) then per-entry `FUN_0061FEC0` (tooltip - `AddLine` equivalent, takes color codes from a 4-byte-stride - table at `0xAD2DC0` / `0xAD2DB8` / `0xAD2DB0` for - visible/hidden/special states). - -For a 1.12 port we'd need to derive: - -- The frame-manager global (3.3.5's `DAT_00B499A8` — different VA - in 1.12, different field offsets likely). -- Frame struct offsets for rect, strata, flags, next, name vftable - slot — the layout shifted between 1.12 and 3.3.5 (same way unit - field offsets did, per the `OFF_UNIT_FIELD_HEALTH` discovery in - CLAUDE.md). -- AddTooltipLine engine address (1.12 has it — every - `GameTooltip:SetX` method calls it internally). -- Lua-side reference lookup helper (whatever 1.12 uses to map - frame → Lua table; the `FrameScript_GetObject` chain at - `0x6F3740` is part of this). - -### Recommendation - -The Lua-side iterator switch is done. The C++ port stays deferred -unless we need the engine frame list for something else -(`GetMouseFocus` polyfill, anonymous-frame inspector with extra -fields beyond what Lua exposes, single-tooltip strict polyfill, -etc.) that would amortize the RE cost. +A mirror of 4.3.4's CGTooltip::SetFrameStack in +[src/tooltip/FrameStack.cpp](src/tooltip/FrameStack.cpp), plus the +`OnTooltipSetFrameStack` script in Tooltip::SetEvents. Every input exists in +1.12: the CSimpleTop frame list, the NDC cursor + NDCToDDC, CLayoutFrame +GetRect, the frame's visible / enabled-input / strata / level fields, +FrameStrataToString and the two default tooltip colors (all in Offsets.h). +The `/fstack` UI itself is addon Lua and can now call the method. ## ~~47. `IsPlayerSpell(spellID)`~~ — DONE diff --git a/docs/API.md b/docs/API.md index a7119119..9f4c904d 100644 --- a/docs/API.md +++ b/docs/API.md @@ -316,7 +316,8 @@ build instructions. - [`GameTooltip:SetHyperlink(link)`](#gametooltipsethyperlinklink) - [`GameTooltip:SetHyperlinkCompareItem("itemLink" [, offset, shiftButton, comparisonTooltip])`](#gametooltipsethyperlinkcompareitemitemlink--offset-shiftbutton-comparisontooltip) - [`GameTooltip:IsEquippedItem()`](#gametooltipisequippeditem) - - [`OnTooltipSet*` scripts (Item / Spell / Unit / GameObject)](#ontooltipset-scripts) + - [`GameTooltip:SetFrameStack([showHidden])`](#gametooltipsetframestackshowhidden) + - [`OnTooltipSet*` scripts (Item / Spell / Unit / GameObject / FrameStack)](#ontooltipset-scripts) - [`GameTooltip:SetItemByGUID(itemGUID)`](#gametooltipsetitembyguiditemguid) - [`GameTooltip:SetEquipmentSet(name)`](#gametooltipsetequipmentsetname) - [Equipment sets on item tooltips](#equipment-sets-on-item-tooltips) @@ -5644,6 +5645,11 @@ Returns a table of the mouse-enabled frames under the cursor, from top to bottom. Index 1 is the frame that `GetMouseFocus()` returns, so `GetMouseFocus() == GetMouseFoci()[1]` is always true. +A frame is in the table when it can get mouse input at the cursor +position, the same test that sets the mouse focus. Hit-rect insets +(`SetHitRectInsets`) apply, and a frame in a scroll frame counts only +inside the visible part of the scroll frame. + ```lua local foci = GetMouseFoci() -- foci[1] == GetMouseFocus() @@ -7774,6 +7780,46 @@ GameTooltip:SetBagItem(0, 1) if GameTooltip:IsEquippedItem() then ... end -- false unless that item is also worn ``` +### `GameTooltip:SetFrameStack([showHidden])` + +Fills the tooltip with the frames under the cursor. This is the method that +`/fstack` uses. The method also shows the tooltip, so set an owner first. + +```lua +GameTooltip:SetOwner(UIParent, "ANCHOR_NONE") +GameTooltip:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -20, 20) +GameTooltip:SetFrameStack() -- shown frames only +GameTooltip:SetFrameStack(true) -- also hidden frames +``` + +The first line shows the text `DEBUG_FRAMESTACK` and the cursor position, in +the same units as `GetCursorPosition()`. Below it, the frames are in groups by +strata, from the top strata down. Each group starts with the strata name. In a +group, frames go from the highest frame level down. Each frame line shows +` name`: + +- A frame with a name shows that name. +- A frame with no name shows its Lua table, for example `table: 1A2B3C4D`. +- When the global variable with the frame's name is not that frame, the line + shows the table and the name, for example `table: 1A2B3C4D (MyFrame)`. + +The color of a line tells you the state of the frame: + +| Color | Frame | +|---|---| +| Orange | Shown, and the mouse is enabled | +| Blue | Shown, and the mouse is not enabled | +| Grey | Hidden (only with `showHidden`) | + +The color has two shades. The shade changes when the frame level changes. + +A frame is in the list when the cursor is in its rectangle. Hit-rect insets +and clipping by a parent frame have no effect on the list. The list also +includes frames that do not get mouse input. + +The method runs the `OnTooltipSetFrameStack` script before it shows the +tooltip. + ### `OnTooltipSet*` scripts `OnTooltipSetItem` / `OnTooltipSetSpell` / `OnTooltipSetUnit` / @@ -7791,9 +7837,10 @@ tooltips by hooking one script per object type instead of wrapping every | `OnTooltipSetSpell` | `SetSpell`, `SetSpellByID`, `SetTalent`, `SetShapeshift` | | `OnTooltipSetUnit` | `SetUnit` and unit mouseover | | `OnTooltipSetGameObject` | gameobject mouseover (herb/ore nodes, chests, mailboxes, signs) | +| `OnTooltipSetFrameStack` | [`SetFrameStack`](#gametooltipsetframestackshowhidden) | The engine ships only `OnTooltipAddMoney` / `OnTooltipCleared` / -`OnTooltipSetDefaultAnchor`; these four are added. +`OnTooltipSetDefaultAnchor`; these five are added. The handler receives the tooltip as the **global `this`**, the frame-script convention — *not* a `self` argument (like every built-in diff --git a/src/Game.cpp b/src/Game.cpp index 48507787..b72ce37a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -74,6 +74,7 @@ namespace Lua { F(PCall, lua_pcall, LUA_PCALL) \ F(Next, lua_next, LUA_NEXT) \ F(Type, lua_type, LUA_TYPE) \ + F(TypeName, lua_typename, LUA_TYPE_NAME) \ F(Error, lua_error, LUA_ERROR) \ F(ToPointer, lua_topointer, LUA_TO_POINTER) \ F(ToThread, lua_tothread, LUA_TO_THREAD) \ diff --git a/src/Game.h b/src/Game.h index 96d66e4e..b29fdc68 100644 --- a/src/Game.h +++ b/src/Game.h @@ -257,6 +257,7 @@ using lua_call_t = void(__fastcall *)(void *L, int nargs, int nresults); using lua_pcall_t = int(__fastcall *)(void *L, int nargs, int nresults, int errfunc); using lua_next_t = int(__fastcall *)(void *L, int idx); using lua_type_t = int(__fastcall *)(void *L, int index); +using lua_typename_t = const char *(__fastcall *)(void *L, int type); // The 1.12 `lua_error` wrapper at `0x6F4940` is actually variadic — // it does `lua_pushvfstring(L, fmt, args)` then prepends `luaL_where` // and throws. Existing callers pass a single literal (no `%`), which @@ -315,6 +316,7 @@ extern const lua_call_t Call; extern const lua_pcall_t PCall; extern const lua_next_t Next; extern const lua_type_t Type; +extern const lua_typename_t TypeName; extern const lua_error_t Error; extern const lua_topointer_t ToPointer; extern const lua_tothread_t ToThread; diff --git a/src/Offsets.h b/src/Offsets.h index 42be2205..a4705503 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -690,8 +690,10 @@ enum Offsets { OFF_SIMPLETEXTURE_SHADER = 0x128, // Get the region's resolved screen rect: `__thiscall(region+OFF_REGION_ANCHOR, // float out[4]) -> int` (1 = valid, 0 = not laid out yet). Reads the anchor - // sub-object's +0x40..+0x4C = {top, left, bottom, right}, gated on the - // rect-valid bit [+0x3c]&1. This is the engine's own source for the corner + // sub-object's +0x40..+0x4C = {bottom, left, top, right} — 4.3.4's + // CLayoutFrame::GetRect and its CRect {minY, minX, maxY, maxX}, in DDC + // (Script_GetBottom 0x007A1CE0 reads out[0], Script_GetTop 0x007A1BC0 + // out[2]) — gated on the rect-valid bit [+0x3c]&1. This is the engine's own source for the corner // rect (see FUN_00770410's SetTexCoord path); texture/Rotation.cpp reads it to // rebuild axis-aligned corners before rotating. FUN_REGION_GET_RECT = 0x00768320, @@ -2812,13 +2814,70 @@ enum Offsets { // Head of the engine's frame list (`*(*VAR_UI_CONTEXT_PTR + 0xCC4)`), // and each frame CObject's next-in-enumeration pointer at +0x308 — the // list `Script_EnumerateFrames` (0x00705F60) walks. A node with its low - // bit set (or null) is the end sentinel. `Frame::MouseFoci` walks it to - // build the GetMouseFoci stack. + // bit set (or null) is the end sentinel. `Tooltip::FrameStack` walks it + // (4.3.4's CSimpleTop::m_frames). OFF_UI_CONTEXT_FRAME_LIST_HEAD = 0xCC4, OFF_FRAME_ENUM_NEXT = 0x308, // Frame level, a plain int at CObject +0xC4 (the value `Script_GetFrameLevel` // at 0x007744A0 pushes as `frame[0x31]`). Read directly for stack ordering. OFF_FRAME_LEVEL = 0xC4, + // Frame strata, an int at +0xC0: `Script_GetFrameStrata` (0x007742A0) + // pushes FUN_FRAME_STRATA_TO_STRING([frame+0xC0]). + OFF_FRAME_STRATA = 0xC0, + // `int` — nonzero when the frame is effectively shown (4.3.4's + // CSimpleFrame::m_visible). `Script_IsVisible` (0x007758D0) pushes + // `[frame+0xD4] != 0`. + OFF_FRAME_VISIBLE = 0xD4, + // Enabled-input bitmask (4.3.4's m_enabledEventsBitmask), one bit per + // input event type: bit `1 << SIMPLE_EVENT_*`. `Script_IsMouseEnabled` + // (0x00777130) pushes `([frame+0xCC] >> 2) & 1`. + OFF_FRAME_ENABLED_EVENTS = 0xCC, + // Input event types (4.3.4's CSimpleEventType). EnableMouse (0x00777070) + // enables / disables type 2 through FUN_0076AF00 / FUN_0076AF50. + SIMPLE_EVENT_MOUSE = 2, + // The UI context's input queues (4.3.4's CSimpleTop::m_eventqueue[type] + // [strata]): `ctx + OFF_UI_CONTEXT_EVENT_QUEUES + (strata + + // FRAME_STRATA_COUNT * type) * EVENT_QUEUE_STRIDE`. Each holds node + // pointers at +OFF_EVENT_QUEUE_DATA (count at +OFF_EVENT_QUEUE_COUNT); + // a node is `{frame, priority}`, kept sorted by priority high to low + // (default priority = frame level). FUN_0076AF00 adds a frame only while + // it is visible (OFF_FRAME_VISIBLE) — via the register routine + // FUN_00764AA0 — so a queue holds exactly the shown frames with that input + // enabled. The mouse-focus pass FUN_007660D0 walks the mouse queues from + // strata 8 down to 0 and makes the first frame whose mouse handler + // (vtable+0x3C, base 0x0076B320 → FUN_FRAME_HIT_TEST) hits the focus. + OFF_UI_CONTEXT_EVENT_QUEUES = 0xD10, + FRAME_STRATA_COUNT = 9, + EVENT_QUEUE_STRIDE = 0x1C, + OFF_EVENT_QUEUE_DATA = 0x08, + OFF_EVENT_QUEUE_COUNT = 0x10, + // The mouse hit test — `int __thiscall(frame, const float point[2])`, + // point {x, y} in DDC. Needs the frame's rect valid, then tests the hit + // rect at +0xD8..+0xE4 (layout rect less the hit-rect insets); a frame + // with FRAME_FLAG_SCROLLED tests it clipped to its nearest ancestor + // without that flag. No side effects (the vtable+0x3C wrapper adds the + // drag-start check before calling it). + FUN_FRAME_HIT_TEST = 0x0076B020, + // FrameStrataToString — `const char *__fastcall(int strata /*ecx*/)`. + // Scans the 8-entry `{int strata, const char *name}` table at 0x008119F8 + // (BACKGROUND = 1 … TOOLTIP = 8); anything else → "UNKNOWN" (0x00838044). + FUN_FRAME_STRATA_TO_STRING = 0x006F1820, + // The cursor in NDC: two floats {x, y} at `*VAR_UI_CONTEXT_PTR + 0x1118` + // (4.3.4's CSimpleTop::m_mousePosition). GetCursorPosition (0x0048B820) + // and the mouse hit test FUN_007664F0 both read it and convert it with + // FUN_COORD_NDC_TO_DDC. + OFF_UI_CONTEXT_MOUSE_POSITION = 0x1118, + // NDCToDDC — `void __fastcall(float *outX /*ecx*/, float *outY /*edx*/, + // float x, float y)`, `ret 8`: x × VAR_COORD_DDC_WIDTH_SCALE, + // y × VAR_COORD_DDC_HEIGHT_SCALE. Either out pointer may be null. DDC is + // the space frame rects (FUN_REGION_GET_RECT) are stored in. + FUN_COORD_NDC_TO_DDC = 0x0041AD80, + // The two default tooltip line colors, packed {b,g,r,a} like every + // FUN_GAMETOOLTIP_ADD_LINE color: text 0xFFFFFFFF (static init 0x00528E90) + // and title 0xFFFFD200 (static init 0x00528E50) — 4.3.4's + // s_tooltipTextColor / s_tooltipTitleColor. + VAR_TOOLTIP_TEXT_COLOR = 0x00C0CF60, + VAR_TOOLTIP_TITLE_COLOR = 0x00C0D3E8, FUN_SCRIPT_FRAME_SHOW = 0x00775750, FUN_SCRIPT_FRAME_HIDE = 0x00775810, FUN_SCRIPT_FRAME_SETMINRESIZE = 0x00776020, @@ -2839,14 +2898,6 @@ enum Offsets { // it so a unit-attributed frame registers as the mouse-focus (bare frames // otherwise never hover). Standard `int __fastcall(void *L)` Script_* shape. FUN_SCRIPT_FRAME_ENABLEMOUSE = 0x00777070, - // Frame predicates used by `Frame::MouseFoci` to filter the enumeration to - // the interactive stack (visible + mouse-enabled). Standard Script_* shape - // `int __fastcall(void *L)`, self at Lua idx 1, result pushed at idx 2. - FUN_SCRIPT_FRAME_IS_VISIBLE = 0x007758D0, - FUN_SCRIPT_FRAME_IS_MOUSE_ENABLED = 0x00777130, - // `frame:GetFrameStrata()` — pushes the strata name string; ranked for - // stack ordering (`Frame::MouseFoci`). - FUN_SCRIPT_FRAME_GET_STRATA = 0x007742A0, // (The button click dispatcher is FUN_BUTTON_CLICK, defined with the frame // script slots near FUN_FRAME_RUN_SCRIPT_WITH_CONTEXT. A note here used to // forbid hooking it on a SuperWoW-collision theory that a per-DLL scan has @@ -6309,6 +6360,9 @@ enum Offsets { LUA_REMOVE = 0x6F30D0, // (was 0x6F32B0, which is lua_replace) LUA_REPLACE = 0x6F32B0, LUA_TYPE = 0x6F3400, + // `lua_typename(L, type)` — `const char *__fastcall`; indexes the name + // array at 0x00811CD0, `-1` → "no value" (0x00871624). + LUA_TYPE_NAME = 0x6F3480, LUA_GET_TOP = 0x6F3070, LUA_SET_TOP = 0x6F3080, LUA_CALL = 0x6F4180, diff --git a/src/frame/MouseFoci.cpp b/src/frame/MouseFoci.cpp index 279b594c..4ac27cba 100644 --- a/src/frame/MouseFoci.cpp +++ b/src/frame/MouseFoci.cpp @@ -15,177 +15,71 @@ // // Modern WoW exposes GetMouseFoci() alongside GetMouseFocus(), with the // invariant `GetMouseFocus() == GetMouseFoci()[1]`. Vanilla stores only the -// single topmost focus — Script_GetMouseFocus (0x0048DF40) reads -// `*(*VAR_UI_CONTEXT_PTR + OFF_UI_CONTEXT_MOUSE_FOCUS)` and there is no stored -// stack — so this is a native reimplementation of the frame-stack walk -// Blizzard_DebugTools does in Lua (`EnumerateFrames` + `IsMouseOver`): +// single topmost focus (Script_GetMouseFocus 0x0048DF40 reads +// `*(*VAR_UI_CONTEXT_PTR + OFF_UI_CONTEXT_MOUSE_FOCUS)`), so this replays the +// engine's own mouse-focus pass, FUN_007660D0, without stopping at the first +// hit: // -// 1. Walk the engine frame list (the same one Script_EnumerateFrames -// 0x00705F60 iterates): head at `*(ctx + OFF_UI_CONTEXT_FRAME_LIST_HEAD)`, -// each frame's next at `+OFF_FRAME_ENUM_NEXT`, a low-bit-set / null node -// ends it. -// 2. Keep frames that are visible + mouse-enabled and whose rect contains the -// cursor (hit-test mirrors Frame::Modern's IsMouseOver: Region getters + -// cursor / effective-scale). -// 3. Order top-first by strata rank then frame level. -// 4. Force index 1 to be the engine's stored focus so the documented -// invariant holds exactly, regardless of how our sort tie-breaks. +// - candidates are the mouse input queues (OFF_UI_CONTEXT_EVENT_QUEUES, +// type SIMPLE_EVENT_MOUSE): exactly the shown, mouse-enabled frames, per +// strata, already in the engine's priority order; +// - strata are walked from the top (8) down, as the pass does; +// - each frame is tested with FUN_FRAME_HIT_TEST against the cursor +// converted by NDCToDDC — the pass's own test, so hit-rect insets and +// scroll clipping count. (The pass calls it through the frame's mouse +// handler, which also starts drags; we call the test directly.) // -// Pure query — no hooks, registered like GetMouseFocus. +// The engine's stored focus is still forced to index 1: it is the value +// GetMouseFocus returns, and it can lag the cursor until the next mouse move. +// +// Pure query — no hooks, no Lua calls, registered like GetMouseFocus. #include "Game.h" #include "Offsets.h" #include "ui/FrameObject.h" -#include #include -#include #include namespace Frame::MouseFoci { namespace { -using ScriptFn_t = int(__fastcall *)(void *L); - -int CallScript(uintptr_t fn, void *L) { - return reinterpret_cast(fn)(L); -} - -// The UI context is a pointer stored at VAR_UI_CONTEXT_PTR (null pre-world). -void *UIContext() { - return Game::Read(static_cast(Offsets::VAR_UI_CONTEXT_PTR)); -} - -// Strata name -> draw rank, low (behind) to high (on top). Unknown -> 0. -int StrataRank(const char *s) { - if (s == nullptr) - return 0; - static const struct { - const char *name; - int rank; - } kStrata[] = { - {"WORLD", 0}, {"BACKGROUND", 1}, {"LOW", 2}, - {"MEDIUM", 3}, {"HIGH", 4}, {"DIALOG", 5}, - {"FULLSCREEN", 6}, {"FULLSCREEN_DIALOG", 7}, {"TOOLTIP", 8}, - }; - for (const auto &e : kStrata) - if (std::strcmp(s, e.name) == 0) - return e.rank; - return 0; -} - -// Region getter that pushes one number, called with `self` at Lua idx 1. -// Returns false when it pushed nil/none (unresolved rect). -bool SelfNumber(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; -} - -// Boolean predicate (IsVisible / IsMouseEnabled), `self` at Lua idx 1. -bool SelfBool(void *L, uintptr_t fn) { - Game::Lua::SetTop(L, 1); - CallScript(fn, L); - return Game::Lua::ToBoolean(L, 2) != 0; -} - -struct Cursor { - double x, y; -}; - -// GetCursorPosition and the Region rect getters share the same scale factor; -// dividing the cursor by the frame's effective scale (the field GetLeft/etc. -// themselves divide by) puts both in the frame's logical space. Mirrors -// Frame::Modern's IsMouseOver exactly. -Cursor ReadCursor(void *L) { - Game::Lua::SetTop(L, 0); - CallScript(Offsets::FUN_SCRIPT_GETCURSORPOSITION, L); // (x, y) - return Cursor{Game::Lua::ToNumber(L, 1), Game::Lua::ToNumber(L, 2)}; -} - -// With `self` at Lua idx 1, is the cursor within the frame's rect? -bool CursorOverSelf(void *L, void *obj, const Cursor &cur) { - const float effScale = Game::Read(obj, Offsets::OFF_REGION_EFFECTIVE_SCALE); - if (effScale == 0.0f) - return false; // unpositioned region — no rect - double left, right, top, bottom; - if (!SelfNumber(L, Offsets::FUN_SCRIPT_REGION_GETLEFT, &left) || - !SelfNumber(L, Offsets::FUN_SCRIPT_REGION_GETRIGHT, &right) || - !SelfNumber(L, Offsets::FUN_SCRIPT_REGION_GETTOP, &top) || - !SelfNumber(L, Offsets::FUN_SCRIPT_REGION_GETBOTTOM, &bottom)) - return false; - const double x = cur.x / effScale; - const double y = cur.y / effScale; - return x >= left && x <= right && y >= bottom && y <= top; -} - -struct Hit { - void *obj; - int strata; - int level; -}; +using NDCToDDC_t = void(__fastcall *)(float *outX, float *outY, float x, float y); +using HitTest_t = int(__thiscall *)(void *frame, const float *point); int __fastcall Script_GetMouseFoci(void *L) { - void *ctx = UIContext(); - Game::Lua::SetTop(L, 0); - if (ctx == nullptr) { - Game::Lua::NewTable(L); // pre-world → empty table - return 1; - } - - // The engine's authoritative topmost focus — the frame GetMouseFocus - // returns. Forced to index 1 below. - void *focus = Game::Read(ctx, Offsets::OFF_UI_CONTEXT_MOUSE_FOCUS); - - const Cursor cur = ReadCursor(L); - - std::vector hits; - void *node = Game::Read(ctx, Offsets::OFF_UI_CONTEXT_FRAME_LIST_HEAD); - while (node != nullptr && (reinterpret_cast(node) & 1) == 0) { - // Read the next link before any Lua work (the walk is engine-list - // based, independent of the Lua stack, but this keeps it obvious). - void *next = Game::Read(node, Offsets::OFF_FRAME_ENUM_NEXT); - - Game::Lua::SetTop(L, 0); - UI::FrameObject::Push(L, node); // (self) - - if (SelfBool(L, Offsets::FUN_SCRIPT_FRAME_IS_VISIBLE) && - SelfBool(L, Offsets::FUN_SCRIPT_FRAME_IS_MOUSE_ENABLED) && - CursorOverSelf(L, node, cur)) { - Game::Lua::SetTop(L, 1); - CallScript(Offsets::FUN_SCRIPT_FRAME_GET_STRATA, L); // (self, strata) - const int rank = StrataRank(Game::Lua::ToString(L, 2)); - const int level = Game::Read(node, Offsets::OFF_FRAME_LEVEL); - hits.push_back(Hit{node, rank, level}); - } - - node = next; - } - - // Top-first: higher strata, then higher level within a strata. - std::sort(hits.begin(), hits.end(), [](const Hit &a, const Hit &b) { - if (a.strata != b.strata) - return a.strata > b.strata; - return a.level > b.level; - }); - - // Order the frames, forcing GetMouseFocus() to be [1] so the invariant - // holds by construction (focus may be a frame our rect test skips, e.g. - // the WorldFrame, or a same-strata/level tie our sort orders differently). - std::vector ordered; - ordered.reserve(hits.size() + 1); - if (focus != nullptr) - ordered.push_back(focus); - for (const Hit &h : hits) - if (h.obj != focus) - ordered.push_back(h.obj); - + auto *ctx = Game::Read(Offsets::VAR_UI_CONTEXT_PTR); Game::Lua::SetTop(L, 0); Game::Lua::NewTable(L); // result at idx 1 + if (ctx == nullptr) + return 1; // pre-world → empty table + + void *focus = Game::Read(ctx, Offsets::OFF_UI_CONTEXT_MOUSE_FOCUS); + + const float *mouse = Game::Ptr(ctx, Offsets::OFF_UI_CONTEXT_MOUSE_POSITION); + float point[2] = {0.0f, 0.0f}; + reinterpret_cast(Offsets::FUN_COORD_NDC_TO_DDC)(&point[0], &point[1], mouse[0], + mouse[1]); + + std::vector ordered; + if (focus != nullptr) + ordered.push_back(focus); + auto hitTest = reinterpret_cast(Offsets::FUN_FRAME_HIT_TEST); + for (int strata = Offsets::FRAME_STRATA_COUNT - 1; strata >= 0; --strata) { + const uint8_t *queue = + ctx + Offsets::OFF_UI_CONTEXT_EVENT_QUEUES + + (strata + Offsets::FRAME_STRATA_COUNT * Offsets::SIMPLE_EVENT_MOUSE) * + Offsets::EVENT_QUEUE_STRIDE; + auto *const *nodes = Game::Read(queue, Offsets::OFF_EVENT_QUEUE_DATA); + const uint32_t count = Game::Read(queue, Offsets::OFF_EVENT_QUEUE_COUNT); + for (uint32_t i = 0; i < count; ++i) { + void *frame = Game::Read(nodes[i], 0); + if (frame != focus && hitTest(frame, point) != 0) + ordered.push_back(frame); + } + } + for (size_t i = 0; i < ordered.size(); ++i) { Game::Lua::PushNumber(L, static_cast(i + 1)); UI::FrameObject::Push(L, ordered[i]); diff --git a/src/tooltip/FrameStack.cpp b/src/tooltip/FrameStack.cpp new file mode 100644 index 00000000..bba1d348 --- /dev/null +++ b/src/tooltip/FrameStack.cpp @@ -0,0 +1,233 @@ +// 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 . + +// `GameTooltip:SetFrameStack([showHidden])` — fills the tooltip with every +// frame under the cursor, grouped by strata, top first. The engine half of +// Blizzard_DebugTools' /fstack (FrameStackTooltip calls it on each OnUpdate). +// +// A mirror of 4.3.4's CGTooltip::SetFrameStack (0x0046D380 in build 15595), +// which 1.12 lacks but whose every input it has: +// +// - the frame list CSimpleTop::m_frames: `*ctx + OFF_UI_CONTEXT_FRAME_LIST_HEAD`, +// next at OFF_FRAME_ENUM_NEXT — the list Script_EnumerateFrames walks; +// - the cursor CSimpleTop::m_mousePosition (OFF_UI_CONTEXT_MOUSE_POSITION), +// converted with the engine's NDCToDDC into the space frame rects live in; +// - CLayoutFrame::GetRect (FUN_REGION_GET_RECT) on the frame's layout +// sub-object, and the frame's visible / enabled-input / strata / level +// fields — the same fields IsVisible / IsMouseEnabled / GetFrameStrata / +// GetFrameLevel read; +// - FrameStrataToString, the two default tooltip colors, and the +// DEBUG_FRAMESTACK global string (shipped in the addon's locale files). +// +// The plain layout rect is used, not the mouse hit rect (hit-rect insets, +// clipping): 4.3.4 lists what is drawn under the cursor, including frames +// that do not take the mouse — those just get the other color. +// +// Fires OnTooltipSetFrameStack (Tooltip::SetEvents) before Show, as 4.3.4 does. + +#include "Game.h" +#include "Offsets.h" +#include "tooltip/Lines.h" +#include "tooltip/SetEvents.h" + +#include +#include +#include +#include +#include + +namespace Tooltip::FrameStack { + +namespace { + +using GetName_t = const char *(__thiscall *)(void *object); +using GetRect_t = int(__thiscall *)(void *layout, float *rect); +using NDCToDDC_t = void(__fastcall *)(float *outX, float *outY, float x, float y); +using StrataToString_t = const char *(__fastcall *)(int strata); +using SStrCmpI_t = int(__stdcall *)(const char *a, const char *b, int n); + +constexpr int kStormMaxStr = 0x7FFFFFFF; + +// 4.3.4's line colors, packed {b,g,r,a} like every add-line color; two of each +// so neighbouring frame levels alternate (s_frameStack*Colors). +constexpr uint32_t kMouseColors[2] = {0xFFFF9966, 0xFFDDDD88}; +constexpr uint32_t kColors[2] = {0xFF6699FF, 0xFF88DDDD}; +constexpr uint32_t kHiddenColors[2] = {0xFF666666, 0xFF999999}; + +// One past the highest strata (TOOLTIP = 8), so the first entry always opens +// a strata header — 4.3.4's `lastStrata = 9`. +constexpr int kNoStrata = 9; + +// 4.3.4's FrameStackInfo. +struct Entry { + char name[1024]; + int strata; + int level; + bool mouseEnabled; + uint32_t visible; +}; + +// QSortFrameStack: strata, then level, both high to low, then name. +bool Before(const Entry &a, const Entry &b) { + if (a.strata != b.strata) + return a.strata > b.strata; + if (a.level != b.level) + return a.level > b.level; + auto cmp = reinterpret_cast(Offsets::FUN_SSTR_CMP_I); + return cmp(a.name, b.name, kStormMaxStr) < 0; +} + +// The frame's display name, as 4.3.4 forms it: the global name when +// `_G[name]` is this frame, else the frame's Lua object as "table: %p", +// followed by "(name)" when the frame has a name. +void FormatName(void *L, void *frame, char *out, size_t size) { + auto *vtable = Game::Read(frame, 0); + const char *name = reinterpret_cast( + vtable[Offsets::VTBL_SCRIPTOBJECT_GET_NAME / sizeof(uintptr_t)])(frame); + + if (name == nullptr || *name == '\0') { + Game::Lua::PushObject(L, frame); + std::snprintf(out, size, "%s: %p", Game::Lua::TypeName(L, Game::Lua::Type(L, -1)), + Game::Lua::ToPointer(L, -1)); + Game::Lua::SetTop(L, -2); + return; + } + + Game::Lua::PushString(L, name); + Game::Lua::RawGet(L, Game::Lua::GLOBALS_INDEX); + if (Game::Lua::Type(L, -1) == Game::Lua::TYPE_TABLE) { + void *object = Game::Lua::ResolveObject(L, -1); + Game::Lua::SetTop(L, -2); + if (object == frame) { + std::snprintf(out, size, "%s", name); + return; + } + Game::Lua::PushObject(L, frame); + } + std::snprintf(out, size, "%s: %p (%s)", Game::Lua::TypeName(L, Game::Lua::Type(L, -1)), + Game::Lua::ToPointer(L, -1), name); + Game::Lua::SetTop(L, -2); +} + +int __fastcall Script_GameTooltipSetFrameStack(void *L) { + void *self = Game::Lua::ResolveTooltip(L); + if (self == nullptr) + return 0; + const bool showHidden = Game::Lua::ToBoolean(L, 2) != 0; + + Lines::Clear(self); + + auto *ctx = Game::Read(Offsets::VAR_UI_CONTEXT_PTR); + if (ctx == nullptr) + return 0; + const float *mouse = Game::Ptr(ctx, Offsets::OFF_UI_CONTEXT_MOUSE_POSITION); + float x = 0.0f; + float y = 0.0f; + reinterpret_cast(Offsets::FUN_COORD_NDC_TO_DDC)(&x, &y, mouse[0], mouse[1]); + + void *frame = Game::Read(ctx, Offsets::OFF_UI_CONTEXT_FRAME_LIST_HEAD); + if (frame == nullptr || (reinterpret_cast(frame) & 1) != 0) + return 0; + + std::vector stack; + for (; frame != nullptr && (reinterpret_cast(frame) & 1) == 0; + frame = Game::Read(frame, Offsets::OFF_FRAME_ENUM_NEXT)) { + float rect[4]; // {minY, minX, maxY, maxX} + if (reinterpret_cast(Offsets::FUN_REGION_GET_RECT)( + static_cast(frame) + Offsets::OFF_REGION_ANCHOR, rect) == 0) + continue; + const uint32_t visible = Game::Read(frame, Offsets::OFF_FRAME_VISIBLE); + if ((visible == 0 && !showHidden) || x < rect[1] || x > rect[3] || y > rect[2] || + y < rect[0]) + continue; + + Entry &e = stack.emplace_back(); + FormatName(L, frame, e.name, sizeof(e.name)); + e.strata = Game::Read(frame, Offsets::OFF_FRAME_STRATA); + e.level = Game::Read(frame, Offsets::OFF_FRAME_LEVEL); + e.mouseEnabled = (Game::Read(frame, Offsets::OFF_FRAME_ENABLED_EVENTS) & + (1u << Offsets::SIMPLE_EVENT_MOUSE)) != 0; + e.visible = visible; + } + + std::sort(stack.begin(), stack.end(), Before); + + const uint32_t textColor = Game::Read(Offsets::VAR_TOOLTIP_TEXT_COLOR); + const uint32_t titleColor = Game::Read(Offsets::VAR_TOOLTIP_TITLE_COLOR); + using GetText_t = const char *(__fastcall *)(const char *name, int count, int gender); + const char *header = + reinterpret_cast(Offsets::FUN_FRAMESCRIPT_GET_TEXT)("DEBUG_FRAMESTACK", -1, 0); + + // The cursor in UI pixels — the values GetCursorPosition returns. + char text[1024]; + std::snprintf(text, sizeof(text), "(%.2f, %.2f)", Game::InternalToPixel(x), + Game::InternalToPixel(y)); + Lines::AddDouble(self, header, text, textColor, titleColor); + + auto strataName = reinterpret_cast(Offsets::FUN_FRAME_STRATA_TO_STRING); + int toggle = 1; + int lastStrata = kNoStrata; + int lastLevel = 0; + for (const Entry &e : stack) { + if (e.strata != lastStrata) { + Lines::AddDouble(self, strataName(e.strata), nullptr, textColor, textColor); + lastLevel = e.level; + lastStrata = e.strata; + toggle = 0; + } + if (e.level != lastLevel) { + toggle = (toggle + 1) % 2; + lastLevel = e.level; + } + std::snprintf(text, sizeof(text), " <%d> %s", e.level, e.name); + const uint32_t &color = !e.visible ? kHiddenColors[toggle] + : e.mouseEnabled ? kMouseColors[toggle] + : kColors[toggle]; + Lines::AddDouble(self, text, nullptr, color, color); + } + + SetEvents::FireFrameStack(self); + + Game::Lua::SetTop(L, 1); + Lines::Show(L); + return 0; +} + +const Game::Lua::FrameMethodEntry g_methods[] = { + {"SetFrameStack", &Script_GameTooltipSetFrameStack}, +}; + +const Game::Doc::Field kArgs[] = { + Game::Doc::Opt("showHidden", "bool", "false", "Also list hidden frames, in grey."), +}; +const Game::Doc::Function kSetFrameStack{ + "Fills the tooltip with every frame under the cursor, grouped by strata from the top. " + "Frames that take the mouse are orange, other shown frames blue.", + kArgs, {}}; + +const Game::Doc::Method g_methodDocs[] = { + {"SetFrameStack", &kSetFrameStack}, +}; + +void RegisterLuaFunctions() { + Game::Lua::RegisterFrameMethods( + reinterpret_cast(Offsets::VAR_GAMETOOLTIP_METHOD_REGISTRY), g_methods, + static_cast(sizeof(g_methods) / sizeof(g_methods[0])), g_methodDocs, + static_cast(sizeof(g_methodDocs) / sizeof(g_methodDocs[0]))); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Tooltip::FrameStack diff --git a/src/tooltip/Lines.h b/src/tooltip/Lines.h index 999f496a..232e5e04 100644 --- a/src/tooltip/Lines.h +++ b/src/tooltip/Lines.h @@ -41,6 +41,18 @@ inline void Add(void *self, const char *text, const uint32_t &color, bool wrap = self, text, nullptr, &color, nullptr, wrap ? 1 : 0); } +// Appends a two-column line (FUN_GAMETOOLTIP_ADD_LINE): `left` in +// `leftColor`, `right` (may be null) in `rightColor`. Same color packing and +// lifetime rule as Add. +inline void AddDouble(void *self, const char *left, const char *right, + const uint32_t &leftColor, const uint32_t &rightColor) { + using AddLine_t = void(__thiscall *)(void *self, const char *left, const char *right, + const void *leftColor, const void *rightColor, + int wrap); + reinterpret_cast(Offsets::FUN_GAMETOOLTIP_ADD_LINE)( + self, left, right, &leftColor, &rightColor, 0); +} + // Shows the tooltip at Lua stack[1] (the engine's Script_Show reads self // there). inline void Show(void *L) { diff --git a/src/tooltip/SetEvents.cpp b/src/tooltip/SetEvents.cpp index fd9fdf3c..0c56feaa 100644 --- a/src/tooltip/SetEvents.cpp +++ b/src/tooltip/SetEvents.cpp @@ -28,6 +28,10 @@ // resolver's only contract is "return the address the script lives at", so // external storage works exactly like an in-object slot. // +// - OnTooltipSetFrameStack has no engine builder: Tooltip::FrameStack builds +// the frame stack itself and fires it through FireFrameStack, right before +// Show, where 4.3.4's CGTooltip::SetFrameStack fires it. +// // - Each object type has a single builder funnel that every Set*/mouseover // path converges on (item FUN_GAMETOOLTIP_BUILD_ITEM, spell _BUILD_SPELL, // unit _BUILD_UNIT, gameobject _BUILD_GAMEOBJECT). We co-hook each and, @@ -53,7 +57,7 @@ namespace { // One entry per script kind. Order is arbitrary but the cell slot array and // the builder hooks index by it. -enum ScriptKind { SK_ITEM = 0, SK_SPELL, SK_UNIT, SK_GAMEOBJECT, SK_COUNT }; +enum ScriptKind { SK_ITEM = 0, SK_SPELL, SK_UNIT, SK_GAMEOBJECT, SK_FRAMESTACK, SK_COUNT }; // Script names, matched case-insensitively against the resolver's input. constexpr const char *kNamesLower[SK_COUNT] = { @@ -61,6 +65,7 @@ constexpr const char *kNamesLower[SK_COUNT] = { "ontooltipsetspell", "ontooltipsetunit", "ontooltipsetgameobject", + "ontooltipsetframestack", }; // Per-tooltip storage: one 8-byte {handler, context} slot per script kind. @@ -246,6 +251,8 @@ static const Game::HookAutoRegister _buildGOHook{ } // namespace +void FireFrameStack(void *tooltip) { FireScript(tooltip, SK_FRAMESTACK); } + Suppressor::Suppressor() { ++g_suppress; } Suppressor::~Suppressor() { if (g_suppress > 0) diff --git a/src/tooltip/SetEvents.h b/src/tooltip/SetEvents.h index 1d4595b4..bf033819 100644 --- a/src/tooltip/SetEvents.h +++ b/src/tooltip/SetEvents.h @@ -29,6 +29,10 @@ struct Suppressor { Suppressor &operator=(const Suppressor &) = delete; }; +// Runs the tooltip's OnTooltipSetFrameStack handler, if it has one. +// Tooltip::FrameStack calls it after it adds the frame-stack lines. +void FireFrameStack(void *tooltip); + // Drop every per-tooltip handler cell. Must run at each UI teardown // (FrameScript_Initialize_h — /reload and /logout): the engine destroys and // recreates the tooltip objects, so the cells' tooltip pointers go stale and