Changeset - r2071:5fee74696551
[Not reviewed]
master
0 2 0
ludde - 19 years ago 2005-07-15 18:43:39
ludde@openttd.org
(svn r2581) Fix: Behave a bit safer if central server sends bogus information about clients.
Change: Made code a little bit more readable by putting _selected_item in a local var with a shorter name.
2 files changed with 39 insertions and 31 deletions:
0 comments (0 inline, 0 general)
network.h
Show inline comments
 
@@ -40,24 +40,26 @@
 
// How many vehicle/station types we put over the network
 
#define NETWORK_VEHICLE_TYPES 5
 
#define NETWORK_STATION_TYPES 5
 

	
 
enum {
 
	NETWORK_NAME_LENGTH        = 80,
 
	NETWORK_HOSTNAME_LENGTH    = 80,
 
	NETWORK_REVISION_LENGTH    = 10,
 
	NETWORK_PASSWORD_LENGTH    = 20,
 
	NETWORK_PLAYERS_LENGTH     = 200,
 
	NETWORK_CLIENT_NAME_LENGTH = 25,
 
	NETWORK_RCONCOMMAND_LENGTH = 500,
 

	
 
	NETWORK_NUM_LANGUAGES      = 4,
 
};
 

	
 
// This is the struct used by both client and server
 
//  some fields will be empty on the client (like game_password) by default
 
//  and only filled with data a player enters.
 
typedef struct NetworkGameInfo {
 
	char server_name[NETWORK_NAME_LENGTH];					// Server name
 
	char hostname[NETWORK_HOSTNAME_LENGTH];					// Hostname of the server (if any)
 
	char server_revision[NETWORK_REVISION_LENGTH];	// The SVN version number the server is using (e.g.: 'r304')
 
																									//  It even shows a SVN version in release-version, so
 
																									//  it is easy to compare if a server is of the correct version
 
	byte server_lang;																// Language of the server (we should make a nice table for this)
network_gui.c
Show inline comments
 
@@ -90,36 +90,38 @@ void UpdateNetworkGameWindow(bool unsele
 
		SetWindowDirty(w);
 
	}
 
}
 

	
 
static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
 
