* 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
This commit is contained in:
Tim Angus 2008-07-07 22:31:39 +00:00
parent 809c361744
commit 37d664d4b2
24 changed files with 239 additions and 110 deletions

View file

@ -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();

View file

@ -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;
@ -877,6 +985,22 @@ void Cvar_InfoStringBuffer( int bit, char* buff, int buffsize ) {
Q_strncpyz(buff,Cvar_InfoString(bit),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
@ -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
=====================

View file

@ -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

View file

@ -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;

View file

@ -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 );