ISource2ModCommands
Command and chat handling interface for Source2Mod.
Provides functionality for:
- Registering console commands
- Listening to console command execution
- Handling in-game chat messages
Interface for registering and handling commands.
Supports:
- Chat-based commands (player messages)
- Console commands (server or client)
- Command listeners with pre/post execution hooks
Interface id ISource2ModCommands003 (SOURCE2MOD_COMMANDS_INTERFACE) · Global g_pSource2ModCommands · Header source2mod/ISource2ModCommands.h
Types
Source2ModCommandId
using Source2ModCommandId = Source2ModHookId;A console command of the plugin's own (RegisterConCommand).
Source2ModCommandHookId
using Source2ModCommandHookId = Source2ModHookId;A listener on a command somebody else owns (RegisterConListener).
Source2ModChatListenerId
using Source2ModChatListenerId = Source2ModHookId;A chat-only listener (RegisterChatListener).
ChatHandler
using ChatHandler = Source2ModCallback<void(const Source2ModCommandContext&, const Source2ModCommandArgs&, bool post)>;A chat command or console command handler: a function, an object and a method, or a lambda (Source2ModCallback).
CommandHandler
using CommandHandler = Source2ModCallback<Action(const Source2ModCommandContext&, const Source2ModCommandArgs&, bool post)>;Callback for console commands.
Methods
RegisterChatListener
virtual Source2ModChatListenerId RegisterChatListener(const char* pchName, ChatHandler handler, const char* pchPermission = nullptr) = 0;Registers a chat command.
Only fires for a chat message that starts with one of the configured chat triggers (public or silent, "!" and "/" by default), e.g. "!kick" or "/kick"; plain "kick" typed into chat does not fire it. The trigger is stripped before matching, so args.Arg(0) is the bare name. A silent trigger hides the message; a public one lets it show.
Whose it is, the core reads off the handler (see Source2ModCallback); what a plugin still holds at unload the core drops for it.
| Parameter | Type | Default | Description |
|---|---|---|---|
pchName | const char* | Command name without a trigger (e.g. "kick") | |
handler | ChatHandler | A function, an object and a method, or a lambda | |
pchPermission | const char* | nullptr | What a player needs to run it (see ISource2ModPermissions); nullptr or "" for everybody. A player without it gets the access-denied answer instead. |
Returns Source2ModChatListenerId — The id UnregisterChatListener(id) takes
UnregisterChatListener
virtual bool UnregisterChatListener(const char* pchName, const ChatHandler& handler) = 0;
virtual bool UnregisterChatListener(Source2ModChatListenerId id) = 0;Drops the chat command with this handler -- a function or an object and a method; a lambda goes by its id.
Drops the chat command RegisterChatListener() returned the id for.
| Parameter | Type |
|---|---|
pchName | const char* |
handler | const ChatHandler& |
id | Source2ModChatListenerId |
Returns bool — true when one was found
RegisterConCommand
virtual Source2ModCommandId RegisterConCommand(const char* pchName, ChatHandler handler, const char* pchPermission = nullptr) = 0;Registers a console command, reachable from chat as "!name" and "/name" too. Several handlers may share a name; each runs.
Whose it is, the core reads off the handler (see Source2ModCallback); what a plugin still holds at unload the core drops for it.
| Parameter | Type | Default | Description |
|---|---|---|---|
pchName | const char* | Command name (e.g. "sv_test") | |
handler | ChatHandler | A function, an object and a method, or a lambda | |
pchPermission | const char* | nullptr | What a player needs to run it (see ISource2ModPermissions); nullptr or "" for everybody. The server console always may. The owner can re-assign it under "Overrides" in permissions.json. |
Returns Source2ModCommandId — The id UnregisterConCommand(id) takes
UnregisterConCommand
virtual bool UnregisterConCommand(const char* pchName, const ChatHandler& handler) = 0;
virtual bool UnregisterConCommand(Source2ModCommandId id) = 0;Drops the console command handler -- a function or an object and a method; a lambda goes by its id. The command name stays claimed until the plugin unloads.
Drops the console command handler RegisterConCommand() returned the id for.
| Parameter | Type |
|---|---|
pchName | const char* |
handler | const ChatHandler& |
id | Source2ModCommandId |
Returns bool — true when one was found
RegisterConListener
virtual Source2ModCommandHookId RegisterConListener(const char* pchName, CommandHandler handler, bool post) = 0;Registers a listener on an existing console command.
The handler's Action:
- Ignore: do nothing
- Override: the original still runs; commands return nothing, so this has no further effect. Later pre listeners and all post listeners still fire.
- Supersede: block the original (pre only); the remaining pre listeners and all post listeners are skipped
Whose it is, the core reads off the handler (see Source2ModCallback); what a plugin still holds at unload the core drops for it.
| Parameter | Type | Description |
|---|---|---|
pchName | const char* | Existing command name to listen for |
handler | CommandHandler | A function, an object and a method, or a lambda |
post | bool | false to run before the original, true to run after |
Returns Source2ModCommandHookId — The id UnregisterConListener(id) takes
UnregisterConListener
virtual bool UnregisterConListener(const char* pchName, const CommandHandler& handler, bool post) = 0;
virtual bool UnregisterConListener(Source2ModCommandHookId id) = 0;Drops the console listener with this handler -- a function or an object and a method; a lambda goes by its id.
Drops the console listener RegisterConListener() returned the id for.
| Parameter | Type |
|---|---|
pchName | const char* |
handler | const CommandHandler& |
post | bool |
id | Source2ModCommandHookId |
Returns bool — true when one was found
ReplyToCommand
virtual void ReplyToCommand(const Source2ModCommandContext& ctx, const char* pszMessage) = 0;Answers a command where it was typed: to the player's chat when it came from chat, to the player's console when it came from their console, to the server console when it came from there. One line; the newline is added.
| Parameter | Type |
|---|---|
ctx | const Source2ModCommandContext& |
pszMessage | const char* |
Source2ModCommandContext
Constructors
Source2ModCommandContext(CPlayerSlot slot, int target, Source2ModCommandSource source = Source2ModCommandSource::Console);GetPlayerSlot
CPlayerSlot GetPlayerSlot() const;The player who issued the command; invalid for the server console.
GetCommandTarget
int GetCommandTarget() const;The engine's CommandTarget_t (CT_NO_TARGET, CT_FIRST_SPLITSCREEN_CLIENT, ...).
GetSource
Source2ModCommandSource GetSource() const;Where it was typed.
IsFromChat
bool IsFromChat() const;Typed in chat, with either trigger.
Source2ModCommandArgs
A command's arguments, in Source2Mod's own layout; same accessors as the engine's CCommand. The strings belong to the dispatch and are gone when the handler returns -- copy what has to outlive it.
Fields
| Field | Type |
|---|---|
kMaxArgs | int |
Constructors
Source2ModCommandArgs() = default;
Source2ModCommandArgs(int argc, const char* const* argv, const char* argS, const char* command);ArgC
int ArgC() const;Number of arguments, the command name included.
Arg
const char* Arg(int i) const;Argument i, the command name at 0; "" past the end.
| Parameter | Type |
|---|---|
i | int |
ArgS
const char* ArgS() const;Everything after the command name, as typed.
GetCommandString
const char* GetCommandString() const;The whole command line, as typed.
Enums
Source2ModCommandSource
enum class Source2ModCommandSource : uint8_tWhere a command was typed: what ReplyToCommand() answers into.
| Name | Description |
|---|---|
Console | A console -- the server's or the player's own |
Chat | Chat, with a public trigger ("!kick"); the message shows |
SilentChat | Chat, with a silent trigger ("/kick"); the message is hidden |
Macros
REGISTER_CHAT_LISTENER
#define REGISTER_CHAT_LISTENER(pchName, ...) g_pSource2ModCommands->RegisterChatListener(pchName, __VA_ARGS__)RegisterConCommand(name, handler) or RegisterConCommand(name, handler, permission).
REPLY_TO_COMMAND
#define REPLY_TO_COMMAND(ctx, fmt, ...) \
do {
char _source2modReply[512];
snprintf(_source2modReply, sizeof(_source2modReply), fmt, ##__VA_ARGS__);
g_pSource2ModCommands->ReplyToCommand(ctx, _source2modReply);
} while (0)ReplyToCommand() with printf formatting.
Other macros
| Macro | Expands to |
|---|---|
REGISTER_CON_COMMAND(pchName, ...) | g_pSource2ModCommands->RegisterConCommand(pchName, __VA_ARGS__) |
REGISTER_CON_LISTENER(pchName, handler, post) | g_pSource2ModCommands->RegisterConListener(pchName, handler, post) |