sourcemm/convar.cpp
author David Anderson <dvander@alliedmods.net>
Wed Aug 09 02:05:57 2006 +0000 (2006-08-09)
changeset 232 a86d52ff721c
parent 7 8ef11f6189e4
permissions -rw-r--r--
Merged in changes from the latest SDK
     1 //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
     2 //
     3 // Purpose: 
     4 //
     5 // $NoKeywords: $
     6 //
     7 //=============================================================================//
     8 
     9 #ifdef _XBOX
    10 #include "xbox/xbox_platform.h"
    11 #include "xbox/xbox_win32stubs.h"
    12 #include "xbox/xbox_core.h"
    13 #endif
    14 #include <stdio.h>
    15 #include <stdlib.h>
    16 #include <string.h>
    17 #include "basetypes.h"
    18 #include "convar.h"
    19 #include "vstdlib/strtools.h"
    20 #include "icvar.h"
    21 #include "tier0/dbg.h"
    22 #ifdef _XBOX
    23 #include "vstdlib/ICommandLine.h"
    24 #endif
    25 #include "tier0/memdbgon.h"
    26 
    27 ConCommandBase			*ConCommandBase::s_pConCommandBases = NULL;
    28 IConCommandBaseAccessor	*ConCommandBase::s_pAccessor = NULL;
    29 
    30 #ifdef _XBOX
    31 static char const* GetCommandLineValue( char const *pVariableName )
    32 {
    33 	int nLen = Q_strlen(pVariableName);
    34 	char *pSearch = (char*)stackalloc(nLen + 2);
    35 	pSearch[0] = '+';
    36 	memcpy(&pSearch[1], pVariableName, nLen + 1);
    37 	return CommandLine()->ParmValue(pSearch);
    38 }
    39 #endif
    40 
    41 #if defined( _XBOX ) && !defined( _RETAIL )
    42 void ConCommandBaseMgr::PublishCommands( bool bForce )
    43 {
    44 	ConCommandBase	*pCur;
    45 	const char		*commands[2048];
    46 	const char		*helptext[2048];
    47 	int				numCommands = 0;
    48 
    49 	if ( bForce )
    50 	{
    51 		for ( pCur=ConCommandBase::s_pConCommandBases; pCur; pCur=pCur->m_pNext )
    52 		{
    53 			pCur->m_bRegistered = false;
    54 		}
    55 	}
    56 
    57 	// iterate and publish commands to the remote console
    58 	for ( pCur=ConCommandBase::s_pConCommandBases; pCur; pCur=pCur->m_pNext )
    59 	{
    60 		if ( !pCur->m_bRegistered )
    61 		{
    62 			// add unregistered commands to list
    63 			if ( numCommands < sizeof(commands)/sizeof(commands[0]) )
    64 			{
    65 				commands[numCommands] = pCur->m_pszName;
    66 				helptext[numCommands] = pCur->m_pszHelpString;
    67 				numCommands++;
    68 			}
    69 
    70 			// mark as registered
    71 			pCur->m_bRegistered = true;
    72 		}
    73 	}
    74 	if ( numCommands )
    75 	{
    76 		XBX_rAddCommands( numCommands, commands, helptext );
    77 	}
    78 }
    79 #endif
    80 
    81 #ifdef _XBOX
    82 bool ConCommandBaseMgr::Fixup(ConCommandBase* pConCommand)
    83 {
    84 	ConCommandBase	*pCur;
    85 	ConCommandBase	*pPrev2;
    86 	ConCommandBase	*pCur2;
    87 	ConCommandBase	*pNext2;
    88 	const char		*name;
    89 	static int		initCount = 0;
    90 
    91 	// xboxissue - cvars and its class hierarchy could not be made to instance per subsystem
    92 	// without massive mangling and re-arranging, instead...
    93 	// there is only a single chain and therefore single /init/fixup
    94 	// missing: need to identify which subsystem
    95 	// could pass as part of declaration in constructor, but how to hide parameter for pc
    96 	// the accessors (aka callbacks to subsystems) to register with engine 
    97 	// cannot be invoked as their unlink logic expect private lists
    98 	// so this just mimics the expected end result
    99 	// must handle early and late constructors
   100 	// late constructors are usually function scoped static
   101 	if (!pConCommand)
   102 	{
   103 		// the caller is one-time-init 
   104 		if (++initCount > 1)
   105 		{
   106 			// the list has already been fixed
   107 			return true;
   108 		}
   109 	}
   110 	else
   111 	{
   112 		// the caller is a console command constructor
   113 		if (!initCount)
   114 		{
   115 			// the list has not been fixed yet 
   116 			// no special behavior
   117 			return false;
   118 		}
   119 		else
   120 		{
   121 			// the list has already been fixed
   122 			// the console command is a late constructor
   123 			// add in to fixed list 
   124 			bool hasParent = false;
   125 			if (!pConCommand->IsCommand())
   126 			{
   127 				pCur  = ConCommandBase::s_pConCommandBases;
   128 				while (pCur)
   129 				{
   130 					if (pCur->IsCommand() && !stricmp(pCur->m_pszName, pConCommand->m_pszName))
   131 					{
   132 						// set its parent
   133 						((ConVar*)pConCommand)->m_pParent = ((ConVar*)pCur)->m_pParent;
   134 						hasParent = true;
   135 						break;
   136 					}
   137 					pCur = pCur->m_pNext;
   138 				}
   139 			}
   140 			if (!hasParent)
   141 			{
   142 				// add to head of list
   143 				pConCommand->m_pNext = ConCommandBase::s_pConCommandBases;
   144 				ConCommandBase::s_pConCommandBases = pConCommand;
   145 			}
   146 			else
   147 				return true;
   148 		}
   149 	}
   150 
   151 	if (initCount == 1)
   152 	{
   153 		// iterate the cvars and set their possible proxy parents
   154 		// skip over registered (fixed) entries
   155 		pCur = ConCommandBase::s_pConCommandBases;
   156 		while (pCur)
   157 		{
   158 			if (!pCur->IsCommand() && !pCur->m_bRegistered)
   159 			{
   160 				// iterate from the next node until end of list
   161 				name   = pCur->m_pszName; 
   162 				pPrev2 = pCur;		
   163 				pCur2  = pCur->m_pNext;
   164 				while (pCur2)
   165 				{
   166 					pNext2 = pCur2->m_pNext;
   167 					if (!pCur2->IsCommand() && !stricmp(pCur2->m_pszName, name))
   168 					{
   169 						// found duplicate
   170 						// unlink and fixup
   171 						pCur2->m_pNext  = NULL;
   172 						pPrev2->m_pNext = pNext2;
   173 
   174 						// set its parent
   175 						((ConVar*)pCur2)->m_pParent = ((ConVar*)pCur)->m_pParent;
   176 					}
   177 					else
   178 					{
   179 						// no unlink, advance to next node
   180 						pPrev2 = pCur2;
   181 					}
   182 
   183 					pCur2 = pNext2;
   184 				}
   185 
   186 				char const *pValue = GetCommandLineValue(name);
   187 				if (pValue)
   188 					((ConVar*)pCur)->SetValue(pValue);
   189 			}
   190 			pCur = pCur->m_pNext;
   191 		}
   192 	}
   193 
   194 #if !defined( _RETAIL )
   195 	XBX_rTimeStampLog( Plat_FloatTime(), "xbx PublishCommands:Start" );
   196 
   197 	PublishCommands( false );
   198 
   199 	XBX_rTimeStampLog( Plat_FloatTime(), "xbx PublishCommands:Done" );
   200 #endif
   201 
   202 	// fixup has been performed
   203 	return true;
   204 }
   205 #endif
   206 
   207 // ----------------------------------------------------------------------------- //
   208 // ConCommandBaseMgr.
   209 // ----------------------------------------------------------------------------- //
   210 void ConCommandBaseMgr::OneTimeInit( IConCommandBaseAccessor *pAccessor )
   211 {
   212 #ifdef _XBOX
   213 	// fixup the list
   214 	ConCommandBaseMgr::Fixup(NULL);
   215 #else
   216 	ConCommandBase *pCur, *pNext;
   217 
   218 	ConCommandBase::s_pAccessor = pAccessor;
   219 	pCur = ConCommandBase::s_pConCommandBases;
   220 	while ( pCur )
   221 	{
   222 		pNext = pCur->m_pNext;
   223 		pCur->Init();
   224 		pCur = pNext;
   225 	}
   226 #endif
   227 }
   228 
   229 //-----------------------------------------------------------------------------
   230 // Purpose: Default constructor
   231 //-----------------------------------------------------------------------------
   232 ConCommandBase::ConCommandBase( void )
   233 {
   234 	m_bRegistered   = false;
   235 	m_pszName       = NULL;
   236 	m_pszHelpString = NULL;
   237 
   238 	m_nFlags = 0;
   239 	m_pNext  = NULL;
   240 }
   241 
   242 //-----------------------------------------------------------------------------
   243 // Purpose: The base console invoked command/cvar interface
   244 // Input  : *pName - name of variable/command
   245 //			*pHelpString - help text
   246 //			flags - flags
   247 //-----------------------------------------------------------------------------
   248 ConCommandBase::ConCommandBase( char const *pName, char const *pHelpString /*=0*/, int flags /*= 0*/ )
   249 {
   250 	Create( pName, pHelpString, flags );
   251 }
   252 
   253 //-----------------------------------------------------------------------------
   254 // Purpose: 
   255 //-----------------------------------------------------------------------------
   256 ConCommandBase::~ConCommandBase( void )
   257 {
   258 }
   259 
   260 //-----------------------------------------------------------------------------
   261 // Purpose: 
   262 // Output : Returns true on success, false on failure.
   263 //-----------------------------------------------------------------------------
   264 bool ConCommandBase::IsCommand( void ) const
   265 { 
   266 //	Assert( 0 ); This can't assert. . causes a recursive assert in Sys_Printf, etc.
   267 	return true;
   268 }
   269 	
   270 //-----------------------------------------------------------------------------
   271 // Purpose: 
   272 // Input  : *pName - 
   273 //			callback - 
   274 //			*pHelpString - 
   275 //			flags - 
   276 //-----------------------------------------------------------------------------
   277 void ConCommandBase::Create( char const *pName, char const *pHelpString /*= 0*/, int flags /*= 0*/ )
   278 {
   279 	static char *empty_string = "";
   280 
   281 	m_bRegistered = false;
   282 
   283 	// Name should be static data
   284 	Assert( pName );
   285 	m_pszName = pName;
   286 	m_pszHelpString = pHelpString ? pHelpString : empty_string;
   287 
   288 	m_nFlags = flags;
   289 
   290 #ifdef _XBOX
   291 	if (ConCommandBaseMgr::Fixup(this))
   292 		return;
   293 #endif
   294 
   295 	if ( !( m_nFlags & FCVAR_UNREGISTERED ) )
   296 	{
   297 #ifndef _XBOX
   298 		m_pNext = s_pConCommandBases;
   299 		s_pConCommandBases = this;
   300 #else
   301 		// xboxissue - engine cvars should be at head of list to
   302 		// ensure they are set as the cvar master/parent during fixup
   303 		if (!s_pConCommandBases || !(flags & FCVAR_NON_ENGINE))
   304 		{
   305 			// engine cvars, place at head of list
   306 			m_pNext = s_pConCommandBases;
   307 			s_pConCommandBases = this;
   308 		}
   309 		else
   310 		{
   311 			// non-engine cvars, place at end of list
   312 			ConCommandBase *cur = s_pConCommandBases;
   313 			while (1)
   314 			{
   315 				if (!cur->m_pNext)
   316 				{
   317 					cur->m_pNext = this;
   318 					m_pNext      = NULL;
   319 					break;
   320 				}
   321 				cur = cur->m_pNext;
   322 			}
   323 		}
   324 #endif
   325 	}
   326 	else
   327 	{
   328 		// It's unregistered
   329 		m_pNext = NULL;
   330 	}
   331 
   332 	// If s_pAccessor is already set (this ConVar is not a global variable),
   333 	//  register it.
   334 	if ( s_pAccessor )
   335 	{
   336 		Init();
   337 	}
   338 }
   339 
   340 //-----------------------------------------------------------------------------
   341 // Purpose: Used internally by OneTimeInit to initialize.
   342 //-----------------------------------------------------------------------------
   343 void ConCommandBase::Init()
   344 {
   345 	if ( !s_pAccessor )
   346 		return;
   347 	
   348 	if ( m_bRegistered )
   349 		return;
   350 
   351 	s_pAccessor->RegisterConCommandBase( this );
   352 
   353 	m_bRegistered = true;
   354 }
   355 
   356 //-----------------------------------------------------------------------------
   357 // Purpose: 
   358 // Input  : *name - 
   359 // Output : ConCommandBase
   360 //-----------------------------------------------------------------------------
   361 ConCommandBase const *ConCommandBase::FindCommand( char const *name )
   362 {
   363 	ConCommandBase const *cmd = GetCommands();
   364 	for ( ; cmd; cmd = cmd->GetNext() )
   365 	{
   366 		if ( !stricmp( name, cmd->GetName() ) )
   367 			return cmd;
   368 	}
   369 	return NULL;
   370 }
   371 
   372 //-----------------------------------------------------------------------------
   373 // Purpose: 
   374 // Output : ConCommandBase
   375 //-----------------------------------------------------------------------------
   376 const ConCommandBase *ConCommandBase::GetCommands( void )
   377 {
   378 	return s_pConCommandBases;
   379 }
   380 
   381 //-----------------------------------------------------------------------------
   382 // Purpose: 
   383 // Input  : *var - 
   384 // Output : static
   385 //-----------------------------------------------------------------------------
   386 void ConCommandBase::AddToList( ConCommandBase *var )
   387 {
   388 	// This routine is only valid on root ConCommandBases
   389 	var->m_pNext = s_pConCommandBases;
   390 	s_pConCommandBases = var;
   391 }
   392 
   393 //-----------------------------------------------------------------------------
   394 // Purpose: Return name of the command/var
   395 // Output : char const
   396 //-----------------------------------------------------------------------------
   397 char const *ConCommandBase::GetName( void ) const
   398 {
   399 	return m_pszName;
   400 }
   401 
   402 //-----------------------------------------------------------------------------
   403 // Purpose: 
   404 // Input  : flag - 
   405 //-----------------------------------------------------------------------------
   406 void ConCommandBase::RemoveFlaggedCommands( int flag )
   407 {
   408 	ConCommandBase	*pNewList;
   409 	ConCommandBase  *pCommand, *pNext;
   410 
   411 	pNewList = NULL;
   412 
   413 	pCommand = s_pConCommandBases;
   414 	while ( pCommand )
   415 	{
   416 		pNext = pCommand->m_pNext;
   417 		if ( !( pCommand->m_nFlags & flag ) )
   418 		{
   419 			pCommand->m_pNext = pNewList;
   420 			pNewList = pCommand;
   421 		}
   422 		else
   423 		{
   424 			// Unlink
   425 			pCommand->m_pNext = NULL;
   426 		}
   427 
   428 		pCommand = pNext;
   429 	}
   430 	
   431 	s_pConCommandBases = pNewList;
   432 }
   433 
   434 void ConCommandBase::RevertFlaggedCvars( int flag )
   435 {
   436 	for (const ConCommandBase *var= GetCommands() ; var ; var=var->GetNext())
   437 	{
   438 		if ( var->IsCommand() )
   439 			continue;
   440 
   441 		ConVar *cvar = ( ConVar * )var;
   442 
   443 		if ( !cvar->IsBitSet( flag ) )
   444 			continue;
   445 
   446 		// It's == to the default value, don't count
   447 		if ( !Q_strcasecmp( cvar->GetDefault(), cvar->GetString() ) )
   448 			continue;
   449 
   450 		cvar->Revert();
   451 
   452 		// DevMsg( "%s = \"%s\" (reverted)\n", cvar->GetName(), cvar->GetString() );
   453 	}
   454 }
   455 
   456 //-----------------------------------------------------------------------------
   457 // Purpose: 
   458 // Input  : flag - 
   459 // Output : Returns true on success, false on failure.
   460 //-----------------------------------------------------------------------------
   461 bool ConCommandBase::IsBitSet( int flag ) const
   462 {
   463 	return ( flag & m_nFlags ) ? true : false;
   464 }
   465 
   466 //-----------------------------------------------------------------------------
   467 // Purpose: 
   468 // Input  : flags - 
   469 //-----------------------------------------------------------------------------
   470 void ConCommandBase::AddFlags( int flags )
   471 {
   472 	m_nFlags |= flags;
   473 }
   474 
   475 //-----------------------------------------------------------------------------
   476 // Purpose: 
   477 // Output : const ConCommandBase
   478 //-----------------------------------------------------------------------------
   479 const ConCommandBase *ConCommandBase::GetNext( void ) const
   480 {
   481 	return m_pNext;
   482 }
   483 
   484 //-----------------------------------------------------------------------------
   485 // Purpose: 
   486 // Input  : *next - 
   487 //-----------------------------------------------------------------------------
   488 void ConCommandBase::SetNext( ConCommandBase *next )
   489 {
   490 	m_pNext = next;
   491 }
   492 
   493 //-----------------------------------------------------------------------------
   494 // Purpose: Copies string using local new/delete operators
   495 // Input  : *from - 
   496 // Output : char
   497 //-----------------------------------------------------------------------------
   498 char *ConCommandBase::CopyString( char const *from )
   499 {
   500 	int		len;
   501 	char	*to;
   502 
   503 	len = strlen( from );
   504 	if ( len <= 0 )
   505 	{
   506 		to = new char[1];
   507 		to[0] = 0;
   508 	}
   509 	else
   510 	{
   511 		to = new char[len+1];
   512 		Q_strncpy( to, from, len+1 );
   513 	}
   514 	return to;
   515 }
   516 
   517 //-----------------------------------------------------------------------------
   518 // Purpose: 
   519 // Output : char const
   520 //-----------------------------------------------------------------------------
   521 char const *ConCommandBase::GetHelpText( void ) const
   522 {
   523 	return m_pszHelpString;
   524 }
   525 
   526 //-----------------------------------------------------------------------------
   527 // Purpose: Has this cvar been registered
   528 // Output : Returns true on success, false on failure.
   529 //-----------------------------------------------------------------------------
   530 bool ConCommandBase::IsRegistered( void ) const
   531 {
   532 	return m_bRegistered;
   533 }
   534 
   535 //-----------------------------------------------------------------------------
   536 // Purpose: Constructs a console command
   537 // Input  : *pName - name of command
   538 //			callback - function to call upon execution
   539 //			*pHelpString - help text for command
   540 //			flags - command flags, if any
   541 //-----------------------------------------------------------------------------
   542 ConCommand::ConCommand( char const *pName, FnCommandCallback callback, char const *pHelpString /*= 0*/, int flags /*= 0*/, FnCommandCompletionCallback completionFunc /*= 0*/ )
   543 {
   544 	Create( pName, callback, pHelpString, flags, completionFunc );
   545 }
   546 
   547 //-----------------------------------------------------------------------------
   548 // Purpose: 
   549 //-----------------------------------------------------------------------------
   550 ConCommand::~ConCommand( void )
   551 {
   552 }
   553 
   554 //-----------------------------------------------------------------------------
   555 // Purpose: 
   556 // Output : Returns true on success, false on failure.
   557 //-----------------------------------------------------------------------------
   558 bool ConCommand::IsCommand( void ) const
   559 { 
   560 	return true;
   561 }
   562 
   563 //-----------------------------------------------------------------------------
   564 // Purpose: Invoke the function if there is one
   565 //-----------------------------------------------------------------------------
   566 void ConCommand::Dispatch( void )
   567 {
   568 	if ( m_fnCommandCallback )
   569 	{
   570 		( *m_fnCommandCallback )();
   571 	}
   572 	else
   573 	{
   574 		// Command without callback!!!
   575 		Assert( 0 );
   576 	}
   577 }
   578 
   579 //-----------------------------------------------------------------------------
   580 // Purpose: 
   581 // Input  : *partial - 
   582 //			context - 
   583 //			longest - 
   584 //			maxcommands - 
   585 //			**commands - 
   586 // Output : int
   587 //-----------------------------------------------------------------------------
   588 int DefaultCompletionFunc( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
   589 {
   590 	return 0;
   591 }
   592 
   593 //-----------------------------------------------------------------------------
   594 // Purpose: Create the named command
   595 // Input  : *pName - 
   596 //			callback - 
   597 //			*pHelpString - 
   598 //			flags - 
   599 //-----------------------------------------------------------------------------
   600 void ConCommand::Create( char const *pName, FnCommandCallback callback, char const *pHelpString /*= 0*/, int flags /*= 0*/, FnCommandCompletionCallback completionFunc /*=0*/ )
   601 {
   602 	// Set the callback
   603 	m_fnCommandCallback = callback;
   604 
   605 	m_fnCompletionCallback = completionFunc ? completionFunc : DefaultCompletionFunc;
   606 	m_bHasCompletionCallback = completionFunc != 0 ? true : false;
   607 
   608 	// Setup the rest
   609 	BaseClass::Create( pName, pHelpString, flags );
   610 }
   611 
   612 //-----------------------------------------------------------------------------
   613 // Purpose: 
   614 // Input  : *partial - 
   615 //			context - 
   616 //			longest - 
   617 //			maxcommands - 
   618 //			**commands - 
   619 //-----------------------------------------------------------------------------
   620 int	ConCommand::AutoCompleteSuggest( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] )
   621 {
   622 	Assert( m_fnCompletionCallback );
   623 	if ( !m_fnCompletionCallback )
   624 		return 0;
   625 
   626 	return ( m_fnCompletionCallback )( partial, commands );
   627 }
   628 
   629 //-----------------------------------------------------------------------------
   630 // Purpose: 
   631 // Output : Returns true on success, false on failure.
   632 //-----------------------------------------------------------------------------
   633 bool ConCommand::CanAutoComplete( void )
   634 {
   635 	return m_bHasCompletionCallback;
   636 }
   637 
   638 // ----------------------------------------------------------------------------- //
   639 // ConVar.
   640 // ----------------------------------------------------------------------------- //
   641 ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags /* = 0 */ )
   642 {
   643 	Create( pName, pDefaultValue, flags );
   644 }
   645 
   646 // ----------------------------------------------------------------------------- //
   647 // ConVar.
   648 // ----------------------------------------------------------------------------- //
   649 ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString )
   650 {
   651 	Create( pName, pDefaultValue, flags, pHelpString );
   652 }
   653 
   654 // ----------------------------------------------------------------------------- //
   655 // ConVar.
   656 // ----------------------------------------------------------------------------- //
   657 ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax )
   658 {
   659 	Create( pName, pDefaultValue, flags, pHelpString, bMin, fMin, bMax, fMax );
   660 }
   661 
   662 // ----------------------------------------------------------------------------- //
   663 // ConVar.
   664 // ----------------------------------------------------------------------------- //
   665 ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString, FnChangeCallback callback )
   666 {
   667 	Create( pName, pDefaultValue, flags, pHelpString, false, 0.0, false, 0.0, callback );
   668 }
   669 
   670 // ----------------------------------------------------------------------------- //
   671 // ConVar.
   672 // ----------------------------------------------------------------------------- //
   673 ConVar::ConVar( char const *pName, char const *pDefaultValue, int flags, char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax, FnChangeCallback callback )
   674 {
   675 	Create( pName, pDefaultValue, flags, pHelpString, bMin, fMin, bMax, fMax, callback );
   676 }
   677 
   678 //-----------------------------------------------------------------------------
   679 // Purpose: Free dynamic memory
   680 //-----------------------------------------------------------------------------
   681 ConVar::~ConVar( void )
   682 {
   683 	if ( m_pszString )
   684 	{
   685 		delete[] m_pszString;
   686 		m_pszString = NULL;
   687 	}
   688 }
   689 
   690 
   691 //-----------------------------------------------------------------------------
   692 // Install a change callback (there shouldn't already be one....)
   693 //-----------------------------------------------------------------------------
   694 
   695 void ConVar::InstallChangeCallback( FnChangeCallback callback )
   696 {
   697 	Assert( !m_fnChangeCallback || !callback );
   698 	m_fnChangeCallback = callback;
   699 
   700 	if (m_fnChangeCallback)
   701 	{
   702 		// Call it immediately to set the initial value...
   703 		m_fnChangeCallback( this, m_pszString );
   704 	}
   705 }
   706 
   707 bool ConVar::IsBitSet( int flag ) const
   708 {
   709 	return ( flag & m_pParent->m_nFlags ) ? true : false;
   710 }
   711 
   712 char const *ConVar::GetHelpText( void ) const
   713 {
   714 	return m_pParent->m_pszHelpString;
   715 }
   716 
   717 void ConVar::AddFlags( int flags )
   718 {
   719 	m_pParent->m_nFlags |= flags;
   720 }
   721 
   722 bool ConVar::IsRegistered( void ) const
   723 {
   724 	return m_pParent->m_bRegistered;
   725 }
   726 
   727 char const *ConVar::GetName( void ) const
   728 {
   729 	return m_pParent->m_pszName;
   730 }
   731 
   732 //-----------------------------------------------------------------------------
   733 // Purpose: 
   734 // Output : Returns true on success, false on failure.
   735 //-----------------------------------------------------------------------------
   736 bool ConVar::IsCommand( void ) const
   737 { 
   738 	return false;
   739 }
   740 
   741 //-----------------------------------------------------------------------------
   742 // Purpose: 
   743 // Input  : 
   744 //-----------------------------------------------------------------------------
   745 void ConVar::Init()
   746 {
   747 	BaseClass::Init();
   748 }
   749 
   750 //-----------------------------------------------------------------------------
   751 // Purpose: 
   752 // Input  : *value - 
   753 //-----------------------------------------------------------------------------
   754 void ConVar::InternalSetValue( char const *value )
   755 {
   756 	float fNewValue;
   757 	char  tempVal[ 32 ];
   758 	char  *val;
   759 
   760 	Assert(m_pParent == this); // Only valid for root convars.
   761 
   762 	val = (char *)value;
   763 	fNewValue = ( float )atof( value );
   764 
   765 	if ( ClampValue( fNewValue ) )
   766 	{
   767 		Q_snprintf( tempVal,sizeof(tempVal), "%f", fNewValue );
   768 		val = tempVal;
   769 	}
   770 	
   771 	// Redetermine value
   772 	m_fValue		= fNewValue;
   773 	m_nValue		= ( int )( m_fValue );
   774 
   775 	if ( !(m_nFlags & FCVAR_NEVER_AS_STRING) )
   776 		ChangeStringValue( val );
   777 }
   778 
   779 //-----------------------------------------------------------------------------
   780 // Purpose: 
   781 // Input  : *tempVal - 
   782 //-----------------------------------------------------------------------------
   783 void ConVar::ChangeStringValue( char const *tempVal )
   784 {
   785 	Assert( !( m_nFlags & FCVAR_NEVER_AS_STRING ) );
   786 
   787  	char* pszOldValue = (char*)stackalloc( m_StringLength );
   788 	memcpy( pszOldValue, m_pszString, m_StringLength );
   789 	
   790 	int len = Q_strlen(tempVal) + 1;
   791 
   792 	if ( len > m_StringLength)
   793 	{
   794 		if (m_pszString)
   795 			delete[] m_pszString;
   796 
   797 		m_pszString	= new char[len];
   798 		m_StringLength = len;
   799 	}
   800 
   801 	memcpy( m_pszString, tempVal, len );
   802 
   803 	// Invoke any necessary callback function
   804 	if ( m_fnChangeCallback )
   805 	{
   806 		m_fnChangeCallback( this, pszOldValue );
   807 	}
   808 
   809 	GetCVarIF()->CallGlobalChangeCallback( this, pszOldValue );
   810 
   811 	stackfree( pszOldValue );
   812 }
   813 
   814 //-----------------------------------------------------------------------------
   815 // Purpose: Check whether to clamp and then perform clamp
   816 // Input  : value - 
   817 // Output : Returns true if value changed
   818 //-----------------------------------------------------------------------------
   819 bool ConVar::ClampValue( float& value )
   820 {
   821 	if ( m_bHasMin && ( value < m_fMinVal ) )
   822 	{
   823 		value = m_fMinVal;
   824 		return true;
   825 	}
   826 	
   827 	if ( m_bHasMax && ( value > m_fMaxVal ) )
   828 	{
   829 		value = m_fMaxVal;
   830 		return true;
   831 	}
   832 
   833 	return false;
   834 }
   835 
   836 //-----------------------------------------------------------------------------
   837 // Purpose: 
   838 // Input  : *value - 
   839 //-----------------------------------------------------------------------------
   840 void ConVar::InternalSetFloatValue( float fNewValue )
   841 {
   842 	if ( fNewValue == m_fValue )
   843 		return;
   844 
   845 	Assert( m_pParent == this ); // Only valid for root convars.
   846 
   847 	// Check bounds
   848 	ClampValue( fNewValue );
   849 
   850 	// Redetermine value
   851 	m_fValue		= fNewValue;
   852 	m_nValue		= ( int )m_fValue;
   853 
   854 	if ( !( m_nFlags & FCVAR_NEVER_AS_STRING ) )
   855 	{
   856 		char tempVal[ 32 ];
   857 		Q_snprintf( tempVal, sizeof( tempVal), "%f", m_fValue );
   858 		ChangeStringValue( tempVal );
   859 	}
   860 	else
   861 	{
   862 		Assert( !m_fnChangeCallback );
   863 	}
   864 }
   865 
   866 //-----------------------------------------------------------------------------
   867 // Purpose: 
   868 // Input  : *value - 
   869 //-----------------------------------------------------------------------------
   870 void ConVar::InternalSetIntValue( int nValue )
   871 {
   872 	if ( nValue == m_nValue )
   873 		return;
   874 
   875 	Assert( m_pParent == this ); // Only valid for root convars.
   876 
   877 	float fValue = (float)nValue;
   878 	if ( ClampValue( fValue ) )
   879 	{
   880 		nValue = ( int )( fValue );
   881 	}
   882 
   883 	// Redetermine value
   884 	m_fValue		= fValue;
   885 	m_nValue		= nValue;
   886 
   887 	if ( !( m_nFlags & FCVAR_NEVER_AS_STRING ) )
   888 	{
   889 		char tempVal[ 32 ];
   890 		Q_snprintf( tempVal, sizeof( tempVal ), "%d", m_nValue );
   891 		ChangeStringValue( tempVal );
   892 	}
   893 	else
   894 	{
   895 		Assert( !m_fnChangeCallback );
   896 	}
   897 }
   898 
   899 //-----------------------------------------------------------------------------
   900 // Purpose: Private creation
   901 //-----------------------------------------------------------------------------
   902 void ConVar::Create( char const *pName, char const *pDefaultValue, int flags /*= 0*/,
   903 	char const *pHelpString /*= NULL*/, bool bMin /*= false*/, float fMin /*= 0.0*/,
   904 	bool bMax /*= false*/, float fMax /*= false*/, FnChangeCallback callback /*= NULL*/ )
   905 {
   906 	static char *empty_string = "";
   907 
   908 	m_pParent = this;
   909 
   910 	// Name should be static data
   911 	m_pszDefaultValue	= pDefaultValue ? pDefaultValue : empty_string;
   912 	Assert( pDefaultValue );
   913 
   914 	m_StringLength = strlen( m_pszDefaultValue ) + 1;
   915 	m_pszString = new char[m_StringLength];
   916 	memcpy( m_pszString, m_pszDefaultValue, m_StringLength );
   917 	
   918 	m_bHasMin = bMin;
   919 	m_fMinVal = fMin;
   920 	m_bHasMax = bMax;
   921 	m_fMaxVal = fMax;
   922 	
   923 	m_fnChangeCallback = callback;
   924 
   925 	m_fValue = ( float )atof( m_pszString );
   926 
   927 	// Bounds Check, should never happen, if it does, no big deal
   928 	if ( m_bHasMin && ( m_fValue < m_fMinVal ) )
   929 	{
   930 		Assert( 0 );
   931 	}
   932 
   933 	if ( m_bHasMax && ( m_fValue > m_fMaxVal ) )
   934 	{
   935 		Assert( 0 );
   936 	}
   937 
   938 	m_nValue = ( int )m_fValue;
   939 
   940 	BaseClass::Create( pName, pHelpString, flags );
   941 }
   942 
   943 //-----------------------------------------------------------------------------
   944 // Purpose: 
   945 // Input  : *value - 
   946 //-----------------------------------------------------------------------------
   947 void ConVar::SetValue(char const *value)
   948 {
   949 	ConVar *var = ( ConVar * )m_pParent;
   950 	var->InternalSetValue( value );
   951 }
   952 
   953 //-----------------------------------------------------------------------------
   954 // Purpose: 
   955 // Input  : value - 
   956 //-----------------------------------------------------------------------------
   957 void ConVar::SetValue( float value )
   958 {
   959 	ConVar *var = ( ConVar * )m_pParent;
   960 	var->InternalSetFloatValue( value );
   961 }
   962 
   963 //-----------------------------------------------------------------------------
   964 // Purpose: 
   965 // Input  : value - 
   966 //-----------------------------------------------------------------------------
   967 void ConVar::SetValue( int value )
   968 {
   969 	ConVar *var = ( ConVar * )m_pParent;
   970 	var->InternalSetIntValue( value );
   971 }
   972 
   973 //-----------------------------------------------------------------------------
   974 // Purpose: Reset to default value
   975 //-----------------------------------------------------------------------------
   976 void ConVar::Revert( void )
   977 {
   978 	// Force default value again
   979 	ConVar *var = ( ConVar * )m_pParent;
   980 	var->SetValue( var->m_pszDefaultValue );
   981 }
   982 
   983 //-----------------------------------------------------------------------------
   984 // Purpose: 
   985 //-----------------------------------------------------------------------------
   986 void ConVar::RevertAll( void )
   987 {
   988 	ConCommandBase *p = s_pConCommandBases;
   989 	while ( p  )
   990 	{
   991 		if ( !p->IsCommand() )
   992 		{
   993 			ConVar *var = ( ConVar * )p;
   994 			var->Revert();
   995 		}
   996 		p = p->m_pNext;
   997 	}
   998 }
   999 
  1000 //-----------------------------------------------------------------------------
  1001 // Purpose: 
  1002 // Input  : minVal - 
  1003 // Output : true if there is a min set
  1004 //-----------------------------------------------------------------------------
  1005 bool ConVar::GetMin( float& minVal ) const
  1006 {
  1007 	minVal = m_pParent->m_fMinVal;
  1008 	return m_pParent->m_bHasMin;
  1009 }
  1010 
  1011 //-----------------------------------------------------------------------------
  1012 // Purpose: 
  1013 // Input  : maxVal - 
  1014 //-----------------------------------------------------------------------------
  1015 bool ConVar::GetMax( float& maxVal ) const
  1016 {
  1017 	maxVal = m_pParent->m_fMaxVal;
  1018 	return m_pParent->m_bHasMax;
  1019 }
  1020 
  1021 //-----------------------------------------------------------------------------
  1022 // Purpose: 
  1023 // Output : char const
  1024 //-----------------------------------------------------------------------------
  1025 char const *ConVar::GetDefault( void ) const
  1026 {
  1027 	return m_pParent->m_pszDefaultValue;
  1028 }
  1029