From 3c4dbb3e81f6dc9ed62f72ce74b2fd95dc7d706e Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Wed, 13 May 2026 22:19:26 -0500 Subject: [PATCH] C_Item.GetItemMaxStackSizeByID --- AddOns/!!!ClassicAPI/Util/ItemUtil.lua | 16 +++++ README.md | 2 +- docs/API.md | 17 ++++++ src/Offsets.h | 6 ++ src/item/MaxStackSize.cpp | 85 ++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/item/MaxStackSize.cpp diff --git a/AddOns/!!!ClassicAPI/Util/ItemUtil.lua b/AddOns/!!!ClassicAPI/Util/ItemUtil.lua index fd925f5e..ae7eb77d 100644 --- a/AddOns/!!!ClassicAPI/Util/ItemUtil.lua +++ b/AddOns/!!!ClassicAPI/Util/ItemUtil.lua @@ -215,6 +215,22 @@ function ItemMixin:GetItemQualityColorRGB() -- requires item data to be loaded return colorTbl.color:GetRGB() end +function ItemMixin:GetItemMaxStackSize() -- requires item data to be loaded + if self:GetStaticBackingItem() then + return C_Item.GetItemMaxStackSizeByID(self:GetStaticBackingItem()); + end + + if not self:IsItemEmpty() then + return C_Item.GetItemMaxStackSize(self:GetItemLocation()); + end + return nil; +end + +function ItemMixin:IsStackable() -- requires item data to be loaded + local maxStackSize = self:GetItemMaxStackSize(); + return maxStackSize and maxStackSize > 1; +end + function ItemMixin:GetInventoryType() if self:GetStaticBackingItem() then return C_Item.GetItemInventoryTypeByID(self:GetStaticBackingItem()) diff --git a/README.md b/README.md index 46844e35..67a21695 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | GameTooltip | `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetUnitAura` | | Hooks | `hooksecurefunc` | | Input | `IsLeftAltKeyDown`, `IsLeftControlKeyDown`, `IsLeftShiftKeyDown`, `IsModifierKeyDown`, `IsRightAltKeyDown`, `IsRightControlKeyDown`, `IsRightShiftKeyDown` | -| Item | `C_Item.DoesItemExist`, `C_Item.DoesItemExistByID`, `C_Item.EquipItemByName`, `C_Item.GetCurrentItemLevel`, `C_Item.GetDetailedItemLevelInfo`, `C_Item.GetItemCount`, `C_Item.GetItemFamily`, `C_Item.GetItemGUID`, `C_Item.GetItemIcon`, `C_Item.GetItemIconByID`, `C_Item.GetItemID`, `C_Item.GetItemInfoInstant`, `C_Item.GetItemInventoryType`, `C_Item.GetItemInventoryTypeByID`, `C_Item.GetItemLink`, `C_Item.GetItemName`, `C_Item.GetItemNameByID`, `C_Item.GetItemQuality`, `C_Item.GetItemQualityByID`, `C_Item.GetItemSpell`, `C_Item.IsBound`, `C_Item.IsEquippableItem`, `C_Item.IsEquippedItem`, `C_Item.IsItemDataCached`, `C_Item.IsItemDataCachedByID`, `C_Item.IsLocked`, `C_Item.RequestLoadItemData`, `C_Item.RequestLoadItemDataByID`, `GetAuctionItemID`, `GetAuctionSellItemID`, `GetCraftReagentItemID`, `GetInboxItemID`, `GetInventoryItemDurability`, `GetInventoryItemID`, `GetInventoryItemRepairCost`, `GetItemIcon`, `GetLootRollItemID`, `GetLootSlotItemID`, `GetMerchantItemID`, `GetQuestItemID`, `GetQuestLogItemID`, `GetTradePlayerItemID`, `GetTradeSkillItemID`, `GetTradeSkillReagentItemID`, `GetTradeTargetItemID`, `OffhandHasWeapon` | +| Item | `C_Item.DoesItemExist`, `C_Item.DoesItemExistByID`, `C_Item.EquipItemByName`, `C_Item.GetCurrentItemLevel`, `C_Item.GetDetailedItemLevelInfo`, `C_Item.GetItemCount`, `C_Item.GetItemFamily`, `C_Item.GetItemGUID`, `C_Item.GetItemIcon`, `C_Item.GetItemIconByID`, `C_Item.GetItemID`, `C_Item.GetItemInfoInstant`, `C_Item.GetItemInventoryType`, `C_Item.GetItemInventoryTypeByID`, `C_Item.GetItemLink`, `C_Item.GetItemMaxStackSize`, `C_Item.GetItemMaxStackSizeByID`, `C_Item.GetItemName`, `C_Item.GetItemNameByID`, `C_Item.GetItemQuality`, `C_Item.GetItemQualityByID`, `C_Item.GetItemSpell`, `C_Item.IsBound`, `C_Item.IsEquippableItem`, `C_Item.IsEquippedItem`, `C_Item.IsItemDataCached`, `C_Item.IsItemDataCachedByID`, `C_Item.IsLocked`, `C_Item.RequestLoadItemData`, `C_Item.RequestLoadItemDataByID`, `GetAuctionItemID`, `GetAuctionSellItemID`, `GetCraftReagentItemID`, `GetInboxItemID`, `GetInventoryItemDurability`, `GetInventoryItemID`, `GetInventoryItemRepairCost`, `GetItemIcon`, `GetLootRollItemID`, `GetLootSlotItemID`, `GetMerchantItemID`, `GetQuestItemID`, `GetQuestLogItemID`, `GetTradePlayerItemID`, `GetTradeSkillItemID`, `GetTradeSkillReagentItemID`, `GetTradeTargetItemID`, `OffhandHasWeapon` | | NameCache | `C_PlayerInfo.RememberPlayer`, `ClassicAPI.GetNameCacheScanEnabled`, `ClassicAPI.GetPersistentNameCacheEnabled`, `ClassicAPI.SetNameCacheScanEnabled`, `ClassicAPI.SetPersistentNameCacheEnabled`, `GetPlayerInfoByGUID` | | Quest | `C_QuestLog.GetTitleForQuestID`, `C_QuestLog.RequestLoadQuestByID`, `GetQuestIDFromLogIndex` | | Spell | `C_Spell.GetSpellDescription`, `C_Spell.GetSpellInfo`, `C_Spell.GetSpellLink`, `C_Spell.GetSpellName`, `C_Spell.GetSpellTexture`, `C_Spell.IsSpellPassive`, `C_Spell.IsSpellUsable`, `CastAutoRepeatSpell`, `FindSpellBookSlotByID`, `GetCraftSpellID`, `GetSpellInfo`, `GetSpellLink`, `GetSpellSchool`, `IsPassiveSpell`, `IsPlayerSpell`, `IsSpellKnown`, `IsUsableSpell` | diff --git a/docs/API.md b/docs/API.md index a980ab8b..91876ed0 100644 --- a/docs/API.md +++ b/docs/API.md @@ -67,6 +67,7 @@ build instructions. - [`C_Item.GetItemName(itemLocation)` / `C_Item.GetItemNameByID(item)`](#c_itemgetitemnameitemlocation--c_itemgetitemnamebyiditem) - [`C_Item.GetItemQuality(itemLocation)` / `C_Item.GetItemQualityByID(item)`](#c_itemgetitemqualityitemlocation--c_itemgetitemqualitybyiditem) - [`C_Item.GetCurrentItemLevel(itemLocation)` / `C_Item.GetDetailedItemLevelInfo(item)`](#c_itemgetcurrentitemlevelitemlocation--c_itemgetdetaileditemlevelinfoitem) + - [`C_Item.GetItemMaxStackSize(itemLocation)` / `C_Item.GetItemMaxStackSizeByID(item)`](#c_itemgetitemmaxstacksizeitemlocation--c_itemgetitemmaxstacksizebyiditem) - [`C_Item.GetItemLink(itemLocation)`](#c_itemgetitemlinkitemlocation) - [`C_Item.GetItemInventoryType(itemLocation)` / `C_Item.GetItemInventoryTypeByID(item)`](#c_itemgetiteminventorytypeitemlocation--c_itemgetiteminventorytypebyiditem) - [`C_Item.IsLocked(itemLocation)`](#c_itemislockeditemlocation) @@ -2037,6 +2038,22 @@ current level, callers that care about the extra returns will see local ilvl = C_Item.GetCurrentItemLevel({equipmentSlotIndex = INVSLOT_HEAD}) ``` +### `C_Item.GetItemMaxStackSize(itemLocation)` / `C_Item.GetItemMaxStackSizeByID(item)` + +Returns the item type's max stack size — what you'd find as the 8th +return of `GetItemInfo(item)`. `1` for non-stackable items; `5`, `20`, +`200`, etc. for stackables. Different from `C_Item.GetStackCount` / +the engine's `GetContainerItemInfo`, which return the **current** +count in a specific slot. + +```lua +local cap = C_Item.GetItemMaxStackSizeByID(2589) -- Linen Cloth → 20 +``` + +Single `uint32` read at cache record `+0x60` (`m_stackable`). By-ID +form fires a background cache fill on miss and returns nil; re-call +after `GET_ITEM_INFO_RECEIVED`. + ### `C_Item.GetItemLink(itemLocation)` Returns the fully-decorated per-instance hyperlink for the item at diff --git a/src/Offsets.h b/src/Offsets.h index 7e78eb27..acc5aeca 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -580,6 +580,12 @@ enum Offsets { OFF_ITEMSTATS_QUALITY = 0x1C, // u32 — 0=Poor … 5=Legendary OFF_ITEMSTATS_INVENTORY_TYPE = 0x2C, OFF_ITEMSTATS_ITEM_LEVEL = 0x38, // u32 — base ilvl from ItemSparse + // `m_stackable` — max stack size for this item type (1 for non- + // stackable; 5/20/200/etc. for stackable). The instance-level + // current stack count is a different field (`OFF_DESCRIPTOR_STACK_COUNT` + // at +0x20 on the CGItem's m_objectFields descriptor — see + // `Item::Count.cpp`). + OFF_ITEMSTATS_STACKABLE = 0x60, // Bag-only fields. `m_containerSlots` (slot count) and `m_bagFamily` // (the BagFamily bitfield — quiver=1, ammo pouch=2, soul bag=4 in // vanilla) live deep in the record. Offsets derived from diff --git a/src/item/MaxStackSize.cpp b/src/item/MaxStackSize.cpp new file mode 100644 index 00000000..27ed13d6 --- /dev/null +++ b/src/item/MaxStackSize.cpp @@ -0,0 +1,85 @@ +// 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 "item/Arg.h" +#include "item/Data.h" +#include "item/ID.h" +#include "item/Location.h" + +#include + +namespace Item::MaxStackSize { + +namespace { + +using GetItemRecord_t = const uint8_t *(__thiscall *)(void *cache, uint32_t itemID, + const uint64_t *guid, void *callback, + void *userData, int unused); + +const uint8_t *PeekItemRecord(uint32_t itemID) { + auto fn = reinterpret_cast(Offsets::FUN_DBCACHE_ITEMSTATS_GET_RECORD); + auto *cache = reinterpret_cast(Offsets::VAR_ITEMDB_CACHE); + const uint64_t zeroGuid = 0; + return fn(cache, itemID, &zeroGuid, nullptr, nullptr, 0); +} + +int PushMaxStackSizeForItemID(void *L, int itemID) { + if (itemID <= 0) + return 0; + const uint8_t *record = PeekItemRecord(static_cast(itemID)); + if (record == nullptr) { + Item::Data::WarmCache(static_cast(itemID)); + return 0; + } + const uint32_t maxStack = *reinterpret_cast( + record + Offsets::OFF_ITEMSTATS_STACKABLE); + Game::Lua::PushNumber(L, static_cast(maxStack)); + return 1; +} + +} // namespace + +// `C_Item.GetItemMaxStackSize(itemLocation)` — max stack size for +// the item type at the given location. Returns the type's stack +// cap (e.g. 20 for Linen Cloth) regardless of how many are +// actually in the slot; for the current stack count of a specific +// item, use `C_Item.GetStackCount`. Non-stackable items return 1. +// nil for empty / uncached / invalid locations. +static int __fastcall Script_C_Item_GetItemMaxStackSize(void *L) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { + Game::Lua::Error(L, "Usage: C_Item.GetItemMaxStackSize(itemLocation)"); + return 0; + } + return PushMaxStackSizeForItemID(L, Item::ID::FromCGItem(Item::Location::Resolve(L, 1))); +} + +// `C_Item.GetItemMaxStackSizeByID(itemInfo)` — same data, but +// keyed on itemID (or `"item:NNN..."` link string). For uncached +// items, returns nil and fires a background cache fill — re-call +// after `GET_ITEM_INFO_RECEIVED` for the value. +static int __fastcall Script_C_Item_GetItemMaxStackSizeByID(void *L) { + return PushMaxStackSizeForItemID(L, Item::Arg::ResolveItemID(L, 1)); +} + +static void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_Item", "GetItemMaxStackSize", + &Script_C_Item_GetItemMaxStackSize); + Game::Lua::RegisterTableFunction("C_Item", "GetItemMaxStackSizeByID", + &Script_C_Item_GetItemMaxStackSizeByID); +} + +static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace Item::MaxStackSize