aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/util/printcommands.cpp
blob: 34d566669130e8c65aae675a1f09e5eac392ef95 (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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "printcommands.h"
#include "core/convar/cvar.h"
#include "core/convar/convar.h"
#include "core/convar/concommand.h"

void PrintCommandHelpDialogue(const ConCommandBase* command, const char* name)
{
	if (!command)
	{
		spdlog::info("unknown command {}", name);
		return;
	}

	// temp because command->IsCommand does not currently work
	ConVar* cvar = g_pCVar->FindVar(command->m_pszName);

	// 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)
			{
				// special case, slightly hacky: PRINTABLEONLY is for commands, GAMEDLL_FOR_REMOTE_CLIENTS is for concommands, both have the
				// same value
				if (flagPair.first == FCVAR_PRINTABLEONLY)
				{
					if (cvar && !strcmp(flagPair.second, "GAMEDLL_FOR_REMOTE_CLIENTS"))
						continue;

					if (!cvar && !strcmp(flagPair.second, "PRINTABLEONLY"))
						continue;
				}

				flagString += flagPair.second;
				flagString += " ";
			}
		}

		flagString += ") ";
	}

	if (cvar)
		spdlog::info("\"{}\" = \"{}\" {}- {}", cvar->GetBaseName(), cvar->GetString(), flagString, cvar->GetHelpText());
	else
		spdlog::info("\"{}\" {} - {}", command->m_pszName, flagString, command->GetHelpText());
}

void TryPrintCvarHelpForCommand(const char* pCommand)
{
	// try to display help text for an inputted command string from the console
	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;
}

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];

	ConCommandBase* var;
	CCVarIteratorInternal* itint = g_pCVar->FactoryInternalIterator();
	std::map<std::string, ConCommandBase*> sorted;
	for (itint->SetFirst(); itint->IsValid(); itint->Next())
	{
		var = itint->Get();
		if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
		{
			sorted.insert({var->m_pszName, var});
		}
	}
	delete itint;

	for (auto& map : sorted)
	{
		bool bPrintCommand = true;
		for (int i = 0; i < arg.ArgC() - 1; i++)
		{
			// make lowercase to avoid case sensitivity
			strncpy_s(pTempName, sizeof(pTempName), map.second->m_pszName, sizeof(pTempName) - 1);
			strncpy_s(pTempSearchTerm, sizeof(pTempSearchTerm), arg.Arg(i + 1), sizeof(pTempSearchTerm) - 1);

			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);
	}
}

void ConCommand_findflags(const CCommand& arg)
{
	if (arg.ArgC() < 2)
	{
		spdlog::info("Usage: findflags <string>");
		for (auto& flagPair : g_PrintCommandFlags)
			spdlog::info("   - {}", flagPair.second);

		return;
	}

	// convert input flag to uppercase
	char* upperFlag = new char[strlen(arg.Arg(1))];
	strcpy(upperFlag, arg.Arg(1));

	for (int i = 0; upperFlag[i]; i++)
		upperFlag[i] = toupper(upperFlag[i]);

	// resolve flag name => int flags
	int resolvedFlag = FCVAR_NONE;
	for (auto& flagPair : g_PrintCommandFlags)
	{
		if (!strcmp(flagPair.second, upperFlag))
		{
			resolvedFlag |= flagPair.first;
			break;
		}
	}

	ConCommandBase* var;
	CCVarIteratorInternal* itint = g_pCVar->FactoryInternalIterator();
	std::map<std::string, ConCommandBase*> sorted;
	for (itint->SetFirst(); itint->IsValid(); itint->Next())
	{
		var = itint->Get();
		if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
		{
			sorted.insert({var->m_pszName, var});
		}
	}
	delete itint;

	for (auto& map : sorted)
	{
		if (map.second->m_nFlags & resolvedFlag)
			PrintCommandHelpDialogue(map.second, map.second->m_pszName);
	}

	delete[] upperFlag;
}

void ConCommand_list(const CCommand& arg)
{
	ConCommandBase* var;
	CCVarIteratorInternal* itint = g_pCVar->FactoryInternalIterator();
	std::map<std::string, ConCommandBase*> sorted;
	for (itint->SetFirst(); itint->IsValid(); itint->Next())
	{
		var = itint->Get();
		if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
		{
			sorted.insert({var->m_pszName, var});
		}
	}
	delete itint;

	for (auto& map : sorted)
	{
		PrintCommandHelpDialogue(map.second, map.second->m_pszName);
	}
	spdlog::info("{} total convars/concommands", sorted.size());
}

void ConCommand_differences(const CCommand& arg)
{
	CCVarIteratorInternal* itint = g_pCVar->FactoryInternalIterator();
	std::map<std::string, ConCommandBase*> sorted;

	for (itint->SetFirst(); itint->IsValid(); itint->Next())
	{
		ConCommandBase* var = itint->Get();
		if (!var->IsFlagSet(FCVAR_DEVELOPMENTONLY) && !var->IsFlagSet(FCVAR_HIDDEN))
		{
			sorted.insert({var->m_pszName, var});
		}
	}
	delete itint;

	for (auto& map : sorted)
	{
		ConVar* cvar = g_pCVar->FindVar(map.second->m_pszName);

		if (!cvar)
		{
			continue;
		}

		if (strcmp(cvar->GetString(), "FCVAR_NEVER_AS_STRING") == NULL)
		{
			continue;
		}

		if (strcmp(cvar->GetString(), cvar->m_pszDefaultValue) == NULL)
		{
			continue;
		}

		std::string formatted =
			fmt::format("\"{}\" = \"{}\" ( def. \"{}\" )", cvar->GetBaseName(), cvar->GetString(), cvar->m_pszDefaultValue);

		if (cvar->m_bHasMin)
		{
			formatted.append(fmt::format(" min. {}", cvar->m_fMinVal));
		}

		if (cvar->m_bHasMax)
		{
			formatted.append(fmt::format(" max. {}", cvar->m_fMaxVal));
		}

		formatted.append(fmt::format(" - {}", cvar->GetHelpText()));
		spdlog::info(formatted);
	}
}

void InitialiseCommandPrint()
{
	RegisterConCommand(
		"convar_find", ConCommand_find, "Find convars/concommands with the specified string in their name/help text.", FCVAR_NONE);

	// these commands already exist, 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 = FCVAR_NONE;
	helpCommand->m_pCommandCallback = ConCommand_help;

	ConCommand* findCommand = g_pCVar->FindCommand("convar_findByFlags");
	findCommand->m_nFlags = FCVAR_NONE;
	findCommand->m_pCommandCallback = ConCommand_findflags;

	ConCommand* listCommand = g_pCVar->FindCommand("convar_list");
	listCommand->m_nFlags = FCVAR_NONE;
	listCommand->m_pCommandCallback = ConCommand_list;

	ConCommand* diffCommand = g_pCVar->FindCommand("convar_differences");
	diffCommand->m_nFlags = FCVAR_NONE;
	diffCommand->m_pCommandCallback = ConCommand_differences;
}