mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-08-20 04:59:11 +00:00
Codechange: move client name in settings to std::string
This commit is contained in:
@@ -846,7 +846,7 @@ static void NetworkInitGameInfo()
|
||||
NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
|
||||
ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
|
||||
|
||||
strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
|
||||
strecpy(ci->client_name, _settings_client.network.client_name.c_str(), lastof(ci->client_name));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -857,10 +857,10 @@ static void NetworkInitGameInfo()
|
||||
*/
|
||||
static void CheckClientAndServerName()
|
||||
{
|
||||
static const char *fallback_client_name = "Unnamed Client";
|
||||
if (StrEmpty(_settings_client.network.client_name) || strcmp(_settings_client.network.client_name, fallback_client_name) == 0) {
|
||||
DEBUG(net, 1, "No \"client_name\" has been set, using \"%s\" instead. Please set this now using the \"name <new name>\" command", fallback_client_name);
|
||||
strecpy(_settings_client.network.client_name, fallback_client_name, lastof(_settings_client.network.client_name));
|
||||
static const std::string fallback_client_name = "Unnamed Client";
|
||||
if (_settings_client.network.client_name.empty() || _settings_client.network.client_name.compare(fallback_client_name) == 0) {
|
||||
DEBUG(net, 1, "No \"client_name\" has been set, using \"%s\" instead. Please set this now using the \"name <new name>\" command", fallback_client_name.c_str());
|
||||
_settings_client.network.client_name = fallback_client_name;
|
||||
}
|
||||
|
||||
static const std::string fallback_server_name = "Unnamed Server";
|
||||
|
@@ -1305,10 +1305,10 @@ void NetworkClientsToSpectators(CompanyID cid)
|
||||
* @param client_name The client name to check for validity.
|
||||
* @return True iff the name is valid.
|
||||
*/
|
||||
bool NetworkIsValidClientName(const char *client_name)
|
||||
bool NetworkIsValidClientName(const std::string_view client_name)
|
||||
{
|
||||
if (StrEmpty(client_name)) return false;
|
||||
if (*client_name == ' ') return false;
|
||||
if (client_name.empty()) return false;
|
||||
if (client_name[0] == ' ') return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1327,7 +1327,7 @@ bool NetworkIsValidClientName(const char *client_name)
|
||||
* and trailing spaces.
|
||||
* @return True iff the client name is valid.
|
||||
*/
|
||||
bool NetworkValidateClientName(char *client_name)
|
||||
bool NetworkValidateClientName(std::string &client_name)
|
||||
{
|
||||
StrTrimInPlace(client_name);
|
||||
if (NetworkIsValidClientName(client_name)) return true;
|
||||
@@ -1363,13 +1363,16 @@ void NetworkUpdateClientName()
|
||||
if (!NetworkValidateClientName()) return;
|
||||
|
||||
/* Don't change the name if it is the same as the old name */
|
||||
if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
|
||||
if (_settings_client.network.client_name.compare(ci->client_name) != 0) {
|
||||
if (!_network_server) {
|
||||
MyClient::SendSetName(_settings_client.network.client_name);
|
||||
MyClient::SendSetName(_settings_client.network.client_name.c_str());
|
||||
} else {
|
||||
if (NetworkFindName(_settings_client.network.client_name, lastof(_settings_client.network.client_name))) {
|
||||
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, _settings_client.network.client_name);
|
||||
strecpy(ci->client_name, _settings_client.network.client_name, lastof(ci->client_name));
|
||||
/* Copy to a temporary buffer so no #n gets added after our name in the settings when there are duplicate names. */
|
||||
char temporary_name[NETWORK_CLIENT_NAME_LENGTH];
|
||||
strecpy(temporary_name, _settings_client.network.client_name.c_str(), lastof(temporary_name));
|
||||
if (NetworkFindName(temporary_name, lastof(temporary_name))) {
|
||||
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, temporary_name);
|
||||
strecpy(ci->client_name, temporary_name, lastof(ci->client_name));
|
||||
NetworkUpdateClientInfo(CLIENT_ID_SERVER);
|
||||
}
|
||||
}
|
||||
|
@@ -35,9 +35,9 @@ extern StringList _network_host_list;
|
||||
extern StringList _network_ban_list;
|
||||
|
||||
byte NetworkSpectatorCount();
|
||||
bool NetworkIsValidClientName(const char *client_name);
|
||||
bool NetworkIsValidClientName(const std::string_view client_name);
|
||||
bool NetworkValidateClientName();
|
||||
bool NetworkValidateClientName(char *client_name);
|
||||
bool NetworkValidateClientName(std::string &client_name);
|
||||
void NetworkUpdateClientName();
|
||||
bool NetworkCompanyHasClients(CompanyID company);
|
||||
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password);
|
||||
|
@@ -462,7 +462,7 @@ public:
|
||||
this->FinishInitNested(WN_NETWORK_WINDOW_GAME);
|
||||
|
||||
this->querystrings[WID_NG_CLIENT] = &this->name_editbox;
|
||||
this->name_editbox.text.Assign(_settings_client.network.client_name);
|
||||
this->name_editbox.text.Assign(_settings_client.network.client_name.c_str());
|
||||
|
||||
this->querystrings[WID_NG_FILTER] = &this->filter_editbox;
|
||||
this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
|
||||
@@ -820,7 +820,7 @@ public:
|
||||
case WID_NG_CLIENT:
|
||||
/* Validation of the name will happen once the user tries to join or start a game, as getting
|
||||
* error messages while typing (e.g. when you clear the name) defeats the purpose of the check. */
|
||||
strecpy(_settings_client.network.client_name, this->name_editbox.text.buf, lastof(_settings_client.network.client_name));
|
||||
_settings_client.network.client_name = this->name_editbox.text.buf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2207,11 +2207,12 @@ public:
|
||||
}
|
||||
|
||||
case WID_CL_CLIENT_NAME_EDIT: {
|
||||
if (!NetworkValidateClientName(str)) break;
|
||||
std::string client_name(str);
|
||||
if (!NetworkValidateClientName(client_name)) break;
|
||||
|
||||
uint index;
|
||||
GetSettingFromName("network.client_name", &index);
|
||||
SetSettingValue(index, str);
|
||||
SetSettingValue(index, client_name.c_str());
|
||||
this->InvalidateData();
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user