diff --git a/src/Offsets.h b/src/Offsets.h index 47f0e1de..4e0e040c 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -1751,6 +1751,37 @@ enum Offsets { // every one of them carries 5236 in its GUID. Read by `Unit::CreatureID`. OFF_UNIT_INSTANCE_ENTRY = 0x0C, + // ── Nameplates (CGNamePlateFrame) ────────────────────────────────── + // + // The same machinery as 3.3.5's (decomp: WoW/Source/Ui/NamePlateFrame.h, + // Object/ObjectClient/Unit_C.cpp), verified function by function: + // FUN_006086E0 = CGUnit_C::NamePlateAddOrUpdate — acquire (FUN_006087F0, + // the recycle-or-allocate GetNewNameplateFrame), bind (FUN_007CB6D0 = + // CGNamePlateFrame::Initialize, which copies the unit's GUID into the + // plate), then sort it onto the shown list (FUN_00608870 = + // NamePlateSort); + // FUN_00608A10 = CGUnit_C::DestroyNamePlate — unlink from the shown + // list, link onto the recycle list (head 0x00C4D920 / tail 0x00C4D91C), + // hide, zero unit+0xE60. Its callers are all real releases: the unit + // destructor FUN_005FB5E0, the despawn reset FUN_005FBB60, the name + // cache callback FUN_005FB840 (drops the plate so it rebuilds with the + // name), the show/hide decision, and FUN_0060F890 = + // NamePlateClearAll, which walks the shown list itself and resolves + // each plate's unit by GUID with FUN_00468460 — the model + // NamePlate::Walk follows. + // + // VAR_NAMEPLATE_LIST_HEAD is the shown list's first plate (0 or a + // low-bit-tagged sentinel ends it); each plate's next is at + // OFF_NAMEPLATE_LINK_NEXT. Sorted by NamePlateSort, which inserts a plate + // before the first one at the same or a nearer screen-centre distance — + // so the list runs FARTHEST first. A recycled plate keeps its old GUID + // until Initialize rebinds it; a plate on the shown list is always bound. + // unit+0xE60 is the unit's plate (CGUnit_C::m_namePlateFrame). + VAR_NAMEPLATE_LIST_HEAD = 0x00C4D92C, + OFF_NAMEPLATE_LINK_NEXT = 0x4E0, + OFF_NAMEPLATE_GUID = 0x4E8, + OFF_UNIT_NAMEPLATE = 0xE60, + // CGCreature client-side creature-data cache row. Populated by the // engine when an NPC GUID becomes visible (the same row the // `CreatureCache` SMSG fills with the templated NPC's name, diff --git a/src/nameplate/Events.cpp b/src/nameplate/Events.cpp index cebb4ac0..fcbf47de 100644 --- a/src/nameplate/Events.cpp +++ b/src/nameplate/Events.cpp @@ -14,12 +14,12 @@ // Nameplate lifecycle events: NAME_PLATE_CREATED, NAME_PLATE_UNIT_ADDED, // NAME_PLATE_UNIT_REMOVED. // -// Per-frame poll: walk visible CGUnits with allocated nameplates, diff -// against last frame's snapshot, fire events on real transitions only. -// The engine's internal hide/show cycle (z-order rebuilds, anchor -// changes, ~7 callers of `FUN_00608A10` that transiently zero `+0xE60`) -// is absorbed because we only compare with the previous *frame's* -// state, not every transient `unit + 0xE60` write. +// Per-frame poll: walk the engine's shown-plate list (`NamePlate::Walk`), +// diff against last frame's snapshot, fire events on real transitions only. +// Comparing frame to frame (instead of hooking acquire / release) keeps the +// hot per-unit `FUN_006086E0` un-hooked and folds a release and re-acquire +// inside one frame — the name-cache callback `FUN_005FB840` drops a plate so +// it rebuilds with the name — into no event at all. // // Payload shape (matches modern WoW exactly): // - `NAME_PLATE_CREATED` — `arg1` is the nameplate **Frame**. The @@ -107,17 +107,14 @@ std::vector g_slots; uint64_t g_removingGuid = 0; const void *g_removingPlate = nullptr; -// True iff `plate` is bound to some unit in THIS tick's walk — i.e. the engine -// pulled the frame off its freelist and rebound it to another unit in the gap -// between the tick that last saw it and the tick that noticed it was gone. -// Frames are pooled and reused (`FUN_006087F0`), so the pointer stays valid -// memory either way; what makes it unservable is that it now belongs to -// somebody else. -bool PlateReassigned(const void *plate) { - for (const auto &kv : g_currentTickPlates) - if (kv.second == plate) - return true; - return false; +// True iff the engine has rebound `plate` to a unit other than `guid` since +// the tick that last saw it. Frames are pooled and reused (`FUN_006087F0`), so +// the pointer stays valid memory either way; the plate's own GUID +// (OFF_NAMEPLATE_GUID — kept on a recycled plate until `Initialize` rebinds +// it) says whose it is now. +bool PlateReassigned(const void *plate, uint64_t guid) { + const auto *p = static_cast(plate); + return *reinterpret_cast(p + Offsets::OFF_NAMEPLATE_GUID) != guid; } // Assign `guid` the lowest free slot, reusing a vacated one when present and @@ -240,11 +237,7 @@ void OnWorldTick() { g_currentTickPlates.clear(); g_currentTickPlates.reserve(64); // typical visible-nameplate ceiling NamePlate::Walk::ForEachNamePlatedUnit( - [](const uint8_t *, const uint8_t *nameplate, - const uint8_t *instance) { - const uint64_t guid = *reinterpret_cast(instance); - if (guid == 0) - return; + [](const uint8_t *, const uint8_t *nameplate, uint64_t guid) { g_currentTickPlates.emplace(guid, nameplate); }); @@ -299,7 +292,7 @@ void OnWorldTick() { // Let the handler reach the frame it is being told about; live // state can no longer answer for it. Skipped once the engine has // rebound the frame to another unit. - if (!PlateReassigned(kv.second)) { + if (!PlateReassigned(kv.second, kv.first)) { g_removingGuid = kv.first; g_removingPlate = kv.second; } @@ -315,8 +308,7 @@ void OnWorldTick() { } } - // After the REMOVED pass, which needs every bound frame for - // `PlateReassigned`. + // Deferred ADDs leave the snapshot so the next tick sees them as new. for (uint64_t guid : g_deferredAdds) g_currentTickPlates.erase(guid); diff --git a/src/nameplate/Info.cpp b/src/nameplate/Info.cpp index b8abc309..a6f71f1a 100644 --- a/src/nameplate/Info.cpp +++ b/src/nameplate/Info.cpp @@ -28,32 +28,12 @@ // strings of the same set of units. Cheapest enumeration when an addon // only needs the GUIDs. // -// Vanilla 1.12 stores each unit's nameplate pointer at `CGUnit + 0xE60` -// (verified via `FUN_006086E0`'s "ensure nameplate exists" path). The -// nameplate caches the unit's GUID at `+0x4E8` for back-lookup and -// carries a Storm link node at `+0x4DC`/`+0x4E0` (prev link-address / -// next base-address). -// -// There IS a central list of live nameplates — header `{0x00C4D928, -// 0x00C4D92C}`, with a free/recycle list at `{0x00C4D91C, 0x00C4D920}` -// — but we deliberately don't enumerate through it: -// -// - `FUN_00608870` re-sorts that list by screen depth on EVERY plate's -// position update, unlinking and reinserting the node. It is in -// motion throughout the render, whereas no nameplate code touches -// the object hash table. -// - `FUN_006087F0` recycles a pooled node without clearing `+0x4E8`, -// so a node's cached GUID is stale until `FUN_007CB6D0` rebinds it. -// `unit + 0xE60` is the only authoritative live binding. -// - We need the unit and its instance block for the GUID regardless, -// and the hash walk yields all three together. -// -// So we enumerate by walking the local-player-anchored object hash -// table instead (`player + 0x1C` = bucket array, `player + 0x24` = -// mask). Each bucket header stores the link-field offset at byte 0 and -// the chain-head pointer at byte 8 — Storm's intrusive-hash pattern. -// Filter by `TYPEMASK_UNIT` (`flags & 0x08` at `*(entry+8) + 8`) and -// check `+0xE60` for a non-null nameplate pointer. +// Enumeration walks the engine's list of shown nameplates and resolves each +// plate's unit by the GUID it carries (`NamePlate::Walk`, see Offsets.h +// `VAR_NAMEPLATE_LIST_HEAD` for the engine functions behind it). An earlier +// version walked the whole object hash table every call instead, on the +// belief that the list was unsafe to read; it is re-sorted only by plain +// engine code on the main thread, and every plate on it is bound. #include "Game.h" #include "Offsets.h" @@ -69,7 +49,6 @@ namespace NamePlate::Info { namespace { using NamePlate::Walk::ForEachNamePlatedUnit; -using NamePlate::Walk::kOffUnitNamePlate; using TokenToGUID_t = uint64_t(__fastcall *)(const char *token); @@ -79,8 +58,7 @@ static int __fastcall Script_GetNamePlates(void *L) { Game::Lua::NewTable(L); int nextIndex = 1; ForEachNamePlatedUnit( - [L, &nextIndex](const uint8_t *, const uint8_t *nameplate, - const uint8_t *) { + [L, &nextIndex](const uint8_t *, const uint8_t *nameplate, uint64_t) { Game::Lua::PushNumber(L, static_cast(nextIndex++)); // Shared helper validates refkey freshness (defensive vs. // stale-across-reload) and falls back to a fresh wrapper @@ -96,11 +74,7 @@ static int __fastcall Script_GetNamePlateGUIDs(void *L) { Game::Lua::NewTable(L); int nextIndex = 1; ForEachNamePlatedUnit( - [L, &nextIndex](const uint8_t *, const uint8_t *, - const uint8_t *instance) { - const uint64_t guid = *reinterpret_cast(instance); - if (guid == 0) - return; + [L, &nextIndex](const uint8_t *, const uint8_t *, uint64_t guid) { char buf[Guid::STRING_SIZE]; Game::Lua::PushNumber(L, static_cast(nextIndex++)); Game::Lua::PushString(L, @@ -135,7 +109,7 @@ static void PushNamePlateForGUID(void *L, uint64_t guid) { auto *unit = static_cast( Object::ByGuid(Offsets::TYPEMASK_UNIT, guid, "NamePlate", 0x172)); if (unit != nullptr) - nameplate = *reinterpret_cast(unit + kOffUnitNamePlate); + nameplate = *reinterpret_cast(unit + Offsets::OFF_UNIT_NAMEPLATE); // Neither read can answer during `NAME_PLATE_UNIT_REMOVED`: the event // reports that the unit→plate binding is gone, and the unit itself is diff --git a/src/nameplate/Walk.h b/src/nameplate/Walk.h index e6fe869f..53323f34 100644 --- a/src/nameplate/Walk.h +++ b/src/nameplate/Walk.h @@ -13,12 +13,13 @@ #pragma once +#include "Offsets.h" +#include "object/Resolve.h" + #include -// Shared nameplate enumeration — walks the local-player-anchored -// object hash table for visible `TYPEMASK_UNIT` entries and invokes -// `emit(unit, nameplate, instance)` for each unit that currently has -// an allocated nameplate (`unit + 0xE60` non-null). +// Shared nameplate enumeration — walks the engine's list of shown +// nameplates and invokes `emit(unit, nameplate, guid)` for each. // // Header-only template — used by both the Lua-facing accessors in // `nameplate/Info.cpp` and the per-tick differ in `nameplate/Events.cpp`. @@ -74,66 +75,41 @@ void PrepareForReload(); namespace NamePlate::Walk { -// `0x00B41414` is a player-related global that overlays multiple -// sub-objects at different offsets. We read the object-hash-table -// fields at `+0x1C` (bucket array) and `+0x24` (bucket mask) — that -// sub-struct is safe to walk for any unit the engine knows about. -// **Do not feed this pointer to inventory routines** (`GetItemBySlot` -// etc.); the CGPlayer_C inventory manager lives at a different -// offset (`+0x1D38`) and is sourced via `ResolveUnitToken("player")`, -// not via this global. See CLAUDE.md "Resolving an item-location" -// for the inventory-crash story. -constexpr uintptr_t kLocalPlayerGlobal = 0x00B41414; -constexpr int kOffPlayerBucketArray = 0x1C; -constexpr int kOffPlayerBucketMask = 0x24; -constexpr int kBucketStride = 12; -constexpr int kBucketLinkOffsetField = 0; -constexpr int kBucketChainHeadField = 8; -constexpr int kOffEntryInstanceBlock = 0x08; -constexpr int kOffInstanceTypeMask = 0x08; -constexpr uint32_t kTypeMaskUnit = 0x08; -constexpr int kOffUnitNamePlate = 0xE60; - +// Calls `emit(unit, nameplate, guid)` for every shown nameplate, in the +// engine's own list order (farthest from the screen centre first). Returns +// the count. +// +// Walks the engine's shown-plate list (VAR_NAMEPLATE_LIST_HEAD) — a dozen +// or so entries — and resolves each plate's unit by the GUID the plate +// carries, exactly as the engine's NamePlateClearAll (FUN_0060F890) does. A +// plate counts only while that unit still points back at it +// (unit+0xE60 == plate), the binding the engine itself reads. The list is +// re-sorted as plates move, but only by plain engine code on the main +// thread, so it is consistent whenever a WorldTick handler or a Lua call +// reads it. template int ForEachNamePlatedUnit(F &&emit) { - auto *player = *reinterpret_cast(kLocalPlayerGlobal); - if (player == nullptr) - return 0; - - auto *buckets = *reinterpret_cast( - player + kOffPlayerBucketArray); - const uint32_t mask = *reinterpret_cast( - player + kOffPlayerBucketMask); - if (buckets == nullptr || mask == 0xFFFFFFFFu) - return 0; - + // A corrupt link must not spin forever; the engine shows a few dozen + // plates at most. + constexpr int kMaxPlates = 1024; + uintptr_t entry = *reinterpret_cast( + static_cast(Offsets::VAR_NAMEPLATE_LIST_HEAD)); int count = 0; - for (uint32_t b = 0; b <= mask; ++b) { - const uint8_t *bucket = buckets + b * kBucketStride; - const uint32_t linkOffset = *reinterpret_cast( - bucket + kBucketLinkOffsetField); - uintptr_t entry = *reinterpret_cast( - bucket + kBucketChainHeadField); - - while (entry != 0 && (entry & 1) == 0) { - auto *obj = reinterpret_cast(entry); - auto *instance = *reinterpret_cast( - obj + kOffEntryInstanceBlock); - if (instance != nullptr) { - const uint32_t typeMask = *reinterpret_cast( - instance + kOffInstanceTypeMask); - if ((typeMask & kTypeMaskUnit) != 0) { - const auto *nameplate = *reinterpret_cast( - obj + kOffUnitNamePlate); - if (nameplate != nullptr) { - emit(obj, nameplate, instance); - ++count; - } - } - } - entry = *reinterpret_cast( - obj + linkOffset + 4); - } + for (int guard = 0; entry != 0 && (entry & 1) == 0 && guard < kMaxPlates; ++guard) { + const auto *plate = reinterpret_cast(entry); + entry = *reinterpret_cast(plate + Offsets::OFF_NAMEPLATE_LINK_NEXT); + const uint64_t guid = + *reinterpret_cast(plate + Offsets::OFF_NAMEPLATE_GUID); + if (guid == 0) + continue; + const auto *unit = static_cast( + Object::ByGuid(Offsets::TYPEMASK_UNIT, guid, nullptr, 0)); + if (unit == nullptr || + *reinterpret_cast(unit + Offsets::OFF_UNIT_NAMEPLATE) != + plate) + continue; + emit(unit, plate, guid); + ++count; } return count; }