OpenTTD Source  20240917-master-g9ab0a47812
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);
46  item->info.gamescript_version = -1;
48 
49  if (prev_item == nullptr) {
50  _network_game_list = item;
51  } else {
52  prev_item->next = item;
53  }
54 
56 
57  return item;
58 }
59 
65 {
66  NetworkGameList *prev_item = nullptr;
67  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
68  if (remove == item) {
69  if (prev_item == nullptr) {
70  _network_game_list = remove->next;
71  } else {
72  prev_item->next = remove->next;
73  }
74 
75  /* Remove GRFConfig information */
77  delete remove;
78 
79  NetworkRebuildHostList();
81  return;
82  }
83  prev_item = item;
84  }
85 }
86 
93 {
94  NetworkGameList **prev_item = &_network_game_list;
95 
96  for (NetworkGameList *item = _network_game_list; item != nullptr;) {
97  if (!item->manually && item->version < _network_game_list_version) {
98  NetworkGameList *remove = item;
99  item = item->next;
100  *prev_item = item;
101 
102  /* Remove GRFConfig information */
104  delete remove;
105  } else {
106  prev_item = &item->next;
107  item = item->next;
108  }
109  }
110 
112 }
113 
119 {
120  for (NetworkGameList *item = _network_game_list; item != nullptr; item = item->next) {
121  /* Reset compatibility state */
122  item->info.compatible = item->info.version_compatible;
123 
124  for (GRFConfig *c = item->info.grfconfig; c != nullptr; c = c->next) {
125  assert(HasBit(c->flags, GCF_COPY));
126 
127  const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, &c->ident.md5sum);
128  if (f == nullptr) {
129  /* Don't know the GRF (anymore), so mark game incompatible. */
130  c->status = GCS_NOT_FOUND;
131 
132  /* If we miss a file, we're obviously incompatible. */
133  item->info.compatible = false;
134  } else {
135  c->filename = f->filename;
136  c->name = f->name;
137  c->info = f->info;
138  c->status = GCS_UNKNOWN;
139  }
140  }
141  }
142 
144 }
NetworkServerGameInfo::gamescript_version
int gamescript_version
Version of the gamescript.
Definition: network_game_info.h:113
NetworkGameList::connection_string
std::string connection_string
Address of the server.
Definition: network_gamelist.h:31
GRFConfig::info
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Definition: newgrf_config.h:158
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:352
GRFConfig::filename
std::string filename
Filename - either with or without full path.
Definition: newgrf_config.h:156
GCS_NOT_FOUND
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:37
ServerAddress::Parse
static ServerAddress Parse(const std::string &connection_string, uint16_t default_port, CompanyID *company_id=nullptr)
Convert a string containing either "hostname", "hostname:port" or invite code to a ServerAddress,...
Definition: address.cpp:450
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
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:118
_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:147
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:35
NETWORK_DEFAULT_PORT
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:25
NetworkGameListRemoveExpired
void NetworkGameListRemoveExpired()
Remove all servers that have not recently been updated.
Definition: network_gamelist.cpp:92
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:157
network_udp.h
WC_NETWORK_WINDOW
@ WC_NETWORK_WINDOW
Network window; Window numbers:
Definition: window_type.h:478
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:3225
network_internal.h
NetworkGameList
Structure with information shown in the game list (GUI)
Definition: network_gamelist.h:27
NetworkGameList::next
NetworkGameList * next
Next pointer to make a linked game list.
Definition: network_gamelist.h:36
network_gamelist.h
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:192
NetworkGameList::info
NetworkGameInfo info
The game information of this server.
Definition: network_gamelist.h:30
_network_game_list_version
int _network_game_list_version
Current version of all items in the list.
Definition: network_gamelist.cpp:24
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:686
NetworkGameListRemoveItem
void NetworkGameListRemoveItem(NetworkGameList *remove)
Remove an item from the gamelist linked list.
Definition: network_gamelist.cpp:64
ServerAddress::connection_string
std::string connection_string
The connection string for this ServerAddress.
Definition: address.h:210
NetworkServerGameInfo::grfconfig
GRFConfig * grfconfig
List of NewGRF files used.
Definition: network_game_info.h:97
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103