OpenTTD Source 20250528-master-g3aca5d62a8
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
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;
23
30NetworkGame *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
44
45 return item.get();
46}
47
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();
60 }
61}
62
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
74}
75
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
106}
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:25
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:3282
@ WC_NETWORK_WINDOW
Network window; Window numbers: