(svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
authordominik
Mon, 20 Dec 2004 22:14:39 +0000
changeset 738 0b2fb79e64fc
parent 737 ceb3dce50a37
child 739 ab2b478cd962
(svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
functions.h
network.c
network.h
network_gamelist.c
network_gamelist.h
network_gui.c
network_udp.c
network_udp.h
settings.c
--- a/functions.h	Mon Dec 20 18:11:22 2004 +0000
+++ b/functions.h	Mon Dec 20 22:14:39 2004 +0000
@@ -160,7 +160,6 @@
 void NetworkUDPGameLoop(void);
 bool NetworkServerStart(void);
 bool NetworkClientConnectGame(const byte* host, unsigned short port);
-void NetworkQueryServer(const byte* host, unsigned short port, bool game_info);
 void NetworkReboot();
 void NetworkDisconnect();
 void NetworkSend_Command(uint32 tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback);
--- a/network.c	Mon Dec 20 18:11:22 2004 +0000
+++ b/network.c	Mon Dec 20 22:14:39 2004 +0000
@@ -774,24 +774,23 @@
 	NetworkUDPInitialize();
 
 	// add all servers from the config file to our list
-	for (i=0; i != lengthof(_network_server_list); i++) {
-		if (_network_server_list[i] == NULL) break;
-		NetworkAddServer(_network_server_list[i]);
+	for (i=0; i != lengthof(_network_host_list); i++) {
+		if (_network_host_list[i] == NULL) break;
+		NetworkAddServer(_network_host_list[i]);
 	}
 }
 
 // Query a server to fetch his game-info
 //  If game_info is true, only the gameinfo is fetched,
 //   else only the client_info is fetched
-void NetworkQueryServer(const byte* host, unsigned short port, bool game_info)
+NetworkGameList *NetworkQueryServer(const byte* host, unsigned short port, bool game_info)
 {
-	if (!_network_available) return;
+	if (!_network_available) return NULL;
 
 	NetworkDisconnect();
 
 	if (game_info) {
-		NetworkUDPQueryServer(host, port);
-		return;
+		return NetworkUDPQueryServer(host, port);
 	}
 
 	NetworkInitialize();
@@ -807,18 +806,22 @@
 	// We are connected
 	if (_networking) {
 		SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)();
-		return;
+		return NULL;
 	}
 
 	// No networking, close everything down again
 	NetworkDisconnect();
+	return NULL;
 }
 
-// validates an address entered as a string and adds the server to
-// the list
+/* Validates an address entered as a string and adds the server to
+ * the list. If you use this functions, the games will be marked
+ * as manually added. */
 void NetworkAddServer(const byte *b)
 {
 	if (*b != '\0') {
+		NetworkGameList *item;
+		uint i;
 		const byte *port = NULL;
 		const byte *player = NULL;
 		byte host[NETWORK_HOSTNAME_LENGTH];
@@ -834,7 +837,26 @@
 		if (player != NULL) _network_playas = atoi(player);
 		if (port != NULL) rport = atoi(port);
 
-		NetworkQueryServer(host, rport, true);
+		item = NetworkQueryServer(host, rport, true);
+		item->manually = true;
+	}
+}
+
+/* Generates the list of manually added hosts from NetworkGameList and 
+ * dumps them into the array _network_host_list. This array is needed
+ * by the function that generates the config file. */
+void NetworkRebuildHostList()
+{
+	int i=0;
+	NetworkGameList *item = _network_game_list;
+	while (item != NULL && i!=lengthof(_network_host_list)) {
+		if (item->manually)
+			_network_host_list[i++] = strdup(item->info.hostname);
+		item = item->next;
+	}
+
+	for (; i<lengthof(_network_host_list); i++) {
+		_network_host_list[i] = strdup("");
 	}
 }
 
--- a/network.h	Mon Dec 20 18:11:22 2004 +0000
+++ b/network.h	Mon Dec 20 22:14:39 2004 +0000
@@ -101,6 +101,7 @@
 	uint32 ip;
 	uint16 port;
 	bool online;																		// False if the server did not respond (default status)
+	bool manually;																	// True if the server was added manually
 	struct NetworkGameList *next;
 } NetworkGameList;
 
@@ -132,7 +133,7 @@
 VARDEF char _network_player_name[NETWORK_NAME_LENGTH];
 VARDEF char _network_default_ip[NETWORK_HOSTNAME_LENGTH];
 #define MAX_SAVED_SERVERS 10
-VARDEF char *_network_server_list[MAX_SAVED_SERVERS];
+VARDEF char *_network_host_list[MAX_SAVED_SERVERS];
 
 VARDEF uint16 _network_own_client_index;
 VARDEF char _network_unique_id[NETWORK_NAME_LENGTH]; // Our own unique ID
@@ -194,5 +195,7 @@
 void ParseConnectionString(const byte **player, const byte **port, byte *connection_string);
 void NetworkUpdateClientInfo(uint16 client_index);
 void NetworkAddServer(const byte *b);
+void NetworkRebuildHostList();
+NetworkGameList *NetworkQueryServer(const byte* host, unsigned short port, bool game_info);
 
 #endif /* NETWORK_H */
--- a/network_gamelist.c	Mon Dec 20 18:11:22 2004 +0000
+++ b/network_gamelist.c	Mon Dec 20 22:14:39 2004 +0000
@@ -65,6 +65,33 @@
 	return item;
 }
 
