mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
* frame: backport Frame:SetAttribute/GetAttribute + unit-frame mouseover/click Adds native SetAttribute / SetAttributeNoHandler / GetAttribute on the base Frame registry: a per-frame, case-insensitive value store with the 3.3.5 prefix/name/suffix getter precedence, stored on the frame's own Lua table. A `unit` attribute makes the frame a mouseover source. A WorldTick poll of the engine's mouse-focus frame (GetMouseFocus's global) calls the real mouseover setter (FUN_00492890) for its unit -- 1:1 with hovering the 3D model: model highlight, mouseover tooltip, UPDATE_MOUSEOVER_UNIT, and the native GUID slot. Stomp-proof (the engine's 3D-hover setter is event-driven) and independent of the frame's scripts, so an addon setting its own OnEnter can't break it. A `type1`/`type2`/`type` attribute installs a chained OnClick on that frame only (PCall-guarded, Button-only) that dispatches target / assist / focus on the unit at click time, then runs any prior handler. No global hooks: mouseover reads the mouse-focus global; clicks use the frame's own OnClick. Deliberately does NOT MinHook the button-click dispatcher (FUN_00779540) -- SuperWoW's click-casting owns that prologue and a second inline hook corrupts the trampoline (ERROR #132). * frame: extend attribute backport (one-verb click dispatcher + OnAttributeChanged) The `type*` click path now mirrors retail's SecureActionButton_OnClick: resolve ONE verb per click from the modifier/button-qualified `type` attribute and perform it, instead of a fixed `type1=target`. - Modifier/button resolution: `[alt-/ctrl-/shift-]type[1..5]` with prefix..name..suffix -> name..suffix -> name precedence, read from the live key state and the click's button. - Verbs: target (with the engine's default-interaction precedence - cast a pending spell / drop a cursor item on the unit instead of switching target), assist, focus, spell, stopcasting, macro, and menu. - Own-the-click: when a verb resolves we perform it and do NOT chain the frame's previous OnClick, so a configured `type1` no longer double- dispatches alongside an addon's own conditional click handler. - Self-healing wire: addons that re-SetScript OnClick (pfUI on every raid relayout) clobber our closure; WireOnClick re-wraps a clobbered Lua handler but skips its own C closure, so re-setting `type*` recovers without double-chaining. Dispatch goes straight to C++ where we own the code - no Lua round-trip through the global table: spell -> Spell::AtUnit::CastByName (a native unit-targeted cast; the unit's GUID is fed to the engine's dispatcher so there's no target juggling, and ground-target spells land at the unit's feet), focus -> Unit::Focus::Set, and the target verb's predicates -> Spell::AtCursor::IsPlacementActive / Cursor::Info::HasItem. Only genuine engine entries (TargetUnit/AssistUnit/SpellTargetUnit/DropItemOnUnit/ SpellStopCasting) and addon-provided functions still go through Lua globals. Spell::AtUnit gains a C++ header sharing its CastCore with the C_Spell.CastAtUnit Lua entry. macro verb: takes the content from the `macrotext`/`macro` attribute and prefers an addon-provided RunMacro (SuperCleveRoidMacros etc. - handles named macros and extended macro text), falling back to running the text natively when no RunMacro global is present. The native path runs each line through the stock FrameXML ChatEdit_ParseText via a throwaway edit-box (GetText closure + no-op __index), so it needs no addon. menu/togglemenu verb: pops the standard unit dropdown at the cursor via the new ClassicAPI_ToggleUnitMenu (Util/SecureUnitMenu.lua), resolving the unit -> menu type with Blizzard's own generic resolver (self->SELF, pet->PET, grouped player->PARTY, other player->PLAYER, NPC->RAID_TARGET_ICON). Keeps the FrameXML dropdown logic in Lua with the C side as a thin call. OnAttributeChanged is now a real SetScript/GetScript/HookScript-able frame script on every frame: SetAttribute fires it, SetAttributeNoHandler suppresses it. Made settable by co-hooking the base-frame script-name resolver FUN_0076a0d0 (the one every frame type delegates through) and handing back an external per-frame {handler, context} cell for that name - frames are immortal in 1.12, so the cell never goes stale. One hook does it (vs the tooltip OnTooltipSet* work's five) because we already own the firing side. 1.12 convention: the handler reads this = frame, arg1 = name, arg2 = value. Recursion-guarded so a handler may itself SetAttribute. * frame: add Frame:ClearAttribute (retail 11.2.0) `cleared = frame:ClearAttribute(name)` removes an attribute and returns whether it was set. Registered on the base Frame method registry next to SetAttribute/GetAttribute, so every frame has it. Routes through DoSet's nil-value path (removes the stored key, drops a `unit` mouseover binding) but does NOT fire OnAttributeChanged — verified against a live retail 11.2 client: SetAttribute fires the handler, ClearAttribute fires nothing, and the return is true for a set attribute / false when there was nothing to clear. * frame: store attributes in the Lua registry, hidden from Lua Attributes were kept as a subtable on the frame's own Lua table under a control-char key ("\1ClassicAPIAttributes"). That key, while collision- safe, is still enumerable via pairs(frame) and readable/writable from Lua. Move the store to registry[frame] in the Lua registry (LUA_REGISTRYINDEX), which has no _G path — so the attribute map is invisible to pairs(frame) and can't be read or tampered with from script, matching how retail keeps attributes C-side. Only PushSub changed (create/fetch now key the registry by the frame table); GetAttribute/DoSet/CopyAttr sit on top unchanged. 1.12 frames are immortal, so the strong registry key never leaks. The dead kAttrKey constant is removed. Verified in-game: SetAttribute/GetAttribute still round-trip, and a pairs(frame) scan no longer finds any ClassicAPI key. * frame: add the `item` click verb (SecureActionButton item action) type*="item" uses the modifier-qualified `item` attribute: - a "bag slot" string (e.g. "0 1") -> UseContainerItem(bag, slot); - otherwise an item name / itemID / link -> C_Item.UseItemByName(item, unit), passing the unit as the use target (self-use items ignore it, targeted ones like scrolls/bandages apply to it); - the deprecated `bag` + `slot` attributes are honored when `item` is unset. Both bag paths go through lua_call (new CallTableFunc2Str / CallGlobalNum2) rather than a direct C++ call: the item-use bag walk does SetTop(L, 0), so it needs its own Lua stack frame — a direct call would wipe the dispatcher's stack. (spell stays direct because Spell::AtUnit is stack-free; the helper comment records the distinction.) Retail's "bag slot" string is the preferred form; the deprecated per-attribute form is supported too. * docs: format the attribute click verbs as a table The verbs were one long paragraph of nested parentheticals - hard to scan. Replace it with a Verb | Extra attributes | Action table so each verb's inputs and behavior are legible at a glance. Content-equivalent; no API change. * frame: clear target on the `target` verb when unit is "none" Retail's SecureActionButton target action treats a "none" unit as "clear target". Match it: the `target` verb calls ClearTarget() when the resolved `unit` is "none" (case-insensitive), before the normal cursor/spell precedence. Mouseover already handles "none" implicitly (it resolves to GUID 0 = no unit). Docs' target-verb row updated. * frame: pass modern positional args to script handlers Vanilla always invokes frame-script handlers with zero Lua args; the handler reads this/arg1/event as globals. Modern addon ports write SetScript("OnMouseWheel", function(self, delta) ...) and get nothing. Co-hook both script runners -- FUN_FRAME_INVOKE_SCRIPT (no-arg scripts) and FUN_FRAME_RUN_SCRIPT_ARGS (scripts with values) -- and reimplement their pcall tail: keep the engine's exact this/argN global save-set- restore and message-handler errfunc, but additionally push self + arg1..argN as real positional arguments and pcall with nargs = 1+N. Purely additive: a zero-param vanilla handler ignores the extras and reads globals; a modern (self, delta) handler binds them. OnEvent gets (self, event, arg1..argN), detected structurally by matching the invoked handler ref against the frame OnEvent slot (frame+0x0C) -- exact, since each SetScript makes a distinct ref. Runtime switch SetModernScriptArgs()/GetModernScriptArgs(), default on; when off both detours are a straight passthrough to the original runner, which matters because FUN_FRAME_RUN_SCRIPT_ARGS is the hottest Lua path in the engine (every OnUpdate frame). Verified in-game across OnMouseWheel/OnClick/OnValueChanged/OnUpdate/OnEnter/OnLeave/OnEvent. * net: consolidate spell SMSG hooks onto one dispatch funnel The spell subsystem's per-opcode SMSG handlers (SpellGo, SpellStart, SpellDelayed, ChannelStart/Update, SpellFailure, SpellFailedOther, SpellCooldown) were each co-hooked directly -- and nampower (hadesmem) plus SuperWoW detour the same prologues. Two inline hookers on one prologue is the documented MinHook collision risk, and a whole-protocol bisect fingered exactly this cluster as the crash-suspect surface. Add Net::PacketDispatch: a receive-side mirror of Net::SendObserver that co-hooks the single incoming-SMSG dispatch funnel FUN_NET_MESSAGE_DISPATCH (0x00537AA0) -- a chokepoint nampower does NOT hook (it detours the leaf handlers) -- and fans each packet out to opcode subscribers with the cursor at the body and centralized save/restore. Migrate all eight handlers to subscribers: aura/Source (SpellGo, which re-decodes the packed guids + spellId the leaf handler used to hand it pre-decoded), spell/Cast (Start/Delayed/Channel*/Failure*), and lossofcontrol (Cooldown). The parse bodies are unchanged; only the hook site moved. Subscribers run before the engine's leaf handler instead of after a co-hook's original -- verified order-independent for all eight (self-contained cache/state updates; SpellGo's aura-application handoff rides a separate SMSG_UPDATE_OBJECT). Cuts the ClassicAPI-nampower shared spell prologues from 10 to 3 (the OnAura* method hooks, which have no packet chokepoint to relocate to). One hook on an uncontended function replaces eight on contended ones. Verified in-game: cast bars, channels (incl. Mind Control cast-then- channel), pushback, remote-cast interrupt eviction, and C_LossOfControl school lockouts all intact. * ScriptArgs is back to opt-in * frame: fix ScriptArgs comments to match opt-in default * frame/net: dedupe packet observers, reuse Lua/token helpers, fix attr edge cases Post-review cleanup of the attribute + SMSG-consolidation work. - net: extract the shared packet-observer plumbing (PacketObserver.h) — the intrusive subscriber list, Subscribe(), and the read-opcode / reset-cursor / fan-out / restore loop — so Net::PacketDispatch (SMSG, u16) and Net::SendObserver (CMSG, u32) each reduce to a one-line hook thunk over FanOutByOpcode<OpcodeT>. Drops the dead per-namespace Callback aliases rather than keeping them as compat shims. - Game::Lua: promote PushGlobalFunction / CallGlobal / CallGlobalString into the shared Lua helpers (a pattern several modules open-code); Frame attributes use them instead of private copies. - frame/Attributes: resolve unit tokens through Unit::Identity::GuidForToken (engine resolver + player fast-path) instead of a duplicate raw-cast wrapper. - frame/Attributes: wire the chained OnClick for every "type" action attribute the click resolver reads (IsTypeAttr: modifier prefixes + type1..type5), not just type/type1/type2 — a middle-click or modifier-only binding now installs. - frame/Attributes: FireAttributeChanged snapshots and restores the caller's arg1/arg2 around the fire, so calling SetAttribute from inside a running script handler no longer clobbers its globals.
330 lines
12 KiB
C++
330 lines
12 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/>.
|
|
|
|
#include "Game.h"
|
|
#include "MinHook.h"
|
|
#include "Offsets.h"
|
|
|
|
namespace Game {
|
|
|
|
namespace Lua {
|
|
// Each entry binds a typed function pointer in `Game::Lua::` to the
|
|
// corresponding raw VA in `Offsets`. The X-macro keeps the column-aligned
|
|
// list visually scannable and removes 27 lines of identical cast boilerplate.
|
|
#define CLASSICAPI_LUA_BINDINGS(F) \
|
|
F(IsNumber, lua_isnumber, LUA_IS_NUMBER) \
|
|
F(IsString, lua_isstring, LUA_IS_STRING) \
|
|
F(ToNumber, lua_tonumber, LUA_TO_NUMBER) \
|
|
F(ToBoolean, lua_toboolean, LUA_TO_BOOLEAN) \
|
|
F(ToString, lua_tostring, LUA_TO_STRING) \
|
|
F(StrLen, lua_strlen, LUA_STR_LEN) \
|
|
F(PushNumber, lua_pushnumber, LUA_PUSH_NUMBER) \
|
|
F(PushNil, lua_pushnil, LUA_PUSH_NIL) \
|
|
F(PushBoolean, lua_pushboolean, LUA_PUSH_BOOLEAN) \
|
|
F(PushString, lua_pushstring, LUA_PUSH_STRING) \
|
|
F(PushLString, lua_pushlstring, LUA_PUSH_LSTRING) \
|
|
F(PushValue, lua_pushvalue, LUA_PUSH_VALUE) \
|
|
F(PushCClosure,lua_pushcclosure,LUA_PUSH_CCLOSURE)\
|
|
F(NewTable, lua_newtable, LUA_NEW_TABLE) \
|
|
F(GetTable, lua_gettable, LUA_GET_TABLE) \
|
|
F(RawGet, lua_rawget, LUA_RAW_GET) \
|
|
F(SetTable, lua_settable, LUA_SET_TABLE) \
|
|
F(RawSet, lua_rawset, LUA_RAW_SET) \
|
|
F(Insert, lua_insert, LUA_INSERT) \
|
|
F(Remove, lua_remove, LUA_REMOVE) \
|
|
F(GetTop, lua_gettop, LUA_GET_TOP) \
|
|
F(SetTop, lua_settop, LUA_SET_TOP) \
|
|
F(Call, lua_call, LUA_CALL) \
|
|
F(PCall, lua_pcall, LUA_PCALL) \
|
|
F(Next, lua_next, LUA_NEXT) \
|
|
F(Type, lua_type, LUA_TYPE) \
|
|
F(Error, lua_error, LUA_ERROR) \
|
|
F(ToPointer, lua_topointer, LUA_TO_POINTER) \
|
|
F(ToThread, lua_tothread, LUA_TO_THREAD) \
|
|
F(IsCFunction, lua_iscfunction, LUA_IS_C_FUNCTION)\
|
|
F(XMove, lua_xmove, LUA_X_MOVE) \
|
|
F(NewThread, lua_newthread, LUA_NEW_THREAD) \
|
|
F(Resume, lua_resume, LUA_RESUME) \
|
|
F(Yield, lua_yield, LUA_YIELD) \
|
|
F(ArgError, luaL_argerror, LUAL_ARG_ERROR) \
|
|
F(SetN, luaL_setn, LUAL_SETN) \
|
|
F(CheckStack, lua_checkstack, LUA_CHECK_STACK)
|
|
|
|
#define CLASSICAPI_BIND_LUA(Name, Typedef, Offset) \
|
|
const Typedef##_t Name = reinterpret_cast<Typedef##_t>(Offsets::Offset);
|
|
CLASSICAPI_LUA_BINDINGS(CLASSICAPI_BIND_LUA)
|
|
#undef CLASSICAPI_BIND_LUA
|
|
#undef CLASSICAPI_LUA_BINDINGS
|
|
|
|
namespace {
|
|
using FrameScript_RegisterFunction_t = void(__fastcall *)(const char *name, CFunction func);
|
|
using RegisterFrameMethods_t = void(__fastcall *)(const FrameMethodEntry *table, int count,
|
|
void *context);
|
|
} // namespace
|
|
|
|
void *State() {
|
|
return *reinterpret_cast<void **>(static_cast<uintptr_t>(Offsets::VAR_LUA_STATE));
|
|
}
|
|
|
|
namespace {
|
|
using FrameScriptPushObject_t = void(__fastcall *)(void *L, int idx, int unused);
|
|
using FrameScriptGetObject_t = void *(__fastcall *)(void *L, int idx);
|
|
} // namespace
|
|
|
|
void *ResolveObject(void *L, int idx) {
|
|
auto PushObject = reinterpret_cast<FrameScriptPushObject_t>(
|
|
Offsets::FUN_FRAMESCRIPT_PUSH_OBJECT);
|
|
auto GetObject = reinterpret_cast<FrameScriptGetObject_t>(
|
|
Offsets::FUN_FRAMESCRIPT_GET_OBJECT);
|
|
PushObject(L, idx, 0);
|
|
void *result = GetObject(L, -1);
|
|
SetTop(L, -2);
|
|
return result;
|
|
}
|
|
|
|
void RegisterGlobalFunction(const char *name, CFunction func) {
|
|
auto fn = reinterpret_cast<FrameScript_RegisterFunction_t>(
|
|
Offsets::FUN_FRAMESCRIPT_REGISTER_FUNCTION);
|
|
fn(name, func);
|
|
}
|
|
|
|
// Aliased to the same engine entry; the engine reads VAR_LUA_STATE
|
|
// internally to decide which state to write to, and the glue hook
|
|
// runs while that pointer is set to the glue state. Wrapped as a
|
|
// named function (rather than `using RegisterGlueFunction = ...`) so
|
|
// callers express intent at the call site.
|
|
void RegisterGlueFunction(const char *name, CFunction func) {
|
|
auto fn = reinterpret_cast<FrameScript_RegisterFunction_t>(
|
|
Offsets::FUN_FRAMESCRIPT_REGISTER_FUNCTION);
|
|
fn(name, func);
|
|
}
|
|
|
|
void RegisterFrameMethods(void *context, const FrameMethodEntry *table, int count) {
|
|
auto fn = reinterpret_cast<RegisterFrameMethods_t>(Offsets::FUN_REGISTER_FRAME_METHODS);
|
|
fn(table, count, context);
|
|
}
|
|
|
|
// Looks up `_G[name]`. If absent, creates a fresh table and binds it.
|
|
// Leaves the resulting table on top of the stack.
|
|
namespace {
|
|
void EnsureGlobalTable(void *L, const char *name) {
|
|
PushString(L, name);
|
|
GetTable(L, GLOBALS_INDEX);
|
|
if (Type(L, -1) == TYPE_TABLE)
|
|
return;
|
|
SetTop(L, -2); // pop the non-table. []
|
|
NewTable(L); // [tbl]
|
|
PushValue(L, -1); // [tbl, tbl]
|
|
PushString(L, name); // [tbl, tbl, name]
|
|
Insert(L, -2); // [tbl, name, tbl]
|
|
SetTable(L, GLOBALS_INDEX); // _G[name] = tbl; pops k+v. [tbl]
|
|
}
|
|
} // namespace
|
|
|
|
// Registers `func` at `_G[tableName][methodName]`. If the namespace
|
|
// doesn't already exist, creates an empty table for it.
|
|
void RegisterTableFunction(const char *tableName, const char *methodName, CFunction func) {
|
|
void *L = State();
|
|
if (L == nullptr)
|
|
return;
|
|
EnsureGlobalTable(L, tableName); // [tbl]
|
|
PushString(L, methodName); // [tbl, methodName]
|
|
PushCClosure(L, func, 0); // [tbl, methodName, closure]
|
|
SetTable(L, -3); // tbl[m]=c; pops k+v. [tbl]
|
|
SetTop(L, -2); // pop tbl. []
|
|
}
|
|
|
|
void RegisterIntegerEnum(const char *parent, const char *sub,
|
|
const EnumIntegerEntry *entries, int count) {
|
|
void *L = State();
|
|
if (L == nullptr)
|
|
return;
|
|
EnsureGlobalTable(L, parent); // [parentTbl]
|
|
PushString(L, sub); // [parentTbl, subName]
|
|
NewTable(L); // [parentTbl, subName, subTbl]
|
|
for (int i = 0; i < count; ++i) {
|
|
PushString(L, entries[i].key);
|
|
PushNumber(L, static_cast<double>(entries[i].value));
|
|
SetTable(L, -3); // subTbl[key] = value
|
|
}
|
|
SetTable(L, -3); // parentTbl[sub] = subTbl
|
|
SetTop(L, -2); // pop parentTbl
|
|
}
|
|
|
|
void SetFieldNumber(void *L, const char *key, double value) {
|
|
PushString(L, key);
|
|
PushNumber(L, value);
|
|
SetTable(L, -3);
|
|
}
|
|
|
|
void SetFieldString(void *L, const char *key, const char *value) {
|
|
PushString(L, key);
|
|
PushString(L, value != nullptr ? value : "");
|
|
SetTable(L, -3);
|
|
}
|
|
|
|
void SetFieldBool(void *L, const char *key, bool value) {
|
|
PushString(L, key);
|
|
PushBoolean(L, static_cast<int>(value));
|
|
SetTable(L, -3);
|
|
}
|
|
|
|
void SetGlobalNumber(void *L, const char *name, double value) {
|
|
PushString(L, name);
|
|
PushNumber(L, value);
|
|
RawSet(L, GLOBALS_INDEX);
|
|
}
|
|
|
|
bool PushGlobalFunction(void *L, const char *name) {
|
|
PushString(L, name);
|
|
GetTable(L, GLOBALS_INDEX);
|
|
if (Type(L, -1) == TYPE_FUNCTION)
|
|
return true;
|
|
SetTop(L, -2); // pop the non-function
|
|
return false;
|
|
}
|
|
|
|
void CallGlobal(void *L, const char *name) {
|
|
const int top = GetTop(L);
|
|
if (PushGlobalFunction(L, name))
|
|
Call(L, 0, 0);
|
|
SetTop(L, top);
|
|
}
|
|
|
|
bool CallGlobalString(void *L, const char *name, const char *arg) {
|
|
const int top = GetTop(L);
|
|
const bool called = PushGlobalFunction(L, name);
|
|
if (called) {
|
|
PushString(L, arg);
|
|
Call(L, 1, 0);
|
|
}
|
|
SetTop(L, top);
|
|
return called;
|
|
}
|
|
|
|
void PushLocalizedString(void *L, const char *globalName, const char *fallback) {
|
|
PushString(L, globalName);
|
|
GetTable(L, GLOBALS_INDEX);
|
|
if (Type(L, -1) != TYPE_STRING) {
|
|
// Replace the non-string at top with the fallback so callers
|
|
// always see exactly one string at -1.
|
|
SetTop(L, GetTop(L) - 1);
|
|
PushString(L, fallback);
|
|
}
|
|
}
|
|
|
|
// In-place `string.gsub(stack[-1], pattern, replacement)` — replaces
|
|
// the string at the top of the stack with the substitution result.
|
|
// Discards gsub's second return (the substitution count) by
|
|
// requesting only one return from `Call`.
|
|
static void GsubInPlace(void *L, const char *pattern, const char *replacement) {
|
|
PushString(L, "string");
|
|
GetTable(L, GLOBALS_INDEX);
|
|
PushString(L, "gsub");
|
|
GetTable(L, -2); // [..., str, string, gsub]
|
|
Remove(L, -2); // [..., str, gsub]
|
|
Insert(L, -2); // [..., gsub, str]
|
|
PushString(L, pattern);
|
|
PushString(L, replacement);
|
|
Call(L, 3, 1); // [..., result]
|
|
}
|
|
|
|
void PushLocalizedFormatInt(void *L, const char *globalName,
|
|
const char *fallback, int n) {
|
|
// Resolve `string.format` from _G.
|
|
PushString(L, "format");
|
|
GetTable(L, GLOBALS_INDEX);
|
|
PushLocalizedString(L, globalName, fallback);
|
|
|
|
// Expand WoW's `|4singular:plural;` (and 3-form
|
|
// `|4nom:gen_sing:plural;`) pluralization template by piggybacking
|
|
// on Lua's `string.gsub`. The 2- vs 3-form distinction is locale
|
|
// grammar (Slavic counting has a separate genitive-singular form
|
|
// for 2..4); we collapse to nominative/plural since vanilla has
|
|
// no proper locale-aware counting rules. 3-form pattern runs
|
|
// first because it's more specific. Vanilla's `string.format`
|
|
// doesn't process `|4` natively, so without this the template
|
|
// would render literally.
|
|
const char *picker3 = (n == 1) ? "%1" : "%3";
|
|
const char *picker2 = (n == 1) ? "%1" : "%2";
|
|
GsubInPlace(L, "|4([^:;]+):([^:;]+):([^;]+);", picker3);
|
|
GsubInPlace(L, "|4([^:;]+):([^;]+);", picker2);
|
|
|
|
PushNumber(L, static_cast<double>(n));
|
|
Call(L, 2, 1); // format(expandedFmt, n) → string
|
|
}
|
|
} // namespace Lua
|
|
|
|
namespace Console {
|
|
namespace {
|
|
using RegisterCommand_t = int(__fastcall *)(const char *name, void *handler,
|
|
int category, const char *description);
|
|
using Write_t = void(__fastcall *)(const char *line, int colorFlag);
|
|
} // namespace
|
|
|
|
void RegisterCommand(const char *name, CommandHandler handler, int category,
|
|
const char *description) {
|
|
auto fn = reinterpret_cast<RegisterCommand_t>(Offsets::FUN_CONSOLE_COMMAND_REGISTER);
|
|
fn(name, reinterpret_cast<void *>(handler), category, description);
|
|
}
|
|
|
|
void Write(const char *line) {
|
|
auto fn = reinterpret_cast<Write_t>(Offsets::FUN_CONSOLE_WRITE);
|
|
fn(line, 0);
|
|
}
|
|
} // namespace Console
|
|
|
|
namespace {
|
|
ModuleAutoRegister *g_moduleHead = nullptr;
|
|
GlueModuleAutoRegister *g_glueModuleHead = nullptr;
|
|
HookAutoRegister *g_hookHead = nullptr;
|
|
} // namespace
|
|
|
|
ModuleAutoRegister::ModuleAutoRegister(Fn f) : fn(f), next(g_moduleHead) {
|
|
g_moduleHead = this;
|
|
}
|
|
|
|
void RunModuleRegistrations() {
|
|
for (auto *node = g_moduleHead; node != nullptr; node = node->next)
|
|
node->fn();
|
|
}
|
|
|
|
GlueModuleAutoRegister::GlueModuleAutoRegister(Fn f)
|
|
: fn(f), next(g_glueModuleHead) {
|
|
g_glueModuleHead = this;
|
|
}
|
|
|
|
void RunGlueModuleRegistrations() {
|
|
for (auto *node = g_glueModuleHead; node != nullptr; node = node->next)
|
|
node->fn();
|
|
}
|
|
|
|
HookAutoRegister::HookAutoRegister(uintptr_t target, void *hook, void **original)
|
|
: target(target), hook(hook), original(original), next(g_hookHead) {
|
|
g_hookHead = this;
|
|
}
|
|
|
|
bool RunHookRegistrations() {
|
|
for (auto *node = g_hookHead; node != nullptr; node = node->next) {
|
|
auto *targetPtr = reinterpret_cast<LPVOID>(node->target);
|
|
if (MH_CreateHook(targetPtr, node->hook,
|
|
reinterpret_cast<LPVOID *>(node->original)) != MH_OK)
|
|
return false;
|
|
if (MH_EnableHook(targetPtr) != MH_OK)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace Game
|