From c5b1431073ff0d3d62ffdc8fe012f9f470e0d18e Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Wed, 9 Sep 2026 12:17:33 -0500 Subject: [PATCH] feat(cvar): default scriptMemory to 0 Sets the cvar most people already set by hand, so a fresh install does not meet the interface memory popup. Only the default is overridden: the write happens when the live value still equals the cvar's registered value (read from +0x30, so no hardcoded 49152), which leaves a deliberate Config.wtf choice alone. The archive flag makes the 0 persist, so the comparison fails on every later boot and the write happens once per install. What the cvar gates, since none of it is obvious: FUN_00495590 polls Lua's live allocation (KB) against it, forces a full GC when over, and if still over fires MEMORY_EXHAUSTED and starts a 15-second timer, after which FUN_00401560(0) exits the process. FrameXML shows the event as the "disable some interface add-ons and restart" popup. The poll is gated on `limit > 0`, so 0 disables it, and nothing else caps Lua memory (lmemPool's allocator is six size classes falling through to the Storm heap, with no ceiling of its own). Timing is load-bearing: scriptMemory is writable on the glue state and read-only in the world, where the innermost setter's `if (flags & 4) return;` makes any write a silent no-op. The glue registration is what makes this land; the in-game one stays as a second chance. Verified by deleting the Config.wtf line, launching, and finding 0 in the AddOns dialog and back in Config.wtf on exit. Adds CVar::Factory::GetDefaultString for the comparison. --- docs/API.md | 27 ++++++++++ src/cvar/Factory.cpp | 12 ++++- src/cvar/Factory.h | 15 ++++++ src/cvar/ScriptMemory.cpp | 109 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 src/cvar/ScriptMemory.cpp diff --git a/docs/API.md b/docs/API.md index aa2375c5..7b9ddd1e 100644 --- a/docs/API.md +++ b/docs/API.md @@ -118,6 +118,7 @@ build instructions. - [`C_CVar.AreCVarsLoaded()`](#c_cvararecvarsloaded) - [`C_CVar.GetCVarBitfield(name, index)` / `C_CVar.SetCVarBitfield(name, index, value)`](#c_cvargetcvarbitfieldname-index--c_cvarsetcvarbitfieldname-index-value) - [`C_CVar.GetCVarBool(cvar)`](#c_cvargetcvarboolcvar) + - [The interface memory limit](#the-interface-memory-limit) - [Cursor](#cursor) - [`GetCursorInfo()`](#getcursorinfo) @@ -3019,6 +3020,32 @@ immediately visible on the other (a `SetCVar("foo", "1")` from a GlueXML script will read back as `"1"` on the in-world side, and vice versa). +### The interface memory limit + +The `scriptMemory` cvar caps how much memory the interface may use, counted +in kilobytes. Its own value is 49152, which is 48 MB. + +| Stage | What happens | +|---|---| +| Interface memory goes over the limit | The client frees what it can. | +| It is still over | A popup asks you to disable add-ons and restart. | +| 15 seconds later | The client closes. | + +A value of 0 turns the check off. ClassicAPI sets 0 for you, so a large +add-on set does not meet the popup, and it does that only while the cvar +still holds its own value. If you picked a value yourself, your value +stands. + +`SetCVar` cannot change this cvar while you are in the world, because the +client makes it read-only there. To pick your own limit, close the game and +add the line yourself in `WTF\Config.wtf`: + +``` +SET scriptMemory "65536" +``` + +That asks for a 64 MB limit, and any value above 0 turns the check back on. + ## Cursor ### `GetCursorInfo()` diff --git a/src/cvar/Factory.cpp b/src/cvar/Factory.cpp index 3754f34c..018decb8 100644 --- a/src/cvar/Factory.cpp +++ b/src/cvar/Factory.cpp @@ -47,11 +47,15 @@ using FindCVar_t = void *(__fastcall *)(const char *name); using SetCVarValue_t = char(__thiscall *)(void *cvar, const char *value, int a3, int a4, int a5, int a6); -const char *ReadValue(void *cvar) { +const char *ReadStringField(void *cvar, uintptr_t offset) { if (cvar == nullptr) return nullptr; return *reinterpret_cast( - static_cast(cvar) + Offsets::OFF_CVAR_VALUE_STR); + static_cast(cvar) + offset); +} + +const char *ReadValue(void *cvar) { + return ReadStringField(cvar, Offsets::OFF_CVAR_VALUE_STR); } } // namespace @@ -76,6 +80,10 @@ const char *GetString(Handle cvar) { return ReadValue(cvar); } +const char *GetDefaultString(Handle cvar) { + return ReadStringField(cvar, Offsets::OFF_CVAR_DEFAULT_STR); +} + int GetInt(Handle cvar, int fallback) { const char *value = ReadValue(cvar); if (value == nullptr || value[0] == '\0') diff --git a/src/cvar/Factory.h b/src/cvar/Factory.h index dfbbc552..4b64cf31 100644 --- a/src/cvar/Factory.h +++ b/src/cvar/Factory.h @@ -66,6 +66,14 @@ Handle Find(const char *name); // valid for the cvar's lifetime. const char *GetString(Handle cvar); +// Read the value the cvar was REGISTERED with, which the engine keeps +// alongside the live value and never rewrites. Comparing the two is how the +// engine's own `cvarlist` decides whether to print "(default: %s)", and it is +// the only way to tell "nobody has ever set this" from "set to exactly the +// default" without a marker of our own. Returns null if `cvar` is null or the +// cvar was registered without a default. +const char *GetDefaultString(Handle cvar); + // Read the value parsed as a base-10 integer, or `fallback` if `cvar` is // null or the value isn't numeric. int GetInt(Handle cvar, int fallback = 0); @@ -75,6 +83,13 @@ int GetInt(Handle cvar, int fallback = 0); void SetString(Handle cvar, const char *value); // Convenience: set from an integer. +// +// Both setters go through the engine's own value setter, which REFUSES the +// write when the cvar carries `CVAR_FLAG_READ_ONLY` — silently, since the +// innermost writer opens with `if (flags & 0x4) return;`. Some cvars gain +// that bit partway through boot (`scriptMemory` is writable on the glue +// state and read-only once in the world), so a write that has to land needs +// to happen before the bit appears rather than fight it. void SetInt(Handle cvar, int value); } // namespace CVar::Factory diff --git a/src/cvar/ScriptMemory.cpp b/src/cvar/ScriptMemory.cpp new file mode 100644 index 00000000..7bb5958f --- /dev/null +++ b/src/cvar/ScriptMemory.cpp @@ -0,0 +1,109 @@ +// 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 . + +// `CVar::ScriptMemory` — turn the interface memory watchdog off by default. +// +// The engine polls Lua's live allocation against the `scriptMemory` cvar +// (KB; registered with default "49152", so 48 MB) in `FUN_00495590`: +// +// limit = *(int *)(scriptMemoryCVar + 0x28); // parsed-int cache +// if (enabled && limit > 0) { +// if (luaKB() > limit) ForceFullGC(); +// if (luaKB() > limit) { +// if (!deadline) { deadline = now + 15000; +// Fire(MEMORY_EXHAUSTED, "%d", mb); } +// else if (now >= deadline) Quit(); // FUN_00401560(0) +// } else if (deadline) { deadline = 0; Fire(MEMORY_RECOVERED); } +// } +// +// FrameXML turns MEMORY_EXHAUSTED into the "disable some interface add-ons +// and restart" popup, and 15 seconds later the client EXITS. So the cap is +// not advisory: it ends the session. +// +// 48 MB was a reasonable budget for the addons of the day and is not one for +// a modern set, so the popup and the shutdown land on people whose addons +// are working correctly. The gate is `limit > 0`, so writing 0 disables the +// poll outright — the value Blizzard's own AddOns dialog at character select +// lets a user type, via `SetScriptMemory` (which multiplies by 1024; +// verified `SHL EAX,0xa` at 0x0046DA8D). +// +// Nothing else caps Lua memory. `lmemPool.cpp`'s allocator (`FUN_006FAE90`) +// is six size classes that fall through to the Storm heap for anything +// larger, with no ceiling of its own; its "not enough memory" is a genuine +// allocation failure. So 0 hands the bound to the process address space +// (this client is Large Address Aware) rather than to a number picked in +// 2006 — the engine's real limit instead of an invented one. +// +// TIMING IS THE WHOLE TRICK. The cvar becomes READ-ONLY partway through boot: +// in the world, `SetCVar("scriptMemory", "49152")` raises `"scriptMemory" is +// read only` and `GetCVarInfo` reports `isReadOnly`, which is bit +// `CVAR_FLAG_READ_ONLY` at `+0x1C` — and the innermost setter +// (`FUN_0063E0B0`) opens with `if (flags & 4) return;`, so a write then is a +// silent no-op. But the bit is NOT set yet on the glue state, where the write +// below lands: verified in game by deleting the Config.wtf line, launching, +// and finding 0 in the AddOns dialog and back in Config.wtf on exit. +// +// The bit is not set at registration either: the disassembly at `0x00402E24` +// pushes flags 0 and registerConsole 0, so the registrar computes +// `0 | 1 | 0x80000000`, and nothing in the binary ORs bit 2 into a cvar's +// flags. Something between the glue screen and the world sets it; the +// mechanism is still unidentified. Practical consequence: this must keep +// running from the GLUE registration. Do not "simplify" it to the in-game +// hook alone — the write would silently stop working, and the in-game hook +// stays only as a second chance for a boot that somehow skips glue. +// +// We override the DEFAULT ONLY: if the live value differs from the value the +// cvar was registered with, someone chose it and we leave it alone. The cvar +// carries the archive flag, so our 0 persists to Config.wtf and the +// comparison then fails on every later boot — the write happens once per +// install, not once per session. The one false positive is a user who puts +// exactly the default in Config.wtf, which reads as untouched; recovering the +// watchdog means choosing any other value. +// +// Runs from both the glue and in-game registration flows. Cvar storage is +// process-global and the write is idempotent by construction (afterwards the +// value no longer equals the default), so whichever fires first wins and the +// other is a no-op. Glue fires earliest, which also means the AddOns dialog +// shows 0 rather than 48 on a first launch. + +#include "cvar/Factory.h" + +#include "Game.h" + +#include + +namespace CVar::ScriptMemory { + +namespace { + +constexpr const char *kCVarName = "scriptMemory"; + +void ApplyDefaultOverride() { + Factory::Handle cvar = Factory::Find(kCVarName); + if (cvar == nullptr) + return; // registered at boot by the engine; absent only if that moved + const char *value = Factory::GetString(cvar); + const char *fallback = Factory::GetDefaultString(cvar); + if (value == nullptr || fallback == nullptr) + return; + if (std::strcmp(value, fallback) != 0) + return; // deliberately set — the user's choice stands + Factory::SetInt(cvar, 0); // lands on the glue state; see the timing note +} + +const Game::ModuleAutoRegister _autoreg{&ApplyDefaultOverride}; +const Game::GlueModuleAutoRegister _glueAutoreg{&ApplyDefaultOverride}; + +} // namespace + +} // namespace CVar::ScriptMemory