Changeset - r25464:b2a279799858
[Not reviewed]
master
0 11 0
rubidium42 - 4 years ago 2021-04-27 20:02:40
rubidium@openttd.org
Codechange: move client name in settings to std::string
11 files changed with 39 insertions and 61 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -714,20 +714,20 @@ DEF_CONSOLE_CMD(ConClientNickChange)
 

	
 
	if (NetworkClientInfo::GetByClientID(client_id) == nullptr) {
 
		IConsoleError("Invalid client");
 
		return true;
 
	}
 

	
 
	char *client_name = argv[2];
 
	std::string client_name(argv[2]);
 
	StrTrimInPlace(client_name);
 
	if (!NetworkIsValidClientName(client_name)) {
 
		IConsoleError("Cannot give a client an empty name");
 
		return true;
 
	}
 

	
 
	if (!NetworkServerChangeClientName(client_id, client_name)) {
 
	if (!NetworkServerChangeClientName(client_id, client_name.c_str())) {
 
		IConsoleError("Cannot give a client a duplicate name");
 
	}
 

	
 
	return true;
 
}
 

	
src/network/network.cpp
Show inline comments
 
@@ -843,27 +843,27 @@ static void NetworkInitGameInfo()
 

	
 
	/* There should be always space for the server. */
 
	assert(NetworkClientInfo::CanAllocateItem());
 
	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));
 
}
 

	
 
/**
 
 * Check whether the client and server name are set, for a dedicated server and if not set them to some default
 
 * value and tell the user to change this as soon as possible.
 
 * If the saved name is the default value, then the user is told to override  this value too.
 
 * This is only meant dedicated servers, as for the other servers the GUI ensures a name has been entered.
 
 */
 
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";
 
	if (_settings_client.network.server_name.empty() || _settings_client.network.server_name.compare(fallback_server_name) == 0) {
 
		DEBUG(net, 1, "No \"server_name\" has been set, using \"%s\" instead. Please set this now using the \"server_name <new name>\" command", fallback_server_name.c_str());
 
		_settings_client.network.server_name = fallback_server_name;
src/network/network_client.cpp
Show inline comments
 
@@ -1302,16 +1302,16 @@ void NetworkClientsToSpectators(CompanyI
 
 * 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)
 
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;
 
}
 

	
 
/**
 
 * Trim the given client name in place, i.e. remove leading and trailing spaces.
 
 * After the trim check whether the client name is valid. A client name is valid
 
@@ -1324,13 +1324,13 @@ bool NetworkIsValidClientName(const char
 
 * This function is not suitable for ensuring a valid client name at the server
 
 * as the error message will then be shown to the host instead of the client.
 
 * @param client_name The client name to validate. It will be trimmed of leading
 
 *                    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;
 

	
 
	ShowErrorMessage(STR_NETWORK_ERROR_BAD_PLAYER_NAME, INVALID_STRING_ID, WL_ERROR);
 
	return false;
 
@@ -1360,19 +1360,22 @@ void NetworkUpdateClientName()
 
	 * This method is called from that post change callback. So, when the client name is
 
	 * changed via the console there is no easy way to prevent an invalid name. Though,
 
	 * we can prevent it getting sent here. */
 
	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);
 
			}
 
		}
 
	}
 
}
 

	
src/network/network_func.h
Show inline comments
 
@@ -32,15 +32,15 @@ extern bool _network_need_advertise;
 
extern uint8 _network_reconnect;
 
extern StringList _network_bind_list;
 
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);
 
void NetworkReboot();
 
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
 
void NetworkGameLoop();
src/network/network_gui.cpp
Show inline comments
 
@@ -459,13 +459,13 @@ public:
 

	
 
		this->CreateNestedTree();
 
		this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR);
 
		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;
 
		this->SetFocusedWidget(WID_NG_FILTER);
 

	
 
		/* As the master-server doesn't support "websocket" servers yet, we
 
@@ -817,13 +817,13 @@ public:
 
				break;
 
			}
 

	
 
			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;
 
		}
 
	}
 

	
 
	void OnQueryTextFinished(char *str) override
 
	{
 
@@ -2204,17 +2204,18 @@ public:
 
				SetSettingValue(index, StrEmpty(str) ? "Unnamed Server" : str);
 
				this->InvalidateData();
 
				break;
 
			}
 

	
 
			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;
 
			}
 

	
 
			case WID_CL_COMPANY_JOIN:
 
				NetworkClientRequestMove(this->join_company, str);
src/newgrf_storage.h
Show inline comments
 
@@ -18,13 +18,13 @@
 
 */
 
enum PersistentStorageMode {
 
	PSM_ENTER_GAMELOOP,   ///< Enter the gameloop, changes will be permanent.
 
