1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/convar.cpp Sat Apr 16 19:59:26 2005 +0000
1.3 @@ -0,0 +1,812 @@
1.4 +//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
1.5 +//
1.6 +// Purpose:
1.7 +//
1.8 +// $NoKeywords: $
1.9 +//
1.10 +//=============================================================================//
1.11 +
1.12 +#include <stdio.h>
1.13 +#include <stdlib.h>
1.14 +#include <string.h>
1.15 +#include "basetypes.h"
1.16 +#include "convar.h"
1.17 +#include "vstdlib/strtools.h"
1.18 +#include "tier0/dbg.h"
1.19 +#include "tier0/memdbgon.h"
1.20 +
1.21 +
1.22 +ConCommandBase *ConCommandBase::s_pConCommandBases = NULL;
1.23 +IConCommandBaseAccessor *ConCommandBase::s_pAccessor = NULL;
1.24 +
1.25 +// ----------------------------------------------------------------------------- //
1.26 +// ConCommandBaseMgr.
1.27 +// ----------------------------------------------------------------------------- //
1.28 +void ConCommandBaseMgr::OneTimeInit( IConCommandBaseAccessor *pAccessor )
1.29 +{
1.30 + ConCommandBase *pCur, *pNext;
1.31 +
1.32 + ConCommandBase::s_pAccessor = pAccessor;
1.33 +
1.34 + pCur = ConCommandBase::s_pConCommandBases;
1.35 + while ( pCur )
1.36 + {
1.37 + pNext = pCur->m_pNext;
1.38 + pCur->Init();
1.39 + pCur = pNext;
1.40 + }
1.41 +}
1.42 +
1.43 +//-----------------------------------------------------------------------------
1.44 +// Purpose: Default constructor
1.45 +//-----------------------------------------------------------------------------
1.46 +ConCommandBase::ConCommandBase( void )
1.47 +{
1.48 + m_bRegistered = false;
1.49 + m_pszName = NULL;
1.50 + m_pszHelpString = NULL;
1.51 +
1.52 + m_nFlags = 0;
1.53 + m_pNext = NULL;
1.54 +}
1.55 +
1.56 +//-----------------------------------------------------------------------------
1.57 +// Purpose: The base console invoked command/cvar interface
1.58 +// Input : *pName - name of variable/command
1.59 +// *pHelpString - help text
1.60 +// flags - flags
1.61 +//-----------------------------------------------------------------------------
1.62 +ConCommandBase::ConCommandBase( char const *pName, char const *pHelpString /*=0*/, int flags /*= 0*/ )
1.63 +{
1.64 + Create( pName, pHelpString, flags );
1.65 +}
1.66 +
1.67 +//-----------------------------------------------------------------------------
1.68 +// Purpose:
1.69 +//-----------------------------------------------------------------------------
1.70 +ConCommandBase::~ConCommandBase( void )
1.71 +{
1.72 +}
1.73 +
1.74 +//-----------------------------------------------------------------------------
1.75 +// Purpose:
1.76 +// Output : Returns true on success, false on failure.
1.77 +//-----------------------------------------------------------------------------
1.78 +bool ConCommandBase::IsCommand( void ) const
1.79 +{
1.80 +// Assert( 0 ); This can't assert. . causes a recursive assert in Sys_Printf, etc.
1.81 + return true;
1.82 +}
1.83 +
1.84 +//-----------------------------------------------------------------------------
1.85 +// Purpose:
1.86 +// Input : *pName -
1.87 +// callback -
1.88 +// *pHelpString -
1.89 +// flags -
1.90 +//-----------------------------------------------------------------------------
1.91 +void ConCommandBase::Create( char const *pName, char const *pHelpString /*= 0*/, int flags /*= 0*/ )
1.92 +{
1.93 + static char *empty_string = "";
1.94 +
1.95 + m_bRegistered = false;
1.96 +
1.97 + // Name should be static data
1.98 + Assert( pName );
1.99 + m_pszName = pName;
1.100 + m_pszHelpString = pHelpString ? pHelpString : empty_string;
1.101 +
1.102 + m_nFlags = flags;
1.103 +
1.104 + if ( !( m_nFlags & FCVAR_UNREGISTERED ) )
1.105 + {
1.106 + m_pNext = s_pConCommandBases;
1.107 + s_pConCommandBases = this;
1.108 + }
1.109 + else
1.110 + {
1.111 + // It's unregistered
1.112 + m_pNext = NULL;
1.113 + }
1.114 +
1.115 + // If s_pAccessor is already set (this ConVar is not a global variable),
1.116 + // register it.
1.117 + if ( s_pAccessor )
1.118 + {
1.119 + Init();
1.120 + }
1.121 +}
1.122 +
1.123 +//-----------------------------------------------------------------------------
1.124 +// Purpose: Used internally by OneTimeInit to initialize.
1.125 +//-----------------------------------------------------------------------------
1.126 +void ConCommandBase::Init()
1.127 +{
1.128 + if ( !s_pAccessor )
1.129 + return;
1.130 +
1.131 + if ( m_bRegistered )
1.132 + return;
1.133 +
1.134 + s_pAccessor->RegisterConCommandBase( this );
1.135 +
1.136 + m_bRegistered = true;
1.137 +}
1.138 +
1.139 +//-----------------------------------------------------------------------------
1.140 +// Purpose:
1.141 +// Input : *name -
1.142 +// Output : ConCommandBase
1.143 +//-----------------------------------------------------------------------------
1.144 +ConCommandBase const *ConCommandBase::FindCommand( char const *name )
1.145 +{
1.146 + ConCommandBase const *cmd = GetCommands();
1.147 + for ( ; cmd; cmd = cmd->GetNext() )
1.148 + {
1.149 + if ( !stricmp( name, cmd->GetName() ) )
1.150 + return cmd;
1.151 + }
1.152 + return NULL;
1.153 +}
1.154 +
1.155 +//-----------------------------------------------------------------------------
1.156 +// Purpose:
1.157 +// Output : ConCommandBase
1.158 +//-----------------------------------------------------------------------------
1.159 +const ConCommandBase *ConCommandBase::GetCommands( void )
1.160 +{
1.161 + return s_pConCommandBases;
1.162 +}
1.163 +
1.164 +//-----------------------------------------------------------------------------
1.165 +// Purpose:
1.166 +// Input : *var -
1.167 +// Output : static
1.168 +//-----------------------------------------------------------------------------
1.169 +void ConCommandBase::AddToList( ConCommandBase *var )
1.170 +{
1.171 + // This routine is only valid on root ConCommandBases
1.172 + var->m_pNext = s_pConCommandBases;
1.173 + s_pConCommandBases = var;
1.174 +}
1.175 +
1.176 +//-----------------------------------------------------------------------------
1.177 +// Purpose: Return name of the command/var
1.178 +// Output : char const
1.179 +//-----------------------------------------------------------------------------
1.180 +char const *ConCommandBase::GetName( void ) const
1.181 +{
1.182 + return m_pszName;
1.183 +}
1.184 +
1.185 +//-----------------------------------------------------------------------------
1.186 +// Purpose:
1.187 +// Input : flag -
1.188 +//-----------------------------------------------------------------------------
1.189 +void ConCommandBase::RemoveFlaggedCommands( int flag )
1.190 +{
1.191 + ConCommandBase *pNewList;
1.192 + ConCommandBase *pCommand, *pNext;
1.193 +
1.194 + pNewList = NULL;
1.195 +
1.196 + pCommand = s_pConCommandBases;
1.197 + while ( pCommand )
1.198 + {
1.199 + pNext = pCommand->m_pNext;
1.200 + if ( !( pCommand->m_nFlags & flag ) )
1.201 + {
1.202 + pCommand->m_pNext = pNewList;
1.203 + pNewList = pCommand;
1.204 + }
1.205 + else
1.206 + {
1.207 + // Unlink
1.208 + pCommand->m_pNext = NULL;
1.209 + }
1.210 +
1.211 + pCommand = pNext;
1.212 + }
1.213 +
1.214 + s_pConCommandBases = pNewList;
1.215 +}
1.216 +
1.217 +void ConCommandBase::RevertFlaggedCvars( int flag )
1.218 +{
1.219 + for (const ConCommandBase *var= GetCommands() ; var ; var=var->GetNext())
1.220 + {
1.221 + if ( var->IsCommand() )
1.222 + continue;
1.223 +
1.224 + ConVar *cvar = ( ConVar * )var;
1.225 +
1.226 + if ( !cvar->IsBitSet( flag ) )
1.227 + continue;
1.228 +
1.229 + // It's == to the default value, don't count
1.230 + if ( !Q_strcasecmp( cvar->GetDefault(), cvar->GetString() ) )
1.231 + continue;
1.232 +
1.233 + cvar->Revert();
1.234 +
1.235 + // DevMsg( "%s = \"%s\" (reverted)\n", cvar->GetName(), cvar->GetString() );
1.236 + }
1.237 +}
1.238 +
1.239 +//-----------------------------------------------------------------------------
1.240 +// Purpose:
1.241 +// Input : flag -
1.242 +// Output : Returns true on success, false on failure.
1.243 +//-----------------------------------------------------------------------------
1.244 +bool ConCommandBase::IsBitSet( int flag ) const
1.245 +{
1.246 + return ( flag & m_nFlags ) ? true : false;
1.247 +}
1.248 +
1.249 +//-----------------------------------------------------------------------------
1.250 +// Purpose:
1.251 +// Input : flags -
1.252 +//-----------------------------------------------------------------------------
1.253 +void ConCommandBase::AddFlags( int flags )
1.254 +{
1.255 + m_nFlags |= flags;
1.256 +}
1.257 +
1.258 +//-----------------------------------------------------------------------------
1.259 +// Purpose:
1.260 +// Output : const ConCommandBase
1.261 +//-----------------------------------------------------------------------------
1.262 +const ConCommandBase *ConCommandBase::GetNext( void ) const
1.263 +{
1.264 + return m_pNext;
1.265 +}
1.266 +
1.267 +//-----------------------------------------------------------------------------
1.268 +// Purpose:
1.269 +// Input : *next -
1.270 +//-----------------------------------------------------------------------------
1.271 +void ConCommandBase::SetNext( ConCommandBase *next )
1.272 +{
1.273 + m_pNext = next;
1.274 +}
1.275 +
1.276 +//-----------------------------------------------------------------------------
1.277 +// Purpose: Copies string using local new/delete operators
1.278 +// Input : *from -
1.279 +// Output : char
1.280 +//-----------------------------------------------------------------------------
1.281 +char *ConCommandBase::CopyString( char const *from )
1.282 +{
1.283 + int len;
1.284 + char *to;
1.285 +
1.286 + len = strlen( from );
1.287 + if ( len <= 0 )
1.288 + {
1.289 + to = new char[1];
1.290 + to[0] = 0;
1.291 + }
1.292 + else
1.293 + {
1.294 + to = new char[len+1];
1.295 + Q_strncpy( to, from, len+1 );
1.296 + }
1.297 + return to;
1.298 +}
1.299 +
1.300 +//-----------------------------------------------------------------------------
1.301 +// Purpose:
1.302 +// Output : char const
1.303 +//-----------------------------------------------------------------------------
1.304 +char const *ConCommandBase::GetHelpText( void ) const
1.305 +{
1.306 + return m_pszHelpString;
1.307 +}
1.308 +
1.309 +//-----------------------------------------------------------------------------
1.310 +// Purpose: Has this cvar been registered
1.311 +// Output : Returns true on success, false on failure.
1.312 +//-----------------------------------------------------------------------------
1.313 +bool ConCommandBase::IsRegistered( void ) const
1.314 +{
1.315 + return m_bRegistered;
1.316 +}
1.317 +
1.318 +//-----------------------------------------------------------------------------
1.319 +// Purpose: Constructs a console command
1.320 +// Input : *pName - name of command
1.321 +// callback - function to call upon execution
1.322 +// *pHelpString - help text for command
1.323 +// flags - command flags, if any
1.324 +//-----------------------------------------------------------------------------
1.325 +ConCommand::ConCommand( char const *pName, FnCommandCallback callback, char const *pHelpString /*= 0*/, int flags /*= 0*/, FnCommandCompletionCallback completionFunc /*= 0*/ )
1.326 +{
1.327 + Create( pName, callback, pHelpString, flags, completionFunc );
1.328 +}
1.329 +
1.330 +//-----------------------------------------------------------------------------
1.331 +// Purpose:
1.332 +//-----------------------------------------------------------------------------
1.333 +ConCommand::~ConCommand( void )
1.334 +{
1.335 +
1.336 +}
1.337 +
1.338 +//-----------------------------------------------------------------------------
1.339 +// Purpose:
1.340 +// Output : Returns true on success, false on failure.
1.341 +//-----------------------------------------------------------------------------
1.342 +bool ConCommand::IsCommand( void ) const
1.343 +{
1.344 + return true;
1.345 +}
1.346 +
1.347 +//-----------------------------------------------------------------------------
1.348 +// Purpose: Invoke the function if there is one
1.349 +//-----------------------------------------------------------------------------
1.350 +void ConCommand::Dispatch( void )
1.351 +{
1.352 + if ( m_fnCommandCallback )
1.353 + {
1.354 + ( *m_fnCommandCallback )();
1.355 + }
1.356 + else
1.357 + {
1.358 + // Command without callback!!!
1.359 + Assert( 0 );
1.360 + }
1.361 +}
1.362 +
1.363 +//-----------------------------------------------------------------------------
1.364 +// Purpose:
1.365 +// Input : *partial -
1.366 +// context -
1.367 +// longest -
1.368 +// maxcommands -
1.369 +// **commands -
1.370 +// Output : int
1.371 +//-----------------------------------------------------------------------------
1.372 +int DefaultCompletionFunc( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
1.373 +{
1.374 + return 0;
1.375 +}
1.376 +
1.377 +//-----------------------------------------------------------------------------
1.378 +// Purpose: Create the named command
1.379 +// Input : *pName -
1.380 +// callback -
1.381 +// *pHelpString -
1.382 +// flags -
1.383 +//-----------------------------------------------------------------------------
1.384 +void ConCommand::Create( char const *pName, FnCommandCallback callback, char const *pHelpString /*= 0*/, int flags /*= 0*/, FnCommandCompletionCallback completionFunc /*=0*/ )
1.385 +{
1.386 + // Set the callback
1.387 + m_fnCommandCallback = callback;
1.388 +
1.389 + m_fnCompletionCallback = completionFunc ? completionFunc : DefaultCompletionFunc;
1.390 + m_bHasCompletionCallback = completionFunc != 0 ? true : false;
1.391 +
1.392 + // Setup the rest
1.393 + BaseClass::Create( pName, pHelpString, flags );
1.394 +}
1.395 +
1.396 +//-----------------------------------------------------------------------------
1.397 +// Purpose:
1.398 +// Input : *partial -
1.399 +// context -
1.400 +// longest -
1.401 +// maxcommands -
1.402 +// **commands -
1.403 +//-----------------------------------------------------------------------------
1.404 +int ConCommand::AutoCompleteSuggest( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
1.405 +{
1.406 + Assert( m_fnCompletionCallback );
1.407 + if ( !m_fnCompletionCallback )
1.408 + return 0;
1.409 +
1.410 + return ( m_fnCompletionCallback )( partial, commands );
1.411 +}
1.412 +
1.413 +//-----------------------------------------------------------------------------
1.414 +// Purpose:
1.415 +// Output : Returns true on success, false on failure.
1.416 +//-----------------------------------------------------------------------------
1.417 +bool ConCommand::CanAutoComplete( void )
1.418 +{
1.419 + return m_bHasCompletionCallback;
1.420 +}
1.421 +
1.422 +// ----------------------------------------------------------------------------- //
1.423 +// ConVar.
1.424 +// ----------------------------------------------------------------------------- //
1.425 +ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags /* = 0 */ )
1.426 +{
1.427 + Create( pName, pDefaultValue, flags );
1.428 +}
1.429 +
1.430 +// ----------------------------------------------------------------------------- //
1.431 +// ConVar.
1.432 +// ----------------------------------------------------------------------------- //
1.433 +ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString )
1.434 +{
1.435 + Create( pName, pDefaultValue, flags, pHelpString );
1.436 +}
1.437 +
1.438 +// ----------------------------------------------------------------------------- //
1.439 +// ConVar.
1.440 +// ----------------------------------------------------------------------------- //
1.441 +ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax )
1.442 +{
1.443 + Create( pName, pDefaultValue, flags, pHelpString, bMin, fMin, bMax, fMax );
1.444 +}
1.445 +
1.446 +// ----------------------------------------------------------------------------- //
1.447 +// ConVar.
1.448 +// ----------------------------------------------------------------------------- //
1.449 +ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString, FnChangeCallback callback )
1.450 +{
1.451 + Create( pName, pDefaultValue, flags, pHelpString, false, 0.0, false, 0.0, callback );
1.452 +}
1.453 +
1.454 +// ----------------------------------------------------------------------------- //
1.455 +// ConVar.
1.456 +// ----------------------------------------------------------------------------- //
1.457 +ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax, FnChangeCallback callback )
1.458 +{
1.459 + Create( pName, pDefaultValue, flags, pHelpString, bMin, fMin, bMax, fMax, callback );
1.460 +}
1.461 +
1.462 +//-----------------------------------------------------------------------------
1.463 +// Purpose: Free dynamic memory
1.464 +//-----------------------------------------------------------------------------
1.465 +ConVar::~ConVar( void )
1.466 +{
1.467 + if ( m_pszString )
1.468 + {
1.469 + delete[] m_pszString;
1.470 + m_pszString = NULL;
1.471 + }
1.472 +}
1.473 +
1.474 +
1.475 +//-----------------------------------------------------------------------------
1.476 +// Install a change callback (there shouldn't already be one....)
1.477 +//-----------------------------------------------------------------------------
1.478 +
1.479 +void ConVar::InstallChangeCallback( FnChangeCallback callback )
1.480 +{
1.481 + Assert( !m_fnChangeCallback || !callback );
1.482 + m_fnChangeCallback = callback;
1.483 +
1.484 + if (m_fnChangeCallback)
1.485 + {
1.486 + // Call it immediately to set the initial value...
1.487 + m_fnChangeCallback( this, m_pszString );
1.488 + }
1.489 +}
1.490 +
1.491 +bool ConVar::IsBitSet( int flag ) const
1.492 +{
1.493 + return ( flag & m_pParent->m_nFlags ) ? true : false;
1.494 +}
1.495 +
1.496 +char const *ConVar::GetHelpText( void ) const
1.497 +{
1.498 + return m_pParent->m_pszHelpString;
1.499 +}
1.500 +
1.501 +void ConVar::AddFlags( int flags )
1.502 +{
1.503 + m_pParent->m_nFlags |= flags;
1.504 +}
1.505 +
1.506 +bool ConVar::IsRegistered( void ) const
1.507 +{
1.508 + return m_pParent->m_bRegistered;
1.509 +}
1.510 +
1.511 +char const *ConVar::GetName( void ) const
1.512 +{
1.513 + return m_pParent->m_pszName;
1.514 +}
1.515 +
1.516 +//-----------------------------------------------------------------------------
1.517 +// Purpose:
1.518 +// Output : Returns true on success, false on failure.
1.519 +//-----------------------------------------------------------------------------
1.520 +bool ConVar::IsCommand( void ) const
1.521 +{
1.522 + return false;
1.523 +}
1.524 +
1.525 +//-----------------------------------------------------------------------------
1.526 +// Purpose:
1.527 +// Input :
1.528 +//-----------------------------------------------------------------------------
1.529 +void ConVar::Init()
1.530 +{
1.531 + BaseClass::Init();
1.532 +}
1.533 +
1.534 +//-----------------------------------------------------------------------------
1.535 +// Purpose:
1.536 +// Input : *value -
1.537 +//-----------------------------------------------------------------------------
1.538 +void ConVar::InternalSetValue( char const *value )
1.539 +{
1.540 + float fNewValue;
1.541 + char tempVal[ 32 ];
1.542 + char *val;
1.543 +
1.544 + Assert(m_pParent == this); // Only valid for root convars.
1.545 +
1.546 + val = (char *)value;
1.547 + fNewValue = ( float )atof( value );
1.548 +
1.549 + if ( ClampValue( fNewValue ) )
1.550 + {
1.551 + Q_snprintf( tempVal,sizeof(tempVal), "%f", fNewValue );
1.552 + val = tempVal;
1.553 + }
1.554 +
1.555 + // Redetermine value
1.556 + m_fValue = fNewValue;
1.557 + m_nValue = ( int )( m_fValue );
1.558 +
1.559 + if ( !(m_nFlags & FCVAR_NEVER_AS_STRING) )
1.560 + ChangeStringValue( val );
1.561 +}
1.562 +
1.563 +//-----------------------------------------------------------------------------
1.564 +// Purpose:
1.565 +// Input : *tempVal -
1.566 +//-----------------------------------------------------------------------------
1.567 +void ConVar::ChangeStringValue( char const *tempVal )
1.568 +{
1.569 + Assert( !( m_nFlags & FCVAR_NEVER_AS_STRING ) );
1.570 +
1.571 + char* pszOldValue = (char*)stackalloc( m_StringLength );
1.572 +
1.573 + if ( m_fnChangeCallback )
1.574 + {
1.575 + memcpy( pszOldValue, m_pszString, m_StringLength );
1.576 + }
1.577 +
1.578 + int len = Q_strlen(tempVal) + 1;
1.579 +
1.580 + if ( len > m_StringLength)
1.581 + {
1.582 + if (m_pszString)
1.583 + delete[] m_pszString;
1.584 +
1.585 + m_pszString = new char[len];
1.586 + m_StringLength = len;
1.587 + }
1.588 +
1.589 + memcpy( m_pszString, tempVal, len );
1.590 +
1.591 + // Invoke any necessary callback function
1.592 + if ( m_fnChangeCallback )
1.593 + {
1.594 + m_fnChangeCallback( this, pszOldValue );
1.595 + }
1.596 +
1.597 + stackfree( pszOldValue );
1.598 +}
1.599 +
1.600 +//-----------------------------------------------------------------------------
1.601 +// Purpose: Check whether to clamp and then perform clamp
1.602 +// Input : value -
1.603 +// Output : Returns true if value changed
1.604 +//-----------------------------------------------------------------------------
1.605 +bool ConVar::ClampValue( float& value )
1.606 +{
1.607 + if ( m_bHasMin && ( value < m_fMinVal ) )
1.608 + {
1.609 + value = m_fMinVal;
1.610 + return true;
1.611 + }
1.612 +
1.613 + if ( m_bHasMax && ( value > m_fMaxVal ) )
1.614 + {
1.615 + value = m_fMaxVal;
1.616 + return true;
1.617 + }
1.618 +
1.619 + return false;
1.620 +}
1.621 +
1.622 +//-----------------------------------------------------------------------------
1.623 +// Purpose:
1.624 +// Input : *value -
1.625 +//-----------------------------------------------------------------------------
1.626 +void ConVar::InternalSetFloatValue( float fNewValue )
1.627 +{
1.628 + if ( fNewValue == m_fValue )
1.629 + return;
1.630 +
1.631 + Assert( m_pParent == this ); // Only valid for root convars.
1.632 +
1.633 + // Check bounds
1.634 + ClampValue( fNewValue );
1.635 +
1.636 + // Redetermine value
1.637 + m_fValue = fNewValue;
1.638 + m_nValue = ( int )m_fValue;
1.639 +
1.640 + if ( !( m_nFlags & FCVAR_NEVER_AS_STRING ) )
1.641 + {
1.642 + char tempVal[ 32 ];
1.643 + Q_snprintf( tempVal, sizeof( tempVal), "%f", m_fValue );
1.644 + ChangeStringValue( tempVal );
1.645 + }
1.646 + else
1.647 + {
1.648 + Assert( !m_fnChangeCallback );
1.649 + }
1.650 +}
1.651 +
1.652 +//-----------------------------------------------------------------------------
1.653 +// Purpose:
1.654 +// Input : *value -
1.655 +//-----------------------------------------------------------------------------
1.656 +void ConVar::InternalSetIntValue( int nValue )
1.657 +{
1.658 + if ( nValue == m_nValue )
1.659 + return;
1.660 +
1.661 + Assert( m_pParent == this ); // Only valid for root convars.
1.662 +
1.663 + float fValue = (float)nValue;
1.664 + if ( ClampValue( fValue ) )
1.665 + {
1.666 + nValue = ( int )( fValue );
1.667 + }
1.668 +
1.669 + // Redetermine value
1.670 + m_fValue = fValue;
1.671 + m_nValue = nValue;
1.672 +
1.673 + if ( !( m_nFlags & FCVAR_NEVER_AS_STRING ) )
1.674 + {
1.675 + char tempVal[ 32 ];
1.676 + Q_snprintf( tempVal, sizeof( tempVal ), "%d", m_nValue );
1.677 + ChangeStringValue( tempVal );
1.678 + }
1.679 + else
1.680 + {
1.681 + Assert( !m_fnChangeCallback );
1.682 + }
1.683 +}
1.684 +
1.685 +//-----------------------------------------------------------------------------
1.686 +// Purpose: Private creation
1.687 +//-----------------------------------------------------------------------------
1.688 +void ConVar::Create( char const *pName, char const *pDefaultValue, int flags /*= 0*/,
1.689 + char const *pHelpString /*= NULL*/, bool bMin /*= false*/, float fMin /*= 0.0*/,
1.690 + bool bMax /*= false*/, float fMax /*= false*/, FnChangeCallback callback /*= NULL*/ )
1.691 +{
1.692 + static char *empty_string = "";
1.693 +
1.694 + m_pParent = this;
1.695 +
1.696 + // Name should be static data
1.697 + m_pszDefaultValue = pDefaultValue ? pDefaultValue : empty_string;
1.698 + Assert( pDefaultValue );
1.699 +
1.700 + m_StringLength = strlen( m_pszDefaultValue ) + 1;
1.701 + m_pszString = new char[m_StringLength];
1.702 + memcpy( m_pszString, m_pszDefaultValue, m_StringLength );
1.703 +
1.704 + m_bHasMin = bMin;
1.705 + m_fMinVal = fMin;
1.706 + m_bHasMax = bMax;
1.707 + m_fMaxVal = fMax;
1.708 +
1.709 + m_fnChangeCallback = callback;
1.710 +
1.711 + m_fValue = ( float )atof( m_pszString );
1.712 +
1.713 + // Bounds Check, should never happen, if it does, no big deal
1.714 + if ( m_bHasMin && ( m_fValue < m_fMinVal ) )
1.715 + {
1.716 + Assert( 0 );
1.717 + }
1.718 +
1.719 + if ( m_bHasMax && ( m_fValue > m_fMaxVal ) )
1.720 + {
1.721 + Assert( 0 );
1.722 + }
1.723 +
1.724 + m_nValue = ( int )m_fValue;
1.725 +
1.726 + BaseClass::Create( pName, pHelpString, flags );
1.727 +}
1.728 +
1.729 +//-----------------------------------------------------------------------------
1.730 +// Purpose:
1.731 +// Input : *value -
1.732 +//-----------------------------------------------------------------------------
1.733 +void ConVar::SetValue(char const *value)
1.734 +{
1.735 + ConVar *var = ( ConVar * )m_pParent;
1.736 + var->InternalSetValue( value );
1.737 +}
1.738 +
1.739 +//-----------------------------------------------------------------------------
1.740 +// Purpose:
1.741 +// Input : value -
1.742 +//-----------------------------------------------------------------------------
1.743 +void ConVar::SetValue( float value )
1.744 +{
1.745 + ConVar *var = ( ConVar * )m_pParent;
1.746 + var->InternalSetFloatValue( value );
1.747 +}
1.748 +
1.749 +//-----------------------------------------------------------------------------
1.750 +// Purpose:
1.751 +// Input : value -
1.752 +//-----------------------------------------------------------------------------
1.753 +void ConVar::SetValue( int value )
1.754 +{
1.755 + ConVar *var = ( ConVar * )m_pParent;
1.756 + var->InternalSetIntValue( value );
1.757 +}
1.758 +
1.759 +//-----------------------------------------------------------------------------
1.760 +// Purpose: Reset to default value
1.761 +//-----------------------------------------------------------------------------
1.762 +void ConVar::Revert( void )
1.763 +{
1.764 + // Force default value again
1.765 + ConVar *var = ( ConVar * )m_pParent;
1.766 + var->SetValue( var->m_pszDefaultValue );
1.767 +}
1.768 +
1.769 +//-----------------------------------------------------------------------------
1.770 +// Purpose:
1.771 +//-----------------------------------------------------------------------------
1.772 +void ConVar::RevertAll( void )
1.773 +{
1.774 + ConCommandBase *p = s_pConCommandBases;
1.775 + while ( p )
1.776 + {
1.777 + if ( !p->IsCommand() )
1.778 + {
1.779 + ConVar *var = ( ConVar * )p;
1.780 + var->Revert();
1.781 + }
1.782 + p = p->m_pNext;
1.783 + }
1.784 +}
1.785 +
1.786 +//-----------------------------------------------------------------------------
1.787 +// Purpose:
1.788 +// Input : minVal -
1.789 +// Output : true if there is a min set
1.790 +//-----------------------------------------------------------------------------
1.791 +bool ConVar::GetMin( float& minVal ) const
1.792 +{
1.793 + minVal = m_pParent->m_fMinVal;
1.794 + return m_pParent->m_bHasMin;
1.795 +}
1.796 +
1.797 +//-----------------------------------------------------------------------------
1.798 +// Purpose:
1.799 +// Input : maxVal -
1.800 +//-----------------------------------------------------------------------------
1.801 +bool ConVar::GetMax( float& maxVal ) const
1.802 +{
1.803 + maxVal = m_pParent->m_fMaxVal;
1.804 + return m_pParent->m_bHasMax;
1.805 +}
1.806 +
1.807 +//-----------------------------------------------------------------------------
1.808 +// Purpose:
1.809 +// Output : char const
1.810 +//-----------------------------------------------------------------------------
1.811 +char const *ConVar::GetDefault( void ) const
1.812 +{
1.813 + return m_pParent->m_pszDefaultValue;
1.814 +}
1.815 +