2023-03-28 00:50:08 +02:00
|
|
|
|
//===== Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ======//
|
|
|
|
|
//
|
|
|
|
|
// Purpose:
|
|
|
|
|
//
|
|
|
|
|
// $Workfile: $
|
|
|
|
|
// $Date: $
|
|
|
|
|
//
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// $NoKeywords: $
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
|
2023-05-10 00:05:38 +02:00
|
|
|
|
#ifndef TIER1_CMD_H
|
|
|
|
|
#define TIER1_CMD_H
|
2023-03-28 00:50:08 +02:00
|
|
|
|
|
2022-07-29 17:30:05 +02:00
|
|
|
|
#include "tier1/characterset.h"
|
2022-02-21 17:56:31 +01:00
|
|
|
|
|
2022-02-19 02:31:16 +01:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Command buffer context
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-03-28 12:03:07 +02:00
|
|
|
|
enum class ECommandTarget_t : int
|
2022-02-19 02:31:16 +01:00
|
|
|
|
{
|
|
|
|
|
CBUF_FIRST_PLAYER = 0,
|
|
|
|
|
CBUF_LAST_PLAYER = MAX_SPLITSCREEN_CLIENTS - 1,
|
|
|
|
|
CBUF_SERVER = CBUF_LAST_PLAYER + 1,
|
|
|
|
|
|
|
|
|
|
CBUF_COUNT,
|
2022-03-28 12:03:07 +02:00
|
|
|
|
};
|
2022-02-19 02:31:16 +01:00
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Sources of console commands
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
enum class cmd_source_t : int
|
|
|
|
|
{
|
|
|
|
|
kCommandSrcCode,
|
|
|
|
|
kCommandSrcClientCmd,
|
|
|
|
|
kCommandSrcUserInput,
|
|
|
|
|
kCommandSrcNetClient,
|
|
|
|
|
kCommandSrcNetServer,
|
|
|
|
|
kCommandSrcDemoFile,
|
|
|
|
|
kCommandSrcInvalid = -1
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-14 23:16:24 +01:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: Command tokenizer
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2021-12-25 22:36:38 +01:00
|
|
|
|
class CCommand
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
COMMAND_MAX_ARGC = 64,
|
|
|
|
|
COMMAND_MAX_LENGTH = 512,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2022-07-30 00:43:40 +02:00
|
|
|
|
CCommand();
|
2022-07-29 17:30:05 +02:00
|
|
|
|
CCommand(int nArgC, const char** ppArgV, cmd_source_t source);
|
2023-08-04 14:32:06 +02:00
|
|
|
|
bool Tokenize(const char* pCommand, cmd_source_t source = cmd_source_t::kCommandSrcCode, characterset_t* pBreakSet = nullptr);
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
2023-11-25 11:27:47 +01:00
|
|
|
|
int ArgC(void) const;
|
2022-02-14 23:16:24 +01:00
|
|
|
|
const char** ArgV(void) const;
|
|
|
|
|
const char* ArgS(void) const;
|
|
|
|
|
const char* GetCommandString(void) const;
|
|
|
|
|
const char* Arg(int nIndex) const;
|
|
|
|
|
const char* operator[](int nIndex) const;
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
2022-03-28 18:47:11 +02:00
|
|
|
|
bool HasOnlyDigits(int nIndex) const;
|
2023-11-25 11:27:47 +01:00
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
|
|
static int MaxCommandLength(void);
|
|
|
|
|
static characterset_t* DefaultBreakSet();
|
2022-03-28 18:47:11 +02:00
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
|
private:
|
2022-07-29 17:30:05 +02:00
|
|
|
|
cmd_source_t m_nQueuedVal;
|
2022-03-02 02:03:46 +01:00
|
|
|
|
int m_nArgc;
|
2024-02-10 01:35:24 +01:00
|
|
|
|
ssize_t m_nArgv0Size;
|
2021-12-25 22:36:38 +01:00
|
|
|
|
char m_pArgSBuffer[COMMAND_MAX_LENGTH];
|
|
|
|
|
char m_pArgvBuffer[COMMAND_MAX_LENGTH];
|
|
|
|
|
const char* m_ppArgv[COMMAND_MAX_ARGC];
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-25 11:27:47 +01:00
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns max command length
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline int CCommand::MaxCommandLength(void)
|
|
|
|
|
{
|
|
|
|
|
return COMMAND_MAX_LENGTH - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns argument count
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline int CCommand::ArgC(void) const
|
|
|
|
|
{
|
|
|
|
|
return m_nArgc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns argument vector
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline 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
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline 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
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline const char* CCommand::GetCommandString(void) const
|
|
|
|
|
{
|
|
|
|
|
return m_nArgc ? m_pArgSBuffer : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: returns argument from index as string
|
|
|
|
|
// Input : nIndex -
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
|
if (nIndex < 0 || nIndex >= m_nArgc)
|
|
|
|
|
{
|
2024-01-14 22:57:52 +01:00
|
|
|
|
Assert(0);
|
2023-11-25 11:27:47 +01:00
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return m_ppArgv[nIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Purpose: gets at arguments
|
|
|
|
|
// Input : nInput -
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
inline const char* CCommand::operator[](int nIndex) const
|
|
|
|
|
{
|
|
|
|
|
return Arg(nIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 00:05:38 +02:00
|
|
|
|
#endif // TIER1_CMD_H
|