added saving of favorites

This commit is contained in:
Magnus Auvinen 2008-09-03 21:02:30 +00:00
parent 53da3f0d40
commit 171ca682d3
4 changed files with 119 additions and 0 deletions

View file

@ -528,6 +528,88 @@ int net_host_lookup(const char *hostname, NETADDR *addr, int types)
return 0;
}
static int parse_int(int *out, const char **str)
{
int i = 0;
*out = 0;
if(**str < '0' || **str > '9')
return -1;
i = **str - '0';
(*str)++;
while(1)
{
if(**str < '0' || **str > '9')
{
*out = i;
return 0;
}
i = (i*10) + (**str - '0');
(*str)++;
}
return 0;
}
static int parse_char(char c, const char **str)
{
if(**str != c) return -1;
(*str)++;
return 0;
}
static int parse_uint8(unsigned char *out, const char **str)
{
int i;
if(parse_int(&i, str) != 0) return -1;
if(i < 0 || i > 0xff) return -1;
*out = i;
return 0;
}
static int parse_uint16(unsigned short *out, const char **str)
{
int i;
if(parse_int(&i, str) != 0) return -1;
if(i < 0 || i > 0xffff) return -1;
*out = i;
return 0;
}
int net_addr_from_str(NETADDR *addr, const char *string)
{
const char *str = string;
mem_zero(addr, sizeof(NETADDR));
if(str[0] == '[')
{
/* TODO: ipv6 */
}
else
{
/* ipv4 */
if(parse_uint8(&addr->ip[0], &str)) return -1;
if(parse_char('.', &str)) return -1;
if(parse_uint8(&addr->ip[1], &str)) return -1;
if(parse_char('.', &str)) return -1;
if(parse_uint8(&addr->ip[2], &str)) return -1;
if(parse_char('.', &str)) return -1;
if(parse_uint8(&addr->ip[3], &str)) return -1;
if(*str == ':')
{
str++;
if(parse_uint16(&addr->port, &str)) return -1;
}
addr->type = NETTYPE_IPV4;
}
return 0;
}
NETSOCKET net_udp_create(NETADDR bindaddr)
{
/* TODO: IPv6 support */

View file

@ -480,6 +480,19 @@ int net_addr_comp(const NETADDR *a, const NETADDR *b);
*/
void net_addr_str(const NETADDR *addr, char *string, int max_length);
/*
Function: net_addr_from_str
Turns string into a network address.
Returns:
0 on success
Parameters:
addr - Address to fill in.
string - String to parse.
*/
int net_addr_from_str(NETADDR *addr, const char *string);
/* Group: Network UDP */
/*

View file

@ -44,6 +44,7 @@ NETCLIENT *net;
/* TODO: ugly, fix me */
extern void client_serverbrowse_set(NETADDR *addr, int request, int token, SERVER_INFO *info);
extern void client_serverbrowse_save();
static int snapshot_part;
static int64 local_start_time;
@ -1496,6 +1497,13 @@ static void con_rcon(void *result, void *user_data)
client_rcon(console_arg_string(result, 0));
}
static void con_addfavorite(void *result, void *user_data)
{
NETADDR addr;
if(net_addr_from_str(&addr, console_arg_string(result, 0)) == 0)
client_serverbrowse_addfavorite(addr);
}
static void client_register_commands()
{
MACRO_REGISTER_COMMAND("quit", "", con_quit, 0x0);
@ -1504,6 +1512,8 @@ static void client_register_commands()
MACRO_REGISTER_COMMAND("ping", "", con_ping, 0x0);
MACRO_REGISTER_COMMAND("screenshot", "", con_screenshot, 0x0);
MACRO_REGISTER_COMMAND("rcon", "r", con_rcon, 0x0);
MACRO_REGISTER_COMMAND("add_favorite", "s", con_addfavorite, 0x0);
}
void client_save_line(const char *line)
@ -1552,6 +1562,7 @@ int main(int argc, char **argv)
if(engine_config_write_start() == 0)
{
config_save();
client_serverbrowse_save();
modc_save_config();
engine_config_write_stop();
}

View file

@ -589,3 +589,16 @@ void client_serverbrowse_removefavorite(NETADDR addr)
}
}
}
void client_serverbrowse_save()
{
int i;
char addrstr[128];
char buffer[256];
for(i = 0; i < num_favorite_servers; i++)
{
net_addr_str(&favorite_servers[i], addrstr, sizeof(addrstr));
str_format(buffer, sizeof(buffer), "add_favorite %s", addrstr);
engine_config_write_line(buffer);
}
}