]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cmd.c
cvar: use better method of checking flags during registration
[xonotic/darkplaces.git] / cmd.c
diff --git a/cmd.c b/cmd.c
index 1f2609569ef42e31dc189f03afa6512b6629dc5d..b1294c3c11f3f48882eda24881464d41427171b8 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -1593,7 +1593,7 @@ static void Cmd_Apropos_f(cmd_state_t *cmd)
        Con_Printf("%i result%s\n\n", count, (count > 1) ? "s" : "");
 }
 
-static cmd_state_t *Cmd_AddInterpreter(cmd_buf_t *cbuf, cvar_state_t *cvars, int cvars_flagsmask, int cmds_flagsmask, cmd_userdefined_t *userdefined)
+static cmd_state_t *Cmd_AddInterpreter(cmd_buf_t *cbuf, cvar_state_t *cvars, unsigned cvars_flagsmask, unsigned cmds_flagsmask, cmd_userdefined_t *userdefined)
 {
        cmd_state_t *cmd = (cmd_state_t *)Mem_Alloc(tempmempool, sizeof(cmd_state_t));
        
@@ -1604,7 +1604,7 @@ static cmd_state_t *Cmd_AddInterpreter(cmd_buf_t *cbuf, cvar_state_t *cvars, int
 
        cmd->cvars = cvars;
        cmd->cvars_flagsmask = cvars_flagsmask;
-       cmd->cmd_flags = cmds_flagsmask;
+       cmd->cmd_flagsmask = cmds_flagsmask;
        cmd->userdefined = userdefined;
 
        return cmd;
@@ -1618,6 +1618,8 @@ Cmd_Init
 void Cmd_Init(void)
 {
        cmd_buf_t *cbuf;
+       unsigned cvars_flagsmask, cmds_flagsmask;
+
        cbuf_mempool = Mem_AllocPool("Command buffer", 0, NULL);
        cbuf = (cmd_buf_t *)Mem_Alloc(cbuf_mempool, sizeof(cmd_buf_t));
        cbuf->maxsize = CMDBUFSIZE;
@@ -1633,7 +1635,17 @@ void Cmd_Init(void)
        cmd_iter_all = (cmd_iter_t *)Mem_Alloc(tempmempool, sizeof(cmd_iter_t) * 3);
 
        // local console
-       cmd_iter_all[0].cmd = cmd_local = Cmd_AddInterpreter(cbuf, &cvars_all, CF_CLIENT | CF_SERVER, CF_CLIENT | CF_CLIENT_FROM_SERVER | CF_SERVER_FROM_CLIENT, &cmd_userdefined_all);
+       if (cls.state == ca_dedicated)
+       {
+               cvars_flagsmask = CF_SERVER;
+               cmds_flagsmask = CF_SERVER | CF_SERVER_FROM_CLIENT;
+       }
+       else
+       {
+               cvars_flagsmask = CF_CLIENT | CF_SERVER;
+               cmds_flagsmask = CF_CLIENT | CF_SERVER | CF_CLIENT_FROM_SERVER | CF_SERVER_FROM_CLIENT;
+       }
+       cmd_iter_all[0].cmd = cmd_local = Cmd_AddInterpreter(cbuf, &cvars_all, cvars_flagsmask, cmds_flagsmask, &cmd_userdefined_all);
        cmd_local->Handle = Cmd_CL_Callback;
 
        // server commands received from clients have no reason to access cvars, cvar expansion seems perilous.
@@ -1773,7 +1785,7 @@ static void Cmd_TokenizeString (cmd_state_t *cmd, const char *text)
 Cmd_AddCommand
 ============
 */
-void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const char *description)
+void Cmd_AddCommand(unsigned flags, const char *cmd_name, xcommand_t function, const char *description)
 {
        cmd_function_t *func;
        cmd_function_t *prev, *current;
@@ -1783,7 +1795,7 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const
        for (i = 0; i < 2; i++)
        {
                cmd = cmd_iter_all[i].cmd;
-               if (flags & cmd->cmd_flags)
+               if (flags & cmd->cmd_flagsmask)
                {
                        // fail if the command is a variable name
                        if (Cvar_FindVar(cmd->cvars, cmd_name, ~0))
@@ -2201,21 +2213,25 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb
        for (func = cmd->userdefined->qc_functions; func; func = func->next)
                if (!strcasecmp(cmd->argv[0], func->name))
                        if(cmd->Handle(cmd, func, text, src))
-                               goto done;
+                               goto functions_done;
 
        for (func = cmd->engine_functions; func; func=func->next)
                if (!strcasecmp (cmd->argv[0], func->name))
                        if(cmd->Handle(cmd, func, text, src))
-                               goto done;
+                               goto functions_done;
 
-       // if it's a client command and no command was found, say so.
+functions_done:
+       // If it's a client command and wasn't found and handled, say so.
+       // Also don't let clients call server aliases.
        if (cmd->source == src_client)
        {
-               Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
+               if (!func)
+                       Con_Printf("Client \"%s\" tried to execute \"%s\"\n", host_client->name, text);
                goto done;
        }
 
 // check alias
+       // Execute any alias with the same name as a command after the command.
        for (a=cmd->userdefined->alias ; a ; a=a->next)
        {
                if (!strcasecmp (cmd->argv[0], a->name))
@@ -2225,6 +2241,10 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb
                }
        }
 
+       // If the command was found and handled don't try to handle it as a cvar.
+       if (func)
+               goto done;
+
 // check cvars
        if (!Cvar_Command(cmd) && host.framecount > 0)
                Con_Printf(CON_WARN "Unknown command \"%s\"\n", Cmd_Argv(cmd, 0));