Skip to content

Commit 4d9df51

Browse files
committed
Completed a lot more code optimizations and clean up
1 parent 75861a7 commit 4d9df51

File tree

6 files changed

+152
-134
lines changed

6 files changed

+152
-134
lines changed

commands.cpp

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ConVar p2mm_splitscreen("p2mm_splitscreen", "0", FCVAR_HIDDEN, "Flag for the mai
2121
//---------------------------------------------------------------------------------
2222
// UTIL P2:MM ConVars | ConVars the host can change.
2323
//---------------------------------------------------------------------------------
24-
ConVar p2mm_forbidclientcommands("p2mm_forbidclientcommands", "1", FCVAR_NONE, "Stop client commands clients shouldn't be executing.");
24+
ConVar p2mm_forbid_clientcommands("p2mm_forbid_clientcommands", "1", FCVAR_NONE, "Stop client commands clients shouldn't be executing.");
2525
ConVar p2mm_deathicons("p2mm_deathicons", "1", FCVAR_NONE, "Whether or not when players die the death icon should appear.");
2626
ConVar p2mm_instantrespawn("p2mm_instantrespawn", "0", FCVAR_NONE, "Whether respawning should be instant or not.");
2727

@@ -30,22 +30,21 @@ ConVar p2mm_instantrespawn("p2mm_instantrespawn", "0", FCVAR_NONE, "Whether resp
3030
//---------------------------------------------------------------------------------
3131
ConVar p2mm_developer("p2mm_developer", "0", FCVAR_NONE, "Enable for P2:MM developer messages.");
3232

33-
void UpdateDisplayGEsConVar(IConVar* var, const char* pOldValue, float flOldValue)
33+
static void UpdateDisplayGEsConVar(IConVar* var, const char* pOldValue, float flOldValue)
3434
{
35-
ConVar* pGEConVar = g_pCVar->FindVar("display_game_events");
36-
if (pGEConVar)
37-
pGEConVar->SetValue(((ConVar*)var)->GetBool());
35+
if (ConVar* pGEConVar = g_pCVar->FindVar("display_game_events"))
36+
pGEConVar->SetValue(dynamic_cast<ConVar*>(var)->GetBool());
3837
}
39-
ConVar p2mm_spewgameeventinfo("p2mm_spewgameeventinfo", "0", FCVAR_NONE, "Log information from called game events in the console, p2mm_developer must also be on. Can cause lots of console spam.", UpdateDisplayGEsConVar);
38+
ConVar p2mm_spew_gameevent_info("p2mm_spew_gameevent_info", "0", FCVAR_NONE, "Log information from called game events in the console, p2mm_developer must also be on. Can cause lots of console spam.", UpdateDisplayGEsConVar);
4039

4140
//---------------------------------------------------------------------------------
4241
// P2:MM p2mm_map ConCommand Logic
4342
//---------------------------------------------------------------------------------
44-
std::vector<std::string> mapList; // List of maps for the p2mm_map command auto complete.
45-
std::vector<std::string> workshopMapList; // List of all workshop map for the p2mm_map auto complete.
43+
static std::vector<std::string> mapList; // List of maps for the p2mm_map command auto complete.
44+
static std::vector<std::string> workshopMapList; // List of all workshop map for the p2mm_map auto complete.
4645

47-
// Update the map list avaliable to p2mm_map by scanning for all map files in SearchPath.
48-
void updateMapsList() {
46+
// Update the map list available to p2mm_map by scanning for all map files in SearchPath.
47+
void UpdateMapsList() {
4948
mapList.clear();
5049
CUtlVector<CUtlString> outList;
5150
AddFilesToList(outList, "maps", "GAME", "bsp");
@@ -54,9 +53,9 @@ void updateMapsList() {
5453
{
5554
// Get each map and get their relative path to each SearchPath and make slashes forward slashes.
5655
// Then turn relativePath into a std::string to easily manipulate.
57-
const char* curmap = outList[i];
56+
const char* curMap = outList[i];
5857
char relativePath[MAX_PATH] = { 0 };
59-
g_pFileSystem->FullPathToRelativePathEx(curmap, "GAME", relativePath, sizeof(relativePath));
58+
g_pFileSystem->FullPathToRelativePathEx(curMap, "GAME", relativePath, sizeof(relativePath));
6059
V_FixSlashes(relativePath, '/');
6160
V_StripExtension(relativePath, relativePath, sizeof(relativePath));
6261
std::string fixedRelativePath(relativePath);
@@ -65,14 +64,14 @@ void updateMapsList() {
6564
fixedRelativePath.erase(0, strlen("maps/"));
6665

6766
// Remove the whole "workshop/(workshop id)" part if there isn't multiple workshop maps of the same file name.
68-
size_t lastSlashPos = fixedRelativePath.find_last_of("/");
67+
size_t lastSlashPos = fixedRelativePath.find_last_of('/');
6968
if (lastSlashPos != std::string::npos && fixedRelativePath.rfind("workshop/") != std::string::npos)
7069
{
7170
fixedRelativePath.erase(0, strlen("workshop/"));
7271
workshopMapList.push_back(fixedRelativePath);
7372
}
7473

75-
// Push the map string on to the list to display avaliable options for the command.
74+
// Push the map string on to the list to display available options for the command.
7675
mapList.push_back(std::move(fixedRelativePath));
7776
}
7877
}
@@ -82,22 +81,22 @@ static int p2mm_map_CompletionFunc(const char* partial, char commands[COMMAND_CO
8281
{
8382
// If the map list is empty, generate it.
8483
if (mapList.empty()) {
85-
updateMapsList();
84+
UpdateMapsList();
8685
}
8786

8887
// Assemble together the current state of the inputted command.
89-
const char* concommand = "p2mm_map ";
90-
const char* match = (V_strstr(partial, concommand) == partial) ? partial + V_strlen(concommand) : partial;
88+
const char* conCommand = "p2mm_map ";
89+
const char* match = (V_strstr(partial, conCommand) == partial) ? partial + V_strlen(conCommand) : partial;
9190

9291
// Go through the map list searching for matches with the assembled inputted command.
9392
int numMatchedMaps = 0;
94-
for (const std::string map : mapList)
93+
for (const std::string& map : mapList)
9594
{
9695
if (numMatchedMaps >= COMMAND_COMPLETION_MAXITEMS) break;
9796

9897
if (V_strstr(map.c_str(), match))
9998
{
100-
V_snprintf(commands[numMatchedMaps++], COMMAND_COMPLETION_ITEM_LENGTH, "%s%s", concommand, map.c_str());
99+
V_snprintf(commands[numMatchedMaps++], COMMAND_COMPLETION_ITEM_LENGTH, "%s%s", conCommand, map.c_str());
101100
}
102101
}
103102

@@ -108,14 +107,14 @@ CON_COMMAND_F_COMPLETION(p2mm_map, "Starts up a P2:MM session with a requested m
108107
{
109108
// If the map list is empty, generate it.
110109
if (mapList.empty()) {
111-
updateMapsList();
110+
UpdateMapsList();
112111
}
113112

114113
// Make sure the CON_COMMAND was executed correctly.
115114
if (args.ArgC() < 2 || FStrEq(args.Arg(1), ""))
116115
{
117-
updateMapsList();
118116
P2MMLog(WARNING, false, "p2mm_map called incorrectly! Usage: \"p2mm_map (map to start)\"");
117+
UpdateMapsList();
119118
return;
120119
}
121120

@@ -142,7 +141,7 @@ CON_COMMAND_F_COMPLETION(p2mm_map, "Starts up a P2:MM session with a requested m
142141
P2MMLog(WARNING, false, "p2mm_map was called with P2MM_LASTMAP, but p2mm_lastmap is empty or invalid!");
143142
engineClient->ExecuteClientCmd("disconnect \"There is no last map recorded or the map doesn't exist! Please start a play session with the other options first.\"");
144143
engineClient->ExecuteClientCmd(completePVCmd);
145-
updateMapsList();
144+
UpdateMapsList();
146145
return;
147146
}
148147
V_strcpy(requestedMap, p2mm_lastmap.GetString());
@@ -152,20 +151,17 @@ CON_COMMAND_F_COMPLETION(p2mm_map, "Starts up a P2:MM session with a requested m
152151

153152
// Check if the requested map is a workshop map.
154153
std::string tempMapStr = requestedMap;
155-
for (const std::string map : workshopMapList)
154+
for (const std::string& map : workshopMapList)
156155
{
157156
if (tempMapStr == map)
158-
{
159-
tempMapStr = std::string("workshop/" + tempMapStr);
160-
V_strcpy(requestedMap, tempMapStr.c_str());
161-
}
157+
V_strcpy(requestedMap, std::string("workshop/" + tempMapStr).c_str());
162158
}
163159

164160
// Check if the supplied map is a valid map.
165161
if (!engineServer->IsMapValid(requestedMap))
166162
{
167-
updateMapsList();
168163
P2MMLog(WARNING, false, "p2mm_map was given a non-valid map or one that doesn't exist! \"%s\"", requestedMap);
164+
UpdateMapsList();
169165
return;
170166
}
171167

@@ -201,7 +197,7 @@ CON_COMMAND_F_COMPLETION(p2mm_map, "Starts up a P2:MM session with a requested m
201197

202198
CON_COMMAND(p2mm_updatemaplist, "Manually updates the list of available maps that can be loaded with p2mm_map.")
203199
{
204-
updateMapsList();
200+
UpdateMapsList();
205201
}
206202

207203
CON_COMMAND(p2mm_maplist, "Lists available maps that can be loaded with p2mm_map.")
@@ -229,8 +225,8 @@ CON_COMMAND(p2mm_respawnall, "Respawns all players.")
229225
}
230226
}
231227

232-
bool m_ConVarConCommandsShown = false; // Bool to track if the hidden ConVars and ConCommands are showing.
233-
std::vector<ConCommandBase*> toggledCVCCs; // List of toggled ConVars and ConCommands with the FCVAR_DEVELOPMENTONLY and FCVAR_HIDDEN ConVar flags removed.
228+
static bool m_ConVarConCommandsShown = false; // Bool to track if the hidden ConVars and ConCommands are showing.
229+
static std::vector<ConCommandBase*> toggledCVCCs; // List of toggled ConVars and ConCommands with the FCVAR_DEVELOPMENTONLY and FCVAR_HIDDEN ConVar flags removed.
234230
CON_COMMAND_F(p2mm_toggle_dev_cc_cvars, "Toggle showing any ConVars and ConCommands that have the FCVAR_DEVELOPMENTONLY and FCVAR_HIDDEN ConVar flags.", FCVAR_HIDDEN)
235231
{
236232
int iToggleCount = 0; // To tell the user how many ConVars and ConCommands where toggle to show or hide.
@@ -311,7 +307,7 @@ CON_COMMAND_F(p2mm_helloworld2, "Hello World 2: Electric Boogaloo!", FCVAR_HIDDE
311307
ConVar p2mm_gelocity_laps_default("p2mm_gelocity_laps_default", "3", FCVAR_NONE, "Set the default amount of laps for a Gelocity race.", true, 1, true, 300);
312308
ConVar p2mm_gelocity_music_default("p2mm_gelocity_music_default", "0", FCVAR_NONE, "Set the default music track for a Gelocity race.", true, 0, true, 5);
313309

314-
void GelocityTournament(IConVar* var, const char* pOldValue, float flOldValue)
310+
static void GelocityTournament(IConVar* var, const char* pOldValue, float flOldValue)
315311
{
316312
// Check if host is in a gelocity map.
317313
if (!InGelocityMap())
@@ -337,7 +333,7 @@ void GelocityTournament(IConVar* var, const char* pOldValue, float flOldValue)
337333
}
338334
ConVar p2mm_gelocity_tournamentmode("p2mm_gelocity_tournamentmode", "0", FCVAR_NONE, "Turn on or off tournament mode.", true, 0, true, 1, GelocityTournament);
339335

340-
void GelocityButtons(IConVar* var, const char* pOldValue, float flOldValue)
336+
static void GelocityButtons(IConVar* var, const char* pOldValue, float flOldValue)
341337
{
342338
// Check if host is in a gelocity map.
343339
if (!InGelocityMap())
@@ -351,7 +347,7 @@ void GelocityButtons(IConVar* var, const char* pOldValue, float flOldValue)
351347
}
352348

353349
// Lock or unlock the buttons.
354-
if (!((ConVar*)var)->GetBool())
350+
if (!dynamic_cast<ConVar*>(var)->GetBool())
355351
{
356352
g_pScriptVM->Run(
357353
"EntFire(\"rounds_button_1\", \"Unlock\");"
@@ -427,7 +423,7 @@ CON_COMMAND(p2mm_gelocity_laps, "Set lap count for the Gelocity Race. Specify 0
427423
lapMessage.fxTime = 0.f;
428424
lapMessage.channel = 3;
429425

430-
UTIL_HudMessage(NULL, lapMessage, std::string("Race Laps: " + std::string(args.Arg(1))).c_str());
426+
UTIL_HudMessage(nullptr, lapMessage, std::string("Race Laps: " + std::string(args.Arg(1))).c_str());
431427
}
432428

433429
CON_COMMAND(p2mm_gelocity_music, "Set the music track for the Gelocity Race. 0-5 0 = No Music.")
@@ -462,7 +458,7 @@ CON_COMMAND(p2mm_gelocity_music, "Set the music track for the Gelocity Race. 0-5
462458
return;
463459
}
464460

465-
g_pScriptVM->Run(std::string("iMusicTrack <- " + std::to_string(V_atoi(args.Arg(1))) + "; EntFire(\"counter_music\", \"SetValue\", iMusicTrack.tostring());").c_str(), false);
461+
g_pScriptVM->Run(std::string("iMusicTrack <- " + std::to_string(V_atoi(args.Arg(1))) + R"(; EntFire("counter_music", "SetValue", iMusicTrack.tostring());)").c_str(), false);
466462
HudMessageParams musicMessage;
467463
musicMessage.x = -1;
468464
musicMessage.y = 0.2f;
@@ -564,10 +560,11 @@ void RemovePlayerOperation(bool bBanning, int userid)
564560
P2MMLog(INFO, true, "Banning?: %i", bBanning);
565561
P2MMLog(INFO, true, "userID: %i", bannedPlayer.userID);
566562
P2MMLog(INFO, true, "username: %s", bannedPlayer.username.c_str());
563+
P2MMLog(INFO, true, "guid: %s", bannedPlayer.guid.c_str());
567564
}
568565

569566
// Display UI for either banning or kicking so host can ban or kick a player.
570-
void RemovePlayerUI(int playerIndex, bool bBanning)
567+
void RemovePlayerUI(const int playerIndex, const bool bBanning)
571568
{
572569
if (!IsGameActive())
573570
{

commands.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ extern ConVar p2mm_splitscreen;
3333
//---------------------------------------------------------------------------------
3434
// Core P2:MM ConVars | These shouldn't be modified manually. Hidden to prevent accidentally breaking something.
3535
//---------------------------------------------------------------------------------
36-
extern ConVar p2mm_forbidclientcommands;
36+
extern ConVar p2mm_forbid_clientcommands;
3737
extern ConVar p2mm_deathicons;
3838
extern ConVar p2mm_instantrespawn;
3939

4040
//---------------------------------------------------------------------------------
4141
// Debug P2:MM ConVars | Self-explanatory.
4242
//---------------------------------------------------------------------------------
4343
extern ConVar p2mm_developer;
44-
extern ConVar p2mm_spewgameeventinfo;
44+
extern ConVar p2mm_spew_gameevent_info;

0 commit comments

Comments
 (0)