DBC/GUID cleanup

This commit is contained in:
Brues
2026-05-14 14:24:11 -05:00
parent ef7ccb4723
commit e7c72b124e
11 changed files with 197 additions and 160 deletions
+7 -24
View File
@@ -15,6 +15,7 @@
#include "Game.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
#include <cstdint>
@@ -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<const int *>(
static_cast<uintptr_t>(Offsets::VAR_SPELL_RECORD_COUNT));
if (static_cast<int>(spellID) > count)
return nullptr;
auto *records = *reinterpret_cast<const uint8_t *const *const *>(
static_cast<uintptr_t>(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<const int *>(
static_cast<uintptr_t>(Offsets::VAR_SPELL_ICON_COUNT));
if (iconID > iconCount)
return nullptr;
auto *iconRecords = *reinterpret_cast<const uint8_t *const *const *>(
static_cast<uintptr_t>(Offsets::VAR_SPELL_ICON_RECORDS));
if (iconRecords == nullptr)
return nullptr;
const uint8_t *iconRecord = iconRecords[iconID];
if (iconRecord == nullptr)
return nullptr;
return *reinterpret_cast<const char *const *>(
iconRecord + OFF_SPELLICON_PATH);
return DBC::StringField(Offsets::VAR_SPELL_ICON_RECORDS,
Offsets::VAR_SPELL_ICON_COUNT,
static_cast<uint32_t>(iconID),
OFF_SPELLICON_PATH);
}
} // namespace
+15 -65
View File
@@ -39,9 +39,10 @@
#include "Game.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
#include "guid/Guid.h"
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
@@ -64,52 +65,6 @@ struct Snapshot {
// recently-observed char-select state. Read by the Lua bindings.
std::vector<Snapshot> g_chars;
int CurrentLocale() {
return *reinterpret_cast<const int *>(
static_cast<uintptr_t>(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<const int *>(countVar);
if (id > count)
return nullptr;
const uint8_t *const *records =
*reinterpret_cast<const uint8_t *const *const *>(recordsVar);
if (records == nullptr)
return nullptr;
const uint8_t *record = records[id];
if (record == nullptr)
return nullptr;
const char *name = *reinterpret_cast<const char *const *>(
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<const int *>(countVar);
if (id > count)
return nullptr;
const uint8_t *const *records =
*reinterpret_cast<const uint8_t *const *const *>(recordsVar);
if (records == nullptr)
return nullptr;
const uint8_t *record = records[id];
if (record == nullptr)
return nullptr;
return *reinterpret_cast<const char *const *>(record + fieldOffset);
}
void TakeSnapshot() {
g_chars.clear();
const int count = *reinterpret_cast<const int *>(
@@ -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<double>(s.level));
const char *area = LookupDBCName(
const char *area = DBC::LocalizedField(
Offsets::VAR_AREATABLE_RECORDS, Offsets::VAR_AREATABLE_COUNT,
static_cast<int>(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<uint32_t>(s.guid >> 32),
static_cast<uint32_t>(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<double>(s.level));
const char *area = LookupDBCName(
const char *area = DBC::LocalizedField(
Offsets::VAR_AREATABLE_RECORDS, Offsets::VAR_AREATABLE_COUNT,
static_cast<int>(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<double>(s.sex));
char buf[24];
std::snprintf(buf, sizeof(buf), "0x%08X%08X",
static_cast<uint32_t>(s.guid >> 32),
static_cast<uint32_t>(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);
+5 -5
View File
@@ -41,9 +41,9 @@
#include "Game.h"
#include "Offsets.h"
#include "guid/Guid.h"
#include <cstdint>
#include <cstdio>
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<uint64_t>(g_currentGuidHi) << 32) | g_currentGuidLo;
char buf[Guid::STRING_SIZE];
Game::Lua::PushString(L, Guid::FormatAsString(guid, buf, sizeof buf));
return 1;
}
+61
View File
@@ -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 <https://www.gnu.org/licenses/>.
#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<const int *>(countVar);
if (static_cast<int>(id) > count)
return nullptr;
const uint8_t *const *records =
*reinterpret_cast<const uint8_t *const *const *>(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<const int *>(
static_cast<uintptr_t>(Offsets::VAR_LOCALE_INDEX));
const char *const *names = reinterpret_cast<const char *const *>(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<const char *const *>(record + offset));
}
} // namespace DBC
+68
View File
@@ -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 <https://www.gnu.org/licenses/>.
#pragma once
#include <cstdint>
// `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
+10
View File
@@ -14,6 +14,7 @@
#include "Guid.h"
#include <cstddef>
#include <cstdio>
#include <cstring>
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<uint32_t>(guid >> 32),
static_cast<uint32_t>(guid));
return buf;
}
Type Classify(uint64_t guid) {
if (guid == 0)
return Type::Unknown;
+12
View File
@@ -13,6 +13,7 @@
#pragma once
#include <cstddef>
#include <cstdint>
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
+3 -7
View File
@@ -13,10 +13,10 @@
#include "Game.h"
#include "Offsets.h"
#include "guid/Guid.h"
#include "item/Location.h"
#include <cstdint>
#include <cstdio>
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<uint32_t>(guid);
const uint32_t hi = static_cast<uint32_t>(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;
}
+9 -43
View File
@@ -37,6 +37,7 @@
#include "Game.h"
#include "NameCache.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
#include "guid/Guid.h"
#include <cstdint>
@@ -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<const int *>(countVar);
if (static_cast<int>(id) > count)
return nullptr;
const uint8_t *const *records =
*reinterpret_cast<const uint8_t *const *const *>(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<const int *>(
static_cast<uintptr_t>(Offsets::VAR_LOCALE_INDEX));
const char *const *names = reinterpret_cast<const char *const *>(record + offset);
result = names[locale];
} else {
result = *reinterpret_cast<const char *const *>(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 : "");
+4 -9
View File
@@ -13,6 +13,7 @@
#include "Game.h"
#include "Offsets.h"
#include "dbc/Lookup.h"
#include "spell/Tooltip.h"
#include <cstdint>
@@ -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<const int *>(
static_cast<uintptr_t>(Offsets::VAR_TALENT_DBC_COUNT));
if (talentID == 0 || static_cast<int>(talentID) > maxID)
return 0;
auto *records = *reinterpret_cast<const uint8_t *const *const *>(
static_cast<uintptr_t>(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<const uint32_t *>(rec + Offsets::OFF_TALENT_SPELL_RANK);
+3 -7
View File
@@ -13,9 +13,9 @@
#include "Game.h"
#include "Offsets.h"
#include "guid/Guid.h"
#include <cstdint>
#include <cstdio>
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<uint32_t>(guid);
const uint32_t hi = static_cast<uint32_t>(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;
}