2010-07-22 01:46:14 -05:00
//===== Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
// $Workfile: $
// $Date: $
//
//-----------------------------------------------------------------------------
// $NoKeywords: $
//===========================================================================//
# ifndef CONVAR_H
# define CONVAR_H
# if _WIN32
# pragma once
# endif
2023-04-02 11:52:25 -04:00
# include <interfaces/interfaces.h>
2010-07-22 01:46:14 -05:00
# include "tier0/dbg.h"
# include "tier1/utlvector.h"
# include "tier1/utlstring.h"
2012-05-21 02:49:35 -05:00
# include "Color.h"
2023-03-30 00:54:33 +03:00
# include "mathlib/vector.h"
# include "mathlib/vector2d.h"
# include "mathlib/vector4d.h"
2010-07-22 01:46:14 -05:00
# ifdef _WIN32
# define FORCEINLINE_CVAR FORCEINLINE
# elif POSIX
# define FORCEINLINE_CVAR inline
# else
# error "implement me"
# endif
2023-03-30 00:54:33 +03:00
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class ConVar ;
2023-04-03 23:06:12 -04:00
class ConVarRef ;
2023-03-30 00:54:33 +03:00
class CCommand ;
2023-04-03 23:06:12 -04:00
class ConCommandRef ;
2023-03-30 00:54:33 +03:00
class ConCommandBase ;
struct characterset_t ;
class ConVarRefAbstract ;
class CSplitScreenSlot ;
union CVValue_t ;
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Uncomment me to test for threading issues for material system convars
// NOTE: You want to disable all threading when you do this
// +host_thread_mode 0 +r_threaded_particles 0 +sv_parallel_packentities 0 +sv_disable_querycache 0
//-----------------------------------------------------------------------------
//#define CONVAR_TEST_MATERIAL_THREAD_CONVARS 1
2023-03-31 06:19:32 +03:00
DECLARE_HANDLE_32BIT ( ConVarHandle ) ;
# define CONVAR_ID_INVALID ConVarHandle::MakeHandle( 0xFFFFFFFF )
2023-03-30 00:18:38 +03:00
2023-03-31 06:19:32 +03:00
DECLARE_HANDLE_32BIT ( ConCommandHandle ) ;
# define CONCOMMAND_ID_INVALID ConCommandHandle::MakeHandle( 0xFFFFFFFF )
2023-03-30 00:18:38 +03:00
//-----------------------------------------------------------------------------
// ConVar flags
//-----------------------------------------------------------------------------
// The default, no flags at all
# define FCVAR_NONE 0
// Command to ConVars and ConCommands
// ConVar Systems
# define FCVAR_LINKED_CONCOMMAND (1<<0)
# define FCVAR_DEVELOPMENTONLY (1<<1) // Hidden in released products. Flag is removed automatically if ALLOW_DEVELOPMENT_CVARS is defined.
# define FCVAR_GAMEDLL (1<<2) // defined by the game DLL
# define FCVAR_CLIENTDLL (1<<3) // defined by the client DLL
# define FCVAR_HIDDEN (1<<4) // Hidden. Doesn't appear in find or auto complete. Like DEVELOPMENTONLY, but can't be compiled out.
// ConVar only
# 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
# define FCVAR_SPONLY (1<<6) // This cvar cannot be changed by clients connected to a multiplayer server.
# define FCVAR_ARCHIVE (1<<7) // set to cause it to be saved to vars.rc
# define FCVAR_NOTIFY (1<<8) // notifies players when changed
# define FCVAR_USERINFO (1<<9) // changes the client's info string
# define FCVAR_MISSING0 (1<<10) // Something that hides the cvar from the cvar lookups
# 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
# define FCVAR_MISSING1 (1<<12) // Something that hides the cvar from the cvar lookups
// It's a ConVar that's shared between the client and the server.
// At signon, the values of all such ConVars are sent from the server to the client (skipped for local
// client, of course )
// If a change is requested it must come from the console (i.e., no remote client changes)
// If a value is changed while a server is active, it's replicated to all connected clients
# define FCVAR_REPLICATED (1<<13) // server setting enforced on clients, TODO rename to FCAR_SERVER at some time
# define FCVAR_CHEAT (1<<14) // Only useable in singleplayer / debug / multiplayer & sv_cheats
# define FCVAR_PER_USER (1<<15) // causes varnameN where N == 2 through max splitscreen slots for mod to be autogenerated
# define FCVAR_DEMO (1<<16) // record this cvar when starting a demo file
# define FCVAR_DONTRECORD (1<<17) // don't record these command in demofiles
# define FCVAR_MISSING2 (1<<18)
# define FCVAR_RELEASE (1<<19) // Cvars tagged with this are the only cvars avaliable to customers
# define FCVAR_MENUBAR_ITEM (1<<20)
# define FCVAR_MISSING3 (1<<21)
# define FCVAR_NOT_CONNECTED (1<<22) // cvar cannot be changed by a client that is connected to a server
# define FCVAR_VCONSOLE_FUZZY_MATCHING (1<<23)
# define FCVAR_SERVER_CAN_EXECUTE (1<<24) // the server is allowed to execute this command on clients via ClientCommand/NET_StringCmd/CBaseClientState::ProcessStringCmd.
# define FCVAR_MISSING4 (1<<25)
# define FCVAR_SERVER_CANNOT_QUERY (1<<26) // If this is set, then the server is not allowed to query this cvar's value (via IServerPluginHelpers::StartQueryCvarValue).
# define FCVAR_VCONSOLE_SET_FOCUS (1<<27)
# define FCVAR_CLIENTCMD_CAN_EXECUTE (1<<28) // IVEngineClient::ClientCmd is allowed to execute this command.
// Note: IVEngineClient::ClientCmd_Unrestricted can run any client command.
# define FCVAR_EXECUTE_PER_TICK (1<<29)
//-----------------------------------------------------------------------------
// Called when a ConVar changes value
//-----------------------------------------------------------------------------
typedef void ( * FnChangeCallbackGlobal_t ) ( ConVarRefAbstract * cvar , CSplitScreenSlot nSlot , const char * pNewValue , const char * pOldValue ) ;
typedef void ( * FnChangeCallback_t ) ( ConVarRefAbstract * cvar , CSplitScreenSlot nSlot , CVValue_t * pNewValue , CVValue_t * pOldValue ) ;
//-----------------------------------------------------------------------------
// ConVar & ConCommand creation listener callbacks
//-----------------------------------------------------------------------------
2023-03-30 01:16:42 +03:00
class ICreationListenerCallbacks
2023-03-30 00:18:38 +03:00
{
2023-03-30 01:16:42 +03:00
public :
virtual void ConVarCreationCallback ( ConVarRefAbstract * pNewCvar ) = 0 ;
2023-03-31 06:19:32 +03:00
virtual void ConCommandCreationCallback ( ConCommandRef * pNewCommand ) = 0 ;
2023-03-30 00:18:38 +03:00
} ;
2010-07-22 01:46:14 -05:00
2013-07-12 02:25:04 -04:00
struct CCommandContext
{
CCommandContext ( int index )
{
_index = index ;
}
int Get ( ) const
{
return _index ;
}
private :
int _index ;
} ;
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Any executable that wants to use ConVars need to implement one of
// these to hook up access to console variables.
//-----------------------------------------------------------------------------
class IConCommandBaseAccessor
{
public :
// Flags is a combination of FCVAR flags in cvar.h.
// hOut is filled in with a handle to the variable.
virtual bool RegisterConCommandBase ( ConCommandBase * pVar ) = 0 ;
} ;
//-----------------------------------------------------------------------------
// Helper method for console development
//-----------------------------------------------------------------------------
# if defined( _X360 )
void ConVar_PublishToVXConsole ( ) ;
# endif
//-----------------------------------------------------------------------------
// Called when a ConCommand needs to execute
//-----------------------------------------------------------------------------
2023-03-31 06:19:32 +03:00
typedef void ( * FnCommandCallback_t ) ( const CCommandContext & context ) ;
typedef void ( * FnCommandCallbackDefault_t ) ( const CCommandContext & context , const CCommand & command ) ;
typedef void ( * FnCommandCallbackEmpty_t ) ( ) ;
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Returns 0 to COMMAND_COMPLETION_MAXITEMS worth of completion strings
//-----------------------------------------------------------------------------
2015-07-09 13:07:26 -04:00
typedef int ( * FnCommandCompletionCallback ) ( const char * partial , CUtlVector < CUtlString > & commands ) ;
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Interface version
//-----------------------------------------------------------------------------
class ICommandCallback
{
public :
2015-07-09 13:07:26 -04:00
virtual void CommandCallback ( const CCommandContext & context , const CCommand & command ) = 0 ;
2013-04-22 18:57:11 -04:00
} ;
2010-07-22 01:46:14 -05:00
class ICommandCompletionCallback
{
public :
virtual int CommandCompletionCallback ( const char * pPartial , CUtlVector < CUtlString > & commands ) = 0 ;
} ;
2023-03-28 14:21:16 +03:00
enum EConVarType : short
{
EConVarType_Bool ,
EConVarType_Int16 ,
EConVarType_UInt16 ,
EConVarType_Int32 ,
EConVarType_UInt32 ,
EConVarType_Int64 ,
EConVarType_UInt64 ,
EConVarType_Float32 ,
EConVarType_Float64 ,
EConVarType_String ,
EConVarType_Color ,
EConVarType_Vector2 ,
EConVarType_Vector3 ,
EConVarType_Vector4 ,
EConVarType_Qangle
} ;
union CVValue_t
{
bool m_bValue ;
2023-03-28 18:52:30 +03:00
short m_i16Value ;
uint16 m_u16Value ;
int m_i32Value ;
2023-03-28 14:21:16 +03:00
uint m_u32Value ;
2023-03-28 18:52:30 +03:00
int64 m_i64Value ;
uint64 m_u64Value ;
2023-03-28 14:21:16 +03:00
float m_flValue ;
double m_dbValue ;
const char * m_szValue ;
2023-03-28 18:52:30 +03:00
Color m_clrValue ;
Vector2D m_vec2Value ;
Vector m_vec3Value ;
Vector4D m_vec4Value ;
QAngle m_angValue ;
} ;
2023-03-28 14:21:16 +03:00
struct ConVarDataType_t
{
const char * name ;
int data_size ;
2023-03-31 06:19:32 +03:00
int primitive ;
void * InitValue ; // Only used for string type
2023-03-28 14:52:20 +03:00
void * CloneValue ;
2023-03-31 06:19:32 +03:00
void * DestroyValue ; // Only used for string type
2023-03-28 14:21:16 +03:00
void * FromString ;
void * ToString ;
void * IsEqual ;
void * Clamp ;
2023-03-31 06:19:32 +03:00
2023-03-28 14:21:16 +03:00
const char * default_string_value ;
2023-03-31 06:19:32 +03:00
ConVarDataType_t * default ;
2023-03-28 14:21:16 +03:00
} ;
2023-03-31 06:19:32 +03:00
class ConVarRefAbstract
2023-03-28 14:21:16 +03:00
{
2023-03-31 06:19:32 +03:00
public :
ConVarHandle * handle ;
ConVar * cvar ;
2023-03-28 14:21:16 +03:00
} ;
2023-03-31 06:19:32 +03:00
// Should be size of 56 (0x38)
struct ConVarValueDescription_t
2023-03-28 14:21:16 +03:00
{
2023-03-31 06:19:32 +03:00
// This gets copied to the ConVar class on creation
int unk1 ;
bool has_default ;
bool has_min ;
bool has_max ;
CVValue_t default_value ;
CVValue_t min_value ;
CVValue_t max_value ;
} ;
// Should be size of 96 (0x60)
struct ConVarDesc_t
{
const char * name ;
const char * description ;
2023-03-28 14:21:16 +03:00
int64 flags ;
2023-03-31 06:19:32 +03:00
ConVarValueDescription_t value_info ;
void * callback ;
EConVarType type ;
2023-03-28 14:21:16 +03:00
} ;
2023-03-31 06:19:32 +03:00
// Should be size of 64 (0x40)
2023-03-28 14:21:16 +03:00
class ConVar
{
public :
const char * name ;
CVValue_t * defaultValue ;
CVValue_t * minValue ;
CVValue_t * maxValue ;
const char * description ;
EConVarType type ;
2023-03-31 06:19:32 +03:00
// This gets copied from the ConVarDesc_t on creation
short unk1 ;
2023-03-28 14:21:16 +03:00
unsigned int timesChanged ;
int64 flags ;
2023-03-31 06:19:32 +03:00
unsigned int callback_index ;
// Used when setting default, max, min values from the ConVarDesc_t
// although that's not the only place of usage
// flags seems to be:
// (1 << 0) Skip setting value to split screen slots and also something keyvalues related
// (1 << 1) Skip setting default value
// (1 << 2) Skip setting min/max values
int allocation_flag_of_some_sort ;
CVValue_t values [ ] ;
2023-03-28 14:21:16 +03:00
} ;
2023-03-31 06:19:32 +03:00
class ConCommandRef
2023-03-28 14:21:16 +03:00
{
2023-03-31 06:19:32 +03:00
ConCommandHandle handle ;
} ;
2023-03-28 14:21:16 +03:00
2023-03-31 06:19:32 +03:00
class ConCommandBase
{
public :
const char * name ;
const char * description ;
int64 flags ;
2023-03-28 14:21:16 +03:00
} ;
2023-03-31 06:19:32 +03:00
struct ConCommandCB
{
// Call this function when executing the command
union
{
void * m_fnCallbackAny ;
FnCommandCallback_t m_fnCommandCallback ;
FnCommandCallbackEmpty_t m_fnCommandCallbackEmpty ;
FnCommandCallbackDefault_t m_fnCommandCallbackDefault ;
ICommandCallback * m_pCommandCallback ;
} ;
bool m_bUsingCommandCallbackInterface : 1 ;
bool m_bUsingEmptyCommandCallback : 1 ;
bool m_bUsingCommandCallback : 1 ;
} ;
// Should be size of 64 (0x40)
2023-04-02 11:52:25 -04:00
class ConCommandDesc_t : public ConCommandBase
2023-03-28 14:21:16 +03:00
{
2023-04-02 11:52:25 -04:00
public :
ConCommandDesc_t ( const char * pName , FnCommandCallback_t callback , const char * pHelpString = 0 , int64 flags = 0 )
{
name = pName ;
description = pHelpString ;
this - > flags = flags ;
this - > callback . m_fnCommandCallback = callback ;
this - > callback . m_bUsingCommandCallback = true ;
this - > callback . m_bUsingCommandCallbackInterface = false ;
this - > callback . m_bUsingEmptyCommandCallback = false ;
}
ConCommandDesc_t ( const char * pName , ICommandCallback * pCallback , const char * pHelpString = 0 , int64 flags = 0 )
{
name = pName ;
description = pHelpString ;
this - > flags = flags ;
this - > callback . m_pCommandCallback = pCallback ;
this - > callback . m_bUsingCommandCallback = false ;
this - > callback . m_bUsingCommandCallbackInterface = true ;
this - > callback . m_bUsingEmptyCommandCallback = false ;
}
ConCommandRef * Register ( ConCommandHandle & hndl )
{
2023-04-03 23:06:12 -04:00
//return g_pCVar->RegisterConCommand(hndl, this);
2023-04-02 11:52:25 -04:00
}
2023-03-28 14:21:16 +03:00
public :
2023-03-31 06:19:32 +03:00
ConCommandCB callback ;
ConCommandCB autocompletion_callback ;
ConCommandRef * parent ;
2023-03-28 14:21:16 +03:00
} ;
2023-03-31 06:19:32 +03:00
// Should be size of 48 (0x30) (56 in linked list)
2023-04-02 11:52:25 -04:00
class ConCommand : public ConCommandBase
2023-03-31 06:19:32 +03:00
{
public :
ConCommandCB autocompletion_callback ;
int ccvar_autocomplete_callback_index ;
int ccvar_callbackslist_callback_index ;
} ;
//class CConCommandMemberAccessor : IConCommandAccessor, ConCommandRef
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Purpose: The base console invoked command/cvar interface
//-----------------------------------------------------------------------------
2023-03-28 14:21:16 +03:00
#if 0
2010-07-22 01:46:14 -05:00
class ConCommandBase
{
friend class CCvar ;
friend class ConVar ;
friend class ConCommand ;
2018-03-14 18:09:45 -04:00
friend void ConVar_Register ( int64 nCVarFlag , IConCommandBaseAccessor * pAccessor ) ;
2010-07-22 01:46:14 -05:00
friend void ConVar_PublishToVXConsole ( ) ;
// FIXME: Remove when ConVar changes are done
friend class CDefaultCvar ;
public :
ConCommandBase ( void ) ;
ConCommandBase ( const char * pName , const char * pHelpString = 0 ,
2018-03-13 17:54:16 -04:00
int64 flags = 0 ) ;
2010-07-22 01:46:14 -05:00
virtual ~ ConCommandBase ( void ) ;
virtual bool IsCommand ( void ) const ;
2015-10-19 15:53:41 -04:00
virtual bool IsBoundedVar ( void ) const ;
2010-07-22 01:46:14 -05:00
// Check flag
2015-07-09 08:58:19 -04:00
virtual bool IsFlagSet ( int64 flag ) const ;
2010-07-22 01:46:14 -05:00
// Set flag
2015-07-09 08:58:19 -04:00
virtual void AddFlags ( int64 flags ) ;
2010-07-22 01:46:14 -05:00
// Clear flag
2015-07-09 08:58:19 -04:00
virtual void RemoveFlags ( int64 flags ) ;
2010-07-22 01:46:14 -05:00
2015-07-09 08:58:19 -04:00
virtual int64 GetFlags ( ) const ;
2010-07-22 01:46:14 -05:00
// Return name of cvar
virtual const char * GetName ( void ) const ;
// Return help text for cvar
virtual const char * GetHelpText ( void ) const ;
// Deal with next pointer
const ConCommandBase * GetNext ( void ) const ;
ConCommandBase * GetNext ( void ) ;
virtual bool IsRegistered ( void ) const ;
// Returns the DLL identifier
virtual CVarDLLIdentifier_t GetDLLIdentifier ( ) const ;
protected :
virtual void Create ( const char * pName , const char * pHelpString = 0 ,
2018-03-13 17:54:16 -04:00
int64 flags = 0 ) ;
2010-07-22 01:46:14 -05:00
// Used internally by OneTimeInit to initialize/shutdown
virtual void Init ( ) ;
void Shutdown ( ) ;
// Internal copy routine ( uses new operator from correct module )
char * CopyString ( const char * from ) ;
2015-07-09 08:58:19 -04:00
private :
2010-07-22 01:46:14 -05:00
// Next ConVar in chain
// Prior to register, it points to the next convar in the DLL.
// Once registered, though, m_pNext is reset to point to the next
// convar in the global list
ConCommandBase * m_pNext ;
// Has the cvar been added to the global list?
bool m_bRegistered ;
// Static data
const char * m_pszName ;
const char * m_pszHelpString ;
// ConVar flags
2015-07-09 08:58:19 -04:00
int64 m_nFlags ;
2010-07-22 01:46:14 -05:00
protected :
// ConVars add themselves to this list for the executable.
// Then ConVar_Register runs through all the console variables
// and registers them into a global list stored in vstdlib.dll
static ConCommandBase * s_pConCommandBases ;
// ConVars in this executable use this 'global' to access values.
static IConCommandBaseAccessor * s_pAccessor ;
} ;
2023-03-28 14:21:16 +03:00
# endif
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Command tokenizer
//-----------------------------------------------------------------------------
class CCommand
{
public :
CCommand ( ) ;
CCommand ( int nArgC , const char * * ppArgV ) ;
bool Tokenize ( const char * pCommand , characterset_t * pBreakSet = NULL ) ;
void Reset ( ) ;
int ArgC ( ) const ;
const char * * ArgV ( ) const ;
const char * ArgS ( ) const ; // All args that occur after the 0th arg, in string form
const char * GetCommandString ( ) const ; // The entire command in string form, including the 0th arg
const char * operator [ ] ( int nIndex ) const ; // Gets at arguments
const char * Arg ( int nIndex ) const ; // Gets at arguments
// Helper functions to parse arguments to commands.
const char * FindArg ( const char * pName ) const ;
int FindArgInt ( const char * pName , int nDefaultVal ) const ;
static int MaxCommandLength ( ) ;
static characterset_t * DefaultBreakSet ( ) ;
private :
enum
{
COMMAND_MAX_ARGC = 64 ,
COMMAND_MAX_LENGTH = 512 ,
} ;
2023-04-03 22:53:14 -04:00
int m_nArgv0Size ;
CUtlVectorFixedGrowable < char , COMMAND_MAX_LENGTH > m_ArgSBuffer ;
CUtlVectorFixedGrowable < char , COMMAND_MAX_LENGTH > m_ArgvBuffer ;
CUtlVectorFixedGrowable < char * , COMMAND_MAX_ARGC > m_Args ;
2010-07-22 01:46:14 -05:00
} ;
inline int CCommand : : MaxCommandLength ( )
{
return COMMAND_MAX_LENGTH - 1 ;
}
inline int CCommand : : ArgC ( ) const
{
2023-04-03 22:53:14 -04:00
return m_Args . Count ( ) ;
2010-07-22 01:46:14 -05:00
}
inline const char * * CCommand : : ArgV ( ) const
{
2023-04-03 22:53:14 -04:00
return ArgC ( ) ? ( const char * * ) m_Args . Base ( ) : NULL ;
2010-07-22 01:46:14 -05:00
}
inline const char * CCommand : : ArgS ( ) const
{
2023-04-03 22:53:14 -04:00
return m_nArgv0Size ? * ( const char * * ) ( m_ArgSBuffer . Base ( ) + m_nArgv0Size ) : " " ;
2010-07-22 01:46:14 -05:00
}
inline const char * CCommand : : GetCommandString ( ) const
{
2023-04-03 22:53:14 -04:00
return ArgC ( ) ? m_ArgSBuffer . Base ( ) : " " ;
2010-07-22 01:46:14 -05:00
}
inline const char * CCommand : : Arg ( int nIndex ) const
{
// FIXME: Many command handlers appear to not be particularly careful
// about checking for valid argc range. For now, we're going to
// do the extra check and return an empty string if it's out of range
2023-04-03 22:53:14 -04:00
if ( nIndex < 0 | | nIndex > = ArgC ( ) )
2010-07-22 01:46:14 -05:00
return " " ;
2023-04-03 22:53:14 -04:00
return m_Args [ nIndex ] ;
2010-07-22 01:46:14 -05:00
}
inline const char * CCommand : : operator [ ] ( int nIndex ) const
{
return Arg ( nIndex ) ;
}
2023-03-28 14:21:16 +03:00
#if 0
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Purpose: The console invoked command
//-----------------------------------------------------------------------------
class ConCommand : public ConCommandBase
{
friend class CCvar ;
public :
typedef ConCommandBase BaseClass ;
ConCommand ( const char * pName , FnCommandCallback_t callback ,
2018-03-13 17:54:16 -04:00
const char * pHelpString = 0 , int64 flags = 0 , FnCommandCompletionCallback completionFunc = 0 ) ;
2016-08-29 19:16:45 -04:00
ConCommand ( const char * pName , FnCommandCallbackV1_t callback ,
2018-03-13 17:54:16 -04:00
const char * pHelpString = 0 , int64 flags = 0 , FnCommandCompletionCallback completionFunc = 0 ) ;
2016-08-29 19:16:45 -04:00
ConCommand ( const char * pName , FnCommandCallbackV2_t callback ,
2018-03-13 17:54:16 -04:00
const char * pHelpString = 0 , int64 flags = 0 , FnCommandCompletionCallback completionFunc = 0 ) ;
2010-07-22 01:46:14 -05:00
ConCommand ( const char * pName , ICommandCallback * pCallback ,
2018-03-13 17:54:16 -04:00
const char * pHelpString = 0 , int64 flags = 0 , ICommandCompletionCallback * pCommandCompletionCallback = 0 ) ;
2010-07-22 01:46:14 -05:00
virtual ~ ConCommand ( void ) ;
virtual bool IsCommand ( void ) const ;
virtual int AutoCompleteSuggest ( const char * partial , CUtlVector < CUtlString > & commands ) ;
virtual bool CanAutoComplete ( void ) ;
// Invoke the function
2013-07-12 02:25:04 -04:00
virtual void Dispatch ( const CCommandContext & context , const CCommand & command ) ;
2010-07-22 01:46:14 -05:00
private :
// NOTE: To maintain backward compat, we have to be very careful:
// All public virtual methods must appear in the same order always
// since engine code will be calling into this code, which *does not match*
// in the mod code; it's using slightly different, but compatible versions
// of this class. Also: Be very careful about adding new fields to this class.
// Those fields will not exist in the version of this class that is instanced
// in mod code.
union
{
FnCommandCompletionCallback m_fnCompletionCallback ;
ICommandCompletionCallback * m_pCommandCompletionCallback ;
} ;
bool m_bHasCompletionCallback : 1 ;
2015-07-09 13:07:26 -04:00
bool m_bUsingCommandCompletionInterface : 1 ;
struct ConCommandCB
{
// Call this function when executing the command
union
{
2016-08-29 19:16:45 -04:00
void * m_fnCallbackAny ;
2015-07-09 13:07:26 -04:00
FnCommandCallback_t m_fnCommandCallback ;
2016-08-29 19:16:45 -04:00
FnCommandCallbackV1_t m_fnCommandCallbackV1 ;
2015-07-09 13:07:26 -04:00
FnCommandCallbackV2_t m_fnCommandCallbackV2 ;
ICommandCallback * m_pCommandCallback ;
} ;
bool m_bUsingCommandCallbackInterface : 1 ;
bool m_bUsingOldCommandCallback : 1 ;
2016-08-29 19:16:45 -04:00
bool m_bUsingV1CommandCallback : 1 ;
2015-07-09 13:07:26 -04:00
bool m_bUsingV2CommandCallback : 1 ;
} ;
CUtlVector < ConCommandCB > m_Callbacks ;
ConCommand * m_pParent ;
2010-07-22 01:46:14 -05:00
} ;
//-----------------------------------------------------------------------------
// Purpose: A console variable
//-----------------------------------------------------------------------------
class ConVar : public ConCommandBase , public IConVar
{
friend class CCvar ;
friend class ConVarRef ;
friend class SplitScreenConVarRef ;
public :
typedef ConCommandBase BaseClass ;
2018-03-13 17:54:16 -04:00
ConVar ( const char * pName , const char * pDefaultValue , int64 flags = 0 ) ;
2010-07-22 01:46:14 -05:00
2018-03-13 17:54:16 -04:00
ConVar ( const char * pName , const char * pDefaultValue , int64 flags ,
2010-07-22 01:46:14 -05:00
const char * pHelpString ) ;
2018-03-13 17:54:16 -04:00
ConVar ( const char * pName , const char * pDefaultValue , int64 flags ,
2010-07-22 01:46:14 -05:00
const char * pHelpString , bool bMin , float fMin , bool bMax , float fMax ) ;
2018-03-13 17:54:16 -04:00
ConVar ( const char * pName , const char * pDefaultValue , int64 flags ,
2010-07-22 01:46:14 -05:00
const char * pHelpString , FnChangeCallback_t callback ) ;
2018-03-13 17:54:16 -04:00
ConVar ( const char * pName , const char * pDefaultValue , int64 flags ,
2010-07-22 01:46:14 -05:00
const char * pHelpString , bool bMin , float fMin , bool bMax , float fMax ,
FnChangeCallback_t callback ) ;
virtual ~ ConVar ( void ) ;
2015-07-09 08:58:19 -04:00
virtual bool IsFlagSet ( int64 flag ) const ;
2010-07-22 01:46:14 -05:00
virtual const char * GetHelpText ( void ) const ;
virtual bool IsRegistered ( void ) const ;
virtual const char * GetName ( void ) const ;
// Return name of command (usually == GetName(), except in case of FCVAR_SS_ADDED vars
virtual const char * GetBaseName ( void ) const ;
virtual int GetSplitScreenPlayerSlot ( ) const ;
2015-07-09 08:58:19 -04:00
virtual void AddFlags ( int64 flags ) ;
virtual int64 GetFlags ( ) const ;
2010-07-22 01:46:14 -05:00
virtual bool IsCommand ( void ) const ;
// Install a change callback (there shouldn't already be one....)
void InstallChangeCallback ( FnChangeCallback_t callback , bool bInvoke = true ) ;
void RemoveChangeCallback ( FnChangeCallback_t callbackToRemove ) ;
int GetChangeCallbackCount ( ) const { return m_pParent - > m_fnChangeCallbacks . Count ( ) ; }
FnChangeCallback_t GetChangeCallback ( int slot ) const { return m_pParent - > m_fnChangeCallbacks [ slot ] ; }
// Retrieve value
FORCEINLINE_CVAR float GetFloat ( void ) const ;
FORCEINLINE_CVAR int GetInt ( void ) const ;
FORCEINLINE_CVAR Color GetColor ( void ) const ;
FORCEINLINE_CVAR bool GetBool ( ) const { return ! ! GetInt ( ) ; }
FORCEINLINE_CVAR char const * GetString ( void ) const ;
// Compiler driven selection for template use
template < typename T > T Get ( void ) const ;
template < typename T > T Get ( T * ) const ;
// Any function that allocates/frees memory needs to be virtual or else you'll have crashes
// from alloc/free across dll/exe boundaries.
// These just call into the IConCommandBaseAccessor to check flags and set the var (which ends up calling InternalSetValue).
virtual void SetValue ( const char * value ) ;
virtual void SetValue ( float value ) ;
virtual void SetValue ( int value ) ;
virtual void SetValue ( Color value ) ;
// Reset to default value
void Revert ( void ) ;
// True if it has a min/max setting
bool HasMin ( ) const ;
bool HasMax ( ) const ;
bool GetMin ( float & minVal ) const ;
bool GetMax ( float & maxVal ) const ;
float GetMinValue ( ) const ;
float GetMaxValue ( ) const ;
const char * GetDefault ( void ) const ;
// Value
struct CVValue_t
{
char * m_pszString ;
int m_StringLength ;
// Values
float m_fValue ;
int m_nValue ;
} ;
FORCEINLINE_CVAR CVValue_t & GetRawValue ( )
{
return m_Value ;
}
FORCEINLINE_CVAR const CVValue_t & GetRawValue ( ) const
{
return m_Value ;
}
2015-10-19 15:53:41 -04:00
virtual float GetFloatVirtualized ( void ) const ;
virtual int GetIntVirtualized ( void ) const ;
virtual bool GetBoolVirtualized ( void ) const ;
2010-07-22 01:46:14 -05:00
private :
bool InternalSetColorFromString ( const char * value ) ;
// Called by CCvar when the value of a var is changing.
virtual void InternalSetValue ( const char * value ) ;
// For CVARs marked FCVAR_NEVER_AS_STRING
virtual void InternalSetFloatValue ( float fNewValue ) ;
virtual void InternalSetIntValue ( int nValue ) ;
virtual void InternalSetColorValue ( Color value ) ;
virtual bool ClampValue ( float & value ) ;
virtual void ChangeStringValue ( const char * tempVal , float flOldValue ) ;
2018-03-13 17:54:16 -04:00
virtual void Create ( const char * pName , const char * pDefaultValue , int64 flags = 0 ,
2010-07-22 01:46:14 -05:00
const char * pHelpString = 0 , bool bMin = false , float fMin = 0.0 ,
bool bMax = false , float fMax = false , FnChangeCallback_t callback = 0 ) ;
// Used internally by OneTimeInit to initialize.
virtual void Init ( ) ;
protected :
// This either points to "this" or it points to the original declaration of a ConVar.
// This allows ConVars to exist in separate modules, and they all use the first one to be declared.
// m_pParent->m_pParent must equal m_pParent (ie: m_pParent must be the root, or original, ConVar).
ConVar * m_pParent ;
// Static data
const char * m_pszDefaultValue ;
CVValue_t m_Value ;
// Min/Max values
bool m_bHasMin ;
float m_fMinVal ;
bool m_bHasMax ;
float m_fMaxVal ;
// Call this function when ConVar changes
CUtlVector < FnChangeCallback_t > m_fnChangeCallbacks ;
} ;
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a float
// Output : float
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR float ConVar : : GetFloat ( void ) const
{
# ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
Assert ( ThreadInMainThread ( ) | | IsFlagSet ( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) ) ;
# endif
return m_pParent - > m_Value . m_fValue ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as an int
// Output : int
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR int ConVar : : GetInt ( void ) const
{
# ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
Assert ( ThreadInMainThread ( ) | | IsFlagSet ( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) ) ;
# endif
return m_pParent - > m_Value . m_nValue ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a color
// Output : Color
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR Color ConVar : : GetColor ( void ) const
{
# ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
Assert ( ThreadInMainThread ( ) | | IsFlagSet ( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) ) ;
# endif
unsigned char * pColorElement = ( ( unsigned char * ) & m_pParent - > m_Value . m_nValue ) ;
return Color ( pColorElement [ 0 ] , pColorElement [ 1 ] , pColorElement [ 2 ] , pColorElement [ 3 ] ) ;
}
//-----------------------------------------------------------------------------
template < > FORCEINLINE_CVAR float ConVar : : Get < float > ( void ) const { return GetFloat ( ) ; }
template < > FORCEINLINE_CVAR int ConVar : : Get < int > ( void ) const { return GetInt ( ) ; }
template < > FORCEINLINE_CVAR bool ConVar : : Get < bool > ( void ) const { return GetBool ( ) ; }
template < > FORCEINLINE_CVAR const char * ConVar : : Get < const char * > ( void ) const { return GetString ( ) ; }
template < > FORCEINLINE_CVAR float ConVar : : Get < float > ( float * p ) const { return ( * p = GetFloat ( ) ) ; }
template < > FORCEINLINE_CVAR int ConVar : : Get < int > ( int * p ) const { return ( * p = GetInt ( ) ) ; }
template < > FORCEINLINE_CVAR bool ConVar : : Get < bool > ( bool * p ) const { return ( * p = GetBool ( ) ) ; }
template < > FORCEINLINE_CVAR const char * ConVar : : Get < const char * > ( char const * * p ) const { return ( * p = GetString ( ) ) ; }
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
// Output : const char *
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR const char * ConVar : : GetString ( void ) const
{
# ifdef CONVAR_TEST_MATERIAL_THREAD_CONVARS
Assert ( ThreadInMainThread ( ) | | IsFlagSet ( FCVAR_MATERIAL_THREAD_MASK | FCVAR_ACCESSIBLE_FROM_THREADS ) ) ;
# endif
if ( m_nFlags & FCVAR_NEVER_AS_STRING )
return " FCVAR_NEVER_AS_STRING " ;
char const * str = m_pParent - > m_Value . m_pszString ;
return str ? str : " " ;
}
class CSplitScreenAddedConVar : public ConVar
{
typedef ConVar BaseClass ;
public :
CSplitScreenAddedConVar ( int nSplitScreenSlot , const char * pName , const ConVar * pBaseVar ) :
BaseClass
(
pName ,
pBaseVar - > GetDefault ( ) ,
// Keep basevar flags, except remove _SS and add _SS_ADDED instead
( pBaseVar - > GetFlags ( ) & ~ FCVAR_SS ) | FCVAR_SS_ADDED ,
pBaseVar - > GetHelpText ( ) ,
pBaseVar - > HasMin ( ) ,
pBaseVar - > GetMinValue ( ) ,
pBaseVar - > HasMax ( ) ,
pBaseVar - > GetMaxValue ( )
) ,
m_pBaseVar ( pBaseVar ) ,
m_nSplitScreenSlot ( nSplitScreenSlot )
{
for ( int i = 0 ; i < pBaseVar - > GetChangeCallbackCount ( ) ; + + i )
{
InstallChangeCallback ( pBaseVar - > GetChangeCallback ( i ) , false ) ;
}
Assert ( nSplitScreenSlot > = 1 ) ;
Assert ( nSplitScreenSlot < MAX_SPLITSCREEN_CLIENTS ) ;
Assert ( m_pBaseVar ) ;
Assert ( IsFlagSet ( FCVAR_SS_ADDED ) ) ;
Assert ( ! IsFlagSet ( FCVAR_SS ) ) ;
}
const ConVar * GetBaseVar ( ) const ;
virtual const char * GetBaseName ( ) const ;
void SetSplitScreenPlayerSlot ( int nSlot ) ;
virtual int GetSplitScreenPlayerSlot ( ) const ;
protected :
const ConVar * m_pBaseVar ;
int m_nSplitScreenSlot ;
} ;
FORCEINLINE_CVAR const ConVar * CSplitScreenAddedConVar : : GetBaseVar ( ) const
{
Assert ( m_pBaseVar ) ;
return m_pBaseVar ;
}
FORCEINLINE_CVAR const char * CSplitScreenAddedConVar : : GetBaseName ( ) const
{
Assert ( m_pBaseVar ) ;
return m_pBaseVar - > GetName ( ) ;
}
FORCEINLINE_CVAR void CSplitScreenAddedConVar : : SetSplitScreenPlayerSlot ( int nSlot )
{
m_nSplitScreenSlot = nSlot ;
}
FORCEINLINE_CVAR int CSplitScreenAddedConVar : : GetSplitScreenPlayerSlot ( ) const
{
return m_nSplitScreenSlot ;
}
2023-03-28 14:21:16 +03:00
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Used to read/write convars that already exist (replaces the FindVar method)
//-----------------------------------------------------------------------------
class ConVarRef
{
public :
ConVarRef ( const char * pName ) ;
ConVarRef ( const char * pName , bool bIgnoreMissing ) ;
ConVarRef ( IConVar * pConVar ) ;
void Init ( const char * pName , bool bIgnoreMissing ) ;
bool IsValid ( ) const ;
2015-07-09 08:58:19 -04:00
bool IsFlagSet ( int64 nFlags ) const ;
2010-07-22 01:46:14 -05:00
IConVar * GetLinkedConVar ( ) ;
// Get/Set value
float GetFloat ( void ) const ;
int GetInt ( void ) const ;
Color GetColor ( void ) const ;
bool GetBool ( ) const { return ! ! GetInt ( ) ; }
const char * GetString ( void ) const ;
void SetValue ( const char * pValue ) ;
void SetValue ( float flValue ) ;
void SetValue ( int nValue ) ;
void SetValue ( Color value ) ;
void SetValue ( bool bValue ) ;
const char * GetName ( ) const ;
const char * GetDefault ( ) const ;
const char * GetBaseName ( ) const ;
int GetSplitScreenPlayerSlot ( ) const ;
private :
// High-speed method to read convar data
IConVar * m_pConVar ;
ConVar * m_pConVarState ;
} ;
//-----------------------------------------------------------------------------
// Did we find an existing convar of that name?
//-----------------------------------------------------------------------------
2015-07-09 08:58:19 -04:00
FORCEINLINE_CVAR bool ConVarRef : : IsFlagSet ( int64 nFlags ) const
2010-07-22 01:46:14 -05:00
{
return ( m_pConVar - > IsFlagSet ( nFlags ) ! = 0 ) ;
}
FORCEINLINE_CVAR IConVar * ConVarRef : : GetLinkedConVar ( )
{
return m_pConVar ;
}
FORCEINLINE_CVAR const char * ConVarRef : : GetName ( ) const
{
return m_pConVar - > GetName ( ) ;
}
FORCEINLINE_CVAR const char * ConVarRef : : GetBaseName ( ) const
{
return m_pConVar - > GetBaseName ( ) ;
}
FORCEINLINE_CVAR int ConVarRef : : GetSplitScreenPlayerSlot ( ) const
{
return m_pConVar - > GetSplitScreenPlayerSlot ( ) ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a float
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR float ConVarRef : : GetFloat ( void ) const
{
return m_pConVarState - > m_Value . m_fValue ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as an int
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR int ConVarRef : : GetInt ( void ) const
{
return m_pConVarState - > m_Value . m_nValue ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a color
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR Color ConVarRef : : GetColor ( void ) const
{
return m_pConVarState - > GetColor ( ) ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR const char * ConVarRef : : GetString ( void ) const
{
Assert ( ! IsFlagSet ( FCVAR_NEVER_AS_STRING ) ) ;
return m_pConVarState - > m_Value . m_pszString ;
}
FORCEINLINE_CVAR void ConVarRef : : SetValue ( const char * pValue )
{
m_pConVar - > SetValue ( pValue ) ;
}
FORCEINLINE_CVAR void ConVarRef : : SetValue ( float flValue )
{
m_pConVar - > SetValue ( flValue ) ;
}
FORCEINLINE_CVAR void ConVarRef : : SetValue ( int nValue )
{
m_pConVar - > SetValue ( nValue ) ;
}
FORCEINLINE_CVAR void ConVarRef : : SetValue ( Color value )
{
m_pConVar - > SetValue ( value ) ;
}
FORCEINLINE_CVAR void ConVarRef : : SetValue ( bool bValue )
{
m_pConVar - > SetValue ( bValue ? 1 : 0 ) ;
}
FORCEINLINE_CVAR const char * ConVarRef : : GetDefault ( ) const
{
return m_pConVarState - > m_pszDefaultValue ;
}
//-----------------------------------------------------------------------------
// Helper for referencing splitscreen convars (i.e., "name" and "name2")
//-----------------------------------------------------------------------------
class SplitScreenConVarRef
{
public :
SplitScreenConVarRef ( const char * pName ) ;
SplitScreenConVarRef ( const char * pName , bool bIgnoreMissing ) ;
SplitScreenConVarRef ( IConVar * pConVar ) ;
void Init ( const char * pName , bool bIgnoreMissing ) ;
bool IsValid ( ) const ;
2015-07-09 08:58:19 -04:00
bool IsFlagSet ( int64 nFlags ) const ;
2010-07-22 01:46:14 -05:00
// Get/Set value
float GetFloat ( int nSlot ) const ;
int GetInt ( int nSlot ) const ;
Color GetColor ( int nSlot ) const ;
bool GetBool ( int nSlot ) const { return ! ! GetInt ( nSlot ) ; }
const char * GetString ( int nSlot ) const ;
void SetValue ( int nSlot , const char * pValue ) ;
void SetValue ( int nSlot , float flValue ) ;
void SetValue ( int nSlot , int nValue ) ;
void SetValue ( int nSlot , Color value ) ;
void SetValue ( int nSlot , bool bValue ) ;
const char * GetName ( int nSlot ) const ;
const char * GetDefault ( ) const ;
const char * GetBaseName ( ) const ;
private :
struct cv_t
{
IConVar * m_pConVar ;
ConVar * m_pConVarState ;
} ;
cv_t m_Info [ MAX_SPLITSCREEN_CLIENTS ] ;
} ;
//-----------------------------------------------------------------------------
// Did we find an existing convar of that name?
//-----------------------------------------------------------------------------
2015-07-09 08:58:19 -04:00
FORCEINLINE_CVAR bool SplitScreenConVarRef : : IsFlagSet ( int64 nFlags ) const
2010-07-22 01:46:14 -05:00
{
return ( m_Info [ 0 ] . m_pConVar - > IsFlagSet ( nFlags ) ! = 0 ) ;
}
FORCEINLINE_CVAR const char * SplitScreenConVarRef : : GetName ( int nSlot ) const
{
return m_Info [ nSlot ] . m_pConVar - > GetName ( ) ;
}
FORCEINLINE_CVAR const char * SplitScreenConVarRef : : GetBaseName ( ) const
{
return m_Info [ 0 ] . m_pConVar - > GetBaseName ( ) ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a float
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR float SplitScreenConVarRef : : GetFloat ( int nSlot ) const
{
return m_Info [ nSlot ] . m_pConVarState - > m_Value . m_fValue ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as an int
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR int SplitScreenConVarRef : : GetInt ( int nSlot ) const
{
return m_Info [ nSlot ] . m_pConVarState - > m_Value . m_nValue ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as an int
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR Color SplitScreenConVarRef : : GetColor ( int nSlot ) const
{
return m_Info [ nSlot ] . m_pConVarState - > GetColor ( ) ;
}
//-----------------------------------------------------------------------------
// Purpose: Return ConVar value as a string, return "" for bogus string pointer, etc.
//-----------------------------------------------------------------------------
FORCEINLINE_CVAR const char * SplitScreenConVarRef : : GetString ( int nSlot ) const
{
Assert ( ! IsFlagSet ( FCVAR_NEVER_AS_STRING ) ) ;
return m_Info [ nSlot ] . m_pConVarState - > m_Value . m_pszString ;
}
FORCEINLINE_CVAR void SplitScreenConVarRef : : SetValue ( int nSlot , const char * pValue )
{
m_Info [ nSlot ] . m_pConVar - > SetValue ( pValue ) ;
}
FORCEINLINE_CVAR void SplitScreenConVarRef : : SetValue ( int nSlot , float flValue )
{
m_Info [ nSlot ] . m_pConVar - > SetValue ( flValue ) ;
}
FORCEINLINE_CVAR void SplitScreenConVarRef : : SetValue ( int nSlot , int nValue )
{
m_Info [ nSlot ] . m_pConVar - > SetValue ( nValue ) ;
}
FORCEINLINE_CVAR void SplitScreenConVarRef : : SetValue ( int nSlot , Color value )
{
m_Info [ nSlot ] . m_pConVar - > SetValue ( value ) ;
}
FORCEINLINE_CVAR void SplitScreenConVarRef : : SetValue ( int nSlot , bool bValue )
{
m_Info [ nSlot ] . m_pConVar - > SetValue ( bValue ? 1 : 0 ) ;
}
FORCEINLINE_CVAR const char * SplitScreenConVarRef : : GetDefault ( ) const
{
return m_Info [ 0 ] . m_pConVarState - > m_pszDefaultValue ;
}
//-----------------------------------------------------------------------------
// Called by the framework to register ConCommands with the ICVar
//-----------------------------------------------------------------------------
2018-03-14 18:09:45 -04:00
void ConVar_Register ( int64 nCVarFlag = 0 , IConCommandBaseAccessor * pAccessor = NULL ) ;
2010-07-22 01:46:14 -05:00
void ConVar_Unregister ( ) ;
//-----------------------------------------------------------------------------
// Utility methods
//-----------------------------------------------------------------------------
void ConVar_PrintDescription ( const ConCommandBase * pVar ) ;
//-----------------------------------------------------------------------------
// Purpose: Utility class to quickly allow ConCommands to call member methods
//-----------------------------------------------------------------------------
2012-05-21 02:49:35 -05:00
# ifdef _MSC_VER
2010-07-22 01:46:14 -05:00
# pragma warning (disable : 4355 )
2012-05-21 02:49:35 -05:00
# endif
2010-07-22 01:46:14 -05:00
template < class T >
class CConCommandMemberAccessor : public ConCommand , public ICommandCallback , public ICommandCompletionCallback
{
typedef ConCommand BaseClass ;
typedef void ( T : : * FnMemberCommandCallback_t ) ( const CCommand & command ) ;
typedef int ( T : : * FnMemberCommandCompletionCallback_t ) ( const char * pPartial , CUtlVector < CUtlString > & commands ) ;
public :
CConCommandMemberAccessor ( T * pOwner , const char * pName , FnMemberCommandCallback_t callback , const char * pHelpString = 0 ,
2018-03-13 17:54:16 -04:00
int64 flags = 0 , FnMemberCommandCompletionCallback_t completionFunc = 0 ) :
2010-07-22 01:46:14 -05:00
BaseClass ( pName , this , pHelpString , flags , ( completionFunc ! = 0 ) ? this : NULL )
{
m_pOwner = pOwner ;
m_Func = callback ;
m_CompletionFunc = completionFunc ;
}
~ CConCommandMemberAccessor ( )
{
Shutdown ( ) ;
}
void SetOwner ( T * pOwner )
{
m_pOwner = pOwner ;
}
virtual void CommandCallback ( const CCommand & command )
{
Assert ( m_pOwner & & m_Func ) ;
( m_pOwner - > * m_Func ) ( command ) ;
}
virtual int CommandCompletionCallback ( const char * pPartial , CUtlVector < CUtlString > & commands )
{
Assert ( m_pOwner & & m_CompletionFunc ) ;
return ( m_pOwner - > * m_CompletionFunc ) ( pPartial , commands ) ;
}
private :
T * m_pOwner ;
FnMemberCommandCallback_t m_Func ;
FnMemberCommandCompletionCallback_t m_CompletionFunc ;
} ;
2023-03-28 14:21:16 +03:00
# endif
2012-05-21 02:49:35 -05:00
# ifdef _MSC_VER
2010-07-22 01:46:14 -05:00
# pragma warning ( default : 4355 )
2012-05-21 02:49:35 -05:00
# endif
2010-07-22 01:46:14 -05:00
//-----------------------------------------------------------------------------
// Purpose: Utility macros to quicky generate a simple console command
//-----------------------------------------------------------------------------
# define CON_COMMAND( name, description ) \
2015-07-09 13:07:26 -04:00
static void name ( const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , name , description ) ; \
2015-07-09 13:07:26 -04:00
static void name ( const CCommand & args )
2010-07-22 01:46:14 -05:00
# ifdef CLIENT_DLL
# define CON_COMMAND_SHARED( name, description ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command_client ( # name " _client " , name , description ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# else
# define CON_COMMAND_SHARED( name, description ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , name , description ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# endif
# define CON_COMMAND_F( name, description, flags ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , name , description , flags ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# ifdef CLIENT_DLL
# define CON_COMMAND_F_SHARED( name, description, flags ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command_client ( # name " _client " , name , description , flags ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# else
# define CON_COMMAND_F_SHARED( name, description, flags ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , name , description , flags ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# endif
# define CON_COMMAND_F_COMPLETION( name, description, flags, completion ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , name , description , flags , completion ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# ifdef CLIENT_DLL
# define CON_COMMAND_F_COMPLETION_SHARED( name, description, flags, completion ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command_client ( # name " _client " , name , description , flags , completion ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# else
# define CON_COMMAND_F_COMPLETION_SHARED( name, description, flags, completion ) \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , name , description , flags , completion ) ; \
2013-07-12 02:25:04 -04:00
static void name ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# endif
# define CON_COMMAND_EXTERN( name, _funcname, description ) \
2013-07-12 02:25:04 -04:00
void _funcname ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , _funcname , description ) ; \
2013-07-12 02:25:04 -04:00
void _funcname ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# define CON_COMMAND_EXTERN_F( name, _funcname, description, flags ) \
2013-07-12 02:25:04 -04:00
void _funcname ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
static ConCommand name # # _command ( # name , _funcname , description , flags ) ; \
2013-07-12 02:25:04 -04:00
void _funcname ( const CCommandContext & context , const CCommand & args )
2010-07-22 01:46:14 -05:00
# define CON_COMMAND_MEMBER_F( _thisclass, name, _funcname, description, flags ) \
2013-07-12 02:25:04 -04:00
void _funcname ( const CCommandContext & context , const CCommand & args ) ; \
2010-07-22 01:46:14 -05:00
friend class CCommandMemberInitializer_ # # _funcname ; \
class CCommandMemberInitializer_ # # _funcname \
{ \
public : \
CCommandMemberInitializer_ # # _funcname ( ) : m_ConCommandAccessor ( NULL , name , & _thisclass : : _funcname , description , flags ) \
{ \
m_ConCommandAccessor . SetOwner ( GET_OUTER ( _thisclass , m_ # # _funcname # # _register ) ) ; \
} \
private : \
CConCommandMemberAccessor < _thisclass > m_ConCommandAccessor ; \
} ; \
\
CCommandMemberInitializer_ # # _funcname m_ # # _funcname # # _register ; \
# endif // CONVAR_H