]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Add a mutator hook for menu commands
authorMario <mario@smbclan.net>
Wed, 4 Nov 2015 08:06:36 +0000 (18:06 +1000)
committerMario <mario@smbclan.net>
Wed, 4 Nov 2015 08:06:36 +0000 (18:06 +1000)
qcsrc/menu/command/menu_cmd.qc
qcsrc/menu/mutators/events.qh [new file with mode: 0644]

index b3b6e020cfca0b1e19bd072f6dfca57599ccf727..6293241f12abc5586bb133c15df4e098d22b2d94 100644 (file)
@@ -3,6 +3,8 @@
 #include "../menu.qh"
 #include "../oo/classes.qc"
 
+#include "../mutators/events.qh"
+
 #include "../../common/command/generic.qh"
 
 .entity firstChild, nextSibling;
@@ -38,8 +40,8 @@ float updateConwidths(float width, float height, float pixelheight);
 
 void GameCommand(string theCommand)
 {
-       float argc;
-       argc = tokenize_console(theCommand);
+       int argc = tokenize_console(theCommand);
+       string ss = strtolower(argv(0));
 
        if (argv(0) == "help" || argc == 0)
        {
@@ -126,5 +128,8 @@ void GameCommand(string theCommand)
                return;
        }
 
+       if(MUTATOR_CALLHOOK(Menu_ConsoleCommand, ss, argc, theCommand)) // handled by a mutator
+               return;
+
        LOG_INFO(_("Invalid command. For a list of supported commands, try menu_cmd help.\n"));
 }
diff --git a/qcsrc/menu/mutators/events.qh b/qcsrc/menu/mutators/events.qh
new file mode 100644 (file)
index 0000000..a3dc720
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef MENU_MUTATORS_EVENTS_H
+#define MENU_MUTATORS_EVENTS_H
+
+#include "../../common/mutators/base.qh"
+
+// globals
+
+string cmd_name;
+int cmd_argc;
+string cmd_string;
+
+/**
+ * Called when a menu 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, Menu_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_Menu_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(Menu_ConsoleCommand, EV_Menu_ConsoleCommand);
+#endif