mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
C_Container.GetContainerItemDurability
This commit is contained in:
@@ -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` |
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
+74
@@ -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
|
||||
|
||||
+48
-42
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "Game.h"
|
||||
#include "Offsets.h"
|
||||
#include "item/Location.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
@@ -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<const uint8_t *const *>(
|
||||
item + Offsets::OFF_ITEM_DESCRIPTOR);
|
||||
if (descriptor == nullptr)
|
||||
return 0;
|
||||
const uint32_t cur = *reinterpret_cast<const uint32_t *>(
|
||||
descriptor + Offsets::OFF_DESCRIPTOR_DURABILITY);
|
||||
const uint32_t max = *reinterpret_cast<const uint32_t *>(
|
||||
descriptor + Offsets::OFF_DESCRIPTOR_MAX_DURABILITY);
|
||||
if (max == 0)
|
||||
return 0;
|
||||
Game::Lua::PushNumber(L, static_cast<double>(cur));
|
||||
Game::Lua::PushNumber(L, static_cast<double>(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<int>(Game::Lua::ToNumber(L, 1));
|
||||
if (slot1Based < 1)
|
||||
return 0;
|
||||
return PushDurabilityForItem(L, Item::Location::ResolveEquipmentSlot(slot1Based));
|
||||
}
|
||||
|
||||
auto ResolveUnitToken =
|
||||
reinterpret_cast<ResolveUnitToken_t>(Offsets::FUN_RESOLVE_UNIT_TOKEN);
|
||||
auto *player = static_cast<uint8_t *>(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<GetItemBySlot_t>(
|
||||
Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT);
|
||||
auto *item = static_cast<const uint8_t *>(GetItemBySlot(invMgr, slot1Based - 1));
|
||||
if (item == nullptr)
|
||||
return 0;
|
||||
|
||||
auto *descriptor = *reinterpret_cast<const uint8_t *const *>(
|
||||
item + Offsets::OFF_ITEM_DESCRIPTOR);
|
||||
if (descriptor == nullptr)
|
||||
return 0;
|
||||
|
||||
const uint32_t cur = *reinterpret_cast<const uint32_t *>(
|
||||
descriptor + Offsets::OFF_DESCRIPTOR_DURABILITY);
|
||||
const uint32_t max = *reinterpret_cast<const uint32_t *>(
|
||||
descriptor + Offsets::OFF_DESCRIPTOR_MAX_DURABILITY);
|
||||
if (max == 0)
|
||||
return 0;
|
||||
|
||||
Game::Lua::PushNumber(L, static_cast<double>(cur));
|
||||
Game::Lua::PushNumber(L, static_cast<double>(max));
|
||||
return 2;
|
||||
}
|
||||
const int bagID = static_cast<int>(Game::Lua::ToNumber(L, 1));
|
||||
const int slotIndex = static_cast<int>(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};
|
||||
|
||||
+12
-13
@@ -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<GetItemBySlot_t>(
|
||||
Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT);
|
||||
return static_cast<const uint8_t *>(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<GetItemBySlot_t>(
|
||||
Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT);
|
||||
return static_cast<const uint8_t *>(GetItemBySlot(invMgr, slot1Based - 1));
|
||||
}
|
||||
|
||||
const uint8_t *ResolveBag(void *L, int bagID, int slotIndex) {
|
||||
return ResolveBagSlot(L, bagID, slotIndex);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user