From a40236eb84f770b06dc3a736ac3ac8ddca62a2cb Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu, 23 Jul 2026 21:12:06 -0500 Subject: [PATCH] spell: add GameTooltip:AddSpellByID(spellID) Append counterpart to SetSpellByID -- adds a spell's tooltip (name, cast time, cost, range, description) to the current tooltip WITHOUT clearing existing lines, so callers can compose tooltips like AddLine(header) + AddSpellByID(id). BuildSpellTooltip's last arg gates the clear: SetSpellByID passes 0 (clears via FUN_00530050 first); AddSpellByID passes 1 (skips the clear and appends). That append path is the engine's talent "next rank" preview, so it emits a "Next rank:" header instead of the spell name and doesn't stash the displayed spellID -- we overwrite that header line (at the pre-build numLines index) with the real name from Spell.dbc and let GetSpell keep reflecting the base tooltip. Verified in-game. --- README.md | 2 +- docs/API.md | 27 ++++++++++++++++++ src/spell/Tooltip.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ src/spell/Tooltip.h | 9 ++++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index da7ac03d..61b9f739 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | [FriendList](docs/API.md#friendlist) | `C_FriendList.IsWhoQueryPending`, `C_FriendList.SendWhoQueryByName` | | [GameObject](docs/API.md#gameobject) | `C_GameObjectInfo.GetGameObjectInfoByID`, `C_GameObjectInfo.RequestLoadGameObjectByID`, `ClosestGameObjectPosition` | | [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:GetGameObject`, `GameTooltip:GetItem`, `GameTooltip:GetOwner`, `GameTooltip:GetSpell`, `GameTooltip:GetUnitGUID`, `GameTooltip:HasGameObject`, `GameTooltip:HasItem`, `GameTooltip:HasSpell`, `GameTooltip:HasUnit`, `GameTooltip:IsEquippedItem`, `GameTooltip:SetEquipmentSet`, `GameTooltip:SetHyperlinkCompareItem`, `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByGUID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetUnitAura`, `OnTooltipSetItem` (script) | +| [GameTooltip](docs/API.md#gametooltip) | `GameTooltip:AddSpellByID`, `GameTooltip:GetGameObject`, `GameTooltip:GetItem`, `GameTooltip:GetOwner`, `GameTooltip:GetSpell`, `GameTooltip:GetUnitGUID`, `GameTooltip:HasGameObject`, `GameTooltip:HasItem`, `GameTooltip:HasSpell`, `GameTooltip:HasUnit`, `GameTooltip:IsEquippedItem`, `GameTooltip:SetEquipmentSet`, `GameTooltip:SetHyperlinkCompareItem`, `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByGUID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetUnitAura`, `OnTooltipSetItem` (script) | | [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` | diff --git a/docs/API.md b/docs/API.md index 93907394..917ba74a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -161,6 +161,7 @@ build instructions. - [GameTooltip](#gametooltip) - [`GameTooltip:SetSpellByID(spellID)`](#gametooltipsetspellbyidspellid) + - [`GameTooltip:AddSpellByID(spellID)`](#gametooltipaddspellbyidspellid) - [`GameTooltip:SetTalentByID(talentID)`](#gametooltipsettalentbyidtalentid) - [`GameTooltip:SetInventoryItemByID(itemID)`](#gametooltipsetinventoryitembyiditemid) - [`GameTooltip:SetHyperlinkCompareItem("itemLink" [, offset, shiftButton, comparisonTooltip])`](#gametooltipsethyperlinkcompareitemitemlink--offset-shiftbutton-comparisontooltip) @@ -3787,6 +3788,32 @@ GameTooltip:SetSpellByID(133) -- Fireball GameTooltip:Show() ``` +### `GameTooltip:AddSpellByID(spellID)` + +The append counterpart to [`SetSpellByID`](#gametooltipsetspellbyidspellid): +adds a spell's full tooltip (name, cast time, cost, range, description) to +the *current* tooltip **without clearing** the existing lines. Works for any +`spellID`, learned or not. Lets you compose tooltips — e.g. a header line +plus a spell block. + +```lua +GameTooltip:SetOwner(UIParent, "ANCHOR_CURSOR") +GameTooltip:AddLine("Rank 3 grants:") +GameTooltip:AddSpellByID(133) -- Fireball, appended below the header +GameTooltip:Show() +``` + +Unlike `SetSpellByID`, this does **not** update what +[`GetSpell`](#gametooltipgetspell) reports — the appended spell isn't the +tooltip's "primary" spell, so `GetSpell` keeps reflecting whatever `SetX` +call (if any) built the base tooltip. Silent no-op for `spellID <= 0` or an +unknown spell. + +> Vanilla's tooltip builder only exposes "append" via its internal talent +> "next rank" preview, which emits a `Next rank:` header instead of the +> spell name; we overwrite that header line with the real name so the +> appended block reads normally. + ### `GameTooltip:GetItem()` Returns `(name, link, itemID)` for whichever item the tooltip is diff --git a/src/spell/Tooltip.cpp b/src/spell/Tooltip.cpp index 636d1b59..636174c0 100644 --- a/src/spell/Tooltip.cpp +++ b/src/spell/Tooltip.cpp @@ -55,6 +55,71 @@ static int __fastcall Script_GameTooltipSetSpellByID(void *L) { return 0; } +// `GameTooltip:AddSpellByID(spellID)` — APPENDS a spell's tooltip to the +// current one instead of clearing+rebuilding (the `SetSpellByID` behavior). +// The natural pair to `SetSpellByID`; lets callers compose tooltips like +// `AddLine(talentName) + AddSpellByID(rankSpellID)`. +// +// `BuildSpellTooltip`'s last arg (`param_7`) gates the clear: `0` calls the +// per-tooltip Clear (`FUN_00530050`) first — that's `SetSpellByID`; non-zero +// SKIPS the clear and appends. The append path is the engine's talent +// "next rank" preview, so it (a) emits a `"\nNext rank:"` header line +// instead of the spell name, and (b) does NOT stash the displayed spellID at +// `+0x39C` (so `GetSpell` keeps reflecting the base tooltip — correct for an +// append). We fix (a) by overwriting that header line — which lands at index +// `numLines`-before-the-build (0-based) — with the real spell name via the +// line pool's left-text FontString. Verified against `FUN_0052E610`. +void AppendByID(void *L, int spellID) { + if (spellID <= 0) + return; + void *tooltipObj = Game::Lua::ResolveObject(L, 1); + if (tooltipObj == nullptr) + return; + auto *tt = static_cast(tooltipObj); + + const int before = *reinterpret_cast( + tt + Offsets::OFF_GAMETOOLTIP_NUM_LINES); + + auto BuildSpellTooltip = + reinterpret_cast(Offsets::FUN_GAMETOOLTIP_BUILD_SPELL_TOOLTIP); + BuildSpellTooltip(tooltipObj, spellID, 0, 0, 0, 0, 0, /*param_7 (append)=*/1); + + const int after = *reinterpret_cast( + tt + Offsets::OFF_GAMETOOLTIP_NUM_LINES); + if (after <= before) + return; // nothing appended (bad spellID / missing Spell.dbc record) + + const uint8_t *record = Spell::Lookup::RecordForID(spellID); + if (record == nullptr) + return; + const int locale = *reinterpret_cast(Offsets::VAR_LOCALE_INDEX); + const char *name = *reinterpret_cast( + record + OFF_SPELL_NAME + locale * 4); + if (name == nullptr || name[0] == '\0') + return; + + // Left-text FontString array: descriptor at +0x324, data ptr at +0x8. + auto **textLeft = *reinterpret_cast( + tt + Offsets::OFF_GAMETOOLTIP_TEXTLEFT_DESC + 8); + if (textLeft == nullptr) + return; + void *fs = textLeft[before]; + if (fs == nullptr) + return; + using SetText_t = void(__thiscall *)(void *fs, const char *text, int flag); + reinterpret_cast(Offsets::FUN_FONTSTRING_SET_TEXT)(fs, name, 0); +} + +static int __fastcall Script_GameTooltipAddSpellByID(void *L) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE || + !Game::Lua::IsNumber(L, 2)) { + Game::Lua::Error(L, "Usage: GameTooltip:AddSpellByID(spellID)"); + return 0; + } + AppendByID(L, static_cast(Game::Lua::ToNumber(L, 2))); + return 0; +} + // `GameTooltip:GetSpell()` → (name, rank, spellID) for whichever spell // the tooltip is currently displaying, or nothing if it isn't showing // a spell. BuildSpellTooltip writes the spellID to `tooltip+0x39C`; @@ -126,6 +191,7 @@ static int __fastcall Script_GameTooltipHasSpell(void *L) { static const Game::Lua::FrameMethodEntry g_methods[] = { {"SetSpellByID", &Script_GameTooltipSetSpellByID}, + {"AddSpellByID", &Script_GameTooltipAddSpellByID}, {"GetSpell", &Script_GameTooltipGetSpell}, {"HasSpell", &Script_GameTooltipHasSpell}, }; diff --git a/src/spell/Tooltip.h b/src/spell/Tooltip.h index 5f354c1b..fb19b713 100644 --- a/src/spell/Tooltip.h +++ b/src/spell/Tooltip.h @@ -26,4 +26,13 @@ namespace Spell::Tooltip { // so we render the rank-1 spell tooltip instead). void ShowByID(void *L, int spellID); +// Like `ShowByID` but APPENDS the spell's tooltip to the current one +// (no clear), and overwrites the engine's talent "next rank" header with +// the real spell name. Backs `GameTooltip:AddSpellByID`. Silent no-op if +// `spellID <= 0`, stack[1] isn't a frame object, or the build added no +// lines. Does not stash the displayed spellID (so `GetSpell` still +// reflects the base tooltip). Useful for cross-class talent tooltips +// (`AddLine(talentName) + AppendByID(rankSpellID)`). +void AppendByID(void *L, int spellID); + } // namespace Spell::Tooltip