From 3354bf7bbd8f5cec782db4d3757ce13cb7ce6a0d Mon Sep 17 00:00:00 2001 From: TimePath Date: Wed, 26 Aug 2015 08:54:10 +1000 Subject: [PATCH] Allow mutators to define menu commands --- qcsrc/client/command/cl_cmd.qc | 2 +- qcsrc/client/mutators/events.qh | 32 ------------------------------- qcsrc/common/mutators/events.qh | 30 +++++++++++++++++++++++++++++ qcsrc/menu/command/menu_cmd.qc | 10 ++++++---- qcsrc/menu/progs.src | 1 + qcsrc/server/mutators/events.qh | 34 ++------------------------------- 6 files changed, 40 insertions(+), 69 deletions(-) diff --git a/qcsrc/client/command/cl_cmd.qc b/qcsrc/client/command/cl_cmd.qc index 9fe33a4fc..d153dd52b 100644 --- a/qcsrc/client/command/cl_cmd.qc +++ b/qcsrc/client/command/cl_cmd.qc @@ -555,7 +555,7 @@ void GameCommand(string command) // continue as usual and scan for normal commands if (GenericCommand(command)// handled by common/command/generic.qc || LocalCommand_macro_command(argc) // handled by one of the above LocalCommand_* functions - || MUTATOR_CALLHOOK(CSQC_ConsoleCommand, s, argc, command) // handled by a mutator + || MUTATOR_CALLHOOK(GameCommand, s, argc, command) // handled by a mutator ) return; // nothing above caught the command, must be invalid diff --git a/qcsrc/client/mutators/events.qh b/qcsrc/client/mutators/events.qh index 16ae922f2..026f4bd74 100644 --- a/qcsrc/client/mutators/events.qh +++ b/qcsrc/client/mutators/events.qh @@ -3,38 +3,6 @@ #include "../../common/mutators/base.qh" -// globals - -string cmd_name; -int cmd_argc; -string cmd_string; - -/** - * Called when a client command is parsed - * NOTE: hooks MUST start with if (MUTATOR_RETURNVALUE) return false; - * NOTE: return true if you handled the command, return false to continue handling - * NOTE: THESE HOOKS MUST NEVER EVER CALL tokenize() - * // example: - * MUTATOR_HOOKFUNCTION(foo, CSQC_ConsoleCommand) { - * if (MUTATOR_RETURNVALUE) return false; // command was already handled - * if (cmd_name == "echocvar" && cmd_argc >= 2) { - * print(cvar_string(argv(1)), "\n"); - * return true; - * } - * if (cmd_name == "echostring" && cmd_argc >= 2) { - * print(substring(cmd_string, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), "\n"); - * return true; - * } - * return false; - * } - */ -#define EV_CSQC_ConsoleCommand(i, o) \ - /** command name */ i(string, cmd_name) \ - /** also, argv() can be used */ i(int, cmd_argc) \ - /** whole command, use only if you really have to */ i(string, cmd_string) \ - /**/ -MUTATOR_HOOKABLE(CSQC_ConsoleCommand, EV_CSQC_ConsoleCommand); - /* Called when the crosshair is being updated */ MUTATOR_HOOKABLE(UpdateCrosshair, EV_NO_ARGS); diff --git a/qcsrc/common/mutators/events.qh b/qcsrc/common/mutators/events.qh index eb46486b6..12984a924 100644 --- a/qcsrc/common/mutators/events.qh +++ b/qcsrc/common/mutators/events.qh @@ -39,4 +39,34 @@ MUTATOR_HOOKABLE(BuildMutatorsString, EV_BuildMutatorsString); /**/ MUTATOR_HOOKABLE(BuildMutatorsPrettyString, EV_BuildMutatorsPrettyString); +string cmd_name; +int cmd_argc; +string cmd_string; + +/** + * Called when a game command is parsed + * NOTE: hooks MUST start with if (MUTATOR_RETURNVALUE) return false; + * NOTE: return true if you handled the command, return false to continue handling + * NOTE: THESE HOOKS MUST NEVER EVER CALL tokenize() + * // example: + * MUTATOR_HOOKFUNCTION(foo, GameCommand) { + * if (MUTATOR_RETURNVALUE) return false; // command was already handled + * if (cmd_name == "echocvar" && cmd_argc >= 2) { + * print(cvar_string(argv(1)), "\n"); + * return true; + * } + * if (cmd_name == "echostring" && cmd_argc >= 2) { + * print(substring(cmd_string, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), "\n"); + * return true; + * } + * return false; + * } + */ +#define EV_GameCommand(i, o) \ + /** command name */ i(string, cmd_name) \ + /** also, argv() can be used */ i(int, cmd_argc) \ + /** whole command, use only if you really have to */ i(string, cmd_string) \ + /**/ +MUTATOR_HOOKABLE(GameCommand, EV_GameCommand); + #endif diff --git a/qcsrc/menu/command/menu_cmd.qc b/qcsrc/menu/command/menu_cmd.qc index 3f3f72f57..b03a9f6cc 100644 --- a/qcsrc/menu/command/menu_cmd.qc +++ b/qcsrc/menu/command/menu_cmd.qc @@ -36,10 +36,10 @@ void _dumptree_close(entity pass, entity me) float updateConwidths(float width, float height, float pixelheight); -void GameCommand(string theCommand) +void GameCommand(string command) { - float argc; - argc = tokenize_console(theCommand); + int argc = tokenize_console(command); + string s = strtolower(argv(0)); if(argv(0) == "help" || argc == 0) { @@ -53,7 +53,7 @@ void GameCommand(string theCommand) return; } - if(GenericCommand(theCommand)) + if(GenericCommand(command)) return; if(argv(0) == "sync") @@ -130,5 +130,7 @@ void GameCommand(string theCommand) return; } + if (MUTATOR_CALLHOOK(GameCommand, s, argc, command)) return; // handled by a mutator + print(_("Invalid command. For a list of supported commands, try menu_cmd help.\n")); } diff --git a/qcsrc/menu/progs.src b/qcsrc/menu/progs.src index 2bde451f8..d1d2a2262 100644 --- a/qcsrc/menu/progs.src +++ b/qcsrc/menu/progs.src @@ -24,6 +24,7 @@ xonotic/util.qc ../common/items/all.qc ../common/monsters/all.qc +../common/mutators/all.qc ../common/vehicles/all.qc ../common/weapons/all.qc diff --git a/qcsrc/server/mutators/events.qh b/qcsrc/server/mutators/events.qh index 71542d543..14de9f3ab 100644 --- a/qcsrc/server/mutators/events.qh +++ b/qcsrc/server/mutators/events.qh @@ -347,38 +347,8 @@ MUTATOR_HOOKABLE(PlayerRegen, EV_PlayerRegen); */ MUTATOR_HOOKABLE(PlayerUseKey, EV_NO_ARGS); -/** - * called when a client command is parsed - * NOTE: hooks MUST start with if (MUTATOR_RETURNVALUE) return false; - * NOTE: return true if you handled the command, return false to continue handling - * NOTE: THESE HOOKS MUST NEVER EVER CALL tokenize() - * // example: - * MUTATOR_HOOKFUNCTION(foo_SV_ParseClientCommand) - * { - * if (MUTATOR_RETURNVALUE) // command was already handled? - * return false; - * if (cmd_name == "echocvar" && cmd_argc >= 2) - * { - * print(cvar_string(argv(1)), "\n"); - * return true; - * } - * if (cmd_name == "echostring" && cmd_argc >= 2) - * { - * print(substring(cmd_string, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), "\n"); - * return true; - * } - * return false; - * } - */ -#define EV_SV_ParseClientCommand(i, o) \ - /** command name */ i(string, cmd_name) \ - /** also, argv() can be used */ i(int, cmd_argc) \ - /** whole command, use only if you really have to */ i(string, cmd_string) \ - /**/ -string cmd_name; -int cmd_argc; -string cmd_string; -MUTATOR_HOOKABLE(SV_ParseClientCommand, EV_SV_ParseClientCommand); +/** Called when a client command is parsed, same structure as GameCommand */ +MUTATOR_HOOKABLE(SV_ParseClientCommand, EV_GameCommand); /** * called when a spawnpoint is being evaluated -- 2.39.2