Files
ClassicAPI/src/item/scrape/InboxID.cpp
T
Brues c54556f6a1 refactor: centralize engine field/global access via Game::Read/Ref/Ptr
Replace the raw `*reinterpret_cast<const T *>(base + Offsets::OFF_X)`
idiom (and its double-cast variant when base wasn't already a byte
pointer) with three typed accessors in Game.h:

  Game::Read<T>(base, offset)  field value read
  Game::Ref<T>(base, offset)   writable field lvalue
  Game::Ptr<T>(base, offset)   pointer to a field (arrays / out-params)
  Game::Read<T>(address) / Ref<T>(address)  fixed-VA globals

Pure mechanical refactor, zero behavior change: ~330 cast sites across
84 files collapse to 651 helper calls. Offsets stay in Offsets.h per the
single-source-of-truth rule — the helpers only remove the cast noise and
fold away the inner `reinterpret_cast<uint8_t*>(obj)` base casts.

Left as raw casts by design: FUN_* function-pointer casts, void*/char*
VA handles passed as arguments (caches, method registries), subobject-
anchor pointers fed to __thiscall helpers, `void *volatile` player-ptr
readiness gates (converting would strip volatile), and VA-as-array-base
captures that are subscripted later.
2026-08-19 11:01:19 -05:00

65 lines
2.2 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/>.
#include "Game.h"
#include "Offsets.h"
#include <cstdint>
namespace Item::InboxID {
// `GetInboxItemID(mailID)` — itemID companion to `GetInboxItem`
// (which already returns name/texture/count/quality/canUse but not
// itemID). nil for empty / OOR mail slot, or when the mail entry
// has no attached item (gold-only mail).
static int __fastcall Script_GetInboxItemID(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: GetInboxItemID(mailID)");
return 0;
}
const int mailID = static_cast<int>(Game::Lua::ToNumber(L, 1)) - 1;
if (mailID < 0)
return 0;
const int count = static_cast<int>(Game::Read<uint32_t>(
Offsets::VAR_INBOX_COUNT));
if (mailID >= count)
return 0;
// `VAR_INBOX_ENTRIES` is a pointer slot, not the array — the
// engine reads it via `MOV ECX, [imm32]; MOV EDI, [ECX + idx*4]`
// (two derefs total before the entry pointer is in hand).
auto **entries = Game::Read<const uint8_t **>(Offsets::VAR_INBOX_ENTRIES);
if (entries == nullptr)
return 0;
auto *entry = entries[mailID];
if (entry == nullptr)
return 0;
const int itemID = static_cast<int>(Game::Read<uint32_t>(
entry, Offsets::OFF_INBOX_ENTRY_ITEM_ID));
if (itemID == 0)
return 0;
Game::Lua::PushNumber(L, static_cast<double>(itemID));
return 1;
}
static void RegisterLuaFunctions() {
Game::Lua::RegisterGlobalFunction("GetInboxItemID", &Script_GetInboxItemID);
}
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace Item::InboxID