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