diff --git a/src/DllMain.cpp b/src/DllMain.cpp index 24b9fe02..753107e0 100644 --- a/src/DllMain.cpp +++ b/src/DllMain.cpp @@ -16,6 +16,7 @@ #include "MinHook.h" #include "Offsets.h" #include "item/Bound.h" +#include "item/ID.h" #include "item/Info.h" #include "quest/Log.h" #include "spell/Info.h" @@ -42,6 +43,7 @@ static void __fastcall LoadScriptFunctions_h() { Spell::Info::RegisterLuaFunctions(); Quest::Log::RegisterLuaFunctions(); Item::Bound::RegisterLuaFunctions(); + Item::ID::RegisterLuaFunctions(); Item::Info::RegisterLuaFunctions(); } diff --git a/src/Offsets.h b/src/Offsets.h index 6eef9c08..df0bad91 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -61,6 +61,22 @@ enum Offsets { OFF_DESCRIPTOR_FLAGS = 0x3C, ITEM_FLAG_SOULBOUND = 0x01, + // CGItem has TWO descriptor-like pointers and they hold different things: + // +0x114 = m_objectFields (the UpdateField array we read FLAGS from at + // +0x3C). For item instances, OBJECT_FIELD_ENTRY in this array + // is empirically 0 — don't try to read itemID from here. + // +0x08 = a separate identification block. Contains the item's GUID at + // +0x00 (qword) and the itemID at +0x0C (dword). + // The canonical "look up the cache record for this item" sequence is: + // item = GetItemBySlot(invMgr, slot) + // id = *(uint32_t *)(*(void **)(item + 0x08) + 0x0C) + // _GetRecord(cache, id, ...) + // Verified at 0x004C8B1F-2D (inventory→cache lookup right after a + // GetItemBySlot call). The same shape appears in many other inventory + // sites that call _GetRecord. + OFF_ITEM_INSTANCE_BLOCK = 0x08, + OFF_INSTANCE_BLOCK_ITEM_ID = 0x0C, + // Item stats cache (the client-side cache of ItemSparse-style records that // gets populated by SMSG_ITEM_QUERY_SINGLE_RESPONSE). The cache instance // lives directly at this VA — `_GetRecord`'s `this` argument is the literal diff --git a/src/item/Bound.cpp b/src/item/Bound.cpp index 251bb04b..72ff64b2 100644 --- a/src/item/Bound.cpp +++ b/src/item/Bound.cpp @@ -15,25 +15,12 @@ #include "Game.h" #include "Offsets.h" +#include "item/Location.h" #include namespace Item::Bound { -using GetItemBySlot_t = void *(__thiscall *)(void *thisInvMgr, int slot); -using PackBagSlot_t = int(__fastcall *)(void *L, void **outInvMgr, - int *outLinearSlot, int *outUnused); -using ResolveUnitToken_t = void *(__fastcall *)(const char *token); - -static void *ResolveActivePlayerInvMgr() { - auto ResolveUnitToken = - reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); - auto *player = static_cast(ResolveUnitToken("player")); - if (player == nullptr) - return nullptr; - return player + Offsets::OFF_PLAYER_INVENTORY_MANAGER; -} - static bool ItemIsSoulbound(const uint8_t *item) { auto *descriptor = *reinterpret_cast( item + Offsets::OFF_ITEM_DESCRIPTOR); @@ -44,80 +31,15 @@ static bool ItemIsSoulbound(const uint8_t *item) { return (flags & Offsets::ITEM_FLAG_SOULBOUND) != 0; } -// Reads `loc.fieldName` and returns it as an int. Returns false via the -// boolean result if the field is missing or non-numeric. Always leaves the -// Lua stack as it found it. -static bool TryReadIntField(void *L, int locIdx, const char *fieldName, int *out) { - Game::Lua::PushString(L, fieldName); - Game::Lua::GetTable(L, locIdx); // pops key, pushes value - const bool ok = Game::Lua::IsNumber(L, -1); - if (ok) - *out = static_cast(Game::Lua::ToNumber(L, -1)); - Game::Lua::SetTop(L, -2); // pop value (or nil) - return ok; -} - -static bool LookupEquipment(int luaSlot, bool *outBound) { - void *invMgr = ResolveActivePlayerInvMgr(); - if (invMgr == nullptr) - return false; - // 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); - auto *item = static_cast(GetItemBySlot(invMgr, slot)); - *outBound = (item != nullptr) && ItemIsSoulbound(item); - return true; -} - -static bool LookupBag(void *L, int bagID, int slotIndex, bool *outBound) { - // PackBagSlot reads bagID/slotIndex from Lua stack[1] and stack[2] (it's - // designed to be called from a Lua-callback context like - // Script_GetContainerItemInfo). Replace the stack so positions 1 and 2 - // hold the raw values for PackBagSlot's internal lua_tonumber reads. - Game::Lua::SetTop(L, 0); - Game::Lua::PushNumber(L, static_cast(bagID)); - Game::Lua::PushNumber(L, static_cast(slotIndex)); - - void *invMgr = nullptr; - int linearSlot = 0; - int unused = 0; - auto PackBagSlot = reinterpret_cast(Offsets::FUN_PACK_BAG_SLOT); - if (!PackBagSlot(L, &invMgr, &linearSlot, &unused) || invMgr == nullptr) - return false; - - auto GetItemBySlot = reinterpret_cast( - Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT); - auto *item = static_cast(GetItemBySlot(invMgr, linearSlot)); - *outBound = (item != nullptr) && ItemIsSoulbound(item); - return true; -} - static int __fastcall Script_IsBound(void *L) { if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { Game::Lua::Error(L, "Usage: C_Item.IsBound(itemLocation)"); return 0; } - bool bound = false; - int eqSlot = 0; - if (TryReadIntField(L, 1, "equipmentSlotIndex", &eqSlot)) { - LookupEquipment(eqSlot, &bound); - Game::Lua::PushBoolean(L, bound ? 1 : 0); - return 1; - } - - int bagID = 0, slotIndex = 0; - if (TryReadIntField(L, 1, "bagID", &bagID) && - TryReadIntField(L, 1, "slotIndex", &slotIndex)) { - LookupBag(L, bagID, slotIndex, &bound); - Game::Lua::PushBoolean(L, bound ? 1 : 0); - return 1; - } - - Game::Lua::PushBoolean(L, 0); + const uint8_t *item = Item::Location::Resolve(L, 1); + const bool bound = (item != nullptr) && ItemIsSoulbound(item); + Game::Lua::PushBoolean(L, bound ? 1 : 0); return 1; } diff --git a/src/item/ID.cpp b/src/item/ID.cpp new file mode 100644 index 00000000..6b7d144f --- /dev/null +++ b/src/item/ID.cpp @@ -0,0 +1,51 @@ +// 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 "ID.h" + +#include "Game.h" +#include "Offsets.h" +#include "item/Location.h" + +#include + +namespace Item::ID { + +static int __fastcall Script_GetItemID(void *L) { + if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) { + Game::Lua::Error(L, "Usage: C_Item.GetItemID(itemLocation)"); + return 0; + } + + const uint8_t *item = Item::Location::Resolve(L, 1); + if (item == nullptr) { + Game::Lua::PushNil(L); + return 1; + } + auto *instance = *reinterpret_cast( + item + Offsets::OFF_ITEM_INSTANCE_BLOCK); + if (instance == nullptr) { + Game::Lua::PushNil(L); + return 1; + } + const uint32_t itemID = *reinterpret_cast( + instance + Offsets::OFF_INSTANCE_BLOCK_ITEM_ID); + Game::Lua::PushNumber(L, static_cast(itemID)); + return 1; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_Item", "GetItemID", &Script_GetItemID); +} + +} // namespace Item::ID diff --git a/src/item/ID.h b/src/item/ID.h new file mode 100644 index 00000000..337afa8d --- /dev/null +++ b/src/item/ID.h @@ -0,0 +1,20 @@ +// 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 . + +#pragma once + +namespace Item::ID { + +void RegisterLuaFunctions(); + +} // namespace Item::ID diff --git a/src/item/Location.cpp b/src/item/Location.cpp new file mode 100644 index 00000000..1e91b61a --- /dev/null +++ b/src/item/Location.cpp @@ -0,0 +1,102 @@ +// 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 "Location.h" + +#include "../Game.h" +#include "../Offsets.h" + +namespace Item::Location { + +namespace { + +using GetItemBySlot_t = void *(__thiscall *)(void *thisInvMgr, int slot); +using PackBagSlot_t = int(__fastcall *)(void *L, void **outInvMgr, int *outLinearSlot, + int *outUnused); +using ResolveUnitToken_t = void *(__fastcall *)(const char *token); + +void *ResolveActivePlayerInvMgr() { + auto ResolveUnitToken = + reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); + auto *player = static_cast(ResolveUnitToken("player")); + if (player == nullptr) + return nullptr; + return player + Offsets::OFF_PLAYER_INVENTORY_MANAGER; +} + +// Reads `loc.fieldName` and returns it as an int. Returns false via the +// boolean result if the field is missing or non-numeric. Always leaves the +// Lua stack as it found it. +bool TryReadIntField(void *L, int locIdx, const char *fieldName, int *out) { + Game::Lua::PushString(L, fieldName); + Game::Lua::GetTable(L, locIdx); // pops key, pushes value + const bool ok = Game::Lua::IsNumber(L, -1); + if (ok) + *out = static_cast(Game::Lua::ToNumber(L, -1)); + Game::Lua::SetTop(L, -2); // pop value (or nil) + 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 + // Script_GetContainerItemInfo). Replace the stack so positions 1 and 2 + // hold the raw values for PackBagSlot's internal lua_tonumber reads. + Game::Lua::SetTop(L, 0); + Game::Lua::PushNumber(L, static_cast(bagID)); + Game::Lua::PushNumber(L, static_cast(slotIndex)); + + void *invMgr = nullptr; + int linearSlot = 0; + int unused = 0; + auto PackBagSlot = reinterpret_cast(Offsets::FUN_PACK_BAG_SLOT); + if (!PackBagSlot(L, &invMgr, &linearSlot, &unused) || invMgr == nullptr) + return nullptr; + + auto GetItemBySlot = reinterpret_cast( + Offsets::FUN_ITEMMGR_GET_ITEM_BY_SLOT); + return static_cast(GetItemBySlot(invMgr, linearSlot)); +} + +} // namespace + +const uint8_t *Resolve(void *L, int locIdx) { + if (Game::Lua::Type(L, locIdx) != Game::Lua::TYPE_TABLE) + return nullptr; + + int eqSlot = 0; + if (TryReadIntField(L, locIdx, "equipmentSlotIndex", &eqSlot)) + return ResolveEquipmentSlot(eqSlot); + + int bagID = 0, slotIndex = 0; + if (TryReadIntField(L, locIdx, "bagID", &bagID) && + TryReadIntField(L, locIdx, "slotIndex", &slotIndex)) + return ResolveBagSlot(L, bagID, slotIndex); + + return nullptr; +} + +} // namespace Item::Location diff --git a/src/item/Location.h b/src/item/Location.h new file mode 100644 index 00000000..8b2f4df5 --- /dev/null +++ b/src/item/Location.h @@ -0,0 +1,37 @@ +// 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 . + +#pragma once + +#include + +namespace Item::Location { + +// Resolves an item-location table at Lua stack index `locIdx` to a `CGItem *` +// pointer (returned as a raw byte pointer for use with `OFF_*` offsets). +// +// Accepts the same shapes the modern `ItemLocation` mixin uses: +// { equipmentSlotIndex = N } 1-based slot index, character pane +// { bagID = B, slotIndex = S } both required, bag/slot tuple +// +// Returns `nullptr` when the stack value isn't a table, the table doesn't +// carry a valid location, or the resolved slot is empty. Does NOT raise a +// Lua error — callers should validate the argument type beforehand if they +// want a typed error message. +// +// Walking the bag path stomps the Lua stack (a requirement of the engine's +// `PackBagSlot` helper, which reads its inputs from stack[1] and stack[2]). +// Only safe to call from inside a Lua callback. +const uint8_t *Resolve(void *L, int locIdx); + +} // namespace Item::Location