core/convar_sm_l4d.h
author Nicholas Hastings <skamonkey@gmail.com>
Tue Mar 09 18:41:25 2010 -0500 (2010-03-09)
changeset 2932 77cd75e433b4
parent 2850 1242bcb70fa9
child 3099 1b7bf8509625
permissions -rw-r--r--
Fixed inaccurate description for sm_unban (bug 4245, r=dvander)
ds@2400
     1
//===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
ds@2400
     2
//
ds@2400
     3
// Purpose: 
ds@2400
     4
//
ds@2400
     5
// $Workfile:     $
ds@2400
     6
// $Date:         $
ds@2400
     7
//
ds@2400
     8
//-----------------------------------------------------------------------------
ds@2400
     9
// $NoKeywords: $
ds@2400
    10
//===========================================================================//
ds@2400
    11
ds@2400
    12
#ifndef CONVAR_H
ds@2400
    13
#define CONVAR_H
ds@2400
    14
ds@2400
    15
#if _WIN32
ds@2400
    16
#pragma once
ds@2400
    17
#endif
ds@2400
    18
ds@2400
    19
#include "tier0/dbg.h"
ds@2400
    20
#include "tier1/iconvar.h"
ds@2400
    21
#include "tier1/utlvector.h"
ds@2400
    22
#include "tier1/utlstring.h"
ds@2400
    23
#include "icvar.h"
ds@2400
    24
ds@2400
    25
#ifdef _WIN32
ds@2400
    26
#define FORCEINLINE_CVAR FORCEINLINE
ds@2400
    27
#elif _LINUX
ds@2400
    28
#define FORCEINLINE_CVAR inline
ds@2400
    29
#else
ds@2400
    30
#error "implement me"
ds@2400
    31
#endif
ds@2400
    32
ds@2400
    33
ds@2400
    34
//-----------------------------------------------------------------------------
ds@2400
    35
// Forward declarations
ds@2400
    36
//-----------------------------------------------------------------------------
ds@2400
    37
class ConVar;
ds@2400
    38
class CCommand;
ds@2400
    39
class ConCommand;
ds@2400
    40
class ConCommandBase;
ds@2400
    41
struct characterset_t;
ds@2400
    42
ds@2400
    43
ds@2400
    44
ds@2400
    45
//-----------------------------------------------------------------------------
ds@2400
    46
// Any executable that wants to use ConVars need to implement one of
ds@2400
    47
// these to hook up access to console variables.
ds@2400
    48
//-----------------------------------------------------------------------------
ds@2400
    49
class IConCommandBaseAccessor
ds@2400
    50
{
ds@2400
    51
public:
ds@2400
    52
	// Flags is a combination of FCVAR flags in cvar.h.
ds@2400
    53
	// hOut is filled in with a handle to the variable.
ds@2400
    54
	virtual bool RegisterConCommandBase( ConCommandBase *pVar ) = 0;
ds@2400
    55
};
ds@2400
    56
ds@2400
    57
ds@2400
    58
//-----------------------------------------------------------------------------
ds@2400
    59
// Helper method for console development
ds@2400
    60
//-----------------------------------------------------------------------------
ds@2400
    61
#if defined( _X360 ) && !defined( _RETAIL )
ds@2400
    62
void ConVar_PublishToVXConsole();
ds@2400
    63
#endif
ds@2400
    64
ds@2400
    65
ds@2400
    66
//-----------------------------------------------------------------------------
ds@2400
    67
// Called when a ConCommand needs to execute
ds@2400
    68
//-----------------------------------------------------------------------------
ds@2400
    69
typedef void ( *FnCommandCallbackV1_t )( void );
ds@2400
    70
typedef void ( *FnCommandCallback_t )( const CCommand &command );
ds@2400
    71
ds@2400
    72
#define COMMAND_COMPLETION_MAXITEMS		64
ds@2400
    73
#define COMMAND_COMPLETION_ITEM_LENGTH	64
ds@2400
    74
ds@2400
    75
//-----------------------------------------------------------------------------
ds@2400
    76
// Returns 0 to COMMAND_COMPLETION_MAXITEMS worth of completion strings
ds@2400
    77
//-----------------------------------------------------------------------------
ds@2400
    78
typedef int  ( *FnCommandCompletionCallback )( const char *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] );
ds@2400
    79
ds@2400
    80
ds@2400
    81
//-----------------------------------------------------------------------------
ds@2400
    82
// Interface version
ds@2400
    83
//-----------------------------------------------------------------------------
ds@2400
    84
class ICommandCallback
ds@2400
    85
{
ds@2400
    86
public:
ds@2400
    87
	virtual void CommandCallback( const CCommand &command ) = 0;
ds@2400
    88
};
ds@2400
    89
ds@2400
    90
