core/convar_sm_swarm.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3518 5ee88a5a8bb5
child 4144 353e86f4c6ab
permissions -rw-r--r--
Updated some CS:GO gamedata.
     1 //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
     2 //
     3 // Purpose: 
     4 //
     5 // $Workfile:     $
     6 // $Date:         $
     7 //
     8 //-----------------------------------------------------------------------------
     9 // $NoKeywords: $
    10 //===========================================================================//
    11 
    12 #ifndef CONVAR_H
    13 #define CONVAR_H
    14 
    15 #if _WIN32
    16 #pragma once
    17 #endif
    18 
    19 #include "tier0/dbg.h"
    20 #include "tier1/iconvar.h"
    21 #include "tier1/utlvector.h"
    22 #include "tier1/utlstring.h"
    23 #include "Color.h"
    24 #include "icvar.h"
    25 
    26 #ifdef _WIN32
    27 #define FORCEINLINE_CVAR FORCEINLINE
    28 #elif POSIX
    29 #define FORCEINLINE_CVAR inline
    30 #else
    31 #error "implement me"
    32 #endif
    33 
    34 
    35 //-----------------------------------------------------------------------------
    36 // Uncomment me to test for threading issues for material system convars
    37 // NOTE: You want to disable all threading when you do this
    38 // +host_thread_mode 0 +r_threaded_particles 0 +sv_parallel_packentities 0 +sv_disable_querycache 0
    39 //-----------------------------------------------------------------------------
    40 //#define CONVAR_TEST_MATERIAL_THREAD_CONVARS 1
    41 
    42 
    43 //-----------------------------------------------------------------------------
    44 // Forward declarations
    45 //-----------------------------------------------------------------------------
    46 class ConVar;
    47 class CCommand;
    48 class ConCommand;
    49 class ConCommandBase;
    50 struct characterset_t;
    51 
    52 
    53 
    54 //-----------------------------------------------------------------------------
    55 // Any executable that wants to use ConVars need to implement one of
    56 // these to hook up access to console variables.
    57 //-----------------------------------------------------------------------------
    58 class IConCommandBaseAccessor
    59 {
    60 public:
    61 	// Flags is a combination of FCVAR flags in cvar.h.
    62 	// hOut is filled in with a handle to the variable.
    63 	virtual bool RegisterConCommandBase( ConCommandBase *pVar ) = 0;
    64 };
    65 
    66 
    67 //-----------------------------------------------------------------------------
    68 // Helper method for console development
    69 //-----------------------------------------------------------------------------
    70 #if defined( _X360 )
    71 void ConVar_PublishToVXConsole();
    72 #endif
    73 
    74 
    75 //-----------------------------------------------------------------------------
    76 // Called when a ConCommand needs to execute
    77 //-----------------------------------------------------------------------------
    78 typedef void ( *FnCommandCallbackV1_t )( void );
    79 typedef void ( *FnCommandCallback_t )( const CCommand &command );
    80 
    81 #define COMMAND_COMPLETION_MAXITEMS		64
    82 #define COMMAND_COMPLETION_ITEM_LENGTH	64
    83 
    84 //-----------------------------------------------------------------------------
    85 // Returns 0 to COMMAND_COMPLETION_MAXITEMS worth of completion strings
    86 //-----------------------------------------------------------------------------
    87 typedef int  ( *FnCommandCompletionCallback )( const char *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] );
    88 
    89 
    90 //-----------------------------------------------------------------------------
    91 // Interface version
    92 //-----------------------------------------------------------------------------
    93 class ICommandCallback
    94 {
    95 public:
    96 	virtual void CommandCallback( const CCommand &command ) = 0;
    97 };
    98 
    99 class ICommandCompletionCallback
   100 {
   101 public:
   102 	virtual int  CommandCompletionCallback( const char *pPartial, CUtlVector< CUtlString > &commands ) = 0;
   103 };
   104 
   105 //-----------------------------------------------------------------------------
   106 // Purpose: The base console invoked command/cvar interface
   107 //-----------------------------------------------------------------------------
   108 class ConCommandBase
   109 {
   110 	friend class CCvar;
   111 	friend class ConVar;
   112 	friend class ConCommand;
   113 	friend void ConVar_Register( int nCVarFlag, IConCommandBaseAccessor *pAccessor );
   114 	friend void ConVar_PublishToVXConsole();
   115 
   116 	// FIXME: Remove when ConVar changes are done
   117 	friend class CDefaultCvar;
   118 
   119 public:
   120 								ConCommandBase( void );
   121 								ConCommandBase( const char *pName, const char *pHelpString = 0, 
   122 									int flags = 0 );
   123 
   124 	virtual						~ConCommandBase( void );
   125 
   126 	virtual	bool				IsCommand( void ) const;
   127 
   128 	// Check flag
   129 	virtual bool				IsFlagSet( int flag ) const;
   130 	// Set flag
   131 	virtual void				AddFlags( int flags );
   132 	// Clear flag
   133 	virtual void				RemoveFlags( int flags );
   134 
   135 	virtual int					GetFlags() const;
   136 
   137 	// Return name of cvar
   138 	virtual const char			*GetName( void ) const;
   139 
   140 	// Return help text for cvar
   141 	virtual const char			*GetHelpText( void ) const;
   142 
   143 	// Deal with next pointer
   144 	const ConCommandBase		*GetNext( void ) const;
   145 	ConCommandBase				*GetNext( void );
   146 	
   147 	virtual bool				IsRegistered( void ) const;
   148 
   149 	// Returns the DLL identifier
   150 	virtual CVarDLLIdentifier_t	GetDLLIdentifier() const;
   151 
   152 protected:
   153 	virtual void				Create( const char *pName, const char *pHelpString = 0, 
   154 									int flags = 0 );
   155 
   156 	// Used internally by OneTimeInit to initialize/shutdown
   157 	virtual void				Init();
   158 	void						Shutdown();
   159 
   160 	// Internal copy routine ( uses new operator from correct module )
   161 	char						*CopyString( const char *from );
   162 
   163 private:
   164 	// Next ConVar in chain
   165 	// Prior to register, it points to the next convar in the DLL.
   166 	// Once registered, though, m_pNext is reset to point to the next
   167 	// convar in the global list
   168 	ConCommandBase				*m_pNext;
   169 
   170 	// Has the cvar been added to the global list?
   171 	bool						m_bRegistered;
   172 
   173 	// Static data
   174 	const char 					*m_pszName;
   175 	const char 					*m_pszHelpString;
   176 	
   177 	// ConVar flags
   178 	int							m_nFlags;
   179 
   180 protected:
   181 	// ConVars add themselves to this list for the executable. 
   182 	// Then ConVar_Register runs through  all the console variables 
   183 	// and registers them into a global list stored in vstdlib.dll
   184 	static ConCommandBase		*s_pConCommandBases;
   185 
   186 	// ConVars in this executable use this 'global' to access values.
   187 	static IConCommandBaseAccessor	*s_pAccessor;
   188 public:
   189 	inline void SetFlags(int flags)	
   190 	{ 
   191 		m_nFlags = flags; 
   192 	}
   193 };
   194 
   195 
   196 //-----------------------------------------------------------------------------
   197 // Command tokenizer
   198 //-----------------------------------------------------------------------------
   199 class CCommand
   200 {
   201 public:
   202 	CCommand();
   203 	CCommand( int nArgC, const char **ppArgV );
   204 	bool Tokenize( const char *pCommand, characterset_t *pBreakSet = NULL );
   205 	void Reset();
   206 
   207 	int ArgC() const;
   208 	const char **ArgV() const;
   209 	const char *ArgS() const;					// All args that occur after the 0th arg, in string form
   210 	const char *GetCommandString() const;		// The entire command in string form, including the 0th arg
   211 	const char *operator[]( int nIndex ) const;	// Gets at arguments
   212 	const char *Arg( int nIndex ) const;		// Gets at arguments
   213 	
   214 	// Helper functions to parse arguments to commands.
   215 	const char* FindArg( const char *pName ) const;
   216 	int FindArgInt( const char *pName, int nDefaultVal ) const;
   217 
   218 	static int MaxCommandLength();
   219 	static characterset_t* DefaultBreakSet();
   220 
   221 private:
   222 	enum
   223 	{
   224 		COMMAND_MAX_ARGC = 64,
   225 		COMMAND_MAX_LENGTH = 512,
   226 	};
   227 
   228 	int		m_nArgc;
   229 	int		m_nArgv0Size;
   230 	char	m_pArgSBuffer[ COMMAND_MAX_LENGTH ];
   231 	char	m_pArgvBuffer[ COMMAND_MAX_LENGTH ];
   232 	const char*	m_ppArgv[ COMMAND_MAX_ARGC ];
   233 };
   234 
   235 inline int CCommand::MaxCommandLength()
   236 {
   237 	return COMMAND_MAX_LENGTH - 1;
   238 }
   239 
   240 inline int CCommand::ArgC() const
   241 {
   242 	return m_nArgc;
   243 }
   244 
   245 inline const char **CCommand::ArgV() const
   246 {
   247 	return m_nArgc ? (const char**)m_ppArgv : NULL;
   248 }
   249 
   250 inline const char *CCommand::ArgS() const
   251 {
   252 	return m_nArgv0Size ? &m_pArgSBuffer[m_nArgv0Size] : "";
   253 }
   254 
   255 inline const char *CCommand::GetCommandString() const
   256 {
   257 	return m_nArgc ? m_pArgSBuffer : "";
   258 }
   259 
   260 inline const char *CCommand::Arg( int nIndex ) const
   261 {
   262 	// FIXME: Many command handlers appear to not be particularly careful
   263 	// about checking for valid argc range. For now, we're going to
   264 	// do the extra check and return an empty string if it's out of range
   265 	if ( nIndex < 0 || nIndex >= m_nArgc )
   266 		return "";
   267 	return m_ppArgv[nIndex];
   268 }
   269 
   270 inline const char *CCommand::operator[]( int nIndex ) const
   271 {
   272 	return Arg( nIndex );
   273 }
   274 
   275 
   276 //-----------------------------------------------------------------------------
   277 // Purpose: The console invoked command
   278 //-----------------------------------------------------------------------------
   279 class ConCommand : public ConCommandBase
   280 {
   281 friend class CCvar;
   282 
   283 public:
   284 	typedef ConCommandBase BaseClass;
   285 
   286 	ConCommand( const char *pName, FnCommandCallbackV1_t callback, 
   287 		const char *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
   288 	ConCommand( const char *pName, FnCommandCallback_t callback, 
   289 		const char *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
   290 	ConCommand( const char *pName, ICommandCallback *pCallback, 
   291 		const char *pHelpString = 0, int flags = 0, ICommandCompletionCallback *pCommandCompletionCallback = 0 );
   292 
   293 	virtual ~ConCommand( void );
   294 
   295 	virtual	bool IsCommand( void ) const;
   296 
   297 	virtual int AutoCompleteSuggest( const char *partial, CUtlVector< CUtlString > &commands );
   298 
   299 	virtual bool CanAutoComplete( void );
   300 
   301 	// Invoke the function
   302 	virtual void Dispatch( const CCommand &command );
   303 
   304 private:
   305 	// NOTE: To maintain backward compat, we have to be very careful:
   306 	// All public virtual methods must appear in the same order always
   307 	// since engine code will be calling into this code, which *does not match*
   308 	// in the mod code; it's using slightly different, but compatible versions
   309 	// of this class. Also: Be very careful about adding new fields to this class.
   310 	// Those fields will not exist in the version of this class that is instanced
   311 	// in mod code.
   312 
   313 	// Call this function when executing the command
   314 	union
   315 	{
   316 		FnCommandCallbackV1_t m_fnCommandCallbackV1;
   317 		FnCommandCallback_t m_fnCommandCallback;
   318 		ICommandCallback *m_pCommandCallback; 
   319 	};
   320 
   321 	union
   322 	{
   323 		FnCommandCompletionCallback	m_fnCompletionCallback;
   324 		ICommandCompletionCallback *m_pCommandCompletionCallback;
   325 	};
   326 
   327 	bool m_bHasCompletionCallback : 1;
   328 	bool m_bUsingNewCommandCallback : 1;
   329 	bool m_bUsingCommandCallbackInterface : 1;
   330 public:
   331 	inline FnCommandCallback_t GetCallback() const
   332 	{ 
   333 		return m_fnCommandCallback; 
   334 	}
   335 };
   336 
   337 
   338 //-----------------------------------------------------------------------------
   339 // Purpose: A console variable
   340 //-----------------------------------------------------------------------------
   341 class ConVar : public ConCommandBase, public IConVar
   342 {
   343 friend class CCvar;
   344 friend class ConVarRef;
   345 friend class SplitScreenConVarRef;
   346 
   347 public:
   348 	typedef ConCommandBase BaseClass;
   349 
   350 								ConVar( const char *pName, const char *pDefaultValue, int flags = 0);
   351 
   352 								ConVar( const char *pName, const char *pDefaultValue, int flags, 
   353 									const char *pHelpString );
   354 								ConVar( const char *pName, const char *pDefaultValue, int flags, 
   355 									const char *pHelpString, bool bMin, float fMin, bool bMax, float fMax );
   356 								ConVar( const char *pName, const char *pDefaultValue, int flags, 
   357 									const char *pHelpString, FnChangeCallback_t callback );
   358 								ConVar( const char *pName, const char *pDefaultValue, int flags, 
   359 									const char *pHelpString, bool bMin, float fMin, bool bMax, float fMax,
   360 									FnChangeCallback_t callback );
   361 
   362 	virtual						~ConVar( void );
   363 
   364 	virtual bool				IsFlagSet( int flag ) const;
   365 	virtual const char*			GetHelpText( void ) const;
   366 	virtual bool				IsRegistered( void ) const;
   367 	virtual const char			*GetName( void ) const;
   368 	// Return name of command (usually == GetName(), except in case of FCVAR_SS_ADDED vars
   369 	virtual const char			*GetBaseName( void ) const;
   370 	virtual int					GetSplitScreenPlayerSlot() const;
   371 
   372 	virtual void				AddFlags( int flags );
   373 	virtual int					GetFlags() const;
   374 	virtual	bool				IsCommand( void ) const;
   375 
   376 	// Install a change callback (there shouldn't already be one....)
   377 	void InstallChangeCallback( FnChangeCallback_t callback, bool bInvoke = true );
   378 	void RemoveChangeCallback( FnChangeCallback_t callbackToRemove );
   379 
   380 	int GetChangeCallbackCount() const { return m_pParent->m_fnChangeCallbacks.Count(); }
   381 	FnChangeCallback_t GetChangeCallback( int slot ) const { return m_pParent->m_fnChangeCallbacks[ slot ]; }
   382 
   383 	// Retrieve value
   384 #if SOURCE_ENGINE == SE_CSGO
   385 	virtual float					GetFloat( void ) const;
   386 	virtual int						GetInt( void ) const;
   387 #else
   388 	FORCEINLINE_CVAR float			GetFloat( void ) const;
   389 	FORCEINLINE_CVAR int			GetInt( void ) const;
   390 #endif
   391 
   392 	FORCEINLINE_CVAR Color			GetColor( void ) const;
   393 	FORCEINLINE_CVAR bool			GetBool() const {  return !!GetInt(); }
   394 	FORCEINLINE_CVAR char const	   *GetString( void ) const;
   395 
   396 	// Compiler driven selection for template use
   397 	template <typename T> T Get( void ) const;
   398 	template <typename T> T Get( T * ) const;
   399 
   400 	// Any function that allocates/frees memory needs to be virtual or else you'll have crashes
   401 	//  from alloc/free across dll/exe boundaries.
   402 	
   403 	// These just call into the IConCommandBaseAccessor to check flags and set the var (which ends up calling InternalSetValue).
   404 	virtual void				SetValue( const char *value );
   405 	virtual void				SetValue( float value );
   406 	virtual void				SetValue( int value );
   407 	virtual void				SetValue( Color value );
   408 	
   409 	// Reset to default value
   410 	void						Revert( void );
   411 
   412 	// True if it has a min/max setting
   413 	bool						HasMin() const;
   414 	bool						HasMax() const;
   415 
   416 	bool						GetMin( float& minVal ) const;
   417 	bool						GetMax( float& maxVal ) const;
   418 
   419 	float						GetMinValue() const;
   420 	float						GetMaxValue() const;
   421 
   422 	const char					*GetDefault( void ) const;
   423 
   424 	// Value
   425 	struct CVValue_t
   426 	{
   427 		char						*m_pszString;
   428 		int							m_StringLength;
   429 
   430 		// Values
   431 		float						m_fValue;
   432 		int							m_nValue;
   433 	};
   434 
   435 	FORCEINLINE_CVAR CVValue_t &GetRawValue()
   436 	{
   437 		return m_Value;
   438 	}
   439 	FORCEINLINE_CVAR const CVValue_t &GetRawValue() const
   440 	{
   441 		return m_Value;
   442 	}
   443 
   444 private:
   445 	bool						InternalSetColorFromString( const char *value );
   446 	// Called by CCvar when the value of a var is changing.
   447 	virtual void				InternalSetValue(const char *value);
   448 	// For CVARs marked FCVAR_NEVER_AS_STRING
   449 	virtual void				InternalSetFloatValue( float fNewValue );
   450 	virtual void				InternalSetIntValue( int nValue );
   451 	virtual void				InternalSetColorValue( Color value );
   452 
   453 	virtual bool				ClampValue( float& value );
   454 	virtual void				ChangeStringValue( const char *tempVal, float flOldValue );
   455 
   456 	virtual void				Create( const char *pName, const char *pDefaultValue, int flags = 0,
   457 									const char *pHelpString = 0, bool bMin = false, float fMin = 0.0,
   458 									bool bMax = false, float fMax = false, FnChangeCallback_t callback = 0 );
   459 
   460 	// Used internally by OneTimeInit to initialize.
   461 	virtual void				Init();
   462 
   463 
   464 
   465 protected:
   466 
   467 	// This either points to "this" or it points to the original declaration of a ConVar.
   468 	// This allows ConVars to exist in separate modules, and they all use the first one to be declared.
   469 	// m_pParent->m_pParent must equal m_pParent (ie: m_pParent must be the root, or original, ConVar).
   470 	ConVar						*m_pParent;
   471 
   472 	// Static data
   473 	const char					*m_pszDefaultValue;
   474 	
   475 	CVValue_t					m_Value;
   476 
   477 	// Min/Max values
   478 	bool						m_bHasMin;
   479 	float						m_fMinVal;
   480 	bool						m_bHasMax;
   481 	float						m_fMaxVal;
   482 	
   483 	// Call this function when ConVar changes
   484 	CUtlVector< FnChangeCallback_t > m_fnChangeCallbacks;
   485 public:
   486 	inline void SetMin(bool set, float min=0.0)
   487 	{ 
   488 		m_bHasMin = set; 
   489 		m_fMinVal = min; 
   490 	}
   491 	inline void SetMax(bool set, float max=0.0)
   492 	{
   493 		m_bHasMax = set;
   494 		m_fMaxVal = max; 
   495 	}
   496 };
   497 
   498 
   499 //-----------------------------------------------------------------------------
   500 // Purpose: Return ConVar value as a float
   501 // Output : float
   502 //-----------------------------------------------------------------------------
   503 FORCEINLINE_CVAR float ConVar::GetFloat( void ) const
   504 {
   505 #ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
   506 	Assert( ThreadInMainThread() || IsFlagSet( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) );
   507 #endif
   508 	return m_pParent->m_Value.m_fValue;
   509 }
   510 
   511 //-----------------------------------------------------------------------------
   512 // Purpose: Return ConVar value as an int
   513 // Output : int
   514 //-----------------------------------------------------------------------------
   515 FORCEINLINE_CVAR int ConVar::GetInt( void ) const 
   516 {
   517 #ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
   518 	Assert( ThreadInMainThread() || IsFlagSet( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) );
   519 #endif
   520 	return m_pParent->m_Value.m_nValue;
   521 }
   522 
   523 //-----------------------------------------------------------------------------
   524 // Purpose: Return ConVar value as a color
   525 // Output : Color
   526 //-----------------------------------------------------------------------------
   527 FORCEINLINE_CVAR Color ConVar::GetColor( void ) const 
   528 {
   529 #ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
   530 	Assert( ThreadInMainThread() || IsFlagSet( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) );
   531 #endif
   532 	unsigned char *pColorElement = ((unsigned char *)&m_pParent->m_Value.m_nValue);
   533 	return Color( pColorElement[0], pColorElement[1], pColorElement[2], pColorElement[3] );
   534 }
   535 
   536 
   537 //-----------------------------------------------------------------------------
   538 
   539 template <> FORCEINLINE_CVAR float			ConVar::Get<float>( void ) const		{ return GetFloat(); }
   540 template <> FORCEINLINE_CVAR int			ConVar::Get<int>( void ) const			{ return GetInt(); }
   541 template <> FORCEINLINE_CVAR bool			ConVar::Get<bool>( void ) const			{ return GetBool(); }
   542 template <> FORCEINLINE_CVAR const char *	ConVar::Get<const char *>( void ) const	{ return GetString(); }
   543 template <> FORCEINLINE_CVAR float			ConVar::Get<float>( float *p ) const				{ return ( *p = GetFloat() ); }
   544 template <> FORCEINLINE_CVAR int			ConVar::Get<int>( int *p ) const					{ return ( *p = GetInt() ); }
   545 template <> FORCEINLINE_CVAR bool			ConVar::Get<bool>( bool *p ) const					{ return ( *p = GetBool() ); }
   546 template <> FORCEINLINE_CVAR const char *	ConVar::Get<const char *>( char const **p ) const	{ return ( *p = GetString() ); }
   547 
   548 //-----------------------------------------------------------------------------
   549 // Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
   550 // Output : const char *
   551 //-----------------------------------------------------------------------------
   552 FORCEINLINE_CVAR const char *ConVar::GetString( void ) const 
   553 {
   554 #ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
   555 	Assert( ThreadInMainThread() || IsFlagSet( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) );
   556 #endif
   557 	if ( m_nFlags & FCVAR_NEVER_AS_STRING )
   558 		return "FCVAR_NEVER_AS_STRING";
   559 	
   560 	char const *str = m_pParent->m_Value.m_pszString;
   561 	return str ? str : "";
   562 }
   563 
   564 class CSplitScreenAddedConVar : public ConVar
   565 {
   566 	typedef ConVar BaseClass;
   567 public:
   568 	CSplitScreenAddedConVar( int nSplitScreenSlot, const char *pName, const ConVar *pBaseVar ) :
   569 		BaseClass
   570 		( 
   571 			pName, 
   572 			pBaseVar->GetDefault(), 
   573 			// Keep basevar flags, except remove _SS and add _SS_ADDED instead
   574 			( pBaseVar->GetFlags() & ~FCVAR_SS ) | FCVAR_SS_ADDED, 
   575 			pBaseVar->GetHelpText(), 
   576 			pBaseVar->HasMin(),
   577 			pBaseVar->GetMinValue(),
   578 			pBaseVar->HasMax(),
   579 			pBaseVar->GetMaxValue()
   580 		),
   581 		m_pBaseVar( pBaseVar ),
   582 		m_nSplitScreenSlot( nSplitScreenSlot )
   583 	{
   584 		for ( int i = 0; i < pBaseVar->GetChangeCallbackCount(); ++i )
   585 		{
   586 			InstallChangeCallback( pBaseVar->GetChangeCallback( i ), false );
   587 		}
   588 		Assert( nSplitScreenSlot >= 1 );
   589 		Assert( nSplitScreenSlot < MAX_SPLITSCREEN_CLIENTS );
   590 		Assert( m_pBaseVar );
   591 		Assert( IsFlagSet( FCVAR_SS_ADDED ) );
   592 		Assert( !IsFlagSet( FCVAR_SS ) );
   593 	}
   594 
   595 	const ConVar *GetBaseVar() const;
   596 	virtual const char *GetBaseName() const;
   597 	void SetSplitScreenPlayerSlot( int nSlot );
   598 	virtual int GetSplitScreenPlayerSlot() const;
   599 
   600 protected:
   601 
   602 	const ConVar	*m_pBaseVar;
   603 	int		m_nSplitScreenSlot;
   604 };
   605 
   606 FORCEINLINE_CVAR const ConVar *CSplitScreenAddedConVar::GetBaseVar() const 
   607 { 
   608 	Assert( m_pBaseVar );
   609 	return m_pBaseVar; 
   610 }
   611 
   612 FORCEINLINE_CVAR const char *CSplitScreenAddedConVar::GetBaseName() const 
   613 { 
   614 	Assert( m_pBaseVar );
   615 	return m_pBaseVar->GetName(); 
   616 }
   617 
   618 FORCEINLINE_CVAR void CSplitScreenAddedConVar::SetSplitScreenPlayerSlot( int nSlot ) 
   619 { 
   620 	m_nSplitScreenSlot = nSlot; 
   621 }
   622 
   623 FORCEINLINE_CVAR int CSplitScreenAddedConVar::GetSplitScreenPlayerSlot() const 
   624 { 
   625 	return m_nSplitScreenSlot; 
   626 }
   627 
   628 //-----------------------------------------------------------------------------
   629 // Used to read/write convars that already exist (replaces the FindVar method)
   630 //-----------------------------------------------------------------------------
   631 class ConVarRef
   632 {
   633 public:
   634 	ConVarRef( const char *pName );
   635 	ConVarRef( const char *pName, bool bIgnoreMissing );
   636 	ConVarRef( IConVar *pConVar );
   637 
   638 	void Init( const char *pName, bool bIgnoreMissing );
   639 	bool IsValid() const;
   640 	bool IsFlagSet( int nFlags ) const;
   641 	IConVar *GetLinkedConVar();
   642 
   643 	// Get/Set value
   644 	float GetFloat( void ) const;
   645 	int GetInt( void ) const;
   646 	Color GetColor( void ) const;
   647 	bool GetBool() const { return !!GetInt(); }
   648 	const char *GetString( void ) const;
   649 
   650 	void SetValue( const char *pValue );
   651 	void SetValue( float flValue );
   652 	void SetValue( int nValue );
   653 	void SetValue( Color value );
   654 	void SetValue( bool bValue );
   655 
   656 	const char *GetName() const;
   657 
   658 	const char *GetDefault() const;
   659 
   660 	const char *GetBaseName() const;
   661 
   662 	int	GetSplitScreenPlayerSlot() const;
   663 
   664 private:
   665 	// High-speed method to read convar data
   666 	IConVar *m_pConVar;
   667 	ConVar *m_pConVarState;
   668 };
   669 
   670 
   671 //-----------------------------------------------------------------------------
   672 // Did we find an existing convar of that name?
   673 //-----------------------------------------------------------------------------
   674 FORCEINLINE_CVAR bool ConVarRef::IsFlagSet( int nFlags ) const
   675 {
   676 	return ( m_pConVar->IsFlagSet( nFlags ) != 0 );
   677 }
   678 
   679 FORCEINLINE_CVAR IConVar *ConVarRef::GetLinkedConVar()
   680 {
   681 	return m_pConVar;
   682 }
   683 
   684 FORCEINLINE_CVAR const char *ConVarRef::GetName() const
   685 {
   686 	return m_pConVar->GetName();
   687 }
   688 
   689 FORCEINLINE_CVAR const char *ConVarRef::GetBaseName() const
   690 {
   691 	return m_pConVar->GetBaseName();
   692 }
   693 
   694 FORCEINLINE_CVAR int ConVarRef::GetSplitScreenPlayerSlot() const
   695 {
   696 	return m_pConVar->GetSplitScreenPlayerSlot();
   697 }
   698 
   699 //-----------------------------------------------------------------------------
   700 // Purpose: Return ConVar value as a float
   701 //-----------------------------------------------------------------------------
   702 FORCEINLINE_CVAR float ConVarRef::GetFloat( void ) const
   703 {
   704 	return m_pConVarState->m_Value.m_fValue;
   705 }
   706 
   707 //-----------------------------------------------------------------------------
   708 // Purpose: Return ConVar value as an int
   709 //-----------------------------------------------------------------------------
   710 FORCEINLINE_CVAR int ConVarRef::GetInt( void ) const 
   711 {
   712 	return m_pConVarState->m_Value.m_nValue;
   713 }
   714 
   715 //-----------------------------------------------------------------------------
   716 // Purpose: Return ConVar value as a color
   717 //-----------------------------------------------------------------------------
   718 FORCEINLINE_CVAR Color ConVarRef::GetColor( void ) const 
   719 {
   720 	return m_pConVarState->GetColor();
   721 }
   722 
   723 //-----------------------------------------------------------------------------
   724 // Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
   725 //-----------------------------------------------------------------------------
   726 FORCEINLINE_CVAR const char *ConVarRef::GetString( void ) const 
   727 {
   728 	Assert( !IsFlagSet( FCVAR_NEVER_AS_STRING ) );
   729 	return m_pConVarState->m_Value.m_pszString;
   730 }
   731 
   732 
   733 FORCEINLINE_CVAR void ConVarRef::SetValue( const char *pValue )
   734 {
   735 	m_pConVar->SetValue( pValue );
   736 }
   737 
   738 FORCEINLINE_CVAR void ConVarRef::SetValue( float flValue )
   739 {
   740 	m_pConVar->SetValue( flValue );
   741 }
   742 
   743 FORCEINLINE_CVAR void ConVarRef::SetValue( int nValue )
   744 {
   745 	m_pConVar->SetValue( nValue );
   746 }
   747 
   748 FORCEINLINE_CVAR void ConVarRef::SetValue( Color value )
   749 {
   750 	m_pConVar->SetValue( value );
   751 }
   752 
   753 FORCEINLINE_CVAR void ConVarRef::SetValue( bool bValue )
   754 {
   755 	m_pConVar->SetValue( bValue ? 1 : 0 );
   756 }
   757 
   758 FORCEINLINE_CVAR const char *ConVarRef::GetDefault() const
   759 {
   760 	return m_pConVarState->m_pszDefaultValue;
   761 }
   762 
   763 //-----------------------------------------------------------------------------
   764 // Helper for referencing splitscreen convars (i.e., "name" and "name2")
   765 //-----------------------------------------------------------------------------
   766 class SplitScreenConVarRef
   767 {
   768 public:
   769 	SplitScreenConVarRef( const char *pName );
   770 	SplitScreenConVarRef( const char *pName, bool bIgnoreMissing );
   771 	SplitScreenConVarRef( IConVar *pConVar );
   772 
   773 	void Init( const char *pName, bool bIgnoreMissing );
   774 	bool IsValid() const;
   775 	bool IsFlagSet( int nFlags ) const;
   776 
   777 	// Get/Set value
   778 	float GetFloat( int nSlot ) const;
   779 	int GetInt( int nSlot ) const;
   780 	Color GetColor( int nSlot ) const;
   781 	bool GetBool( int nSlot ) const { return !!GetInt( nSlot ); }
   782 	const char *GetString( int nSlot  ) const;
   783 
   784 	void SetValue( int nSlot, const char *pValue );
   785 	void SetValue( int nSlot, float flValue );
   786 	void SetValue( int nSlot, int nValue );
   787 	void SetValue( int nSlot, Color value );
   788 	void SetValue( int nSlot, bool bValue );
   789 
   790 	const char *GetName( int nSlot ) const;
   791 
   792 	const char *GetDefault() const;
   793 
   794 	const char *GetBaseName() const;
   795 
   796 private:
   797 	struct cv_t
   798 	{
   799 		IConVar *m_pConVar;
   800 		ConVar *m_pConVarState;
   801 	};
   802 
   803 	cv_t	m_Info[ MAX_SPLITSCREEN_CLIENTS ];
   804 };
   805 
   806 //-----------------------------------------------------------------------------
   807 // Did we find an existing convar of that name?
   808 //-----------------------------------------------------------------------------
   809 FORCEINLINE_CVAR bool SplitScreenConVarRef::IsFlagSet( int nFlags ) const
   810 {
   811 	return ( m_Info[ 0 ].m_pConVar->IsFlagSet( nFlags ) != 0 );
   812 }
   813 
   814 FORCEINLINE_CVAR const char *SplitScreenConVarRef::GetName( int nSlot ) const
   815 {
   816 	return m_Info[ nSlot ].m_pConVar->GetName();
   817 }
   818 
   819 FORCEINLINE_CVAR const char *SplitScreenConVarRef::GetBaseName() const
   820 {
   821 	return m_Info[ 0 ].m_pConVar->GetBaseName();
   822 }
   823 
   824 //-----------------------------------------------------------------------------
   825 // Purpose: Return ConVar value as a float
   826 //-----------------------------------------------------------------------------
   827 FORCEINLINE_CVAR float SplitScreenConVarRef::GetFloat( int nSlot ) const
   828 {
   829 	return m_Info[ nSlot ].m_pConVarState->m_Value.m_fValue;
   830 }
   831 
   832 //-----------------------------------------------------------------------------
   833 // Purpose: Return ConVar value as an int
   834 //-----------------------------------------------------------------------------
   835 FORCEINLINE_CVAR int SplitScreenConVarRef::GetInt( int nSlot ) const 
   836 {
   837 	return m_Info[ nSlot ].m_pConVarState->m_Value.m_nValue;
   838 }
   839 
   840 //-----------------------------------------------------------------------------
   841 // Purpose: Return ConVar value as an int
   842 //-----------------------------------------------------------------------------
   843 FORCEINLINE_CVAR Color SplitScreenConVarRef::GetColor( int nSlot ) const 
   844 {
   845 	return m_Info[ nSlot ].m_pConVarState->GetColor();
   846 }
   847 
   848 //-----------------------------------------------------------------------------
   849 // Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
   850 //-----------------------------------------------------------------------------
   851 FORCEINLINE_CVAR const char *SplitScreenConVarRef::GetString( int nSlot ) const 
   852 {
   853 	Assert( !IsFlagSet( FCVAR_NEVER_AS_STRING ) );
   854 	return m_Info[ nSlot ].m_pConVarState->m_Value.m_pszString;
   855 }
   856 
   857 
   858 FORCEINLINE_CVAR void SplitScreenConVarRef::SetValue( int nSlot, const char *pValue )
   859 {
   860 	m_Info[ nSlot ].m_pConVar->SetValue( pValue );
   861 }
   862 
   863 FORCEINLINE_CVAR void SplitScreenConVarRef::SetValue( int nSlot, float flValue )
   864 {
   865 	m_Info[ nSlot ].m_pConVar->SetValue( flValue );
   866 }
   867 
   868 FORCEINLINE_CVAR void SplitScreenConVarRef::SetValue( int nSlot, int nValue )
   869 {
   870 	m_Info[ nSlot ].m_pConVar->SetValue( nValue );
   871 }
   872 
   873 FORCEINLINE_CVAR void SplitScreenConVarRef::SetValue( int nSlot, Color value )
   874 {
   875 	m_Info[ nSlot ].m_pConVar->SetValue( value );
   876 }
   877 
   878 FORCEINLINE_CVAR void SplitScreenConVarRef::SetValue( int nSlot, bool bValue )
   879 {
   880 	m_Info[ nSlot ].m_pConVar->SetValue( bValue ? 1 : 0 );
   881 }
   882 
   883 FORCEINLINE_CVAR const char *SplitScreenConVarRef::GetDefault() const
   884 {
   885 	return m_Info[ 0 ].m_pConVarState->m_pszDefaultValue;
   886 }
   887 
   888 //-----------------------------------------------------------------------------
   889 // Called by the framework to register ConCommands with the ICVar
   890 //-----------------------------------------------------------------------------
   891 void ConVar_Register( int nCVarFlag = 0, IConCommandBaseAccessor *pAccessor = NULL );
   892 void ConVar_Unregister( );
   893 
   894 
   895 //-----------------------------------------------------------------------------
   896 // Utility methods 
   897 //-----------------------------------------------------------------------------
   898 void ConVar_PrintDescription( const ConCommandBase *pVar );
   899 
   900 
   901 //-----------------------------------------------------------------------------
   902 // Purpose: Utility class to quickly allow ConCommands to call member methods
   903 //-----------------------------------------------------------------------------
   904 #ifdef _WIN32
   905 #pragma warning (disable : 4355 )
   906 #endif
   907 
   908 template< class T >
   909 class CConCommandMemberAccessor : public ConCommand, public ICommandCallback, public ICommandCompletionCallback
   910 {
   911 	typedef ConCommand BaseClass;
   912 	typedef void ( T::*FnMemberCommandCallback_t )( const CCommand &command );
   913 	typedef int  ( T::*FnMemberCommandCompletionCallback_t )( const char *pPartial, CUtlVector< CUtlString > &commands );
   914 
   915 public:
   916 	CConCommandMemberAccessor( T* pOwner, const char *pName, FnMemberCommandCallback_t callback, const char *pHelpString = 0,
   917 		int flags = 0, FnMemberCommandCompletionCallback_t completionFunc = 0 ) :
   918 		BaseClass( pName, this, pHelpString, flags, ( completionFunc != 0 ) ? this : NULL )
   919 	{
   920 		m_pOwner = pOwner;
   921 		m_Func = callback;
   922 		m_CompletionFunc = completionFunc;
   923 	}
   924 
   925 	~CConCommandMemberAccessor()
   926 	{
   927 		Shutdown();
   928 	}
   929 
   930 	void SetOwner( T* pOwner )
   931 	{
   932 		m_pOwner = pOwner;
   933 	}
   934 
   935 	virtual void CommandCallback( const CCommand &command )
   936 	{
   937 		Assert( m_pOwner && m_Func );
   938 		(m_pOwner->*m_Func)( command );
   939 	}
   940 
   941 	virtual int  CommandCompletionCallback( const char *pPartial, CUtlVector< CUtlString > &commands )
   942 	{
   943 		Assert( m_pOwner && m_CompletionFunc );
   944 		return (m_pOwner->*m_CompletionFunc)( pPartial, commands );
   945 	}
   946 
   947 private:
   948 	T* m_pOwner;
   949 	FnMemberCommandCallback_t m_Func;
   950 	FnMemberCommandCompletionCallback_t m_CompletionFunc;
   951 };
   952 
   953 #ifdef _WIN32
   954 #pragma warning ( default : 4355 )
   955 #endif
   956 
   957 
   958 //-----------------------------------------------------------------------------
   959 // Purpose: Utility macros to quicky generate a simple console command
   960 //-----------------------------------------------------------------------------
   961 #define CON_COMMAND( name, description ) \
   962    static void name( const CCommand &args ); \
   963    static ConCommand name##_command( #name, name, description ); \
   964    static void name( const CCommand &args )
   965 
   966 #ifdef CLIENT_DLL
   967 	#define CON_COMMAND_SHARED( name, description ) \
   968 		static void name( const CCommand &args ); \
   969 		static ConCommand name##_command_client( #name "_client", name, description ); \
   970 		static void name( const CCommand &args )
   971 #else
   972 	#define CON_COMMAND_SHARED( name, description ) \
   973 		static void name( const CCommand &args ); \
   974 		static ConCommand name##_command( #name, name, description ); \
   975 		static void name( const CCommand &args )
   976 #endif
   977 
   978 
   979 #define CON_COMMAND_F( name, description, flags ) \
   980 	static void name( const CCommand &args ); \
   981 	static ConCommand name##_command( #name, name, description, flags ); \
   982 	static void name( const CCommand &args )
   983 
   984 #ifdef CLIENT_DLL
   985 	#define CON_COMMAND_F_SHARED( name, description, flags ) \
   986 		static void name( const CCommand &args ); \
   987 		static ConCommand name##_command_client( #name "_client", name, description, flags ); \
   988 		static void name( const CCommand &args )
   989 #else
   990 	#define CON_COMMAND_F_SHARED( name, description, flags ) \
   991 		static void name( const CCommand &args ); \
   992 		static ConCommand name##_command( #name, name, description, flags ); \
   993 		static void name( const CCommand &args )
   994 #endif
   995 
   996 
   997 #define CON_COMMAND_F_COMPLETION( name, description, flags, completion ) \
   998 	static void name( const CCommand &args ); \
   999 	static ConCommand name##_command( #name, name, description, flags, completion ); \
  1000 	static void name( const CCommand &args )
  1001 
  1002 #ifdef CLIENT_DLL
  1003 	#define CON_COMMAND_F_COMPLETION_SHARED( name, description, flags, completion ) \
  1004 		static void name( const CCommand &args ); \
  1005 		static ConCommand name##_command_client( #name "_client", name, description, flags, completion ); \
  1006 		static void name( const CCommand &args )
  1007 #else
  1008 	#define CON_COMMAND_F_COMPLETION_SHARED( name, description, flags, completion ) \
  1009 		static void name( const CCommand &args ); \
  1010 		static ConCommand name##_command( #name, name, description, flags, completion ); \
  1011 		static void name( const CCommand &args )
  1012 #endif
  1013 
  1014 
  1015 #define CON_COMMAND_EXTERN( name, _funcname, description ) \
  1016 	void _funcname( const CCommand &args ); \
  1017 	static ConCommand name##_command( #name, _funcname, description ); \
  1018 	void _funcname( const CCommand &args )
  1019 
  1020 #define CON_COMMAND_EXTERN_F( name, _funcname, description, flags ) \
  1021 	void _funcname( const CCommand &args ); \
  1022 	static ConCommand name##_command( #name, _funcname, description, flags ); \
  1023 	void _funcname( const CCommand &args )
  1024 
  1025 #define CON_COMMAND_MEMBER_F( _thisclass, name, _funcname, description, flags ) \
  1026 	void _funcname( const CCommand &args );						\
  1027 	friend class CCommandMemberInitializer_##_funcname;			\
  1028 	class CCommandMemberInitializer_##_funcname					\
  1029 	{															\
  1030 	public:														\
  1031 		CCommandMemberInitializer_##_funcname() : m_ConCommandAccessor( NULL, name, &_thisclass::_funcname, description, flags )	\
  1032 		{														\
  1033 			m_ConCommandAccessor.SetOwner( GET_OUTER( _thisclass, m_##_funcname##_register ) );	\
  1034 		}														\
  1035 	private:													\
  1036 		CConCommandMemberAccessor< _thisclass > m_ConCommandAccessor;	\
  1037 	};															\
  1038 																\
  1039 	CCommandMemberInitializer_##_funcname m_##_funcname##_register;		\
  1040 
  1041 
  1042 #endif // CONVAR_H