From d366fec43e26a4d3dd833d7ac2a6d24db87569f0 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Thu, 13 Aug 2026 00:03:42 -0500 Subject: [PATCH] fix(attributes): restore the previous target after a `macro` click-cast The `macro` verb selected the clicked unit and left it targeted (Clique's click-heal minus the restore). Snapshot the current selection GUID, retarget the clicked unit, run the macro, then restore the prior target via FUN_TARGET_BY_GUID (which validates the GUID and drops a stale one), or ClearTarget when there was no prior target. The macro's actions dispatch synchronously while the clicked unit is selected, so restoring afterward doesn't affect them. --- docs/API.md | 2 +- src/frame/Attributes.cpp | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/docs/API.md b/docs/API.md index 42e88bd8..4334a2e1 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4039,7 +4039,7 @@ modifier/button-qualified, same precedence as `type`): | `focus` | — | Sets the ClassicAPI focus to the `unit`. | | `spell` | `spell` | Casts the `spell` on the `unit` via [`C_Spell.CastAtUnit`](#c_spellcastatunitspellidorname-unit) — the unit's GUID goes straight to the cast dispatcher (no target juggling), and ground-target spells land at the unit's feet. | | `item` | `item`, or `bag`+`slot` | Uses an item on the `unit`. `item` may be a name / itemID / link (used via `C_Item.UseItemByName`, unit as the target) or a `"bag slot"` string like `"0 1"` (used via `UseContainerItem`). The deprecated `bag`+`slot` attributes are used when `item` is unset. | -| `macro` | `macrotext` or `macro` | Runs the macro text. Prefers an addon `RunMacro` (e.g. SuperCleveRoidMacros — named macros + extended conditionals); otherwise runs the text natively, line by line, through the stock `ChatEdit_ParseText`. | +| `macro` | `macrotext` or `macro` | Runs the macro text. To give the macro a unit, the handler selects the clicked `unit`, runs the macro, then restores the target from before the click. It prefers an addon `RunMacro` (for example, SuperCleveRoidMacros — named macros and extended conditionals). If no addon `RunMacro` exists, it runs the text natively, line by line, through the stock `ChatEdit_ParseText`. | | `stopcasting` | — | Stops the current cast. | | `menu` / `togglemenu` | — | Pops the standard unit dropdown at the cursor (whisper / inspect / trade / invite / …, the same menu `PlayerFrame` / `TargetFrame` / `PartyMemberFrame` show). | diff --git a/src/frame/Attributes.cpp b/src/frame/Attributes.cpp index c05ee716..4dc5dc5d 100644 --- a/src/frame/Attributes.cpp +++ b/src/frame/Attributes.cpp @@ -650,12 +650,25 @@ bool DispatchVerb(void *L, int fi, const char *prefix, const char *suffix, // conditional, and unlike the `spell` verb (which feeds a GUID straight // to the cast dispatcher) we can't inject a target into arbitrary macro // text — so a plain `/cast Flash Heal` would hit the current target, not - // the clicked unit. Emulate the classic Clique / pfUIevan click-heal: - // target the clicked unit first, then run the macro. Like pfUIevan, the - // previous target is NOT restored (the click doubles as a target swap). - if (unit) + // the clicked unit. Emulate the classic Clique click-heal: snapshot the + // current target, retarget the clicked unit, run the macro, then restore + // the previous target so the click doesn't leave the player retargeted. + // The macro's actions (/cast etc.) dispatch against the clicked unit + // synchronously while it's selected, so restoring the target afterward + // doesn't affect them. + uint64_t prevTarget = 0; + const bool swapped = unit != nullptr; + if (swapped) { + prevTarget = + (static_cast(*reinterpret_cast( + Offsets::VAR_CURRENT_SELECTION_GUID_HI)) + << 32) | + *reinterpret_cast( + Offsets::VAR_CURRENT_SELECTION_GUID_LO); Game::Lua::CallGlobalString(L, "TargetUnit", unit); + } char macro[512]; + bool handled = false; // `macrotext` is raw macro text (the modern default). Run it as text // through the stock chat parser so each line dispatches via // SlashCmdList — that routes /cast etc. through any addon slash hooks @@ -664,17 +677,28 @@ bool DispatchVerb(void *L, int fi, const char *prefix, const char *suffix, // would silently resolve nothing. if (ReadModAttr(L, fi, prefix, "macrotext", suffix, macro, sizeof macro)) { RunMacroTextC(L, macro); - return true; + handled = true; } // Deprecated `macro` form: a saved-macro name/index. Prefer an // addon-provided RunMacro (SuperCleveRoidMacros, pfUI, …) to run the // named macro's body; fall back to the stock parser when none exists. - if (ReadModAttr(L, fi, prefix, "macro", suffix, macro, sizeof macro)) { + else if (ReadModAttr(L, fi, prefix, "macro", suffix, macro, sizeof macro)) { if (!Game::Lua::CallGlobalString(L, "RunMacro", macro)) RunMacroTextC(L, macro); - return true; + handled = true; } - return false; + // Restore the target the click swapped away from. `FUN_TARGET_BY_GUID` + // validates the GUID resolves to a live unit and bails otherwise, so a + // target that despawned mid-macro is dropped cleanly rather than + // committed; a zero prior target means "no target" → ClearTarget. + if (swapped) { + if (prevTarget) + reinterpret_cast( + Offsets::FUN_TARGET_BY_GUID)(&prevTarget); + else + Game::Lua::CallGlobal(L, "ClearTarget"); + } + return handled; } if (Ascii::EqualCI(verb, "stop") || Ascii::EqualCI(verb, "stopcasting")) { Game::Lua::CallGlobal(L, "SpellStopCasting");