{
 
	switch (e->event) {
 
	case WE_CREATE: /* focus input box */
 
		_selected_field = 3;
 
		_selected_item = NULL;
 
		break;
 
	case WE_PAINT: {
 
		NetworkGameList *sel = _selected_item;
 

	
 
		w->disabled_state = 0;
 

	
 
		if (_selected_item == NULL) {
 
		if (sel == NULL) {
 
			SETBIT(w->disabled_state, 17); SETBIT(w->disabled_state, 18);
 
		} else if (!_selected_item->online) {
 
		} else if (!sel->online) {
 
			SETBIT(w->disabled_state, 17); // Server offline, join button disabled
 
		} else if (_selected_item->info.clients_on == _selected_item->info.clients_max) {
 
		} else if (sel->info.clients_on == sel->info.clients_max) {
 
			SETBIT(w->disabled_state, 17); // Server full, join button disabled
 

	
 
			// revisions don't match, check if server has no revision; then allow connection
 
		} else if (strncmp(_selected_item->info.server_revision, _openttd_revision, NETWORK_REVISION_LENGTH - 1) != 0) {
 
			if (strncmp(_selected_item->info.server_revision, NOREV_STRING, sizeof(_selected_item->info.server_revision)) != 0)
 
		} else if (strncmp(sel->info.server_revision, _openttd_revision, NETWORK_REVISION_LENGTH - 1) != 0) {
 
			if (strncmp(sel->info.server_revision, NOREV_STRING, sizeof(sel->info.server_revision)) != 0)
 
				SETBIT(w->disabled_state, 17); // Revision mismatch, join button disabled
 
		}
 

	
 
		SetDParam(0, 0x00);
 
		SetDParam(7, _lan_internet_types_dropdown[_network_lan_internet]);
 
		DrawWindowWidgets(w);
 

	
 
		DrawEditBox(w, 3);
 

	
 
		DrawString(9, 23, STR_NETWORK_PLAYER_NAME, 2);
 
		DrawString(9, 43, STR_NETWORK_CONNECTION, 2);
 

	
 
@@ -130,29 +132,29 @@ static void NetworkGameWindowWndProc(Win
 
			uint16 y = NET_PRC__OFFSET_TOP_WIDGET + 3;
 
			int32 n = 0;
 
			int32 pos = w->vscroll.pos;
 
			char servername[NETWORK_NAME_LENGTH];
 
			const NetworkGameList *cur_item = _network_game_list;
 

	
 
			while (pos > 0 && cur_item != NULL) {
 
				pos--;
 
				cur_item = cur_item->next;
 
			}
 

	
 
			while (cur_item != NULL) {
 
				bool compatible = (strncmp(cur_item->info.server_revision, _openttd_revision, NETWORK_REVISION_LENGTH - 1) == 0);
 
				if (strncmp(cur_item->info.server_revision, NOREV_STRING, sizeof(cur_item->info.server_revision)) == 0)
 
					compatible = true;
 
				bool compatible =
 
					!strncmp(cur_item->info.server_revision, _openttd_revision, NETWORK_REVISION_LENGTH - 1) ||
 
					!strncmp(cur_item->info.server_revision, NOREV_STRING, sizeof(cur_item->info.server_revision));
 

	
 
				if (cur_item == _selected_item)
 
				if (cur_item == sel)
 
					GfxFillRect(11, y - 2, 218, y + 9, 10); // show highlighted item with a different colour
 

	
 
				snprintf(servername, sizeof(servername), "%s", cur_item->info.server_name);
 
				NetworkTruncateString(servername, 110);
 
				DoDrawString(servername, 15, y, 16); // server name
 

	
 
				SetDParam(0, cur_item->info.clients_on);
 
				SetDParam(1, cur_item->info.clients_max);
 
				DrawString(135, y, STR_NETWORK_CLIENTS_ONLINE, 2);
 

	
 
				// only draw icons if the server is online
 
				if (cur_item->online) {
 
@@ -167,87 +169,91 @@ static void NetworkGameWindowWndProc(Win
 
					// draw flag according to server language
 
					DrawSprite(SPR_FLAGS_BASE + cur_item->info.server_lang, 206, y);
 
				}
 

	
 
				cur_item = cur_item->next;
 
				y += NET_PRC__SIZE_OF_ROW;
 
				if (++n == w->vscroll.cap) { break;} // max number of games in the window
 
			}
 
		}
 

	
 
		// right menu
 
		GfxFillRect(252, 23, 478, 65, 157);
 
		if (_selected_item == NULL) {
 
		if (sel == NULL) {
 
			DrawStringMultiCenter(365, 40, STR_NETWORK_GAME_INFO, 0);
 
		} else if (!_selected_item->online) {
 
			SetDParamStr(0, _selected_item->info.server_name);
 
		} else if (!sel->online) {
 
			SetDParamStr(0, sel->info.server_name);
 
			DrawStringMultiCenter(365, 42, STR_ORANGE, 2); // game name
 

	
 
			DrawStringMultiCenter(365, 110, STR_NETWORK_SERVER_OFFLINE, 2); // server offline
 
		} else { // show game info
 
			uint16 y = 70;
 

	
 
			DrawStringMultiCenter(365, 30, STR_NETWORK_GAME_INFO, 0);
 

	
 

	
 
			SetDParamStr(0, _selected_item->info.server_name);
 
			SetDParamStr(0, sel->info.server_name);
 
			DrawStringMultiCenter(365, 42, STR_ORANGE, 2); // game name
 

	
 
			SetDParamStr(0, _selected_item->info.map_name);
 
			SetDParamStr(0, sel->info.map_name);
 
			DrawStringMultiCenter(365, 54, STR_02BD, 2); // map name
 

	
 
			SetDParam(0, _selected_item->info.clients_on);
 
			SetDParam(1, _selected_item->info.clients_max);
 
			SetDParam(0, sel->info.clients_on);
 
			SetDParam(1, sel->info.clients_max);
 
			DrawString(260, y, STR_NETWORK_CLIENTS, 2); // clients on the server / maximum slots
 
			y+=10;
 

	
 
			SetDParam(0, STR_NETWORK_LANG_ANY+_selected_item->info.server_lang);
 
			DrawString(260, y, STR_NETWORK_LANGUAGE, 2); // server language
 
			if (sel->info.server_lang < NETWORK_NUM_LANGUAGES) {
 
				SetDParam(0, STR_NETWORK_LANG_ANY+sel->info.server_lang);
 
				DrawString(260, y, STR_NETWORK_LANGUAGE, 2); // server language
 
			}
 
			y+=10;
 

	
 
			SetDParam(0, STR_TEMPERATE_LANDSCAPE+_selected_item->info.map_set);
 
			DrawString(260, y, STR_NETWORK_TILESET, 2); // tileset
 
			if (sel->info.map_set < NUM_LANDSCAPE ) {
 
				SetDParam(0, STR_TEMPERATE_LANDSCAPE+sel->info.map_set);
 
				DrawString(260, y, STR_NETWORK_TILESET, 2); // tileset
 
			}
 
			y+=10;
 

	
 
			SetDParam(0, _selected_item->info.map_width);
 
			SetDParam(1, _selected_item->info.map_height);
 
			SetDParam(0, sel->info.map_width);
 
			SetDParam(1, sel->info.map_height);
 
			DrawString(260, y, STR_NETWORK_MAP_SIZE, 2); // map size
 
			y+=10;
 

	
 
			SetDParamStr(0, _selected_item->info.server_revision);
 
			SetDParamStr(0, sel->info.server_revision);
 
			DrawString(260, y, STR_NETWORK_SERVER_VERSION, 2); // server version
 
			y+=10;
 

	
 
			SetDParamStr(0, _selected_item->info.hostname);
 
			SetDParam(1, _selected_item->port);
 
			SetDParamStr(0, sel->info.hostname);
 
			SetDParam(1, sel->port);
 
			DrawString(260, y, STR_NETWORK_SERVER_ADDRESS, 2); // server address
 
			y+=10;
 

	
 
			SetDParam(0, _selected_item->info.start_date);
 
			SetDParam(0, sel->info.start_date);
 
			DrawString(260, y, STR_NETWORK_START_DATE, 2); // start date
 
			y+=10;
 

	
 
			SetDParam(0, _selected_item->info.game_date);
 
			SetDParam(0, sel->info.game_date);
 
			DrawString(260, y, STR_NETWORK_CURRENT_DATE, 2); // current date
 
			y+=10;
 

	
 
			y+=2;
 

	
 
			if (strncmp(_selected_item->info.server_revision, _openttd_revision, NETWORK_REVISION_LENGTH - 1) != 0) {
 
				if (strncmp(_selected_item->info.server_revision, NOREV_STRING, sizeof(_selected_item->info.server_revision)) != 0)
 
			if (strncmp(sel->info.server_revision, _openttd_revision, NETWORK_REVISION_LENGTH - 1) != 0) {
 
				if (strncmp(sel->info.server_revision, NOREV_STRING, sizeof(sel->info.server_revision)) != 0)
 
					DrawStringMultiCenter(365, y, STR_NETWORK_VERSION_MISMATCH, 2); // server mismatch
 
			} else if (_selected_item->info.clients_on == _selected_item->info.clients_max) {
 
			} else if (sel->info.clients_on == sel->info.clients_max) {
 
				// Show: server full, when clients_on == clients_max
 
				DrawStringMultiCenter(365, y, STR_NETWORK_SERVER_FULL, 2); // server full
 
			} else if (_selected_item->info.use_password)
 
			} else if (sel->info.use_password)
 
				DrawStringMultiCenter(365, y, STR_NETWORK_PASSWORD, 2); // password warning
 

	
 
			y+=10;
 
		}
 
	}	break;
 

	
 
	case WE_CLICK:
 
		_selected_field = e->click.widget;
 
		switch(e->click.widget) {
 
		case 0: case 14: /* Close 'X' | Cancel button */
 
			DeleteWindowById(WC_NETWORK_WINDOW, 0);
 
			break;
0 comments (0 inline, 0 general)