Skip to content

Optimize command handling and add support for command aliases - #5277

Open
MohabCodeX wants to merge 2 commits into
multitheftauto:masterfrom
MohabCodeX:feat/command-aliases
Open

Optimize command handling and add support for command aliases#5277
MohabCodeX wants to merge 2 commits into
multitheftauto:masterfrom
MohabCodeX:feat/command-aliases

Conversation

@MohabCodeX

Copy link
Copy Markdown
Contributor

This PR is split into two commits / parts:

Part 1: Command Handling Refactor & Performance Optimization

  • Eliminated dynamic heap allocations (new/delete) on every command execution by using a fast stack-allocated buffer on both client and server, removing unnecessary heap churn.
  • Optimized client command removal from $O(N^2)$ to $O(N)$ by eliminating repeated list iteration restarts when deleting matching commands.
  • Modernized command bindings to use ArgumentParser and cleaned up Hungarian notation across the command manager.

Part 2: Command Aliases Support

Scripters often struggle when trying to assign or remove multiple aliases for the same command in a single line. For example, they currently have to write:

addCommandHandler("pm", sendPrivateMessage)
addCommandHandler("msg", sendPrivateMessage)
addCommandHandler("tell", sendPrivateMessage)
addCommandHandler("whisper", sendPrivateMessage)
addCommandHandler("w", sendPrivateMessage)

Now, they can simply do:

addCommandHandler({"pm", "msg", "tell", "whisper", "w"}, sendPrivateMessage)

And similarly for removing them:

removeCommandHandler({"pm", "msg", "tell", "whisper", "w"}, sendPrivateMessage)

So... why not? It's a very useful QoL feature. While workarounds exist using Lua wrapper loops, having native core support is much cleaner, faster, and standardizes the workflow.

Everything is backward compatible with single string commands.


API Changes

addCommandHandler

bool addCommandHandler ( string / table commandNames, function handlerFunction [, bool restricted = false, bool caseSensitive = true ] ) -- Server
bool addCommandHandler ( string / table commandNames, function handlerFunction [, bool caseSensitive = true ] ) -- Client

removeCommandHandler

bool removeCommandHandler ( string / table commandNames [, function handlerFunction ] )

Testing

An automated test resource test_command_aliases.zip was created to verify all changes:

  • Runs test automatically on start and prints results directly to the chatbox on both client and server.
  • Multi-command aliases are included for manual in-game testing:
  • Server: /sv_test, /sv_alias, /sv_hello
  • Client: /cl_test, /cl_alias, /cl_hello

@FileEX FileEX added enhancement New feature or request refactor labels Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants