From 2c2f280c514f55bee669b1c4bb9ce664eb50faee Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sun, 6 Sep 2026 15:46:30 -0500 Subject: [PATCH] feat(console): add ConsoleGetAllCommands Returns every console command and console variable as ConsoleCommandInfo tables, backed by the registry FUN_CONSOLE_COMMAND_REGISTER fills. The walk mirrors the `help` handler (FUN_0063FDC0), the only other thing that enumerates it: step with next = *(uint32_t *)(node + LINK_OFFSET + 4), stop on null or a low-bit-tagged sentinel. Commands and CVars are ONE list, not two. The CVar registrar's last act is FUN_CONSOLE_COMMAND_REGISTER(cvar->name, 0x0063DDE0, categoryId, help) -- which is what makes `/console ` work -- so the command list already holds every CVar, and walking the CVar list that `cvarlist` and the config saver use would report each one twice. That shared handler address is also an exact commandType test: a node pointing at it is a Cvar, anything else a Command. A pointer comparison against a value the registrar itself writes, not a name or category heuristic. Named as a global, not a C_Console member. The wiki files it under C_Console.GetAllCommands, but on a live 12.1.0 client C_Console is nil and ConsoleGetAllCommands is the real name -- the page is stale about the namespace. The field names come from Blizzard's own generated API docs and were then confirmed against a live entry: command, help, category, commandType, scriptContents, scriptParameters, with the two script fields empty strings rather than nil there too. Enum.ConsoleCategory and Enum.ConsoleCommandType are stated in full rather than derived from the engine's nine categories. Deriving works -- reading VAR_CONSOLE_CATEGORY_TABLE and capitalizing "debug" to "Debug" was verified in-game -- but it makes the enum track the wrong thing. It is a Blizzard contract, not a description of the local console, and it carries values this client cannot produce: Reveal 9 and None 10 have no category here, Macro 2 and Script 3 no registration path. A client that renamed its `gm` category would silently register Enum.ConsoleCategory.Master and turn Enum.ConsoleCategory.Gm into nil, breaking a constant meant to be fixed everywhere. Offsets.h keeps the verification: ids 0..8 are debug, graphics, console, combat, game, default, net, sound, gm in that order, so a category read off a node lands inside the enum without translation. scriptContents and scriptParameters describe console macros and scripts, which have no registration path here, so both are always "". Verified in-game: 538 entries, 487 of them CVars. `help` -> Command / Console with its description, `realmList` -> Cvar / Default, and our own `ExportDBCFiles` -> Command / Debug, which also proves the walk sees runtime registrations rather than only boot-time ones. C_Console is absent, both enums resolve including the values this client cannot return, and every field has its declared type. --- README.md | 1 + docs/API.md | 37 ++++++++++ src/Offsets.h | 34 +++++++++ src/console/Commands.cpp | 155 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 src/console/Commands.cpp diff --git a/README.md b/README.md index 69c51c97..ec79169a 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ reference in **[docs/API.md](docs/API.md)**. | [Class](docs/API.md#class) | `FillLocalizedClassList` | | [ColorUtil](docs/API.md#colorutil) | `C_ColorUtil.ConvertRGBToHSV`, `C_ColorUtil.ConvertHSVToRGB`, `C_ColorUtil.ConvertHSVToHSL`, `C_ColorUtil.ConvertHSLToHSV`, `C_ColorUtil.ConvertHSLToRGB`, `C_ColorUtil.GenerateTextColorCode`, `C_ColorUtil.WrapTextInColor`, `C_ColorUtil.WrapTextInColorCode` | | [Combat](docs/API.md#combat) | `InCombatLockdown`, `StartAttack`, `StopAttack` | +| [Console](docs/API.md#console) | `ConsoleGetAllCommands` | | [CVar](docs/API.md#cvar) | `C_CVar.GetCVarBool` | | [Cursor](docs/API.md#cursor) | `GetCursorInfo` | | [Container](docs/API.md#container) | `C_Container.CalculateTotalNumberOfFreeBagSlots`, `C_Container.GetContainerItemCharges`, `C_Container.GetContainerItemDurability`, `C_Container.GetContainerItemID`, `C_Container.GetContainerItemInfo`, `C_Container.GetContainerItemRepairCost`, `C_Container.GetContainerNumFreeSlots`, `C_Container.GetItemCooldown`, `C_Container.HasContainerItem`, `C_Container.IsContainerItemOpenable`, `C_Container.MoveItem`, `C_Container.PlayerHasHearthstone`, `C_Container.SwapItems`, `C_Container.UseHearthstone`, `GetItemCooldown` | diff --git a/docs/API.md b/docs/API.md index 8cb63af2..cb54a1a4 100644 --- a/docs/API.md +++ b/docs/API.md @@ -60,6 +60,7 @@ build instructions. - [`StopAttack()`](#stopattack) - [Console](#console) + - [`ConsoleGetAllCommands()`](#consolegetallcommands) - [`ExportInterfaceFiles art|code` (console command)](#exportinterfacefiles-artcode-console-command) - [`ExportDBCFiles` (console command)](#exportdbcfiles-console-command) @@ -1588,6 +1589,42 @@ Stops your melee auto-attack. If you are not attacking, the call does nothing. ## Console +### `ConsoleGetAllCommands()` + +Returns a list of every console command and console variable the client has, +each as a table: + +| Field | Type | Meaning | +|-------|------|---------| +| `command` | string | the name you type in the console | +| `help` | string | the description, or `""` when it has none | +| `category` | number | an `Enum.ConsoleCategory` value | +| `commandType` | number | an `Enum.ConsoleCommandType` value | +| `scriptContents` | string | always `""` | +| `scriptParameters` | string | always `""` | + +```lua +for _, info in ipairs(ConsoleGetAllCommands()) do + if info.commandType == Enum.ConsoleCommandType.Cvar then + print(info.command, GetCVar(info.command)) + end +end +``` + +Console variables are included, and most of them carry no description, so +`help` is often `""` for them. Console macros and scripts do not exist on this +client, so `commandType` is only ever `Cvar` or `Command`, and the two script +fields are always empty. + +`Enum.ConsoleCategory` holds `Debug` 0, `Graphics` 1, `Console` 2, `Combat` 3, +`Game` 4, `Default` 5, `Net` 6, `Sound` 7, `Gm` 8, `Reveal` 9 and `None` 10. +Every command reports one of `Debug` through `Gm`. +`Enum.ConsoleCommandType` holds `Cvar` 0, `Command` 1, `Macro` 2 and +`Script` 3. + +The list is live: a command added while the game runs, such as one of the export +commands below, appears in the next call. + ### `ExportInterfaceFiles art|code` (console command) A **developer-console command** (not a Lua function) — type it into the diff --git a/src/Offsets.h b/src/Offsets.h index 874b0a05..07397953 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -5573,6 +5573,40 @@ enum Offsets { // command's own registration in `FUN_006400E0`. FUN_CONSOLE_COMMAND_REGISTER = 0x0063F9E0, + // The registry that registrar fills: a Storm intrusive list plus a by-name + // hash. `Console::Commands` walks the list to back `C_Console.GetAllCommands`, + // mirroring the `help` handler (`FUN_0063FDC0`) exactly — it prints each + // command whose `+0x20` matches the requested category, stepping with + // `next = *(uint32_t *)(node + LINK_OFFSET + 4)` and stopping on a null or + // low-bit-tagged sentinel, the same tagged-pointer idiom as the addon + // registry. LINK_OFFSET is a value, not a pointer. + VAR_CONSOLE_COMMAND_LIST_HEAD = 0x00C4F854, + VAR_CONSOLE_COMMAND_LINK_OFFSET = 0x00C4F84C, + // Node fields, from the registrar's stores (`node[5..8]`) cross-checked + // against the help handler's reads. The registrar keeps `name` and `help` + // BY POINTER — it copies neither — so both outlive it only because every + // caller passes a literal. + OFF_CONSOLE_COMMAND_NAME = 0x14, + OFF_CONSOLE_COMMAND_HANDLER = 0x18, + OFF_CONSOLE_COMMAND_HELP = 0x1C, // may be null + OFF_CONSOLE_COMMAND_CATEGORY = 0x20, + // Every CVar is ALSO a console command: the CVar registrar's last act is + // `FUN_CONSOLE_COMMAND_REGISTER(cvar->name, 0x0063DDE0, categoryId, help)`, + // with this shared get/set handler. So the command list already contains + // every CVar — walking the CVar list too would double-count — and the + // handler pointer is an exact test for which kind a node is. + FUN_CONSOLE_CVAR_COMMAND_HANDLER = 0x0063DDE0, + // `{ int id; char name[0x14]; }` x 9, read by the bare `help` command to + // list the categories. Ids 0..8 are debug, graphics, console, combat, game, + // default, net, sound, gm, in that order — which is Enum.ConsoleCategory's + // own 0..8, so a category read off a command node needs no translation. + // This table is where that claim was checked; `Console::Commands` states + // the enum in full rather than deriving it, because the enum also carries + // Reveal (9) and None (10), which this client has no categories for. + VAR_CONSOLE_CATEGORY_TABLE = 0x008653A0, + CONSOLE_CATEGORY_COUNT = 9, + SIZEOF_CONSOLE_CATEGORY_ENTRY = 0x18, + // `ConsoleWrite(const char *line /*ecx*/, int colorFlag /*edx*/)` — // appends a line to the developer console's output buffer. No-ops // cleanly when the console isn't active (gated on the console-init diff --git a/src/console/Commands.cpp b/src/console/Commands.cpp new file mode 100644 index 00000000..bf91bbce --- /dev/null +++ b/src/console/Commands.cpp @@ -0,0 +1,155 @@ +// 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 . + +// `ConsoleGetAllCommands()` — every console command and console variable the +// client currently has, as `ConsoleCommandInfo` tables. +// +// A plain global, not a `C_Console` member: the wiki files this under +// `C_Console.GetAllCommands`, but on a live client that namespace does not +// exist and `ConsoleGetAllCommands` is the real name. +// +// The engine keeps them in one registry, filled by +// FUN_CONSOLE_COMMAND_REGISTER: a Storm intrusive list for iteration plus a +// by-name hash for dispatch. This walks the list the same way the `help` +// command does, which is the only other thing that enumerates it. +// +// ONE LIST, NOT TWO. Commands and CVars do not live separately. The CVar +// registrar's last act is to register the CVar as a console command as well — +// `FUN_CONSOLE_COMMAND_REGISTER(cvar->name, , categoryId, +// help)` — which is what makes `/console ` work. So the +// command list already holds every CVar, and walking the CVar list too (the one +// `cvarlist` and the config saver use) would report each one twice. +// +// That also gives an exact `commandType`: every CVar shares one handler +// address, so a node pointing at it is a CVar and anything else is a command. +// It is a pointer comparison against a value the registrar itself writes, not a +// name or category heuristic. +// +// FIELDS WE CANNOT FILL. `scriptContents` and `scriptParameters` describe +// console macros and scripts, which this client has no notion of — there is no +// registration path that could produce one. They are always the empty string, +// so a caller reading them gets a string rather than a nil surprise. `category` +// is the engine's own number, and the engine's table (debug, graphics, console, +// combat, game, default, net, sound, gm) is Enum.ConsoleCategory's 0..8 exactly, +// so it needs no translation. + +#include "Game.h" +#include "Offsets.h" + +#include + +namespace Console::Commands { + +namespace { + +// Enum.ConsoleCommandType. Macro (2) and Script (3) exist in the contract but +// cannot occur here. +constexpr int kCommandTypeCvar = 0; +constexpr int kCommandTypeCommand = 1; + +// A list node, or 0 once the walk reaches the end. The low bit tags the +// sentinel, the same convention the addon registry and the parser lists use. +bool IsNode(uintptr_t node) { return node != 0 && (node & 1) == 0; } + +void SetStringField(void *L, const char *key, const char *value) { + Game::Lua::PushString(L, key); + Game::Lua::PushString(L, value != nullptr ? value : ""); + Game::Lua::SetTable(L, -3); +} + +void SetIntField(void *L, const char *key, int value) { + Game::Lua::PushString(L, key); + Game::Lua::PushNumber(L, static_cast(value)); + Game::Lua::SetTable(L, -3); +} + +// `ConsoleGetAllCommands() -> ConsoleCommandInfo[]` +int __fastcall Script_GetAllCommands(void *L) { + Game::Lua::SetTop(L, 0); + Game::Lua::NewTable(L); + + const uintptr_t linkOffset = + Game::Read(Offsets::VAR_CONSOLE_COMMAND_LINK_OFFSET); + uintptr_t node = Game::Read(Offsets::VAR_CONSOLE_COMMAND_LIST_HEAD); + + int count = 0; + while (IsNode(node)) { + const auto *name = + Game::Read(node + Offsets::OFF_CONSOLE_COMMAND_NAME); + const auto *help = + Game::Read(node + Offsets::OFF_CONSOLE_COMMAND_HELP); + const uintptr_t handler = + Game::Read(node + Offsets::OFF_CONSOLE_COMMAND_HANDLER); + const int category = + Game::Read(node + Offsets::OFF_CONSOLE_COMMAND_CATEGORY); + + // Step before building the entry: the walk stays correct even if a + // field read below is skipped. + node = Game::Read(node + linkOffset + 4); + + if (name == nullptr) + continue; // a node the registrar never finished; nothing to report + + Game::Lua::PushNumber(L, ++count); + Game::Lua::NewTable(L); + SetStringField(L, "command", name); + SetStringField(L, "help", help); + SetIntField(L, "category", category); + SetIntField(L, "commandType", + handler == Offsets::FUN_CONSOLE_CVAR_COMMAND_HANDLER + ? kCommandTypeCvar + : kCommandTypeCommand); + SetStringField(L, "scriptContents", ""); + SetStringField(L, "scriptParameters", ""); + Game::Lua::SetTable(L, -3); + } + return 1; +} + +// Both enums are Blizzard's, stated in full rather than derived from the +// engine's own category table. They are an API contract, not a description of +// what this client happens to have: an addon asking for a value we can never +// return still deserves the number instead of a nil. `Macro` and `Script` have +// no registration path here, and `Reveal` / `None` are not among the engine's +// nine categories -- everything else lines up, which is the point. +const Game::Lua::EnumIntegerEntry kCommandTypeEntries[] = { + {"Cvar", kCommandTypeCvar}, + {"Command", kCommandTypeCommand}, + {"Macro", 2}, + {"Script", 3}, +}; + +// The engine's table at VAR_CONSOLE_CATEGORY_TABLE holds ids 0..8 named debug, +// graphics, console, combat, game, default, net, sound, gm, in that order -- +// so a `category` read off a command node is already one of these. +const Game::Lua::EnumIntegerEntry kCategoryEntries[] = { + {"Debug", 0}, {"Graphics", 1}, {"Console", 2}, {"Combat", 3}, + {"Game", 4}, {"Default", 5}, {"Net", 6}, {"Sound", 7}, + {"Gm", 8}, {"Reveal", 9}, {"None", 10}, +}; + +void RegisterLuaFunctions() { + Game::Lua::RegisterGlobalFunction("ConsoleGetAllCommands", &Script_GetAllCommands); + Game::Lua::RegisterIntegerEnum("Enum", "ConsoleCommandType", kCommandTypeEntries, + sizeof(kCommandTypeEntries) / + sizeof(kCommandTypeEntries[0])); + Game::Lua::RegisterIntegerEnum("Enum", "ConsoleCategory", kCategoryEntries, + sizeof(kCategoryEntries) / + sizeof(kCategoryEntries[0])); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +} // namespace Console::Commands