class ICommandCompletionCallback
ds@2400
    91
{
ds@2400
    92
public:
ds@2400
    93
	virtual int  CommandCompletionCallback( const char *pPartial, CUtlVector< CUtlString > &commands ) = 0;
ds@2400
    94
};
ds@2400
    95
ds@2400
    96
//-----------------------------------------------------------------------------
ds@2400
    97
// Purpose: The base console invoked command/cvar interface
ds@2400
    98
//-----------------------------------------------------------------------------
ds@2400
    99
class ConCommandBase
ds@2400
   100
{
ds@2400
   101
	friend class CCvar;
ds@2400
   102
	friend class ConVar;
ds@2400
   103
	friend class ConCommand;
ds@2400
   104
	friend void ConVar_Register( int nCVarFlag, IConCommandBaseAccessor *pAccessor );
ds@2400
   105
	friend void ConVar_PublishToVXConsole();
ds@2400
   106
ds@2400
   107
	// FIXME: Remove when ConVar changes are done
ds@2400
   108
	friend class CDefaultCvar;
ds@2400
   109
ds@2400
   110
public:
ds@2400
   111
								ConCommandBase( void );
ds@2400
   112
								ConCommandBase( const char *pName, const char *pHelpString = 0, 
ds@2400
   113
									int flags = 0 );
ds@2400
   114
ds@2400
   115
	virtual						~ConCommandBase( void );
ds@2400
   116
ds@2400
   117
	virtual	bool				IsCommand( void ) const;
ds@2400
   118
ds@2400
   119
	// Check flag
ds@2400
   120
	virtual bool				IsFlagSet( int flag ) const;
ds@2400
   121
	// Set flag
ds@2400
   122
	virtual void				AddFlags( int flags );
ds@2400
   123
	// Remove flag
ds@2400
   124
	virtual void				RemoveFlags( int flags );
ds@2400
   125
	// Get flags
ds@2400
   126
	virtual int					GetFlags( void ) const;
ds@2400
   127
ds@2400
   128
	// Return name of cvar
ds@2400
   129
	virtual const char			*GetName( void ) const;
ds@2400
   130
ds@2400
   131
	// Return help text for cvar
ds@2400
   132
	virtual const char			*GetHelpText( void ) const;
ds@2400
   133
ds@2400
   134
	// Deal with next pointer
ds@2400
   135
	const ConCommandBase		*GetNext( void ) const;
ds@2400
   136
	ConCommandBase				*GetNext( void );
ds@2400
   137
ds@2400
   138
	inline void SetNext(ConCommandBase *pBase)
ds@2400
   139
	{
ds@2400
   140
		m_pNext = pBase;
ds@2400
   141
	}
ds@2400
   142
	
ds@2400
   143
	virtual bool				IsRegistered( void ) const;
ds@2400
   144
ds@2400
   145
	// Returns the DLL identifier
ds@2400
   146
	virtual CVarDLLIdentifier_t	GetDLLIdentifier() const;
ds@2400
   147
ds@2400
   148
protected:
ds@2400
   149
	virtual void				Create( const char *pName, const char *pHelpString = 0, 
ds@2400
   150
									int flags = 0 );
ds@2400
   151
ds@2400
   152
	// Used internally by OneTimeInit to initialize/shutdown
ds@2400
   153
	virtual void				Init();
ds@2400
   154
	void						Shutdown();
ds@2400
   155
ds@2400
   156
	// Internal copy routine ( uses new operator from correct module )
ds@2400
   157
	char						*CopyString( const char *from );
ds@2400
   158
ds@2400
   159
private:
ds@2400
   160
	// Next ConVar in chain
ds@2400
   161
	// Prior to register, it points to the next convar in the DLL.
ds@2400
   162
	// Once registered, though, m_pNext is reset to point to the next
ds@2400
   163
	// convar in the global list
ds@2400
   164
	ConCommandBase				*m_pNext;
ds@2400
   165
ds@2400
   166
	// Has the cvar been added to the global list?
ds@2400
   167
	bool						m_bRegistered;
ds@2400
   168
ds@2400
   169
	// Static data
ds@2400
   170
	const char 					*m_pszName;
ds@2400
   171
	const char 					*m_pszHelpString;
ds@2400
   172
	
ds@2400
   173
	// ConVar flags
ds@2400
   174
	int							m_nFlags;
ds@2400
   175
ds@2400
   176
protected:
ds@2400
   177
	// ConVars add themselves to this list for the executable. 
ds@2400
   178
	// Then ConVar_Register runs through  all the console variables 
ds@2400
   179
	// and registers them into a global list stored in vstdlib.dll
ds@2400
   180
	static ConCommandBase		*s_pConCommandBases;
ds@2400
   181
ds@2400
   182
	// ConVars in this executable use this 'global' to access values.
ds@2400
   183
	static IConCommandBaseAccessor	*s_pAccessor;
ds@2400
   184
public:
ds@2400
   185
	inline void SetFlags(int flags)	
ds@2400
   186
	{ 
ds@2400
   187
		m_nFlags = flags; 
ds@2400
   188
	}
ds@2400
   189
};
ds@2400
   190
