Command Parsing

From P2SR Wiki

Command Parsing

Overview

The parsing of commands is fundamental to all source engine games, as it is essential for the player to actually be able to send instructions to the game. Player-triggered commands are generally action commands (+forward, +duck, +attack, etc) executed through keybinds, but arbitrary commands can be executed through the developer console, such as changing what commands the aforementioned keybinds execute. The goal of this article is to explain how the source engine splits up a given string (the 'command string') into arguments.

Source Spaghetti

There are a number of idiosyncrasies specific to the source engine implementation of this method. Some follow.

  • The maximum operable command length (COMMAND_MAX_LENGTH) is set to 512, but the comparison is performed as such: if length >= COMMAND_MAX_LENGTH - 1. As such, the effective maximum length is 510. Over this length, the command will be completely discarded. Further investigation is required regarding nMaxLen.
CCommand::Tokenize: Encountered command which overflows the tokenizer buffer.. Skipping!
  • The maximum number of arguments in a command (COMMAND_MAX_ARGC) is set to 64, but due to array indexing strangeness, a command with exactly 64 arguments will display a warning but will still function. The arguments will be clamped to the first 64 after surpassing this value.
CCommand::Tokenize: Encountered command which overflows the argument buffer.. Clamped!

Delimiters

To begin, it is necessary to understand the concept of delimiters, which are special characters in a plaintext string that separate logical statements within it. The delimiters used in the source engine are space ( ), quotation marks ("), and the 'breakset' which comprises parentheses (( and )), curly brackets ({ and }), the colon (:), and the apostrophe ('). All these delimiters except quotation marks are ignored within quotation marks. That is to say, a quotation mark in a command unequivocally separates two arguments.

It should also be noted that non-ASCII characters do not behave nicely in the in-game console, but cooperate when specified in a .cfg file. More investigation is necessary. In any case, make sure your quotation marks are ASCII 0x22.

Example

TBW

Empirical Tests

These are here simply to assist in empirically verifying the conclusions reached in this article.

// Command maximum length. Below is 510 a's. Add/remove a's or spaces as necessary. Note that the in-game console is clamped to a maximum of 255 characters, and therefore testing of *this* limitation should be done using `exec` and a .cfg file.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

// Command maximum argument count.
// This command will generate a console warning, but will nonetheless function. Adding ' 65' will be ineffective, and removing ' 64' will remove the warning. Note that the first argument 'echo' is counted.
echo 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64