]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/command/generic.qc
Merge branch 'master' into Mario/stats_eloranking
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / generic.qc
index 5b53592831e8832154eea0220e4cace928af910d..dcd8d958422b660a941e639fbb23d6eb82de838a 100644 (file)
@@ -1,12 +1,14 @@
-#include "all.qh"
+#include "generic.qh"
+#include "_mod.qh"
+#include "reg.qh"
 
 #include "markup.qh"
 #include "rpn.qh"
 
 #include "../mapinfo.qh"
 
-#ifndef MENUQC
-       #include "../notifications.qh"
+#ifdef GAMEQC
+       #include "../notifications/all.qh"
 #endif
 
 #ifdef CSQC
 #endif
 
 #ifdef SVQC
-       #include <server/command/banning.qh>
-       #include <server/command/cmd.qh>
-       #include <server/command/common.qh>
-       #include <server/command/sv_cmd.qh>
+       #include <server/command/_mod.qh>
        #include <common/turrets/config.qh>
        #include <common/weapons/config.qh>
 #endif
 //  Last updated: February 19th, 2012
 // =========================================================
 
-// used by generic commands for better help/usage information
-string GetProgramCommandPrefix()
-{
-       #ifdef SVQC
-       return "sv_cmd";
-       #endif
-       #ifdef CSQC
-       return "cl_cmd";
-       #endif
-       #ifdef MENUQC
-       return "menu_cmd";
-       #endif
-}
 
 // used by curl command
 void Curl_URI_Get_Callback(int id, float status, string data)
@@ -49,7 +35,7 @@ void Curl_URI_Get_Callback(int id, float status, string data)
        string do_cvar = curl_uri_get_cvar[i];
        if(status != 0)
        {
-               LOG_TRACEF("error: status is %d\n", status);
+               LOG_TRACEF("error: status is %d", status);
                if(do_cvar)
                        strunzone(do_cvar);
                return;
@@ -71,7 +57,7 @@ void Curl_URI_Get_Callback(int id, float status, string data)
 //  Command Sub-Functions
 // =======================
 
-void GenericCommand_addtolist(float request, float argc)
+void GenericCommand_addtolist(int request, int argc)
 {
        switch(request)
        {
@@ -88,31 +74,30 @@ void GenericCommand_addtolist(float request, float argc)
                                }
                                else // add it to the end of the list if the list doesn't already have it
                                {
-                                       argc = tokenizebyseparator(cvar_string(original_cvar), " ");
-                                       int i;
-                                       for(i = 0; i < argc; ++i)
-                                               if(argv(i) == tmp_string)
-                                                       return; // already in list
+                                       FOREACH_WORD(cvar_string(original_cvar), it == tmp_string,
+                                       {
+                                               return; // already in the list
+                                       });
 
-                                       cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
+                                       cvar_set(original_cvar, cons(cvar_string(original_cvar), tmp_string));
                                }
                                return;
                        }
                }
 
                default:
-                       LOG_INFO("Incorrect parameters for ^2addtolist^7\n");
+                       LOG_INFO("Incorrect parameters for ^2addtolist^7");
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
-                       LOG_INFO("  Where 'variable' is what to add 'value' to.\n");
-                       LOG_INFO("See also: ^2removefromlist^7\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " addtolist variable value");
+                       LOG_INFO("  Where 'variable' is what to add 'value' to.");
+                       LOG_INFO("See also: ^2removefromlist^7");
                        return;
                }
        }
 }
 
-void GenericCommand_qc_curl(float request, float argc)
+void GenericCommand_qc_curl(int request, int argc)
 {
        switch(request)
        {
@@ -168,7 +153,7 @@ void GenericCommand_qc_curl(float request, float argc)
                                curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
                        }
                        else
-                               LOG_INFO(_("error creating curl handle\n"));
+                               LOG_INFO(_("error creating curl handle"));
 
                        buf_del(buf);
 
@@ -178,13 +163,13 @@ void GenericCommand_qc_curl(float request, float argc)
                default:
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]");
                        return;
                }
        }
 }
 
-GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to *_cmd_dump.txt")
+GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to <program>_cmd_dump.txt")
 {
        switch(request)
        {
@@ -199,32 +184,37 @@ GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to *_cmd_dump.tx
                                #ifdef SVQC
                                        CMD_Write("dump of server console commands:\n");
                                        GameCommand_macro_write_aliases(fh);
+                                       CMD_Write("\n");
 
-                                       CMD_Write("\ndump of networked client only commands:\n");
+                                       CMD_Write("dump of networked client only commands:\n");
                                        ClientCommand_macro_write_aliases(fh);
+                                       CMD_Write("\n");
 
-                                       CMD_Write("\ndump of common commands:\n");
+                                       CMD_Write("dump of common commands:\n");
                                        CommonCommand_macro_write_aliases(fh);
+                                       CMD_Write("\n");
 
-                                       CMD_Write("\ndump of ban commands:\n");
+                                       CMD_Write("dump of ban commands:\n");
                                        BanCommand_macro_write_aliases(fh);
+                                       CMD_Write("\n");
                                #endif
 
                                #ifdef CSQC
                                        CMD_Write("dump of client commands:\n");
                                        LocalCommand_macro_write_aliases(fh);
+                                       CMD_Write("\n");
                                #endif
 
-                               CMD_Write("\ndump of generic commands:\n");
+                               CMD_Write("dump of generic commands:\n");
                                GenericCommand_macro_write_aliases(fh);
 
-                               LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
+                               LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.");
 
                                fclose(fh);
                        }
                        else
                        {
-                               LOG_INFO("^1Error: ^7Could not dump to file!\n");
+                               LOG_INFO("^1Error: ^7Could not dump to file!");
                        }
                        return;
                }
@@ -232,14 +222,14 @@ GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to *_cmd_dump.tx
                default:
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
-                       LOG_INFO("  No arguments required.\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " dumpcommands");
+                       LOG_INFO("  No arguments required.");
                        return;
                }
        }
 }
 
-void GenericCommand_maplist(float request, float argc)
+void GenericCommand_maplist(int request, int argc)
 {
        switch(request)
        {
@@ -256,7 +246,7 @@ void GenericCommand_maplist(float request, float argc)
                                        {
                                                if (!fexists(strcat("maps/", argv(2), ".bsp")))
                                                {
-                                                       LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!\n");
+                                                       LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!");
                                                        break;
                                                }
 
@@ -310,19 +300,19 @@ void GenericCommand_maplist(float request, float argc)
                }
 
                default:
-                       LOG_INFO("Incorrect parameters for ^2maplist^7\n");
+                       LOG_INFO("Incorrect parameters for ^2maplist^7");
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
-                       LOG_INFO("  Where 'action' is the command to complete,\n");
-                       LOG_INFO("  and 'map' is what it acts upon (if required).\n");
-                       LOG_INFO("  Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " maplist action [map]");
+                       LOG_INFO("  Where 'action' is the command to complete,");
+                       LOG_INFO("  and 'map' is what it acts upon (if required).");
+                       LOG_INFO("  Full list of commands here: \"add, cleanup, remove, shuffle.\"");
                        return;
                }
        }
 }
 
-void GenericCommand_nextframe(float request, float arguments, string command)
+void GenericCommand_nextframe(int request, string command)
 {
        switch(request)
        {
@@ -335,14 +325,14 @@ void GenericCommand_nextframe(float request, float arguments, string command)
                default:
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
-                       LOG_INFO("  Where command will be executed next frame of this VM\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " nextframe command...");
+                       LOG_INFO("  Where command will be executed next frame of this VM");
                        return;
                }
        }
 }
 
-void GenericCommand_removefromlist(float request, float argc)
+void GenericCommand_removefromlist(int request, int argc)
 {
        switch(request)
        {
@@ -350,19 +340,15 @@ void GenericCommand_removefromlist(float request, float argc)
                {
                        if(argc == 3)
                        {
-                               float i;
                                string original_cvar = argv(1);
                                string removal = argv(2);
-                               string tmp_string;
-
-                               argc = tokenizebyseparator(cvar_string(original_cvar), " ");
 
-                               tmp_string = "";
-                               for(i = 0; i < argc; ++i)
-                                       if(argv(i) != removal)
-                                               tmp_string = strcat(tmp_string, " ", argv(i));
+                               string tmp_string = "";
+                               FOREACH_WORD(cvar_string(original_cvar), it != removal,
+                               {
+                                       tmp_string = cons(tmp_string, it);
+                               });
 
-                               tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
                                cvar_set(original_cvar, tmp_string);
 
                                return;
@@ -370,29 +356,43 @@ void GenericCommand_removefromlist(float request, float argc)
                }
 
                default:
-                       LOG_INFO("Incorrect parameters for ^2removefromlist^7\n");
+                       LOG_INFO("Incorrect parameters for ^2removefromlist^7");
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
-                       LOG_INFO("  Where 'variable' is what cvar to remove 'value' from.\n");
-                       LOG_INFO("See also: ^2addtolist^7\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value");
+                       LOG_INFO("  Where 'variable' is what cvar to remove 'value' from.");
+                       LOG_INFO("See also: ^2addtolist^7");
                        return;
                }
        }
 }
 