ds@2400
   191
ds@2400
   192
//-----------------------------------------------------------------------------
ds@2400
   193
// Command tokenizer
ds@2400
   194
//-----------------------------------------------------------------------------
ds@2400
   195
class CCommand
ds@2400
   196
{
ds@2400
   197
public:
ds@2400
   198
	CCommand();
ds@2400
   199
	CCommand( int nArgC, const char **ppArgV );
ds@2400
   200
	bool Tokenize( const char *pCommand, characterset_t *pBreakSet = NULL );
ds@2400
   201
	void Reset();
ds@2400
   202
ds@2400
   203
	int ArgC() const;
ds@2400
   204
	const char **ArgV() const;
ds@2400
   205
	const char *ArgS() const;					// All args that occur after the 0th arg, in string form
ds@2400
   206
	const char *GetCommandString() const;		// The entire command in string form, including the 0th arg
ds@2400
   207
	const char *operator[]( int nIndex ) const;	// Gets at arguments
ds@2400
   208
	const char *Arg( int nIndex ) const;		// Gets at arguments
ds@2400
   209
	
ds@2400
   210
	// Helper functions to parse arguments to commands.
ds@2400
   211
	const char* FindArg( const char *pName ) const;
ds@2400
   212
	int FindArgInt( const char *pName, int nDefaultVal ) const;
ds@2400
   213
ds@2400
   214
	static int MaxCommandLength();
ds@2400
   215
	static characterset_t* DefaultBreakSet();
ds@2400
   216
ds@2400
   217
private:
ds@2400
   218
	enum
ds@2400
   219
	{
ds@2400
   220
		COMMAND_MAX_ARGC = 64,
ds@2400
   221
		COMMAND_MAX_LENGTH = 512,
ds@2400
   222
	};
ds@2400
   223
ds@2400
   224
	int		m_nArgc;
ds@2400
   225
	int		m_nArgv0Size;
ds@2400
   226
	char	m_pArgSBuffer[ COMMAND_MAX_LENGTH ];
ds@2400
   227
	char	m_pArgvBuffer[ COMMAND_MAX_LENGTH ];
ds@2400
   228
	const char*	m_ppArgv[ COMMAND_MAX_ARGC ];
ds@2400
   229
};
ds@2400
   230
ds@2400
   231
inline int CCommand::MaxCommandLength()
ds@2400
   232
{
ds@2400
   233
	return COMMAND_MAX_LENGTH - 1;
ds@2400
   234
}
ds@2400
   235
ds@2400
   236
inline int CCommand::ArgC() const
ds@2400
   237
{
ds@2400
   238
	return m_nArgc;
ds@2400
   239
}
ds@2400
   240
ds@2400
   241
inline const char **CCommand::ArgV() const
ds@2400
   242
{
ds@2400
   243
	return m_nArgc ? (const char**)m_ppArgv : NULL;
ds@2400
   244
}
ds@2400
   245
ds@2400
   246
inline const char *CCommand::ArgS() const
ds@2400
   247
{
ds@2400
   248
	return m_nArgv0Size ? &m_pArgSBuffer[m_nArgv0Size] : "";
ds@2400
   249
}
ds@2400
   250
ds@2400
   251
inline const char *CCommand::GetCommandString() const
ds@2400
   252
{
ds@2400
   253
	return m_nArgc ? m_pArgSBuffer : "";
ds@2400
   254
}
ds@2400
   255
ds@2400
   256
inline const char *CCommand::Arg( int nIndex ) const
ds@2400
   257
{
ds@2400
   258
	// FIXME: Many command handlers appear to not be particularly careful
ds@2400
   259
	// about checking for valid argc range. For now, we're going to
ds@2400
   260
	// do the extra check and return an empty string if it's out of range
ds@2400
   261
	if ( nIndex < 0 || nIndex >= m_nArgc )
ds@2400
   262
		return "";
ds@2400
   263
	return m_ppArgv[nIndex];
ds@2400
   264
}
ds@2400
   265
ds@2400
   266
inline const char *CCommand::operator[]( int nIndex ) const
ds@2400
   267
{
ds@2400
   268
	return Arg( nIndex );
ds@2400
   269
}
ds@2400
   270
ds@2400
   271
ds@2400
   272
//-----------------------------------------------------------------------------
ds@2400
   273
// Purpose: The console invoked command
ds@2400
   274
//-----------------------------------------------------------------------------
ds@2400
   275