	PSM_LEAVE_GAMELOOP,   ///< Leave the gameloop, changes will be temporary.
 
	PSM_ENTER_COMMAND,    ///< Enter command scope, changes will be permanent.
 
	PSM_LEAVE_COMMAND,    ///< Leave command scope, revert to previous mode.
 
	PSM_ENTER_TESTMODE,   ///< Enter command test mode, changes will be tempoary.
 
	PSM_ENTER_TESTMODE,   ///< Enter command test mode, changes will be temporary.
 
	PSM_LEAVE_TESTMODE,   ///< Leave command test mode, revert to previous mode.
 
};
 

	
 
/**
 
 * Base class for all persistent NewGRF storage arrays. Nothing fancy, only here
 
 * so we have a generalised access to the virtual methods.
src/settings_type.h
Show inline comments
 
@@ -264,13 +264,13 @@ struct NetworkSettings {
 
	bool   server_admin_chat;                             ///< allow private chat for the server to be distributed to the admin network
 
	std::string server_name;                              ///< name of the server
 
	std::string server_password;                          ///< password for joining this server
 
	std::string rcon_password;                            ///< password for rconsole (server side)
 
	std::string admin_password;                           ///< password for the admin network
 
	bool   server_advertise;                              ///< advertise the server to the masterserver
 
	char   client_name[NETWORK_CLIENT_NAME_LENGTH];       ///< name of the player (as client)
 
	std::string client_name;                              ///< name of the player (as client)
 
	std::string default_company_pass;                     ///< default password for new companies in encrypted form
 
	std::string connect_to_ip;                            ///< default for the "Add server" query
 
	std::string network_id;                               ///< network ID for servers
 
	bool   autoclean_companies;                           ///< automatically remove companies that are not in use
 
	uint8  autoclean_unprotected;                         ///< remove passwordless companies after this many months
 
	uint8  autoclean_protected;                           ///< remove the password from passworded companies after this many months
src/string.cpp
Show inline comments
 
@@ -323,61 +323,38 @@ bool StrValid(const char *str, const cha
 
/**
 
 * Trim the spaces from the begin of given string in place, i.e. the string buffer
 
 * that is passed will be modified whenever spaces exist in the given string.
 
 * When there are spaces at the begin, the whole string is moved forward.
 
 * @param str The string to perform the in place left trimming on.
 
 */
 
static void StrLeftTrimInPlace(char *str)
 
static void StrLeftTrimInPlace(std::string &str)
 
{
 
	if (StrEmpty(str)) return;
 

	
 
	char *first_non_space = str;
 
	while (*first_non_space == ' ') first_non_space++;
 

	
 
	if (first_non_space == str) return;
 

	
 
	/* The source will reach '\0' first, but set the '\0' on the destination afterwards. */
 
	char *dst = str;
 
	for (char *src = first_non_space; *src != '\0'; dst++, src++) *dst = *src;
 
	*dst = '\0';
 
	size_t pos = str.find_first_not_of(' ');
 
	str.erase(0, pos);
 
}
 

	
 
/**
 
 * Trim the spaces from the end of given string in place, i.e. the string buffer
 
 * that is passed will be modified whenever spaces exist in the given string.
 
 * When there are spaces at the end, the '\0' will be moved forward.
 
 * @param str The string to perform the in place left trimming on.
 
 */
 
static void StrRightTrimInPlace(char *str)
 
static void StrRightTrimInPlace(std::string &str)
 
{
 
	if (StrEmpty(str)) return;
 

	
 
	char *end = str;
 
	while (*end != '\0') end++;
 

	
 
	char *last_non_space = end - 1;
 
	while (last_non_space >= str && *last_non_space == ' ') last_non_space--;
 

	
 
	/* The last non space points to the last character of the string that is not
 
	 * a space. For a string with only spaces or an empty string this would be
 
	 * the position before the begin of the string. The previous search ensures
 
	 * that this location before the string is not read.
 
	 * In any case, the character after the last non space character will be
 
	 * either a space or the existing termination, so it can be set to '\0'.
 
	 */
 
	last_non_space[1] = '\0';
 
	size_t pos = str.find_last_not_of(' ');
 
	if (pos != std::string::npos) str.erase(pos + 1);
 
}
 

	
 
/**
 
 * Trim the spaces from given string in place, i.e. the string buffer that
 
 * is passed will be modified whenever spaces exist in the given string.
 
 * When there are spaces at the begin, the whole string is moved forward
 
 * and when there are spaces at the back the '\0' termination is moved.
 
 * @param str The string to perform the in place trimming on.
 
 */
 
void StrTrimInPlace(char *str)
 
void StrTrimInPlace(std::string &str)
 
{
 
	StrLeftTrimInPlace(str);
 
	StrRightTrimInPlace(str);
 
}
 

	
 