-void GenericCommand_restartnotifs(float request)
+void GenericCommand_restartnotifs(int request)
 {
        switch(request)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       #ifndef MENUQC
+                       #ifdef GAMEQC
+                       int NOTIF_ANNCE_COUNT = 0;
+                       int NOTIF_INFO_COUNT = 0;
+                       int NOTIF_CENTER_COUNT = 0;
+                       int NOTIF_MULTI_COUNT = 0;
+                       int NOTIF_CHOICE_COUNT = 0;
+                       FOREACH(Notifications, true, {
+                               switch (it.nent_type)
+                               {
+                                       case MSG_ANNCE: ++NOTIF_ANNCE_COUNT; break;
+                                       case MSG_INFO: ++NOTIF_INFO_COUNT; break;
+                                       case MSG_CENTER: ++NOTIF_CENTER_COUNT; break;
+                                       case MSG_MULTI: ++NOTIF_MULTI_COUNT; break;
+                                       case MSG_CHOICE: ++NOTIF_CHOICE_COUNT; break;
+                               }
+                       });
+
                        LOG_INFOF(
-                               strcat(
-                                       "Restart_Notifications(): Restarting %d notifications... ",
-                                       "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
-                               ),
+                               "Restart_Notifications(): Restarting %d notifications... "
+                               "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d",
                                (
                                        NOTIF_ANNCE_COUNT +
                                        NOTIF_INFO_COUNT +
@@ -409,7 +409,7 @@ void GenericCommand_restartnotifs(float request)
                        Destroy_All_Notifications();
                        CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
                        #else
-                       LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
+                       LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd."));
                        #endif
                        return;
                }
@@ -417,14 +417,14 @@ void GenericCommand_restartnotifs(float request)
                default:
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
-                       LOG_INFO("  No arguments required.\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " restartnotifs");
+                       LOG_INFO("  No arguments required.");
                        return;
                }
        }
 }
 
-void GenericCommand_settemp(float request, float argc)
+void GenericCommand_settemp(int request, int argc)
 {
        switch(request)
        {
@@ -434,9 +434,9 @@ void GenericCommand_settemp(float request, float argc)
                        {
                                float f = cvar_settemp(argv(1), argv(2));
                                if(f == 1)
-                                       LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
+                                       LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.");
                                else if(f == -1)
-                                       LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
+                                       LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".");
                                // else cvar_settemp itself errors out
 
                                return;
@@ -444,18 +444,18 @@ void GenericCommand_settemp(float request, float argc)
                }
 
                default:
-                       LOG_INFO("Incorrect parameters for ^2settemp^7\n");
+                       LOG_INFO("Incorrect parameters for ^2settemp^7");
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
-                       LOG_INFO("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
-                       LOG_INFO("See also: ^2settemp_restore^7\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"");
+                       LOG_INFO("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.");
+                       LOG_INFO("See also: ^2settemp_restore^7");
                        return;
                }
        }
 }
 
-void GenericCommand_settemp_restore(float request, float argc)
+void GenericCommand_settemp_restore(int request)
 {
        switch(request)
        {
@@ -464,9 +464,9 @@ void GenericCommand_settemp_restore(float request, float argc)
                        float i = cvar_settemp_restore();
 
                        if(i)
-                               LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
+                               LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.");
                        else
-                               LOG_TRACE("Nothing to restore.\n");
+                               LOG_TRACE("Nothing to restore.");
 
                        return;
                }
@@ -474,15 +474,15 @@ void GenericCommand_settemp_restore(float request, float argc)
                default:
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
-                       LOG_INFO("  No arguments required.\n");
-                       LOG_INFO("See also: ^2settemp^7\n");
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " settemp_restore");
+                       LOG_INFO("  No arguments required.");
+                       LOG_INFO("See also: ^2settemp^7");
                        return;
                }
        }
 }
 
