From: TimePath Date: Fri, 21 Aug 2015 07:59:28 +0000 (+1000) Subject: CSQC_ConsoleCommand event hook X-Git-Tag: xonotic-v0.8.2~2061 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=ec1a7a95b1f7b7431066038b0b07c1214aadaff3;p=xonotic%2Fxonotic-data.pk3dir.git CSQC_ConsoleCommand event hook --- diff --git a/qcsrc/client/command/cl_cmd.qc b/qcsrc/client/command/cl_cmd.qc index ed8cb190d..a79a83661 100644 --- a/qcsrc/client/command/cl_cmd.qc +++ b/qcsrc/client/command/cl_cmd.qc @@ -14,6 +14,8 @@ #include "../mapvoting.qh" #include "../miscfunctions.qh" +#include "../mutators/events.qh" + #include "../../common/mapinfo.qh" #include "../../common/command/generic.qh" @@ -605,14 +607,12 @@ void ConsoleCommand_macro_init() #ifndef CAMERATEST } #endif - - return; } -bool ConsoleCommand_macro_normal(int argc) +bool ConsoleCommand_macro_normal(string s, int argc) { #define CONSOLE_COMMAND(name,execution) \ - { if(name == strtolower(argv(0))) { { execution } return true; } } + { if (name == s) { { execution } return true; } } CONSOLE_COMMANDS_NORMAL(); #undef CONSOLE_COMMAND @@ -620,12 +620,12 @@ bool ConsoleCommand_macro_normal(int argc) return false; } -bool ConsoleCommand_macro_movement(int argc) +bool ConsoleCommand_macro_movement(string s, int argc) { if(camera_active) { #define CONSOLE_COMMAND(name,execution) \ - { if(name == strtolower(argv(0))) { { execution } return true; } } + { if (name == s) { { execution } return true; } } CONSOLE_COMMANDS_MOVEMENT(); #undef CONSOLE_COMMAND @@ -643,17 +643,10 @@ bool ConsoleCommand_macro_movement(int argc) bool CSQC_ConsoleCommand(string command) { int argc = tokenize_console(command); - - if(ConsoleCommand_macro_normal(argc)) - { - return true; - } - else if(ConsoleCommand_macro_movement(argc)) - { - return true; - } - - // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it. - - return false; + string s = strtolower(argv(0)); + // Return value should be true if CSQC handled the command, otherwise return false to have the engine handle it. + return (ConsoleCommand_macro_normal(s, argc) + || ConsoleCommand_macro_movement(s, argc) + || MUTATOR_CALLHOOK(CSQC_ConsoleCommand, s, argc, command) + ); } diff --git a/qcsrc/client/mutators/events.qh b/qcsrc/client/mutators/events.qh new file mode 100644 index 000000000..362e8d321 --- /dev/null +++ b/qcsrc/client/mutators/events.qh @@ -0,0 +1,38 @@ +#ifndef CLIENT_MUTATORS_EVENTS_H +#define CLIENT_MUTATORS_EVENTS_H + +#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); + +#endif diff --git a/qcsrc/server/mutators/events.qh b/qcsrc/server/mutators/events.qh index 52be590ba..ab652d1b4 100644 --- a/qcsrc/server/mutators/events.qh +++ b/qcsrc/server/mutators/events.qh @@ -290,8 +290,8 @@ MUTATOR_HOOKABLE(PlayerUseKey, EV_NO_ARGS); /** * called when a client command is parsed - * NOTE: hooks MUST start with if(MUTATOR_RETURNVALUE) return 0; - * NOTE: return 1 if you handled the command, return 0 to continue handling + * 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)