diff --git a/README.md b/README.md index db401f05..78e15ee0 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,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: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) | +| [GameTooltip](docs/API.md#gametooltip) | `GameTooltip:AddSpellByID`, `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: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/TODO.md b/TODO.md index 11ed3ea8..5fce00c1 100644 --- a/TODO.md +++ b/TODO.md @@ -1740,7 +1740,14 @@ is a thin wrapper. If they're inlined, we'd either need to skip the clear ourselves (call after some no-op state setup) or manually call the line-emission primitives. -## ~~61. `GameTooltip:GetItem()` / `GetSpell()` / `GetUnit()`~~ — `GetItem` + `GetSpell` DONE; `GetUnit` skipped +## ~~61. `GameTooltip:GetItem()` / `GetSpell()` / `GetUnit()`~~ — DONE + +`GetUnit` shipped later in [src/unit/Tooltip.cpp](src/unit/Tooltip.cpp): +`SetUnit` is re-registered in front of the engine's (no MinHook) to +stage the token, and the token binds in the unit-builder co-hook that +`Tooltip::SetEvents` already owned. The builder has three callers: +SetUnit, the world-mouseover setter (→ `"mouseover"`), and an in-place +refresh (keeps the token). The notes below are the original skip. Modern query methods that return what the tooltip is currently showing: diff --git a/docs/API.md b/docs/API.md index aeeb8c23..4617d014 100644 --- a/docs/API.md +++ b/docs/API.md @@ -306,7 +306,7 @@ build instructions. - [`GameTooltip:GetItem()`](#gametooltipgetitem) - [`GameTooltip:GetSpell()`](#gametooltipgetspell) - [`GameTooltip:HasItem()` / `GameTooltip:HasSpell()`](#gametooltiphasitem--gametooltiphasspell) - - [`GameTooltip:GetUnitGUID()` / `GameTooltip:HasUnit()`](#gametooltipgetunitguid--gametooltiphasunit) + - [`GameTooltip:GetUnit()` / `GameTooltip:GetUnitGUID()` / `GameTooltip:HasUnit()`](#gametooltipgetunit--gametooltipgetunitguid--gametooltiphasunit) - [`GameTooltip:GetGameObject()` / `GameTooltip:HasGameObject()`](#gametooltipgetgameobject--gametooltiphasgameobject) - [`GameTooltip:GetOwner()`](#gametooltipgetowner) @@ -7118,44 +7118,46 @@ if GameTooltip:HasItem() then end ``` -### `GameTooltip:GetUnitGUID()` / `GameTooltip:HasUnit()` +### `GameTooltip:GetUnit()` / `GameTooltip:GetUnitGUID()` / `GameTooltip:HasUnit()` -`GetUnitGUID()` returns `(name, guidString)` for whichever unit the -tooltip is currently displaying, or nothing if it isn't showing a -unit. Return order mirrors `GameTooltip:GetUnit()` (name -first) — so addons porting from -`local name, unit = ttip:GetUnit()` can swap to `GetUnitGUID` and -keep their existing destructuring. `name` is the unit's display name -— the same string that appears in the tooltip header, or one of the -engine's `"UNKNOWNOBJECT"` / `"Unknown Being"` fallbacks for a remote -unit whose info hasn't been queried yet. `guidString` is the -canonical `"0xHHHHHHHHLLLLLLLL"` format returned by -[`UnitGUID(unit)`](#unitguidunit). +`GetUnit()` returns `(name, unitToken)` for the unit that the tooltip +shows. It returns nothing if the tooltip does not show a unit. -`HasUnit()` is a boolean companion — returns `true` if the tooltip is -currently displaying a unit. +- `name` is the unit's display name. This is the same string that is + in the tooltip header. For a remote unit that is not in the cache + yet, it is `"UNKNOWNOBJECT"` or `"Unknown Being"`. +- `unitToken` is the string that was given to `SetUnit`, as it was + given (for example `"target"`, `"party2"`, `"focus"`). For the + tooltip that shows when the cursor is on a unit in the world, it is + `"mouseover"`. For the tooltip that shows when the cursor is on a + unit frame with a `unit` attribute, it is the frame's unit (for + example `"party1"`). This token also works for a group member who + is offline or far away, but `"mouseover"` does not. + +`GetUnitGUID()` returns `(name, guidString)`. `guidString` has the +`"0xHHHHHHHHLLLLLLLL"` format of [`UnitGUID(unit)`](#unitguidunit). +Use it when you must identify the unit after its token changes. For +example, `"mouseover"` does not point to the unit after the cursor +moves away, but the GUID stays the same. + +`HasUnit()` returns `true` if the tooltip shows a unit. ```lua GameTooltip:SetUnit("target") -local name, guid = GameTooltip:GetUnitGUID() --- name = "Hogger" +local name, unit = GameTooltip:GetUnit() +-- name = "Hogger", unit = "target" + +local _, guid = GameTooltip:GetUnitGUID() -- guid = "0xF130001234..." (Creature) or "0x000000...ABC123" (Player) if GameTooltip:HasUnit() then - -- cheap predicate, no name-resolution work + -- fast check, no name lookup end ``` -> **Why `GetUnit()` returns a GUID, not a token.** A `(name, unitToken)` -> form would return the exact `"target"` / `"focus"` / `"mouseover"` / -> etc. string passed to `SetUnit`. But the engine drops the token at the -> `Script_GameTooltip_SetUnit` boundary — it converts the token to a -> 64-bit GUID and discards the original string. Reconstructing a -> plausible token by walking known tokens and reverse-matching by -> GUID is possible but lossy (multiple tokens can refer to the same -> GUID — `"target"` and `"raid1"` simultaneously, for instance), so -> we expose the GUID directly instead, which is what addons actually -> need for cross-referencing with `UnitGUID`, the NameCache, etc. +> **Note:** If a tooltip continues to show a unit from `SetUnit`, and +> the cursor then moves onto the same unit in the world, `GetUnit()` +> continues to return the `SetUnit` token, not `"mouseover"`. ### `GameTooltip:GetGameObject()` / `GameTooltip:HasGameObject()` diff --git a/src/Offsets.h b/src/Offsets.h index e625357c..976c753a 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -137,6 +137,13 @@ enum Offsets { // Macro-slot tooltip: `__thiscall(tooltip, uint macroID)` — clear + // SetText(entry+OFF_MACRO_NAME) + Show. Internal, not a script entry. FUN_GAMETOOLTIP_SET_MACRO = 0x0052B040, + // `Script_GameTooltip_SetUnit` (slot 31): self typecheck, lua_isstring(2), + // FUN_TOKEN_TO_GUID (raises on an unknown token), then — only for a + // non-zero GUID — FUN_GAMETOOLTIP_BUILD_UNIT. Always pushes nil (the + // builder returns 0 on every path). The token string is discarded after + // the resolve; `Unit::Tooltip` re-registers the name in front of it to + // keep the token for `GetUnit` and tail-calls it with the untouched stack. + FUN_SCRIPT_GAMETOOLTIP_SET_UNIT = 0x005349B0, FUN_SCRIPT_GAMETOOLTIP_SET_UNIT_BUFF = 0x00534AC0, // slot 32 FUN_SCRIPT_GAMETOOLTIP_SET_UNIT_DEBUFF = 0x00534E30, // slot 33 FUN_SCRIPT_GAMETOOLTIP_SET_TALENT = 0x00535170, // slot 34 @@ -5665,6 +5672,11 @@ enum Offsets { OFF_GROUP_MEMBER_STATUS_FLAGS = 0x08, GROUP_MEMBER_STATUS_ONLINE = 0x1, FUN_GROUP_MEMBER_SLOT_LOOKUP = 0x00496420, + // Inline member name in the slot record — `Script_UnitName` (0x00517020) + // pushes `slot + 8` for a GUID with no live object, as does the unit + // tooltip builder's no-object branch (0x00529FE0). This is why + // `UnitName("partyN")` answers for an offline member. + OFF_GROUP_MEMBER_SLOT_NAME = 0x08, // Health/power fields within the two group-member blocks — the // out-of-range fallback the engine's UnitHealth/UnitMana (and their Max diff --git a/src/frame/Attributes.cpp b/src/frame/Attributes.cpp index 1aadc60a..24da4b79 100644 --- a/src/frame/Attributes.cpp +++ b/src/frame/Attributes.cpp @@ -117,6 +117,7 @@ #include "tick/WorldTick.h" #include "unit/Focus.h" #include "unit/Identity.h" +#include "unit/Tooltip.h" #include #include @@ -390,20 +391,30 @@ void MouseoverTick() { const void *focus = CurrentMouseFocus(); uint64_t guid = 0; + std::string token; // a copy: handlers run below may rewrite the map if (focus != nullptr) { auto it = g_unitByFrame.find(focus); - if (it != g_unitByFrame.end()) - guid = Unit::Identity::GuidForToken( - it->second.c_str()); // live — follows target changes + if (it != g_unitByFrame.end()) { + token = it->second; + guid = Unit::Identity::GuidForToken(token.c_str()); // live — follows target changes + } } if (guid != g_lastSet) { + // The tooltip built below is for the frame's unit, so `GetUnit()` + // answers with the frame's token — "mouseover" would not resolve for + // a member with no live object (see Unit::Tooltip). + if (guid != 0) + Unit::Tooltip::StageToken( + Game::Read(static_cast(Offsets::VAR_GAMETOOLTIP_OBJECT_PTR)), + token.c_str()); CallEngineMouseover(guid); // sets slot + highlight + tooltip + event // Offline / far out-of-range party or raid member: the setter wrote the // GUID slot but skipped the tooltip (no live object). Build it from the // roster so hovering the frame still shows name / level / "Offline". if (guid != 0 && !GuidHasLiveUnit(guid)) BuildRosterUnitTooltip(guid); + Unit::Tooltip::ClearStagedToken(); g_lastSet = guid; } } diff --git a/src/tooltip/SetEvents.cpp b/src/tooltip/SetEvents.cpp index 15138c72..54accd21 100644 --- a/src/tooltip/SetEvents.cpp +++ b/src/tooltip/SetEvents.cpp @@ -42,6 +42,7 @@ #include "Game.h" #include "Offsets.h" +#include "unit/Tooltip.h" #include @@ -200,6 +201,7 @@ 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) { + Unit::Tooltip::BeforeBuild(self, static_cast(guid)); const int ret = g_buildUnitOriginal(self, edx, guid); FireScript(self, SK_UNIT); return ret; diff --git a/src/unit/Tooltip.cpp b/src/unit/Tooltip.cpp index 06438de7..6d28c79d 100644 --- a/src/unit/Tooltip.cpp +++ b/src/unit/Tooltip.cpp @@ -11,11 +11,15 @@ // You should have received a copy of the GNU General Public License along with // ClassicAPI. If not, see . +#include "Tooltip.h" + #include "Game.h" #include "Offsets.h" #include "aura/Data.h" #include "guid/Guid.h" #include "object/Resolve.h" +#include "unit/Identity.h" +#include "unit/TokenResolve.h" #include @@ -105,30 +109,174 @@ static int __fastcall Script_GameTooltipSetUnitAura(void *L) { return fn(L); } -// Vanilla 1.12 drops the unit-token string at the `Script_GameTooltip_SetUnit` -// boundary: the token is resolved to a 64-bit GUID via TokenToGUID, the GUID -// is passed to the inner `FUN_00529FE0` builder, and the original string is -// discarded. The builder writes the GUID into `[tooltip + 0x368]` / `+0x36C` -// and the shared `FUN_00530050` clear zeroes both at the start of every -// `SetX` call — same gating pattern HasSpell / HasItem use. +// The unit a tooltip shows lives in the engine as a GUID only: the builder +// `FUN_GAMETOOLTIP_BUILD_UNIT` writes it to `[tooltip + 0x368]` / `+0x36C`, and +// the shared clear `FUN_00530050` zeroes both at the start of every `SetX` — +// same gating pattern HasSpell / HasItem use. The token string is discarded +// by `Script_GameTooltip_SetUnit` right after FUN_TOKEN_TO_GUID resolves it. // -// Modern `GameTooltip:GetUnit()` returns `(name, unitToken)`, but the token -// can't be recovered exactly in vanilla — multiple unit tokens (`"target"`, -// `"focus"`, `"mouseover"` etc.) can point at the same GUID at any given -// moment, and the engine doesn't remember which one was used. The GUID is -// what addons actually need for cross-referencing (it threads through -// `UnitGUID`, the NameCache, item-link unique IDs, etc.); exposing it -// directly as `GetUnitGUID()` avoids inventing a faux token that wouldn't -// match the original Lua input anyway. +// `GetUnit()` needs that token, and the GUID can't give it back (`"target"` +// and `"raid3"` can name one GUID at once). So we keep it where it enters. +// The builder has exactly three engine callers (xrefs, verified): +// +// - `Script_GameTooltip_SetUnit` (0x005349B0) — the Lua token. We +// re-register `SetUnit` in front of it (most recent registration wins, +// as for `SetAction`) and stage the token as PENDING for that tooltip. +// - `FUN_00492890` — the world mouseover setter; stores the mouseover GUID, +// then builds into GameTooltip. The unit IS "mouseover". +// - `FUN_004919d0` — rebuilds GameTooltip in place, only when the unit +// changed is the one it already shows (guid == [+0x368]). Keeps its token. +// +// `Frame::Attributes` drives the engine's mouseover setter (and, for a member +// with no live object, the builder directly) when the cursor rests on a unit +// frame. It stages the FRAME'S token around those calls, because "mouseover" +// would name nothing there: the engine resolves the "mouseover" token only +// while the unit has a live object (FUN_00515970's mouseover branch returns 0 +// otherwise), unlike "partyN", which reads the roster. +// +// The token binds inside the builder (BeforeBuild, from the one co-hook +// Tooltip::SetEvents owns), not after SetUnit returns: OnTooltipSetUnit fires +// from within the builder, and its handler must already see the new token. +// +// Known gap: a tooltip kept visible on unit X via SetUnit("target") while the +// cursor enters X in the world takes the mouseover build with the same GUID +// it already shows, which reads as a refresh and keeps "target". Leaving the +// owning frame normally hides the tooltip (the clear zeroes the GUID) first. + +namespace { + +constexpr int kMaxTokenLen = 64; // longer than any real token ("0x" + 16 hex = 18) +constexpr const char *kMouseoverToken = "mouseover"; + +struct Record { + void *tooltip; + uint32_t guid[2]; + char token[kMaxTokenLen]; +}; + +// Few tooltips, same reasoning and lifetime as Tooltip::SetEvents' cells: the +// objects are recreated on every /reload, so the table is dropped at teardown. +constexpr int kMaxTooltips = 32; +Record g_records[kMaxTooltips]; +int g_recordCount = 0; + +// The token the next build of `g_pendingTooltip` binds (StageToken). Matched +// by tooltip, so a stale stage (the engine raised past our reset) can never +// bind to another tooltip's build; every stager also resets it on entry. +void *g_pendingTooltip = nullptr; +char g_pendingToken[kMaxTokenLen]; + +void CopyToken(char *dst, const char *src) { + int i = 0; + for (; i < kMaxTokenLen - 1 && src[i] != '\0'; ++i) + dst[i] = src[i]; + dst[i] = '\0'; +} + +Record *Find(const void *tooltip) { + for (int i = 0; i < g_recordCount; ++i) + if (g_records[i].tooltip == tooltip) + return &g_records[i]; + return nullptr; +} + +Record *FindOrCreate(void *tooltip) { + if (Record *r = Find(tooltip)) + return r; + if (g_recordCount >= kMaxTooltips) + return nullptr; + Record &r = g_records[g_recordCount++]; + r.tooltip = tooltip; + return &r; +} + +void Bind(void *tooltip, const uint32_t *guid, const char *token) { + Record *r = FindOrCreate(tooltip); + if (r == nullptr) + return; + r->guid[0] = guid[0]; + r->guid[1] = guid[1]; + CopyToken(r->token, token); +} + +void PrepareForReload() { + g_recordCount = 0; + g_pendingTooltip = nullptr; +} + +const Game::ReloadAutoRegister _reloadReg{&PrepareForReload}; + +} // namespace + +void StageToken(void *tooltip, const char *token) { + g_pendingTooltip = nullptr; + if (tooltip == nullptr || token == nullptr) + return; + CopyToken(g_pendingToken, token); + g_pendingTooltip = tooltip; +} + +void ClearStagedToken() { g_pendingTooltip = nullptr; } + +void BeforeBuild(void *tooltip, const uint32_t *guid) { + if (tooltip == nullptr || guid == nullptr) + return; + if (g_pendingTooltip == tooltip) { + g_pendingTooltip = nullptr; + Bind(tooltip, guid, g_pendingToken); + return; + } + // Not from our SetUnit: FUN_004919d0's in-place refresh keeps the bound + // token; every other caller is a mouseover build. + const auto *base = static_cast(tooltip); + const uint32_t liveLo = Game::Read(base, Offsets::OFF_TOOLTIP_UNIT_GUID_LO); + const uint32_t liveHi = Game::Read(base, Offsets::OFF_TOOLTIP_UNIT_GUID_HI); + const Record *r = Find(tooltip); + if (r != nullptr && liveLo == guid[0] && liveHi == guid[1] && + r->guid[0] == guid[0] && r->guid[1] == guid[1]) + return; + Bind(tooltip, guid, kMouseoverToken); +} + +// `GameTooltip:SetUnit(unit)` — the engine's own method, with the token +// staged for BeforeBuild. Anything we can't vouch for goes to the engine with +// the untouched stack so it raises its own errors: a non-tooltip self, a +// non-string arg, or a string the engine's resolver would reject (its raise +// would longjmp past our reset, so we never stage one). +static int __fastcall Script_GameTooltipSetUnit(void *L) { + using Script_t = int(__fastcall *)(void *L); + const auto engine = reinterpret_cast(Offsets::FUN_SCRIPT_GAMETOOLTIP_SET_UNIT); + + ClearStagedToken(); + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE || !Game::Lua::IsString(L, 2)) + return engine(L); + void *tooltip = Game::Lua::ResolveTooltip(L, 1, /*raiseError*/ false); + const char *token = Game::Lua::ToString(L, 2); + if (tooltip == nullptr || token == nullptr || !Unit::TokenResolve::IsUnitToken(token)) + return engine(L); + + StageToken(tooltip, token); + const int n = engine(L); + ClearStagedToken(); // a token naming nothing never reaches the builder + return n; +} using ObjectGetName_t = const char *(__fastcall *)(void *obj, void *edx_unused, int *outFlags); +// The name `UnitName` would give for this GUID, in `Script_UnitName`'s order +// (0x00517020): the live object, else — no object, e.g. an offline or far +// party/raid member — the group roster slot's inline name, else the player +// NameCache. The unit tooltip builder draws its header from the same sources. static const char *ResolveUnitName(uint32_t guidLo, uint32_t guidHi) { - void *obj = Object::ByGuid(Offsets::TYPEMASK_UNIT, - (static_cast(guidHi) << 32) | guidLo, - "GameTooltip:GetUnitGUID", 0x172); - if (obj == nullptr) - return nullptr; + uint64_t guid = (static_cast(guidHi) << 32) | guidLo; + void *obj = Object::ByGuid(Offsets::TYPEMASK_UNIT, guid, "GameTooltip:GetUnit", 0x172); + if (obj == nullptr) { + using SlotLookup_t = const uint8_t *(__fastcall *)(const uint64_t *guid); + if (const uint8_t *slot = reinterpret_cast( + Offsets::FUN_GROUP_MEMBER_SLOT_LOOKUP)(&guid)) + return Game::Ptr(slot, Offsets::OFF_GROUP_MEMBER_SLOT_NAME); + return Unit::Identity::NameForGuid(guid); + } // `FUN_OBJECT_GET_NAME` is __thiscall — wire as __fastcall with the // ignored EDX slot. Returns a const char* (the canonical display // name for the object); falls back to engine `"UNKNOWNOBJECT"` / @@ -139,9 +287,9 @@ static const char *ResolveUnitName(uint32_t guidLo, uint32_t guidHi) { // `GameTooltip:GetUnitGUID()` → (name, guidString) for whichever unit // the tooltip is currently displaying, or nothing if it isn't showing -// a unit. Return order matches modern's `GameTooltip:GetUnit()` shape -// (name first), so addons porting from `local name, unit = ttip:GetUnit()` -// can swap to `GetUnitGUID` and keep their existing destructuring. +// a unit. Same shape as `GetUnit()` with the GUID in the token's place — the +// GUID outlives a token like "mouseover", which stops naming the unit as soon +// as the cursor moves on. // `name` is the unit's display name (may be `"UNKNOWNOBJECT"` for a // remote unit whose info hasn't been queried yet — same fallback the // engine uses internally). `guidString` is the canonical @@ -175,6 +323,41 @@ static int __fastcall Script_GameTooltipGetUnitGUID(void *L) { return 2; } +// `GameTooltip:GetUnit()` → (name, unitToken) for whichever unit the tooltip +// is showing, or nothing if it isn't showing one. `unitToken` is the string +// given to `SetUnit`, or "mouseover" for the world-mouseover tooltip. The +// token is only returned while its bound GUID is still the one the tooltip +// shows; nil otherwise. +static int __fastcall Script_GameTooltipGetUnit(void *L) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { + Game::Lua::Error(L, "Usage: GameTooltip:GetUnit()"); + return 0; + } + void *tooltipObj = Game::Lua::ResolveTooltip(L); + if (tooltipObj == nullptr) + return 0; + + const auto *base = static_cast(tooltipObj); + const uint32_t guidLo = + Game::Read(base, Offsets::OFF_TOOLTIP_UNIT_GUID_LO); + const uint32_t guidHi = + Game::Read(base, Offsets::OFF_TOOLTIP_UNIT_GUID_HI); + if (guidLo == 0 && guidHi == 0) + return 0; + + if (const char *name = ResolveUnitName(guidLo, guidHi)) + Game::Lua::PushString(L, name); + else + Game::Lua::PushNil(L); + + const Record *r = Find(tooltipObj); + if (r != nullptr && r->guid[0] == guidLo && r->guid[1] == guidHi) + Game::Lua::PushString(L, r->token); + else + Game::Lua::PushNil(L); + return 2; +} + // `GameTooltip:HasUnit()` — boolean companion to `GetUnitGUID`. Returns // true iff the tooltip is currently showing a unit (i.e., the stored // GUID is non-zero, which the shared tooltip-clear zeroes on every @@ -200,6 +383,8 @@ static int __fastcall Script_GameTooltipHasUnit(void *L) { static const Game::Lua::FrameMethodEntry g_methods[] = { {"SetUnitAura", &Script_GameTooltipSetUnitAura}, + {"SetUnit", &Script_GameTooltipSetUnit}, + {"GetUnit", &Script_GameTooltipGetUnit}, {"GetUnitGUID", &Script_GameTooltipGetUnitGUID}, {"HasUnit", &Script_GameTooltipHasUnit}, }; diff --git a/src/unit/Tooltip.h b/src/unit/Tooltip.h new file mode 100644 index 00000000..0e9bd0a1 --- /dev/null +++ b/src/unit/Tooltip.h @@ -0,0 +1,35 @@ +// 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 . + +#pragma once + +#include + +namespace Unit::Tooltip { + +// Called by the unit-builder co-hook (Tooltip::SetEvents owns the one hook on +// FUN_GAMETOOLTIP_BUILD_UNIT) BEFORE the original runs, so the token is bound +// before the build clears the tooltip and before OnTooltipSetUnit fires — +// a handler calling `self:GetUnit()` sees the token of the unit being built. +// `guid` is the builder's own argument (lo, hi). +void BeforeBuild(void *tooltip, const uint32_t *guid); + +// Name the token the next unit build of `tooltip` binds to, for a caller that +// drives a build itself (our SetUnit, Frame::Attributes' unit-frame hover). +// Always pair with ClearStagedToken after the call that may build — a call +// that ends up building nothing must not leave the token for a later, +// unrelated build. Without a stage, a build is a world mouseover. +void StageToken(void *tooltip, const char *token); +void ClearStagedToken(); + +} // namespace Unit::Tooltip