-void GenericCommand_runtest(float request, float argc)
+void GenericCommand_runtest(int request, int argc)
 {
        switch(request)
        {
@@ -502,7 +502,7 @@ void GenericCommand_runtest(float request, float argc)
                default:
                case CMD_REQUEST_USAGE:
                {
-                       LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
+                       LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " [function to run]");
                        return;
                }
        }
@@ -510,7 +510,7 @@ void GenericCommand_runtest(float request, float argc)
 
 /* use this when creating a new command, making sure to place it in alphabetical order... also,
 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
-void GenericCommand_(float request)
+void GenericCommand_(int request)
 {
        switch(request)
        {
@@ -534,43 +534,43 @@ void GenericCommand_(float request)
 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
 GENERIC_COMMAND(addtolist, "Add a string to a cvar") { GenericCommand_addtolist(request, arguments); }
 GENERIC_COMMAND(maplist, "Automatic control of maplist") { GenericCommand_maplist(request, arguments); }
-GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM") { GenericCommand_nextframe(request, arguments, command); }
+GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM") { GenericCommand_nextframe(request, command); }
 GENERIC_COMMAND(qc_curl, "Queries a URL") { GenericCommand_qc_curl(request, arguments); }
 GENERIC_COMMAND(removefromlist, "Remove a string from a cvar") { GenericCommand_removefromlist(request, arguments); }
 GENERIC_COMMAND(restartnotifs, "Re-initialize all notifications") { GenericCommand_restartnotifs(request); }
 GENERIC_COMMAND(rpn, "RPN calculator") { GenericCommand_rpn(request, arguments, command); }
 GENERIC_COMMAND(settemp, "Temporarily set a value to a cvar which is restored later") { GenericCommand_settemp(request, arguments); }
-GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command") { GenericCommand_settemp_restore(request, arguments); }
+GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command") { GenericCommand_settemp_restore(request); }
 GENERIC_COMMAND(runtest, "Run unit tests") { GenericCommand_runtest(request, arguments); }
 
 void GenericCommand_macro_help()
 {
-       FOREACH(GENERIC_COMMANDS, true, LAMBDA(LOG_INFOF("  ^2%s^7: %s\n", it.m_name, it.m_description)));
+       FOREACH(GENERIC_COMMANDS, true, LOG_INFOF("  ^2%s^7: %s", it.m_name, it.m_description));
 }
 
-float GenericCommand_macro_command(float argc, string command)
+float GenericCommand_macro_command(int argc, string command)
 {
        string c = strtolower(argv(0));
-       FOREACH(GENERIC_COMMANDS, it.m_name == c, LAMBDA(
-               it.m_invokecmd(CMD_REQUEST_COMMAND, NULL, argc, command);
+       FOREACH(GENERIC_COMMANDS, it.m_name == c, {
+               it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
                return true;
-       ));
+       });
        return false;
 }
 
-float GenericCommand_macro_usage(float argc)
+float GenericCommand_macro_usage(int argc)
 {
        string c = strtolower(argv(1));
-       FOREACH(GENERIC_COMMANDS, it.m_name == c, LAMBDA(
-               it.m_invokecmd(CMD_REQUEST_USAGE, NULL, argc, "");
+       FOREACH(GENERIC_COMMANDS, it.m_name == c, {
+               it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
                return true;
-       ));
+       });
        return false;
 }
 
 void GenericCommand_macro_write_aliases(float fh)
 {
-       FOREACH(GENERIC_COMMANDS, true, LAMBDA(CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description)));
+       FOREACH(GENERIC_COMMANDS, true, CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description));
 }
 
 
@@ -581,7 +581,7 @@ void GenericCommand_macro_write_aliases(float fh)
 
 float GenericCommand(string command)
 {
-       float argc = tokenize_console(command);
+       int argc = tokenize_console(command);
        float n, j, f, i;
        string s, s2, c;
        vector rgb;