OpenTTD Source  12.0-beta2
network_gamelist.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
13 #include "../stdafx.h"
14 #include "../debug.h"
15 #include "../window_func.h"
16 #include "network_internal.h"
17 #include "network_udp.h"
18 #include "network_gamelist.h"
19 #include <atomic>
20 
21 #include "../safeguards.h"
22 
25 
32 NetworkGameList *NetworkGameListAddItem(const std::string &connection_string)
33 {
34  NetworkGameList *item, *prev_item;
35 
36  /* Parse the connection string to ensure the default port is there. */
37  const std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT).connection_string;
38 
39  prev_item = nullptr;
40  for (item = _network_game_list; item != nullptr; item = item->next) {
41  if (item->connection_string == resolved_connection_string) return item;
42  prev_item = item;
43  }
44 
45  item = new NetworkGameList(resolved_connection_string);
47 
48  if (prev_item == nullptr) {
49  _network_game_list = item;
50  } else {
51  prev_item->next = item;
52  }
53 
55 
56  return item;
57 }
58 
64 {
65  NetworkGameList *prev_item = nullptr;
66  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
67  if (remove == item) {
68  if (prev_item == nullptr) {
69  _network_game_list = remove->next;
70  } else {
71  prev_item->next = remove->next;
72  }
73 
74  /* Remove GRFConfig information */
76  delete remove;
77 
78  NetworkRebuildHostList();
80  return;
81  }
82  prev_item = item;
83  }
84 }
85 
92 {
93  NetworkGameList **prev_item = &_network_game_list;
94 
95  for (NetworkGameList *item = _network_game_list; item != nullptr;) {
96  if (!item->manually && item->version < _network_game_list_version) {
97  NetworkGameList *remove = item;
98  item = item->next;
99  *prev_item = item;
100 
101  /* Remove GRFConfig information */
103  delete remove;
104  } else {
105  prev_item = &item->next;
106  item = item->next;
107  }
108  }
109 
111 }
112 
118 {
119  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
120  /* Reset compatibility state */
121  item->info.compatible = item->info.version_compatible;
122 
123  for (GRFConfig *c = item->info.grfconfig; c != nullptr; c = c->next) {
124  assert(HasBit(c->flags, GCF_COPY));
125 
126  const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
127  if (f == nullptr) {
128  /* Don't know the GRF (anymore), so mark game incompatible. */
129  c->status = GCS_NOT_FOUND;
130 
131  /* If we miss a file, we're obviously incompatible. */
132  item->info.compatible = false;
133  } else {
134  c->filename = f->filename;
135  c->name = f->name;
136  c->info = f->info;
137  c->status = GCS_UNKNOWN;
138  }
139  }
140  }
141 
143 }
NetworkGameList::connection_string
std::string connection_string
Address of the server.
Definition: network_gamelist.h:22
GRFConfig::info
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Definition: newgrf_config.h:167
NETWORK_DEFAULT_PORT
static const uint16 NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:28
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:736
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:400
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
GCS_NOT_FOUND
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:37
GCF_COPY
@ GCF_COPY
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:27
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:66
NetworkGameListAddItem
NetworkGameList * NetworkGameListAddItem(const std::string &connection_string)
Add a new item to the linked gamelist.
Definition: network_gamelist.cpp:32
ServerAddress::Parse
static ServerAddress Parse(const std::string &connection_string, uint16 default_port, CompanyID *company_id=nullptr)
Convert a string containing either "hostname", "hostname:port" or invite code to a ServerAddress,...
Definition: address.cpp:454
NetworkAfterNewGRFScan
void NetworkAfterNewGRFScan()
Rebuild the GRFConfig's of the servers in the game list as we did a rescan and might have found new N...
Definition: network_gamelist.cpp:117
_network_game_list
NetworkGameList * _network_game_list
Game list of this client.
Definition: network_gamelist.cpp:23
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:155
NetworkGameList::version
int version
Used to see which servers are no longer available on the Game Coordinator and can be removed.
Definition: network_gamelist.h:26
NetworkGameListRemoveExpired
void NetworkGameListRemoveExpired()
Remove all servers that have not recently been updated.
Definition: network_gamelist.cpp:91
GCS_UNKNOWN
@ GCS_UNKNOWN
The status of this grf file is unknown.
Definition: newgrf_config.h:35
GRFConfig::name
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
Definition: newgrf_config.h:166
network_udp.h
WC_NETWORK_WINDOW
@ WC_NETWORK_WINDOW
Network window; Window numbers:
Definition: window_type.h:464
InvalidateWindowClassesData
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3235
network_internal.h
NetworkGameList
Structure with information shown in the game list (GUI)
Definition: network_gamelist.h:18
NetworkGameList::next
NetworkGameList * next
Next pointer to make a linked game list.
Definition: network_gamelist.h:27
network_gamelist.h
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:199
GRFConfig::filename
char * filename
Filename - either with or without full path.
Definition: newgrf_config.h:165
NetworkGameList::info
NetworkGameInfo info
The game information of this server.
Definition: network_gamelist.h:21
_network_game_list_version
int _network_game_list_version
Current version of all items in the list.
Definition: network_gamelist.cpp:24
NetworkGameListRemoveItem
void NetworkGameListRemoveItem(NetworkGameList *remove)
Remove an item from the gamelist linked list.
Definition: network_gamelist.cpp:63
ServerAddress::connection_string
std::string connection_string
The connection string for this ServerAddress.
Definition: address.h:212
NetworkServerGameInfo::grfconfig
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game_info.h:94