OpenTTD Source: src/network/network_gamelist.cpp Source File

OpenTTD Source 13.1

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
23 NetworkGameList *_network_game_list = nullptr;
24 int _network_game_list_version = 0;
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;
47 item->version = _network_game_list_version;
48
49 if (prev_item == nullptr) {
50 _network_game_list = item;
51 } else {
52 prev_item->next = item;
53 }
54
55 UpdateNetworkGameWindow();
56
57 return item;
58 }
59
64 void NetworkGameListRemoveItem(NetworkGameList *remove)
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 */
76 ClearGRFConfigList(&remove->info.grfconfig);
77 delete remove;
78
79 NetworkRebuildHostList();
80 UpdateNetworkGameWindow();
81 return;
82 }
83 prev_item = item;
84 }
85 }
86
92 void NetworkGameListRemoveExpired()
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 */
103 ClearGRFConfigList(&remove->info.grfconfig);
104 delete remove;
105 } else {
106 prev_item = &item->next;
107 item = item->next;
108 }
109 }
110
111 UpdateNetworkGameWindow();
112 }
113
118 void NetworkAfterNewGRFScan()
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
143 InvalidateWindowClassesData(WC_NETWORK_WINDOW);
144 }
NetworkServerGameInfo::gamescript_version
int gamescript_version
Version of the gamescript.
Definition: game_info.h:109
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: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:745
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:67
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: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: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:35
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:166
network_udp.h
WC_NETWORK_WINDOW
@ WC_NETWORK_WINDOW
Network window; Window numbers:
Definition: window_type.h:465
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:3269
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: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:30
_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:64
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