From 5a449a56ea292c2118783687bd4193a6d13c0c04 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Fri, 1 Jun 2018 19:57:54 +0300 Subject: [PATCH] Port restricted cmds and cvars from old engine. TODO: implement better stufftext filter --- engine/common/cmd.c | 162 ++++++++++++----------------------------- engine/common/crtlib.h | 2 + engine/common/cvar.h | 1 + 3 files changed, 48 insertions(+), 117 deletions(-) diff --git a/engine/common/cmd.c b/engine/common/cmd.c index eee29541..60cd99b1 100644 --- a/engine/common/cmd.c +++ b/engine/common/cmd.c @@ -603,32 +603,40 @@ void Cmd_TokenizeString( char *text ) /* ============ -Cmd_AddCommand +Cmd_AddCommandEx ============ */ -void Cmd_AddCommand( const char *cmd_name, xcommand_t function, const char *cmd_desc ) +static int Cmd_AddCommandEx( const char *funcname, const char *cmd_name, xcommand_t function, + const char *cmd_desc, int iFlags ) { cmd_t *cmd, *cur, *prev; + if( !cmd_name || !*cmd_name ) + { + MsgDev( D_ERROR, "Cmd_AddServerCommand: NULL name\n" ); + return 0; + } + // fail if the command is a variable name if( Cvar_FindVar( cmd_name )) { - Con_Printf( S_ERROR "Cmd_AddCommand: %s already defined as a var\n", cmd_name ); - return; + MsgDev( D_ERROR, "Cmd_AddServerCommand: %s already defined as a var\n", cmd_name ); + return 0; } - + // fail if the command already exists if( Cmd_Exists( cmd_name )) { - Con_Printf( S_ERROR "Cmd_AddCommand: %s already defined\n", cmd_name ); - return; + MsgDev( D_ERROR, "Cmd_AddServerCommand: %s already defined\n", cmd_name ); + return 0; } // use a small malloc to avoid zone fragmentation - cmd = Z_Malloc( sizeof( cmd_t )); + cmd = Z_Malloc( sizeof( cmd_t ) ); cmd->name = copystring( cmd_name ); cmd->desc = copystring( cmd_desc ); cmd->function = function; + cmd->flags = iFlags; // insert it at the right alphanumeric position for( prev = NULL, cur = cmd_functions; cur && Q_strcmp( cur->name, cmd_name ) < 0; prev = cur, cur = cur->next ); @@ -636,6 +644,32 @@ void Cmd_AddCommand( const char *cmd_name, xcommand_t function, const char *cmd_ if( prev ) prev->next = cmd; else cmd_functions = cmd; cmd->next = cur; + +#if defined(XASH_HASHED_VARS) + BaseCmd_Insert( HM_CMD, cmd, cmd->name ); +#endif + + return 1; +} + +/* +============ +Cmd_AddCommand +============ +*/ +void Cmd_AddCommand( const char *cmd_name, xcommand_t function, const char *cmd_desc ) +{ + Cmd_AddCommandEx( __FUNCTION__, cmd_name, function, cmd_desc, 0 ); +} + +/* +============ +Cmd_AddRestrictedCommand +============ +*/ +void Cmd_AddRestrictedCommand( const char *cmd_name, xcommand_t function, const char *cmd_desc ) +{ + Cmd_AddCommandEx( __FUNCTION__, cmd_name, function, cmd_desc, CMD_LOCALONLY ); } /* @@ -645,41 +679,7 @@ Cmd_AddServerCommand */ void Cmd_AddServerCommand( const char *cmd_name, xcommand_t function ) { - cmd_t *cmd, *cur, *prev; - - if( !cmd_name || !*cmd_name ) - { - MsgDev( D_ERROR, "Cmd_AddServerCommand: NULL name\n" ); - return; - } - - // fail if the command is a variable name - if( Cvar_FindVar( cmd_name )) - { - MsgDev( D_ERROR, "Cmd_AddServerCommand: %s already defined as a var\n", cmd_name ); - return; - } - - // fail if the command already exists - if( Cmd_Exists( cmd_name )) - { - MsgDev( D_ERROR, "Cmd_AddServerCommand: %s already defined\n", cmd_name ); - return; - } - - // use a small malloc to avoid zone fragmentation - cmd = Z_Malloc( sizeof( cmd_t )); - cmd->name = copystring( cmd_name ); - cmd->desc = copystring( "server command" ); - cmd->function = function; - cmd->flags = CMD_SERVERDLL; - - // insert it at the right alphanumeric position - for( prev = NULL, cur = cmd_functions; cur && Q_strcmp( cur->name, cmd_name ) < 0; prev = cur, cur = cur->next ); - - if( prev ) prev->next = cmd; - else cmd_functions = cmd; - cmd->next = cur; + Cmd_AddCommandEx( __FUNCTION__, cmd_name, function, "server command", CMD_SERVERDLL ); } /* @@ -689,43 +689,7 @@ Cmd_AddClientCommand */ int Cmd_AddClientCommand( const char *cmd_name, xcommand_t function ) { - cmd_t *cmd, *cur, *prev; - - if( !cmd_name || !*cmd_name ) - { - MsgDev( D_ERROR, "Cmd_AddClientCommand: NULL name\n" ); - return 0; - } - - // fail if the command is a variable name - if( Cvar_FindVar( cmd_name )) - { - MsgDev( D_ERROR, "Cmd_AddClientCommand: %s already defined as a var\n", cmd_name ); - return 0; - } - - // fail if the command already exists - if( Cmd_Exists( cmd_name )) - { - MsgDev( D_ERROR, "Cmd_AddClientCommand: %s already defined\n", cmd_name ); - return 0; - } - - // use a small malloc to avoid zone fragmentation - cmd = Z_Malloc( sizeof( cmd_t )); - cmd->name = copystring( cmd_name ); - cmd->desc = copystring( "client command" ); - cmd->function = function; - cmd->flags = CMD_CLIENTDLL; - - // insert it at the right alphanumeric position - for( prev = NULL, cur = cmd_functions; cur && Q_strcmp( cur->name, cmd_name ) < 0; prev = cur, cur = cur->next ); - - if( prev ) prev->next = cmd; - else cmd_functions = cmd; - cmd->next = cur; - - return 1; + return Cmd_AddCommandEx( __FUNCTION__, cmd_name, function, "server command", CMD_CLIENTDLL ); } /* @@ -735,43 +699,7 @@ Cmd_AddGameUICommand */ int Cmd_AddGameUICommand( const char *cmd_name, xcommand_t function ) { - cmd_t *cmd, *cur, *prev; - - if( !cmd_name || !*cmd_name ) - { - MsgDev( D_ERROR, "Cmd_AddGameUICommand: NULL name\n" ); - return 0; - } - - // fail if the command is a variable name - if( Cvar_FindVar( cmd_name )) - { - MsgDev( D_ERROR, "Cmd_AddGameUICommand: %s already defined as a var\n", cmd_name ); - return 0; - } - - // fail if the command already exists - if( Cmd_Exists( cmd_name )) - { - MsgDev( D_ERROR, "Cmd_AddGameUICommand: %s already defined\n", cmd_name ); - return 0; - } - - // use a small malloc to avoid zone fragmentation - cmd = Z_Malloc( sizeof( cmd_t )); - cmd->name = copystring( cmd_name ); - cmd->desc = copystring( "GameUI command" ); - cmd->function = function; - cmd->flags = CMD_GAMEUIDLL; - - // insert it at the right alphanumeric position - for( prev = NULL, cur = cmd_functions; cur && Q_strcmp( cur->name, cmd_name ) < 0; prev = cur, cur = cur->next ); - - if( prev ) prev->next = cmd; - else cmd_functions = cmd; - cmd->next = cur; - - return 1; + return Cmd_AddCommandEx( __FUNCTION__, cmd_name, function, "server command", CMD_GAMEUIDLL ); } /* diff --git a/engine/common/crtlib.h b/engine/common/crtlib.h index a3c1e262..034574a8 100644 --- a/engine/common/crtlib.h +++ b/engine/common/crtlib.h @@ -36,6 +36,7 @@ enum #define CMD_SERVERDLL BIT( 0 ) // added by server.dll #define CMD_CLIENTDLL BIT( 1 ) // added by client.dll #define CMD_GAMEUIDLL BIT( 2 ) // added by GameUI.dll +#define CMD_LOCALONLY BIT( 3 ) // restricted from server commands typedef void (*xcommand_t)( void ); @@ -54,6 +55,7 @@ const char *Cmd_Argv( int arg ); void Cmd_Init( void ); void Cmd_Unlink( int group ); void Cmd_AddCommand( const char *cmd_name, xcommand_t function, const char *cmd_desc ); +void Cmd_AddRestrictedCommand( const char *cmd_name, xcommand_t function, const char *cmd_desc ); void Cmd_AddServerCommand( const char *cmd_name, xcommand_t function ); int Cmd_AddClientCommand( const char *cmd_name, xcommand_t function ); int Cmd_AddGameUICommand( const char *cmd_name, xcommand_t function ); diff --git a/engine/common/cvar.h b/engine/common/cvar.h index 464a4dff..2ee7addf 100644 --- a/engine/common/cvar.h +++ b/engine/common/cvar.h @@ -43,6 +43,7 @@ typedef struct convar_s #define FCVAR_ALLOCATED (1<<19) // this convar_t is fully dynamic allocated (include description) #define FCVAR_VIDRESTART (1<<20) // recreate the window is cvar with this flag was changed #define FCVAR_TEMPORARY (1<<21) // these cvars holds their values and can be unlink in any time +#define FCVAR_LOCALONLY (1<<22) // can be set only from local buffers #define CVAR_DEFINE( cv, cvname, cvstr, cvflags, cvdesc ) convar_t cv = { cvname, cvstr, cvflags, 0.0f, (void *)CVAR_SENTINEL, cvdesc } #define CVAR_DEFINE_AUTO( cv, cvstr, cvflags, cvdesc ) convar_t cv = { #cv, cvstr, cvflags, 0.0f, (void *)CVAR_SENTINEL, cvdesc }