From ab93f3de91b08f841d440f86d8b4a4d48ad00292 Mon Sep 17 00:00:00 2001 From: cloudwalk Date: Wed, 17 Jun 2020 01:30:38 +0000 Subject: [PATCH] Iterate through interpreters and compare with stored bitmasks to add commands This is much, much simpler. This also fixes an issue where quite a few of the userinfo commands couldn't be used while disconnected. git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12686 d7cf8633-e32d-0410-b094-e92efae38249 --- cmd.c | 138 +++++++++++++++++++++++++++------------------------------- cmd.h | 8 ++-- 2 files changed, 70 insertions(+), 76 deletions(-) diff --git a/cmd.c b/cmd.c index 348825eb..ec638b19 100644 --- a/cmd.c +++ b/cmd.c @@ -1512,14 +1512,17 @@ void Cmd_Init(void) // client console can see server cvars because the user may start a server cmd_client.cvars = &cvars_all; cmd_client.cvars_flagsmask = CVAR_CLIENT | CVAR_SERVER; + cmd_client.cmd_flags = CMD_CLIENT | CMD_CLIENT_FROM_SERVER; cmd_client.userdefined = &cmd_userdefined_all; // dedicated server console can only see server cvars, there is no client cmd_server.cvars = &cvars_all; cmd_server.cvars_flagsmask = CVAR_SERVER; + cmd_server.cmd_flags = CMD_SERVER; cmd_server.userdefined = &cmd_userdefined_all; // server commands received from clients have no reason to access cvars, cvar expansion seems perilous. cmd_serverfromclient.cvars = &cvars_null; cmd_serverfromclient.cvars_flagsmask = 0; + cmd_serverfromclient.cmd_flags = CMD_SERVER_FROM_CLIENT; cmd_serverfromclient.userdefined = &cmd_userdefined_null; } @@ -1691,98 +1694,87 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const cmd_function_t *func; cmd_function_t *prev, *current; cmd_state_t *cmd; - xcommand_t function_actual; int i; - for (i = 1; i < (1<<8); i *= 2) + for (i = 0; i < 3; i++) { - function_actual = function; - if ((i == CMD_CLIENT) && (flags & i)) + cmd = cmd_iter_all[i].cmd; + if (flags & cmd->cmd_flags) { - cmd = &cmd_client; - if (flags & 8) - function_actual = Cmd_ForwardToServer_f; - } - else if ((i == CMD_SERVER) && (flags & i)) - cmd = &cmd_server; - else if ((i == 8) && (flags & i)) // CMD_SERVER_FROM_CLIENT - cmd = &cmd_serverfromclient; - else - continue; - - // fail if the command is a variable name - if (Cvar_FindVar(cmd->cvars, cmd_name, ~0)) - { - Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name); - return; - } + // fail if the command is a variable name + if (Cvar_FindVar(cmd->cvars, cmd_name, ~0)) + { + Con_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name); + return; + } - if (function_actual) - { - // fail if the command already exists in this interpreter - for (func = cmd->engine_functions; func; func = func->next) + if (function) { - if (!strcmp(cmd_name, func->name)) + // fail if the command already exists in this interpreter + for (func = cmd->engine_functions; func; func = func->next) { - // Allow overriding forward to server - if(func->function == Cmd_ForwardToServer_f && (func->flags & 8)) - break; - else + if (!strcmp(cmd_name, func->name)) { - Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name); - goto nested_continue; + // Allow overriding forward to server + if(func->function == Cmd_ForwardToServer_f && (func->flags & 8)) + break; + else + { + Con_Printf("Cmd_AddCommand: %s already defined\n", cmd_name); + goto nested_continue; + } } } - } - func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t)); - func->flags = flags; - func->name = cmd_name; - func->function = function_actual; - func->description = description; - func->next = cmd->engine_functions; - - // insert it at the right alphanumeric position - for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next) - ; - if (prev) { - prev->next = func; - } - else { - cmd->engine_functions = func; + func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t)); + func->flags = flags; + func->name = cmd_name; + func->function = function; + func->description = description; + func->next = cmd->engine_functions; + + // insert it at the right alphanumeric position + for (prev = NULL, current = cmd->engine_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next) + ; + if (prev) { + prev->next = func; + } + else { + cmd->engine_functions = func; + } + func->next = current; } - func->next = current; - } - else - { - // mark csqcfunc if the function already exists in the csqc_functions list - for (func = cmd->userdefined->csqc_functions; func; func = func->next) + else { - if (!strcmp(cmd_name, func->name)) + // mark csqcfunc if the function already exists in the csqc_functions list + for (func = cmd->userdefined->csqc_functions; func; func = func->next) { - func->csqcfunc = true; //[515]: csqc - continue; + if (!strcmp(cmd_name, func->name)) + { + func->csqcfunc = true; //[515]: csqc + continue; + } } - } - func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t)); - func->name = cmd_name; - func->function = function_actual; - func->description = description; - func->csqcfunc = true; //[515]: csqc - func->next = cmd->userdefined->csqc_functions; + func = (cmd_function_t *)Mem_Alloc(cmd->mempool, sizeof(cmd_function_t)); + func->name = cmd_name; + func->function = function; + func->description = description; + func->csqcfunc = true; //[515]: csqc + func->next = cmd->userdefined->csqc_functions; - // insert it at the right alphanumeric position - for (prev = NULL, current = cmd->userdefined->csqc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next) - ; - if (prev) { - prev->next = func; - } - else { - cmd->userdefined->csqc_functions = func; + // insert it at the right alphanumeric position + for (prev = NULL, current = cmd->userdefined->csqc_functions; current && strcmp(current->name, func->name) < 0; prev = current, current = current->next) + ; + if (prev) { + prev->next = func; + } + else { + cmd->userdefined->csqc_functions = func; + } + func->next = current; } - func->next = current; } nested_continue: continue; diff --git a/cmd.h b/cmd.h index 9b0046b0..8badf436 100644 --- a/cmd.h +++ b/cmd.h @@ -45,9 +45,9 @@ struct cmd_state_s; #define CMD_CLIENT (1<<0) #define CMD_SERVER (1<<1) #define CMD_CLIENT_FROM_SERVER (1<<2) -#define CMD_SERVER_FROM_CLIENT 9 -#define CMD_INITWAIT (1<<5) -#define CMD_CHEAT (1<<6) +#define CMD_SERVER_FROM_CLIENT (1<<3) +#define CMD_INITWAIT (1<<4) +#define CMD_CHEAT (1<<5) #define CMD_SHARED 3 @@ -128,6 +128,8 @@ typedef struct cmd_state_s cvar_state_t *cvars; // which cvar system is this cmd state able to access? (&cvars_all or &cvars_null) int cvars_flagsmask; // which CVAR_* flags should be visible to this interpreter? (CVAR_CLIENT | CVAR_SERVER, or just CVAR_SERVER) + + int cmd_flags; // cmd flags that identify this interpreter } cmd_state_t; -- 2.39.2