From addef29adccdabf7efd570f736c96f74218131f5 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Fri, 15 May 2026 14:30:13 -0500 Subject: [PATCH] Bypass Script_GetCVar, return nil for cvars that don't exist tested on anniversary --- docs/API.md | 24 +++++++++++++----------- src/Offsets.h | 21 ++++++++++++++++----- src/cvar/Bool.cpp | 39 +++++++++++++++++++++++---------------- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/docs/API.md b/docs/API.md index 37352258..1c6bc863 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4248,28 +4248,30 @@ Errors via `lua_error` on: ### `C_CVar.GetCVarBool(cvar)` -Returns the cvar's value coerced to a boolean. The `C_CVar` -namespace was added in 10.x; vanilla 1.12 only has `GetCVar` which -returns a string. +Returns the cvar's value coerced to a boolean, or `nil` if no cvar +by that name is registered. The `C_CVar` namespace was added in +10.x; vanilla 1.12 only has `GetCVar` which returns a string. ```lua -C_CVar.GetCVarBool("autoLootDefault") -- true if cvar is "1" or "true" -C_CVar.GetCVarBool("nameplateMotion") -- false if cvar is "0", empty, or unknown +C_CVar.GetCVarBool("gxMaximize") -- true if the window is set fullscreen +C_CVar.GetCVarBool("CombatDamage") -- false if floating damage text is off +C_CVar.GetCVarBool("doesNotExist") -- nil — distinguishable from false ``` -Coercion rules: +Coercion rules (only applied when the cvar exists): | Cvar string value | Result | |---|---| | `"1"` or any non-zero numeric (`"42"`, `"-3"`, …) | `true` | | `"true"` (case-insensitive) | `true` | -| `"0"` or empty / nil / unknown cvar | `false` | +| `"0"` or empty | `false` | | Non-numeric, non-`"true"` (`"on"`, `"yes"`) | `false` | +| Cvar name not registered | `nil` | -Implementation dispatches the engine's own `Script_GetCVar` to -fetch the string, then runs the coercion in C. Unknown cvars -silently return `false` rather than erroring — matches modern -behavior. +Implementation reads the cvar registry directly (engine's +`FUN_FIND_CVAR` at `0x0063DEC0`), bypassing `GetCVar`'s Lua-error +path for unknown cvars. The `nil` return lets callers distinguish +"the cvar exists and is falsy" from "no such cvar." ## Table diff --git a/src/Offsets.h b/src/Offsets.h index 66cd038f..b75af3d2 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -112,11 +112,22 @@ enum Offsets { // Registers a single global Lua function. __fastcall(name, func). FUN_FRAMESCRIPT_REGISTER_FUNCTION = 0x00704120, - // `Script_GetCVar(L)` — the vanilla Lua C function for `GetCVar`. - // Reads the cvar name from stack[1], pushes the string value (or - // nil for unknown cvars). Dispatched from `C_CVar.GetCVarBool` to - // fetch the string before coercing to bool. - FUN_SCRIPT_GET_CVAR = 0x00488BA0, + // Direct cvar lookup — `__fastcall(const char *name) → CVar* | NULL`. + // Hash-table by-name lookup over the CVar registry; same call + // `Script_GetCVar` makes internally before the engine wraps the + // result in lua_pushstring + a "CVar doesn't exist" error path. + // Calling it directly lets us skip both the Lua roundtrip and the + // unknown-cvar error — we coerce NULL to false instead, matching + // modern `C_CVar.GetCVarBool` semantics. + // + // The returned struct holds the value string at `+0x20` (read by + // `Script_GetCVar` at `0x00488BF9` as `mov edx, [eax+0x20]; call + // lua_pushstring`). The value is the raw `char *` the engine + // stores; reading it directly is safe for the lifetime of the + // cvar (vanilla cvars don't get re-allocated outside `/console + // set` flows, which we don't race here). + FUN_FIND_CVAR = 0x0063DEC0, + OFF_CVAR_VALUE_STR = 0x20, // Game::ResolveUnitToken — __fastcall(ecx = const char *token) → CGUnit_C *. // Returns the unit pointer for "player", "target", "party1", etc. Use this diff --git a/src/cvar/Bool.cpp b/src/cvar/Bool.cpp index 9079bf8f..a77a2f7b 100644 --- a/src/cvar/Bool.cpp +++ b/src/cvar/Bool.cpp @@ -14,11 +14,13 @@ #include "Game.h" #include "Offsets.h" +#include + namespace CVar::Bool { namespace { -using ScriptFn_t = int(__fastcall *)(void *L); +using FindCVar_t = const uint8_t *(__fastcall *)(const char *name); // Case-insensitive "true" check for the string-form match. Bounded // 4-char comparison + null terminator — same shape `BookTypeIsPet` @@ -60,28 +62,33 @@ bool StringToBool(const char *s) { return value != 0; } -// `C_CVar.GetCVarBool(cvar)` — dispatches the engine's own -// `Script_GetCVar` to read the string, then coerces. Returns 1 -// (one value pushed) on any input — modern behavior is "false" for -// missing/unknown cvars rather than nil/error. +// `C_CVar.GetCVarBool(cvar)` — looks the cvar up directly through +// the engine's by-name hash table at `FUN_FIND_CVAR`, reads the +// value string at `+0x20`, coerces to bool. Returns `nil` for +// unknown cvars so callers can distinguish "missing" from "set to +// false" — same shape as modern `C_CVar.GetCVarBool`. +// +// Going through `Script_GetCVar` would work too but adds a Lua-stack +// roundtrip and raises a Lua error for unknown cvars +// (`"CVar \"%s\" doesn't exist."`). The direct lookup lets us turn +// that into the nil return cleanly. int __fastcall Script_C_CVar_GetCVarBool(void *L) { if (!Game::Lua::IsString(L, 1)) { Game::Lua::Error(L, "Usage: C_CVar.GetCVarBool(\"cvar\")"); return 0; } - // Reuse the existing stack[1] (the cvar name). Script_GetCVar - // reads its arg from stack[1] and pushes the string (or nil) on - // top. Don't blow the stack first — the name is already there. - auto getCVar = reinterpret_cast(Offsets::FUN_SCRIPT_GET_CVAR); - getCVar(L); + const char *name = Game::Lua::ToString(L, 1); - const char *value = nullptr; - if (Game::Lua::Type(L, -1) == Game::Lua::TYPE_STRING) - value = Game::Lua::ToString(L, -1); + auto findCVar = reinterpret_cast(Offsets::FUN_FIND_CVAR); + const uint8_t *cvar = findCVar(name); + if (cvar == nullptr) { + Game::Lua::PushNil(L); + return 1; + } - const bool result = StringToBool(value); - Game::Lua::SetTop(L, 0); - Game::Lua::PushBoolean(L, result ? 1 : 0); + const char *value = *reinterpret_cast( + cvar + Offsets::OFF_CVAR_VALUE_STR); + Game::Lua::PushBoolean(L, StringToBool(value) ? 1 : 0); return 1; }