-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltip.cpp
More file actions
142 lines (125 loc) · 5.66 KB
/
Tooltip.cpp
File metadata and controls
142 lines (125 loc) · 5.66 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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/>.
#include "Game.h"
#include "Offsets.h"
#include "spell/Lookup.h"
#include "spell/Tooltip.h"
#include <cstdint>
namespace Spell::Tooltip {
// Spell.dbc record offsets used by GetSpell. Kept local rather than
// pulled from Spell::Info — that module keeps these as `static
// constexpr` privates and exposing them just for two readers here
// would force a header change.
static constexpr int OFF_SPELL_NAME = 0x1E0; // localized char *[9]
static constexpr int OFF_SPELL_RANK = 0x204; // localized char *[9]
using BuildSpellTooltip_t = void(__thiscall *)(void *thisObj, int spellID, int arg2, int arg3,
int isPet, int arg5, int arg6, int arg7);
void ShowByID(void *L, int spellID) {
if (spellID <= 0)
return;
void *tooltipObj = Game::Lua::ResolveObject(L, 1);
if (tooltipObj == nullptr)
return;
auto BuildSpellTooltip =
reinterpret_cast<BuildSpellTooltip_t>(Offsets::FUN_GAMETOOLTIP_BUILD_SPELL_TOOLTIP);
BuildSpellTooltip(tooltipObj, spellID, 0, 0, 0, 0, 0, 0);
}
static int __fastcall Script_GameTooltipSetSpellByID(void *L) {
if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) {
Game::Lua::Error(L, "Usage: GameTooltipSetSpellByID(self, spellID)");
return 0;
}
if (!Game::Lua::IsNumber(L, 2)) {
Game::Lua::Error(L, "Usage: GameTooltipSetSpellByID(self, spellID)");
return 0;
}
const int spellID = static_cast<int>(Game::Lua::ToNumber(L, 2));
ShowByID(L, spellID);
return 0;
}
// `GameTooltip:GetSpell()` → (name, rank, spellID) for whichever spell
// the tooltip is currently displaying, or nothing if it isn't showing
// a spell. BuildSpellTooltip writes the spellID to `tooltip+0x39C`;
// the per-tooltip Clear at FUN_00530050 zeroes the same slot on
// Hide/before-redraw, so a non-zero read means the spell tooltip is
// live. Name + rank come from the Spell.dbc record's locale-string
// arrays at +0x1E0 and +0x204 (locale index at VAR_LOCALE_INDEX).
//
// Aura-tooltip coverage (SetUnitBuff / SetUnitDebuff / SetPlayerBuff)
// was attempted via a MinHook on the inner aura builder at
// FUN_0052F880; that consistently crashed in pfUI-style
// hooksecurefunc paths, with the trampoline executing from corrupted
// JIT memory. Reverted pending a different design. For now,
// aura-path tooltips return nothing here.
static int __fastcall Script_GameTooltipGetSpell(void *L) {
if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) {
Game::Lua::Error(L, "Usage: GameTooltip:GetSpell()");
return 0;
}
void *tooltipObj = Game::Lua::ResolveObject(L, 1);
if (tooltipObj == nullptr)
return 0;
const int spellID = *reinterpret_cast<const int *>(
static_cast<const uint8_t *>(tooltipObj) + Offsets::OFF_TOOLTIP_SPELL_ID);
if (spellID <= 0)
return 0;
const uint8_t *record = Spell::Lookup::RecordForID(spellID);
if (record == nullptr)
return 0;
const int locale = *reinterpret_cast<const int *>(Offsets::VAR_LOCALE_INDEX);
const char *name = *reinterpret_cast<const char *const *>(
record + OFF_SPELL_NAME + locale * 4);
const char *rank = *reinterpret_cast<const char *const *>(
record + OFF_SPELL_RANK + locale * 4);
if (name == nullptr)
return 0;
Game::Lua::PushString(L, name);
// Spell rank is optional in Spell.dbc — some spells (most racials,
// some procs) have an empty rank slot. Surface that as an empty
// string to match modern semantics; an absent rank is not nil.
Game::Lua::PushString(L, rank != nullptr ? rank : "");
Game::Lua::PushNumber(L, static_cast<double>(spellID));
return 3;
}
// `GameTooltip:HasSpell()` — boolean companion to `GetSpell`. Returns
// true iff the tooltip is currently showing a cast spell tooltip.
// Aura tooltips (SetUnitBuff/SetUnitDebuff/SetPlayerBuff) leave
// `OFF_TOOLTIP_SPELL_ID` zero — see the note on `Script_GameTooltipGetSpell`.
static int __fastcall Script_GameTooltipHasSpell(void *L) {
if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE) {
Game::Lua::Error(L, "Usage: GameTooltip:HasSpell()");
return 0;
}
void *tooltipObj = Game::Lua::ResolveObject(L, 1);
if (tooltipObj == nullptr) {
Game::Lua::PushBool(L, 0);
return 1;
}
const int spellID = *reinterpret_cast<const int *>(
static_cast<const uint8_t *>(tooltipObj) + Offsets::OFF_TOOLTIP_SPELL_ID);
Game::Lua::PushBool(L, spellID > 0);
return 1;
}
static const Game::Lua::FrameMethodEntry g_methods[] = {
{"SetSpellByID", &Script_GameTooltipSetSpellByID},
{"GetSpell", &Script_GameTooltipGetSpell},
{"HasSpell", &Script_GameTooltipHasSpell},
};
static void RegisterLuaFunctions() {
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_GAMETOOLTIP_METHOD_REGISTRY),
g_methods,
static_cast<int>(sizeof(g_methods) / sizeof(g_methods[0])));
}
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace Spell::Tooltip