diff --git a/src/item/Link.cpp b/src/item/Link.cpp index f1dbff0c..c9156bec 100644 --- a/src/item/Link.cpp +++ b/src/item/Link.cpp @@ -27,6 +27,9 @@ namespace { using BuildItemLink_t = const char *(__fastcall *)(const void *cgItem); +using BuildInstanceName_t = void(__thiscall *)(const void *cgItem, char *out, + unsigned outSize); + // Cached ItemStats record lookup. Same `_GetRecord` pattern the rest of // the codebase uses; passes a noop NULL callback so no SMSG fires for // uncached items — caller treats "not cached" as a clean failure. @@ -50,6 +53,16 @@ const char *FromCGItem(const uint8_t *cgItem) { return fn(cgItem); } +bool NameFromCGItem(const uint8_t *cgItem, char *out, size_t outSize) { + if (cgItem == nullptr || out == nullptr || outSize == 0) + return false; + out[0] = '\0'; + auto fn = reinterpret_cast( + Offsets::FUN_ITEM_BUILD_INSTANCE_NAME); + fn(cgItem, out, static_cast(outSize)); + return out[0] != '\0'; +} + bool BasicFromItemID(uint32_t itemID, char *out, size_t outSize) { if (out == nullptr || outSize == 0) return false; diff --git a/src/item/Link.h b/src/item/Link.h index 45423722..2addfbff 100644 --- a/src/item/Link.h +++ b/src/item/Link.h @@ -29,6 +29,17 @@ namespace Item::Link { // produce for the same slot. const char *FromCGItem(const uint8_t *cgItem); +// Writes the item's per-instance decorated *display name* into `out` — +// the same `[Name]` the engine stamps into `FromCGItem`'s link, e.g. +// "Iridium Chain of the Owl" (or the base name when the instance carries +// no random suffix). Reads the suffix ID off the CGItem descriptor +// (`+0x98`) and formats it through the engine's own name builder +// (`FUN_ITEM_BUILD_INSTANCE_NAME`). Returns false (and leaves `out` +// empty) for a null item or an empty build result, so callers can fall +// back to the base ItemStats name. Use whenever a name is returned +// alongside a `FromCGItem` link, so the two agree. +bool NameFromCGItem(const uint8_t *cgItem, char *out, size_t outSize); + // Builds a basic itemID-only hyperlink (`"|cffRRGGBB|Hitem:N:0:0:0|h[Name]|h|r"`) // into `out` from the cached ItemStats record for `itemID`. No // enchant / random-suffix decoration — use this when the caller has diff --git a/src/item/Name.cpp b/src/item/Name.cpp index 93a6125d..89f47168 100644 --- a/src/item/Name.cpp +++ b/src/item/Name.cpp @@ -16,6 +16,7 @@ #include "item/Arg.h" #include "item/Data.h" #include "item/ID.h" +#include "item/Link.h" #include "item/Location.h" #include @@ -56,27 +57,13 @@ int PushNameForItemID(void *L, int itemID) { return 1; } -// Engine's per-instance item-name builder: `__thiscall(CGItem *this, -// char *outBuf, uint outSize)`. Reads the instance's random-suffix ID off -// the descriptor (+0x98) and writes the decorated display name -// ("Ethereum Torque of the Sorcerer"), or the base name when there's no -// suffix. Same builder the item-link builder uses for the bracketed name, -// so this matches `GetItemLink`'s name and modern `C_Item.GetItemName`. -using BuildInstanceName_t = void(__thiscall *)(const void *cgItem, char *out, - unsigned outSize); - -// Pushes the suffix-decorated display name for a resolved `CGItem`. -// Returns 0 (pushes nothing) for a null item or an empty build result, -// so the caller can fall back to the base-name-by-itemID path. +// Pushes the suffix-decorated display name for a resolved `CGItem` via +// the shared engine name builder. Returns 0 (pushes nothing) for a null +// item or an empty build result, so the caller can fall back to the +// base-name-by-itemID path. int PushInstanceName(void *L, const uint8_t *cgItem) { - if (cgItem == nullptr) - return 0; char buf[128]; - buf[0] = '\0'; - auto fn = reinterpret_cast( - Offsets::FUN_ITEM_BUILD_INSTANCE_NAME); - fn(cgItem, buf, sizeof(buf)); - if (buf[0] == '\0') + if (!Item::Link::NameFromCGItem(cgItem, buf, sizeof(buf))) return 0; Game::Lua::PushString(L, buf); return 1; diff --git a/src/item/Tooltip.cpp b/src/item/Tooltip.cpp index 54fa789f..14e9f6a9 100644 --- a/src/item/Tooltip.cpp +++ b/src/item/Tooltip.cpp @@ -220,24 +220,19 @@ static int __fastcall Script_GameTooltipGetItem(void *L) { const char *link = Item::Link::FromCGItem( static_cast(cgItem)); if (link != nullptr && *link != '\0') { - // Engine's link builder also writes the dressed - // (random-suffixed) name into the link's `[Name]` - // slot; pull it out by reading the cached base name - // for the return value. The engine doesn't expose - // the dressed name as a separate string, so we - // return the base name — matches modern semantics - // where (name, link) name is the cached display name - // and the link is the full hyperlink. - const uint8_t *record = PeekItemRecord(static_cast(itemID)); - if (record != nullptr) { - const char *name = *reinterpret_cast( - record + Offsets::OFF_ITEMSTATS_NAME); - if (name != nullptr && *name != '\0') { - Game::Lua::PushString(L, name); - Game::Lua::PushString(L, link); - Game::Lua::PushNumber(L, static_cast(itemID)); - return 3; - } + // Return the dressed (random-suffixed) name the engine + // wrote into the link's `[Name]` slot — built off the same + // CGItem via Item::Link::NameFromCGItem — so the returned + // name agrees with the link ("Iridium Chain of the Owl", + // not the base "Iridium Chain") and matches modern + // GameTooltip:GetItem() on suffixed items. + char name[128]; + if (Item::Link::NameFromCGItem( + static_cast(cgItem), name, sizeof(name))) { + Game::Lua::PushString(L, name); + Game::Lua::PushString(L, link); + Game::Lua::PushNumber(L, static_cast(itemID)); + return 3; } } }