-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpell.cpp
More file actions
110 lines (93 loc) · 4.33 KB
/
Spell.cpp
File metadata and controls
110 lines (93 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
// `GetMacroSpell(macroSlot)` — modern (2.4+) reader for the spell a
// macro's first `/cast` (or `/castsequence`, or `CastSpellByName(...)`)
// directive resolves to. Returns `(name, rank, spellID)`, or nothing
// when the macro doesn't contain a cast.
//
// No parsing required — the vanilla engine already walks every macro
// body at create / edit / refresh time via the parser at
// `FUN_MACRO_PARSE_PRIMARY_SPELL` (`0x004EFE00`) and caches the
// resolved spellID on the macro struct at
// `+OFF_MACRO_PRIMARY_SPELL` (`+0x564`). Sentinel values:
// - `0` — macro contains no recognizable cast directive
// - `0xFFFFFFFF` — directive matched but the spell name didn't
// resolve to the player's spellbook (unknown spell)
// - any other — the resolved spellID
//
// We read that cache and translate to the modern 3-tuple, going
// through `Spell::Lookup::RecordForID` and the locale-indexed name /
// rank string arrays in `Spell.dbc` (same path `GameTooltip:GetSpell`
// uses to render its title block).
//
// `Spell::CastNoToggle` hooks the same parser to register our
// `CastSpellNoToggle("<name>")` Lua helper as a cast pattern, so
// macros built around that function show up here too — symmetry the
// engine's parser doesn't natively provide.
#include "Game.h"
#include "Offsets.h"
#include "spell/Lookup.h"
#include <cstdint>
namespace Macro::Spell {
namespace {
constexpr int OFF_SPELL_RANK = 0x204; // 9-locale char *[9] in Spell.dbc
using MacroSlotToEntry_t = const uint8_t *(__fastcall *)(unsigned slot0Based);
const uint8_t *EntryForSlot(int slot1Based) {
if (slot1Based < 1 || slot1Based > Offsets::MACRO_SLOT_MAP_COUNT)
return nullptr;
auto fn = reinterpret_cast<MacroSlotToEntry_t>(
Offsets::FUN_MACRO_SLOT_TO_ENTRY);
return fn(static_cast<unsigned>(slot1Based - 1));
}
// `GetMacroSpell(macroSlot)` — three returns: (name, rank, spellID).
// Returns nothing when the slot is empty, the macro has no cast
// directive, or the directive's name didn't resolve to a known spell.
int __fastcall Script_GetMacroSpell(void *L) {
if (!Game::Lua::IsNumber(L, 1)) {
Game::Lua::Error(L, "Usage: GetMacroSpell(macroSlot)");
return 0;
}
const int slot = static_cast<int>(Game::Lua::ToNumber(L, 1));
const uint8_t *entry = EntryForSlot(slot);
if (entry == nullptr)
return 0;
const uint32_t cached = *reinterpret_cast<const uint32_t *>(
entry + Offsets::OFF_MACRO_PRIMARY_SPELL);
if (cached == 0 || cached == 0xFFFFFFFFu)
return 0;
const int spellID = static_cast<int>(cached);
const uint8_t *record = ::Spell::Lookup::RecordForID(spellID);
if (record == nullptr)
return 0;
const int locale = *reinterpret_cast<const int *>(
static_cast<uintptr_t>(Offsets::VAR_LOCALE_INDEX));
const char *name = *reinterpret_cast<const char *const *>(
record + Offsets::OFF_SPELL_NAMES + locale * 4);
if (name == nullptr)
return 0;
const char *rank = *reinterpret_cast<const char *const *>(
record + OFF_SPELL_RANK + locale * 4);
Game::Lua::PushString(L, name);
// Rank slot is empty for most non-class spells (racials, procs,
// etc.). Surface that as "" so callers can `.. " (" .. rank .. ")"`
// without nil-handling — matches modern behavior.
Game::Lua::PushString(L, rank != nullptr ? rank : "");
Game::Lua::PushNumber(L, static_cast<double>(spellID));
return 3;
}
void RegisterLuaFunctions() {
Game::Lua::RegisterGlobalFunction("GetMacroSpell", &Script_GetMacroSpell);
}
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
} // namespace Macro::Spell