(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.

This commit is contained in:
dominik 2004-12-18 18:58:03 +00:00
parent b9779176d5
commit 853c11d301
4 changed files with 41 additions and 26 deletions

View File

@ -707,6 +707,7 @@ void NetworkClose(void)
void NetworkInitialize(void)
{
ClientState *cs;
uint i;
_local_command_queue = NULL;
@ -730,6 +731,12 @@ void NetworkInitialize(void)
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 @@ void NetworkQueryServer(const byte* host, unsigned short port, bool game_info)
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)
{

View File

@ -131,6 +131,8 @@ VARDEF NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
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 @@ VARDEF byte _network_playas; // an id to play as..
void ParseConnectionString(const byte **player, const byte **port, byte *connection_string);
void NetworkUpdateClientInfo(uint16 client_index);
void AddServer(byte *b);
#endif /* NETWORK_H */

View File

@ -317,22 +317,7 @@ static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
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 @@ void ShowNetworkGameWindow()
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;

View File

@ -122,7 +122,7 @@ static IniGroup *ini_group_alloc(IniFile *ini, const char *grpt, int len)
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 @@ static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc)
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 @@ void LoadFromConfig()
{
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);
}