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

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
13#include "../stdafx.h"
14
15#include "../window_func.h"
16#include "network_internal.h"
17#include "network_gamelist.h"
18
19#include "../safeguards.h"
20
21std::vector<std::unique_ptr<NetworkGame>> _network_game_list;
22 int _network_game_list_version = 0;
23
30 NetworkGame *NetworkGameListAddItem(std::string_view connection_string)
31{
32 /* Parse the connection string to ensure the default port is there. */
33 const std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT).connection_string;
34
35 /* Check if it's already added. */
36 auto it = std::ranges::find(_network_game_list, resolved_connection_string, &NetworkGame::connection_string);
37 if (it != std::end(_network_game_list)) return it->get();
38
39 auto &item = _network_game_list.emplace_back(std::make_unique<NetworkGame>(resolved_connection_string));
40 item->info.gamescript_version = -1;
41 item->version = _network_game_list_version;
42
43 UpdateNetworkGameWindow();
44
45 return item.get();
46}
47
52 void NetworkGameListRemoveItem(NetworkGame *remove)
53{
54 auto it = std::ranges::find_if(_network_game_list, [&remove](const auto &item) { return item.get() == remove; });
55 if (it != std::end(_network_game_list)) {
56 _network_game_list.erase(it);
57
58 NetworkRebuildHostList();
59 UpdateNetworkGameWindow();
60 }
61}
62
68 void NetworkGameListRemoveExpired()
69{
70 auto it = std::remove_if(std::begin(_network_game_list), std::end(_network_game_list), [](const auto &item) { return !item->manually && item->version < _network_game_list_version; });
71 _network_game_list.erase(it, std::end(_network_game_list));
72
73 UpdateNetworkGameWindow();
74}
75
80 void NetworkAfterNewGRFScan()
81{
82 for (const auto &item : _network_game_list) {
83 /* Reset compatibility state */
84 item->info.compatible = item->info.version_compatible;
85
86 for (auto &c : item->info.grfconfig) {
87 assert(c->flags.Test(GRFConfigFlag::Copy));
88
89 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, &c->ident.md5sum);
90 if (f == nullptr) {
91 /* Don't know the GRF (anymore), so mark game incompatible. */
92 c->status = GCS_NOT_FOUND;
93
94 /* If we miss a file, we're obviously incompatible. */
95 item->info.compatible = false;
96 } else {
97 c->filename = f->filename;
98 c->name = f->name;
99 c->info = f->info;
100 c->status = GCS_UNKNOWN;
101 }
102 }
103 }
104
105 InvalidateWindowClassesData(WC_NETWORK_WINDOW);
106}
ServerAddress::connection_string
std::string connection_string
The connection string for this ServerAddress.
Definition address.h:198
ServerAddress::Parse
static ServerAddress Parse(std::string_view 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:444
NETWORK_DEFAULT_PORT
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition config.h:25
NetworkGameListAddItem
NetworkGame * NetworkGameListAddItem(std::string_view connection_string)
Add a new item to the linked gamelist.
Definition network_gamelist.cpp:30
NetworkGameListRemoveItem
void NetworkGameListRemoveItem(NetworkGame *remove)
Remove an item from the gamelist linked list.
Definition network_gamelist.cpp:52
NetworkGameListRemoveExpired
void NetworkGameListRemoveExpired()
Remove all servers that have not recently been updated.
Definition network_gamelist.cpp:68
_network_game_list
std::vector< std::unique_ptr< NetworkGame > > _network_game_list
Game list of this client.
Definition network_gamelist.cpp:21
_network_game_list_version
int _network_game_list_version
Current version of all items in the list.
Definition network_gamelist.cpp:22
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:80
network_gamelist.h
Handling of the list of games.
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition network_gui.cpp:66
network_internal.h
Variables and function used internally.
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:596
GCS_NOT_FOUND
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition newgrf_config.h:36
GCS_UNKNOWN
@ GCS_UNKNOWN
The status of this grf file is unknown.
Definition newgrf_config.h:34
Copy
@ Copy
The data is copied from a grf in _all_grfs.
Definition newgrf_config.h:25
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition newgrf_config.h:208
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:158
GRFConfig::info
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Definition newgrf_config.h:172
GRFConfig::name
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
Definition newgrf_config.h:171
GRFConfig::filename
std::string filename
Filename - either with or without full path.
Definition newgrf_config.h:170
NetworkGame
Structure with information shown in the game list (GUI)
Definition network_gamelist.h:27
NetworkGame::connection_string
std::string connection_string
Address of the server.
Definition network_gamelist.h:31
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:3314
WC_NETWORK_WINDOW
@ WC_NETWORK_WINDOW
Network window; Window numbers:
Definition window_type.h:483