From c01dceabb87a9725165b54d17e5083b3ce3739f4 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sat, 25 Jul 2026 13:19:04 -0500 Subject: [PATCH] unit: class/race fall back to the player-info cache out of range UnitClassBase / UnitRaceBase resolved via FUN_RESOLVE_UNIT_TOKEN and returned (nil,nil) for a groupmate on another map -- but the engine's own UnitClass / UnitRace fall back to the cached player-info record there (verified in-game: UnitClass("raid1")="Priest", UnitRace("raid1")="Human" while our descriptor path yielded nil). Give ours the same fallback: on a descriptor miss, read class (+0x140) / race (+0x138) from the NameCache record by GUID. Verified fixed: UnitClassBase("raid1")=("PRIEST",5), UnitRaceBase("raid1")=("Human",1). That engine-NameCache peek existed in four places (the two above plus loot/History and player/Info). Collapsed them into one primitive, Unit::Identity::PlayerInfoRecord(guid) -> record* (null callback = peek only), and pointed all four at it. FUN_PLAYER_INFO_LOOKUP is now called in exactly one spot. --- src/loot/History.cpp | 13 +------------ src/player/Info.cpp | 27 +++++---------------------- src/unit/ClassBase.cpp | 29 +++++++++++++++++------------ src/unit/Identity.cpp | 18 ++++++++++++++++++ src/unit/Identity.h | 9 +++++++++ src/unit/RaceBase.cpp | 29 +++++++++++++++++------------ 6 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/loot/History.cpp b/src/loot/History.cpp index 28f1c67f..06bda756 100644 --- a/src/loot/History.cpp +++ b/src/loot/History.cpp @@ -92,18 +92,7 @@ int MapRollType(int packetType) { // have gone). Guaranteed loaded at hook time: the roll handler's wrapper // gates on this same lookup succeeding. Returns nullptr if not cached. const uint8_t *LookupPlayerRecord(uint64_t guid) { - if (guid == 0) - return nullptr; - using Lookup_t = const uint8_t *(__thiscall *)(void *cache, uint32_t lo, - uint32_t hi, void *cookie, - void *cb, void *ud, char flag); - auto fn = reinterpret_cast( - static_cast(Offsets::FUN_PLAYER_INFO_LOOKUP)); - uint32_t cookie[2] = {0, 0}; - return fn(reinterpret_cast( - static_cast(Offsets::VAR_PLAYER_NAME_CACHE)), - static_cast(guid), static_cast(guid >> 32), - cookie, nullptr, nullptr, 0); + return Unit::Identity::PlayerInfoRecord(guid); } const char *NameFromRecord(const uint8_t *rec) { diff --git a/src/player/Info.cpp b/src/player/Info.cpp index 7ef12bb5..f3a26dff 100644 --- a/src/player/Info.cpp +++ b/src/player/Info.cpp @@ -39,6 +39,7 @@ #include "Offsets.h" #include "dbc/Lookup.h" #include "guid/Guid.h" +#include "unit/Identity.h" #include #include @@ -47,20 +48,8 @@ namespace Player::Info { namespace { -// `FUN_0055F080` — `__thiscall(this=cache, guidLo, guidHi, &cookie, -// callback, userData, retryFlag) → entryData* or NULL`. -using LookupOrFetch_t = const uint8_t *(__thiscall *)( - void *cache, - uint32_t guidLo, - uint32_t guidHi, - uint64_t *cookie, - void *callback, - void *userData, - int retryFlag); - -// 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`. +// Thin shim: parses a GUID string and splits to hi/lo dwords. Shared parser +// lives in `Guid::Parse`. bool ParseGUID(const char *str, uint32_t &outHi, uint32_t &outLo) { uint64_t value = 0; if (!Guid::Parse(str, &value)) @@ -82,14 +71,8 @@ int __fastcall Script_GetPlayerInfoByGUID(void *L) { if (hi == 0 && lo == 0) return 0; - auto fn = reinterpret_cast(Offsets::FUN_PLAYER_INFO_LOOKUP); - auto *cache = reinterpret_cast( - static_cast(Offsets::VAR_PLAYER_NAME_CACHE)); - - uint64_t cookie = 0; - const uint8_t *entry = fn(cache, lo, hi, &cookie, - nullptr /*callback*/, nullptr /*userData*/, - 0 /*retryFlag*/); + const uint8_t *entry = Unit::Identity::PlayerInfoRecord( + (static_cast(hi) << 32) | lo); const char *name = nullptr; const char *realm = nullptr; diff --git a/src/unit/ClassBase.cpp b/src/unit/ClassBase.cpp index d9bac2d0..a044d2bd 100644 --- a/src/unit/ClassBase.cpp +++ b/src/unit/ClassBase.cpp @@ -77,25 +77,30 @@ int __fastcall Script_UnitClassBase(void *L) { // the in-world descriptor exists), so this resolves at addon-load // instead of returning nil until the player object spawns. All other // tokens go through the unit descriptor. - uint8_t classByte; + uint8_t classByte = 0; if (Unit::Identity::IsPlayerToken(token)) { classByte = *reinterpret_cast(Offsets::VAR_PLAYER_CLASS_BYTE); } else { auto resolve = reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); auto *unit = static_cast(resolve(token)); - if (unit == nullptr) { - Game::Lua::PushNil(L); - Game::Lua::PushNil(L); - return 2; + if (unit != nullptr) { + auto *desc = *reinterpret_cast( + unit + Offsets::OFF_UNIT_DESCRIPTOR); + if (desc != nullptr) + classByte = *(desc + Offsets::OFF_UNIT_DESCRIPTOR_CLASS_BYTE); } - auto *desc = *reinterpret_cast( - unit + Offsets::OFF_UNIT_DESCRIPTOR); - if (desc == nullptr) { - Game::Lua::PushNil(L); - Game::Lua::PushNil(L); - return 2; + // No live descriptor (out of range / different map) → fall back to the + // engine's player-info cache, matching what UnitClass itself does out + // of range (class at OFF_PLAYER_INFO_CLASS). `token` is already valid + // here — resolve() raises on a bad token before this — so GuidForToken + // won't raise. + if (classByte == 0) { + const uint8_t *rec = Unit::Identity::PlayerInfoRecord( + Unit::Identity::GuidForToken(token)); + if (rec != nullptr) + classByte = static_cast(*reinterpret_cast( + rec + Offsets::OFF_PLAYER_INFO_CLASS)); } - classByte = *(desc + Offsets::OFF_UNIT_DESCRIPTOR_CLASS_BYTE); } if (classByte == 0) { Game::Lua::PushNil(L); diff --git a/src/unit/Identity.cpp b/src/unit/Identity.cpp index 7bdf3b8a..47d91d18 100644 --- a/src/unit/Identity.cpp +++ b/src/unit/Identity.cpp @@ -164,6 +164,24 @@ uint64_t GuidForToken(const char *token) { return fn(token); } +const uint8_t *PlayerInfoRecord(uint64_t guid) { + if (guid == 0) + return nullptr; + // Peek the engine's player-info NameCache: a NULL callback means "look up + // only, don't fire a query" (see the FUN_PLAYER_INFO_LOOKUP note in + // Offsets.h). Returns the entry data block for a cached GUID, else null. + using Lookup_t = const uint8_t *(__thiscall *)(void *cache, uint32_t lo, + uint32_t hi, void *cookie, + void *cb, void *ud, char flag); + auto fn = reinterpret_cast( + static_cast(Offsets::FUN_PLAYER_INFO_LOOKUP)); + uint32_t cookie[2] = {0, 0}; + return fn(reinterpret_cast( + static_cast(Offsets::VAR_PLAYER_NAME_CACHE)), + static_cast(guid), static_cast(guid >> 32), + cookie, nullptr, nullptr, 0); +} + // `UnitGUID(unit)` — returns the unit's 64-bit GUID formatted as a // hex string `"0xHHHHHHHHLLLLLLLL"` (16 hex digits, hi dword first). // diff --git a/src/unit/Identity.h b/src/unit/Identity.h index aa015e70..1f980246 100644 --- a/src/unit/Identity.h +++ b/src/unit/Identity.h @@ -86,4 +86,13 @@ uint64_t GuidForToken(const char *token); // `buf` should be at least 32 bytes. const char *TokenFromGUID(uint64_t guid, char *buf, size_t bufSize); +// The engine's cached player-info record for `guid` — the NameCache data block +// (name @ `OFF_PLAYER_INFO_NAME`, realm, race @ `_RACE`, sex, class @ `_CLASS`), +// or null if the GUID isn't cached. Object-independent: it resolves players who +// are out of range / on another map / have left the group, since the cache is +// populated on sync (SMSG_NAME_QUERY_RESPONSE). The single primitive for "what +// do we know about this player GUID" — read fields via the `OFF_PLAYER_INFO_*` +// offsets. Called with a NULL callback, so it only peeks (never fires a query). +const uint8_t *PlayerInfoRecord(uint64_t guid); + } // namespace Unit::Identity diff --git a/src/unit/RaceBase.cpp b/src/unit/RaceBase.cpp index 92f580e1..8885c93a 100644 --- a/src/unit/RaceBase.cpp +++ b/src/unit/RaceBase.cpp @@ -57,25 +57,30 @@ int __fastcall Script_UnitRaceBase(void *L) { // the in-world descriptor exists), so this resolves at addon-load // instead of returning nil until the player object spawns. All other // tokens go through the unit descriptor. - uint8_t raceByte; + uint8_t raceByte = 0; if (Unit::Identity::IsPlayerToken(token)) { raceByte = *reinterpret_cast(Offsets::VAR_PLAYER_RACE_BYTE); } else { auto resolve = reinterpret_cast(Offsets::FUN_RESOLVE_UNIT_TOKEN); auto *unit = static_cast(resolve(token)); - if (unit == nullptr) { - Game::Lua::PushNil(L); - Game::Lua::PushNil(L); - return 2; + if (unit != nullptr) { + auto *desc = *reinterpret_cast( + unit + Offsets::OFF_UNIT_DESCRIPTOR); + if (desc != nullptr) + raceByte = *(desc + Offsets::OFF_UNIT_DESCRIPTOR_RACE_BYTE); } - auto *desc = *reinterpret_cast( - unit + Offsets::OFF_UNIT_DESCRIPTOR); - if (desc == nullptr) { - Game::Lua::PushNil(L); - Game::Lua::PushNil(L); - return 2; + // No live descriptor (out of range / different map) → fall back to the + // engine's player-info cache, matching what UnitRace itself does out of + // range (race at OFF_PLAYER_INFO_RACE). `token` is already valid here — + // resolve() raises on a bad token before this — so GuidForToken won't + // raise. + if (raceByte == 0) { + const uint8_t *rec = Unit::Identity::PlayerInfoRecord( + Unit::Identity::GuidForToken(token)); + if (rec != nullptr) + raceByte = static_cast(*reinterpret_cast( + rec + Offsets::OFF_PLAYER_INFO_RACE)); } - raceByte = *(desc + Offsets::OFF_UNIT_DESCRIPTOR_RACE_BYTE); } if (raceByte == 0) { Game::Lua::PushNil(L);