OpenTTD Source  20241108-master-g80f628063a
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 }
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
std::string connection_string
The connection string for this ServerAddress.
Definition: address.h:210
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
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:25
void NetworkGameListRemoveExpired()
Remove all servers that have not recently been updated.
void NetworkGameListRemoveItem(NetworkGameList *remove)
Remove an item from the gamelist linked list.
NetworkGameList * NetworkGameListAddItem(const std::string &connection_string)
Add a new item to the linked gamelist.
int _network_game_list_version
Current version of all items in the list.
void NetworkAfterNewGRFScan()
Rebuild the GRFConfig's of the servers in the game list as we did a rescan and might have found new N...
NetworkGameList * _network_game_list
Game list of this client.
Handling of the list of games.
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:66
Variables and function used internally.
Sending and receiving UDP messages.
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
@ GCF_COPY
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:26
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:36
@ GCS_UNKNOWN
The status of this grf file is unknown.
Definition: newgrf_config.h:34
@ FGCM_EXACT
Only find Grfs matching md5sum.
Information about GRF, used in the game and (part of it) in savegames.
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
std::string filename
Filename - either with or without full path.
Structure with information shown in the game list (GUI)
int version
Used to see which servers are no longer available on the Game Coordinator and can be removed.
std::string connection_string
Address of the server.
NetworkGameList * next
Next pointer to make a linked game list.
NetworkGameInfo info
The game information of this server.
GRFConfig * grfconfig
List of NewGRF files used.
int gamescript_version
Version of the gamescript.
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:3228
@ WC_NETWORK_WINDOW
Network window; Window numbers:
Definition: window_type.h:478