(svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
authordominik
Sat, 18 Dec 2004 18:58:03 +0000
changeset 710 e8402270679f
parent 709 842c7ca9766d
child 711 11819c4ef994
(svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
network.c
network.h
network_gui.c
settings.c
--- a/network.c	Sat Dec 18 18:19:49 2004 +0000
+++ b/network.c	Sat Dec 18 18:58:03 2004 +0000
@@ -707,6 +707,7 @@
 void NetworkInitialize(void)
 {
 	ClientState *cs;
+	uint i;
 
 	_local_command_queue = NULL;
 
@@ -730,6 +731,12 @@
 	InitPlayerRandoms();
 
 	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;
+		AddServer(_network_server_list[i]);
+	}
 }
 
 // Query a server to fetch his game-info
@@ -766,6 +773,27 @@
 	NetworkDisconnect();
 }
 
+// validates an address entered as a string and adds the server to
+// the list
+void AddServer(byte *b)
+{
+	if (*b != '\0') {
+		const byte *port = NULL;
+		const byte *player = NULL;
+		uint16 rport;
+
+		ttd_strlcpy(_network_default_ip, b, lengthof(_network_default_ip));
+		rport = NETWORK_DEFAULT_PORT;
+
+		ParseConnectionString(&player, &port, b);
+
+		if (player != NULL) _network_playas = atoi(player);
+		if (port != NULL) rport = atoi(port);
+
+		NetworkQueryServer(b, rport, true);
+	}
+}
+
 // Used by clients, to connect to a server
 bool NetworkClientConnectGame(const byte* host, unsigned short port)
 {
--- a/network.h	Sat Dec 18 18:19:49 2004 +0000
+++ b/network.h	Sat Dec 18 18:58:03 2004 +0000
@@ -131,6 +131,8 @@
 
 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 uint16 _network_own_client_index;
 VARDEF char _network_unique_id[NETWORK_NAME_LENGTH]; // Our own unique ID
@@ -191,5 +193,6 @@
 
 void ParseConnectionString(const byte **player, const byte **port, byte *connection_string);
 void NetworkUpdateClientInfo(uint16 client_index);
+void AddServer(byte *b);
 
 #endif /* NETWORK_H */
--- a/network_gui.c	Sat Dec 18 18:19:49 2004 +0000
+++ b/network_gui.c	Sat Dec 18 18:58:03 2004 +0000
@@ -317,22 +317,7 @@
 		break;
 
 	case WE_ON_EDIT_TEXT: {
-		byte *b = e->edittext.str;
-		if (*b != 0) {
-			const byte *port = NULL;
-			const byte *player = NULL;
-			uint16 rport;
-
-			ttd_strlcpy(_network_default_ip, b, lengthof(_network_default_ip));
-			rport = NETWORK_DEFAULT_PORT;
-
-			ParseConnectionString(&player, &port, b);
-
-			if (player != NULL) _network_playas = atoi(player);
-			if (port != NULL) rport = atoi(port);
-
-			NetworkQueryServer(b, rport, true);
-		}
+		AddServer(e->edittext.str);
 	} break;
 
 	case WE_CREATE: {
@@ -387,7 +372,6 @@
 	Window *w;
 	DeleteWindowById(WC_NETWORK_WINDOW, 0);
 
-//	NetworkLobbyInit();
 	w = AllocateWindowDesc(&_network_game_window_desc);
 	ttd_strlcpy(_edit_str_buf, _network_player_name, MAX_QUERYSTR_LEN);
 	w->vscroll.cap = 8;
--- a/settings.c	Sat Dec 18 18:19:49 2004 +0000
+++ b/settings.c	Sat Dec 18 18:58:03 2004 +0000
@@ -122,7 +122,7 @@
 	IniGroup *grp = pool_alloc(&ini->pool, sizeof(IniGroup));
 	grp->ini = ini;
 	grp->name = pool_strdup(&ini->pool, grpt, len);
-	if(!strcmp(grp->name, "newgrf"))
+	if(!strcmp(grp->name, "newgrf") || !strcmp(grp->name, "servers") )
 		grp->type = IGT_LIST;
 	else
 		grp->type = IGT_VARIABLES;
@@ -906,20 +906,19 @@
 	proc(ini, debug_settings,		"debug");
 }
 
-static void LoadGrfSettings(IniFile *ini)
+// loads all items from a *grpname section into the **list
+static void LoadList(IniFile *ini, const char *grpname, char **list, int len)
 {
-	IniGroup *group = ini_getgroup(ini, "newgrf", -1);
+	IniGroup *group = ini_getgroup(ini, grpname, -1);
 	IniItem *item;
 	int i;
 
 	if (!group)
 		return;
-
 	item = group->item;
-	for(i=0; i!=lengthof(_newgrf_files); i++) {
-		if (!item)
-			break;
-		_newgrf_files[i] = strdup(item->value);
+	for ( i=0; i != len; i++) {
+		if (item == NULL) break;
+		list[i] = strdup(item->value);
 		item = item->next;
 	}
 }
@@ -928,7 +927,8 @@
 {
 	IniFile *ini = ini_load(_config_file);
 	HandleSettingDescs(ini, load_setting_desc);
-	LoadGrfSettings(ini);
+	LoadList(ini, "newgrf", _newgrf_files, lengthof(_newgrf_files));
+	LoadList(ini, "servers", _network_server_list, lengthof(_network_server_list));
 	ini_free(ini);
 }