mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 16:10:36 +00:00
The v1.12.8 gap heuristic (keep the stored length when it overshoots the border by exactly one) protected `table.insert(t, nil)` slots (#36) but broke every 5.1-dialect table that is stale by exactly one — a pairs-cleared one-element table, or a recycled table whose previous life was one slot longer. Tablet-2.0's del()/copy() pool produces the latter on every tooltip refresh, so FuBar/Ace2/Tablet menus and tooltips shifted by one with a nil hole at slot 1. The two states are byte-identical, and both consumers reach the heal through table.insert — neither the table nor the caller can split them. Only the writer knows which dialect the table speaks, and the write is observable: co-hook the engine's table.insert (FUN_LUA_TABLE_INSERT = 0x7FB6B0, verified against the table-lib luaL_reg pair in .data and a decompile matching 5.0.2 tinsert line for line) and record {table -> stored length} in a weak-keyed registry table whenever the two-arg form appends a literal nil. Rule 3 now heals every stale table to the border (the 5.1 answer) and keeps the stored length only when the gap is one AND the mark still matches. Stale marks die on their own: any later insert/remove moves the stored length off the recorded value, and the weak key collects with the table. Verified in-game with a 21-test suite (directed regressions for #29/#36/#39, byte-identical twin discrimination, metamethod isolation, coroutine-side marking, and a seeded differential fuzzer against an executable model of the contract): 15/21 on the broken build, 21/21 with the fix, #36's nil-append cases among the passes both times. New plumbing: LUA_SET_METATABLE (0x6F4020, from LuaCAPI.md) and Game::Lua::SetMetatable for building the weak mark table from C. Fixes #39
352 lines
13 KiB
C++
352 lines
13 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 {
|
|
|
|
void *ResolveUnitToken(const char *token) {
|
|
using ResolveUnitToken_t = void *(__fastcall *)(const char *token);
|
|
auto fn = reinterpret_cast<ResolveUnitToken_t>(Offsets::FUN_RESOLVE_UNIT_TOKEN);
|
|
return fn(token);
|
|
}
|
|
|
|
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(SetMetatable,lua_setmetatable,LUA_SET_METATABLE)\
|
|
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 Game::Read<void *>(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;
|
|
ReloadAutoRegister *g_reloadHead = 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();
|
|
}
|
|
|
|
ReloadAutoRegister::ReloadAutoRegister(Fn f) : fn(f), next(g_reloadHead) {
|
|
g_reloadHead = this;
|
|
}
|
|
|
|
void RunReloadCleanups() {
|
|
for (auto *node = g_reloadHead; 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;
|
|
// Queue, don't enable. The caller (InstallHooks) applies every
|
|
// queued hook with a single MH_ApplyQueued, so all ~90 hooks share
|
|
// ONE thread-freeze instead of one freeze per hook. This is the
|
|
// difference that keeps install cheap on machines whose security
|
|
// stack intercepts SuspendThread / VirtualProtect per call.
|
|
if (MH_QueueEnableHook(targetPtr) != MH_OK)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace Game
|