class ConCommand : public ConCommandBase
ds@2400
   276
{
ds@2400
   277
friend class CCvar;
ds@2400
   278
ds@2400
   279
public:
ds@2400
   280
	typedef ConCommandBase BaseClass;
ds@2400
   281
ds@2400
   282
	ConCommand( const char *pName, FnCommandCallbackV1_t callback, 
ds@2400
   283
		const char *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
ds@2400
   284
	ConCommand( const char *pName, FnCommandCallback_t callback, 
ds@2400
   285
		const char *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
ds@2400
   286
	ConCommand( const char *pName, ICommandCallback *pCallback, 
ds@2400
   287
		const char *pHelpString = 0, int flags = 0, ICommandCompletionCallback *pCommandCompletionCallback = 0 );
ds@2400
   288
ds@2400
   289
	virtual ~ConCommand( void );
ds@2400
   290
ds@2400
   291
	virtual	bool IsCommand( void ) const;
ds@2400
   292
ds@2400
   293
	virtual int AutoCompleteSuggest( const char *partial, CUtlVector< CUtlString > &commands );
ds@2400
   294
ds@2400
   295
	virtual bool CanAutoComplete( void );
ds@2400
   296
ds@2400
   297
	// Invoke the function
ds@2400
   298
	virtual void Dispatch( const CCommand &command );
ds@2400
   299
ds@2400
   300
private:
ds@2400
   301
	// NOTE: To maintain backward compat, we have to be very careful:
ds@2400
   302
	// All public virtual methods must appear in the same order always
ds@2400
   303
	// since engine code will be calling into this code, which *does not match*
ds@2400
   304
	// in the mod code; it's using slightly different, but compatible versions
ds@2400
   305
	// of this class. Also: Be very careful about adding new fields to this class.
ds@2400
   306
	// Those fields will not exist in the version of this class that is instanced
ds@2400
   307
	// in mod code.
ds@2400
   308
ds@2400
   309
	// Call this function when executing the command
ds@2400
   310
	union
ds@2400
   311
	{
ds@2400
   312
		FnCommandCallbackV1_t m_fnCommandCallbackV1;
ds@2400
   313
		FnCommandCallback_t m_fnCommandCallback;
ds@2400
   314
		ICommandCallback *m_pCommandCallback; 
ds@2400
   315
	};
ds@2400
   316
ds@2400
   317
	union
ds@2400
   318
	{
ds@2400
   319
		FnCommandCompletionCallback	m_fnCompletionCallback;
ds@2400
   320
		ICommandCompletionCallback *m_pCommandCompletionCallback;
ds@2400
   321
	};
ds@2400
   322
ds@2400
   323
	bool m_bHasCompletionCallback : 1;
ds@2400
   324
	bool m_bUsingNewCommandCallback : 1;
ds@2400
   325
	bool m_bUsingCommandCallbackInterface : 1;
ds@2400
   326
public:
ds@2400
   327
	inline FnCommandCallback_t GetCallback() const
ds@2400
   328
	{ 
ds@2400
   329
		return m_fnCommandCallback; 
ds@2400
   330
	}
ds@2400
   331
};
ds@2400
   332
ds@2400
   333
ds@2400
   334
//-----------------------------------------------------------------------------
ds@2400
   335
// Purpose: A console variable
ds@2400
   336
//-----------------------------------------------------------------------------
ds@2400
   337
class ConVar : public ConCommandBase, public IConVar
ds@2400
   338
{
ds@2400
   339
friend class CCvar;
ds@2400
   340
friend class ConVarRef;
ds@2400
   341
ds@2400
   342
public:
ds@2400
   343
	typedef ConCommandBase BaseClass;
ds@2400
   344
ds@2400
   345
								ConVar( const char *pName, const char *pDefaultValue, int flags = 0);
ds@2400
   346
ds@2400
   347
								ConVar( const char *pName, const char *pDefaultValue, int flags, 
ds@2400
   348
									const char *pHelpString );
ds@2400
   349
								ConVar( const char *pName, const char *pDefaultValue, int flags, 
ds@2400
   350
									const char *pHelpString, bool bMin, float fMin, bool bMax, float fMax );
ds@2400
   351
								ConVar( const char *pName, const char *pDefaultValue, int flags, 
ds@2400
   352
									const char *pHelpString, FnChangeCallback_t callback );
ds@2400
   353
								ConVar( const char *pName, const char *pDefaultValue, int flags, 
ds@2400
   354
									const char *pHelpString, bool bMin, float fMin, bool bMax, float fMax,
ds@2400
   355
									FnChangeCallback_t callback );
ds@2400
   356
ds@2400
   357
	virtual						~ConVar( void );
ds@2400
   358
ds@2400
   359
	virtual	bool				IsCommand( void ) const;
ds@2400
   360
	virtual bool				IsFlagSet( int flag ) const;
ds@2400
   361
	virtual void				AddFlags( int flags );
ds@2400
   362
	virtual int					GetFlags( void ) const;
ds@2400
   363
	virtual const char			*GetName( void ) const;
ds@2400
   364
	virtual const char*			GetHelpText( void ) const;
ds@2400
   365
	virtual bool				IsRegistered( void ) const;
ds@2400
   366
		
ds@2400
   367
	// Install a change callback (there shouldn't already be one....)
ds@2400
   368
	void InstallChangeCallback( FnChangeCallback_t callback );
ds@2400
   369
ds@2400
   370
	// Retrieve value
ds@2400
   371
	FORCEINLINE_CVAR float			GetFloat( void ) const;
ds@2400
   372
	FORCEINLINE_CVAR int			GetInt( void ) const;
ds@2400
   373
	FORCEINLINE_CVAR bool			GetBool() const {  return !!GetInt(); }
ds@2400
   374
	FORCEINLINE_CVAR char const	   *GetString( void ) const;
ds@2400
   375
	
ds@2400
   376
	// Used internally by OneTimeInit to initialize.
ds@2400
   377
	virtual void				Init();
ds@2400
   378
	
ds@2400
   379
	virtual const char 			*GetBaseName( void ) const;
ds@2400
   380
	virtual int					GetSplitScreenPlayerSlot ( void ) const;
ds@2400
   381
ds@2400
   382
	// Any function that allocates/frees memory needs to be virtual or else you'll have crashes
ds@2400
   383
	//  from alloc/free across dll/exe boundaries.
ds@2400
   384
	
ds@2400
   385
	// These just call into the IConCommandBaseAccessor to check flags and set the var (which ends up calling InternalSetValue).
ds@2400
   386
	virtual void				SetValue( const char *value );
ds@2400
   387
	virtual void				SetValue( float value );
ds@2400
   388
	virtual void				SetValue( int value );
pred@2850
   389
#if SOURCE_ENGINE == SE_LEFT4DEAD2
pred@2850
   390
	virtual void				SetValue( Color value );
pred@2850
   391
#endif
ds@2400
   392
	
ds@2400
   393
	// Reset to default value
ds@2400
   394
	void						Revert( void );
ds@2400
   395
ds@2400
   396
	// True if it has a min/max setting
ds@2400
   397
	bool						GetMin( float& minVal ) const;
ds@2400
   398
	bool						GetMax( float& maxVal ) const;
ds@2400
   399
	const char					*GetDefault( void ) const;
ds@2400
   400
ds@2400
   401
private:
ds@2400
   402
	// Called by CCvar when the value of a var is changing.
ds@2400
   403
	virtual void				InternalSetValue(const char *value);
ds@2400
   404
	// For CVARs marked FCVAR_NEVER_AS_STRING
ds@2400
   405
	virtual void				InternalSetFloatValue( float fNewValue );
ds@2400
   406
	virtual void				InternalSetIntValue( int nValue );
ds@2400
   407
ds@2400
   408
	virtual bool				ClampValue( float& value );
ds@2400
   409
	virtual void				ChangeStringValue( const char *tempVal, float flOldValue );
ds@2400
   410
ds@2400
   411
	virtual void				Create( const char *pName, const char *pDefaultValue, int flags = 0,
ds@2400
   412
									const char *pHelpString = 0, bool bMin = false, float fMin = 0.0,
ds@2400
   413
									bool bMax = false, float fMax = false, FnChangeCallback_t callback = 0 );
ds@2400
   414
ds@2400
   415
private:
ds@2400
   416
ds@2400
   417
	// This either points to "this" or it points to the original declaration of a ConVar.
ds@2400
   418
	// This allows ConVars to exist in separate modules, and they all use the first one to be declared.
ds@2400
   419
	// m_pParent->m_pParent must equal m_pParent (ie: m_pParent must be the root, or original, ConVar).
ds@2400
   420
	ConVar						*m_pParent;
ds@2400
   421
ds@2400
   422
	// Static data
ds@2400
   423
	const char					*m_pszDefaultValue;
ds@2400
   424
	
ds@2400
   425
	// Value
ds@2400
   426
	// Dynamically allocated
ds@2400
   427
	char						*m_pszString;
ds@2400
   428
	int							m_StringLength;
ds@2400
   429
ds@2400
   430
	// Values
ds@2400
   431
	float						m_fValue;
ds@2400
   432
	int							m_nValue;
ds@2400
   433
ds@2400
   434
	// Min/Max values
ds@2400
   435
	bool						m_bHasMin;
ds@2400
   436
	float						m_fMinVal;
ds@2400
   437
	bool						m_bHasMax;
ds@2400
   438
	float						m_fMaxVal;
ds@2400
   439
	
ds@2400
   440
	// Call this function when ConVar changes
ds@2400
   441
	FnChangeCallback_t			m_fnChangeCallback;
ds@2400
   442
public:
ds@2400
   443
	inline FnChangeCallback_t GetCallback() const
ds@2400
   444
	{
ds@2400
   445
		return m_fnChangeCallback;
ds@2400
   446
	}
ds@2400
   447
	inline void SetMin(bool set, float min=0.0)
ds@2400
   448
	{ 
ds@2400
   449
		m_bHasMin = set; 
ds@2400
   450
		m_fMinVal = min; 
ds@2400
   451
	}
ds@2400
   452
	inline void SetMax(bool set, float max=0.0)
ds@2400
   453
	{
ds@2400
   454
		m_bHasMax = set;
ds@2400
   455
		m_fMaxVal = max; 
ds@2400
   456
	}
ds@2400
   457
};
ds@2400
   458
ds@2400
   459
ds@2400
   460
//-----------------------------------------------------------------------------
ds@2400
   461
// Purpose: Return ConVar value as a float
ds@2400
   462
// Output : float
ds@2400
   463
//-----------------------------------------------------------------------------
ds@2400
   464
FORCEINLINE_CVAR float ConVar::GetFloat( void ) const
ds@2400
   465
{
ds@2400
   466
	return m_pParent->m_fValue;
ds@2400
   467
}
ds@2400
   468
ds@2400
   469
//-----------------------------------------------------------------------------
ds@2400
   470
// Purpose: Return ConVar value as an int
ds@2400
   471
// Output : int
ds@2400
   472
//-----------------------------------------------------------------------------
ds@2400
   473
FORCEINLINE_CVAR int ConVar::GetInt( void ) const 
ds@2400
   474
{
ds@2400
   475
	return m_pParent->m_nValue;
ds@2400
   476
}
ds@2400
   477
ds@2400
   478
ds@2400
   479
//-----------------------------------------------------------------------------
ds@2400
   480
// Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
ds@2400
   481
// Output : const char *
ds@2400
   482
//-----------------------------------------------------------------------------
ds@2400
   483
FORCEINLINE_CVAR const char *ConVar::GetString( void ) const 
ds@2400
   484
{
ds@2400
   485
	if ( m_nFlags & FCVAR_NEVER_AS_STRING )
ds@2400
   486
		return "FCVAR_NEVER_AS_STRING";
ds@2400
   487
ds@2400
   488
	return ( m_pParent->m_pszString ) ? m_pParent->m_pszString : "";
ds@2400
   489
}
ds@2400
   490
ds@2400
   491
ds@2400
   492
//-----------------------------------------------------------------------------
ds@2400
   493
// Used to read/write convars that already exist (replaces the FindVar method)
ds@2400
   494
//-----------------------------------------------------------------------------
ds@2400
   495
class ConVarRef
ds@2400
   496
{
ds@2400
   497
public:
ds@2400
   498
	ConVarRef( const char *pName );
ds@2400
   499
	ConVarRef( const char *pName, bool bIgnoreMissing );
ds@2400
   500
	ConVarRef( IConVar *pConVar );
ds@2400
   501
ds@2400
   502
	void Init( const char *pName, bool bIgnoreMissing );
ds@2400
   503
	bool IsValid() const;
ds@2400
   504
	bool IsFlagSet( int nFlags ) const;
ds@2400
   505
	IConVar *GetLinkedConVar();
ds@2400
   506
ds@2400
   507
	// Get/Set value
ds@2400
   508
	float GetFloat( void ) const;
ds@2400
   509
	int GetInt( void ) const;
ds@2400
   510
	bool GetBool() const { return !!GetInt(); }
ds@2400
   511
	const char *GetString( void ) const;
ds@2400
   512
ds@2400
   513
	void SetValue( const char *pValue );
ds@2400
   514
	void SetValue( float flValue );
ds@2400
   515
	void SetValue( int nValue );
ds@2400
   516
	void SetValue( bool bValue );
ds@2400
   517
ds@2400
   518
	const char *GetName() const;
ds@2400
   519
ds@2400
   520
	const char *GetDefault() const;
ds@2400
   521
ds@2400
   522
private:
ds@2400
   523
	// High-speed method to read convar data
ds@2400
   524
	IConVar *m_pConVar;
ds@2400
   525
	ConVar *m_pConVarState;
ds@2400
   526
};
ds@2400
   527
ds@2400
   528
ds@2400
   529
//-----------------------------------------------------------------------------
ds@2400
   530
// Did we find an existing convar of that name?
ds@2400
   531
//-----------------------------------------------------------------------------
ds@2400
   532
FORCEINLINE_CVAR bool ConVarRef::IsFlagSet( int nFlags ) const
ds@2400
   533
{
ds@2400
   534
	return ( m_pConVar->IsFlagSet( nFlags ) != 0 );
ds@2400
   535
}
ds@2400
   536
ds@2400
   537
FORCEINLINE_CVAR IConVar *ConVarRef::GetLinkedConVar()
ds@2400
   538
{
ds@2400
   539
	return m_pConVar;
ds@2400
   540
}
ds@2400
   541
ds@2400
   542
FORCEINLINE_CVAR const char *ConVarRef::GetName() const
ds@2400
   543
{
ds@2400
   544
	return m_pConVar->GetName();
ds@2400
   545
}
ds@2400
   546
ds@2400
   547
ds@2400
   548
//-----------------------------------------------------------------------------
ds@2400
   549
// Purpose: Return ConVar value as a float
ds@2400
   550
//-----------------------------------------------------------------------------
ds@2400
   551
FORCEINLINE_CVAR float ConVarRef::GetFloat( void ) const
ds@2400
   552
{
ds@2400
   553
	return m_pConVarState->m_fValue;
ds@2400
   554
}
ds@2400
   555
ds@2400
   556
//-----------------------------------------------------------------------------
ds@2400
   557
// Purpose: Return ConVar value as an int
ds@2400
   558
//-----------------------------------------------------------------------------
ds@2400
   559
FORCEINLINE_CVAR int ConVarRef::GetInt( void ) const 
ds@2400
   560
{
ds@2400
   561
	return m_pConVarState->m_nValue;
ds@2400
   562
}
ds@2400
   563
ds@2400
   564
//-----------------------------------------------------------------------------
ds@2400
   565
// Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
ds@2400
   566
//-----------------------------------------------------------------------------
ds@2400
   567
FORCEINLINE_CVAR const char *ConVarRef::GetString( void ) const 
ds@2400
   568
{
ds@2400
   569
	Assert( !IsFlagSet( FCVAR_NEVER_AS_STRING ) );
ds@2400
   570
	return m_pConVarState->m_pszString;
ds@2400
   571
}
ds@2400
   572
ds@2400
   573
ds@2400
   574
FORCEINLINE_CVAR void ConVarRef::SetValue( const char *pValue )
ds@2400
   575
{
ds@2400
   576
	m_pConVar->SetValue( pValue );
ds@2400
   577
}
ds@2400
   578
ds@2400
   579
FORCEINLINE_CVAR void ConVarRef::SetValue( float flValue )
ds@2400
   580
{
ds@2400
   581
	m_pConVar->SetValue( flValue );
ds@2400
   582
}
ds@2400
   583
ds@2400
   584
FORCEINLINE_CVAR void ConVarRef::SetValue( int nValue )
ds@2400
   585
{
ds@2400
   586
	m_pConVar->SetValue( nValue );
ds@2400
   587
}
ds@2400
   588
ds@2400
   589
FORCEINLINE_CVAR void ConVarRef::SetValue( bool bValue )
ds@2400
   590
{
ds@2400
   591
	m_pConVar->SetValue( bValue ? 1 : 0 );
ds@2400
   592
}
ds@2400
   593
ds@2400
   594
FORCEINLINE_CVAR const char *ConVarRef::GetDefault() const
ds@2400
   595
{
ds@2400
   596
	return m_pConVarState->m_pszDefaultValue;
ds@2400
   597
}
ds@2400
   598
ds@2400
   599
ds@2400
   600
//-----------------------------------------------------------------------------
ds@2400
   601
// Called by the framework to register ConCommands with the ICVar
ds@2400
   602
//-----------------------------------------------------------------------------
ds@2400
   603
void ConVar_Register( int nCVarFlag = 0, IConCommandBaseAccessor *pAccessor = NULL );
ds@2400
   604
void ConVar_Unregister( );
ds@2400
   605
ds@2400
   606
ds@2400
   607
//-----------------------------------------------------------------------------
ds@2400
   608
// Utility methods 
ds@2400
   609
//-----------------------------------------------------------------------------
ds@2400
   610
void ConVar_PrintFlags( const ConCommandBase *var );
ds@2400
   611
void ConVar_PrintDescription( const ConCommandBase *pVar );
ds@2400
   612
ds@2400
   613
ds@2400
   614
//-----------------------------------------------------------------------------
ds@2400
   615
// Purpose: Utility class to quickly allow ConCommands to call member methods
ds@2400
   616
//-----------------------------------------------------------------------------
ds@2400
   617
#ifdef _MSC_VER
ds@2400
   618
#pragma warning (disable : 4355 )
ds@2400
   619
#endif
ds@2400
   620
ds@2400
   621
template< class T >
ds@2400
   622
class CConCommandMemberAccessor : public ConCommand, public ICommandCallback, public ICommandCompletionCallback
ds@2400
   623
{
ds@2400
   624
	typedef ConCommand BaseClass;
ds@2400
   625
	typedef void ( T::*FnMemberCommandCallback_t )( const CCommand &command );
ds@2400
   626
	typedef int  ( T::*FnMemberCommandCompletionCallback_t )( const char *pPartial, CUtlVector< CUtlString > &commands );
ds@2400
   627
ds@2400
   628
public:
ds@2400
   629
	CConCommandMemberAccessor( T* pOwner, const char *pName, FnMemberCommandCallback_t callback, const char *pHelpString = 0,
ds@2400
   630
		int flags = 0, FnMemberCommandCompletionCallback_t completionFunc = 0 ) :
ds@2400
   631
		BaseClass( pName, this, pHelpString, flags, ( completionFunc != 0 ) ? this : NULL )
ds@2400
   632
	{
ds@2400
   633
		m_pOwner = pOwner;
ds@2400
   634
		m_Func = callback;
ds@2400
   635
		m_CompletionFunc = completionFunc;
ds@2400
   636
	}
ds@2400
   637
ds@2400
   638
	~CConCommandMemberAccessor()
ds@2400
   639
	{
ds@2400
   640
		Shutdown();
ds@2400
   641
	}
ds@2400
   642
ds@2400
   643
	void SetOwner( T* pOwner )
ds@2400
   644
	{
ds@2400
   645
		m_pOwner = pOwner;
ds@2400
   646
	}
ds@2400
   647
ds@2400
   648
	virtual void CommandCallback( const CCommand &command )
ds@2400
   649
	{
ds@2400
   650
		Assert( m_pOwner && m_Func );
ds@2400
   651
		(m_pOwner->*m_Func)( command );
ds@2400
   652
	}
ds@2400
   653
ds@2400
   654
	virtual int  CommandCompletionCallback( const char *pPartial, CUtlVector< CUtlString > &commands )
ds@2400
   655
	{
ds@2400
   656
		Assert( m_pOwner && m_CompletionFunc );
ds@2400
   657
		return (m_pOwner->*m_CompletionFunc)( pPartial, commands );
ds@2400
   658
	}
ds@2400
   659
ds@2400
   660
private:
ds@2400
   661
	T* m_pOwner;
ds@2400
   662
	FnMemberCommandCallback_t m_Func;
ds@2400
   663
	FnMemberCommandCompletionCallback_t m_CompletionFunc;
ds@2400
   664
};
ds@2400
   665
ds@2400
   666
#ifdef _MSC_VER
ds@2400
   667
#pragma warning ( default : 4355 )
ds@2400
   668
#endif
ds@2400
   669
ds@2400
   670
//-----------------------------------------------------------------------------
ds@2400
   671
// Purpose: Utility macros to quicky generate a simple console command
ds@2400
   672
//-----------------------------------------------------------------------------
ds@2400
   673
#define CON_COMMAND( name, description ) \
ds@2400
   674
   static void name( const CCommand &args ); \
ds@2400
   675
   static ConCommand name##_command( #name, name, description ); \
ds@2400
   676
   static void name( const CCommand &args )
ds@2400
   677
ds@2400
   678
#define CON_COMMAND_F( name, description, flags ) \
ds@2400
   679
   static void name( const CCommand &args ); \
ds@2400
   680
   static ConCommand name##_command( #name, name, description, flags ); \
ds@2400
   681
   static void name( const CCommand &args )
ds@2400
   682
ds@2400
   683
#define CON_COMMAND_F_COMPLETION( name, description, flags, completion ) \
ds@2400
   684
	static void name( const CCommand &args ); \
ds@2400
   685
	static ConCommand name##_command( #name, name, description, flags, completion ); \
ds@2400
   686
	static void name( const CCommand &args )
ds@2400
   687
ds@2400
   688
#define CON_COMMAND_EXTERN( name, _funcname, description ) \
ds@2400
   689
	void _funcname( const CCommand &args ); \
ds@2400
   690
	static ConCommand name##_command( #name, _funcname, description ); \
ds@2400
   691
	void _funcname( const CCommand &args )
ds@2400
   692
ds@2400
   693
#define CON_COMMAND_EXTERN_F( name, _funcname, description, flags ) \
ds@2400
   694
	void _funcname( const CCommand &args ); \
ds@2400
   695
	static ConCommand name##_command( #name, _funcname, description, flags ); \
ds@2400
   696
	void _funcname( const CCommand &args )
ds@2400
   697
ds@2400
   698
#define CON_COMMAND_MEMBER_F( _thisclass, name, _funcname, description, flags ) \
ds@2400
   699
	void _funcname( const CCommand &args );						\
ds@2400
   700
	friend class CCommandMemberInitializer_##_funcname;			\
ds@2400
   701
	class CCommandMemberInitializer_##_funcname					\
ds@2400
   702
	{															\
ds@2400
   703
	public:														\
ds@2400
   704
		CCommandMemberInitializer_##_funcname() : m_ConCommandAccessor( NULL, name, &_thisclass::_funcname, description, flags )	\
ds@2400
   705
		{														\
ds@2400
   706
			m_ConCommandAccessor.SetOwner( GET_OUTER( _thisclass, m_##_funcname##_register ) );	\
ds@2400
   707
		}														\
ds@2400
   708
	private:													\
ds@2400
   709
		CConCommandMemberAccessor< _thisclass > m_ConCommandAccessor;	\
ds@2400
   710
	};															\
ds@2400
   711
																\
ds@2400
   712
	CCommandMemberInitializer_##_funcname m_##_funcname##_register;		\
ds@2400
   713
ds@2400
   714
ds@2400
   715
#endif // CONVAR_H