mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
Modified clicks: IsModifiedClick, GetModifiedClick, SetModifiedClick, GetNumModifiedClickActions and GetModifiedClickAction, with 4.3.4's <ModifiedClick> Bindings.xml node and its 18 defaults. Each binding set loads, saves and copies them through co-hooks on the engine's own binding-set steps, in 4.3.4's "modifiedclick" file lines. [mod:] asks IsModifiedClick, as 4.3.4's macro conditions do. Direct actions run as 4.3.4's ExecKey runs them: SPELL / ITEM / MACRO on key release through 1.12's spellbook, item and macro functions, cast at you for SELFCAST and at your focus for FOCUSCAST; CLICK sends the frame a mouse down and up, with 4.3.4's virtual-click button logic built from the button's own methods. RunBinding no longer runs them. While a key binding runs, its own modifiers count as released for every modifier read, the engine's IsKeyDown included. Overrides follow 4.3.4: two lists per key, checked inside the key lookup at both of 1.12's lookup steps; GetBindingAction takes checkOverride; isPriority is loose; "" removes; the owner must be a frame. ActionButtonUseKeyDown (default 0) and 4.3.4's action-button key handlers, ported onto 1.12's bars.
253 lines
10 KiB
C++
253 lines
10 KiB
C++
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
// The SPELL / ITEM / MACRO / CLICK binding commands — 4.3.4's
|
|
// CGUIBindings::ExecKey branches (decomp UIBindings.cpp), on 1.12's own
|
|
// primitives:
|
|
//
|
|
// SPELL x on release: the spellbook slot named x (FUN_RESOLVE_SPELL_NAME_TO_SLOT)
|
|
// cast through FUN_SPELL_CAST_DISPATCH at you when SELFCAST is held,
|
|
// at your focus when FOCUSCAST is, else with no explicit target.
|
|
// ITEM x on release: the item named x you carry (equipment, then bags)
|
|
// used through FUN_ITEM_USE with the same target.
|
|
// MACRO x on release: macro index x when x is all digits, else the macro
|
|
// named x, run through FUN_MACRO_RUN_BODY.
|
|
// CLICK f:b press and release: the frame named f gets a mouse down and a
|
|
// mouse up for button b.
|
|
//
|
|
// The release timing is 4.3.4's (and vanilla action buttons'): the command
|
|
// runs when the key comes back up.
|
|
//
|
|
// CLICK: 4.3.4's frames take a `btn` argument on OnLayerMouseDown / Up that
|
|
// marks a virtual click — no hit test, and a button clicks for ANY press
|
|
// (down) or release (up) bit it is registered for. 1.12's take only the mouse
|
|
// event and hit-test it, so the virtual branch is built from the button's own
|
|
// pieces, in 4.3.4's order: the frame's OnMouseDown / OnMouseUp script with
|
|
// the button name, then the Button state machine (click / double-click
|
|
// vmethods, SetButtonState). Only frames that are Buttons get the second part,
|
|
// as only CSimpleButton overrides the handlers 4.3.4 runs.
|
|
|
|
#include "bindings/Command.h"
|
|
|
|
#include "Game.h"
|
|
#include "Offsets.h"
|
|
#include "bindings/ModifiedClick.h"
|
|
#include "frame/ClickFrame.h"
|
|
#include "item/Arg.h"
|
|
#include "item/Location.h"
|
|
#include "time/Clock.h"
|
|
#include "unit/Focus.h"
|
|
#include "unit/Identity.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace Bindings::Command {
|
|
|
|
namespace {
|
|
|
|
constexpr const char kSpell[] = "SPELL ";
|
|
constexpr const char kItem[] = "ITEM ";
|
|
constexpr const char kMacro[] = "MACRO ";
|
|
constexpr const char kClick[] = "CLICK ";
|
|
|
|
bool HasPrefix(const char *command, const char *prefix) {
|
|
return _strnicmp(command, prefix, std::strlen(prefix)) == 0;
|
|
}
|
|
|
|
// The target 4.3.4 picks for SPELL / ITEM: you for SELFCAST, your focus for
|
|
// FOCUSCAST, else none (the engine then uses your target).
|
|
uint64_t ModifiedTarget() {
|
|
if (ModifiedClick::IsModifiedClick("SELFCAST"))
|
|
return Unit::Identity::PlayerGuid();
|
|
if (ModifiedClick::IsModifiedClick("FOCUSCAST"))
|
|
return Unit::Focus::Get();
|
|
return 0;
|
|
}
|
|
|
|
void CastSpell(const char *name) {
|
|
using NameToSlot_t = int(__fastcall *)(const char *name, int *outBookType);
|
|
using CastDispatch_t = void(__fastcall *)(unsigned slot, int bookType, unsigned guidLo,
|
|
unsigned guidHi);
|
|
int book = 0;
|
|
const int slot =
|
|
reinterpret_cast<NameToSlot_t>(Offsets::FUN_RESOLVE_SPELL_NAME_TO_SLOT)(name, &book);
|
|
if (slot < 0)
|
|
return;
|
|
const uint64_t target = ModifiedTarget();
|
|
reinterpret_cast<CastDispatch_t>(Offsets::FUN_SPELL_CAST_DISPATCH)(
|
|
static_cast<unsigned>(slot), book, static_cast<unsigned>(target),
|
|
static_cast<unsigned>(target >> 32));
|
|
}
|
|
|
|
void UseItem(const char *name) {
|
|
using UseItem_t = unsigned(__thiscall *)(const void *item, const uint64_t *targetGuid,
|
|
int flag);
|
|
const Item::Arg::Resolved byName{0, 0, name};
|
|
Item::Location::ByGUIDResult found{};
|
|
if (!Item::Location::FindByArgNoLua(byName, &found) || found.item == nullptr)
|
|
return;
|
|
const uint64_t target = ModifiedTarget();
|
|
reinterpret_cast<UseItem_t>(Offsets::FUN_ITEM_USE)(found.item, &target, 0);
|
|
}
|
|
|
|
void RunMacro(const char *name) {
|
|
using SlotToEntry_t = const uint8_t *(__fastcall *)(unsigned slot);
|
|
using NameToSlot_t = uint32_t(__fastcall *)(const char *name);
|
|
using RunBody_t = void(__fastcall *)(const uint8_t *entry);
|
|
const char *digit = name;
|
|
while (*digit >= '0' && *digit <= '9')
|
|
++digit;
|
|
const uint32_t slot =
|
|
(*name != '\0' && *digit == '\0')
|
|
? static_cast<uint32_t>(std::atoi(name) - 1)
|
|
: reinterpret_cast<NameToSlot_t>(Offsets::FUN_MACRO_NAME_TO_SLOT)(name);
|
|
const uint8_t *entry =
|
|
reinterpret_cast<SlotToEntry_t>(Offsets::FUN_MACRO_SLOT_TO_ENTRY)(slot);
|
|
if (entry != nullptr)
|
|
reinterpret_cast<RunBody_t>(Offsets::FUN_MACRO_RUN_BODY)(entry);
|
|
}
|
|
|
|
// StringToMouseButton: the click mask for a button name, 0 for any other
|
|
// (the click vmethod then names it "UNKNOWN").
|
|
uint32_t MaskForButton(const char *button) {
|
|
static const struct {
|
|
uintptr_t name;
|
|
uint32_t mask;
|
|
} kButtons[] = {
|
|
{Offsets::VAR_BUTTON_NAME_LEFT, 0x01}, {Offsets::VAR_BUTTON_NAME_RIGHT, 0x04},
|
|
{Offsets::VAR_BUTTON_NAME_MIDDLE, 0x02}, {Offsets::VAR_BUTTON_NAME_4, 0x08},
|
|
{Offsets::VAR_BUTTON_NAME_5, 0x10},
|
|
};
|
|
for (const auto &entry : kButtons)
|
|
if (_stricmp(reinterpret_cast<const char *>(entry.name), button) == 0)
|
|
return entry.mask;
|
|
return 0;
|
|
}
|
|
|
|
// The frame's OnMouseDown / OnMouseUp, fired as the engine fires them.
|
|
void FireMouseScript(void *frame, uintptr_t slotOffset, const char *button) {
|
|
using FireVariadic_t = void(__cdecl *)(void *frame, uint32_t *slotPtr, const char *fmt, ...);
|
|
uint32_t *slot = Game::Ptr<uint32_t>(frame, slotOffset);
|
|
if (slot[0] == 0)
|
|
return;
|
|
void *L = Game::Lua::State();
|
|
const int top = L != nullptr ? Game::Lua::GetTop(L) : 0;
|
|
reinterpret_cast<FireVariadic_t>(Offsets::FUN_FRAME_RUN_SCRIPT_VARIADIC)(
|
|
frame, slot, reinterpret_cast<const char *>(Offsets::VAR_SCRIPT_FMT_S), button);
|
|
if (L != nullptr)
|
|
Game::Lua::SetTop(L, top);
|
|
}
|
|
|
|
using ButtonMethod_t = void(__thiscall *)(void *button, uint32_t arg, int flag);
|
|
|
|
void CallButton(void *button, uintptr_t slot, uint32_t arg) {
|
|
auto *vtable = Game::Read<uintptr_t *>(button, 0);
|
|
reinterpret_cast<ButtonMethod_t>(vtable[slot / sizeof(uintptr_t)])(button, arg, 0);
|
|
}
|
|
|
|
int &ButtonState(void *button) { return Game::Ref<int>(button, Offsets::OFF_BUTTON_STATE); }
|
|
bool StateLocked(void *button) {
|
|
return Game::Read<int>(button, Offsets::OFF_BUTTON_STATE_LOCKED) != 0;
|
|
}
|
|
|
|
// The button's click under the engine's "in click" flag.
|
|
template <typename Fn> void InClick(Fn fn) {
|
|
auto &flag = Game::Ref<int>(Offsets::VAR_BUTTON_IN_CLICK);
|
|
flag = 1;
|
|
fn();
|
|
flag = 0;
|
|
}
|
|
|
|
// CSimpleButton::OnLayerMouseDown with a `btn`.
|
|
void VirtualMouseDown(void *frame, const char *button) {
|
|
FireMouseScript(frame, Offsets::OFF_FRAME_ONMOUSEDOWN_SLOT, button);
|
|
if (!Game::Lua::IsA(frame, Offsets::VAR_BUTTON_LUA_TYPE_ID) ||
|
|
ButtonState(frame) == Offsets::BUTTON_STATE_DISABLED)
|
|
return;
|
|
if (Game::Read<uint32_t>(frame, Offsets::OFF_BUTTON_CLICK_MASK) & 0xFF)
|
|
InClick([&] { CallButton(frame, Offsets::VTBL_BUTTON_CLICK, MaskForButton(button)); });
|
|
if (!StateLocked(frame) && ButtonState(frame) != Offsets::BUTTON_STATE_DISABLED)
|
|
CallButton(frame, Offsets::VTBL_BUTTON_SET_STATE, Offsets::BUTTON_STATE_PUSHED);
|
|
}
|
|
|
|
// CSimpleButton::OnLayerMouseUp with a `btn`.
|
|
void VirtualMouseUp(void *frame, const char *button) {
|
|
FireMouseScript(frame, Offsets::OFF_FRAME_ONMOUSEUP_SLOT, button);
|
|
if (!Game::Lua::IsA(frame, Offsets::VAR_BUTTON_LUA_TYPE_ID) ||
|
|
ButtonState(frame) != Offsets::BUTTON_STATE_PUSHED)
|
|
return;
|
|
if (Game::Read<uint32_t>(frame, Offsets::OFF_BUTTON_CLICK_MASK) & 0xFF00) {
|
|
InClick([&] {
|
|
const uint32_t now = Time::Clock::NowMs();
|
|
auto &last = Game::Ref<uint32_t>(frame, Offsets::OFF_BUTTON_LAST_CLICK_MS);
|
|
const uint32_t mask = MaskForButton(button);
|
|
if (Game::Read<uint32_t>(frame, Offsets::OFF_BUTTON_ONDOUBLECLICK_HANDLER) != 0 &&
|
|
last != 0 && Time::Clock::Elapsed(last, now) <= Offsets::BUTTON_DOUBLE_CLICK_MS) {
|
|
CallButton(frame, Offsets::VTBL_BUTTON_DOUBLE_CLICK, mask);
|
|
last = 0;
|
|
} else {
|
|
CallButton(frame, Offsets::VTBL_BUTTON_CLICK, mask);
|
|
last = now;
|
|
}
|
|
});
|
|
}
|
|
if (!StateLocked(frame) && ButtonState(frame) != Offsets::BUTTON_STATE_DISABLED)
|
|
CallButton(frame, Offsets::VTBL_BUTTON_SET_STATE, Offsets::BUTTON_STATE_NORMAL);
|
|
}
|
|
|
|
int Click(const char *target, int isDown) {
|
|
const char *separator = std::strchr(target, ':');
|
|
if (separator == nullptr)
|
|
return 0;
|
|
void *frame = Frame::ClickFrame::Find(Game::Lua::State(), target,
|
|
static_cast<size_t>(separator - target));
|
|
if (frame == nullptr || frame == Game::Read<void *>(Offsets::VAR_GAME_STATE_PTR))
|
|
return 0; // no such frame, or the world frame
|
|
const char *button = separator + 1;
|
|
if (isDown)
|
|
VirtualMouseDown(frame, button);
|
|
else
|
|
VirtualMouseUp(frame, button);
|
|
return 1;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool IsDirectAction(const char *command) {
|
|
return command != nullptr && (HasPrefix(command, kSpell) || HasPrefix(command, kItem) ||
|
|
HasPrefix(command, kMacro) || HasPrefix(command, kClick));
|
|
}
|
|
|
|
int RunKey(const char *command, int isDown) {
|
|
if (HasPrefix(command, kSpell)) {
|
|
if (!isDown)
|
|
CastSpell(command + sizeof kSpell - 1);
|
|
return 1;
|
|
}
|
|
if (HasPrefix(command, kItem)) {
|
|
if (!isDown)
|
|
UseItem(command + sizeof kItem - 1);
|
|
return 1;
|
|
}
|
|
if (HasPrefix(command, kMacro)) {
|
|
if (!isDown)
|
|
RunMacro(command + sizeof kMacro - 1);
|
|
return 1;
|
|
}
|
|
return Click(command + sizeof kClick - 1, isDown);
|
|
}
|
|
|
|
} // namespace Bindings::Command
|