+void NetworkGameListRemoveItem(NetworkGameList *remove)
+{
+	NetworkGameList *item;
+
+	item = _network_game_list;
+
+	// examine head of the list
+	if ( remove == _network_game_list ) {
+		_network_game_list = remove->next;
+		free(remove);
+		DEBUG(net, 4) ("[NET][GameList] Removed server from list");
+		return;
+	}
+
+	// examine each item
+	while ( item->next != NULL ) {
+		if ( item->next == remove )
+		{
+			item->next = remove->next;
+			free(remove);
+			DEBUG(net, 4) ("[NET][GameList] Removed server from list");
+			return;
+		}
+		item = item->next;
+	}
+}
+
 void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online)
 {
 	// We queried a server and now we are going to add it to the list
--- a/network_gamelist.h	Mon Dec 20 18:11:22 2004 +0000
+++ b/network_gamelist.h	Mon Dec 20 22:14:39 2004 +0000
@@ -3,6 +3,7 @@
 
 void NetworkGameListClear(void);
 NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port);
+void NetworkGameListRemoveItem(NetworkGameList *remove);
 void NetworkGameListAddQueriedItem(const NetworkGameInfo *info, bool server_online);
 
 #endif /* NETWORK_GAMELIST_H */
--- a/network_gui.c	Mon Dec 20 18:11:22 2004 +0000
+++ b/network_gui.c	Mon Dec 20 22:14:39 2004 +0000
@@ -9,6 +9,7 @@
 
 #include "table/strings.h"
 #include "network_data.h"
+#include "network_gamelist.h"
 #include "window.h"
 #include "gui.h"
 #include "gfx.h"
@@ -299,13 +300,15 @@
 		break;
 
 	case WE_KEYPRESS:
-		if (_selected_field != 3)
-			break;
-
-		switch (HandleEditBoxKey(w, 3, e)) {
-		case 1:
-			HandleButtonClick(w, 8);
-			break;
+		if (_selected_field != 3) {
+			if ( e->keypress.keycode == WKC_DELETE ) { // press 'delete' to remove servers
+				if (_selected_item != NULL && _selected_item->manually) {
+					NetworkGameListRemoveItem(_selected_item);
+					NetworkRebuildHostList();
+					SetWindowDirty(w);
+					_selected_item = NULL;
+				}
+			}
 		}
 
 		// The name is only allowed when it starts with a letter!
@@ -318,6 +321,7 @@
 
 	case WE_ON_EDIT_TEXT: {
 		NetworkAddServer(e->edittext.str);
+		NetworkRebuildHostList();
 	} break;
 
 	case WE_CREATE: {
--- a/network_udp.c	Mon Dec 20 18:11:22 2004 +0000
+++ b/network_udp.c	Mon Dec 20 22:14:39 2004 +0000
@@ -434,7 +434,7 @@
 	_network_udp_broadcast = 300; // Stay searching for 300 ticks
 }
 
-void NetworkUDPQueryServer(const byte* host, unsigned short port)
+NetworkGameList *NetworkUDPQueryServer(const byte* host, unsigned short port)
 {
 	struct sockaddr_in out_addr;
 	Packet *p;
@@ -444,7 +444,7 @@
 	// No UDP-socket yet..
 	if (_udp_client_socket == INVALID_SOCKET)
 		if (!NetworkUDPListen(0, 0))
-			return;
+			return NULL;
 
 	ttd_strlcpy(hostname, host, sizeof(hostname));
 
@@ -467,6 +467,7 @@
 	free(p);
 
 	UpdateNetworkGameWindow(false);
+	return item;
 }
 
 /* Register us to the master server
--- a/network_udp.h	Mon Dec 20 18:11:22 2004 +0000
+++ b/network_udp.h	Mon Dec 20 22:14:39 2004 +0000
@@ -5,7 +5,7 @@
 bool NetworkUDPListen(uint32 host, uint16 port);
 void NetworkUDPReceive(void);
 void NetworkUDPSearchGame(void);
-void NetworkUDPQueryServer(const byte* host, unsigned short port);
+NetworkGameList *NetworkUDPQueryServer(const byte* host, unsigned short port);
 void NetworkUDPAdvertise(void);
 
 #endif /* NETWORK_LAN_H */
--- a/settings.c	Mon Dec 20 18:11:22 2004 +0000
+++ b/settings.c	Mon Dec 20 22:14:39 2004 +0000
@@ -923,12 +923,37 @@
 	}
 }
 
+static void SaveList(IniFile *ini, const char *grpname, char **list, int len)
+{
+	IniGroup *group = ini_getgroup(ini, grpname, -1);
+	IniItem *item;
+	int i;
+	bool first = true;
+
+	if (!group)
+		return;
+	for ( i=0; i != len; i++) {
+		if ( list[i] == '\0' ) continue;
+
+		if (first) { // add first item to the head of the group
+			item = ini_item_alloc(group, list[i], strlen(list[i]));
+			item->value = item->name;
+			group->item = item;
+			first = false;
+		} else { // all other items are attached to the previous one
+			item->next = ini_item_alloc(group, list[i], strlen(list[i]));
+			item = item->next;
+			item->value = item->name;
+		}
+	}
+}
+
 void LoadFromConfig()
 {
 	IniFile *ini = ini_load(_config_file);
 	HandleSettingDescs(ini, load_setting_desc);
 	LoadList(ini, "newgrf", _newgrf_files, lengthof(_newgrf_files));
-	LoadList(ini, "servers", _network_server_list, lengthof(_network_server_list));
+	LoadList(ini, "servers", _network_host_list, lengthof(_network_host_list));
 	ini_free(ini);
 }
 
@@ -936,6 +961,7 @@
 {
 	IniFile *ini = ini_load(_config_file);
 	HandleSettingDescs(ini, save_setting_desc);
+	SaveList(ini, "servers", _network_host_list, lengthof(_network_host_list));
 	ini_save(_config_file, ini);
 	ini_free(ini);
 }