From ff4a9ffd5a7e68d8bf9dcbc59d24cece296021fc Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:46:56 -0500 Subject: [PATCH] feat(macro): SetMacroSpell, SetMacroItem, macro range unit SetMacroSpell and SetMacroItem publish a spellbook spell or an item as a macro's display, like 4.3.4: a name that resolves to nothing greys the button, and the claim lasts until the macro's body is edited. Each macro now keeps the unit its spell is aimed at (4.3.4's MACRONODE::m_target): the target those functions name, or the [@unit] of the clause a #showtooltip or /cast line matched. Action::Range redirects IsActionInRange's call to the slot range function and, for a macro slot with a unit, passes that GUID to the engine's range check in place of the selection, as CGActionBar::HasValidTarget does. --- README.md | 2 +- docs/API.md | 36 +++++++++++++ src/Offsets.h | 12 +++++ src/action/Range.cpp | 110 ++++++++++++++++++++++++++++++++++++++ src/macro/Display.cpp | 77 ++++++++++++++++++++++++++ src/macro/ShowTooltip.cpp | 89 +++++++++++++++++++++++++++--- src/macro/ShowTooltip.h | 21 +++++++- 7 files changed, 339 insertions(+), 8 deletions(-) create mode 100644 src/action/Range.cpp diff --git a/README.md b/README.md index 3da96bcf..34c19125 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ In the Frame rows, a name that starts with `:` is a method of the widget that th | [LootHistory](docs/API.md#loothistory) | `C_LootHistory.GetNumItems`, `C_LootHistory.GetItem`, `C_LootHistory.GetPlayerInfo`, `C_LootHistory.Clear` | | [LossOfControl](docs/API.md#lossofcontrol) | `C_LossOfControl.GetActiveLossOfControlData`, `C_LossOfControl.GetActiveLossOfControlDataCount`, `C_LossOfControl.GetSchoolLockout` | | [Lua](docs/API.md#lua) | `collectgarbage` (5.1 options), `coroutine.create`, `coroutine.resume`, `coroutine.running`, `coroutine.status`, `coroutine.wrap`, `coroutine.yield`, `CreateFromMixins`, `math.fmod`, `math.huge`, `math.modf`, `Mixin`, `select`, `strcmputf8i`, `strconcat`, `string.concat`, `string.gmatch`, `string.gsub` (table replacement), `string.join`, `string.match`, `string.reverse`, `string.split`, `string.trim`, `strjoin`, `strlenutf8`, `strmatch`, `strreplace`, `strrev`, `strsplit`, `strsplittable`, `strtrim`, `table.count`, `table.maxn`, `table.wipe`, `unpack` (range args), `xpcall` (argument forwarding) | -| [Macros](docs/API.md#macros) | `C_Macro.CreateMacro`, `C_Macro.EditMacro`, `C_Macro.SetMacroDisplay`, `DeleteMacro` / `EditMacro` / `GetMacroInfo` / `PickupMacro` by name, `GetLooseMacroIcons`, `GetLooseMacroItemIcons`, `GetMacroIcons`, `GetMacroItem`, `GetMacroItemIcons`, `GetMacroSpell`, `GetRunningMacro`, `GetRunningMacroButton`, `RunMacroText`, `StopMacro` | +| [Macros](docs/API.md#macros) | `C_Macro.CreateMacro`, `C_Macro.EditMacro`, `C_Macro.SetMacroDisplay`, `DeleteMacro` / `EditMacro` / `GetMacroInfo` / `PickupMacro` by name, `GetLooseMacroIcons`, `GetLooseMacroItemIcons`, `GetMacroIcons`, `GetMacroItem`, `GetMacroItemIcons`, `GetMacroSpell`, `GetRunningMacro`, `GetRunningMacroButton`, `RunMacroText`, `SetMacroItem`, `SetMacroSpell`, `StopMacro` | | [Mail](docs/API.md#mail) | `GetInboxItemLink`, `GetSendMailItemLink` | | [Map](docs/API.md#map) | `C_Map.CanSetUserWaypointOnMap`, `C_Map.ClearUserWaypoint`, `C_Map.GetAreaInfo`, `C_Map.GetAreas`, `C_Map.GetAreaTriggerInfo`, `C_Map.GetAreaTriggers`, `C_Map.GetBestMapForUnit`, `C_Map.GetFallbackWorldMapID`, `C_Map.GetMapAreaIDs`, `C_Map.GetMapArtLayers`, `C_Map.GetMapArtLayerTextures`, `C_Map.GetMapChildrenInfo`, `C_Map.GetMapInfo`, `C_Map.GetMapInfoAtPosition`, `C_Map.GetMapOverlays`, `C_Map.GetMapPosFromWorldPos`, `C_Map.GetMapRectOnMap`, `C_Map.GetMapWorldSize`, `C_Map.GetPlayerMapPosition`, `C_Map.GetUserWaypoint`, `C_Map.GetUserWaypointFromHyperlink`, `C_Map.GetUserWaypointHyperlink`, `C_Map.GetUserWaypointPositionForMap`, `C_Map.GetWorldPosFromMapPos`, `C_Map.HasUserWaypoint`, `C_Map.MapHasArt`, `C_Map.SetUserWaypoint` | | [MapExplorationInfo](docs/API.md#mapexplorationinfo) | `C_MapExplorationInfo.GetExploredAreaIDsAtPosition`, `C_MapExplorationInfo.GetExploredMapTextures`, `C_MapExplorationInfo.GetUnexploredMapTextures` | diff --git a/docs/API.md b/docs/API.md index 59cda1e7..bd7163dd 100644 --- a/docs/API.md +++ b/docs/API.md @@ -617,6 +617,7 @@ build instructions. - [Macro names in `GetMacroInfo` / `EditMacro` / `PickupMacro` / `DeleteMacro`](#macro-names-in-getmacroinfo--editmacro--pickupmacro--deletemacro) - [`GetMacroSpell(macroSlot)`](#getmacrospellmacroslot) - [`GetMacroItem(macroSlot)`](#getmacroitemmacroslot) + - [`SetMacroSpell(macro, spell [, target])` / `SetMacroItem(macro, item [, target])`](#setmacrospellmacro-spell--target--setmacroitemmacro-item--target) - [`GetMacroIcons` / `GetMacroItemIcons` / `GetLooseMacroIcons` / `GetLooseMacroItemIcons`](#getmacroicons--getmacroitemicons--getloosemacroicons--getloosemacroitemicons) - [`C_Macro.CreateMacro` / `C_Macro.EditMacro`](#c_macrocreatemacro--c_macroeditmacro) - [`C_Macro.GetMacroIcon(macroSlot)`](#c_macrogetmacroiconmacroslot) @@ -14549,6 +14550,10 @@ does the same, but keeps the macro name as the tooltip. the cursor, and `[@mouseover][] Rejuvenation` shows the spell and casts on your target instead. If the value names a spell by name that you do not know, nothing shows. Name it by ID to show it anyway. +- The unit of the clause that matched (`[@focus]`, `[target=party1]`) is the + unit for the button's range check. `IsActionInRange` then tells whether that + unit is in range, not whether your target is. With no unit in the clause, the + range check uses your target. A macro without `#showtooltip` or `#show` works the same way as `#show` with no value. If its icon is the question mark, it shows the icon of its @@ -14776,6 +14781,37 @@ local name, link = GetMacroItem(4) -- name = "Major Healthstone", link = "|cffffffff|Hitem:9421:0:0:0|h[Major Healthstone]|h|r" ``` +### `SetMacroSpell(macro, spell [, target])` / `SetMacroItem(macro, item [, target])` + +Sets the spell or the item that a macro's action button shows. The button +then follows it, as for a [`#showtooltip`](#showtooltip-and-show) value: icon, +cooldown, count, usable state and range. + +```lua +SetMacroSpell("Heal", "Flash Heal", "focus") -- show Flash Heal, range to focus +SetMacroItem(3, "Hearthstone") -- show the Hearthstone +``` + +| Argument | Type | Description | +|---|---|---| +| `macro` | number or string | The macro's index, or its name. | +| `spell` | string | `SetMacroSpell` only. A spell in your spellbook, by name, with an optional `(Rank N)`. | +| `item` | string | `SetMacroItem` only. An item you carry, by name, or `item:N` or an item link. | +| `target` | string (optional) | A unit token or a name. The button's range check uses this unit in place of your target. | + +Both functions return `1` when they find the spell or the item. If they do +not find it, they return `nil`, and the button is greyed. If no macro has that +index or name, they return nothing. + +The setting stays until you edit the macro, or until the interface reloads. +Then the macro shows what its own lines resolve to again. The unit of +`target` is found when you call the function. If that unit goes away, the +button has no range state. + +If `spell` or `item` is not a string or a number, the function raises the +error `Usage: SetMacroSpell(macro, spell [,target])` or +`Usage: SetMacroItem(macro, item [,target])`. + ### `GetMacroIcons` / `GetMacroItemIcons` / `GetLooseMacroIcons` / `GetLooseMacroItemIcons` A 4-function append-to-table icon enumeration diff --git a/src/Offsets.h b/src/Offsets.h index 4fd003af..5981d9de 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -6533,6 +6533,18 @@ enum Offsets { // returned via the second arg. // __fastcall uint(uint slot0Based, uint *outPetFlag) FUN_ACTION_SLOT_TO_SPELL = 0x004E5A50, + // An action slot's range answer, `int __fastcall(int slot0, int + // *inRange)`: 0 for no answer (no player, a bad slot, a pet-book spell, + // no record, a spell whose first implicit target takes no unit, or no + // target selected), else 1 with `*inRange` 0 or 1. It calls + // FUN_CHECK_SPELL_TARGET_RANGE(player, record, GUID 0 = the selection, + // report 0) at 0x004E5765; a pass then answers 1 only for a first implicit + // target of 6, 21, 23, 25, 26, 35, 45, 57 or 61 (jump table 0x004E57C4, + // index bytes 0x004E57CC: case 0 for exactly those) with a target selected + // (0x004E579C). Sole caller Script_IsActionInRange, at the call below; + // `Action::Range` redirects it. + FUN_ACTION_SLOT_RANGE = 0x004E56F0, + CALL_IS_ACTION_IN_RANGE_SLOT_RANGE = 0x004E7583, ACTION_TYPE_MASK = 0xF0000000, ACTION_TYPE_SPELL = 0x00000000, // 0x40000000 entries are macros — live only while `entry & 0xBFFFFFFF` diff --git a/src/action/Range.cpp b/src/action/Range.cpp new file mode 100644 index 00000000..c0fbba84 --- /dev/null +++ b/src/action/Range.cpp @@ -0,0 +1,110 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under +// the terms of the GNU 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// The range answer of an action button whose macro is aimed at a unit. +// +// 4.3.4 keeps that unit on the macro (MACRONODE::m_target: the `[@unit]` of +// the clause its display resolved from, or the target SetMacroSpell / +// SetMacroItem named), and CGActionBar::HasValidTarget range-checks the slot's +// spell against it in place of the current target. 1.12's slot range function +// FUN_ACTION_SLOT_RANGE already takes the unit as a GUID for its range check, +// and always passes 0, the selection. `Macro::ShowTooltip` keeps the unit (its +// `Info::unit`), and this answers for those slots with that GUID, in the +// engine's order: the range check, then the implicit-target test, where the +// unit standing in for the selection must exist. Every other slot goes to the +// engine function unchanged. +// +// The function's one caller, Script_IsActionInRange, is redirected rather than +// the function hooked. + +#include "Common.h" +#include "Game.h" +#include "Offsets.h" +#include "action/Slot.h" +#include "macro/ShowTooltip.h" +#include "object/Resolve.h" +#include "spell/Lookup.h" +#include "unit/Identity.h" + +#include + +namespace Action::Range { + +namespace { + +using SlotRange_t = int(__fastcall *)(int slot0, int *inRange); +using SlotToSpell_t = int(__fastcall *)(int slot0, uint32_t *outPetFlag); +using CheckRange_t = uint8_t(__fastcall *)(const void *caster, const uint8_t *record, + uint32_t guidLo, uint32_t guidHi, uint32_t report); + +// The first implicit targets the engine answers for: case 0 of its table. +bool TakesUnitTarget(int32_t implicitTarget) { + switch (implicitTarget) { + case 6: case 21: case 23: case 25: case 26: case 35: case 45: case 57: case 61: + return true; + default: + return false; + } +} + +uint64_t MacroUnit(int slot0) { + const uint32_t macroID = Action::Slot::MacroIDForSlot(slot0); + Macro::ShowTooltip::Info info{}; + // Passive: this runs inside a Lua call, from a button's OnUpdate. + if (macroID == 0 || !Macro::ShowTooltip::LookupPassive(macroID, &info)) + return 0; + return info.unit; +} + +int __fastcall SlotRange_h(int slot0, int *inRange) { + const auto original = reinterpret_cast(Offsets::FUN_ACTION_SLOT_RANGE); + if (slot0 < 0 || slot0 >= Offsets::ACTION_TABLE_MAX_SLOTS) + return original(slot0, inRange); + const uint64_t unit = MacroUnit(slot0); + if (unit == 0) + return original(slot0, inRange); + + const uint8_t *player = Unit::Identity::PlayerObject(); + if (player == nullptr) + return 0; + uint32_t pet = 0; + const int spellID = + reinterpret_cast(Offsets::FUN_ACTION_SLOT_TO_SPELL)(slot0, &pet); + const uint8_t *record = ::Spell::Lookup::RecordForID(spellID); + if (record == nullptr || pet != 0) + return 0; + if (!reinterpret_cast(Offsets::FUN_CHECK_SPELL_TARGET_RANGE)( + player, record, static_cast(unit), static_cast(unit >> 32), 0)) { + *inRange = 0; + return 1; + } + const int32_t implicitTarget = + Game::Read(record, Offsets::OFF_SPELL_RECORD_EFFECT_IMPLICIT_TARGET_A); + if (!TakesUnitTarget(implicitTarget) || Object::ByGuid(Offsets::TYPEMASK_UNIT, unit) == nullptr) + return 0; + *inRange = 1; + return 1; +} + +void Register() { + Common::RedirectCall(Offsets::CALL_IS_ACTION_IN_RANGE_SLOT_RANGE, + Offsets::FUN_ACTION_SLOT_RANGE, + reinterpret_cast(&SlotRange_h)); +} + +const Game::ModuleAutoRegister _autoreg{&Register}; + +} // namespace + +} // namespace Action::Range diff --git a/src/macro/Display.cpp b/src/macro/Display.cpp index ca7c4fac..edf1be8f 100644 --- a/src/macro/Display.cpp +++ b/src/macro/Display.cpp @@ -26,10 +26,19 @@ // publisher hands over what it would have written in the macro and gets the // item-before-spell precedence, `item:N`, links, inventory slots and // `bag slot` for free. +// +// `SetMacroSpell(macro, spell [, target])` and `SetMacroItem(macro, item +// [, target])` are 4.3.4's narrower doors onto the same publish: a spellbook +// spell only, or an item only, aimed at `target` (the unit the button's range +// check uses, see `Action::Range`). As in 4.3.4 the claim lasts until the +// macro is edited, and a name that resolves to nothing greys the button. #include "Game.h" +#include "Offsets.h" #include "macro/ShowTooltip.h" +#include + namespace Macro::Display { namespace { @@ -57,7 +66,75 @@ int __fastcall Script_C_Macro_SetMacroDisplay(void *L) { return 1; } +// The macro slot (1-based) that argument 1 names, as 4.3.4's GetMacroFromLua +// reads it: a number, or a string that converts to one, is an index; any other +// string is a name. 0 when no macro is there. +int MacroSlotArg(void *L) { + using NameToSlot_t = uint32_t(__fastcall *)(const char *name); + using SlotToEntry_t = const uint8_t *(__fastcall *)(uint32_t slot0); + uint32_t slot0 = 0xFFFFFFFFu; + if (Game::Lua::IsNumber(L, 1)) + slot0 = static_cast(static_cast(Game::Lua::ToNumber(L, 1)) - 1); + else if (Game::Lua::IsString(L, 1)) + slot0 = reinterpret_cast(Offsets::FUN_MACRO_NAME_TO_SLOT)( + Game::Lua::ToString(L, 1)); + if (reinterpret_cast(Offsets::FUN_MACRO_SLOT_TO_ENTRY)(slot0) == nullptr) + return 0; + return static_cast(slot0) + 1; +} + +int SetMacroValue(void *L, Macro::ShowTooltip::Accept accept, const char *usage) { + if (!Game::Lua::IsString(L, 2)) { + Game::Lua::Error(L, usage); + return 0; + } + const int macroSlot = MacroSlotArg(L); + if (macroSlot == 0) + return 0; + Macro::ShowTooltip::PublishOptions options; + options.accept = accept; + options.unit = Game::Lua::IsString(L, 3) ? Game::Lua::ToString(L, 3) : nullptr; + options.untilEdit = true; + if (Macro::ShowTooltip::Publish(macroSlot, Game::Lua::ToString(L, 2), options)) + Game::Lua::PushNumber(L, 1); + else + Game::Lua::PushNil(L); + return 1; +} + +int __fastcall Script_SetMacroSpell(void *L) { + return SetMacroValue(L, Macro::ShowTooltip::Accept::Spell, + "Usage: SetMacroSpell(macro, spell [,target])"); +} + +int __fastcall Script_SetMacroItem(void *L) { + return SetMacroValue(L, Macro::ShowTooltip::Accept::Item, + "Usage: SetMacroItem(macro, item [,target])"); +} + +const Game::Doc::Field kSetMacroSpellArgs[] = { + Game::Doc::Req("macro", "string", "The macro's index or name."), + Game::Doc::Req("spell", "string", "The spell's name, with an optional \"(Rank N)\"."), + Game::Doc::Opt("target", "string", nullptr, "The unit that the button's range check uses."), +}; +const Game::Doc::Field kSetMacroItemArgs[] = { + Game::Doc::Req("macro", "string", "The macro's index or name."), + Game::Doc::Req("item", "string", "The item's name, item:N or link."), + Game::Doc::Opt("target", "string", nullptr, "The unit that the button's range check uses."), +}; +const Game::Doc::Field kSetMacroValueRets[] = { + Game::Doc::Opt("found", "number", nullptr, "1 when the name was found."), +}; +const Game::Doc::Function kSetMacroSpell{ + "Sets the spell that a macro's action button shows, until the macro is edited.", + kSetMacroSpellArgs, kSetMacroValueRets, "MacroGlobals"}; +const Game::Doc::Function kSetMacroItem{ + "Sets the item that a macro's action button shows, until the macro is edited.", + kSetMacroItemArgs, kSetMacroValueRets, "MacroGlobals"}; + void RegisterLuaFunctions() { + Game::Lua::RegisterGlobalFunction("SetMacroSpell", &Script_SetMacroSpell, &kSetMacroSpell); + Game::Lua::RegisterGlobalFunction("SetMacroItem", &Script_SetMacroItem, &kSetMacroItem); Game::Lua::RegisterTableFunction("C_Macro", "SetMacroDisplay", &Script_C_Macro_SetMacroDisplay); } diff --git a/src/macro/ShowTooltip.cpp b/src/macro/ShowTooltip.cpp index 36fc06cc..50435b29 100644 --- a/src/macro/ShowTooltip.cpp +++ b/src/macro/ShowTooltip.cpp @@ -81,6 +81,7 @@ #include "tick/WorldTick.h" #include "time/Clock.h" #include "unit/Identity.h" +#include "unit/TokenResolve.h" #include #include @@ -140,6 +141,10 @@ struct Entry { // macro: we neither parse the body nor re-evaluate it, and the lookups // answer from it even while yielding. See the header. bool external = false; + // A `SetMacroSpell` / `SetMacroItem` claim: it ends when the body changes, + // which `Rescan` tells by this hash of the body taken at the claim. + bool untilEdit = false; + uint32_t bodyHash = 0; // The last resolution had a value to resolve, even if it resolved to // nothing. The engine's cache tells those two apart — a value that named // something unresolvable greys the button (`0xFFFFFFFF`), nothing matched @@ -154,6 +159,7 @@ struct Entry { uint32_t spellID = 0; uint32_t isPet = 0; int itemID = 0; + uint64_t unit = 0; // Info::unit uint32_t lastEvalMs = 0; }; @@ -427,6 +433,15 @@ bool Drives(const Entry &e) { return e.kind != Kind::None && !e.foreign && (e.optionCount > 0 || e.none); } +// FNV-1a over a macro's body: enough to tell that an edit changed it. +uint32_t BodyHash(const uint8_t *macroEntry) { + uint32_t h = 2166136261u; + for (const char *p = reinterpret_cast(macroEntry + Offsets::OFF_MACRO_BODY); + *p != '\0'; ++p) + h = (h ^ static_cast(*p)) * 16777619u; + return h; +} + void Rescan(void *L) { if (!g_slashNamesLoaded) LoadSlashNames(L); @@ -445,7 +460,8 @@ void Rescan(void *L) { // its body. Ownership follows the macro, so it only drops when the // slot comes to hold a different one. if (e.external) { - if (e.macroID == macroID) + if (e.macroID == macroID && + !(e.untilEdit && BodyHash(entry) != e.bodyHash)) continue; e = Entry{}; } @@ -484,12 +500,17 @@ void Rescan(void *L) { // shared with `/cast`, so a `[@mouseover][]` clause falls through to its // `[]` group here exactly as it does for the cast. // +// `unit` gets the matched clause's `@unit` (the parser's second return), or +// an empty string when it named none. +// // `*outForeign` is set when the line uses a condition our parser does not // own. We ask quietly, because a macro body is not ours: another macro addon // has its own conditions, and naming one at the player would be noise about a // macro that works. -bool ResolveOptions(void *L, const char *options, char *out, size_t outSize, bool *outForeign) { +bool ResolveOptions(void *L, const char *options, char *out, size_t outSize, char *unit, + size_t unitSize, bool *outForeign) { out[0] = '\0'; + unit[0] = '\0'; *outForeign = false; if (std::strchr(options, '[') == nullptr && std::strchr(options, ';') == nullptr) { CopyTrimmed(options, out, outSize); @@ -513,6 +534,10 @@ bool ResolveOptions(void *L, const char *options, char *out, size_t outSize, boo CopyTrimmed(value, out, outSize); // copy before the SetTop below matched = true; } + if (Game::Lua::Type(L, -2) == Game::Lua::TYPE_STRING) { + if (const char *token = Game::Lua::ToString(L, -2)) + CopyTrimmed(token, unit, unitSize); + } } } Game::Lua::SetTop(L, top); @@ -553,8 +578,20 @@ struct Resolution { uint32_t spellID = 0; uint32_t isPet = 0; int itemID = 0; + uint64_t unit = 0; }; +// The GUID of the unit a clause or a publisher names: a unit token through the +// engine's resolver, else a unit with that exact name. 0 for none, and for a +// token that points at nothing, which leaves the current target in charge. +uint64_t UnitGuid(const char *unit) { + if (unit == nullptr || unit[0] == '\0') + return 0; + if (::Unit::TokenResolve::IsUnitToken(unit)) + return ::Unit::Identity::GuidForToken(unit); + return ::Unit::Identity::FindGuidByName(unit, Offsets::TYPEMASK_UNIT, true); +} + void SetItem(const uint8_t *cgItem, Resolution *r) { const int itemID = Item::ID::FromCGItem(cgItem); if (itemID > 0) { @@ -667,6 +704,28 @@ void ResolveValue(const char *value, Resolution *r, bool explicitValue, bool ite SetSpellByID(n, r); } +// A spell only, as 4.3.4's SetMacroSpell finds it: by name in the spellbook +// (the player's, then the pet's), with `spell:N` and a bare ID as the display +// forms `ResolveValue` gives a published value. +void ResolveSpell(const char *value, Resolution *r) { + if (const int spellRef = ParseSpellRef(value)) { + SetSpellByID(spellRef, r); + return; + } + int isPet = 0; + const int spellID = reinterpret_cast( + Offsets::FUN_RESOLVE_SPELL_NAME_TO_BOOK_ID)(value, &isPet); + if (spellID > 0) { + r->target = Target::Spell; + r->spellID = static_cast(spellID); + r->isPet = (isPet != 0) ? 1u : 0u; + return; + } + int n = 0; + if (ParseInt(value, &n)) + SetSpellByID(n, r); +} + // Repaint every action slot holding `macroID` through the engine's own // slot-changed notifier (recompute usable + ACTIONBAR_SLOT_CHANGED, no server // packet). Lua handlers run synchronously inside — keep the stack balanced. @@ -720,6 +779,9 @@ void ApplyResolution(void *L, Entry &e, const Resolution &r, bool matched) { e.spellID = r.spellID; e.isPet = r.isPet; e.itemID = r.itemID; + // The unit changes no button state the engine caches (the range answer is + // asked for fresh), so it never forces a repaint. + e.unit = r.unit; e.matched = matched; cache = cacheSpell; pet = cachePet; @@ -794,9 +856,11 @@ void Evaluate(void *L, Entry &e, uint32_t nowMs) { bool matched = false; int matchedLine = -1; char value[kOptionsMax] = {}; + char unit[kOptionsMax] = {}; for (int i = 0; i < e.optionCount; ++i) { bool foreign = false; - bool got = ResolveOptions(L, e.options[i], value, sizeof(value), &foreign); + bool got = ResolveOptions(L, e.options[i], value, sizeof(value), unit, sizeof(unit), + &foreign); if (got && value[0] != '\0') { switch (e.lineType[i]) { case LineType::Sequence: @@ -845,6 +909,7 @@ void Evaluate(void *L, Entry &e, uint32_t nowMs) { const LineType type = e.lineType[matchedLine]; ResolveValue(value, &r, type == LineType::Directive, type == LineType::Equip || type == LineType::EquipSlot); + r.unit = UnitGuid(unit); } ApplyResolution(L, e, r, matched); } @@ -868,6 +933,7 @@ void MaintainExternal(void *L) { r.spellID = e.spellID; r.isPet = e.isPet; r.itemID = e.itemID; + r.unit = e.unit; uint32_t wantSpell = 0, wantPet = 0; // `e.matched`, never `kind != None`: `Publish` marks a claimed macro // `ShowTooltip` even when the publisher had nothing for us, and @@ -1167,6 +1233,7 @@ bool LookupEntries(uint32_t macroID, Info *out) { out->spellID = e.spellID; out->isPet = e.isPet; out->itemID = e.itemID; + out->unit = e.unit; // A publisher re-publishes the moment its answer changes, so a claimed // macro never needs the polling refresh a condition of ours does. out->conditional = false; @@ -1180,6 +1247,7 @@ bool LookupEntries(uint32_t macroID, Info *out) { out->spellID = e.spellID; out->isPet = e.isPet; out->itemID = e.itemID; + out->unit = e.unit; out->conditional = e.conditional; return true; } @@ -1198,6 +1266,7 @@ bool LookupEntries(uint32_t macroID, Info *out) { out->spellID = spellID; out->isPet = Game::Read(entry, Offsets::OFF_MACRO_PRIMARY_SPELL_IS_PET); out->itemID = 0; + out->unit = 0; // A bare directive has no option lines, so nothing to re-evaluate. out->conditional = false; return true; @@ -1214,7 +1283,7 @@ const Game::HookAutoRegister _hookPruneSpell{ } // namespace -bool Publish(int macroSlot, const char *value) { +bool Publish(int macroSlot, const char *value, const PublishOptions &options) { if (macroSlot < 1 || macroSlot > kMaxMacros) return false; void *L = ReadyState(); @@ -1234,6 +1303,8 @@ bool Publish(int macroSlot, const char *value) { e.macroID = macroID; e.external = true; } + e.untilEdit = options.untilEdit; + e.bodyHash = options.untilEdit ? BodyHash(macroEntry) : 0; // Kind marks the macro as claimed even when nothing resolved, so the button // falls back to the question mark rather than to our own parse. WHICH kind // is the body's call, not the publisher's: `#show` is the macro's author @@ -1249,8 +1320,14 @@ bool Publish(int macroSlot, const char *value) { const bool matched = value != nullptr && value[0] != '\0'; // A publisher naming a value is naming it explicitly, the same as a // directive that carries one. - if (matched) - ResolveValue(value, &r, /*explicitValue=*/true, /*itemOnly=*/false); + if (matched) { + if (options.accept == Accept::Spell) + ResolveSpell(value, &r); + else + ResolveValue(value, &r, /*explicitValue=*/true, + /*itemOnly=*/options.accept == Accept::Item); + } + r.unit = UnitGuid(options.unit); BusyScope busy; ApplyResolution(L, e, r, matched); diff --git a/src/macro/ShowTooltip.h b/src/macro/ShowTooltip.h index 6ff3543e..2e69d9ce 100644 --- a/src/macro/ShowTooltip.h +++ b/src/macro/ShowTooltip.h @@ -71,6 +71,25 @@ struct Info { // to announce it — a modifier going down, a new mouseover. A tooltip built // from it has to keep re-reading while the cursor rests on the button. bool conditional; + // The unit the macro's spell is aimed at, 4.3.4's MACRONODE::m_target: the + // `[@unit]` of the clause that matched, or the target a `SetMacroSpell` / + // `SetMacroItem` named. 0 means the current target, as for any action. + uint64_t unit; +}; + +// What a published value may resolve to: every form a `#showtooltip` value +// takes (`C_Macro.SetMacroDisplay`), a spellbook spell only (`SetMacroSpell`), +// or an item only (`SetMacroItem`). +enum class Accept : uint8_t { Any, Spell, Item }; + +struct PublishOptions { + Accept accept = Accept::Any; + // A unit token or a name the spell is aimed at; null for the current target. + const char *unit = nullptr; + // The claim ends when the macro's body is edited, as 4.3.4's SetMacroSpell + // and SetMacroItem last only until the macro is parsed again. Otherwise it + // lasts until `Release` or a reload. + bool untilEdit = false; }; // Publish the resolution for macro slot `macroSlot` (1-based, as @@ -89,7 +108,7 @@ struct Info { // state. Whether it also replaces the TOOLTIP is the macro body's call: only // `#showtooltip` hands the tooltip over, while `#show` (icon only) and a body // with no directive at all keep the engine's macro-name tooltip. -bool Publish(int macroSlot, const char *value); +bool Publish(int macroSlot, const char *value, const PublishOptions &options = {}); // Give a published macro back, so our own `#showtooltip` parse resumes for // it. No-op for a macro that was never published.