Codechange: [Network] Pass passwords as std::string to the network code

This commit is contained in:
rubidium42 2021-05-02 09:18:56 +02:00 committed by rubidium42
parent ebe32ad912
commit 297d6e20bf
6 changed files with 24 additions and 28 deletions

View File

@ -844,7 +844,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
assert(_local_company == COMPANY_SPECTATOR); assert(_local_company == COMPANY_SPECTATOR);
SetLocalCompany(c->index); SetLocalCompany(c->index);
if (!_settings_client.network.default_company_pass.empty()) { if (!_settings_client.network.default_company_pass.empty()) {
NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass.c_str()); NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
} }
/* Now that we have a new company, broadcast our company settings to /* Now that we have a new company, broadcast our company settings to

View File

@ -1737,7 +1737,7 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
} }
CompanyID company_id; CompanyID company_id;
const char *password; std::string password;
const char *errormsg; const char *errormsg;
if (argc == 2) { if (argc == 2) {
@ -1759,10 +1759,10 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
password = NetworkChangeCompanyPassword(company_id, password); password = NetworkChangeCompanyPassword(company_id, password);
if (StrEmpty(password)) { if (password.empty()) {
IConsolePrintF(CC_WARNING, "Company password cleared"); IConsolePrintF(CC_WARNING, "Company password cleared");
} else { } else {
IConsolePrintF(CC_WARNING, "Company password changed to: %s", password); IConsolePrintF(CC_WARNING, "Company password changed to: %s", password.c_str());
} }
return true; return true;

View File

@ -152,9 +152,9 @@ byte NetworkSpectatorCount()
* @param password The unhashed password we like to set ('*' or '' resets the password) * @param password The unhashed password we like to set ('*' or '' resets the password)
* @return The password. * @return The password.
*/ */
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password) std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password)
{ {
if (strcmp(password, "*") == 0) password = ""; if (password.compare("*") == 0) password = "";
if (_network_server) { if (_network_server) {
NetworkServerSetCompanyPassword(company_id, password, false); NetworkServerSetCompanyPassword(company_id, password, false);
@ -787,7 +787,7 @@ public:
* @param join_company_password The password for the company. * @param join_company_password The password for the company.
* @return Whether the join has started. * @return Whether the join has started.
*/ */
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password, const char *join_company_password) bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password, const std::string &join_company_password)
{ {
CompanyID join_as = default_company; CompanyID join_as = default_company;
std::string resolved_connection_string = ParseGameConnectionString(connection_string, NETWORK_DEFAULT_PORT, &join_as).GetAddressAsString(false); std::string resolved_connection_string = ParseGameConnectionString(connection_string, NETWORK_DEFAULT_PORT, &join_as).GetAddressAsString(false);

View File

@ -1261,7 +1261,7 @@ void NetworkClient_Connected()
* @param password The password. * @param password The password.
* @param command The command to execute. * @param command The command to execute.
*/ */
void NetworkClientSendRcon(const char *password, const char *command) void NetworkClientSendRcon(const std::string &password, const char *command)
{ {
MyClient::SendRCon(password, command); MyClient::SendRCon(password, command);
} }

View File

@ -40,7 +40,7 @@ bool NetworkValidateClientName();
bool NetworkValidateClientName(std::string &client_name); bool NetworkValidateClientName(std::string &client_name);
void NetworkUpdateClientName(); void NetworkUpdateClientName();
bool NetworkCompanyHasClients(CompanyID company); bool NetworkCompanyHasClients(CompanyID company);
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password); std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password);
void NetworkReboot(); void NetworkReboot();
void NetworkDisconnect(bool blocking = false, bool close_admins = true); void NetworkDisconnect(bool blocking = false, bool close_admins = true);
void NetworkGameLoop(); void NetworkGameLoop();
@ -51,10 +51,10 @@ void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
void NetworkUpdateClientInfo(ClientID client_id); void NetworkUpdateClientInfo(ClientID client_id);
void NetworkClientsToSpectators(CompanyID cid); void NetworkClientsToSpectators(CompanyID cid);
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password = nullptr, const char *join_company_password = nullptr); bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password = "", const std::string &join_company_password = "");
void NetworkClientJoinGame(); void NetworkClientJoinGame();
void NetworkClientRequestMove(CompanyID company, const std::string &pass = ""); void NetworkClientRequestMove(CompanyID company, const std::string &pass = "");
void NetworkClientSendRcon(const char *password, const char *command); void NetworkClientSendRcon(const std::string &password, const char *command);
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0); void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio); bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio);
bool NetworkCompanyIsPassworded(CompanyID company_id); bool NetworkCompanyIsPassworded(CompanyID company_id);

