sourcemm/convar.h
author Scott Ehlert <ds@alliedmods.net>
Mon Apr 17 19:42:55 2006 +0000 (2006-04-17)
changeset 187 d81c2fc9a708
parent 76 39266faeec97
child 232 a86d52ff721c
permissions -rw-r--r--
Fixed a warning in stub and sample :|
     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 #if _WIN32
    15 #pragma once
    16 #endif
    17 
    18 #include "tier0/dbg.h"
    19 
    20 // The default, no flags at all
    21 #define FCVAR_NONE				0 
    22 
    23 // Command to ConVars and ConCommands
    24 #define FCVAR_UNREGISTERED		(1<<0)	// If this is set, don't add to linked list, etc.
    25 #define FCVAR_LAUNCHER			(1<<1) // defined by launcher
    26 #define FCVAR_GAMEDLL			(1<<2)	// defined by the game DLL
    27 #define FCVAR_CLIENTDLL			(1<<3)  // defined by the client DLL
    28 #define FCVAR_MATERIAL_SYSTEM	(1<<4)	// Defined by the material system.
    29 #define FCVAR_STUDIORENDER		(1<<15)	// Defined by the material system.
    30 #define FCVAR_PLUGIN			(1<<18)	// Defined by a 3rd party plugin.
    31 #define FCVAR_CHEAT				(1<<14) // Only useable in singleplayer / debug / multiplayer & sv_cheats
    32 
    33 // ConVar only
    34 #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
    35 #define FCVAR_SPONLY			(1<<6)  // This cvar cannot be changed by clients connected to a multiplayer server.
    36 #define	FCVAR_ARCHIVE			(1<<7)	// set to cause it to be saved to vars.rc
    37 #define	FCVAR_NOTIFY			(1<<8)	// notifies players when changed
    38 #define	FCVAR_USERINFO			(1<<9)	// changes the client's info string
    39 
    40 #define FCVAR_PRINTABLEONLY		(1<<10)  // This cvar's string cannot contain unprintable characters ( e.g., used for player name etc ).
    41 #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
    42 #define FCVAR_NEVER_AS_STRING	(1<<12)  // never try to print that cvar
    43 
    44 // It's a ConVar that's shared between the client and the server.
    45 // At signon, the values of all such ConVars are sent from the server to the client (skipped for local
    46 //  client, of course )
    47 // If a change is requested it must come from the console (i.e., no remote client changes)
    48 // If a value is changed while a server is active, it's replicated to all connected clients
    49 #define FCVAR_REPLICATED		(1<<13)	// server setting enforced on clients, TODO rename to FCAR_SERVER at some time
    50 #define FCVAR_DEMO				(1<<16)  // record this cvar when starting a demo file
    51 #define FCVAR_DONTRECORD		(1<<17)  // don't record these command in demofiles
    52 
    53 #define FCVAR_NOT_CONNECTED		(1<<19)	// cvar cannot be changed by a client that is connected to a server
    54 
    55 
    56 class ConVar;
    57 class ConCommand;
    58 class ConCommandBase;
    59 
    60 // Any executable that wants to use ConVars need to implement one of
    61 // these to hook up access to console variables.
    62 class IConCommandBaseAccessor
    63 {
    64 public:
    65 	// Flags is a combination of FCVAR flags in cvar.h.
    66 	// hOut is filled in with a handle to the variable.
    67 	virtual bool RegisterConCommandBase( ConCommandBase *pVar )=0;
    68 };
    69 
    70 
    71 // You don't have to instantiate one of these, just call its 
    72 // OneTimeInit function when your executable is initializing.
    73 class ConCommandBaseMgr
    74 {
    75 public:
    76 	// Call this ONCE when the executable starts up.
    77 	static void	OneTimeInit( IConCommandBaseAccessor *pAccessor );
    78 };
    79 
    80 // Called when a ConVar changes value
    81 typedef void ( *FnChangeCallback )( ConVar *var, char const *pOldString );
    82 
    83 // Called when a ConCommand needs to execute
    84 typedef void ( *FnCommandCallback )( void );
    85 
    86 #define COMMAND_COMPLETION_MAXITEMS		64
    87 #define COMMAND_COMPLETION_ITEM_LENGTH	64
    88 
    89 // Returns 0 to COMMAND_COMPLETION_MAXITEMS worth of completion strings
    90 typedef int  ( *FnCommandCompletionCallback )( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] );
    91 
    92 //-----------------------------------------------------------------------------
    93 // Purpose: The base console invoked command/cvar interface
    94 //-----------------------------------------------------------------------------
    95 class ConCommandBase
    96 {
    97 	friend class ConCommandBaseMgr;
    98 	friend class CCvar;
    99 	friend class ConVar;
   100 	friend class ConCommand;
   101 
   102 public:
   103 								ConCommandBase( void );
   104 								ConCommandBase( char const *pName, char const *pHelpString = 0, 
   105 									int flags = 0 );
   106 
   107 	virtual						~ConCommandBase( void );
   108 
   109 	virtual	bool				IsCommand( void ) const;
   110 
   111 	// Check flag
   112 	virtual bool				IsBitSet( int flag ) const;
   113 	// Set flag
   114 	virtual void				AddFlags( int flags );
   115 
   116 	// Return name of cvar
   117 	virtual char const			*GetName( void ) const;
   118 
   119 	// Return help text for cvar
   120 	virtual char const			*GetHelpText( void ) const;
   121 
   122 	// Deal with next pointer
   123 	const ConCommandBase		*GetNext( void ) const;
   124 	void						SetNext( ConCommandBase *next );
   125 	
   126 	virtual bool				IsRegistered( void ) const;
   127 
   128 	// Global methods
   129 	static ConCommandBase const	*GetCommands( void );
   130 	static void					AddToList( ConCommandBase *var );
   131 	static void					RemoveFlaggedCommands( int flag );
   132 	static void					RevertFlaggedCvars( int flag );
   133 	static ConCommandBase const	*FindCommand( char const *name );
   134 
   135 protected:
   136 	virtual void				Create( char const *pName, char const *pHelpString = 0, 
   137 									int flags = 0 );
   138 
   139 	// Used internally by OneTimeInit to initialize.
   140 	virtual void				Init();
   141 
   142 	// Internal copy routine ( uses new operator from correct module )
   143 	char						*CopyString( char const *from );
   144 
   145 	// Next ConVar in chain
   146 	ConCommandBase				*m_pNext;
   147 
   148 private:
   149 	// Has the cvar been added to the global list?
   150 	bool						m_bRegistered;
   151 
   152 	// Static data
   153 	char const 					*m_pszName;
   154 	char const 					*m_pszHelpString;
   155 	
   156 	// ConVar flags
   157 	int							m_nFlags;
   158 
   159 protected:
   160 
   161 	// ConVars add themselves to this list for the executable. Then ConVarMgr::Init() runs through 
   162 	// all the console variables and registers them.
   163 	static ConCommandBase		*s_pConCommandBases;
   164 
   165 	// ConVars in this executable use this 'global' to access values.
   166 	static IConCommandBaseAccessor	*s_pAccessor;
   167 };
   168 
   169 //-----------------------------------------------------------------------------
   170 // Purpose: The console invoked command
   171 //-----------------------------------------------------------------------------
   172 class ConCommand : public ConCommandBase
   173 {
   174 friend class ConCommandBaseMgr;
   175 friend class CCvar;
   176 
   177 public:
   178 	typedef ConCommandBase BaseClass;
   179 
   180 								ConCommand( void );
   181 								ConCommand( char const *pName, FnCommandCallback callback, 
   182 									char const *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
   183 
   184 	virtual						~ConCommand( void );
   185 
   186 	virtual	bool				IsCommand( void ) const;
   187 
   188 	virtual int					AutoCompleteSuggest( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] );
   189 
   190 	virtual bool				CanAutoComplete( void );
   191 
   192 	// Invoke the function
   193 	virtual void				Dispatch( void );
   194 
   195 private:
   196 	virtual void				Create( char const *pName, FnCommandCallback callback, 
   197 									char const *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
   198 public:
   199 	// Call this function when executing the command
   200 	FnCommandCallback			m_fnCommandCallback;
   201 
   202 	FnCommandCompletionCallback	m_fnCompletionCallback;
   203 	bool						m_bHasCompletionCallback;
   204 public:
   205 //HACKHACK from BAILOPAN - we want to access this okay!
   206 	FnCommandCallback			GetCallback() { return m_fnCommandCallback; }
   207 };
   208 
   209 //-----------------------------------------------------------------------------
   210 // Purpose: A console variable
   211 //-----------------------------------------------------------------------------
   212 class ConVar : public ConCommandBase
   213 {
   214 friend class ConCommandBaseMgr;
   215 friend class CCvar;
   216 friend class CDefaultCvar;
   217 
   218 public:
   219 	typedef ConCommandBase BaseClass;
   220 
   221 								ConVar( char const *pName, char const *pDefaultValue, int flags = 0);
   222 
   223 								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   224 									char const *pHelpString );
   225 								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   226 									char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax );
   227 								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   228 									char const *pHelpString, FnChangeCallback callback );
   229 								ConVar( char const *pName, char const *pDefaultValue, int flags, 
   230 									char const *pHelpString, bool bMin, float fMin, bool bMax, float fMax,
   231 									FnChangeCallback callback );
   232 
   233 	virtual						~ConVar( void );
   234 
   235 	virtual bool				IsBitSet( int flag ) const;
   236 	virtual char const*			GetHelpText( void ) const;
   237 	virtual bool				IsRegistered( void ) const;
   238 	virtual char const			*GetName( void ) const;
   239 	virtual void				AddFlags( int flags );
   240 	virtual	bool				IsCommand( void ) const;
   241 
   242 	// Install a change callback (there shouldn't already be one....)
   243 	void InstallChangeCallback( FnChangeCallback callback );
   244 
   245 	// Retrieve value
   246 	FORCEINLINE float			GetFloat( void ) const;
   247 	FORCEINLINE int				GetInt( void ) const;
   248 	FORCEINLINE bool			GetBool() const	{  return !!GetInt(); }
   249 	FORCEINLINE char const	   *GetString( void ) const;
   250 
   251 	// Any function that allocates/frees memory needs to be virtual or else you'll have crashes
   252 	//  from alloc/free across dll/exe boundaries.
   253 	
   254 	// These just call into the IConCommandBaseAccessor to check flags and set the var (which ends up calling InternalSetValue).
   255 	virtual void				SetValue( char const *value );
   256 	virtual void				SetValue( float value );
   257 	virtual void				SetValue( int value );
   258 	
   259 	// Reset to default value
   260 	void						Revert( void );
   261 
   262 	// True if it has a min/max setting
   263 	bool						GetMin( float& minVal ) const;
   264 	bool						GetMax( float& maxVal ) const;
   265 	char const					*GetDefault( void ) const;
   266 
   267 	static void					RevertAll( void );
   268 private:
   269 	// Called by CCvar when the value of a var is changing.
   270 	virtual void				InternalSetValue(char const *value);
   271 	// For CVARs marked FCVAR_NEVER_AS_STRING
   272 	virtual void				InternalSetFloatValue( float fNewValue );
   273 	virtual void				InternalSetIntValue( int nValue );
   274 
   275 	virtual bool				ClampValue( float& value );
   276 	virtual void				ChangeStringValue( char const *tempVal );
   277 
   278 	virtual void				Create( char const *pName, char const *pDefaultValue, int flags = 0,
   279 									char const *pHelpString = 0, bool bMin = false, float fMin = 0.0,
   280 									bool bMax = false, float fMax = false, FnChangeCallback callback = 0 );
   281 
   282 	// Used internally by OneTimeInit to initialize.
   283 	virtual void				Init();
   284 
   285 private:
   286 
   287 	// This either points to "this" or it points to the original declaration of a ConVar.
   288 	// This allows ConVars to exist in separate modules, and they all use the first one to be declared.
   289 	// m_pParent->m_pParent must equal m_pParent (ie: m_pParent must be the root, or original, ConVar).
   290 	ConVar						*m_pParent;
   291 
   292 	// Static data
   293 	char const					*m_pszDefaultValue;
   294 	
   295 	// Value
   296 	// Dynamically allocated
   297 	char						*m_pszString;
   298 	int							m_StringLength;
   299 
   300 	// Values
   301 	float						m_fValue;
   302 	int							m_nValue;
   303 
   304 	// Min/Max values
   305 	bool						m_bHasMin;
   306 	float						m_fMinVal;
   307 	bool						m_bHasMax;
   308 	float						m_fMaxVal;
   309 	
   310 	// Call this function when ConVar changes
   311 	FnChangeCallback			m_fnChangeCallback;
   312 };
   313 
   314 //-----------------------------------------------------------------------------
   315 // Purpose: Return ConVar value as a float
   316 // Output : float
   317 //-----------------------------------------------------------------------------
   318 FORCEINLINE float ConVar::GetFloat( void ) const
   319 {
   320 	return m_pParent->m_fValue;
   321 }
   322 
   323 //-----------------------------------------------------------------------------
   324 // Purpose: Return ConVar value as an int
   325 // Output : int
   326 //-----------------------------------------------------------------------------
   327 FORCEINLINE int ConVar::GetInt( void ) const 
   328 {
   329 	return m_pParent->m_nValue;
   330 }
   331 
   332 
   333 //-----------------------------------------------------------------------------
   334 // Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
   335 // Output : char const *
   336 //-----------------------------------------------------------------------------
   337 FORCEINLINE char const *ConVar::GetString( void ) const 
   338 {
   339 	if ( m_nFlags & FCVAR_NEVER_AS_STRING )
   340 	{
   341 		return "FCVAR_NEVER_AS_STRING";
   342 	}
   343 
   344 	return ( m_pParent->m_pszString ) ? m_pParent->m_pszString : "";
   345 }
   346 
   347 
   348 //-----------------------------------------------------------------------------
   349 // Purpose: Utility to quicky generate a simple console command
   350 //-----------------------------------------------------------------------------
   351 #define CON_COMMAND( name, description ) \
   352    static void name(); \
   353    static ConCommand name##_command( #name, name, description ); \
   354    static void name()
   355 
   356 //-----------------------------------------------------------------------------
   357 // Purpose: Utility to quicky generate a simple console command
   358 //-----------------------------------------------------------------------------
   359 #define CON_COMMAND_F( name, description, flags ) \
   360    static void name(); \
   361    static ConCommand name##_command( #name, name, description, flags ); \
   362    static void name()
   363 
   364 
   365 #endif // CONVAR_H