sourcemm/convar.h
changeset 76 39266faeec97
child 232 a86d52ff721c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcemm/convar.h	Fri Jun 24 22:26:19 2005 +0000
     1.3 @@ -0,0 +1,365 @@
     1.4 +//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
     1.5 +//
     1.6 +// Purpose: 
     1.7 +//
     1.8 +// $Workfile:     $
     1.9 +// $Date$
    1.10 +//
    1.11 +//-----------------------------------------------------------------------------
    1.12 +// $NoKeywords: $
    1.13 +//=============================================================================//
    1.14 +
    1.15 +#ifndef CONVAR_H
    1.16 +#define CONVAR_H
    1.17 +#if _WIN32
    1.18 +#pragma once
    1.19 +#endif
    1.20 +
    1.21 +#include "tier0/dbg.h"
    1.22 +
    1.23 +// The default, no flags at all
    1.24 +#define FCVAR_NONE				0 
    1.25 +
    1.26 +// Command to ConVars and ConCommands
    1.27 +#define FCVAR_UNREGISTERED		(1<<0)	// If this is set, don't add to linked list, etc.
    1.28 +#define FCVAR_LAUNCHER			(1<<1) // defined by launcher
    1.29 +#define FCVAR_GAMEDLL			(1<<2)	// defined by the game DLL
    1.30 +#define FCVAR_CLIENTDLL			(1<<3)  // defined by the client DLL
    1.31 +#define FCVAR_MATERIAL_SYSTEM	(1<<4)	// Defined by the material system.
    1.32 +#define FCVAR_STUDIORENDER		(1<<15)	// Defined by the material system.
    1.33 +#define FCVAR_PLUGIN			(1<<18)	// Defined by a 3rd party plugin.
    1.34 +#define FCVAR_CHEAT				(1<<14) // Only useable in singleplayer / debug / multiplayer & sv_cheats
    1.35 +
    1.36 +// ConVar only
    1.37 +#define FCVAR_PROTECTED			(1<<5)  // It's a server cvar, but we don't send the data since it's a password, etc.  Sends 1 if it's not bland/zero, 0 otherwise as value
    1.38 +#define FCVAR_SPONLY			(1<<6)  // This cvar cannot be changed by clients connected to a multiplayer server.
    1.39 +#define	FCVAR_ARCHIVE			(1<<7)	// set to cause it to be saved to vars.rc
    1.40 +#define	FCVAR_NOTIFY			(1<<8)	// notifies players when changed
    1.41 +#define	FCVAR_USERINFO			(1<<9)	// changes the client's info string
    1.42 +
    1.43 +#define FCVAR_PRINTABLEONLY		(1<<10)  // This cvar's string cannot contain unprintable characters ( e.g., used for player name etc ).
    1.44 +#define FCVAR_UNLOGGED			(1<<11)  // If this is a FCVAR_SERVER, don't log changes to the log file / console if we are creating a log
    1.45 +#define FCVAR_NEVER_AS_STRING	(1<<12)  // never try to print that cvar
    1.46 +
    1.47 +// It's a ConVar that's shared between the client and the server.
    1.48 +// At signon, the values of all such ConVars are sent from the server to the client (skipped for local
    1.49 +//  client, of course )
    1.50 +// If a change is requested it must come from the console (i.e., no remote client changes)
    1.51 +// If a value is changed while a server is active, it's replicated to all connected clients
    1.52 +#define FCVAR_REPLICATED		(1<<13)	// server setting enforced on clients, TODO rename to FCAR_SERVER at some time
    1.53 +#define FCVAR_DEMO				(1<<16)  // record this cvar when starting a demo file
    1.54 +#define FCVAR_DONTRECORD		(1<<17)  // don't record these command in demofiles
    1.55 +
    1.56 +#define FCVAR_NOT_CONNECTED		(1<<19)	// cvar cannot be changed by a client that is connected to a server
    1.57 +
    1.58 +
    1.59 +class ConVar;
    1.60 +class ConCommand;
    1.61 +class ConCommandBase;
    1.62 +
    1.63 +// Any executable that wants to use ConVars need to implement one of
    1.64 +// these to hook up access to console variables.
    1.65 +class IConCommandBaseAccessor
    1.66 +{
    1.67 +public:
    1.68 +	// Flags is a combination of FCVAR flags in cvar.h.
    1.69 +	// hOut is filled in with a handle to the variable.
    1.70 +	virtual bool RegisterConCommandBase( ConCommandBase *pVar )=0;
    1.71 +};
    1.72 +
    1.73 +
    1.74 +// You don't have to instantiate one of these, just call its 
    1.75 +// OneTimeInit function when your executable is initializing.
    1.76 +class ConCommandBaseMgr
    1.77 +{
    1.78 +public:
    1.79 +	// Call this ONCE when the executable starts up.
    1.80 +	static void	OneTimeInit( IConCommandBaseAccessor *pAccessor );
    1.81 +};
    1.82 +
    1.83 +// Called when a ConVar changes value
    1.84 +typedef void ( *FnChangeCallback )( ConVar *var, char const *pOldString );
    1.85 +
    1.86 +// Called when a ConCommand needs to execute
    1.87 +typedef void ( *FnCommandCallback )( void );
    1.88 +
    1.89 +#define COMMAND_COMPLETION_MAXITEMS		64
    1.90 +#define COMMAND_COMPLETION_ITEM_LENGTH	64
    1.91 +
    1.92 +// Returns 0 to COMMAND_COMPLETION_MAXITEMS worth of completion strings
    1.93 +typedef int  ( *FnCommandCompletionCallback )( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] );
    1.94 +
    1.95 +//-----------------------------------------------------------------------------
    1.96 +// Purpose: The base console invoked command/cvar interface
    1.97 +//-----------------------------------------------------------------------------
    1.98 +class ConCommandBase
    1.99 +{
   1.100 +	friend class ConCommandBaseMgr;
   1.101 +	friend class CCvar;
   1.102 +	friend class ConVar;
   1.103 +	friend class ConCommand;
   1.104 +
   1.105 +public:
   1.106 +								ConCommandBase( void );
   1.107 +								ConCommandBase( char const *pName, char const *pHelpString = 0, 
   1.108 +									int flags = 0 );
   1.109 +
   1.110 +	virtual						~ConCommandBase( void );
   1.111 +
   1.112 +	virtual	bool				IsCommand( void ) const;
   1.113 +
   1.114 +	// Check flag
   1.115 +	virtual bool				IsBitSet( int flag ) const;
   1.116 +	// Set flag
   1.117 +	virtual void				AddFlags( int flags );
   1.118 +
   1.119 +	// Return name of cvar
   1.120 +	virtual char const			*GetName( void ) const;
   1.121 +
   1.122 +	// Return help text for cvar
   1.123 +	virtual char const			*GetHelpText( void ) const;
   1.124 +
   1.125 +	// Deal with next pointer
   1.126 +	const ConCommandBase		*GetNext( void ) const;
   1.127 +	void						SetNext( ConCommandBase *next );
   1.128 +	
   1.129 +	virtual bool				IsRegistered( void ) const;
   1.130 +
   1.131 +	// Global methods
   1.132 +	static ConCommandBase const	*GetCommands( void );
   1.133 +	static void					AddToList( ConCommandBase *var );
   1.134 +	static void					RemoveFlaggedCommands( int flag );
   1.135 +	static void					RevertFlaggedCvars( int flag );
   1.136 +	static ConCommandBase const	*FindCommand( char const *name );
   1.137 +
   1.138 +protected:
   1.139 +	virtual void				Create( char const *pName, char const *pHelpString = 0, 
   1.140 +									int flags = 0 );
   1.141 +
   1.142 +	// Used internally by OneTimeInit to initialize.
   1.143 +	virtual void				Init();
   1.144 +
   1.145 +	// Internal copy routine ( uses new operator from correct module )
   1.146 +	char						*CopyString( char const *from );
   1.147 +
   1.148 +	// Next ConVar in chain
   1.149 +	ConCommandBase				*m_pNext;
   1.150 +
   1.151 +private:
   1.152 +	// Has the cvar been added to the global list?
   1.153 +	bool						m_bRegistered;
   1.154 +
   1.155 +	// Static data
   1.156 +	char const 					*m_pszName;
   1.157 +	char const 					*m_pszHelpString;
   1.158 +	
   1.159 +	// ConVar flags
   1.160 +	int							m_nFlags;
   1.161 +
   1.162 +protected:
   1.163 +
   1.164 +	// ConVars add themselves to this list for the executable. Then ConVarMgr::Init() runs through 
   1.165 +	// all the console variables and registers them.
   1.166 +	static ConCommandBase		*s_pConCommandBases;
   1.167 +
   1.168 +	// ConVars in this executable use this 'global' to access values.
   1.169 +	static IConCommandBaseAccessor	*s_pAccessor;
   1.170 +};
   1.171 +
   1.172 +//-----------------------------------------------------------------------------
   1.173 +// Purpose: The console invoked command
   1.174 +//-----------------------------------------------------------------------------
   1.175 +class ConCommand : public ConCommandBase
   1.176 +{
   1.177 +friend class ConCommandBaseMgr;
   1.178 +friend class CCvar;
   1.179 +
   1.180 +public:
   1.181 +	typedef ConCommandBase BaseClass;
   1.182 +
   1.183 +								ConCommand( void );
   1.184 +								ConCommand( char const *pName, FnCommandCallback callback, 
   1.185 +									char const *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
   1.186 +
   1.187 +	virtual						~ConCommand( void );
   1.188 +
   1.189 +	virtual	bool				IsCommand( void ) const;
   1.190 +
   1.191 +	virtual int					AutoCompleteSuggest( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] );
   1.192 +
   1.193 +	virtual bool				CanAutoComplete( void );
   1.194 +
   1.195 +	// Invoke the function
   1.196 +	virtual void				Dispatch( void );
   1.197 +
   1.198 +private:
   1.199 +	virtual void				Create( char const *pName, FnCommandCallback callback, 
   1.200 +									char const *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
   1.201 +public:
   1.202 +	// Call this function when executing the command
   1.203 +	FnCommandCallback			m_fnCommandCallback;
   1.204 +
   1.205 +	FnCommandCompletionCallback	m_fnCompletionCallback;
   1.206 +	bool						m_bHasCompletionCallback;
   1.207 +public:
   1.208 +//HACKHACK from BAILOPAN - we want to access this okay!
   1.209 +	FnCommandCallback			GetCallback() { return m_fnCommandCallback; }
   1.210 +};
   1.211 +
   1.212 +//-----------------------------------------------------------------------------
   1.213 +// Purpose: A console variable
   1.214 +//-----------------------------------------------------------------------------
   1.215 +class ConVar : public ConCommandBase
   1.216 +{
   1.217 +friend class ConCommandBaseMgr;
   1.218 +friend class CCvar;
   1.219 +friend class CDefaultCvar;
   1.220 +
   1.221 +public:
   1.222 +	typedef ConCommandBase BaseClass;
   1.223 +
   1.224 +								ConVar( char const *pName, char const *pDefaultValue, int flags = 0);
   1.225 +
   1.226 +								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   1.227 +									char const *pHelpString );
   1.228 +								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   1.229 +									char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax );
   1.230 +								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   1.231 +									char const *pHelpString, FnChangeCallback callback );
   1.232 +								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   1.233 +									char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax,
   1.234 +									FnChangeCallback callback );
   1.235 +
   1.236 +	virtual						~ConVar( void );
   1.237 +
   1.238 +	virtual bool				IsBitSet( int flag ) const;
   1.239 +	virtual char const*			GetHelpText( void ) const;
   1.240 +	virtual bool				IsRegistered( void ) const;
   1.241 +	virtual char const			*GetName( void ) const;
   1.242 +	virtual void				AddFlags( int flags );
   1.243 +	virtual	bool				IsCommand( void ) const;
   1.244 +
   1.245 +	// Install a change callback (there shouldn't already be one....)
   1.246 +	void InstallChangeCallback( FnChangeCallback callback );
   1.247 +
   1.248 +	// Retrieve value
   1.249 +	FORCEINLINE float			GetFloat( void ) const;
   1.250 +	FORCEINLINE int				GetInt( void ) const;
   1.251 +	FORCEINLINE bool			GetBool() const	{  return !!GetInt(); }
   1.252 +	FORCEINLINE char const	   *GetString( void ) const;
   1.253 +
   1.254 +	// Any function that allocates/frees memory needs to be virtual or else you'll have crashes
   1.255 +	//  from alloc/free across dll/exe boundaries.
   1.256 +	
   1.257 +	// These just call into the IConCommandBaseAccessor to check flags and set the var (which ends up calling InternalSetValue).
   1.258 +	virtual void				SetValue( char const *value );
   1.259 +	virtual void				SetValue( float value );
   1.260 +	virtual void				SetValue( int value );
   1.261 +	
   1.262 +	// Reset to default value
   1.263 +	void						Revert( void );
   1.264 +
   1.265 +	// True if it has a min/max setting
   1.266 +	bool						GetMin( float& minVal ) const;
   1.267 +	bool						GetMax( float& maxVal ) const;
   1.268 +	char const					*GetDefault( void ) const;
   1.269 +
   1.270 +	static void					RevertAll( void );
   1.271 +private:
   1.272 +	// Called by CCvar when the value of a var is changing.
   1.273 +	virtual void				InternalSetValue(char const *value);
   1.274 +	// For CVARs marked FCVAR_NEVER_AS_STRING
   1.275 +	virtual void				InternalSetFloatValue( float fNewValue );
   1.276 +	virtual void				InternalSetIntValue( int nValue );
   1.277 +
   1.278 +	virtual bool				ClampValue( float& value );
   1.279 +	virtual void				ChangeStringValue( char const *tempVal );
   1.280 +
   1.281 +	virtual void				Create( char const *pName, char const *pDefaultValue, int flags = 0,
   1.282 +									char const *pHelpString = 0, bool bMin = false, float fMin = 0.0,
   1.283 +									bool bMax = false, float fMax = false, FnChangeCallback callback = 0 );
   1.284 +
   1.285 +	// Used internally by OneTimeInit to initialize.
   1.286 +	virtual void				Init();
   1.287 +
   1.288 +private:
   1.289 +
   1.290 +	// This either points to "this" or it points to the original declaration of a ConVar.
   1.291 +	// This allows ConVars to exist in separate modules, and they all use the first one to be declared.
   1.292 +	// m_pParent->m_pParent must equal m_pParent (ie: m_pParent must be the root, or original, ConVar).
   1.293 +	ConVar						*m_pParent;
   1.294 +
   1.295 +	// Static data
   1.296 +	char const					*m_pszDefaultValue;
   1.297 +	
   1.298 +	// Value
   1.299 +	// Dynamically allocated
   1.300 +	char						*m_pszString;
   1.301 +	int							m_StringLength;
   1.302 +
   1.303 +	// Values
   1.304 +	float						m_fValue;
   1.305 +	int							m_nValue;
   1.306 +
   1.307 +	// Min/Max values
   1.308 +	bool						m_bHasMin;
   1.309 +	float						m_fMinVal;
   1.310 +	bool						m_bHasMax;
   1.311 +	float						m_fMaxVal;
   1.312 +	
   1.313 +	// Call this function when ConVar changes
   1.314 +	FnChangeCallback			m_fnChangeCallback;
   1.315 +};
   1.316 +
   1.317 +//-----------------------------------------------------------------------------
   1.318 +// Purpose: Return ConVar value as a float
   1.319 +// Output : float
   1.320 +//-----------------------------------------------------------------------------
   1.321 +FORCEINLINE float ConVar::GetFloat( void ) const
   1.322 +{
   1.323 +	return m_pParent->m_fValue;
   1.324 +}
   1.325 +
   1.326 +//-----------------------------------------------------------------------------
   1.327 +// Purpose: Return ConVar value as an int
   1.328 +// Output : int
   1.329 +//-----------------------------------------------------------------------------
   1.330 +FORCEINLINE int ConVar::GetInt( void ) const 
   1.331 +{
   1.332 +	return m_pParent->m_nValue;
   1.333 +}
   1.334 +
   1.335 +
   1.336 +//-----------------------------------------------------------------------------
   1.337 +// Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
   1.338 +// Output : char const *
   1.339 +//-----------------------------------------------------------------------------
   1.340 +FORCEINLINE char const *ConVar::GetString( void ) const 
   1.341 +{
   1.342 +	if ( m_nFlags & FCVAR_NEVER_AS_STRING )
   1.343 +	{
   1.344 +		return "FCVAR_NEVER_AS_STRING";
   1.345 +	}
   1.346 +
   1.347 +	return ( m_pParent->m_pszString ) ? m_pParent->m_pszString : "";
   1.348 +}
   1.349 +
   1.350 +
   1.351 +//-----------------------------------------------------------------------------
   1.352 +// Purpose: Utility to quicky generate a simple console command
   1.353 +//-----------------------------------------------------------------------------
   1.354 +#define CON_COMMAND( name, description ) \
   1.355 +   static void name(); \
   1.356 +   static ConCommand name##_command( #name, name, description ); \
   1.357 +   static void name()
   1.358 +
   1.359 +//-----------------------------------------------------------------------------
   1.360 +// Purpose: Utility to quicky generate a simple console command
   1.361 +//-----------------------------------------------------------------------------
   1.362 +#define CON_COMMAND_F( name, description, flags ) \
   1.363 +   static void name(); \
   1.364 +   static ConCommand name##_command( #name, name, description, flags ); \
   1.365 +   static void name()
   1.366 +
   1.367 +
   1.368 +#endif // CONVAR_H