From aef68d5ea465f3ec56b7fc8f9db2c97f8ed2e552 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sun, 1 Nov 2026 21:46:34 -0600 Subject: [PATCH] fix(nameplate): fire REMOVED and ADDED when a unit moves to another plate The name-query callback (0x5FB86E) and the creature-cache reply (0x6046C1) call CGUnit_C::ReleaseNamePlate (0x608A10) so the plate is rebuilt with the name, and the next tick takes a plate again through AllocNamePlate (0x6087F0). The free list is FIFO: release links the frame in at the tail and alloc takes s_freeNamePlates[2], the head. So unless the list was empty, the unit gets a different CGNamePlateFrame. The per-tick diff compared GUIDs only. The GUID was in both snapshots, so no event fired, and an addon that keys its state by frame kept the old, hidden frame. A binding is now the (GUID, frame) pair. A unit on a new frame gets NAME_PLATE_UNIT_REMOVED for the old binding, then NAME_PLATE_UNIT_ADDED for the new one. The REMOVED pass now runs before the ADDED pass, so the old binding's slot is free before the new one is assigned. A re-acquire that hands back the same frame still fires nothing. During REMOVED, GetNamePlateForUnit / GetNamePlateForGUID now return the frame being removed before they read unit+0xE60. After a rebind, that field already names the new frame, and the getters would have given the handler the wrong plate. --- src/nameplate/Events.cpp | 85 +++++++++++++++++++++++----------------- src/nameplate/Info.cpp | 24 ++++++------ src/nameplate/Walk.h | 9 +++-- 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/nameplate/Events.cpp b/src/nameplate/Events.cpp index 454f24cd..95eb57a8 100644 --- a/src/nameplate/Events.cpp +++ b/src/nameplate/Events.cpp @@ -17,9 +17,16 @@ // 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. +// hot per-unit `FUN_006086E0` un-hooked. +// +// A binding is the (GUID, frame) pair, not the GUID. The name-cache and +// creature-cache replies (`FUN_005FB840`, `FUN_00604660`) release a unit's +// plate and re-acquire it within one frame so it rebuilds with the name, and +// the free list is FIFO (`FUN_00608A10` appends, `FUN_006087F0` takes the +// head), so the unit usually comes back on a DIFFERENT frame. That is a new +// nameplate as far as an addon keyed by frame is concerned: REMOVED for the +// old binding, then ADDED for the new one. Only a re-acquire that hands back +// the same frame folds into no event. // // Payload shape (matches modern WoW exactly): // - `NAME_PLATE_CREATED` — `arg1` is the nameplate **Frame**, the @@ -92,9 +99,9 @@ std::vector g_deferredAdds; // subsequent shows. std::vector g_seenPlates; -bool HasGuid(const std::vector &plates, uint64_t guid) { +bool HasBinding(const std::vector &plates, const Plate &binding) { for (const Plate &p : plates) - if (p.guid == guid) + if (p.guid == binding.guid && p.plate == binding.plate) return true; return false; } @@ -200,9 +207,43 @@ void OnWorldTick() { g_currentTickPlates.push_back({guid, nameplate}); }); + // Fire REMOVED for bindings in last tick's snapshot but not current — + // the unit left, or moved to another frame. Runs before the ADDED pass so + // a moved unit's old binding is gone (and its slot free) before the new + // one is announced. The slot is *freed* (set 0), not erased, so surviving + // plates keep their index (retail-exact — no shift); the next ADD reuses + // it. Token is computed before freeing so the payload names the slot just + // vacated. Trailing empties are trimmed so the array tracks the + // high-water mark. + for (const Plate &last : g_lastTickPlates) { + if (!HasBinding(g_currentTickPlates, last)) { + auto it = std::find(g_slots.begin(), g_slots.end(), last.guid); + if (it == g_slots.end()) + continue; + const int oneBased = static_cast(it - g_slots.begin()) + 1; + // Let the handler reach the frame it is being told about; live + // state no longer names it (the unit has no plate, or already the + // new one). Skipped once the engine has rebound the frame to + // another unit. + if (!PlateReassigned(last.plate, last.guid)) { + g_removingGuid = last.guid; + g_removingPlate = last.plate; + } + char tokenBuf[24]; + FireWithString(_evtUnitRemoved, + FormatNamePlateToken(tokenBuf, sizeof tokenBuf, oneBased)); + g_removingGuid = 0; + g_removingPlate = nullptr; + *it = 0; // free the slot (no shift of survivors) + Unit::TokenEvents::Refresh(last.guid); + while (!g_slots.empty() && g_slots.back() == 0) + g_slots.pop_back(); + } + } + // Fire CREATED (with the Frame as arg1) for never-before-seen // frame pointers; ADDED (with `"nameplateN"` token as arg1) for - // GUIDs not in last tick's snapshot. The slot is assigned *before* + // bindings not in last tick's snapshot. The slot is assigned *before* // firing so the token resolves to the newly-added plate during the // event handler. // @@ -221,7 +262,7 @@ void OnWorldTick() { g_seenPlates.push_back(cur.plate); announced = true; } - if (!HasGuid(g_lastTickPlates, cur.guid)) { + if (!HasBinding(g_lastTickPlates, cur)) { if (!announced || _evtUnitAdded.Slot() < 0) { g_deferredAdds.push_back(cur.guid); continue; @@ -236,36 +277,6 @@ void OnWorldTick() { } } - // Fire REMOVED for GUIDs in last tick's snapshot but not current. The - // slot is *freed* (set 0), not erased, so surviving plates keep their - // index (retail-exact — no shift); the next ADD reuses it. Token is - // computed before freeing so the payload names the slot just vacated. - // Trailing empties are trimmed so the array tracks the high-water mark. - for (const Plate &last : g_lastTickPlates) { - if (!HasGuid(g_currentTickPlates, last.guid)) { - auto it = std::find(g_slots.begin(), g_slots.end(), last.guid); - if (it == g_slots.end()) - continue; - const int oneBased = static_cast(it - g_slots.begin()) + 1; - // 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(last.plate, last.guid)) { - g_removingGuid = last.guid; - g_removingPlate = last.plate; - } - char tokenBuf[24]; - FireWithString(_evtUnitRemoved, - FormatNamePlateToken(tokenBuf, sizeof tokenBuf, oneBased)); - g_removingGuid = 0; - g_removingPlate = nullptr; - *it = 0; // free the slot (no shift of survivors) - Unit::TokenEvents::Refresh(last.guid); - while (!g_slots.empty() && g_slots.back() == 0) - g_slots.pop_back(); - } - } - // Deferred ADDs leave the snapshot so the next tick sees them as new. if (!g_deferredAdds.empty()) g_currentTickPlates.erase( diff --git a/src/nameplate/Info.cpp b/src/nameplate/Info.cpp index a6f71f1a..3fe37aa5 100644 --- a/src/nameplate/Info.cpp +++ b/src/nameplate/Info.cpp @@ -105,19 +105,17 @@ static void PushNamePlateForGUID(void *L, uint64_t guid) { Game::Lua::PushNil(L); return; } - const void *nameplate = nullptr; - auto *unit = static_cast( - Object::ByGuid(Offsets::TYPEMASK_UNIT, guid, "NamePlate", 0x172)); - if (unit != nullptr) - 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 - // often already out of the object table. Fall back to the frame the - // removal is announcing so a handler can still reach it, as it can on - // retail. - if (nameplate == nullptr) - nameplate = NamePlate::Events::PlateBeingRemoved(guid); + // During `NAME_PLATE_UNIT_REMOVED` the answer is the frame being removed, + // as on retail. Live state can't give it: the unit→plate binding is gone + // (or already names the unit's new frame), and the unit itself is often + // out of the object table. + const void *nameplate = NamePlate::Events::PlateBeingRemoved(guid); + if (nameplate == nullptr) { + auto *unit = static_cast( + Object::ByGuid(Offsets::TYPEMASK_UNIT, guid, "NamePlate", 0x172)); + if (unit != nullptr) + nameplate = *reinterpret_cast(unit + Offsets::OFF_UNIT_NAMEPLATE); + } if (nameplate == nullptr) { Game::Lua::PushNil(L); diff --git a/src/nameplate/Walk.h b/src/nameplate/Walk.h index 53323f34..d27ff02b 100644 --- a/src/nameplate/Walk.h +++ b/src/nameplate/Walk.h @@ -54,12 +54,13 @@ int GetSlotCount(); // `NAME_PLATE_UNIT_REMOVED` handler is running; `nullptr` at any other time // or for any other GUID. // -// A plate is reported removed precisely because the unit→plate binding -// (`unit + 0xE60`) is already gone, so `GetNamePlateForUnit` / -// `GetNamePlateForGUID` cannot derive it from live state during the event. +// A plate is reported removed because the unit→plate binding +// (`unit + 0xE60`) is gone or already names the unit's new frame, so +// `GetNamePlateForUnit` / `GetNamePlateForGUID` cannot derive it from live +// state during the event. // Retail resolves it there — `NamePlateDriverMixin:OnNamePlateRemoved` calls // `C_NamePlate.GetNamePlateForUnit(unitToken, issecure())` — so the getters in -// `Info.cpp` consult this as their last fallback. Returns `nullptr` when the +// `Info.cpp` consult this before live state. Returns `nullptr` when the // engine has already recycled the frame onto another unit, since serving it // then would hand the handler somebody else's plate. const void *PlateBeingRemoved(uint64_t guid);