OpenTTD Source 15.0-beta2
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
23std::vector<std::unique_ptr<NetworkGame>> _network_game_list;
25
32NetworkGame *NetworkGameListAddItem(const std::string &connection_string)
33{
34 /* Parse the connection string to ensure the default port is there. */
35 const std::string resolved_connection_string = ServerAddress::Parse(connection_string, NETWORK_DEFAULT_PORT).connection_string;
36
37 /* Check if it's already added. */
38 auto it = std::ranges::find(_network_game_list, resolved_connection_string, &NetworkGame::connection_string);
39 if (it != std::end(_network_game_list)) return it->get();
40
41 auto &item = _network_game_list.emplace_back(std::make_unique<NetworkGame>(resolved_connection_string));
42 item->info.gamescript_version = -1;
43 item->version = _network_game_list_version;
44
46
47 return item.get();
48}
49
55{
56 auto it = std::ranges::find_if(_network_game_list, [&remove](const auto &item) { return item.get() == remove; });
57 if (it != std::end(_network_game_list)) {
58 _network_game_list.erase(it);
59
60 NetworkRebuildHostList();
62 }
63}
64
71{
72 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; });
73 _network_game_list.erase(it, std::end(_network_game_list));
74
76}
77
83{
84 for (const auto &item : _network_game_list) {
85 /* Reset compatibility state */
86 item->info.compatible = item->info.version_compatible;
87
88 for (auto &c : item->info.grfconfig) {
89 assert(c->flags.Test(GRFConfigFlag::Copy));
90
91 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, &c->ident.md5sum);
92 if (f == nullptr) {
93 /* Don't know the GRF (anymore), so mark game incompatible. */
94 c->status = GCS_NOT_FOUND;
95
96 /* If we miss a file, we're obviously incompatible. */
97 item->info.compatible = false;
98 } else {
99 c->filename = f->filename;
100 c->name = f->name;
101 c->info = f->info;
102 c->status = GCS_UNKNOWN;
103 }
104 }
105 }
106
108}
std::string connection_string
The connection string for this ServerAddress.
Definition address.h:201
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 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.
NetworkGame * 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...
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.
Sending and receiving UDP messages.
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:3241
@ WC_NETWORK_WINDOW
Network window; Window numbers: