OpenTTD Source 20260108-master-g8ba1860eaa
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
10#include "../stdafx.h"
11
12#include "../window_func.h"
13#include "network_internal.h"
14#include "network_gamelist.h"
15
16#include "../safeguards.h"
17
18std::vector<std::unique_ptr<NetworkGame>> _network_game_list;
20
27NetworkGame *NetworkGameListAddItem(std::string_view connection_string)
28{
29 /* Parse the connection string to ensure the default port is there. */
30 const std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT).connection_string;
31
32 /* Check if it's already added. */
33 auto it = std::ranges::find(_network_game_list, resolved_connection_string, &NetworkGame::connection_string);
34 if (it != std::end(_network_game_list)) return it->get();
35
36 auto &item = _network_game_list.emplace_back(std::make_unique<NetworkGame>(resolved_connection_string));
37 item->info.gamescript_version = -1;
38 item->version = _network_game_list_version;
39
41
42 return item.get();
43}
44
50{
51 auto it = std::ranges::find_if(_network_game_list, [&remove](const auto &item) { return item.get() == remove; });
52 if (it != std::end(_network_game_list)) {
53 _network_game_list.erase(it);
54
55 NetworkRebuildHostList();
57 }
58}
59
66{
67 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; });
68 _network_game_list.erase(it, std::end(_network_game_list));
69
71}
72
78{
79 for (const auto &item : _network_game_list) {
80 /* Reset compatibility state */
81 item->info.compatible = item->info.version_compatible;
82
83 for (auto &c : item->info.grfconfig) {
84 assert(c->flags.Test(GRFConfigFlag::Copy));
85
86 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, &c->ident.md5sum);
87 if (f == nullptr) {
88 /* Don't know the GRF (anymore), so mark game incompatible. */
89 c->status = GCS_NOT_FOUND;
90
91 /* If we miss a file, we're obviously incompatible. */
92 item->info.compatible = false;
93 } else {
94 c->filename = f->filename;
95 c->name = f->name;
96 c->info = f->info;
97 c->status = GCS_UNKNOWN;
98 }
99 }
100 }
101
103}
std::string connection_string
The connection string for this ServerAddress.
Definition address.h:198
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
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition config.h:23
NetworkGame * NetworkGameListAddItem(std::string_view connection_string)
Add a new item to the linked gamelist.
void NetworkGameListRemoveItem(NetworkGame *remove)
Remove an item from the gamelist linked list.
void NetworkGameListRemoveExpired()
Remove all servers that have not recently been updated.
std::vector< std::unique_ptr< NetworkGame > > _network_game_list
Game list of this client.
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...
Handling of the list of games.
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Variables and function used internally.
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
@ GCS_UNKNOWN
The status of this grf file is unknown.
@ Copy
The data is copied from a grf in _all_grfs.
@ 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)
std::string connection_string
Address of the server.
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:3318
@ WC_NETWORK_WINDOW
Network window; Window numbers: