Changeset - r25487:e8c958a8b290
[Not reviewed]
master
0 5 0
rubidium42 - 4 years ago 2021-05-14 16:45:54
rubidium@openttd.org
Codechange: Use std::string GetString where convenient
5 files changed with 18 insertions and 45 deletions:
0 comments (0 inline, 0 general)
src/company_cmd.cpp
Show inline comments
 
@@ -1211,27 +1211,25 @@ CommandCost CmdGiveMoney(TileIndex tile,
 

	
 
	/* You can only transfer funds that is in excess of your loan */
 
	if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return_cmd_error(STR_ERROR_INSUFFICIENT_FUNDS);
 
	if (!Company::IsValidID(dest_company)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		/* Add money to company */
 
		Backup<CompanyID> cur_company(_current_company, dest_company, FILE_LINE);
 
		SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
 
		cur_company.Restore();
 

	
 
		if (_networking) {
 
			char dest_company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH];
 
			SetDParam(0, dest_company);
 
			GetString(dest_company_name, STR_COMPANY_NAME, lastof(dest_company_name));
 
			std::string dest_company_name = GetString(STR_COMPANY_NAME);
 

	
 
			char from_company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH];
 
			SetDParam(0, _current_company);
 
			GetString(from_company_name, STR_COMPANY_NAME, lastof(from_company_name));
 
			std::string from_company_name = GetString(STR_COMPANY_NAME);
 

	
 
			NetworkTextMessage(NETWORK_ACTION_GIVE_MONEY, GetDrawStringCompanyColour(_current_company), false, from_company_name, dest_company_name, amount.GetCost());
 
		}
 
	}
 

	
 
	/* Subtract money from local-company */
 
	return amount;
 
}
src/network/network.cpp
Show inline comments
 
@@ -358,27 +358,25 @@ void NetworkHandlePauseChange(PauseMode 
 
			} else {
 
				switch (changed_mode) {
 
					case PM_PAUSED_NORMAL:         SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_MANUAL); break;
 
					case PM_PAUSED_JOIN:           SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_CONNECTING_CLIENTS); break;
 
					case PM_PAUSED_GAME_SCRIPT:    SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_GAME_SCRIPT); break;
 
					case PM_PAUSED_ACTIVE_CLIENTS: SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_NOT_ENOUGH_PLAYERS); break;
 
					case PM_PAUSED_LINK_GRAPH:     SetDParam(0, STR_NETWORK_SERVER_MESSAGE_GAME_REASON_LINK_GRAPH); break;
 
					default: NOT_REACHED();
 
				}
 
				str = paused ? STR_NETWORK_SERVER_MESSAGE_GAME_PAUSED : STR_NETWORK_SERVER_MESSAGE_GAME_UNPAUSED;
 
			}
 

	
 
			char buffer[DRAW_STRING_BUFFER];
 
			GetString(buffer, str, lastof(buffer));
 
			NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, "", buffer);
 
			NetworkTextMessage(NETWORK_ACTION_SERVER_MESSAGE, CC_DEFAULT, false, "", GetString(str));
 
			break;
 
		}
 

	
 
		default:
 
			return;
 
	}
 
}
 

	
 

	
 
/**
 
 * Helper function for the pause checkers. If pause is true and the
 
 * current pause mode isn't set the game will be paused, if it it false
src/network/network_admin.cpp
Show inline comments
 
@@ -124,29 +124,27 @@ ServerNetworkAdminSocketHandler::~Server
 

	
 
/**
 
 * Send an error to the admin.
 
 * @param error The error to send.
 
 */
 
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendError(NetworkErrorCode error)
 
{
 
	Packet *p = new Packet(ADMIN_PACKET_SERVER_ERROR);
 

	
 
	p->Send_uint8(error);
 
	this->SendPacket(p);
 

	
 
	char str[100];
 
	StringID strid = GetNetworkErrorMsg(error);
 
	GetString(str, strid, lastof(str));
 
	std::string error_message = GetString(GetNetworkErrorMsg(error));
 

	
 
	DEBUG(net, 1, "[admin] The admin '%s' (%s) made an error and has been disconnected: '%s'", this->admin_name, this->admin_version, str);
 
	DEBUG(net, 1, "[admin] The admin '%s' (%s) made an error and has been disconnected: '%s'", this->admin_name, this->admin_version, error_message.c_str());
 

	
 
	return this->CloseConnection(true);
 
}
 

	
 
/** Send the protocol version to the admin. */
 
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendProtocol()
 
{
 
	Packet *p = new Packet(ADMIN_PACKET_SERVER_PROTOCOL);
 

	
 
	/* announce the protocol version */
 
	p->Send_uint8(NETWORK_GAME_ADMIN_VERSION);
 

	
 
@@ -307,74 +305,60 @@ NetworkRecvStatus ServerNetworkAdminSock
 

	
 
	this->SendPacket(p);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Send the admin some information about a company.
 
 * @param c The company to send the information about.
 
 */
 
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyInfo(const Company *c)
 
{
 
	char company_name[NETWORK_COMPANY_NAME_LENGTH];
 
	char manager_name[NETWORK_COMPANY_NAME_LENGTH];
 

	
 
	SetDParam(0, c->index);
 
	GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
 

	
 
	SetDParam(0, c->index);
 
	GetString(manager_name, STR_PRESIDENT_NAME, lastof(manager_name));
 

	
 
	Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_INFO);
 

	
 
	p->Send_uint8 (c->index);
 
	p->Send_string(company_name);
 
	p->Send_string(manager_name);
 
	SetDParam(0, c->index);
 
	p->Send_string(GetString(STR_COMPANY_NAME));
 
	SetDParam(0, c->index);
 
	p->Send_string(GetString(STR_PRESIDENT_NAME));
 
	p->Send_uint8 (c->colour);
 
	p->Send_bool  (NetworkCompanyIsPassworded(c->index));
 
	p->Send_uint32(c->inaugurated_year);
 
	p->Send_bool  (c->is_ai);
 
	p->Send_uint8 (CeilDiv(c->months_of_bankruptcy, 3)); // send as quarters_of_bankruptcy
 

	
 
	for (size_t i = 0; i < lengthof(c->share_owners); i++) {
 
		p->Send_uint8(c->share_owners[i]);
 
	}
 

	
 
	this->SendPacket(p);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 

	
 
/**
 
 * Send an update about a company.
 
 * @param c The company to send the update of.
 
 */
 
NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyUpdate(const Company *c)
 
{
 
	char company_name[NETWORK_COMPANY_NAME_LENGTH];
 
	char manager_name[NETWORK_COMPANY_NAME_LENGTH];
 

	
 
	SetDParam(0, c->index);
 
	GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
 

	
 
	SetDParam(0, c->index);
 
	GetString(manager_name, STR_PRESIDENT_NAME, lastof(manager_name));
 

	
 
	Packet *p = new Packet(ADMIN_PACKET_SERVER_COMPANY_UPDATE);
 

	
 
	p->Send_uint8 (c->index);
 
	p->Send_string(company_name);
 
	p->Send_string(manager_name);
 
	SetDParam(0, c->index);
 
	p->Send_string(GetString(STR_COMPANY_NAME));
 
	SetDParam(0, c->index);
 
	p->Send_string(GetString(STR_PRESIDENT_NAME));
 
	p->Send_uint8 (c->colour);
 
	p->Send_bool  (NetworkCompanyIsPassworded(c->index));
 
	p->Send_uint8 (CeilDiv(c->months_of_bankruptcy, 3)); // send as quarters_of_bankruptcy
 

	
 
	for (size_t i = 0; i < lengthof(c->share_owners); i++) {
 
		p->Send_uint8(c->share_owners[i]);
 
	}
 

	
 
	this->SendPacket(p);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
src/network/network_server.cpp
Show inline comments
 
@@ -423,62 +423,60 @@ NetworkRecvStatus ServerNetworkGameSocke
 

	
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Send an error to the client, and close its connection.
 
 * @param error The error to disconnect for.
 
 * @param reason In case of kicking a client, specifies the reason for kicking the client.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const char *reason)
 
{
 
	char str[100];
 
	Packet *p = new Packet(PACKET_SERVER_ERROR);
 

	
 
	p->Send_uint8(error);
 
	if (reason != nullptr) p->Send_string(reason);
 
	this->SendPacket(p);
 

	
 
	StringID strid = GetNetworkErrorMsg(error);
 
	GetString(str, strid, lastof(str));
 

	
 
	/* Only send when the current client was in game */
 
	if (this->status > STATUS_AUTHORIZED) {
 
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
 

	
 
		this->GetClientName(client_name, lastof(client_name));
 

	
 
		DEBUG(net, 1, "'%s' made an error and has been disconnected: %s", client_name, str);
 
		DEBUG(net, 1, "'%s' made an error and has been disconnected: %s", client_name, GetString(strid).c_str());
 

	
 
		if (error == NETWORK_ERROR_KICKED && reason != nullptr) {
 
			NetworkTextMessage(NETWORK_ACTION_KICKED, CC_DEFAULT, false, client_name, reason, strid);
 
		} else {
 
			NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
 
		}
 

	
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) {
 
				/* Some errors we filter to a more general error. Clients don't have to know the real
 
				 *  reason a joining failed. */
 
				if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) {
 
					error = NETWORK_ERROR_ILLEGAL_PACKET;
 
				}
 
				new_cs->SendErrorQuit(this->client_id, error);
 
			}
 
		}
 

	
 
		NetworkAdminClientError(this->client_id, error);
 
	} else {
 
		DEBUG(net, 1, "Client %d made an error and has been disconnected: %s", this->client_id, str);
 
		DEBUG(net, 1, "Client %d made an error and has been disconnected: %s", this->client_id, GetString(strid).c_str());
 
	}
 

	
 
	/* The client made a mistake, so drop the connection now! */
 
	return this->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
 
}
 

	
 
/** Send the check for the NewGRFs. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_CHECK_NEWGRFS);
 
	const GRFConfig *c;
 
	uint grf_count = 0;
 
@@ -1119,39 +1117,37 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	}
 

	
 
	if (GetCommandFlags(cp.cmd) & CMD_CLIENT_ID) cp.p2 = this->client_id;
 

	
 
	this->incoming_queue.Append(&cp);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet *p)
 
{
 
	/* This packets means a client noticed an error and is reporting this
 
	 *  to us. Display the error and report it to the other clients */
 
	char str[100];
 
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
 
	NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
 

	
 
	/* The client was never joined.. thank the client for the packet, but ignore it */
 
	if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
 
		return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
 
	}
 

	
 
	this->GetClientName(client_name, lastof(client_name));
 

	
 
	StringID strid = GetNetworkErrorMsg(errorno);
 
	GetString(str, strid, lastof(str));
 

	
 
	DEBUG(net, 1, "'%s' reported an error and is closing its connection: %s", client_name, str);
 
	DEBUG(net, 1, "'%s' reported an error and is closing its connection: %s", client_name, GetString(strid).c_str());
 

	
 
	NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
 

	
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs->status >= STATUS_AUTHORIZED) {
 
			new_cs->SendErrorQuit(this->client_id, errorno);
 
		}
 
	}
 

	
 
	NetworkAdminClientError(this->client_id, errorno);
 

	
 
	return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT);
 
@@ -1308,28 +1304,27 @@ void NetworkServerSendChat(NetworkAction
 
			if (ci != nullptr && ci_own != nullptr && ci_own->client_playas == dest) {
 
				NetworkTextMessage(action, GetDrawStringCompanyColour(ci->client_playas), false, ci->client_name, msg, data);
 
				if (from_id == CLIENT_ID_SERVER) show_local = false;
 
				ci_to = ci_own;
 
			}
 

	
 
			/* There is no such client */
 
			if (ci_to == nullptr) break;
 

	
 
			/* Display the message locally (so you know you have sent it) */
 
			if (ci != nullptr && show_local) {
 
				if (from_id == CLIENT_ID_SERVER) {
 
					char name[NETWORK_NAME_LENGTH];
 
					StringID str = Company::IsValidID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
 
					SetDParam(0, ci_to->client_playas);
 
					GetString(name, str, lastof(name));
 
					std::string name = GetString(str);
 
					NetworkTextMessage(action, GetDrawStringCompanyColour(ci_own->client_playas), true, name, msg, data);
 
				} else {
 
					for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
						if (cs->client_id == from_id) {
 
							cs->SendChat(action, ci_to->client_id, true, msg, data);
 
						}
 
					}
 
				}
 
			}
 
			break;
 
		}
 
		default:
src/newgrf.cpp
Show inline comments
 
@@ -7750,27 +7750,25 @@ static void TranslateGRFStrings(ByteRead
 
	uint32 grfid = buf->ReadDWord();
 
	const GRFConfig *c = GetGRFConfig(grfid);
 
	if (c == nullptr || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) {
 
		grfmsg(7, "TranslateGRFStrings: GRFID 0x%08x unknown, skipping action 13", BSWAP32(grfid));
 
		return;
 
	}
 

	
 
	if (c->status == GCS_INITIALISED) {
 
		/* If the file is not active but will be activated later, give an error
 
		 * and disable this file. */
 
		GRFError *error = DisableGrf(STR_NEWGRF_ERROR_LOAD_AFTER);
 

	
 
		char tmp[256];
 
		GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
 
		error->data = tmp;
 
		error->data = GetString(STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE);
 

	
 
		return;
 
	}
 

	
 
	/* Since no language id is supplied for with version 7 and lower NewGRFs, this string has
 
	 * to be added as a generic string, thus the language id of 0x7F. For this to work
 
	 * new_scheme has to be true as well, which will also be implicitly the case for version 8
 
	 * and higher. A language id of 0x7F will be overridden by a non-generic id, so this will
 
	 * not change anything if a string has been provided specifically for this language. */
 
	byte language = _cur.grffile->grf_version >= 8 ? buf->ReadByte() : 0x7F;
 
	byte num_strings = buf->ReadByte();
 
	uint16 first_id  = buf->ReadWord();
0 comments (0 inline, 0 general)