summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-05-02 09:18:56 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-14 23:22:04 +0200
commit297d6e20bf0467d2a42e49bee291829ce3bb7c58 (patch)
treed428b9f170c31b1552b0365b21b916ff6ead6278 /src
parentebe32ad9129bb5846433366509728fb594f3be62 (diff)
downloadopenttd-297d6e20bf0467d2a42e49bee291829ce3bb7c58.tar.xz
Codechange: [Network] Pass passwords as std::string to the network code
Diffstat (limited to 'src')
-rw-r--r--src/company_cmd.cpp2
-rw-r--r--src/console_cmds.cpp6
-rw-r--r--src/network/network.cpp6
-rw-r--r--src/network/network_client.cpp2
-rw-r--r--src/network/network_func.h6
-rw-r--r--src/openttd.cpp30
6 files changed, 24 insertions, 28 deletions
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp
index 45a679c10..17923377a 100644
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -844,7 +844,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
assert(_local_company == COMPANY_SPECTATOR);
SetLocalCompany(c->index);
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
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp
index 82638a5cb..a417831cb 100644
--- a/src/console_cmds.cpp
+++ b/src/console_cmds.cpp
@@ -1737,7 +1737,7 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
}
CompanyID company_id;
- const char *password;
+ std::string password;
const char *errormsg;
if (argc == 2) {
@@ -1759,10 +1759,10 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
password = NetworkChangeCompanyPassword(company_id, password);
- if (StrEmpty(password)) {
+ if (password.empty()) {
IConsolePrintF(CC_WARNING, "Company password cleared");
} else {
- IConsolePrintF(CC_WARNING, "Company password changed to: %s", password);
+ IConsolePrintF(CC_WARNING, "Company password changed to: %s", password.c_str());
}
return true;
diff --git a/src/network/network.cpp b/src/network/network.cpp
index 61baa7617..80ecfc2cd 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -152,9 +152,9 @@ byte NetworkSpectatorCount()
* @param password The unhashed password we like to set ('*' or '' resets 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) {
NetworkServerSetCompanyPassword(company_id, password, false);
@@ -787,7 +787,7 @@ public:
* @param join_company_password The password for the company.
* @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;
std::string resolved_connection_string = ParseGameConnectionString(connection_string, NETWORK_DEFAULT_PORT, &join_as).GetAddressAsString(false);
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index 7e2c7bb23..b6da3dc18 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -1261,7 +1261,7 @@ void NetworkClient_Connected()
* @param password The password.
* @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);
}
diff --git a/src/network/network_func.h b/src/network/network_func.h
index bedc9bc54..82a5deb45 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -40,7 +40,7 @@ bool NetworkValidateClientName();
bool NetworkValidateClientName(std::string &client_name);
void NetworkUpdateClientName();
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 NetworkDisconnect(bool blocking = false, bool close_admins = true);
void NetworkGameLoop();
@@ -51,10 +51,10 @@ void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
void NetworkUpdateClientInfo(ClientID client_id);
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 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);
bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio);
bool NetworkCompanyIsPassworded(CompanyID company_id);
diff --git a/src/openttd.cpp b/src/openttd.cpp
index 5674a4671..fec8bf9ca 100644
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -400,23 +400,19 @@ void OpenBrowser(const char *url)
/** Callback structure of statements to be executed after the NewGRF scan. */
struct AfterNewGRFScan : NewGRFScanCallback {
- Year startyear; ///< The start year.
- uint32 generation_seed; ///< Seed for the new game.
- std::string dedicated_host; ///< Hostname for the dedicated server.
- uint16 dedicated_port; ///< Port for the dedicated server.
- char *network_conn; ///< Information about the server to connect to, or nullptr.
- const char *join_server_password; ///< The password to join the server with.
- const char *join_company_password; ///< The password to join the company with.
- bool save_config; ///< The save config setting.
+ Year startyear = INVALID_YEAR; ///< The start year.
+ uint32 generation_seed = GENERATE_NEW_SEED; ///< Seed for the new game.
+ std::string dedicated_host; ///< Hostname for the dedicated server.
+ uint16 dedicated_port = 0; ///< Port for the dedicated server.
+ std::string connection_string; ///< Information about the server to connect to
+ std::string join_server_password; ///< The password to join the server with.
+ std::string join_company_password; ///< The password to join the company with.
+ bool save_config = true; ///< The save config setting.
/**
* Create a new callback.
*/
- 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)
+ AfterNewGRFScan()
{
/* 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. */
@@ -469,11 +465,11 @@ struct AfterNewGRFScan : NewGRFScanCallback {
/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
- if (_network_available && network_conn != nullptr) {
+ if (_network_available && !connection_string.empty()) {
LoadIntroGame();
_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. */
@@ -567,7 +563,7 @@ int openttd_main(int argc, char *argv[])
break;
case 'f': _dedicated_forks = true; break;
case 'n':
- scanner->network_conn = mgo.opt; // optional IP parameter, nullptr if unset
+ scanner->connection_string = mgo.opt; // optional IP:port#company parameter
break;
case 'l':
debuglog_conn = mgo.opt;
@@ -875,7 +871,7 @@ static void MakeNewGameDone()
/* We are the server, we start a new company (not dedicated),
* so set the default password *if* needed. */
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);