1 //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
8 //-----------------------------------------------------------------------------
10 //=============================================================================//
18 #include "tier0/dbg.h"
20 // The default, no flags at all
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
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
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
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
53 #define FCVAR_NOT_CONNECTED (1<<19) // cvar cannot be changed by a client that is connected to a server
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
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;
71 // You don't have to instantiate one of these, just call its
72 // OneTimeInit function when your executable is initializing.
73 class ConCommandBaseMgr
76 // Call this ONCE when the executable starts up.
77 static void OneTimeInit( IConCommandBaseAccessor *pAccessor );
80 // Called when a ConVar changes value
81 typedef void ( *FnChangeCallback )( ConVar *var, char const *pOldString );
83 // Called when a ConCommand needs to execute
84 typedef void ( *FnCommandCallback )( void );
86 #define COMMAND_COMPLETION_MAXITEMS 64
87 #define COMMAND_COMPLETION_ITEM_LENGTH 64
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 ] );
92 //-----------------------------------------------------------------------------
93 // Purpose: The base console invoked command/cvar interface
94 //-----------------------------------------------------------------------------
97 friend class ConCommandBaseMgr;
100 friend class ConCommand;
103 ConCommandBase( void );
104 ConCommandBase( char const *pName, char const *pHelpString = 0,
107 virtual ~ConCommandBase( void );
109 virtual bool IsCommand( void ) const;
112 virtual bool IsBitSet( int flag ) const;
114 virtual void AddFlags( int flags );
116 // Return name of cvar
117 virtual char const *GetName( void ) const;
119 // Return help text for cvar
120 virtual char const *GetHelpText( void ) const;
122 // Deal with next pointer
123 const ConCommandBase *GetNext( void ) const;
124 void SetNext( ConCommandBase *next );
126 virtual bool IsRegistered( void ) const;
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 );
136 virtual void Create( char const *pName, char const *pHelpString = 0,
139 // Used internally by OneTimeInit to initialize.
142 // Internal copy routine ( uses new operator from correct module )
143 char *CopyString( char const *from );
145 // Next ConVar in chain
146 ConCommandBase *m_pNext;
149 // Has the cvar been added to the global list?
153 char const *m_pszName;
154 char const *m_pszHelpString;
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;
165 // ConVars in this executable use this 'global' to access values.
166 static IConCommandBaseAccessor *s_pAccessor;
169 //-----------------------------------------------------------------------------
170 // Purpose: The console invoked command
171 //-----------------------------------------------------------------------------
172 class ConCommand : public ConCommandBase
174 friend class ConCommandBaseMgr;
178 typedef ConCommandBase BaseClass;
181 ConCommand( char const *pName, FnCommandCallback callback,
182 char const *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
184 virtual ~ConCommand( void );
186 virtual bool IsCommand( void ) const;
188 virtual int AutoCompleteSuggest( char const *partial, char commands[ COMMAND_COMPLETION_MAXITEMS ][ COMMAND_COMPLETION_ITEM_LENGTH ] );
190 virtual bool CanAutoComplete( void );
192 // Invoke the function
193 virtual void Dispatch( void );
196 virtual void Create( char const *pName, FnCommandCallback callback,
197 char const *pHelpString = 0, int flags = 0, FnCommandCompletionCallback completionFunc = 0 );
199 // Call this function when executing the command
200 FnCommandCallback m_fnCommandCallback;
202 FnCommandCompletionCallback m_fnCompletionCallback;
203 bool m_bHasCompletionCallback;
205 //HACKHACK from BAILOPAN - we want to access this okay!
206 FnCommandCallback GetCallback() { return m_fnCommandCallback; }
209 //-----------------------------------------------------------------------------
210 // Purpose: A console variable
211 //-----------------------------------------------------------------------------
212 class ConVar : public ConCommandBase
214 friend class ConCommandBaseMgr;
216 friend class CDefaultCvar;
219 typedef ConCommandBase BaseClass;
221 ConVar( char const *pName, char const *pDefaultValue, int flags = 0);
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 );
233 virtual ~ConVar( void );
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;
242 // Install a change callback (there shouldn't already be one....)
243 void InstallChangeCallback( FnChangeCallback callback );
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;
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.
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 );
259 // Reset to default value
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;
267 static void RevertAll( void );
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 );
275 virtual bool ClampValue( float& value );
276 virtual void ChangeStringValue( char const *tempVal );
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 );
282 // Used internally by OneTimeInit to initialize.
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).
293 char const *m_pszDefaultValue;
296 // Dynamically allocated
310 // Call this function when ConVar changes
311 FnChangeCallback m_fnChangeCallback;
314 //-----------------------------------------------------------------------------
315 // Purpose: Return ConVar value as a float
317 //-----------------------------------------------------------------------------
318 FORCEINLINE float ConVar::GetFloat( void ) const
320 return m_pParent->m_fValue;
323 //-----------------------------------------------------------------------------
324 // Purpose: Return ConVar value as an int
326 //-----------------------------------------------------------------------------
327 FORCEINLINE int ConVar::GetInt( void ) const
329 return m_pParent->m_nValue;
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
339 if ( m_nFlags & FCVAR_NEVER_AS_STRING )
341 return "FCVAR_NEVER_AS_STRING";
344 return ( m_pParent->m_pszString ) ? m_pParent->m_pszString : "";
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 ); \
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 ); \