aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/concommand.h
blob: c745879417e43c80145ff4def02315a9f996bc64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include "convar.h"

// taken from ttf2sdk
class ConCommand
{
	unsigned char unknown[0x68];

  public:
	virtual void EngineDestructor(void) {}
	virtual bool IsCommand(void) const { return false; }
	virtual bool IsFlagSet(int flag) { return false; }
	virtual void AddFlags(int flags) {}
	virtual void RemoveFlags(int flags) {}
	virtual int GetFlags() const { return 0; }
	virtual const char* GetName(void) const { return nullptr; }
	virtual const char* GetHelpText(void) const { return nullptr; }
	virtual bool IsRegistered(void) const { return false; }
	// NOTE: there are more virtual methods here
	// NOTE: Not using the engine's destructor here because it doesn't do anything useful for us
};

// From Source SDK
class CCommand
{
  public:
	CCommand() = delete;

	int64_t 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

	static int MaxCommandLength();

  private:
	enum
	{
		COMMAND_MAX_ARGC = 64,
		COMMAND_MAX_LENGTH = 512,
	};

	int64_t m_nArgc;
	int64_t m_nArgv0Size;
	char m_pArgSBuffer[COMMAND_MAX_LENGTH];
	char m_pArgvBuffer[COMMAND_MAX_LENGTH];
	const char* m_ppArgv[COMMAND_MAX_ARGC];
};

inline int CCommand::MaxCommandLength() { return COMMAND_MAX_LENGTH - 1; }

inline int64_t CCommand::ArgC() const { return m_nArgc; }

inline const char** CCommand::ArgV() const { return m_nArgc ? (const char**)m_ppArgv : NULL; }

inline const char* CCommand::ArgS() const { return m_nArgv0Size ? &m_pArgSBuffer[m_nArgv0Size] : ""; }

inline const char* CCommand::GetCommandString() const { return m_nArgc ? m_pArgSBuffer : ""; }

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)
		return "";
	return m_ppArgv[nIndex];
}

inline const char* CCommand::operator[](int nIndex) const { return Arg(nIndex); }

void RegisterConCommand(const char* name, void (*callback)(const CCommand&), const char* helpString, int flags);
ConCommand* FindConCommand(const char* name);
void InitialiseConCommands(HMODULE baseAddress);