From cb686d93c2c4a67df681a6802a8988a433bc7955 Mon Sep 17 00:00:00 2001
From: Brues <5278969+brues-code@users.noreply.github.com>
Date: Sat, 9 May 2026 00:54:44 -0500
Subject: [PATCH] GetInventoryItemDurability
---
src/Offsets.h | 8 ++++
src/item/Durability.cpp | 87 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 95 insertions(+)
create mode 100644 src/item/Durability.cpp
diff --git a/src/Offsets.h b/src/Offsets.h
index 2861d7c4..0a11d3b0 100644
--- a/src/Offsets.h
+++ b/src/Offsets.h
@@ -111,6 +111,14 @@ enum Offsets {
// descriptor bytes for a worn-and-bound item.
OFF_DESCRIPTOR_FLAGS = 0x3C,
ITEM_FLAG_SOULBOUND = 0x01,
+ // ITEM_FIELD_DURABILITY (current) and ITEM_FIELD_MAXDURABILITY (max) live
+ // adjacent to each other in the descriptor as plain dwords. Verified in
+ // `Script_GetInventoryItemBroken` (`0x004C8590`): after resolving the
+ // descriptor, it reads `[ecx+0xA4]` for max and `[eax+0xA0]` for cur,
+ // and treats the item as broken when `max > 0 && cur == 0`. Items with
+ // no durability concept (consumables, materials) have both fields 0.
+ OFF_DESCRIPTOR_DURABILITY = 0xA0,
+ OFF_DESCRIPTOR_MAX_DURABILITY = 0xA4,
// CGItem has TWO descriptor-like pointers and they hold different things:
// +0x114 = m_objectFields (the UpdateField array we read FLAGS from at
diff --git a/src/item/Durability.cpp b/src/item/Durability.cpp
new file mode 100644
index 00000000..3599f192
--- /dev/null
+++ b/src/item/Durability.cpp
@@ -0,0 +1,87 @@
+// This file is part of ClassicAPI.
+//
+// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
+// of the GNU Lesser 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 Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License along with
+// ClassicAPI. If not, see .
+
+#include "Game.h"
+#include "Offsets.h"
+
+#include
+
+namespace Item::Durability {
+
+namespace {
+
+using GetItemBySlot_t = void *(__thiscall *)(void *invMgr, int slot);
+using ResolveUnitToken_t = void *(__fastcall *)(const char *token);
+
+// `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.
+static int __fastcall Script_GetInventoryItemDurability(void *L) {
+ if (!Game::Lua::IsNumber(L, 1)) {
+ Game::Lua::Error(L, "Usage: GetInventoryItemDurability(invSlot)");
+ return 0;
+ }
+ const int slot1Based = static_cast(Game::Lua::ToNumber(L, 1));
+ if (slot1Based < 1)
+ return 0;
+
+ auto ResolveUnitToken =
+ reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN);
+ auto *player = static_cast(ResolveUnitToken("player"));
+ if (player == nullptr)
+ 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;
+}
+
+} // namespace
+
+static void RegisterLuaFunctions() {
+ Game::Lua::RegisterGlobalFunction("GetInventoryItemDurability",
+ &Script_GetInventoryItemDurability);
+}
+
+static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
+
+} // namespace Item::Durability