From 72fa36956edbf009a0a788f81aead1a461a7ac65 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sat, 25 Jul 2026 22:57:07 -0500 Subject: [PATCH] tooltip: add OnTooltipSetSpell/Unit/GameObject scripts Generalize the item-only Tooltip::SetItemEvent into Tooltip::SetEvents, backing all four object tooltip scripts through one shared fire path (stack save/restore + recursion guard + Suppressor). One resolver co-hook recognizes all four OnTooltipSet* names (the per-tooltip cell now holds a slot per kind), plus one builder co-hook per kind firing its script after the original build: - Spell -> FUN_0052E610 (SetSpell/SetSpellByID/SetTalent/SetShapeshift; 7 stack args, RET 0x1c) - Unit -> FUN_00529fe0 (SetUnit + mouseover; 1 arg, returns an int the caller tests -> co-hook forwards it) - GameObject -> FUN_0052aa20 (GO hover populator; 1 arg) Each hook's declared stack-arg count matches its callee RET N (verified from disassembly) so the caller's stack isn't corrupted. Auras deliberately do NOT fire OnTooltipSetSpell: SetUnitBuff/Debuff/ PlayerBuff funnel through a separate aura-tooltip builder (FUN_0052f880) that bypasses the spell builder -- matching retail, where unit auras are a distinct tooltip data type. Verified in-game: all three new scripts fire (spell hover, unit mouseover, gameobject mouseover), item still fires, no crash. --- README.md | 2 +- docs/API.md | 55 +++-- src/Offsets.h | 17 ++ src/tooltip/CompareItem.cpp | 4 +- src/tooltip/SetEvents.cpp | 241 ++++++++++++++++++++ src/tooltip/{SetItemEvent.h => SetEvents.h} | 14 +- src/tooltip/SetItemEvent.cpp | 168 -------------- 7 files changed, 303 insertions(+), 198 deletions(-) create mode 100644 src/tooltip/SetEvents.cpp rename src/tooltip/{SetItemEvent.h => SetEvents.h} (62%) delete mode 100644 src/tooltip/SetItemEvent.cpp diff --git a/README.md b/README.md index 84c47af1..301f6387 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: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:SetTotem`, `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:SetTotem`, `GameTooltip:SetUnitAura`, `OnTooltipSetItem`, `OnTooltipSetSpell`, `OnTooltipSetUnit`, `OnTooltipSetGameObject` (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` | diff --git a/docs/API.md b/docs/API.md index 18067e07..44dcb91b 100644 --- a/docs/API.md +++ b/docs/API.md @@ -166,7 +166,7 @@ build instructions. - [`GameTooltip:SetInventoryItemByID(itemID)`](#gametooltipsetinventoryitembyiditemid) - [`GameTooltip:SetHyperlinkCompareItem("itemLink" [, offset, shiftButton, comparisonTooltip])`](#gametooltipsethyperlinkcompareitemitemlink--offset-shiftbutton-comparisontooltip) - [`GameTooltip:IsEquippedItem()`](#gametooltipisequippeditem) - - [`OnTooltipSetItem` script](#ontooltipsetitem-script) + - [`OnTooltipSet*` scripts (Item / Spell / Unit / GameObject)](#ontooltipset-scripts) - [`GameTooltip:SetItemByGUID(itemGUID)`](#gametooltipsetitembyguiditemguid) - [`GameTooltip:SetEquipmentSet(name)`](#gametooltipsetequipmentsetname) - [`GameTooltip:SetTotem(slot)`](#gametooltipsettotemslot) @@ -4158,18 +4158,27 @@ GameTooltip:SetBagItem(0, 1) if GameTooltip:IsEquippedItem() then ... end -- false unless that item is also worn ``` -### `OnTooltipSetItem` script +### `OnTooltipSet*` scripts -A real frame script — settable with the standard `SetScript` / `GetScript` / -`HookScript` — that fires whenever a tooltip's **item** is set. Backports the -modern tooltip script so addons annotate item tooltips by hooking one script -instead of wrapping every `Set*` method. - -Fires for every item-setting path (`SetBagItem`, `SetInventoryItem`, -`SetHyperlink` for an `item:` link, `SetMerchantItem`, `SetAuctionItem`, -`SetItemByID`, …). Available on all GameTooltip-type frames (`GameTooltip`, +`OnTooltipSetItem` / `OnTooltipSetSpell` / `OnTooltipSetUnit` / +`OnTooltipSetGameObject` — real frame scripts, settable with the standard +`SetScript` / `GetScript` / +`HookScript` — that fire whenever a tooltip's **item**, **spell**, **unit**, or +**gameobject** is set. Backport the modern tooltip scripts so addons annotate +tooltips by hooking one script per object type instead of wrapping every +`Set*` method. Available on all GameTooltip-type frames (`GameTooltip`, `ItemRefTooltip`, `ShoppingTooltip1/2`, `AtlasLootTooltip`, …). +| Script | Fires after any of | +|---|---| +| `OnTooltipSetItem` | `SetBagItem`, `SetInventoryItem`, `SetHyperlink` (`item:`), `SetMerchantItem`, `SetAuctionItem`, `SetItemByID`, … | +| `OnTooltipSetSpell` | `SetSpell`, `SetSpellByID`, `SetTalent`, `SetShapeshift` | +| `OnTooltipSetUnit` | `SetUnit` and unit mouseover | +| `OnTooltipSetGameObject` | gameobject mouseover (herb/ore nodes, chests, mailboxes, signs) | + +Vanilla only shipped `OnTooltipAddMoney` / `OnTooltipCleared` / +`OnTooltipSetDefaultAnchor`; these four are the backported set. + The handler receives the tooltip as the **global `this`**, the 1.12 frame-script convention — *not* a `self` argument (like every built-in vanilla script: `OnShow`, `OnEvent`, `OnTooltipCleared`, …). Modern-style @@ -4178,21 +4187,25 @@ script: `OnShow`, `OnEvent`, `OnTooltipCleared`, …). Modern-style ```lua GameTooltip:HookScript("OnTooltipSetItem", function() local name, link, id = this:GetItem() -- the tooltip is the global `this` - if id then - this:AddLine("ID: " .. id, 0.6, 0.6, 0.6) - end + if id then this:AddLine("ID: " .. id, 0.6, 0.6, 0.6) end +end) + +GameTooltip:HookScript("OnTooltipSetUnit", function() + local name, unit = this:GetUnit() + if unit then this:AddLine(UnitClassBase(unit), 0.6, 0.6, 0.6) end end) ``` -Vanilla only shipped `OnTooltipAddMoney` / `OnTooltipCleared` / -`OnTooltipSetDefaultAnchor`; `OnTooltipSetSpell` / `OnTooltipSetUnit` are not -yet provided. +**Auras don't fire `OnTooltipSetSpell`** — `SetUnitBuff` / `SetUnitDebuff` / +`SetPlayerBuff` / `SetUnitAura` use a separate aura-tooltip builder, so hovering +a buff/debuff does not trigger the spell script. This matches retail, where unit +auras are a distinct tooltip data type rather than a spell. -**Caveat — the event fires from inside the item-tooltip build.** Lightweight -handler work is fine: reading `this:GetItem()`, `this:AddLine(...)`, printing, -etc. But avoid *re-entrant* tooltip rebuilds from the handler — e.g. -`GameTooltip_ShowCompareItem()`, or a `Set*` call on another tooltip. Because -the handler runs mid-build, re-entering the tooltip / FrameScript machinery can +**Caveat — the event fires from inside the tooltip build.** Lightweight handler +work is fine: reading `this:GetItem()` / `this:GetUnit()`, `this:AddLine(...)`, +printing, etc. But avoid *re-entrant* tooltip rebuilds from the handler — e.g. +`GameTooltip_ShowCompareItem()`, or a `Set*` call on another tooltip. Because the +handler runs mid-build, re-entering the tooltip / FrameScript machinery can collide with other DLLs that hook the same Lua paths (nampower, SuperWoW, weirdutils, …) and crash the client. If you need heavy work like that, defer it to the next frame: diff --git a/src/Offsets.h b/src/Offsets.h index 8df48c91..7721a604 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -140,6 +140,23 @@ enum Offsets { // and call the inner invoker, which is self-contained. FUN_GAMETOOLTIP_SCRIPT_RESOLVER = 0x005295D0, FUN_FRAME_INVOKE_SCRIPT = 0x00704D50, + // The other per-object tooltip builders, co-hooked the same way as + // FUN_GAMETOOLTIP_BUILD_ITEM to back OnTooltipSetSpell / OnTooltipSetUnit / + // OnTooltipSetGameObject (see Tooltip::SetEvents). Each is the single funnel + // its Set*/mouseover paths converge on and clears the tooltip + // (FUN_00530050) before repopulating, so firing after it covers every way + // that object type gets set: + // - Spell FUN_GAMETOOLTIP_BUILD_SPELL_TOOLTIP (0x0052E610, 7 stack args, + // RET 0x1c) — SetSpell/SetSpellByID/SetTalent/SetShapeshift funnel. + // - Unit (0x00529fe0, __thiscall(self, guid*), RET 4) — SetUnit + + // the two engine mouseover paths (FUN_004919d0 / FUN_00492890). + // Returns an int the caller (Script_GameTooltip_SetUnit) tests, so the + // co-hook must forward the original's return value. + // - GameObject (0x0052aa20, __thiscall(self, guid*), RET 4) — the GO + // hover populator; resolves the GO (OBJ_TYPE_GAMEOBJECT) and writes the + // GO field at +0x370. + FUN_GAMETOOLTIP_BUILD_UNIT = 0x00529FE0, + FUN_GAMETOOLTIP_BUILD_GAMEOBJECT = 0x0052AA20, FUN_GAMETOOLTIP_ADD_LINE = 0x00530270, // __thiscall(self, left, right, lColorBGRA*, rColorBGRA*, wrap) OFF_GAMETOOLTIP_NUM_LINES = 0x31C, // int — live line count (AddLine index; +0x320 is the cap) // The displayed item's identity is read via the existing diff --git a/src/tooltip/CompareItem.cpp b/src/tooltip/CompareItem.cpp index 3a7f7527..cdc83b2a 100644 --- a/src/tooltip/CompareItem.cpp +++ b/src/tooltip/CompareItem.cpp @@ -62,7 +62,7 @@ #include "item/Location.h" #include "item/StatAccum.h" #include "item/TooltipItem.h" -#include "tooltip/SetItemEvent.h" +#include "tooltip/SetEvents.h" #include "ui/ColorData.h" #include @@ -598,7 +598,7 @@ int __fastcall Script_SetHyperlinkCompareItem(void *L) { // this internal build so a handler doesn't run mid-way through the // line-shift below (it fires for the *hovered* item's real Set* instead). { - Tooltip::SetItemEvent::Suppressor noEvent; + Tooltip::SetEvents::Suppressor noEvent; reinterpret_cast(Offsets::FUN_GAMETOOLTIP_BUILD_ITEM)( self, static_cast(equippedID), &equippedGUID, &equippedGUID, 0, 0, 0, 0, 0, 0); } diff --git a/src/tooltip/SetEvents.cpp b/src/tooltip/SetEvents.cpp new file mode 100644 index 00000000..583106ea --- /dev/null +++ b/src/tooltip/SetEvents.cpp @@ -0,0 +1,241 @@ +// 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:HookScript("OnTooltipSet{Item,Spell,Unit,GameObject}", fn)` — +// backport of the modern tooltip scripts that fire whenever a tooltip's item / +// spell / unit / gameobject is set, so addons can annotate tooltips by hooking +// one script instead of wrapping every Set* method. Fully native — real frame +// scripts, settable via the standard SetScript / GetScript / HookScript. +// +// 1.12 already has the machinery for tooltip scripts, just not these ones: +// +// - The CGGameTooltip vtable resolver FUN_GAMETOOLTIP_SCRIPT_RESOLVER maps a +// script name to its handler slot on the tooltip (OnTooltipCleared etc., +// each an 8-byte {handler, context} pair). SetScript/GetScript/HookScript +// all go through it. We co-hook it: for our OnTooltipSet* names (which the +// engine doesn't know) we hand back a pointer to a C-side per-tooltip cell +// — the tooltip object (alloc size 0x460) has no free 8-byte slot, and the +// resolver's only contract is "return the address the script lives at", so +// external storage works exactly like an in-object slot. +// +// - 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, +// after the build, fire the matching handler via FUN_FRAME_INVOKE_SCRIPT +// (the same no-arg runner the clear uses for OnTooltipCleared) with the +// tooltip as `self`. +// +// Suppressed during our own Tooltip::Compare equipped-item build (see +// Suppressor), and guarded against a handler that re-enters a builder. + +#include "SetEvents.h" + +#include "Game.h" +#include "Offsets.h" + +#include + +namespace Tooltip::SetEvents { + +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 }; + +// Script names, matched case-insensitively against the resolver's input. +constexpr const char *kNamesLower[SK_COUNT] = { + "ontooltipsetitem", + "ontooltipsetspell", + "ontooltipsetunit", + "ontooltipsetgameobject", +}; + +// Per-tooltip storage: one 8-byte {handler, context} slot per script kind. +// Tooltips are few and permanent (GameTooltip, ItemRefTooltip, +// ShoppingTooltip1/2, AtlasLootTooltip, WorldMapTooltip, plus the odd addon +// tooltip), so a small fixed table with a linear scan is plenty and gives each +// cell a stable address to hand out. +struct Cell { + void *tooltip; + uint32_t slot[SK_COUNT][2]; // [kind] = {handler, exec context} +}; +constexpr int kMaxTooltips = 32; +Cell g_cells[kMaxTooltips]; +int g_cellCount = 0; + +uint32_t *SlotFor(void *tooltip, int kind, bool create) { + for (int i = 0; i < g_cellCount; ++i) + if (g_cells[i].tooltip == tooltip) + return g_cells[i].slot[kind]; + if (!create || g_cellCount >= kMaxTooltips) + return nullptr; + Cell &c = g_cells[g_cellCount++]; + c.tooltip = tooltip; + for (auto &s : c.slot) { + s[0] = 0; + s[1] = 0; + } + return c.slot[kind]; +} + +bool EqualsIgnoreCase(const char *s, const char *literal) { + if (s == nullptr) + return false; + for (;; ++s, ++literal) { + unsigned char a = static_cast(*s); + const unsigned char b = static_cast(*literal); + if (a >= 'A' && a <= 'Z') + a = static_cast(a + 32); + if (a != b) + return false; + if (b == 0) + return true; + } +} + +// --- Suppression / recursion state ------------------------------------- +int g_suppress = 0; // > 0 while our own compare build runs +int g_firing = 0; // recursion guard around any handler fire + +// The engine's real script invoker: binds the global `this` = frame and runs +// the handler under its own protected lua_pcall. We call it directly rather +// than the FUN_00702690 wrapper, whose exec-context mutation is unsafe from the +// deep nested Lua->C->Lua stack our co-hook fires within — see Offsets.h. +using InvokeScript_t = void(__fastcall *)(uint32_t handler, void *frame); + +// Fire the handler (if any) for `kind` on `self`. +// +// We fire from inside a builder, which is itself mid-execution of the engine's +// Script_Set* (and, in the wild, nested under addon Set* wrappers + several +// other DLLs' hooks). The invoker pushes onto the Lua stack and does NOT +// restore the top to where the outer, still-running C code left it — harmless +// from the engine's top-level dispatch, but from here it shifts the stack and +// the outer code (and other DLLs' hooks) then index garbage → deref → crash. +// Snapshot and restore the stack top around the fire so the outer code sees +// exactly the stack it left. +void FireScript(void *self, int kind) { + if (g_suppress > 0 || g_firing > 0) + return; + uint32_t *slot = SlotFor(self, kind, /*create*/ false); + if (slot == nullptr || slot[0] == 0) + return; + void *L = Game::Lua::State(); + const int savedTop = (L != nullptr) ? Game::Lua::GetTop(L) : 0; + ++g_firing; + reinterpret_cast(Offsets::FUN_FRAME_INVOKE_SCRIPT)(slot[0], self); + --g_firing; + if (L != nullptr) + Game::Lua::SetTop(L, savedTop); +} + +// --- Resolver co-hook (CGGameTooltip vtable script-name → slot) --------- +using Resolver_t = int(__fastcall *)(void *self, void *edx, const char *name); +Resolver_t g_resolverOriginal = nullptr; + +int __fastcall Resolver_h(void *self, void *edx, const char *name) { + const int engineSlot = g_resolverOriginal(self, edx, name); + if (engineSlot != 0) // a base-frame or existing tooltip script — leave it + return engineSlot; + for (int k = 0; k < SK_COUNT; ++k) + if (EqualsIgnoreCase(name, kNamesLower[k])) + return reinterpret_cast(SlotFor(self, k, /*create*/ true)); + return 0; +} + +// --- Builder co-hooks -------------------------------------------------- +// Each builder is a __thiscall funnel modelled as __fastcall(self /*ecx*/, +// edx /*unused reg placeholder*/, ...real stack args...). The declared stack +// arg count MUST match the callee's RET N or the caller's stack is corrupted. +// Builders whose callers use the return value (spell, unit) are declared to +// return it so the co-hook forwards it. + +// Item — FUN_GAMETOOLTIP_BUILD_ITEM, 9 stack args, return unused. +using BuildItem_t = void(__fastcall *)(void *self, void *edx, uint32_t itemID, + const void *guid, const void *guid2, int a4, + int a5, int a6, int headerFlag, int a8, int a9); +BuildItem_t g_buildItemOriginal = nullptr; + +void __fastcall BuildItem_h(void *self, void *edx, uint32_t itemID, const void *guid, + const void *guid2, int a4, int a5, int a6, int headerFlag, + int a8, int a9) { + g_buildItemOriginal(self, edx, itemID, guid, guid2, a4, a5, a6, headerFlag, a8, a9); + FireScript(self, SK_ITEM); +} + +// Spell — FUN_GAMETOOLTIP_BUILD_SPELL_TOOLTIP, 7 stack args, returns an int. +using BuildSpell_t = int(__fastcall *)(void *self, void *edx, int spellID, int a2, + int a3, int isPet, int a5, int a6, int a7); +BuildSpell_t g_buildSpellOriginal = nullptr; + +int __fastcall BuildSpell_h(void *self, void *edx, int spellID, int a2, int a3, + int isPet, int a5, int a6, int a7) { + const int ret = g_buildSpellOriginal(self, edx, spellID, a2, a3, isPet, a5, a6, a7); + FireScript(self, SK_SPELL); + return ret; +} + +// Unit — FUN_GAMETOOLTIP_BUILD_UNIT, 1 stack arg (guid), returns an int the +// caller (Script_GameTooltip_SetUnit) tests. +using BuildUnit_t = int(__fastcall *)(void *self, void *edx, const void *guid); +BuildUnit_t g_buildUnitOriginal = nullptr; + +int __fastcall BuildUnit_h(void *self, void *edx, const void *guid) { + const int ret = g_buildUnitOriginal(self, edx, guid); + FireScript(self, SK_UNIT); + return ret; +} + +// GameObject — FUN_GAMETOOLTIP_BUILD_GAMEOBJECT, 1 stack arg (guid), void. +using BuildGO_t = void(__fastcall *)(void *self, void *edx, const void *guid); +BuildGO_t g_buildGOOriginal = nullptr; + +void __fastcall BuildGO_h(void *self, void *edx, const void *guid) { + g_buildGOOriginal(self, edx, guid); + FireScript(self, SK_GAMEOBJECT); +} + +static const Game::HookAutoRegister _resolverHook{ + Offsets::FUN_GAMETOOLTIP_SCRIPT_RESOLVER, + reinterpret_cast(&Resolver_h), + reinterpret_cast(&g_resolverOriginal)}; + +static const Game::HookAutoRegister _buildItemHook{ + Offsets::FUN_GAMETOOLTIP_BUILD_ITEM, + reinterpret_cast(&BuildItem_h), + reinterpret_cast(&g_buildItemOriginal)}; + +static const Game::HookAutoRegister _buildSpellHook{ + Offsets::FUN_GAMETOOLTIP_BUILD_SPELL_TOOLTIP, + reinterpret_cast(&BuildSpell_h), + reinterpret_cast(&g_buildSpellOriginal)}; + +static const Game::HookAutoRegister _buildUnitHook{ + Offsets::FUN_GAMETOOLTIP_BUILD_UNIT, + reinterpret_cast(&BuildUnit_h), + reinterpret_cast(&g_buildUnitOriginal)}; + +static const Game::HookAutoRegister _buildGOHook{ + Offsets::FUN_GAMETOOLTIP_BUILD_GAMEOBJECT, + reinterpret_cast(&BuildGO_h), + reinterpret_cast(&g_buildGOOriginal)}; + +} // namespace + +Suppressor::Suppressor() { ++g_suppress; } +Suppressor::~Suppressor() { + if (g_suppress > 0) + --g_suppress; +} + +} // namespace Tooltip::SetEvents diff --git a/src/tooltip/SetItemEvent.h b/src/tooltip/SetEvents.h similarity index 62% rename from src/tooltip/SetItemEvent.h rename to src/tooltip/SetEvents.h index 3a721765..bb4c865d 100644 --- a/src/tooltip/SetItemEvent.h +++ b/src/tooltip/SetEvents.h @@ -13,12 +13,14 @@ #pragma once -namespace Tooltip::SetItemEvent { +namespace Tooltip::SetEvents { -// RAII guard that suppresses OnTooltipSetItem firing for the duration of an -// engine item-build we invoke *internally* (Tooltip::Compare building the -// equipped item into a shopping tooltip). Without it, a handler would run in -// the middle of our line-shift / delta rendering and see a half-built tooltip. +// RAII guard that suppresses tooltip set-event firing (OnTooltipSetItem etc.) +// for the duration of an engine build we invoke *internally* — currently +// Tooltip::Compare building the equipped item into a shopping tooltip. Without +// it, a handler would run in the middle of our line-shift / delta rendering and +// see a half-built tooltip. Only the item builder runs during that internal +// build, so in practice this gates OnTooltipSetItem; it's global for safety. // Nestable (ref-counted). struct Suppressor { Suppressor(); @@ -27,4 +29,4 @@ struct Suppressor { Suppressor &operator=(const Suppressor &) = delete; }; -} // namespace Tooltip::SetItemEvent +} // namespace Tooltip::SetEvents diff --git a/src/tooltip/SetItemEvent.cpp b/src/tooltip/SetItemEvent.cpp deleted file mode 100644 index b7c754ea..00000000 --- a/src/tooltip/SetItemEvent.cpp +++ /dev/null @@ -1,168 +0,0 @@ -// 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:HookScript("OnTooltipSetItem", fn)` — backport of the modern -// tooltip script that fires whenever a tooltip's item is set, so addons can -// annotate item tooltips by hooking one script instead of wrapping every -// Set* method (the pattern AtlasLoot resorts to in vanilla, rebuilding the -// tooltip and clobbering its item linkage). Fully native — a real frame -// script, settable via the standard SetScript / GetScript / HookScript. -// -// 1.12 already has the machinery for tooltip scripts, just not this one: -// -// - The CGGameTooltip vtable resolver FUN_GAMETOOLTIP_SCRIPT_RESOLVER maps a -// script name to its handler slot on the tooltip (OnTooltipCleared etc., -// each an 8-byte {handler, context} pair). SetScript/GetScript/HookScript -// all go through it. We co-hook it: for "OnTooltipSetItem" (a name the -// engine doesn't know) we hand back a pointer to a C-side per-tooltip cell -// — the tooltip object (alloc size 0x460) has no free 8-byte slot, and the -// resolver's only contract is "return the address the script lives at", so -// external storage works exactly like an in-object slot. -// -// - Every item setter (SetHyperlink item:, SetBagItem, SetInventoryItem, -// SetMerchantItem, SetAuctionItem, …) funnels through the shared item -// builder FUN_GAMETOOLTIP_BUILD_ITEM. We co-hook it and, after the build, -// fire the handler via FUN_FRAME_RUN_SCRIPT (the same no-arg runner the -// clear uses for OnTooltipCleared) with the tooltip as `self`. -// -// Suppressed during our own Tooltip::Compare equipped-item build (see -// Suppressor), and guarded against a handler that re-enters the builder. - -#include "SetItemEvent.h" - -#include "Game.h" -#include "Offsets.h" - -#include - -namespace Tooltip::SetItemEvent { - -namespace { - -// Per-tooltip 8-byte handler slot {handler, context}. Tooltips are few and -// permanent (GameTooltip, ItemRefTooltip, ShoppingTooltip1/2, AtlasLootTooltip, -// WorldMapTooltip, plus the odd addon tooltip), so a small fixed table with a -// linear scan is plenty and gives each cell a stable address to hand out. -struct Cell { - void *tooltip; - uint32_t slot[2]; // [0] = handler, [1] = exec context (written by SetScript) -}; -constexpr int kMaxTooltips = 32; -Cell g_cells[kMaxTooltips]; -int g_cellCount = 0; - -uint32_t *CellFor(void *tooltip, bool create) { - for (int i = 0; i < g_cellCount; ++i) - if (g_cells[i].tooltip == tooltip) - return g_cells[i].slot; - if (!create || g_cellCount >= kMaxTooltips) - return nullptr; - Cell &c = g_cells[g_cellCount++]; - c.tooltip = tooltip; - c.slot[0] = 0; - c.slot[1] = 0; - return c.slot; -} - -bool EqualsIgnoreCase(const char *s, const char *literal) { - if (s == nullptr) - return false; - for (;; ++s, ++literal) { - unsigned char a = static_cast(*s); - const unsigned char b = static_cast(*literal); - if (a >= 'A' && a <= 'Z') - a = static_cast(a + 32); - if (a != b) - return false; - if (b == 0) - return true; - } -} - -constexpr char kScriptNameLower[] = "ontooltipsetitem"; // matched case-insensitively - -// --- Suppression / recursion state ------------------------------------- -int g_suppress = 0; // > 0 while our own compare build runs -int g_firing = 0; // recursion guard around the handler fire - -// --- Resolver co-hook (CGGameTooltip vtable script-name → slot) --------- -using Resolver_t = int(__fastcall *)(void *self, void *edx, const char *name); -Resolver_t g_resolverOriginal = nullptr; - -int __fastcall Resolver_h(void *self, void *edx, const char *name) { - const int engineSlot = g_resolverOriginal(self, edx, name); - if (engineSlot != 0) // a base-frame or existing tooltip script — leave it - return engineSlot; - if (EqualsIgnoreCase(name, kScriptNameLower)) - return reinterpret_cast(CellFor(self, /*create*/ true)); - return 0; -} - -// --- Item-builder co-hook (fires the handler after the item is set) ----- -using Build_t = void(__fastcall *)(void *self, void *edx, uint32_t itemID, const void *guid, - const void *guid2, int a4, int a5, int a6, int headerFlag, - int a8, int a9); -Build_t g_buildOriginal = nullptr; - -// The engine's real script invoker: binds the global `this` = frame and runs -// the handler under its own protected lua_pcall. We call it directly rather -// than the FUN_00702690 wrapper, whose exec-context mutation is unsafe from the -// deep nested Lua->C->Lua stack our co-hook fires within — see Offsets.h. -using InvokeScript_t = void(__fastcall *)(uint32_t handler, void *frame); - -void __fastcall Build_h(void *self, void *edx, uint32_t itemID, const void *guid, - const void *guid2, int a4, int a5, int a6, int headerFlag, int a8, - int a9) { - g_buildOriginal(self, edx, itemID, guid, guid2, a4, a5, a6, headerFlag, a8, a9); - if (g_suppress > 0 || g_firing > 0) - return; - uint32_t *slot = CellFor(self, /*create*/ false); - if (slot == nullptr || slot[0] == 0) - return; - - // We fire from inside the item builder, which is itself mid-execution of - // the engine's Script_Set* (and, in the wild, nested under addon Set* - // wrappers + several other DLLs' hooks). The invoker pushes onto the Lua - // stack and does NOT restore the top to where the outer, still-running C - // code left it — harmless from the engine's top-level dispatch, but from - // here it shifts the stack and the outer code (and other DLLs' hooks) then - // index garbage → deref → crash. Snapshot and restore the stack top around - // the fire so the outer code sees exactly the stack it left. - void *L = Game::Lua::State(); - const int savedTop = (L != nullptr) ? Game::Lua::GetTop(L) : 0; - ++g_firing; - reinterpret_cast(Offsets::FUN_FRAME_INVOKE_SCRIPT)(slot[0], self); - --g_firing; - if (L != nullptr) - Game::Lua::SetTop(L, savedTop); -} - -static const Game::HookAutoRegister _resolverHook{ - Offsets::FUN_GAMETOOLTIP_SCRIPT_RESOLVER, - reinterpret_cast(&Resolver_h), - reinterpret_cast(&g_resolverOriginal)}; - -static const Game::HookAutoRegister _buildHook{ - Offsets::FUN_GAMETOOLTIP_BUILD_ITEM, - reinterpret_cast(&Build_h), - reinterpret_cast(&g_buildOriginal)}; - -} // namespace - -Suppressor::Suppressor() { ++g_suppress; } -Suppressor::~Suppressor() { - if (g_suppress > 0) - --g_suppress; -} - -} // namespace Tooltip::SetItemEvent