diff --git a/src/aura/Data.cpp b/src/aura/Data.cpp index be954b28..52fbc268 100644 --- a/src/aura/Data.cpp +++ b/src/aura/Data.cpp @@ -15,6 +15,7 @@ #include "Game.h" #include "Offsets.h" +#include "dbc/Lookup.h" #include @@ -37,17 +38,8 @@ const uint8_t *Descriptor(const uint8_t *unit) { } const uint8_t *SpellRecord(uint32_t spellID) { - if (spellID == 0) - return nullptr; - const int count = *reinterpret_cast( - static_cast(Offsets::VAR_SPELL_RECORD_COUNT)); - if (static_cast(spellID) > count) - return nullptr; - auto *records = *reinterpret_cast( - static_cast(Offsets::VAR_SPELL_RECORDS)); - if (records == nullptr) - return nullptr; - return records[spellID]; + return DBC::Record(Offsets::VAR_SPELL_RECORDS, + Offsets::VAR_SPELL_RECORD_COUNT, spellID); } int LocaleIndex() { @@ -71,19 +63,10 @@ const char *SpellIconPath(const uint8_t *spellRecord) { spellRecord + OFF_SPELL_ICON_ID); if (iconID <= 0) return nullptr; - const int iconCount = *reinterpret_cast( - static_cast(Offsets::VAR_SPELL_ICON_COUNT)); - if (iconID > iconCount) - return nullptr; - auto *iconRecords = *reinterpret_cast( - static_cast(Offsets::VAR_SPELL_ICON_RECORDS)); - if (iconRecords == nullptr) - return nullptr; - const uint8_t *iconRecord = iconRecords[iconID]; - if (iconRecord == nullptr) - return nullptr; - return *reinterpret_cast( - iconRecord + OFF_SPELLICON_PATH); + return DBC::StringField(Offsets::VAR_SPELL_ICON_RECORDS, + Offsets::VAR_SPELL_ICON_COUNT, + static_cast(iconID), + OFF_SPELLICON_PATH); } } // namespace diff --git a/src/charlist/Cache.cpp b/src/charlist/Cache.cpp index 53dfd1cb..7de05fca 100644 --- a/src/charlist/Cache.cpp +++ b/src/charlist/Cache.cpp @@ -39,9 +39,10 @@ #include "Game.h" #include "Offsets.h" +#include "dbc/Lookup.h" +#include "guid/Guid.h" #include -#include #include #include #include @@ -64,52 +65,6 @@ struct Snapshot { // recently-observed char-select state. Read by the Lua bindings. std::vector g_chars; -int CurrentLocale() { - return *reinterpret_cast( - static_cast(Offsets::VAR_LOCALE_INDEX)); -} - -// Looks up a localized name from a DBC's records-pointer + count -// globals, with a per-record offset to the locale-array. Returns -// nullptr on out-of-range index or missing record. -const char *LookupDBCName(uintptr_t recordsVar, uintptr_t countVar, - int id, int nameOffset) { - if (id < 0) - return nullptr; - const int count = *reinterpret_cast(countVar); - if (id > count) - return nullptr; - const uint8_t *const *records = - *reinterpret_cast(recordsVar); - if (records == nullptr) - return nullptr; - const uint8_t *record = records[id]; - if (record == nullptr) - return nullptr; - const char *name = *reinterpret_cast( - record + nameOffset + CurrentLocale() * 4); - return (name != nullptr && name[0] != '\0') ? name : nullptr; -} - -// Reads the englishRace / class-file-token field from a DBC -// record (single-string, not locale-array). -const char *LookupDBCField(uintptr_t recordsVar, uintptr_t countVar, - int id, int fieldOffset) { - if (id < 0) - return nullptr; - const int count = *reinterpret_cast(countVar); - if (id > count) - return nullptr; - const uint8_t *const *records = - *reinterpret_cast(recordsVar); - if (records == nullptr) - return nullptr; - const uint8_t *record = records[id]; - if (record == nullptr) - return nullptr; - return *reinterpret_cast(record + fieldOffset); -} - void TakeSnapshot() { g_chars.clear(); const int count = *reinterpret_cast( @@ -165,13 +120,13 @@ int __fastcall Script_GetCharacterInfo(void *L) { Game::Lua::PushString(L, s.name.c_str()); - const char *localRace = LookupDBCName( + const char *localRace = DBC::LocalizedField( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, s.raceID, Offsets::OFF_CHRRACES_NAMES); if (localRace != nullptr) Game::Lua::PushString(L, localRace); else Game::Lua::PushString(L, ""); - const char *localClass = LookupDBCName( + const char *localClass = DBC::LocalizedField( Offsets::VAR_CHRCLASSES_RECORDS, Offsets::VAR_CHRCLASSES_COUNT, s.classID, Offsets::OFF_CHRCLASSES_NAMES); if (localClass != nullptr) Game::Lua::PushString(L, localClass); @@ -179,13 +134,13 @@ int __fastcall Script_GetCharacterInfo(void *L) { Game::Lua::PushNumber(L, static_cast(s.level)); - const char *area = LookupDBCName( + const char *area = DBC::LocalizedField( Offsets::VAR_AREATABLE_RECORDS, Offsets::VAR_AREATABLE_COUNT, static_cast(s.areaID), Offsets::OFF_AREATABLE_NAMES); if (area != nullptr) Game::Lua::PushString(L, area); else Game::Lua::PushNil(L); - const char *englishRace = LookupDBCField( + const char *englishRace = DBC::StringField( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, s.raceID, Offsets::OFF_CHRRACES_FILENAME); Game::Lua::PushString(L, englishRace != nullptr ? englishRace : ""); @@ -196,11 +151,8 @@ int __fastcall Script_GetCharacterInfo(void *L) { // past 2^53. Format as `0x` + 16-hex-digit string (hi then lo, // matching `UnitGUID`'s convention) so addons can use it as a // stable string identifier. - char buf[24]; - std::snprintf(buf, sizeof(buf), "0x%08X%08X", - static_cast(s.guid >> 32), - static_cast(s.guid)); - Game::Lua::PushString(L, buf); + char buf[Guid::STRING_SIZE]; + Game::Lua::PushString(L, Guid::FormatAsString(s.guid, buf, sizeof buf)); return 8; } @@ -228,21 +180,21 @@ int __fastcall Script_GetCharacters(void *L) { Game::Lua::SetFieldString(L, "name", s.name.c_str()); - Game::Lua::SetFieldString(L, "localizedRace", LookupDBCName( + Game::Lua::SetFieldString(L, "localizedRace", DBC::LocalizedField( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, s.raceID, Offsets::OFF_CHRRACES_NAMES)); - Game::Lua::SetFieldString(L, "englishRace", LookupDBCField( + Game::Lua::SetFieldString(L, "englishRace", DBC::StringField( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, s.raceID, Offsets::OFF_CHRRACES_FILENAME)); - Game::Lua::SetFieldString(L, "localizedClass", LookupDBCName( + Game::Lua::SetFieldString(L, "localizedClass", DBC::LocalizedField( Offsets::VAR_CHRCLASSES_RECORDS, Offsets::VAR_CHRCLASSES_COUNT, s.classID, Offsets::OFF_CHRCLASSES_NAMES)); Game::Lua::SetFieldNumber(L, "level", static_cast(s.level)); - const char *area = LookupDBCName( + const char *area = DBC::LocalizedField( Offsets::VAR_AREATABLE_RECORDS, Offsets::VAR_AREATABLE_COUNT, static_cast(s.areaID), Offsets::OFF_AREATABLE_NAMES); if (area != nullptr) @@ -250,11 +202,9 @@ int __fastcall Script_GetCharacters(void *L) { Game::Lua::SetFieldNumber(L, "sex", static_cast(s.sex)); - char buf[24]; - std::snprintf(buf, sizeof(buf), "0x%08X%08X", - static_cast(s.guid >> 32), - static_cast(s.guid)); - Game::Lua::SetFieldString(L, "guid", buf); + char buf[Guid::STRING_SIZE]; + Game::Lua::SetFieldString(L, "guid", + Guid::FormatAsString(s.guid, buf, sizeof buf)); // Insert inner-table into outer-table at numeric key. Game::Lua::SetTable(L, -3); diff --git a/src/chat/CurrentGUID.cpp b/src/chat/CurrentGUID.cpp index 7e1577d6..b11f199f 100644 --- a/src/chat/CurrentGUID.cpp +++ b/src/chat/CurrentGUID.cpp @@ -41,9 +41,9 @@ #include "Game.h" #include "Offsets.h" +#include "guid/Guid.h" #include -#include namespace Chat::CurrentGUID { @@ -55,10 +55,10 @@ uint32_t g_currentGuidHi = 0; int __fastcall Script_GetCurrentChatGUID(void *L) { if (g_currentGuidLo == 0 && g_currentGuidHi == 0) return 0; - char buf[24]; - std::snprintf(buf, sizeof(buf), "0x%08X%08X", - g_currentGuidHi, g_currentGuidLo); - Game::Lua::PushString(L, buf); + const uint64_t guid = + (static_cast(g_currentGuidHi) << 32) | g_currentGuidLo; + char buf[Guid::STRING_SIZE]; + Game::Lua::PushString(L, Guid::FormatAsString(guid, buf, sizeof buf)); return 1; } diff --git a/src/dbc/Lookup.cpp b/src/dbc/Lookup.cpp new file mode 100644 index 00000000..a84bac92 --- /dev/null +++ b/src/dbc/Lookup.cpp @@ -0,0 +1,61 @@ +// 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 "Lookup.h" + +#include "Offsets.h" + +namespace DBC { + +namespace { + +const char *NormalizeEmpty(const char *s) { + return (s == nullptr || *s == '\0') ? nullptr : s; +} + +} // namespace + +const uint8_t *Record(uintptr_t recordsVar, uintptr_t countVar, uint32_t id) { + if (id == 0) + return nullptr; + const int count = *reinterpret_cast(countVar); + if (static_cast(id) > count) + return nullptr; + const uint8_t *const *records = + *reinterpret_cast(recordsVar); + if (records == nullptr) + return nullptr; + return records[id]; +} + +const char *LocalizedField(uintptr_t recordsVar, uintptr_t countVar, + uint32_t id, int offset) { + const uint8_t *record = Record(recordsVar, countVar, id); + if (record == nullptr) + return nullptr; + const int locale = *reinterpret_cast( + static_cast(Offsets::VAR_LOCALE_INDEX)); + const char *const *names = reinterpret_cast(record + offset); + return NormalizeEmpty(names[locale]); +} + +const char *StringField(uintptr_t recordsVar, uintptr_t countVar, + uint32_t id, int offset) { + const uint8_t *record = Record(recordsVar, countVar, id); + if (record == nullptr) + return nullptr; + return NormalizeEmpty( + *reinterpret_cast(record + offset)); +} + +} // namespace DBC diff --git a/src/dbc/Lookup.h b/src/dbc/Lookup.h new file mode 100644 index 00000000..ff1dcae3 --- /dev/null +++ b/src/dbc/Lookup.h @@ -0,0 +1,68 @@ +// 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 + +// `DBC::*` — shared readers for the engine's `.dbc` table cache. +// +// Every DBC instance the engine loads has the same 5-DWORD class shape +// in `.data`: records-pointer at `+0x08`, count at `+0x0C` (see +// `docs/DBCs.md`). Lookup boilerplate is the same across every table: +// +// 1. read count from `countVar` +// 2. bounds-check `id` against `[1, count]` (records are 1-based; +// `records[0]` is unused per the engine's storage convention) +// 3. deref records-pointer from `recordsVar` +// 4. index records[id] +// 5. null-guard the slot +// 6. read the field +// +// This module folds that chain into one of two readers depending on +// whether the field is a single string pointer or a 9-entry locale +// array (one ptr per supported locale, indexed by +// `VAR_LOCALE_INDEX`). Both return `nullptr` for OOR ids, empty +// slots, or missing/empty field values — callers compare against +// `nullptr` rather than checking individual error conditions. +namespace DBC { + +// Resolves `id` to its record pointer in the table whose records-base +// global is at `recordsVar` and count global at `countVar`. Returns +// `nullptr` for `id == 0`, OOR ids (> count), and unpopulated slots. +// Caller reads whatever fields it needs off the returned record. +// +// Use this when `LocalizedField` / `StringField` don't fit — e.g. for +// integer or float fields, for fields that point into a *secondary* +// DBC that needs its own chained lookup, or when iterating the whole +// table. +const uint8_t *Record(uintptr_t recordsVar, uintptr_t countVar, uint32_t id); + +// Reads a localized string field from a DBC record. The field at +// `offset` is treated as a 9-entry array of `const char *`, indexed +// by the engine's current locale at `VAR_LOCALE_INDEX`. +// +// Used for the `Name`/`Description` etc. fields on `Spell.dbc`, +// `ChrRaces.dbc`, `AreaTable.dbc`, and every other table whose +// schema marks the field as `Localized`. +const char *LocalizedField(uintptr_t recordsVar, uintptr_t countVar, + uint32_t id, int offset); + +// Reads a single-string field — the field at `offset` is a `const +// char *`, no locale array. Used for filename / english-name fields +// like `ChrRaces.dbc::ClientFilename` or +// `ChrClasses.dbc::ClassFile`. +const char *StringField(uintptr_t recordsVar, uintptr_t countVar, + uint32_t id, int offset); + +} // namespace DBC diff --git a/src/guid/Guid.cpp b/src/guid/Guid.cpp index bfee3859..ce40e66e 100644 --- a/src/guid/Guid.cpp +++ b/src/guid/Guid.cpp @@ -14,6 +14,7 @@ #include "Guid.h" #include +#include #include namespace Guid { @@ -41,6 +42,15 @@ bool Parse(const char *str, uint64_t *out) { return true; } +const char *FormatAsString(uint64_t guid, char *buf, std::size_t cap) { + if (buf == nullptr || cap < STRING_SIZE) + return ""; + std::snprintf(buf, cap, "0x%08X%08X", + static_cast(guid >> 32), + static_cast(guid)); + return buf; +} + Type Classify(uint64_t guid) { if (guid == 0) return Type::Unknown; diff --git a/src/guid/Guid.h b/src/guid/Guid.h index 0a5683dd..200b5c56 100644 --- a/src/guid/Guid.h +++ b/src/guid/Guid.h @@ -13,6 +13,7 @@ #pragma once +#include #include namespace Guid { @@ -75,4 +76,15 @@ inline bool IsGameObject(uint64_t guid) { return Classify(guid) == Type::Game inline bool IsDynamicObject(uint64_t guid) { return Classify(guid) == Type::DynamicObject; } inline bool IsCorpse(uint64_t guid) { return Classify(guid) == Type::Corpse; } +// Minimum buffer size for `FormatAsString`'s output — 16 hex digits + +// the `"0x"` prefix + a NUL terminator. +constexpr std::size_t STRING_SIZE = 19; + +// Formats `guid` as `"0xHHHHHHHHLLLLLLLL"` (high dword then low, +// matching what `UnitGUID` and `C_Item.GetItemGUID` return). Writes +// into `buf`; `cap` must be at least `STRING_SIZE` bytes. Returns the +// buffer pointer for chaining (e.g. +// `PushString(L, Guid::FormatAsString(g, buf, sizeof buf))`). +const char *FormatAsString(uint64_t guid, char *buf, std::size_t cap); + } // namespace Guid diff --git a/src/item/GUID.cpp b/src/item/GUID.cpp index 6e2f515f..80441552 100644 --- a/src/item/GUID.cpp +++ b/src/item/GUID.cpp @@ -13,10 +13,10 @@ #include "Game.h" #include "Offsets.h" +#include "guid/Guid.h" #include "item/Location.h" #include -#include namespace Item::GUID { @@ -50,12 +50,8 @@ static int __fastcall Script_C_Item_GetItemGUID(void *L) { if (guid == 0) return 0; - const uint32_t lo = static_cast(guid); - const uint32_t hi = static_cast(guid >> 32); - - char buf[24]; // "0x" + 16 hex digits + null - std::snprintf(buf, sizeof(buf), "0x%08X%08X", hi, lo); - Game::Lua::PushString(L, buf); + char buf[Guid::STRING_SIZE]; + Game::Lua::PushString(L, Guid::FormatAsString(guid, buf, sizeof buf)); return 1; } diff --git a/src/player/Info.cpp b/src/player/Info.cpp index db5d56b0..62c82a7e 100644 --- a/src/player/Info.cpp +++ b/src/player/Info.cpp @@ -37,6 +37,7 @@ #include "Game.h" #include "NameCache.h" #include "Offsets.h" +#include "dbc/Lookup.h" #include "guid/Guid.h" #include @@ -56,41 +57,6 @@ using LookupOrFetch_t = const uint8_t *(__thiscall *)( void *userData, int retryFlag); -// Reads a localized-or-filename string field out of a DBC record. -// `recordsVar`, `countVar` are the engine globals holding the -// records pointer / count for the DBC. `offset` is one of: -// - the filename field (single string ptr) — pass -// `indexByLocale=false` -// - the names array (9 locale string ptrs, indexed by -// [VAR_LOCALE_INDEX]) — pass `indexByLocale=true` -// Returns nullptr if `id` is out of range, the slot is empty, or -// the field is null/empty. -const char *DBCStringField(uintptr_t recordsVar, uintptr_t countVar, - uint32_t id, int offset, bool indexByLocale) { - if (id == 0) - return nullptr; - const int count = *reinterpret_cast(countVar); - if (static_cast(id) > count) - return nullptr; - const uint8_t *const *records = - *reinterpret_cast(recordsVar); - if (records == nullptr) - return nullptr; - const uint8_t *record = records[id]; - if (record == nullptr) - return nullptr; - const char *result; - if (indexByLocale) { - const int locale = *reinterpret_cast( - static_cast(Offsets::VAR_LOCALE_INDEX)); - const char *const *names = reinterpret_cast(record + offset); - result = names[locale]; - } else { - result = *reinterpret_cast(record + offset); - } - return (result == nullptr || *result == '\0') ? nullptr : result; -} - // Thin shim: parses a GUID string and splits to hi/lo dwords for the // engine's `__thiscall` cache lookup, which takes them as separate // args. Shared parser lives in `Guid::Parse`. @@ -157,18 +123,18 @@ int __fastcall Script_GetPlayerInfoByGUID(void *L) { realm = ""; } - const char *englishClass = DBCStringField( + const char *englishClass = DBC::StringField( Offsets::VAR_CHRCLASSES_RECORDS, Offsets::VAR_CHRCLASSES_COUNT, - classID, Offsets::OFF_CHRCLASSES_FILENAME, /*indexByLocale=*/false); - const char *localizedClass = DBCStringField( + classID, Offsets::OFF_CHRCLASSES_FILENAME); + const char *localizedClass = DBC::LocalizedField( Offsets::VAR_CHRCLASSES_RECORDS, Offsets::VAR_CHRCLASSES_COUNT, - classID, Offsets::OFF_CHRCLASSES_NAMES, /*indexByLocale=*/true); - const char *englishRace = DBCStringField( + classID, Offsets::OFF_CHRCLASSES_NAMES); + const char *englishRace = DBC::StringField( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, - race, Offsets::OFF_CHRRACES_FILENAME, /*indexByLocale=*/false); - const char *localizedRace = DBCStringField( + race, Offsets::OFF_CHRRACES_FILENAME); + const char *localizedRace = DBC::LocalizedField( Offsets::VAR_CHRRACES_RECORDS, Offsets::VAR_CHRRACES_COUNT, - race, Offsets::OFF_CHRRACES_NAMES, /*indexByLocale=*/true); + race, Offsets::OFF_CHRRACES_NAMES); Game::Lua::PushString(L, localizedClass ? localizedClass : ""); Game::Lua::PushString(L, englishClass ? englishClass : ""); diff --git a/src/talent/Tooltip.cpp b/src/talent/Tooltip.cpp index 2d073e85..83f6b828 100644 --- a/src/talent/Tooltip.cpp +++ b/src/talent/Tooltip.cpp @@ -13,6 +13,7 @@ #include "Game.h" #include "Offsets.h" +#include "dbc/Lookup.h" #include "spell/Tooltip.h" #include @@ -64,15 +65,9 @@ static bool FindTalentByID(uint32_t talentID, int *outTab, int *outIdx) { // DBC class shape (`+0x08` of class instance), indexed directly by // talentID; sparse — many slots are NULL across classes. static uint32_t LookupTalentRank1Spell(uint32_t talentID) { - const int maxID = *reinterpret_cast( - static_cast(Offsets::VAR_TALENT_DBC_COUNT)); - if (talentID == 0 || static_cast(talentID) > maxID) - return 0; - auto *records = *reinterpret_cast( - static_cast(Offsets::VAR_TALENT_DBC_RECORDS)); - if (records == nullptr) - return 0; - const uint8_t *rec = records[talentID]; + const uint8_t *rec = DBC::Record( + Offsets::VAR_TALENT_DBC_RECORDS, Offsets::VAR_TALENT_DBC_COUNT, + talentID); if (rec == nullptr) return 0; return *reinterpret_cast(rec + Offsets::OFF_TALENT_SPELL_RANK); diff --git a/src/unit/Identity.cpp b/src/unit/Identity.cpp index f3cee184..bfe48334 100644 --- a/src/unit/Identity.cpp +++ b/src/unit/Identity.cpp @@ -13,9 +13,9 @@ #include "Game.h" #include "Offsets.h" +#include "guid/Guid.h" #include -#include namespace Unit::Identity { @@ -61,12 +61,8 @@ static int __fastcall Script_UnitGUID(void *L) { if (guid == 0) return 0; - const uint32_t lo = static_cast(guid); - const uint32_t hi = static_cast(guid >> 32); - - char buf[24]; // "0x" + 16 hex digits + null = 19 bytes minimum - std::snprintf(buf, sizeof(buf), "0x%08X%08X", hi, lo); - Game::Lua::PushString(L, buf); + char buf[Guid::STRING_SIZE]; + Game::Lua::PushString(L, Guid::FormatAsString(guid, buf, sizeof buf)); return 1; }