2022-05-13 17:54:13 +02:00
//=============================================================================//
//
2022-05-13 20:59:30 +02:00
// Purpose: Console Commands
2022-05-13 17:54:13 +02:00
//
//=============================================================================//
2021-12-25 22:36:38 +01:00
# include "core/stdafx.h"
2022-04-15 04:02:33 +02:00
# include "tier0/tslist.h"
2022-04-09 16:16:40 +02:00
# include "tier1/cmd.h"
# include "tier1/cvar.h"
2022-05-16 21:15:25 +02:00
# include "vstdlib/callback.h"
2021-12-25 22:36:38 +01:00
# include "engine/sys_utils.h"
2022-02-14 23:16:24 +01:00
//-----------------------------------------------------------------------------
// Purpose: returns max command lenght
//-----------------------------------------------------------------------------
int CCommand : : MaxCommandLength ( void )
{
return COMMAND_MAX_LENGTH - 1 ;
}
//-----------------------------------------------------------------------------
// Purpose: returns argument count
//-----------------------------------------------------------------------------
std : : int64_t CCommand : : ArgC ( void ) const
{
return m_nArgc ;
}
//-----------------------------------------------------------------------------
// Purpose: returns argument vector
//-----------------------------------------------------------------------------
const char * * CCommand : : ArgV ( void ) const
{
return m_nArgc ? ( const char * * ) m_ppArgv : NULL ;
}
//-----------------------------------------------------------------------------
// Purpose: returns all args that occur after the 0th arg, in string form
//-----------------------------------------------------------------------------
const char * CCommand : : ArgS ( void ) const
{
return m_nArgv0Size ? & m_pArgSBuffer [ m_nArgv0Size ] : " " ;
}
//-----------------------------------------------------------------------------
// Purpose: returns the entire command in string form, including the 0th arg
//-----------------------------------------------------------------------------
const char * CCommand : : GetCommandString ( void ) const
{
return m_nArgc ? m_pArgSBuffer : " " ;
}
//-----------------------------------------------------------------------------
// Purpose: returns argument from index as string
// Input : nIndex -
//-----------------------------------------------------------------------------
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
if ( nIndex < 0 | | nIndex > = m_nArgc )
{
return " " ;
}
return m_ppArgv [ nIndex ] ;
}
//-----------------------------------------------------------------------------
// Purpose: gets at arguments
// Input : nInput -
//-----------------------------------------------------------------------------
const char * CCommand : : operator [ ] ( int nIndex ) const
{
return Arg ( nIndex ) ;
}
2022-03-28 18:47:11 +02:00
//-----------------------------------------------------------------------------
// Purpose: return boolean depending on if the string only has digits in it
// Input : svString -
//-----------------------------------------------------------------------------
bool CCommand : : HasOnlyDigits ( int nIndex ) const
{
std : : string svString = Arg ( nIndex ) ;
for ( const char & character : svString )
{
if ( std : : isdigit ( character ) = = 0 )
{
return false ;
}
}
return true ;
}
2021-12-25 22:36:38 +01:00
//-----------------------------------------------------------------------------
2022-02-13 15:16:09 +01:00
// Purpose: construct/allocate
2021-12-25 22:36:38 +01:00
//-----------------------------------------------------------------------------
2022-01-09 16:14:17 +01:00
ConCommand : : ConCommand ( const char * pszName , const char * pszHelpString , int nFlags , void * pCallback , void * pCommandCompletionCallback )
2021-12-25 22:36:38 +01:00
{
2022-02-21 17:56:31 +01:00
ConCommand * pCommand = reinterpret_cast < ConCommand * > ( MemAlloc_Wrapper ( sizeof ( ConCommand ) ) ) ; // Allocate new memory with StdMemAlloc else we crash.
memset ( pCommand , ' \0 ' , sizeof ( ConCommand ) ) ; // Set all to null.
2021-12-25 22:36:38 +01:00
2022-02-23 03:41:10 +01:00
pCommand - > m_pConCommandBaseVTable = g_pConCommandVtable . RCast < void * > ( ) ;
pCommand - > m_pszName = pszName ;
pCommand - > m_pszHelpString = pszHelpString ;
pCommand - > m_nFlags = nFlags ;
pCommand - > m_nNullCallBack = NullSub ;
pCommand - > m_pCommandCallback = pCallback ;
pCommand - > m_nCallbackFlags = 2 ;
2022-02-21 17:56:31 +01:00
if ( pCommandCompletionCallback )
2021-12-25 22:36:38 +01:00
{
2022-02-21 17:56:31 +01:00
pCommand - > m_pCompletionCallback = pCommandCompletionCallback ;
2021-12-25 22:36:38 +01:00
}
else
{
2022-02-21 17:56:31 +01:00
pCommand - > m_pCompletionCallback = CallbackStub ;
2021-12-25 22:36:38 +01:00
}
2022-02-21 17:56:31 +01:00
ConCommand_RegisterConCommand ( pCommand ) ;
2022-01-09 16:14:17 +01:00
* this = * pCommand ;
2021-12-25 22:36:38 +01:00
}
//-----------------------------------------------------------------------------
2022-01-09 16:14:17 +01:00
// Purpose: ConCommand registration
2021-12-25 22:36:38 +01:00
//-----------------------------------------------------------------------------
2022-01-09 16:14:17 +01:00
void ConCommand : : Init ( void )
2021-12-25 22:36:38 +01:00
{
//-------------------------------------------------------------------------
// SERVER DLL |
2022-05-16 21:15:25 +02:00
new ConCommand ( " script " , " Run input code as SERVER script on the VM. " , FCVAR_GAMEDLL | FCVAR_CHEAT , SQVM_ServerScript_f , nullptr ) ;
new ConCommand ( " sv_kick " , " Kick a client from the server by name. | Usage: kick \" <name> \" . " , FCVAR_RELEASE , Host_Kick_f , nullptr ) ;
new ConCommand ( " sv_kickid " , " Kick a client from the server by UserID or OriginID | Usage: kickid \" <UserID> \" / \" <OriginID> \" . " , FCVAR_RELEASE , Host_KickID_f , nullptr ) ;
new ConCommand ( " sv_ban " , " Bans a client from the server by name. | Usage: ban <name>. " , FCVAR_RELEASE , Host_Ban_f , nullptr ) ;
new ConCommand ( " sv_banid " , " Bans a client from the server by UserID, OriginID or IPAddress | Usage: banid \" <UserID> \" / \" <OriginID>/<IPAddress> \" . " , FCVAR_RELEASE , Host_BanID_f , nullptr ) ;
new ConCommand ( " sv_unban " , " Unbans a client from the server by OriginID or IPAddress | Usage: unban \" <OriginID> \" / \" <IPAddress> \" . " , FCVAR_RELEASE , Host_Unban_f , nullptr ) ;
new ConCommand ( " sv_reloadbanlist " , " Reloads the ban list from the disk. " , FCVAR_RELEASE , Host_ReloadBanList_f , nullptr ) ;
2021-12-25 22:36:38 +01:00
# ifndef DEDICATED
//-------------------------------------------------------------------------
// CLIENT DLL |
2022-05-16 21:15:25 +02:00
new ConCommand ( " script_client " , " Run input code as CLIENT script on the VM. " , FCVAR_CLIENTDLL | FCVAR_CHEAT , SQVM_ClientScript_f , nullptr ) ;
new ConCommand ( " cl_showconsole " , " Opens the game console. " , FCVAR_CLIENTDLL | FCVAR_RELEASE , GameConsole_Invoke_f , nullptr ) ;
new ConCommand ( " cl_showbrowser " , " Opens the server browser. " , FCVAR_CLIENTDLL | FCVAR_RELEASE , ServerBrowser_Invoke_f , nullptr ) ;
new ConCommand ( " rcon " , " Forward RCON query to remote server. | Usage: rcon \" <query> \" . " , FCVAR_CLIENTDLL | FCVAR_RELEASE , RCON_CmdQuery_f , nullptr ) ;
new ConCommand ( " rcon_disconnect " , " Disconnect from RCON server. " , FCVAR_CLIENTDLL | FCVAR_RELEASE , RCON_Disconnect_f , nullptr ) ;
2022-03-30 22:54:33 +02:00
//-------------------------------------------------------------------------
// UI DLL |
2022-05-16 21:15:25 +02:00
new ConCommand ( " script_ui " , " Run input code as UI script on the VM. " , FCVAR_CLIENTDLL | FCVAR_CHEAT , SQVM_UIScript_f , nullptr ) ;
2021-12-25 22:36:38 +01:00
# endif // !DEDICATED
//-------------------------------------------------------------------------
// FILESYSTEM API |
2022-05-16 21:15:25 +02:00
new ConCommand ( " fs_unpack_vpk " , " Unpacks all files from user specified VPK file. " , FCVAR_DEVELOPMENTONLY , VPK_Unpack_f , nullptr ) ;
new ConCommand ( " fs_mount_vpk " , " Mounts user specified VPK file for FileSystem usage. " , FCVAR_DEVELOPMENTONLY , VPK_Mount_f , nullptr ) ;
2021-12-25 22:36:38 +01:00
//-------------------------------------------------------------------------
// RTECH API |
2022-05-16 21:15:25 +02:00
new ConCommand ( " rtech_strtoguid " , " Calculates the GUID from input data. " , FCVAR_DEVELOPMENTONLY , RTech_StringToGUID_f , nullptr ) ;
new ConCommand ( " pak_requestload " , " Requests asynchronous load for specified RPAK file. " , FCVAR_DEVELOPMENTONLY , Pak_RequestLoad_f , nullptr ) ;
new ConCommand ( " pak_requestunload " , " Requests unload for specified RPAK by ID. " , FCVAR_DEVELOPMENTONLY , Pak_RequestUnload_f , nullptr ) ;
new ConCommand ( " pak_decompress " , " Decompresses the specified RPAK file. " , FCVAR_DEVELOPMENTONLY , RTech_Decompress_f , nullptr ) ;
new ConCommand ( " pak_listpaks " , " Display a list of the loaded Pak files. " , FCVAR_DEVELOPMENTONLY , Pak_ListPaks_f , nullptr ) ;
2021-12-25 22:36:38 +01:00
//-------------------------------------------------------------------------
// NETCHANNEL |
2022-05-16 21:15:25 +02:00
new ConCommand ( " net_setkey " , " Sets user specified base64 net key. " , FCVAR_RELEASE , NET_SetKey_f , nullptr ) ;
new ConCommand ( " net_generatekey " , " Generates and sets a random base64 net key. " , FCVAR_RELEASE , NET_GenerateKey_f , nullptr ) ;
2022-04-14 19:18:59 +02:00
}
//-----------------------------------------------------------------------------
// Purpose: shipped ConCommand initialization
//-----------------------------------------------------------------------------
void ConCommand : : InitShipped ( void )
{
2022-03-18 13:47:22 +01:00
# ifndef DEDICATED
//-------------------------------------------------------------------------
// MATERIAL SYSTEM
2022-05-16 21:15:25 +02:00
g_pCVar - > FindCommand ( " mat_crosshair " ) - > m_pCommandCallback = Mat_CrossHair_f ; // Patch callback function to working callback.
2022-03-18 13:47:22 +01:00
# endif // !DEDICATED
2022-01-09 16:14:17 +01:00
}
2022-04-14 19:18:59 +02:00
//-----------------------------------------------------------------------------
2022-04-16 00:30:46 +02:00
// Purpose: unregister extraneous ConCommand's.
2022-04-14 19:18:59 +02:00
//-----------------------------------------------------------------------------
void ConCommand : : PurgeShipped ( void ) const
{
2022-04-16 00:30:46 +02:00
# ifdef DEDICATED
2022-04-14 19:18:59 +02:00
const char * pszCommandToRemove [ ] =
{
" bind " ,
" bind_held " ,
" bind_list " ,
" bind_list_abilities " ,
" bind_US_standard " ,
" bind_held_US_standard " ,
2022-04-16 00:30:46 +02:00
" unbind " ,
" unbind_US_standard " ,
" unbindall " ,
" unbind_all_gamepad " ,
" unbindall_ignoreGamepad " ,
" unbind_batch " ,
" unbind_held " ,
" unbind_held_US_standard " ,
2022-05-10 01:38:13 +02:00
" uiscript_reset " ,
2022-04-16 00:30:46 +02:00
" getpos_bind " ,
2022-04-14 19:18:59 +02:00
" connect " ,
2022-04-16 00:30:46 +02:00
" silent_connect " ,
" ping " ,
2022-04-14 19:18:59 +02:00
" gameui_activate " ,
" gameui_hide " ,
" weaponSelectOrdnance " ,
" weaponSelectPrimary0 " ,
" weaponSelectPrimary1 " ,
" weaponSelectPrimary2 " ,
" +scriptCommand1 " ,
" -scriptCommand1 " ,
" +scriptCommand2 " ,
" -scriptCommand2 " ,
" +scriptCommand3 " ,
" -scriptCommand3 " ,
" +scriptCommand4 " ,
" -scriptCommand4 " ,
" +scriptCommand5 " ,
" -scriptCommand5 " ,
" +scriptCommand6 " ,
" -scriptCommand6 " ,
" +scriptCommand7 " ,
" -scriptCommand7 " ,
" +scriptCommand8 " ,
" -scriptCommand8 " ,
" +scriptCommand9 " ,
" -scriptCommand9 " ,
} ;
for ( int i = 0 ; i < ( & pszCommandToRemove ) [ 1 ] - pszCommandToRemove ; i + + )
{
ConCommandBase * pCommandBase = g_pCVar - > FindCommandBase ( pszCommandToRemove [ i ] ) ;
if ( pCommandBase )
{
g_pCVar - > UnregisterConCommand ( pCommandBase ) ;
}
}
2022-04-16 00:30:46 +02:00
# endif // DEDICATED
2022-04-14 19:18:59 +02:00
}
2022-01-09 16:14:17 +01:00
//-----------------------------------------------------------------------------
2022-02-23 00:18:46 +01:00
// Purpose: Returns true if this is a command
// Output : bool
2022-01-09 16:14:17 +01:00
//-----------------------------------------------------------------------------
2022-02-23 00:18:46 +01:00
bool ConCommand : : IsCommand ( void ) const
2022-01-09 16:14:17 +01:00
{
2022-02-23 00:18:46 +01:00
return true ;
2022-01-09 16:14:17 +01:00
}
//-----------------------------------------------------------------------------
2022-02-23 00:18:46 +01:00
// Purpose: Returns true if this is a command
// Output : bool
2022-01-09 16:14:17 +01:00
//-----------------------------------------------------------------------------
2022-02-23 00:18:46 +01:00
bool ConCommandBase : : IsCommand ( void ) const
2022-01-09 16:14:17 +01:00
{
2022-02-23 15:56:03 +01:00
return m_pConCommandBaseVTable ! = g_pConVarVtable . RCast < void * > ( ) ;
2022-01-09 16:14:17 +01:00
}
//-----------------------------------------------------------------------------
2022-02-23 00:18:46 +01:00
// Purpose: Has this cvar been registered
// Output : Returns true on success, false on failure.
2022-01-09 16:14:17 +01:00
//-----------------------------------------------------------------------------
2022-02-23 00:18:46 +01:00
bool ConCommandBase : : IsRegistered ( void ) const
2022-01-09 16:14:17 +01:00
{
2022-02-23 00:18:46 +01:00
return m_bRegistered ;
2022-01-09 16:14:17 +01:00
}
//-----------------------------------------------------------------------------
2022-01-12 02:53:07 +01:00
// Purpose: Test each ConCommand query before execution.
2022-01-09 16:14:17 +01:00
// Input : *pCommandBase - nFlags
2022-01-12 02:53:07 +01:00
// Output : False if execution is permitted, true if not.
2022-01-09 16:14:17 +01:00
//-----------------------------------------------------------------------------
bool ConCommandBase : : IsFlagSet ( ConCommandBase * pCommandBase , int nFlags )
{
2022-01-09 17:17:05 +01:00
if ( cm_debug_cmdquery - > GetBool ( ) )
2022-01-09 16:14:17 +01:00
{
printf ( " -------------------------------------------------- \n " ) ;
printf ( " Flaged: %08X \n " , pCommandBase - > m_nFlags ) ;
}
// Mask off FCVAR_CHEATS and FCVAR_DEVELOPMENTONLY.
2022-04-26 20:24:51 +02:00
if ( cm_unset_cheat_cmdquery - > GetBool ( ) )
2022-01-09 16:14:17 +01:00
{
pCommandBase - > RemoveFlags ( FCVAR_DEVELOPMENTONLY | FCVAR_CHEAT ) ;
}
2022-04-26 20:24:51 +02:00
else if ( cm_unset_dev_cmdquery - > GetBool ( ) ) // Mask off FCVAR_DEVELOPMENTONLY.
2022-01-09 16:14:17 +01:00
{
pCommandBase - > RemoveFlags ( FCVAR_DEVELOPMENTONLY ) ;
}
2022-01-09 17:17:05 +01:00
if ( cm_debug_cmdquery - > GetBool ( ) )
2022-01-09 16:14:17 +01:00
{
printf ( " Masked: %08X \n " , pCommandBase - > m_nFlags ) ;
printf ( " Verify: %08X \n " , nFlags ) ;
printf ( " -------------------------------------------------- \n " ) ;
}
2022-04-26 20:24:51 +02:00
if ( nFlags & FCVAR_RELEASE & & ! cm_unset_all_cmdquery - > GetBool ( ) )
2022-01-09 16:14:17 +01:00
{
// Default retail behaviour.
return ConCommandBase_IsFlagSet ( pCommandBase , nFlags ) ;
}
2022-04-26 20:24:51 +02:00
if ( cm_unset_all_cmdquery - > GetBool ( ) )
2022-01-09 16:14:17 +01:00
{
// Returning false on all queries may cause problems.
return false ;
}
2022-04-26 20:24:51 +02:00
// Default behavior.
2022-01-09 16:14:17 +01:00
return pCommandBase - > HasFlags ( nFlags ) ! = 0 ;
2021-12-25 22:36:38 +01:00
}
2022-02-23 00:18:46 +01:00
//-----------------------------------------------------------------------------
// Purpose: Checks if ConCommand has requested flags.
// Input : nFlags -
// Output : True if ConCommand has nFlags.
//-----------------------------------------------------------------------------
bool ConCommandBase : : HasFlags ( int nFlags )
{
return m_nFlags & nFlags ;
}
//-----------------------------------------------------------------------------
// Purpose: Add's flags to ConCommand.
// Input : nFlags -
//-----------------------------------------------------------------------------
void ConCommandBase : : AddFlags ( int nFlags )
{
m_nFlags | = nFlags ;
}
//-----------------------------------------------------------------------------
// Purpose: Removes flags from ConCommand.
// Input : nFlags -
//-----------------------------------------------------------------------------
void ConCommandBase : : RemoveFlags ( int nFlags )
{
m_nFlags & = ~ nFlags ;
}
//-----------------------------------------------------------------------------
// Purpose: Returns current flags.
// Output : int
//-----------------------------------------------------------------------------
int ConCommandBase : : GetFlags ( void ) const
{
return m_nFlags ;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : const ConCommandBase
//-----------------------------------------------------------------------------
ConCommandBase * ConCommandBase : : GetNext ( void ) const
{
return m_pNext ;
}
2022-04-14 19:18:59 +02:00
//-----------------------------------------------------------------------------
// Purpose: Returns the ConCommandBase name.
// Output : const char*
//-----------------------------------------------------------------------------
const char * ConCommandBase : : GetName ( void ) const
{
return m_pszName ;
}
2022-02-23 00:18:46 +01:00
//-----------------------------------------------------------------------------
// Purpose: Returns the ConCommandBase help text.
// Output : const char*
//-----------------------------------------------------------------------------
const char * ConCommandBase : : GetHelpText ( void ) const
{
return m_pszHelpString ;
}
2022-02-23 15:56:03 +01:00
//-----------------------------------------------------------------------------
// Purpose: Returns the ConCommandBase usage text.
// Output : const char*
//-----------------------------------------------------------------------------
const char * ConCommandBase : : GetUsageText ( void ) const
{
return m_pszUsageString ;
}
2022-02-23 00:18:46 +01:00
//-----------------------------------------------------------------------------
// Purpose: Copies string using local new/delete operators
// Input : *szFrom -
// Output : char
//-----------------------------------------------------------------------------
char * ConCommandBase : : CopyString ( const char * szFrom ) const
{
size_t nLen ;
char * szTo ;
nLen = strlen ( szFrom ) ;
if ( nLen < = 0 )
{
szTo = new char [ 1 ] ;
szTo [ 0 ] = 0 ;
}
else
{
szTo = new char [ nLen + 1 ] ;
2022-02-23 00:21:54 +01:00
memmove ( szTo , szFrom , nLen + 1 ) ;
2022-02-23 00:18:46 +01:00
}
return szTo ;
}
2022-03-25 13:17:57 +01:00
//-----------------------------------------------------------------------------
// Purpose: Returns current player calling this function
// Output : ECommandTarget_t -
//-----------------------------------------------------------------------------
ECommandTarget_t Cbuf_GetCurrentPlayer ( void )
{
// Always returns 'CBUF_FIRST_PLAYER' in Respawn's code.
return ECommandTarget_t : : CBUF_FIRST_PLAYER ;
}
2022-02-14 23:16:24 +01:00
///////////////////////////////////////////////////////////////////////////////
2021-12-25 22:36:38 +01:00
void ConCommand_Attach ( )
{
2022-01-09 16:14:17 +01:00
DetourAttach ( ( LPVOID * ) & ConCommandBase_IsFlagSet , & ConCommandBase : : IsFlagSet ) ;
2021-12-25 22:36:38 +01:00
}
void ConCommand_Detach ( )
{
2022-01-09 16:14:17 +01:00
DetourDetach ( ( LPVOID * ) & ConCommandBase_IsFlagSet , & ConCommandBase : : IsFlagSet ) ;
2021-12-25 22:36:38 +01:00
}
2022-01-09 16:14:17 +01:00
ConCommand * g_pConCommand = new ConCommand ( ) ;