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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#include "pch.h"
#include "hooks.h"
#include "convar.h"
#include "sourceconsole.h"
#include "sourceinterface.h"
#include "concommand.h"
#include "hookutils.h"
SourceInterface<CGameConsole>* g_SourceGameConsole;
void ConCommand_toggleconsole(const CCommand& arg)
{
if ((*g_SourceGameConsole)->IsConsoleVisible())
(*g_SourceGameConsole)->Hide();
else
(*g_SourceGameConsole)->Activate();
}
void ConCommand_showconsole(const CCommand& arg)
{
(*g_SourceGameConsole)->Activate();
}
void ConCommand_hideconsole(const CCommand& arg)
{
(*g_SourceGameConsole)->Hide();
}
void PrintCommandHelpDialogue(const ConCommandBase* command, const char* name)
{
if (!command)
{
spdlog::info("unknown command {}", name);
return;
}
// build string for flags if not FCVAR_NONE
std::string flagString;
if (command->GetFlags() != FCVAR_NONE)
{
flagString = "( ";
for (auto& flagPair : g_PrintCommandFlags)
{
if (command->GetFlags() & flagPair.first)
{
flagString += flagPair.second;
flagString += " ";
}
}
flagString += ") ";
}
// temp because command->IsCommand does not currently work
ConVar* cvar = g_pCVar->FindVar(command->m_pszName);
if (cvar)
spdlog::info("\"{}\" = \"{}\" {}- {}", cvar->GetBaseName(), cvar->GetString(), flagString, cvar->GetHelpText());
else
spdlog::info("\"{}\" {} - {}", command->m_pszName, flagString, command->GetHelpText());
}
void ConCommand_help(const CCommand& arg)
{
if (arg.ArgC() < 2)
{
spdlog::info("Usage: help <cvarname>");
return;
}
PrintCommandHelpDialogue(g_pCVar->FindCommandBase(arg.Arg(1)), arg.Arg(1));
}
void ConCommand_find(const CCommand& arg)
{
if (arg.ArgC() < 2)
{
spdlog::info("Usage: find <string> [<string>...]");
return;
}
char pTempName[256];
char pTempSearchTerm[256];
for (auto& map : g_pCVar->DumpToMap())
{
bool bPrintCommand = true;
for (int i = 0; i < arg.ArgC() - 1; i++)
{
// make lowercase to avoid case sensitivity
strncpy(pTempName, map.second->m_pszName, sizeof(pTempName));
strncpy(pTempSearchTerm, arg.Arg(i + 1), sizeof(pTempSearchTerm));
for (int i = 0; pTempName[i]; i++)
pTempName[i] = tolower(pTempName[i]);
for (int i = 0; pTempSearchTerm[i]; i++)
pTempSearchTerm[i] = tolower(pTempSearchTerm[i]);
if (!strstr(pTempName, pTempSearchTerm))
{
bPrintCommand = false;
break;
}
}
if (bPrintCommand)
PrintCommandHelpDialogue(map.second, map.second->m_pszName);
}
}
typedef void (*OnCommandSubmittedType)(CConsoleDialog* consoleDialog, const char* pCommand);
OnCommandSubmittedType onCommandSubmittedOriginal;
void OnCommandSubmittedHook(CConsoleDialog* consoleDialog, const char* pCommand)
{
consoleDialog->m_pConsolePanel->Print("] ");
consoleDialog->m_pConsolePanel->Print(pCommand);
consoleDialog->m_pConsolePanel->Print("\n");
// try to display help text for this cvar
{
int pCommandLen = strlen(pCommand);
char* pCvarStr = new char[pCommandLen];
strcpy(pCvarStr, pCommand);
// trim whitespace from right
for (int i = pCommandLen - 1; i; i--)
{
if (isspace(pCvarStr[i]))
pCvarStr[i] = '\0';
else
break;
}
// check if we're inputting a cvar, but not setting it at all
ConVar* cvar = g_pCVar->FindVar(pCvarStr);
if (cvar)
PrintCommandHelpDialogue(&cvar->m_ConCommandBase, pCvarStr);
delete[] pCvarStr;
}
onCommandSubmittedOriginal(consoleDialog, pCommand);
}
// called from sourceinterface.cpp in client createinterface hooks, on GameClientExports001
void InitialiseConsoleOnInterfaceCreation()
{
(*g_SourceGameConsole)->Initialize();
auto consoleLogger = std::make_shared<SourceConsoleSink>();
consoleLogger->set_pattern("[%l] %v");
spdlog::default_logger()->sinks().push_back(consoleLogger);
// hook OnCommandSubmitted so we print inputted commands
HookEnabler hook;
ENABLER_CREATEHOOK(
hook,
(void*)((*g_SourceGameConsole)->m_pConsole->m_vtable->OnCommandSubmitted),
&OnCommandSubmittedHook,
reinterpret_cast<LPVOID*>(&onCommandSubmittedOriginal));
}
// logging stuff
SourceConsoleSink::SourceConsoleSink()
{
logColours.emplace(spdlog::level::trace, SourceColor(0, 255, 255, 255));
logColours.emplace(spdlog::level::debug, SourceColor(0, 255, 255, 255));
logColours.emplace(spdlog::level::info, SourceColor(255, 255, 255, 255));
logColours.emplace(spdlog::level::warn, SourceColor(255, 255, 0, 255));
logColours.emplace(spdlog::level::err, SourceColor(255, 0, 0, 255));
logColours.emplace(spdlog::level::critical, SourceColor(255, 0, 0, 255));
logColours.emplace(spdlog::level::off, SourceColor(0, 0, 0, 0));
}
void SourceConsoleSink::sink_it_(const spdlog::details::log_msg& msg)
{
if (!(*g_SourceGameConsole)->m_bInitialized)
return;
spdlog::memory_buf_t formatted;
spdlog::sinks::base_sink<std::mutex>::formatter_->format(msg, formatted);
(*g_SourceGameConsole)
->m_pConsole->m_pConsolePanel->ColorPrint(logColours[msg.level], fmt::to_string(formatted).c_str()); // todo needs colour support
}
void SourceConsoleSink::flush_() {}
ON_DLL_LOAD_CLIENT_RELIESON("client.dll", SourceConsole, ConCommand, [](HMODULE baseAddress)
{
g_SourceGameConsole = new SourceInterface<CGameConsole>("client.dll", "GameConsole004");
RegisterConCommand("toggleconsole", ConCommand_toggleconsole, "Show/hide the console.", FCVAR_DONTRECORD);
RegisterConCommand("showconsole", ConCommand_showconsole, "Show the console.", FCVAR_DONTRECORD);
RegisterConCommand("hideconsole", ConCommand_hideconsole, "Hide the console.", FCVAR_DONTRECORD);
RegisterConCommand("find", ConCommand_find, "Find concommands with the specified string in their name/help text.", FCVAR_NONE);
// help is already a command, so we need to modify the preexisting command to use our func instead
// and clear the flags also
ConCommand* helpCommand = g_pCVar->FindCommand("help");
helpCommand->m_nFlags = 0;
helpCommand->m_pCommandCallback = ConCommand_help;
})
|