diff options
author | tma <tma@edf5b092-35ff-0310-97b2-ce42778d08ea> | 2008-07-07 22:31:39 +0000 |
---|---|---|
committer | tma <tma@edf5b092-35ff-0310-97b2-ce42778d08ea> | 2008-07-07 22:31:39 +0000 |
commit | 80e25417be92abff3053a4d8f209349a16e6d334 (patch) | |
tree | 1058d52d8de3d7115cf5b018e00408b0db8c5341 /code/qcommon | |
parent | afbf4dd0c3999e58f59b3d4e3dd4b93d6abe8fd0 (diff) | |
download | ioquake3-aero-80e25417be92abff3053a4d8f209349a16e6d334.tar.gz ioquake3-aero-80e25417be92abff3053a4d8f209349a16e6d334.zip |
* Rename voip cvar to cl_voip
* s/#if USE_VOIP/#ifdef USE_VOIP/
* Add generalised runtime cvar range checking, currently on [sv|cl]_voip,
dedicated and a bunch of r_ variables
git-svn-id: svn://svn.icculus.org/quake3/trunk@1420 edf5b092-35ff-0310-97b2-ce42778d08ea
Diffstat (limited to 'code/qcommon')
-rw-r--r-- | code/qcommon/common.c | 10 | ||||
-rw-r--r-- | code/qcommon/cvar.c | 126 | ||||
-rw-r--r-- | code/qcommon/q_shared.c | 22 | ||||
-rw-r--r-- | code/qcommon/q_shared.h | 22 | ||||
-rw-r--r-- | code/qcommon/qcommon.h | 1 |
5 files changed, 169 insertions, 12 deletions
diff --git a/code/qcommon/common.c b/code/qcommon/common.c index f100b82..5888592 100644 --- a/code/qcommon/common.c +++ b/code/qcommon/common.c @@ -473,10 +473,12 @@ qboolean Com_AddStartupCommands( void ) { continue; } - // set commands won't override menu startup - if ( Q_stricmpn( com_consoleLines[i], "set", 3 ) ) { - added = qtrue; + // set commands already added with Com_StartupVariable + if ( !Q_stricmpn( com_consoleLines[i], "set", 3 ) ) { + continue; } + + added = qtrue; Cbuf_AddText( com_consoleLines[i] ); Cbuf_AddText( "\n" ); } @@ -2562,8 +2564,10 @@ void Com_Init( char *commandLine ) { // get dedicated here for proper hunk megs initialization #ifdef DEDICATED com_dedicated = Cvar_Get ("dedicated", "1", CVAR_INIT); + Cvar_CheckRange( com_dedicated, 1, 2, qtrue ); #else com_dedicated = Cvar_Get ("dedicated", "0", CVAR_LATCH); + Cvar_CheckRange( com_dedicated, 0, 2, qtrue ); #endif // allocate the stack based hunk allocator Com_InitHunkMemory(); diff --git a/code/qcommon/cvar.c b/code/qcommon/cvar.c index 2526e38..fc2a9fa 100644 --- a/code/qcommon/cvar.c +++ b/code/qcommon/cvar.c @@ -189,6 +189,109 @@ void Cvar_CommandCompletion( void(*callback)(const char *s) ) { } } +/* +============ +Cvar_Validate +============ +*/ +static const char *Cvar_Validate( cvar_t *var, + const char *value, qboolean warn ) +{ + static char s[ MAX_CVAR_VALUE_STRING ]; + float valuef; + qboolean changed = qfalse; + + if( !var->validate ) + return value; + + if( !value ) + return value; + + if( Q_isanumber( value ) ) + { + valuef = atof( value ); + + if( var->integral ) + { + if( !Q_isintegral( valuef ) ) + { + if( warn ) + Com_Printf( "WARNING: cvar '%s' must be integral", var->name ); + + valuef = (int)valuef; + changed = qtrue; + } + } + } + else + { + if( warn ) + Com_Printf( "WARNING: cvar '%s' must be numeric", var->name ); + + valuef = atof( var->resetString ); + changed = qtrue; + } + + if( valuef < var->min ) + { + if( warn ) + { + if( changed ) + Com_Printf( " and is" ); + else + Com_Printf( "WARNING: cvar '%s'", var->name ); + + if( Q_isintegral( var->min ) ) + Com_Printf( " out of range (min %d)", (int)var->min ); + else + Com_Printf( " out of range (min %f)", var->min ); + } + + valuef = var->min; + changed = qtrue; + } + else if( valuef > var->max ) + { + if( warn ) + { + if( changed ) + Com_Printf( " and is" ); + else + Com_Printf( "WARNING: cvar '%s'", var->name ); + + if( Q_isintegral( var->max ) ) + Com_Printf( " out of range (max %d)", (int)var->max ); + else + Com_Printf( " out of range (max %f)", var->max ); + } + + valuef = var->max; + changed = qtrue; + } + + if( changed ) + { + if( Q_isintegral( valuef ) ) + { + Com_sprintf( s, sizeof( s ), "%d", (int)valuef ); + + if( warn ) + Com_Printf( ", setting to %d\n", (int)valuef ); + } + else + { + Com_sprintf( s, sizeof( s ), "%f", valuef ); + + if( warn ) + Com_Printf( ", setting to %f\n", valuef ); + } + + return s; + } + else + return value; +} + /* ============ @@ -220,6 +323,8 @@ cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) { var = Cvar_FindVar (var_name); if ( var ) { + var_value = Cvar_Validate( var, var_value, qfalse ); + // if the C code is now specifying a variable that the user already // set a value for, take the new value as the reset value if ( ( var->flags & CVAR_USER_CREATED ) && !( flags & CVAR_USER_CREATED ) @@ -282,6 +387,7 @@ cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) { var->value = atof (var->string); var->integer = atoi(var->string); var->resetString = CopyString( var_value ); + var->validate = qfalse; // link the variable in var->next = cvar_vars; @@ -364,6 +470,8 @@ cvar_t *Cvar_Set2( const char *var_name, const char *value, qboolean force ) { value = var->resetString; } + value = Cvar_Validate( var, value, qtrue ); + if((var->flags & CVAR_LATCH) && var->latchedString) { if(!strcmp(value,var->latchedString)) return var; @@ -879,6 +987,22 @@ void Cvar_InfoStringBuffer( int bit, char* buff, int buffsize ) { /* ===================== +Cvar_CheckRange +===================== +*/ +void Cvar_CheckRange( cvar_t *var, float min, float max, qboolean integral ) +{ + var->validate = qtrue; + var->min = min; + var->max = max; + var->integral = integral; + + // Force an initial range check + Cvar_Set( var->name, var->string ); +} + +/* +===================== Cvar_Register basically a slightly modified Cvar_Get for the interpreted modules @@ -899,7 +1023,7 @@ void Cvar_Register( vmCvar_t *vmCvar, const char *varName, const char *defaultVa /* ===================== -Cvar_Register +Cvar_Update updates an interpreted modules' version of a cvar ===================== diff --git a/code/qcommon/q_shared.c b/code/qcommon/q_shared.c index 1bc8076..e9f59af 100644 --- a/code/qcommon/q_shared.c +++ b/code/qcommon/q_shared.c @@ -726,6 +726,28 @@ char* Q_strrchr( const char* string, int c ) return sp; } +qboolean Q_isanumber( const char *s ) +{ +#ifdef Q3_VM + //FIXME: implement + return qfalse; +#else + char *p; + + if( *s == '\0' ) + return qfalse; + + strtof( s, &p ); + + return *p == '\0'; +#endif +} + +qboolean Q_isintegral( float f ) +{ + return (int)f == f; +} + /* ============= Q_strncpyz diff --git a/code/qcommon/q_shared.h b/code/qcommon/q_shared.h index 9aa19d9..248fcf1 100644 --- a/code/qcommon/q_shared.h +++ b/code/qcommon/q_shared.h @@ -678,6 +678,8 @@ int Q_isprint( int c ); int Q_islower( int c ); int Q_isupper( int c ); int Q_isalpha( int c ); +qboolean Q_isanumber( const char *s ); +qboolean Q_isintegral( float f ); // portable case insensitive compare int Q_stricmp (const char *s1, const char *s2); @@ -786,15 +788,19 @@ default values. // nothing outside the Cvar_*() functions should modify these fields! typedef struct cvar_s { - char *name; - char *string; - char *resetString; // cvar_restart will reset to this value - char *latchedString; // for CVAR_LATCH vars - int flags; + char *name; + char *string; + char *resetString; // cvar_restart will reset to this value + char *latchedString; // for CVAR_LATCH vars + int flags; qboolean modified; // set each time the cvar is changed - int modificationCount; // incremented each time the cvar is changed - float value; // atof( string ) - int integer; // atoi( string ) + int modificationCount; // incremented each time the cvar is changed + float value; // atof( string ) + int integer; // atoi( string ) + qboolean validate; + qboolean integral; + float min; + float max; struct cvar_s *next; struct cvar_s *hashNext; } cvar_t; diff --git a/code/qcommon/qcommon.h b/code/qcommon/qcommon.h index 58b0a29..c5efef8 100644 --- a/code/qcommon/qcommon.h +++ b/code/qcommon/qcommon.h @@ -528,6 +528,7 @@ char *Cvar_InfoString_Big( int bit ); // returns an info string containing all the cvars that have the given bit set // in their flags ( CVAR_USERINFO, CVAR_SERVERINFO, CVAR_SYSTEMINFO, etc ) void Cvar_InfoStringBuffer( int bit, char *buff, int buffsize ); +void Cvar_CheckRange( cvar_t *cv, float minVal, float maxVal, qboolean shouldBeIntegral ); void Cvar_Restart_f( void ); |