From bcef99ef5c8d1297a4612082e3a64dc91da02881 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sat, 9 May 2026 01:04:05 -0500 Subject: [PATCH] C_Container.GetContainerItemDurability --- README.md | 2 +- TODO.md | 37 ++++++++++------- docs/API.md | 74 +++++++++++++++++++++++++++++++++ src/item/Durability.cpp | 90 ++++++++++++++++++++++------------------- src/item/Location.cpp | 25 ++++++------ src/item/Location.h | 8 ++++ 6 files changed, 166 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index b75d1aa1..5202e71b 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Quick index: | GameTooltip | `GameTooltip:SetSpellByID`, `GameTooltip:SetItemByID`, `GameTooltip:SetUnitAura` | | Quest | `GetQuestIDFromLogIndex`, `C_QuestLog.RequestLoadQuestByID` (+ `QUEST_DATA_LOAD_RESULT` event), `C_QuestLog.GetTitleForQuestID` | | Faction | `GetFactionIDByIndex`, `GetFactionInfoByID`, `GetFactionParentID` | -| Item | `C_Item.IsBound`, `C_Item.GetItemID`, `GetInventoryItemID`, `C_Container.GetContainerItemID`, `C_Item.GetItemInfoInstant`, `GetItemIcon` / `C_Item.GetItemIcon` / `C_Item.GetItemIconByID`, `C_Item.IsItemDataCached(ByID)`, `C_Item.RequestLoadItemData(ByID)` (+ `ITEM_DATA_LOAD_RESULT` event) | +| Item | `C_Item.IsBound`, `C_Item.GetItemID`, `GetInventoryItemID`, `C_Container.GetContainerItemID`, `GetInventoryItemDurability`, `C_Container.GetContainerItemDurability`, `C_Item.GetItemInfoInstant`, `GetItemIcon` / `C_Item.GetItemIcon` / `C_Item.GetItemIconByID`, `C_Item.IsItemDataCached(ByID)`, `C_Item.RequestLoadItemData(ByID)` (+ `ITEM_DATA_LOAD_RESULT` event) | | Unit | `UnitGUID` | | Combat | `InCombatLockdown` | | Talent | `GetTalentSpellID`, `GetTalentIDByIndex` | diff --git a/TODO.md b/TODO.md index cd185443..8692bdd6 100644 --- a/TODO.md +++ b/TODO.md @@ -384,22 +384,31 @@ Returns the on-use spell name + spellID for items that have one mapped yet — the `m_spellId[5]` array on the cached item record. Combine the spellID with `GetSpellInfo` for the name. -## ~~24. `GetInventoryItemDurability(invSlot)`~~ — DONE +## ~~24. `GetInventoryItemDurability(invSlot)` / `C_Container.GetContainerItemDurability(containerIndex, slotIndex)`~~ — DONE -Returns `(current, max)` durability for the player's equipped item at -`invSlot` (1-based), or nothing if the slot is empty or the item has -no durability concept (consumables, materials, rings, trinkets, -shirts, tabards). Player-only — matches modern API; the original -TODO line had `(unit, slot)` but modern `GetInventoryItemDurability` -takes only the slot. +Returns `(current, max)` durability for the player's equipped item or +bag/bank slot, or nothing if the slot is empty or the item has no +durability concept (consumables, materials, rings, trinkets, shirts, +tabards). Both forms shipped: -Reads ITEM_FIELD_DURABILITY (+0xA0) and ITEM_FIELD_MAXDURABILITY -(+0xA4) off the CGItem descriptor at `+0x114` — the same descriptor -[src/item/Bound.cpp](src/item/Bound.cpp) reads FLAGS from. Field -offsets verified by decoding `Script_GetInventoryItemBroken` at -`0x004C8590`, which tests `[descriptor+0x3C] & 0x08` for the broken -flag bit AND `[descriptor+0xA4] > 0 && [descriptor+0xA0] == 0` as a -fallback. Modern semantics confirmed against 3.3.5's +- `GetInventoryItemDurability(invSlot)` — character-pane equipment slot + (1-based, 1..19). Player-only by design, matches modern API; the + original TODO line had `(unit, slot)` but modern + `GetInventoryItemDurability` takes only the slot. +- `C_Container.GetContainerItemDurability(containerIndex, slotIndex)` — + bag/bank slot. Goes through `Item::Location::ResolveBag` → + `PackBagSlot` → `GetItemBySlot`, same chain + `C_Container.GetContainerItemID` already used. + +Both share the inner `PushDurabilityForItem` helper — only the +CGItem-resolution path differs. Reads ITEM_FIELD_DURABILITY (+0xA0) and +ITEM_FIELD_MAXDURABILITY (+0xA4) off the CGItem descriptor at `+0x114` +— the same descriptor [src/item/Bound.cpp](src/item/Bound.cpp) reads +FLAGS from. Field offsets verified by decoding +`Script_GetInventoryItemBroken` at `0x004C8590`, which tests +`[descriptor+0x3C] & 0x08` for the broken flag bit AND +`[descriptor+0xA4] > 0 && [descriptor+0xA0] == 0` as a fallback. +Modern semantics confirmed against 3.3.5's `Script_GetInventoryItemDurability` at `0x005EA170`: returns nothing (0 values) when max is 0. diff --git a/docs/API.md b/docs/API.md index 70547104..829c45f6 100644 --- a/docs/API.md +++ b/docs/API.md @@ -34,6 +34,8 @@ build instructions. - [`C_Item.GetItemID(itemLocation)`](#c_itemgetitemiditemlocation) - [`GetInventoryItemID(unit, slot)`](#getinventoryitemidunit-slot) - [`C_Container.GetContainerItemID(bagIndex, slotIndex)`](#c_containergetcontaineritemidbagindex-slotindex) + - [`GetInventoryItemDurability(invSlot)`](#getinventoryitemdurabilityinvslot) + - [`C_Container.GetContainerItemDurability(containerIndex, slotIndex)`](#c_containergetcontaineritemdurabilitycontainerindex-slotindex) - [`GetItemIcon(itemID)` / `C_Item.GetItemIcon(itemLocation)` / `C_Item.GetItemIconByID(item)`](#getitemiconitemid--c_itemgetitemiconitemlocation--c_itemgetitemiconbyiditem) - [`C_Item.GetItemInfoInstant(item)`](#c_itemgetiteminfoinstantitem) - [`C_Item.IsItemDataCachedByID(item)` / `C_Item.IsItemDataCached(itemLocation)`](#c_itemisitemdatacachedbyiditem--c_itemisitemdatacacheditemlocation) @@ -741,6 +743,78 @@ for slot = 1, 16 do end ``` +### `GetInventoryItemDurability(invSlot)` + +Returns `(current, maximum)` durability for the player's equipped item +at `invSlot` (1-based, character-pane slots 1..19), or nothing if the +slot is empty or the item has no durability concept (rings, trinkets, +necks, backs, shirts, tabards, etc.). + +``` +current, maximum = GetInventoryItemDurability(invSlot) +``` + +```lua +local cur, max = GetInventoryItemDurability(INVSLOT_CHEST) +if cur then + -- cur, max are positive integers (e.g. 65, 65 for full chest) +end + +-- Items without durability return nothing — both locals are nil: +local cur, max = GetInventoryItemDurability(INVSLOT_FINGER1) +-- cur == nil, max == nil +``` + +> **Player-only.** Matches modern API: 3.3.5+ `GetInventoryItemDurability` +> takes only the slot, no unit token. Inspect targets / party members' +> equipment durability isn't broadcast in 1.12, so we couldn't expose it +> for other units even if we wanted to. + +> **`(0, max)` vs nothing.** Items that have a durability concept but +> are currently broken (`current == 0`, `max > 0`) still return +> `(0, max)`. The "nothing" return is reserved for items that have no +> durability fields populated at all — `max` is the discriminator, +> matching the engine's own `GetInventoryItemBroken` logic. + +Reads ITEM_FIELD_DURABILITY (+0xA0) and ITEM_FIELD_MAXDURABILITY +(+0xA4) directly off the CGItem's m_objectFields descriptor at `+0x114` +— same descriptor [`C_Item.IsBound`](#c_itemisbounditemlocation) reads +FLAGS from. No DBC indirection. + +Equivalent to the function of the same name introduced in 3.0. + +### `C_Container.GetContainerItemDurability(containerIndex, slotIndex)` + +Bag/bank variant of [`GetInventoryItemDurability`](#getinventoryitemdurabilityinvslot). +Same `(current, maximum)` return shape and the same "nothing for items +without durability" rule. + +``` +current, maximum = C_Container.GetContainerItemDurability(containerIndex, slotIndex) +``` + +- `containerIndex = 0` — the player's main backpack. +- `containerIndex = 1..4` — the player's equipped bag slots. +- `containerIndex = -1, 5..11` — bank-frame slots, only addressable + while the bank window is open. +- `slotIndex` — 1-based, capped at the bag's actual slot count. + +```lua +for slot = 1, GetContainerNumSlots(0) do + local cur, max = C_Container.GetContainerItemDurability(0, slot) + if cur then + -- item at backpack slot has durability + end +end +``` + +Goes through the same bag-resolve chain +[`C_Container.GetContainerItemID`](#c_containergetcontaineritemidbagindex-slotindex) +uses (engine's `PackBagSlot` → `GetItemBySlot`), then reads the same +durability fields off the descriptor. + +Equivalent to the function of the same name introduced in 10.0. + ### `GetItemIcon(itemID)` / `C_Item.GetItemIcon(itemLocation)` / `C_Item.GetItemIconByID(item)` Three accessors that all return the icon path for an item, differing only in diff --git a/src/item/Durability.cpp b/src/item/Durability.cpp index 3599f192..d18a8adf 100644 --- a/src/item/Durability.cpp +++ b/src/item/Durability.cpp @@ -13,6 +13,7 @@ #include "Game.h" #include "Offsets.h" +#include "item/Location.h" #include @@ -20,22 +21,36 @@ namespace Item::Durability { namespace { -using GetItemBySlot_t = void *(__thiscall *)(void *invMgr, int slot); -using ResolveUnitToken_t = void *(__fastcall *)(const char *token); +// Reads ITEM_FIELD_DURABILITY (+0xA0) and ITEM_FIELD_MAXDURABILITY (+0xA4) +// off the CGItem's m_objectFields descriptor at `+0x114`. Same path +// `Script_GetInventoryItemBroken` (`0x004C8590`) uses; field offsets +// verified there. Returns false (no values pushed) when the item is +// missing, the descriptor is null, or max is 0 (item has no durability +// concept — consumables, materials, rings, trinkets, etc.). Modern +// semantics: nothing rather than (0, 0). +static int PushDurabilityForItem(void *L, const uint8_t *item) { + if (item == nullptr) + return 0; + auto *descriptor = *reinterpret_cast( + item + Offsets::OFF_ITEM_DESCRIPTOR); + if (descriptor == nullptr) + return 0; + const uint32_t cur = *reinterpret_cast( + descriptor + Offsets::OFF_DESCRIPTOR_DURABILITY); + const uint32_t max = *reinterpret_cast( + descriptor + Offsets::OFF_DESCRIPTOR_MAX_DURABILITY); + if (max == 0) + return 0; + Game::Lua::PushNumber(L, static_cast(cur)); + Game::Lua::PushNumber(L, static_cast(max)); + return 2; +} -// `GetInventoryItemDurability(invSlot)` — returns `current, maximum` for the -// player's equipped item at `invSlot` (1-based), or nothing if the slot is -// empty or the item has no durability (consumables, materials, etc.). -// -// Same field-access path `Script_GetInventoryItemBroken` uses (`0x004C8590`): -// resolve player → invMgr at `+0x1D38` → `GetItemBySlot(slot - 1)` → CGItem's -// m_objectFields descriptor at `+0x114` → read DURABILITY at `+0xA0` and -// MAX_DURABILITY at `+0xA4` as plain dwords. -// -// Player-only by design (matches modern API). Modern semantics: returns -// nothing — not (0, 0) — when `max == 0`. The 3.3.5 implementation at -// `0x005EA170` does the same: tests max via the GetMaxDurability getter and -// returns 0 values if zero. +// `GetInventoryItemDurability(invSlot)` — returns `current, maximum` for +// the player's equipped item at `invSlot` (1-based), or nothing if the +// slot is empty or the item has no durability. Player-only by design, +// matching modern API; 3.3.5's implementation at `0x005EA170` does the +// same (uses `ResolveActivePlayer`, no unit arg). static int __fastcall Script_GetInventoryItemDurability(void *L) { if (!Game::Lua::IsNumber(L, 1)) { Game::Lua::Error(L, "Usage: GetInventoryItemDurability(invSlot)"); @@ -44,35 +59,24 @@ static int __fastcall Script_GetInventoryItemDurability(void *L) { const int slot1Based = static_cast(Game::Lua::ToNumber(L, 1)); if (slot1Based < 1) return 0; + return PushDurabilityForItem(L, Item::Location::ResolveEquipmentSlot(slot1Based)); +} - auto ResolveUnitToken = - reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); - auto *player = static_cast(ResolveUnitToken("player")); - if (player == nullptr) +// `C_Container.GetContainerItemDurability(containerIndex, slotIndex)` — +// modern positional-arg accessor. Same `(current, max)` shape as +// `GetInventoryItemDurability`, but for bag/bank slots instead of the +// character pane. Goes through `Item::Location::ResolveBag` → +// engine's `PackBagSlot` → `GetItemBySlot` (same chain +// `C_Container.GetContainerItemID` uses). +static int __fastcall Script_C_Container_GetContainerItemDurability(void *L) { + if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsNumber(L, 2)) { + Game::Lua::Error(L, + "Usage: C_Container.GetContainerItemDurability(containerIndex, slotIndex)"); return 0; - - auto *invMgr = player + Offsets::OFF_PLAYER_INVENTORY_MANAGER; - auto GetItemBySlot = reinterpret_cast( - Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT); - auto *item = static_cast(GetItemBySlot(invMgr, slot1Based - 1)); - if (item == nullptr) - return 0; - - auto *descriptor = *reinterpret_cast( - item + Offsets::OFF_ITEM_DESCRIPTOR); - if (descriptor == nullptr) - return 0; - - const uint32_t cur = *reinterpret_cast( - descriptor + Offsets::OFF_DESCRIPTOR_DURABILITY); - const uint32_t max = *reinterpret_cast( - descriptor + Offsets::OFF_DESCRIPTOR_MAX_DURABILITY); - if (max == 0) - return 0; - - Game::Lua::PushNumber(L, static_cast(cur)); - Game::Lua::PushNumber(L, static_cast(max)); - return 2; + } + const int bagID = static_cast(Game::Lua::ToNumber(L, 1)); + const int slotIndex = static_cast(Game::Lua::ToNumber(L, 2)); + return PushDurabilityForItem(L, Item::Location::ResolveBag(L, bagID, slotIndex)); } } // namespace @@ -80,6 +84,8 @@ static int __fastcall Script_GetInventoryItemDurability(void *L) { static void RegisterLuaFunctions() { Game::Lua::RegisterGlobalFunction("GetInventoryItemDurability", &Script_GetInventoryItemDurability); + Game::Lua::RegisterTableFunction("C_Container", "GetContainerItemDurability", + &Script_C_Container_GetContainerItemDurability); } static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; diff --git a/src/item/Location.cpp b/src/item/Location.cpp index 7e286fa0..c2d40bf2 100644 --- a/src/item/Location.cpp +++ b/src/item/Location.cpp @@ -47,19 +47,6 @@ bool TryReadIntField(void *L, int locIdx, const char *fieldName, int *out) { return ok; } -const uint8_t *ResolveEquipmentSlot(int luaSlot) { - void *invMgr = ResolveActivePlayerInvMgr(); - if (invMgr == nullptr) - return nullptr; - // GetItemBySlot expects 0-based linearized slot indices. The built-in - // Lua functions all do `dec eax` on the slot argument before calling — - // see helper at 0x004C8520. - const int slot = luaSlot - 1; - auto GetItemBySlot = reinterpret_cast( - Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT); - return static_cast(GetItemBySlot(invMgr, slot)); -} - const uint8_t *ResolveBagSlot(void *L, int bagID, int slotIndex) { // PackBagSlot reads bagID/slotIndex from Lua stack[1] and stack[2] (it's // designed to be called from a Lua-callback context like @@ -83,6 +70,18 @@ const uint8_t *ResolveBagSlot(void *L, int bagID, int slotIndex) { } // namespace +const uint8_t *ResolveEquipmentSlot(int slot1Based) { + void *invMgr = ResolveActivePlayerInvMgr(); + if (invMgr == nullptr) + return nullptr; + // GetItemBySlot expects 0-based linearized slot indices. The built-in + // Lua functions all do `dec eax` on the slot argument before calling — + // see helper at 0x004C8520. + auto GetItemBySlot = reinterpret_cast( + Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT); + return static_cast(GetItemBySlot(invMgr, slot1Based - 1)); +} + const uint8_t *ResolveBag(void *L, int bagID, int slotIndex) { return ResolveBagSlot(L, bagID, slotIndex); } diff --git a/src/item/Location.h b/src/item/Location.h index 7f38bbfa..d2b60ea2 100644 --- a/src/item/Location.h +++ b/src/item/Location.h @@ -40,4 +40,12 @@ const uint8_t *Resolve(void *L, int locIdx); // `Resolve()` above. const uint8_t *ResolveBag(void *L, int bagID, int slotIndex); +// Resolves a 1-based character-pane equipment slot directly to a +// `CGItem *`. Used by the positional-arg `GetInventoryItemDurability` +// (and any future per-slot getter) that doesn't take an ItemLocation +// table. Player-only — walks the local player's private inventory +// manager, same path the `{equipmentSlotIndex=N}` table form uses. +// Returns nullptr for out-of-range slots or empty equipment. +const uint8_t *ResolveEquipmentSlot(int slot1Based); + } // namespace Item::Location