diff options
-rw-r--r-- | src/console_cmds.cpp | 18 | ||||
-rw-r--r-- | src/core/smallvec_type.hpp | 2 | ||||
-rw-r--r-- | src/network/network.cpp | 31 | ||||
-rw-r--r-- | src/network/network_func.h | 5 | ||||
-rw-r--r-- | src/network/network_gamelist.cpp | 2 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 8 | ||||
-rw-r--r-- | src/network/network_server.cpp | 7 | ||||
-rw-r--r-- | src/settings.cpp | 46 |
8 files changed, 41 insertions, 78 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index bd19fe425..2c63e5fdc 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -422,7 +422,6 @@ DEF_CONSOLE_CMD(ConBan) DEF_CONSOLE_CMD(ConUnBan) { - uint i, index; if (argc == 0) { IConsoleHelp("Unban a client from a network game. Usage: 'unban <ip | client-id>'"); @@ -432,15 +431,14 @@ DEF_CONSOLE_CMD(ConUnBan) if (argc != 2) return false; - index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0; + uint index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0; index--; + uint i = 0; - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] == NULL) continue; - + for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) { if (strcmp(_network_ban_list[i], argv[1]) == 0 || index == i) { free(_network_ban_list[i]); - _network_ban_list[i] = NULL; + _network_ban_list.Erase(iter); IConsolePrint(CC_DEFAULT, "IP unbanned."); return true; } @@ -452,8 +450,6 @@ DEF_CONSOLE_CMD(ConUnBan) DEF_CONSOLE_CMD(ConBanList) { - uint i; - if (argc == 0) { IConsoleHelp("List the IP's of banned clients: Usage 'banlist'"); return true; @@ -461,9 +457,9 @@ DEF_CONSOLE_CMD(ConBanList) IConsolePrint(CC_DEFAULT, "Banlist: "); - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] != NULL) - IConsolePrintF(CC_DEFAULT, " %d) %s", i + 1, _network_ban_list[i]); + uint i = 1; + for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++, i++) { + IConsolePrintF(CC_DEFAULT, " %d) %s", i, *iter); } return true; diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp index 69625a2e0..ae3a827a7 100644 --- a/src/core/smallvec_type.hpp +++ b/src/core/smallvec_type.hpp @@ -267,4 +267,6 @@ public: } }; +typedef AutoFreeSmallVector<char*, 4> StringList; + #endif /* SMALLVEC_TYPE_HPP */ diff --git a/src/network/network.cpp b/src/network/network.cpp index 3bbe7b898..06bd53ee9 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -55,8 +55,8 @@ ClientID _redirect_console_to_client; bool _network_need_advertise; uint32 _network_last_advertise_frame; uint8 _network_reconnect; -char *_network_host_list[10]; -char *_network_ban_list[25]; +StringList _network_host_list; +StringList _network_ban_list; uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode uint32 _frame_counter_max; // To where we may go with our clients uint32 _frame_counter; @@ -492,11 +492,9 @@ static void NetworkAcceptClients() /* Check if the client is banned */ banned = false; - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] == NULL) continue; - + for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++) { /* Check for CIDR separator */ - char *chr_cidr = strchr(_network_ban_list[i], '/'); + char *chr_cidr = strchr(*iter, '/'); if (chr_cidr != NULL) { int cidr = atoi(chr_cidr + 1); @@ -505,7 +503,7 @@ static void NetworkAcceptClients() /* Remove and then replace the / so that inet_addr() works on the IP portion */ *chr_cidr = '\0'; - uint32 ban_ip = inet_addr(_network_ban_list[i]); + uint32 ban_ip = inet_addr(*iter); *chr_cidr = '/'; /* Convert CIDR to mask in network format */ @@ -513,14 +511,14 @@ static void NetworkAcceptClients() if ((sin.sin_addr.s_addr & mask) == (ban_ip & mask)) banned = true; } else { /* No CIDR used, so just perform a simple IP test */ - if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) banned = true; + if (sin.sin_addr.s_addr == inet_addr(*iter)) banned = true; } if (banned) { Packet p(PACKET_SERVER_BANNED); p.PrepareToSend(); - DEBUG(net, 1, "Banned ip tried to join (%s), refused", _network_ban_list[i]); + DEBUG(net, 1, "Banned ip tried to join (%s), refused", *iter); send(s, (const char*)p.buffer, p.size, 0); closesocket(s); @@ -690,19 +688,10 @@ void NetworkAddServer(const char *b) * by the function that generates the config file. */ void NetworkRebuildHostList() { - uint i = 0; - const NetworkGameList *item = _network_game_list; - while (item != NULL && i != lengthof(_network_host_list)) { - if (item->manually) { - free(_network_host_list[i]); - _network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->address.GetPort()); - } - item = item->next; - } + _network_host_list.Clear(); - for (; i < lengthof(_network_host_list); i++) { - free(_network_host_list[i]); - _network_host_list[i] = NULL; + for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) { + if (item->manually) *_network_host_list.Append() = strdup(item->address.GetAddressAsString()); } } diff --git a/src/network/network_func.h b/src/network/network_func.h index 9fdcfa2d5..552abba74 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -11,6 +11,7 @@ #include "network_type.h" #include "../console_type.h" #include "../gfx_type.h" +#include "../core/smallvec_type.hpp" extern NetworkServerGameInfo _network_game_info; extern NetworkCompanyState *_network_company_states; @@ -20,8 +21,8 @@ extern ClientID _redirect_console_to_client; extern bool _network_need_advertise; extern uint32 _network_last_advertise_frame; extern uint8 _network_reconnect; -extern char *_network_host_list[10]; -extern char *_network_ban_list[25]; +extern StringList _network_host_list; +extern StringList _network_ban_list; byte NetworkSpectatorCount(); void NetworkUpdateClientName(); diff --git a/src/network/network_gamelist.cpp b/src/network/network_gamelist.cpp index 3f1db60b3..fc08437f8 100644 --- a/src/network/network_gamelist.cpp +++ b/src/network/network_gamelist.cpp @@ -50,7 +50,7 @@ static void NetworkGameListHandleDelayedInsert() strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname)); item->online = false; } - item->manually = ins_item->manually; + item->manually |= ins_item->manually; UpdateNetworkGameWindow(false); } free(ins_item); diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 3b85b8a03..1f9c756eb 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -951,13 +951,11 @@ void ShowNetworkGameWindow() /* Only show once */ if (first) { - char * const *srv; - first = false; /* add all servers from the config file to our list */ - for (srv = &_network_host_list[0]; srv != endof(_network_host_list) && *srv != NULL; srv++) { - NetworkAddServer(*srv); - } + for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) { + NetworkAddServer(*iter); + } } new NetworkGameWindow(&_network_game_window_desc); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 16695c46b..6bf95e2f9 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1798,12 +1798,7 @@ void NetworkServerBanIP(const char *banip) } /* Add user to ban-list */ - for (uint index = 0; index < lengthof(_network_ban_list); index++) { - if (_network_ban_list[index] == NULL) { - _network_ban_list[index] = strdup(banip); - break; - } - } + *_network_ban_list.Append() = strdup(banip); } bool NetworkCompanyHasClients(CompanyID company) diff --git a/src/settings.cpp b/src/settings.cpp index 580efd25f..63e099e3a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -68,9 +68,8 @@ ClientSettings _settings_client; GameSettings _settings_game; GameSettings _settings_newgame; -typedef const char *SettingListCallbackProc(const IniItem *item, uint index); typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object); -typedef void SettingDescProcList(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc); +typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list); static bool IsSignedVarMemType(VarType vt); @@ -598,27 +597,18 @@ static void ini_save_settings(IniFile *ini, const SettingDesc *sd, const char *g * @param ini IniFile handle to the ini file with the source data * @param grpname character string identifying the section-header of the ini * file that will be parsed - * @param list pointer to an string(pointer) array that will store the parsed * entries of the given section - * @param len the maximum number of items available for the above list - * @param proc callback function that can override how the values are stored - * inside the list */ -static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc) + */ +static void ini_load_setting_list(IniFile *ini, const char *grpname, StringList *list) { IniGroup *group = ini->GetGroup(grpname); - IniItem *item; - const char *entry; - uint i, j; - if (group == NULL) return; + if (group == NULL || list == NULL) return; - for (i = j = 0, item = group->item; item != NULL; item = item->next) { - entry = (proc != NULL) ? proc(item, i++) : item->name; - - if (entry == NULL || list == NULL) continue; + list->Clear(); - if (j == len) break; - list[j++] = strdup(entry); + for (const IniItem *item = group->item; item != NULL; item = item->next) { + if (item->name != NULL) *list->Append() = strdup(item->name); } } @@ -629,24 +619,16 @@ static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list * @param grpname character string identifying the section-header of the ini file * @param list pointer to an string(pointer) array that will be used as the * source to be saved into the relevant ini section - * @param len the maximum number of items available for the above list - * @param proc callback function that can will provide the source data if defined */ -static void ini_save_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc) + */ +static void ini_save_setting_list(IniFile *ini, const char *grpname, StringList *list) { IniGroup *group = ini->GetGroup(grpname); - const char *entry; - uint i; - if (proc == NULL && list == NULL) return; - if (group == NULL) return; + if (group == NULL || list == NULL) return; group->Clear(); - for (i = 0; i != len; i++) { - entry = (proc != NULL) ? proc(NULL, i) : list[i]; - - if (entry == NULL || *entry == '\0') continue; - - group->GetItem(entry, true)->SetValue(""); + for (char **iter = list->Begin(); iter != list->End(); iter++) { + group->GetItem(*iter, true)->SetValue(""); } } @@ -1295,8 +1277,8 @@ static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescP proc(ini, _currency_settings,"currency", &_custom_currency); #ifdef ENABLE_NETWORK - proc_list(ini, "servers", _network_host_list, lengthof(_network_host_list), NULL); - proc_list(ini, "bans", _network_ban_list, lengthof(_network_ban_list), NULL); + proc_list(ini, "servers", &_network_host_list); + proc_list(ini, "bans", &_network_ban_list); #endif /* ENABLE_NETWORK */ } |