mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
Port of 4.3.4's bag search (CGContainerInfo): C_Container.SetItemSearch, C_Container.IsContainerFiltered and INVENTORY_SEARCH_UPDATE. An item matches on its equipment sets' names, its suffixed name, its subclass short name or its equip slot name, case- and accent-folded per locale. The keyring is searched; non-ASCII search text is folded whole. C_Container.GetContainerItemInfo now reports isFiltered and takes an optional table to fill and return, so bag addons can read every slot without a new table per call. Item::Location::ResolveBagArgs resolves the caller's own bag/slot args without wiping the stack.
233 lines
10 KiB
C++
233 lines
10 KiB
C++
// This file is part of ClassicAPI.
|
|
//
|
|
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
|
|
// of the GNU 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 General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along with
|
|
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
// `C_Container.GetContainerItemInfo(containerIndex, slotIndex)` — the modern
|
|
// structured-table accessor (namespaced + table-returning since Patch
|
|
// 10.0.2). Vanilla's global `GetContainerItemInfo` returns a flat 5-tuple
|
|
// (`texture, itemCount, locked, quality, readable` — verified by decoding
|
|
// `Script_GetContainerItemInfo` at `0x004F9670`); backported addons expect
|
|
// the `ContainerItemInfo` table with the wider modern field set. We assemble
|
|
// that table from the same sources the engine reads plus the extra fields it
|
|
// never surfaced in 1.12.
|
|
//
|
|
// Field sources:
|
|
// iconFileID — ItemStats displayInfoID → ItemDisplayInfo.dbc icon path.
|
|
// (1.12 has no fileID system; we return the icon *path*
|
|
// string, same convention as `GetItemIcon`.)
|
|
// stackCount — CGItem descriptor `+OFF_DESCRIPTOR_STACK_COUNT` (the
|
|
// engine's `itemCount` return).
|
|
// isLocked — CGItem `+OFF_ITEM_CLIENT_LOCK & 1` (the `locked` return).
|
|
// quality — ItemStats `+OFF_ITEMSTATS_QUALITY`; nil when uncached
|
|
// (modern contract: `Enum.ItemQuality or nil`).
|
|
// isReadable — static page text OR the per-instance readable-text id,
|
|
// mirroring the engine's `readable` branch at `0x004F97BD`.
|
|
// hasLoot — ItemStats LOOTABLE flag (best-effort static approximation:
|
|
// vanilla can't track post-loot dynamic emptiness client-side).
|
|
// hyperlink — engine link builder via `Item::Link::FromCGItem` (enchant /
|
|
// random-suffix decorated, same as `GetContainerItemLink`).
|
|
// isFiltered — the bag search's verdict (`Container::Search`).
|
|
// hasNoValue — ItemStats sellPrice == 0.
|
|
// itemID — instance block itemID (`Item::ID::FromCGItem`).
|
|
// isBound — CGItem descriptor SOULBOUND flag (`C_Item.IsBound`'s bit).
|
|
// itemName — instance-decorated display name, base ItemStats name on miss.
|
|
//
|
|
// Returns nil for an empty slot or an invalid bag/slot index (modern
|
|
// contract). Cache-derived fields (icon / quality / hasLoot / hasNoValue /
|
|
// name) are omitted/nil when the item's static data isn't cached yet — the
|
|
// live per-instance fields (count / locked / bound / itemID / link) are always
|
|
// present. Passive: does not warm the item cache.
|
|
//
|
|
// ClassicAPI extension: a table as the 3rd argument is filled and returned
|
|
// instead of a new one. Bag addons call this for every slot on every
|
|
// BAG_UPDATE, and in Lua 5.0 a table per call is steady garbage-collector
|
|
// work; the fill clears the optional fields this item lacks. An empty slot
|
|
// still returns nil and leaves the table alone.
|
|
|
|
#include "Game.h"
|
|
#include "Offsets.h"
|
|
#include "container/Search.h"
|
|
#include "item/CGItem.h"
|
|
#include "item/ID.h"
|
|
#include "item/Icon.h"
|
|
#include "item/Link.h"
|
|
#include "item/Location.h"
|
|
#include "item/Record.h"
|
|
#include "item/StatAccum.h"
|
|
|
|
#include <cstdint>
|
|
|
|
namespace Container::ItemInfo {
|
|
|
|
namespace {
|
|
|
|
// Mirrors the engine's `readable` decision (0x004F97BD..0x004F97CF): a slot
|
|
// is readable when its static page text is set OR the per-instance
|
|
// readable-text id is non-zero (letters / server-authored notes).
|
|
bool ItemReadable(const uint8_t *item, const uint8_t *record) {
|
|
if (record != nullptr &&
|
|
Game::Read<uint32_t>(record, Offsets::OFF_ITEMSTATS_PAGE_TEXT) != 0)
|
|
return true;
|
|
const uint8_t *descriptor = Item::ObjectFields(item);
|
|
return descriptor != nullptr &&
|
|
Game::Read<uint32_t>(
|
|
descriptor, Offsets::OFF_DESCRIPTOR_READABLE_TEXT_ID) != 0;
|
|
}
|
|
|
|
int __fastcall Script_C_Container_GetContainerItemInfo(void *L) {
|
|
if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsNumber(L, 2)) {
|
|
Game::Lua::Error(L,
|
|
"Usage: C_Container.GetContainerItemInfo(containerIndex, slotIndex)");
|
|
return 0;
|
|
}
|
|
const bool fill = Game::Lua::Type(L, 3) == Game::Lua::TYPE_TABLE;
|
|
|
|
// Leaves the stack as it is, so a `buf` at index 3 is still there below.
|
|
const uint8_t *item = Item::Location::ResolveBagArgs(L);
|
|
if (item == nullptr) {
|
|
Game::Lua::PushNil(L); // empty slot / bad index → nil (modern contract)
|
|
return 1;
|
|
}
|
|
|
|
// --- Gather everything into locals BEFORE touching the Lua stack. The
|
|
// engine link/name builders and cache/DBC reads below are pure C; keeping
|
|
// them off the half-built table avoids any stack interleaving. ---
|
|
const int itemID = Item::ID::FromCGItem(item);
|
|
const uint8_t *record = (itemID > 0)
|
|
? Item::PeekRecord(static_cast<uint32_t>(itemID))
|
|
: nullptr;
|
|
|
|
// Live per-instance descriptor fields.
|
|
const uint8_t *descriptor = Item::ObjectFields(item);
|
|
int stackCount = 0;
|
|
bool isBound = false;
|
|
if (descriptor != nullptr) {
|
|
stackCount = Game::Read<int>(
|
|
descriptor, Offsets::OFF_DESCRIPTOR_STACK_COUNT);
|
|
isBound = (Game::Read<uint32_t>(
|
|
descriptor, Offsets::OFF_DESCRIPTOR_FLAGS) &
|
|
Offsets::ITEM_FLAG_SOULBOUND) != 0;
|
|
}
|
|
const bool isLocked = (Game::Read<uint32_t>(
|
|
item, Offsets::OFF_ITEM_CLIENT_LOCK) &
|
|
Offsets::ITEM_CLIENT_LOCK_BIT) != 0;
|
|
const bool isReadable = ItemReadable(item, record);
|
|
|
|
// Static template fields (cache-dependent).
|
|
bool haveIcon = false;
|
|
char iconPath[260];
|
|
bool haveQuality = false;
|
|
uint32_t quality = 0;
|
|
bool hasLoot = false;
|
|
bool hasNoValue = false;
|
|
if (record != nullptr) {
|
|
const uint32_t displayInfoID = Game::Read<uint32_t>(
|
|
record, Offsets::OFF_ITEMSTATS_DISPLAY_INFO_ID);
|
|
haveIcon = Item::Icon::PathForDisplayInfoID(displayInfoID, iconPath, sizeof(iconPath));
|
|
quality = Game::Read<uint32_t>(
|
|
record, Offsets::OFF_ITEMSTATS_QUALITY);
|
|
haveQuality = true;
|
|
const uint32_t flags = Game::Read<uint32_t>(
|
|
record, Offsets::OFF_ITEMSTATS_FLAGS);
|
|
hasLoot = (flags & Offsets::ITEM_FLAG_LOOTABLE) != 0;
|
|
hasNoValue = Game::Read<uint32_t>(
|
|
record, Offsets::OFF_ITEMSTATS_SELL_PRICE) == 0;
|
|
}
|
|
|
|
const char *hyperlink = Item::Link::FromCGItem(item);
|
|
|
|
char nameBuf[256];
|
|
const char *itemName = nullptr;
|
|
if (Item::Link::NameFromCGItem(item, nameBuf, sizeof(nameBuf))) {
|
|
itemName = nameBuf;
|
|
} else if (record != nullptr) {
|
|
const char *baseName = Game::Read<const char *>(
|
|
record, Offsets::OFF_ITEMSTATS_NAME);
|
|
if (baseName != nullptr && baseName[0] != '\0')
|
|
itemName = baseName;
|
|
}
|
|
|
|
const bool isFiltered = Container::Search::IsItemFiltered(item);
|
|
|
|
// --- Build the ContainerItemInfo table (only stack-safe SetField* now). ---
|
|
if (fill)
|
|
Game::Lua::PushValue(L, 3);
|
|
else
|
|
Game::Lua::NewTable(L);
|
|
if (haveIcon)
|
|
Game::Lua::SetFieldString(L, "iconFileID", iconPath);
|
|
else if (fill)
|
|
Game::Lua::SetFieldNil(L, "iconFileID");
|
|
Game::Lua::SetFieldNumber(L, "stackCount", static_cast<double>(stackCount));
|
|
Game::Lua::SetFieldBool(L, "isLocked", isLocked);
|
|
if (haveQuality)
|
|
Game::Lua::SetFieldNumber(L, "quality", static_cast<double>(quality));
|
|
else if (fill)
|
|
Game::Lua::SetFieldNil(L, "quality");
|
|
Game::Lua::SetFieldBool(L, "isReadable", isReadable);
|
|
Game::Lua::SetFieldBool(L, "hasLoot", hasLoot);
|
|
if (hyperlink != nullptr && hyperlink[0] != '\0')
|
|
Game::Lua::SetFieldString(L, "hyperlink", hyperlink);
|
|
else if (fill)
|
|
Game::Lua::SetFieldNil(L, "hyperlink");
|
|
Game::Lua::SetFieldBool(L, "isFiltered", isFiltered);
|
|
Game::Lua::SetFieldBool(L, "hasNoValue", hasNoValue);
|
|
Game::Lua::SetFieldNumber(L, "itemID", static_cast<double>(itemID));
|
|
Game::Lua::SetFieldBool(L, "isBound", isBound);
|
|
if (itemName != nullptr)
|
|
Game::Lua::SetFieldString(L, "itemName", itemName);
|
|
else if (fill)
|
|
Game::Lua::SetFieldNil(L, "itemName");
|
|
return 1;
|
|
}
|
|
|
|
const Game::Doc::Field kContainerItemInfoFields[] = {
|
|
Game::Doc::Opt("iconFileID", "string", nullptr,
|
|
"Icon texture path. Nil until the item's data is cached."),
|
|
Game::Doc::Req("stackCount", "number"),
|
|
Game::Doc::Req("isLocked", "bool"),
|
|
Game::Doc::Opt("quality", "ItemQuality", nullptr, "Nil until the item's data is cached."),
|
|
Game::Doc::Req("isReadable", "bool"),
|
|
Game::Doc::Req("hasLoot", "bool"),
|
|
Game::Doc::Opt("hyperlink", "string"),
|
|
Game::Doc::Req("isFiltered", "bool", "True when the bag search hides the item."),
|
|
Game::Doc::Req("hasNoValue", "bool"),
|
|
Game::Doc::Req("itemID", "number"),
|
|
Game::Doc::Req("isBound", "bool"),
|
|
Game::Doc::Opt("itemName", "string", nullptr, "Nil until the item's data is cached."),
|
|
};
|
|
const Game::Doc::Structure kContainerItemInfo{"ContainerItemInfo", "Container",
|
|
kContainerItemInfoFields};
|
|
|
|
const Game::Doc::Field kArgs[] = {
|
|
Game::Doc::Req("containerIndex", "BagIndex"),
|
|
Game::Doc::Req("slotIndex", "luaIndex"),
|
|
Game::Doc::Opt("buf", "table", nullptr,
|
|
"A table to write the item into and return instead of a new one."),
|
|
};
|
|
const Game::Doc::Field kRets[] = {
|
|
Game::Doc::Opt("containerInfo", "ContainerItemInfo", nullptr, "Nil when the slot is empty."),
|
|
};
|
|
const Game::Doc::Function kGetContainerItemInfo{"The item in a bag slot.", kArgs, kRets};
|
|
|
|
void RegisterLuaFunctions() {
|
|
Game::Lua::RegisterTableFunction("C_Container", "GetContainerItemInfo",
|
|
&Script_C_Container_GetContainerItemInfo,
|
|
&kGetContainerItemInfo);
|
|
}
|
|
|
|
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
|
|
|
|
} // namespace
|
|
|
|
} // namespace Container::ItemInfo
|