From 51484ff47ba09fa415489bc8a494610842b1b747 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Fri, 8 May 2026 00:44:05 -0500 Subject: [PATCH] C_Container.GetContainerItemID(bagIndex, slotIndex) --- README.md | 2 +- TODO.md | 19 +++++++++++++----- docs/API.md | 23 ++++++++++++++++++++++ src/item/ID.cpp | 46 ++++++++++++++++++++++++++++++++++--------- src/item/Location.cpp | 4 ++++ src/item/Location.h | 6 ++++++ 6 files changed, 85 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3e59b26b..975e8fb0 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Quick index: | Spell | `GetSpellInfo` (with `(slot, bookType)` overload), `C_Spell.GetSpellInfo`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `GetSpellLink` (with `(slot, bookType)` overload), `C_Spell.GetSpellLink`, `FindSpellBookSlotByID`, `GameTooltip:SetSpellByID`, `C_Spell.GetSpellDescription` | | Quest | `GetQuestIDFromLogIndex`, `C_QuestLog.RequestLoadQuestByID` (+ `QUEST_DATA_LOAD_RESULT` event), `C_QuestLog.GetTitleForQuestID` | | Faction | `GetFactionIDByIndex`, `GetFactionInfoByID` | -| Item | `C_Item.IsBound`, `C_Item.GetItemID`, `GetInventoryItemID`, `C_Item.GetItemInfoInstant`, `C_Item.IsItemDataCached(ByID)`, `C_Item.RequestLoadItemData(ByID)` (+ `ITEM_DATA_LOAD_RESULT` event) | +| Item | `C_Item.IsBound`, `C_Item.GetItemID`, `GetInventoryItemID`, `C_Container.GetContainerItemID`, `C_Item.GetItemInfoInstant`, `C_Item.IsItemDataCached(ByID)`, `C_Item.RequestLoadItemData(ByID)` (+ `ITEM_DATA_LOAD_RESULT` event) | | Events | `C_EventUtils.IsEventValid` | | Global | `CLASSIC_API_VERSION` | diff --git a/TODO.md b/TODO.md index fa556c26..83b8c1bd 100644 --- a/TODO.md +++ b/TODO.md @@ -240,12 +240,21 @@ value of `GetFactionInfo`, but 1.12-3.3.5 don't expose it at all. See [src/faction/Info.cpp](src/faction/Info.cpp) and "Faction lookups" in [CLAUDE.md](CLAUDE.md) for the resolver/dual-count details. -## 16. `GetContainerItemID(bagID, slotIndex)` — trivial +## ~~16. `C_Container.GetContainerItemID(bagIndex, slotIndex)`~~ — DONE -Modern positional accessor for the same data `C_Item.GetItemID({bagID=B, -slotIndex=S})` returns. We already have the full bag→CGItem→itemID chain -in [src/item/Location.cpp](src/item/Location.cpp); this is just a -two-positional-arg wrapper over it. +Shipped under the modern `C_Container.*` namespace (matching post-MoP +WoW). Wraps the existing bag→CGItem path: a new public +`Item::Location::ResolveBag(L, bagID, slotIndex)` exposes the bag +resolver that was previously private inside the table-shape +`Resolve()`, and a small `ItemIDFromCGItem` helper in +[src/item/ID.cpp](src/item/ID.cpp) factors out the CGItem → instance +block → itemID step now used by both `C_Item.GetItemID` and +`C_Container.GetContainerItemID`. + +Note: ships under `C_Container` rather than as the historical legacy +global `GetContainerItemID` since the modern namespace is the canonical +home for bag/container APIs (the global was deprecated in 10.0). If +addons need the legacy global it's a one-line addition. ## 17. `GetItemIcon(item)` — trivial diff --git a/docs/API.md b/docs/API.md index 974d99b1..4734c842 100644 --- a/docs/API.md +++ b/docs/API.md @@ -27,6 +27,7 @@ build instructions. - [`C_Item.IsBound(itemLocation)`](#c_itemisbounditemlocation) - [`C_Item.GetItemID(itemLocation)`](#c_itemgetitemiditemlocation) - [`GetInventoryItemID(unit, slot)`](#getinventoryitemidunit-slot) + - [`C_Container.GetContainerItemID(bagIndex, slotIndex)`](#c_containergetcontaineritemidbagindex-slotindex) - [`C_Item.GetItemInfoInstant(item)`](#c_itemgetiteminfoinstantitem) - [`C_Item.IsItemDataCachedByID(item)` / `C_Item.IsItemDataCached(itemLocation)`](#c_itemisitemdatacachedbyiditem--c_itemisitemdatacacheditemlocation) - [`C_Item.RequestLoadItemDataByID(item)` / `C_Item.RequestLoadItemData(itemLocation)`](#c_itemrequestloaditemdatabyiditem--c_itemrequestloaditemdataitemlocation) @@ -494,6 +495,28 @@ end local headID = GetInventoryItemID("party1", INVSLOT_HEAD) ``` +### `C_Container.GetContainerItemID(bagIndex, slotIndex)` + +Returns the itemID at the given bag/slot, or `nil` if the slot is empty +or the indices are out of range. Modern positional-arg form of the same +lookup `C_Item.GetItemID({bagID=B, slotIndex=S})` performs. + +- `bagIndex = 0` — the player's main backpack. +- `bagIndex = 1..4` — the player's equipped bag slots. +- `slotIndex` — 1-based, capped at the bag's actual slot count (the + engine's `PackBagSlot` rejects out-of-range slots and returns nil + cleanly). + +```lua +for slot = 1, 16 do + local id = C_Container.GetContainerItemID(0, slot) + if id then + local _, type, subtype = C_Item.GetItemInfoInstant(id) + -- ... + end +end +``` + ### `C_Item.GetItemInfoInstant(item)` Modern-style accessor for the always-available subset of item info — the diff --git a/src/item/ID.cpp b/src/item/ID.cpp index ea6a8d62..faef1e20 100644 --- a/src/item/ID.cpp +++ b/src/item/ID.cpp @@ -19,31 +19,59 @@ namespace Item::ID { +// Reads the itemID off a resolved `CGItem *` via the instance block at +// +0x08. Returns 0 for null/unpopulated items so callers can short- +// circuit to a nil push. +static int ItemIDFromCGItem(const uint8_t *item) { + if (item == nullptr) + return 0; + auto *instance = *reinterpret_cast( + item + Offsets::OFF_ITEM_INSTANCE_BLOCK); + if (instance == nullptr) + return 0; + return static_cast(*reinterpret_cast( + instance + Offsets::OFF_INSTANCE_BLOCK_ITEM_ID)); +} + static int __fastcall Script_GetItemID(void *L) { if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { Game::Lua::Error(L, "Usage: C_Item.GetItemID(itemLocation)"); return 0; } - const uint8_t *item = Item::Location::Resolve(L, 1); - if (item == nullptr) { + const int itemID = ItemIDFromCGItem(Item::Location::Resolve(L, 1)); + if (itemID == 0) { Game::Lua::PushNil(L); return 1; } - auto *instance = *reinterpret_cast( - item + Offsets::OFF_ITEM_INSTANCE_BLOCK); - if (instance == nullptr) { - Game::Lua::PushNil(L); - return 1; + Game::Lua::PushNumber(L, static_cast(itemID)); + return 1; +} + +// `C_Container.GetContainerItemID(bagIndex, slotIndex)` — modern +// positional-arg accessor for the same data `C_Item.GetItemID({bagID=B, +// slotIndex=S})` returns. Both go through `Item::Location::ResolveBag` +// → engine `PackBagSlot` → `GetItemBySlot` → CGItem → instance block +// → itemID. Returns nil for empty slots or invalid bag indices. +static int __fastcall Script_C_Container_GetContainerItemID(void *L) { + if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsNumber(L, 2)) { + Game::Lua::Error(L, "Usage: C_Container.GetContainerItemID(bagIndex, slotIndex)"); + return 0; } - const uint32_t itemID = *reinterpret_cast( - instance + Offsets::OFF_INSTANCE_BLOCK_ITEM_ID); + const int bagID = static_cast(Game::Lua::ToNumber(L, 1)); + const int slotIndex = static_cast(Game::Lua::ToNumber(L, 2)); + + const int itemID = ItemIDFromCGItem(Item::Location::ResolveBag(L, bagID, slotIndex)); + if (itemID == 0) + return 0; Game::Lua::PushNumber(L, static_cast(itemID)); return 1; } static void RegisterLuaFunctions() { Game::Lua::RegisterTableFunction("C_Item", "GetItemID", &Script_GetItemID); + Game::Lua::RegisterTableFunction("C_Container", "GetContainerItemID", + &Script_C_Container_GetContainerItemID); } static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; diff --git a/src/item/Location.cpp b/src/item/Location.cpp index 1e91b61a..7e286fa0 100644 --- a/src/item/Location.cpp +++ b/src/item/Location.cpp @@ -83,6 +83,10 @@ const uint8_t *ResolveBagSlot(void *L, int bagID, int slotIndex) { } // namespace +const uint8_t *ResolveBag(void *L, int bagID, int slotIndex) { + return ResolveBagSlot(L, bagID, slotIndex); +} + const uint8_t *Resolve(void *L, int locIdx) { if (Game::Lua::Type(L, locIdx) != Game::Lua::TYPE_TABLE) return nullptr; diff --git a/src/item/Location.h b/src/item/Location.h index 8b2f4df5..7f38bbfa 100644 --- a/src/item/Location.h +++ b/src/item/Location.h @@ -34,4 +34,10 @@ namespace Item::Location { // Only safe to call from inside a Lua callback. const uint8_t *Resolve(void *L, int locIdx); +// Resolves a `(bagID, slotIndex)` pair directly to a `CGItem *`. Used by +// the positional-arg C_Container.* APIs that don't go through the +// ItemLocation table shape. Stomps the Lua stack — same caveat as +// `Resolve()` above. +const uint8_t *ResolveBag(void *L, int bagID, int slotIndex); + } // namespace Item::Location