From 0ba73b84571f5bc83d86215dff2fc9631be208b2 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Fri, 11 Sep 2026 16:34:11 -0500 Subject: [PATCH] fix(macro): leave GetMacroInfo alone and expose the shown icon instead `Macro::Info` re-registered `GetMacroInfo` to answer with the icon a `?` macro's `#showtooltip` resolved to, so the Macro UI's list would match the action button. But the Macro UI seeds its icon selector from that same return -- `MacroFrame_Update` sets `MacroPopupFrame.selectedIconTexture` from it, and `MacroPopupFrame_Update` checks the grid button whose name matches -- so opening the selector on such a macro pre-selected an icon the player never picked, in the one place that edits the stored icon. Saving was never at risk (an unclicked selector leaves `selectedIcon` nil and `EditMacro` then leaves the icon alone), but the display was wrong. One return cannot serve both readers, and the one that edits state has the stronger claim, so `GetMacroInfo` goes back to the engine's: it is the read side of the icon `C_Macro.EditMacro` writes, and every addon that captures it now gets the stored icon. The icon a macro SHOWS becomes something a caller asks for, through `C_Macro.GetMacroIcon`. It reads the macro icon getter this DLL already hooks, so the `?` rule and the active-icon choice stay in one place and the answer matches what the action button and the drag cursor get. Util/MacroIconSelection.lua paints the Macro UI's list buttons from it after the stock update, which is additive -- an empty slot or a macro with a chosen icon answers with what stock already drew. Dropping the re-registration also takes a commonly captured global out of the override chain. --- AddOns/!!!ClassicAPI/!!!ClassicAPI.toc | 1 + .../!!!ClassicAPI/Util/MacroIconSelection.lua | 52 ++++++++++++ docs/API.md | 29 +++++-- src/Offsets.h | 9 ++- src/macro/IconPath.cpp | 52 +++++++++++- src/macro/Info.cpp | 81 ------------------- 6 files changed, 133 insertions(+), 91 deletions(-) create mode 100644 AddOns/!!!ClassicAPI/Util/MacroIconSelection.lua delete mode 100644 src/macro/Info.cpp diff --git a/AddOns/!!!ClassicAPI/!!!ClassicAPI.toc b/AddOns/!!!ClassicAPI/!!!ClassicAPI.toc index 82da94d3..a8f285c6 100644 --- a/AddOns/!!!ClassicAPI/!!!ClassicAPI.toc +++ b/AddOns/!!!ClassicAPI/!!!ClassicAPI.toc @@ -51,3 +51,4 @@ Util\SecureUnitMenu.lua Util\TextureUtil.lua Util\MacroOptions.lua Util\SecureStateDriver.lua +Util\MacroIconSelection.lua diff --git a/AddOns/!!!ClassicAPI/Util/MacroIconSelection.lua b/AddOns/!!!ClassicAPI/Util/MacroIconSelection.lua new file mode 100644 index 00000000..49687855 --- /dev/null +++ b/AddOns/!!!ClassicAPI/Util/MacroIconSelection.lua @@ -0,0 +1,52 @@ +-- MacroIconSelection.lua -- the Macro UI's macro list shows the icon a +-- `#showtooltip` resolved to, the way the action button does. +-- +-- `GetMacroInfo` reports the icon the macro STORES, and it has to keep +-- reporting that one: `MacroFrame_Update` seeds the icon selector from that +-- return, so answering with a resolved icon there would pre-select an icon the +-- player never picked, in the one place that edits the stored icon. +-- +-- That leaves the list buttons showing the question mark for a macro whose +-- action button shows a spell. `C_Macro.GetMacroIcon` returns the icon a macro +-- currently shows, so paint the buttons from it once the stock update has +-- drawn them. An empty slot and a macro with a chosen icon both answer with +-- what stock already drew, so those are left alone. + +local function ApplyIcon(texture, macroSlot) + if not texture then + return + end + local shown = C_Macro.GetMacroIcon(macroSlot) + if shown then + texture:SetTexture(shown) + end +end + +local function PaintMacroList() + if not (MacroFrame and MacroFrame.macroBase) then + return + end + for i = 1, MAX_MACROS do + ApplyIcon(_G["MacroButton" .. i .. "Icon"], MacroFrame.macroBase + i) + end + if MacroFrame.selectedMacro then + ApplyIcon(MacroFrameSelectedMacroButtonIcon, MacroFrame.selectedMacro) + end +end + +local function WrapMacroFrameUpdate() + if not MacroFrame_Update then + return + end + + local original = MacroFrame_Update + -- No `...`: `MacroFrame_Update` takes no arguments, and Lua 5.0 would + -- build an `arg` table for every vararg call. + MacroFrame_Update = function() + original() + PaintMacroList() + end +end + +-- Blizzard_MacroUI loads on demand. +EventUtil.ContinueOnAddOnLoaded("Blizzard_MacroUI", WrapMacroFrameUpdate) diff --git a/docs/API.md b/docs/API.md index 13dd2dba..e75c3b7b 100644 --- a/docs/API.md +++ b/docs/API.md @@ -462,6 +462,7 @@ build instructions. - [`GetMacroItem(macroSlot)`](#getmacroitemmacroslot) - [`GetMacroIcons` / `GetMacroItemIcons` / `GetLooseMacroIcons` / `GetLooseMacroItemIcons`](#getmacroicons--getmacroitemicons--getloosemacroicons--getloosemacroitemicons) - [`C_Macro.CreateMacro` / `C_Macro.EditMacro`](#c_macrocreatemacro--c_macroeditmacro) + - [`C_Macro.GetMacroIcon(macroSlot)`](#c_macrogetmacroiconmacroslot) - [`C_Macro.SetMacroDisplay(macroSlot, value)`](#c_macrosetmacrodisplaymacroslot-value) - [Mail](#mail) @@ -11089,8 +11090,9 @@ Put `#showtooltip` on the first line of a macro. The action button then shows the spell or item that the macro is about: its tooltip, cooldown, usable state, count, range, and auto-repeat highlight. If the macro icon is the question mark, the button, the macro window, and the cursor while you drag -the macro also show the icon of that spell or item. `#show` does the same, -but keeps the macro name as the tooltip. +the macro also show the icon of that spell or item. The icon selector still +shows the question mark, because that is the icon the macro keeps. `#show` +does the same, but keeps the macro name as the tooltip. ``` #showtooltip @@ -11377,6 +11379,23 @@ The legacy `CreateMacro` / `EditMacro` globals do not change. They stay index-only. String-icon callers use the `C_Macro` namespace instead. Edits persist across sessions, the same as edits in the Macro UI. +### `C_Macro.GetMacroIcon(macroSlot)` + +Returns the icon the macro currently shows, as `Interface\Icons\`. +Returns nothing when the slot is empty. + +```lua +C_Macro.GetMacroIcon(1) -- "Interface\\Icons\\Spell_Nature_Rejuvenation" +``` + +This is the icon on the action button: the spell's or item's when a +[`#showtooltip`](#showtooltip-and-show) resolved and the macro's own icon is +the question mark, and the macro's own icon otherwise. + +`GetMacroInfo` returns the icon the macro stores, which is the icon its icon +selector edits. Use this function when you want the icon a macro shows, and +`GetMacroInfo` when you want the one the player picked. + ### `C_Macro.SetMacroDisplay(macroSlot, value)` Tells the client what a macro is about, so the action button shows it. For @@ -11405,9 +11424,9 @@ range, usable, out-of-mana greying, the current-cast highlight and auto-repeat all follow, with no function replaced. The count, cooldown and consumable state for an item follow too. The tooltip follows only when the first line of the macro is `#showtooltip`. A macro with `#show`, or with no -directive, keeps its own name as the tooltip. The icon -appears in three places, two of which Lua cannot reach: the action button, -the cursor while you drag the macro, and the macro window grid. +directive, keeps its own name as the tooltip. The icon appears on the action +button, on the cursor while you drag the macro, and in the macro window. +[`C_Macro.GetMacroIcon`](#c_macrogetmacroiconmacroslot) reads it back. The icon replaces the macro's own only when that is the question mark, which is the same rule `#showtooltip` follows. A macro with a chosen icon keeps it. diff --git a/src/Offsets.h b/src/Offsets.h index c87feab5..b0ade71e 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -4516,9 +4516,12 @@ enum Offsets { FUN_MACRO_SLOT_TO_ENTRY = 0x004F0E40, // `Script_GetMacroInfo` — `int __fastcall(void *L)`; pushes `(name, // "Interface\Icons\", body, isLocal-or-nil)`, always 4 - // returns (nils for an empty slot). `Macro::Info` re-registers - // `GetMacroInfo` in front of it to swap the texture for `?` macros with a - // resolved `#showtooltip` — what the Macro UI grid and the popup read. + // returns (nils for an empty slot). Left alone deliberately: it is the read + // side of the icon `C_Macro.EditMacro` writes, and the Macro UI seeds its + // icon selector from that texture (`MacroFrame_Update` -> + // `MacroPopupFrame.selectedIconTexture`), so a resolved `#showtooltip` + // icon here would report a choice the player never made. The icon a macro + // SHOWS is `C_Macro.GetMacroIcon` (`Macro::IconPath`) instead. FUN_SCRIPT_GET_MACRO_INFO = 0x004F1760, // Macro create/edit workers — back `C_Macro.CreateMacro` / diff --git a/src/macro/IconPath.cpp b/src/macro/IconPath.cpp index 91ec0b9f..40cd5116 100644 --- a/src/macro/IconPath.cpp +++ b/src/macro/IconPath.cpp @@ -21,8 +21,17 @@ // the per-slot texture resolver (`FUN_ACTION_SLOT_TEXTURE`, behind // `GetActionTexture`) and the macro pickup `FUN_MACRO_PICKUP`, which paints // the cursor while a macro is dragged — the two call sites a scan of the -// binary finds. `GetMacroInfo` formats its own path and is handled by -// `Macro::Info`. +// binary finds. +// +// `GetMacroInfo` formats its own path and is left alone on purpose. It is the +// read side of the icon `C_Macro.EditMacro` writes, and the Macro UI seeds its +// icon selector from that return (`MacroFrame_Update` -> +// `MacroPopupFrame.selectedIconTexture`), so answering with a resolved icon +// there would tell the player they had picked an icon they never picked. Any +// caller that wants the icon a macro currently SHOWS asks for it instead, +// through `C_Macro.GetMacroIcon` below — which is this hook's own answer, so +// the rule stays in one place. `Util/MacroIconSelection.lua` uses it to paint +// the Macro UI's list buttons. // // The spell's active icon (toggle / stance up) is chosen through the engine's // own per-slot test `FUN_ACTION_SPELL_ICON_ACTIVE`, on the first action slot @@ -88,6 +97,45 @@ const Game::HookAutoRegister _hookreg{ reinterpret_cast(&MacroIconPath_h), reinterpret_cast(&MacroIconPath_o)}; +using MacroSlotToEntry_t = const uint8_t *(__fastcall *)(unsigned slot0Based); + +// `C_Macro.GetMacroIcon(macroSlot)` — the icon the macro currently shows: the +// spell's or item's when a `#showtooltip` / `#show` resolved and the macro's +// own icon is the question mark, else the macro's own. Nothing for an empty +// slot. +// +// Reads it through the getter this module hooks, so the answer is the same one +// the action button and the drag cursor get, active-icon choice included. +int __fastcall Script_GetMacroIcon(void *L) { + if (!Game::Lua::IsNumber(L, 1)) { + Game::Lua::Error(L, "Usage: C_Macro.GetMacroIcon(macroSlot)"); + return 0; + } + const int slot = static_cast(Game::Lua::ToNumber(L, 1)); + if (slot < 1 || slot > Offsets::MACRO_SLOT_MAP_COUNT) + return 0; + const uint8_t *entry = reinterpret_cast( + Offsets::FUN_MACRO_SLOT_TO_ENTRY)(static_cast(slot - 1)); + if (entry == nullptr) + return 0; + + char path[0x104]; // the engine's own texture buffer size + path[0] = '\0'; + // The hooked address, not the trampoline: the `?` rule lives in the hook. + reinterpret_cast(Offsets::FUN_MACRO_ICON_PATH)( + *reinterpret_cast(entry), path, sizeof(path)); + if (path[0] == '\0') + return 0; + Game::Lua::PushString(L, path); + return 1; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_Macro", "GetMacroIcon", &Script_GetMacroIcon); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + } // namespace } // namespace Macro::IconPath diff --git a/src/macro/Info.cpp b/src/macro/Info.cpp deleted file mode 100644 index 4453461b..00000000 --- a/src/macro/Info.cpp +++ /dev/null @@ -1,81 +0,0 @@ -// 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 . - -// `GetMacroInfo(index)` with the `#showtooltip` / `#show` icon rule: for a -// macro whose icon is the question mark and whose directive resolved to a -// spell or item, the `texture` return is that spell's / item's icon. This is -// what the Macro UI grid and its selected-macro button draw, so the macro -// window shows the same icon as the action bar. -// -// Registered in front of the engine's `GetMacroInfo` (last registration -// wins). The engine function runs unchanged and its four returns are kept; -// only the second value is swapped, in place on the Lua stack. Everything -// else — the stored `?` for an unresolved directive, empty slots, the usage -// error — is the engine's. -// -// The Macro UI's icon popup pre-checks the icon whose name equals this -// texture, but saving still needs an explicit icon click -// (`MacroPopupButton_SelectTexture` is the only writer of `selectedIcon`), so -// the swapped texture can't silently turn a `?` macro into a fixed-icon one. - -#include "Game.h" -#include "Offsets.h" -#include "macro/ShowTooltip.h" - -#include - -namespace Macro::Info { - -namespace { - -using ScriptFn_t = int(__fastcall *)(void *L); - -constexpr uint32_t kIconPathSize = 0x104; // the engine's own texture buffer size - -int __fastcall Script_GetMacroInfo(void *L) { - auto engine = reinterpret_cast(Offsets::FUN_SCRIPT_GET_MACRO_INFO); - if (!Game::Lua::IsNumber(L, 1)) - return engine(L); // raises the engine's own usage error - const int slot = static_cast(Game::Lua::ToNumber(L, 1)); - - const int base = Game::Lua::GetTop(L); - const int count = engine(L); // (name, texture, body, isLocal) - if (count < 2 || slot < 1 || slot > Offsets::MACRO_SLOT_MAP_COUNT) - return count; - - const uint32_t macroID = Game::Read( - static_cast(Offsets::VAR_MACRO_SLOT_MAP) + static_cast(slot - 1) * 4); - Macro::ShowTooltip::Info info; - char icon[kIconPathSize]; - if (macroID == 0 || !Macro::ShowTooltip::Lookup(macroID, &info) || - !Macro::ShowTooltip::HasQuestionMarkIcon(macroID) || - !Macro::ShowTooltip::ResolvedIconPath(info, /*activeIcon=*/false, icon, sizeof(icon))) - return count; - - // Replace the texture (second return, absolute index base + 2): push - // ours, move it into that slot, drop the engine's one slot below it. - Game::Lua::PushString(L, icon); - Game::Lua::Insert(L, base + 2); - Game::Lua::Remove(L, base + 3); - return count; -} - -void RegisterLuaFunctions() { - Game::Lua::RegisterGlobalFunction("GetMacroInfo", &Script_GetMacroInfo); -} - -const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; - -} // namespace - -} // namespace Macro::Info