Skip to content
Permalink
Browse files

Refactor code to extract nickname and password in CClient::ClientInit…

…ialize
  • Loading branch information...
botder committed Sep 6, 2019
1 parent 632130e commit c292216945cc0e5d041c60052982d3f72be9eac6
Showing with 50 additions and 15 deletions.
  1. +40 −15 Client/mods/deathmatch/CClient.cpp
  2. +9 −0 Client/mods/deathmatch/CClient.h
  3. +1 −0 Client/mods/deathmatch/StdInc.h
@@ -144,14 +144,6 @@ int CClient::ClientInitialize(const char* szArguments, CCoreInterface* pCore)
}
else
{
// Parse the arguments (format <nick> <[pass]>)
char* szTemp = new char[strlen(szArguments) + 1];
strcpy(szTemp, szArguments);

// Split it up
char* szNick = strtok(szTemp, " ");
char* szPass = strtok(NULL, " ");

// Are we supposed to launch the server and play locally?
if (stricmp(szArguments, "local") == 0)
{
@@ -173,28 +165,25 @@ int CClient::ClientInitialize(const char* szArguments, CCoreInterface* pCore)
}
else
{
InitializeArguments arguments = ExtractInitializeArguments(szArguments);

// Got the nickname?
if (szNick)
if (!arguments.nickname.empty())
{
// Create clientgame
g_pClientGame = new CClientGame;

// Enable the packet recorder
// g_pClientGame->EnablePacketRecorder ( "log.rec" );
// g_pCore->GetConsole ()->Echo ( "Packetlogger is logging to log.rec" );

// Start the game
g_pClientGame->StartGame(szNick, szPass);
g_pClientGame->StartGame(arguments.nickname.c_str(), arguments.password.c_str());
}
else
{
g_pCore->GetConsole()->Print("deathmatch: not enough arguments");
g_pCore->GetModManager()->RequestUnload();
}
}

// Delete the temp buffer
delete[] szTemp;
}
}

@@ -306,3 +295,39 @@ void CClient::GetPlayerNames(std::vector<SString>& vPlayerNames)
}
}
}

CClient::InitializeArguments CClient::ExtractInitializeArguments(const char* arguments)
{
// Format: "nickname [password]"
// Examples: "GloriousToaster99 secret", "RandomPainter10"
std::string_view view(arguments);
using size_type = std::string_view::size_type;

InitializeArguments result;

// Search for the first whitespace delimiter character
if (size_t nicknameDelimiter = view.find_first_of(' '); nicknameDelimiter != std::string_view::npos)
{
result.nickname = view.substr(0, nicknameDelimiter);

// Search for the next non-whitespace character
if (nicknameDelimiter = view.find_first_not_of(' ', nicknameDelimiter); nicknameDelimiter != std::string_view::npos)
{
// Extract password from the string remainder
if (size_t passwordDelimiter = view.find_first_of(' ', nicknameDelimiter); passwordDelimiter != std::string_view::npos)
{
result.password = view.substr(nicknameDelimiter, passwordDelimiter - nicknameDelimiter);
}
else
{
result.password = view.substr(nicknameDelimiter);
}
}
}
else
{
result.nickname = view;
}

return result;
}
@@ -32,4 +32,13 @@ class CClient : public CClientBase

bool HandleException(CExceptionInformation* pExceptionInformation);
void GetPlayerNames(std::vector<SString>& vPlayerNames);

private:
struct InitializeArguments
{
std::string nickname;
std::string password;
};

static InitializeArguments ExtractInitializeArguments(const char* arguments);
};
@@ -16,6 +16,7 @@
#include <map>
#include <set>
#include <string>
#include <string_view>
#include <sstream>
#include <vector>
#include <cstdio>

1 comment on commit c292216

@Dutchman101

This comment has been minimized.

Copy link
Contributor

commented on c292216 Sep 6, 2019

For the record, this could fix a top 4 crash on client.dll

Please sign in to comment.
You can’t perform that action at this time.