/** Scans the string for colour codes and strips them */
src/string_func.h
Show inline comments
 
@@ -46,13 +46,13 @@ void ValidateString(const char *str);
 
void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
 
void str_strip_colours(char *str);
 
bool strtolower(char *str);
 
bool strtolower(std::string &str, std::string::size_type offs = 0);
 

	
 
bool StrValid(const char *str, const char *last) NOACCESS(2);
 
void StrTrimInPlace(char *str);
 
void StrTrimInPlace(std::string &str);
 

	
 
/**
 
 * Check if a string buffer is empty.
 
 *
 
 * @param s The pointer to the first element of the buffer
 
 * @return true if the buffer starts with the terminating null-character or
src/table/settings.h.preamble
Show inline comments
 
@@ -123,15 +123,12 @@ static size_t ConvertLandscape(const cha
 
#define SDTC_BOOL(var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
 
	SDTG_GENERAL(#var, SDT_BOOLX, SL_VAR, SLE_BOOL, flags, guiflags, _settings_client.var, 1, def, 0, 1, 0, nullptr, str, strhelp, strval, proc, from, to, cat, extra, startup)
 

	
 
#define SDTC_LIST(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
 
	SDTG_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, _settings_client.var, lengthof(_settings_client.var), def, 0, 0, 0, nullptr, str, strhelp, strval, proc, from, to, cat, extra, startup)
 

	
 
#define SDTC_STR(var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
 
	SDTG_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, _settings_client.var, sizeof(_settings_client.var), def, 0, 0, 0, nullptr, str, strhelp, strval, proc, from, to, cat, extra, startup)
 

	
 
#define SDTC_SSTR(var, type, flags, guiflags, def, max_length, str, strhelp, strval, proc, from, to, cat, extra, startup)\
 
	SDTG_GENERAL(#var, SDT_STDSTRING, SL_STDSTR, type, flags, guiflags, _settings_client.var, sizeof(_settings_client.var), def, 0, max_length, 0, nullptr, str, strhelp, strval, proc, from, to, cat, extra, startup)
 

	
 
#define SDTC_OMANY(var, type, flags, guiflags, def, max, full, str, strhelp, strval, proc, from, to, cat, extra, startup)\
 
	SDTG_GENERAL(#var, SDT_ONEOFMANY, SL_VAR, type, flags, guiflags, _settings_client.var, 1, def, 0, max, 0, full, str, strhelp, strval, proc, from, to, cat, extra, startup)
 

	
src/table/settings.ini
Show inline comments
 
@@ -66,13 +66,12 @@ const SettingDesc _settings[] = {
 
SDTG_BOOL  =  SDTG_BOOL($name,              $flags, $guiflags, $var, $def,                        $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTG_VAR   =   SDTG_VAR($name,       $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTG_OMANY = SDTG_OMANY($name,       $type, $flags, $guiflags, $var, $def,       $max, $full,     $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTC_BOOL  =  SDTC_BOOL(       $var,        $flags, $guiflags, $def,                              $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTC_LIST  =  SDTC_LIST(       $var, $type, $flags, $guiflags, $def,                              $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTC_OMANY = SDTC_OMANY(       $var, $type, $flags, $guiflags, $def,             $max, $full,     $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTC_STR   =   SDTC_STR(       $var, $type, $flags, $guiflags, $def,                              $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTC_SSTR  =  SDTC_SSTR(       $var, $type, $flags, $guiflags, $def,             $length,         $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDTC_VAR   =   SDTC_VAR(       $var, $type, $flags, $guiflags, $def,       $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDT_BOOL   =   SDT_BOOL($base, $var,        $flags, $guiflags, $def,                              $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDT_OMANY  =  SDT_OMANY($base, $var, $type, $flags, $guiflags, $def,             $max, $full,     $str, $strhelp, $strval, $proc, $from, $to, $load, $cat, $extra, $startup),
 
SDT_STR    =    SDT_STR($base, $var, $type, $flags, $guiflags, $def,                              $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
SDT_VAR    =    SDT_VAR($base, $var, $type, $flags, $guiflags, $def,       $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to,        $cat, $extra, $startup),
 
@@ -3914,15 +3913,16 @@ cat      = SC_EXPERT
 
[SDTC_BOOL]
 
var      = network.server_advertise
 
flags    = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
 
guiflags = SGF_NETWORK_ONLY
 
def      = false
 

	
 
[SDTC_STR]
 
[SDTC_SSTR]
 
var      = network.client_name
 
type     = SLE_STRB
 
type     = SLE_STR
 
length   = NETWORK_CLIENT_NAME_LENGTH
 
flags    = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
 
def      = nullptr
 
proc     = UpdateClientName
 
cat      = SC_BASIC
 

	
 
[SDTC_SSTR]
0 comments (0 inline, 0 general)