]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
CSQC_ConsoleCommand event hook
authorTimePath <andrew.hardaker1995@gmail.com>
Fri, 21 Aug 2015 07:59:28 +0000 (17:59 +1000)
committerTimePath <andrew.hardaker1995@gmail.com>
Fri, 21 Aug 2015 07:59:28 +0000 (17:59 +1000)
qcsrc/client/command/cl_cmd.qc
qcsrc/client/mutators/events.qh [new file with mode: 0644]
qcsrc/server/mutators/events.qh

index ed8cb190d26ee803a68df32662ff62f3a9e14b2a..a79a836616449458d32f1b58e871d0a14e0ae733 100644 (file)
@@ -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 (file)
index 0000000..362e8d3
--- /dev/null
@@ -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
index 52be590ba5972b8e8eb8b4139bbbe97b628f84a0..ab652d1b4f661df4e69acec055a40a2be7296fab 100644 (file)
@@ -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)