View File

@ -400,23 +400,19 @@ void OpenBrowser(const char *url)
/** Callback structure of statements to be executed after the NewGRF scan. */ /** Callback structure of statements to be executed after the NewGRF scan. */
struct AfterNewGRFScan : NewGRFScanCallback { struct AfterNewGRFScan : NewGRFScanCallback {
Year startyear; ///< The start year. Year startyear = INVALID_YEAR; ///< The start year.
uint32 generation_seed; ///< Seed for the new game. uint32 generation_seed = GENERATE_NEW_SEED; ///< Seed for the new game.
std::string dedicated_host; ///< Hostname for the dedicated server. std::string dedicated_host; ///< Hostname for the dedicated server.
uint16 dedicated_port; ///< Port for the dedicated server. uint16 dedicated_port = 0; ///< Port for the dedicated server.
char *network_conn; ///< Information about the server to connect to, or nullptr. std::string connection_string; ///< Information about the server to connect to
const char *join_server_password; ///< The password to join the server with. std::string join_server_password; ///< The password to join the server with.
const char *join_company_password; ///< The password to join the company with. std::string join_company_password; ///< The password to join the company with.
bool save_config; ///< The save config setting. bool save_config = true; ///< The save config setting.
/** /**
* Create a new callback. * Create a new callback.
*/ */
AfterNewGRFScan() : AfterNewGRFScan()
startyear(INVALID_YEAR), generation_seed(GENERATE_NEW_SEED),
dedicated_port(0), network_conn(nullptr),
join_server_password(nullptr), join_company_password(nullptr),
save_config(true)
{ {
/* Visual C++ 2015 fails compiling this line (AfterNewGRFScan::generation_seed undefined symbol) /* Visual C++ 2015 fails compiling this line (AfterNewGRFScan::generation_seed undefined symbol)
* if it's placed outside a member function, directly in the struct body. */ * if it's placed outside a member function, directly in the struct body. */
@ -469,11 +465,11 @@ struct AfterNewGRFScan : NewGRFScanCallback {
/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */ /* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
if (_switch_mode != SM_NONE) MakeNewgameSettingsLive(); if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
if (_network_available && network_conn != nullptr) { if (_network_available && !connection_string.empty()) {
LoadIntroGame(); LoadIntroGame();
_switch_mode = SM_NONE; _switch_mode = SM_NONE;
NetworkClientConnectGame(network_conn, COMPANY_NEW_COMPANY, join_server_password, join_company_password); NetworkClientConnectGame(connection_string, COMPANY_NEW_COMPANY, join_server_password, join_company_password);
} }
/* After the scan we're not used anymore. */ /* After the scan we're not used anymore. */
@ -567,7 +563,7 @@ int openttd_main(int argc, char *argv[])
break; break;
case 'f': _dedicated_forks = true; break; case 'f': _dedicated_forks = true; break;
case 'n': case 'n':
scanner->network_conn = mgo.opt; // optional IP parameter, nullptr if unset scanner->connection_string = mgo.opt; // optional IP:port#company parameter
break; break;
case 'l': case 'l':
debuglog_conn = mgo.opt; debuglog_conn = mgo.opt;
@ -875,7 +871,7 @@ static void MakeNewGameDone()
/* We are the server, we start a new company (not dedicated), /* We are the server, we start a new company (not dedicated),
* so set the default password *if* needed. */ * so set the default password *if* needed. */
if (_network_server && !_settings_client.network.default_company_pass.empty()) { if (_network_server && !_settings_client.network.default_company_pass.empty()) {
NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass.c_str()); NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
} }
if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE); if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);