Codechange: [Network] Introduce function to validate the client name

pull/251/head
rubidium42 3 years ago committed by rubidium42
parent dc0efd5f2e
commit b14f412117

@ -1250,6 +1250,20 @@ void NetworkClientsToSpectators(CompanyID cid)
cur_company.Restore();
}
/**
* Check whether the given client name is deemed valid for use in network games.
* An empty name (null or '') is not valid as that is essentially no name at all.
* A name starting with white space is not valid for tab completion purposes.
* @param client_name The client name to check for validity.
* @return True iff the name is valid.
*/
bool NetworkIsValidClientName(const char *client_name)
{
if (StrEmpty(client_name)) return false;
if (*client_name == ' ') return false;
return true;
}
/**
* Send the server our name.
*/

@ -36,6 +36,7 @@ extern StringList _network_host_list;
extern StringList _network_ban_list;
byte NetworkSpectatorCount();
bool NetworkIsValidClientName(const char *client_name);
void NetworkUpdateClientName();
bool NetworkCompanyHasClients(CompanyID company);
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password);

@ -808,8 +808,7 @@ public:
}
case WID_NG_CLIENT:
/* Make sure the name does not start with a space, so TAB completion works */
if (!StrEmpty(this->name_editbox.text.buf) && this->name_editbox.text.buf[0] != ' ') {
if (NetworkIsValidClientName(this->name_editbox.text.buf)) {
strecpy(_settings_client.network.client_name, this->name_editbox.text.buf, lastof(_settings_client.network.client_name));
} else {
strecpy(_settings_client.network.client_name, "Player", lastof(_settings_client.network.client_name));

@ -946,7 +946,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
}
/* We need a valid name.. make it Player */
if (StrEmpty(name)) strecpy(name, "Player", lastof(name));
if (!NetworkIsValidClientName(name)) strecpy(name, "Player", lastof(name));
if (!NetworkFindName(name, lastof(name))) { // Change name if duplicate
/* We could not create a name for this client */

Loading…
Cancel
Save