OpenTTD Source 20260107-master-g88a467db19
network_game_info.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#include "network_game_info.h"
12#include "../../company_base.h"
13#include "../../timer/timer_game_calendar.h"
14#include "../../timer/timer_game_tick.h"
15#include "../../debug.h"
16#include "../../map_func.h"
17#include "../../game/game.hpp"
18#include "../../game/game_info.hpp"
19#include "../../settings_type.h"
20#include "../../string_func.h"
21#include "../../rev.h"
22#include "../network_func.h"
23#include "../network.h"
24#include "../network_internal.h"
25#include "packet.h"
26
27#include "table/strings.h"
28
29#include "../../safeguards.h"
30
31
36static const uint GITHASH_SUFFIX_LEN = 12;
37
39
44std::string_view GetNetworkRevisionString()
45{
46 static std::string network_revision;
47
48 if (network_revision.empty()) {
49#if not defined(NETWORK_INTERNAL_H)
50# error("network_internal.h must be included, otherwise the debug related preprocessor tokens won't be picked up correctly.")
51#elif not defined(ENABLE_NETWORK_SYNC_EVERY_FRAME)
52 /* Just a standard build. */
53 network_revision = _openttd_revision;
54#elif defined(NETWORK_SEND_DOUBLE_SEED)
55 /* Build for debugging that sends both parts of the seeds and by doing that practically syncs every frame. */
56 network_revision = fmt::format("dbg_seed-{}", _openttd_revision);
57#else
58 /* Build for debugging that sends the first part of the seed every frame, practically syncing every frame. */
59 network_revision = fmt::format("dbg_sync-{}", _openttd_revision);
60#endif
61 if (_openttd_revision_tagged) {
62 /* Tagged; do not mangle further, though ensure it's not too long. */
63 if (network_revision.size() >= NETWORK_REVISION_LENGTH) network_revision.resize(NETWORK_REVISION_LENGTH - 1);
64 } else {
65 /* Not tagged; add the githash suffix while ensuring the string does not become too long. */
66 assert(_openttd_revision_modified < 3);
67 std::string githash_suffix = fmt::format("-{}{}", "gum"[_openttd_revision_modified], _openttd_revision_hash);
68 if (githash_suffix.size() > GITHASH_SUFFIX_LEN) githash_suffix.resize(GITHASH_SUFFIX_LEN);
69
70 /* Where did the hash start in the original string? Overwrite from that position, unless that would create a too long string. */
71 size_t hash_end = network_revision.find_last_of('-');
72 if (hash_end == std::string::npos) hash_end = network_revision.size();
73 if (hash_end + githash_suffix.size() >= NETWORK_REVISION_LENGTH) hash_end = NETWORK_REVISION_LENGTH - githash_suffix.size() - 1;
74
75 /* Replace the git hash in revision string. */
76 network_revision.replace(hash_end, std::string::npos, githash_suffix);
77 }
78 assert(network_revision.size() < NETWORK_REVISION_LENGTH); // size does not include terminator, constant does, hence strictly less than
79 Debug(net, 3, "Network revision name: {}", network_revision);
80 }
81
82 return network_revision;
83}
84
90static std::string_view ExtractNetworkRevisionHash(std::string_view revision_string)
91{
92 size_t index = revision_string.find_last_of('-');
93 if (index == std::string::npos) return {};
94 return revision_string.substr(index);
95}
96
102bool IsNetworkCompatibleVersion(std::string_view other)
103{
104 std::string_view our_revision = GetNetworkRevisionString();
105 if (our_revision == other) return true;
106
107 /* If this version is tagged, then the revision string must be a complete match,
108 * since there is no git hash suffix in it.
109 * This is needed to avoid situations like "1.9.0-beta1" comparing equal to "2.0.0-beta1". */
110 if (_openttd_revision_tagged) return false;
111
112 /* One of the versions is for some sort of debugging, but not both. */
113 if (other.starts_with("dbg_seed") != our_revision.starts_with("dbg_seed")) return false;
114 if (other.starts_with("dbg_sync") != our_revision.starts_with("dbg_sync")) return false;
115
116 std::string_view hash1 = ExtractNetworkRevisionHash(our_revision);
117 std::string_view hash2 = ExtractNetworkRevisionHash(other);
118 return hash1 == hash2;
119}
120
125{
126 /* Check if we are allowed on this server based on the revision-check. */
129
130 /* Check if we have all the GRFs on the client-system too. */
131 for (const auto &c : ngi.grfconfig) {
132 if (c->status == GCS_NOT_FOUND) ngi.compatible = false;
133 }
134}
135
155
161{
162 /* These variables are updated inside _network_game_info as if they are global variables:
163 * - clients_on
164 * - invite_code
165 * These don't need to be updated manually here.
166 */
168 _network_game_info.spectators_on = NetworkSpectatorCount();
171 return _network_game_info;
172}
173
182static void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig &config, std::string_view name)
183{
184 /* Find the matching GRF file */
185 const GRFConfig *f = FindGRFConfig(config.ident.grfid, FGCM_EXACT, &config.ident.md5sum);
186 if (f == nullptr) {
187 AddGRFTextToList(config.name, name.empty() ? GetString(STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN) : name);
188 config.status = GCS_NOT_FOUND;
189 } else {
190 config.filename = f->filename;
191 config.name = f->name;
192 config.info = f->info;
193 config.url = f->url;
194 }
196}
197
203void SerializeNetworkGameInfo(Packet &p, const NetworkServerGameInfo &info, bool send_newgrf_names)
204{
206
207 /*
208 * Please observe the order.
209 * The parts must be read in the same order as they are sent!
210 */
211
212 /* Update the documentation in game_info.h on changes
213 * to the NetworkGameInfo wire-protocol! */
214
215 /* NETWORK_GAME_INFO_VERSION = 7 */
217
218 /* NETWORK_GAME_INFO_VERSION = 6 */
219 p.Send_uint8(send_newgrf_names ? NST_GRFID_MD5_NAME : NST_GRFID_MD5);
220
221 /* NETWORK_GAME_INFO_VERSION = 5 */
222 GameInfo *game_info = Game::GetInfo();
223 p.Send_uint32(game_info == nullptr ? -1 : (uint32_t)game_info->GetVersion());
224 p.Send_string(game_info == nullptr ? "" : game_info->GetName());
225
226 /* NETWORK_GAME_INFO_VERSION = 4 */
227 {
228 /* Only send the GRF Identification (GRF_ID and MD5 checksum) of
229 * the GRFs that are needed, i.e. the ones that the server has
230 * selected in the NewGRF GUI and not the ones that are used due
231 * to the fact that they are in [newgrf-static] in openttd.cfg */
232 uint count = std::ranges::count_if(info.grfconfig, [](const auto &c) { return !c->flags.Test(GRFConfigFlag::Static); });
233 p.Send_uint8 (count); // Send number of GRFs
234
235 /* Send actual GRF Identifications */
236 for (const auto &c : info.grfconfig) {
237 if (c->flags.Test(GRFConfigFlag::Static)) continue;
238
239 SerializeGRFIdentifier(p, c->ident);
240 if (send_newgrf_names) p.Send_string(c->GetName());
241 }
242 }
243
244 /* NETWORK_GAME_INFO_VERSION = 3 */
245 p.Send_uint32(info.calendar_date.base());
246 p.Send_uint32(info.calendar_start.base());
247
248 /* NETWORK_GAME_INFO_VERSION = 2 */
249 p.Send_uint8 (info.companies_max);
250 p.Send_uint8 (info.companies_on);
251 p.Send_uint8 (info.clients_max); // Used to be max-spectators
252
253 /* NETWORK_GAME_INFO_VERSION = 1 */
254 p.Send_string(info.server_name);
256 p.Send_bool (info.use_password);
257 p.Send_uint8 (info.clients_max);
258 p.Send_uint8 (info.clients_on);
259 p.Send_uint8 (info.spectators_on);
260 p.Send_uint16(info.map_width);
261 p.Send_uint16(info.map_height);
263 p.Send_bool (info.dedicated);
264}
265
272{
273 uint8_t game_info_version = p.Recv_uint8();
274 NewGRFSerializationType newgrf_serialisation = NST_GRFID_MD5;
275
276 /*
277 * Please observe the order.
278 * The parts must be read in the same order as they are sent!
279 */
280
281 /* Update the documentation in game_info.h on changes
282 * to the NetworkGameInfo wire-protocol! */
283
284 switch (game_info_version) {
285 case 7:
286 info.ticks_playing = p.Recv_uint64();
287 [[fallthrough]];
288
289 case 6:
290 newgrf_serialisation = (NewGRFSerializationType)p.Recv_uint8();
291 if (newgrf_serialisation >= NST_END) return;
292 [[fallthrough]];
293
294 case 5: {
295 info.gamescript_version = (int)p.Recv_uint32();
297 [[fallthrough]];
298 }
299
300 case 4: {
301 /* Ensure that the maximum number of NewGRFs and the field in the network
302 * protocol are matched to each other. If that is not the case anymore a
303 * check must be added to ensure the received data is still valid. */
304 static_assert(std::numeric_limits<uint8_t>::max() == NETWORK_MAX_GRF_COUNT);
305 uint num_grfs = p.Recv_uint8();
306
307 GRFConfigList &dst = info.grfconfig;
308 for (uint i = 0; i < num_grfs; i++) {
310 switch (newgrf_serialisation) {
311 case NST_GRFID_MD5:
313 break;
314
317 break;
318
319 case NST_LOOKUP_ID: {
320 if (newgrf_lookup_table == nullptr) return;
321 auto it = newgrf_lookup_table->find(p.Recv_uint32());
322 if (it == newgrf_lookup_table->end()) return;
323 grf = it->second;
324 break;
325 }
326
327 default:
328 NOT_REACHED();
329 }
330
331 auto c = std::make_unique<GRFConfig>();
332 c->ident = grf.ident;
334
335 /* Append GRFConfig to the list */
336 dst.push_back(std::move(c));
337 }
338 [[fallthrough]];
339 }
340
341 case 3:
344 [[fallthrough]];
345
346 case 2:
347 info.companies_max = p.Recv_uint8 ();
348 info.companies_on = p.Recv_uint8 ();
349 p.Recv_uint8(); // Used to contain max-spectators.
350 [[fallthrough]];
351
352 case 1:
355 if (game_info_version < 6) p.Recv_uint8 (); // Used to contain server-lang.
356 info.use_password = p.Recv_bool ();
357 info.clients_max = p.Recv_uint8 ();
358 info.clients_on = p.Recv_uint8 ();
359 info.spectators_on = p.Recv_uint8 ();
360 if (game_info_version < 3) { // 16 bits dates got scrapped and are read earlier
363 }
364 if (game_info_version < 6) while (p.Recv_uint8() != 0) {} // Used to contain the map-name.
365 info.map_width = p.Recv_uint16();
366 info.map_height = p.Recv_uint16();
368 info.dedicated = p.Recv_bool ();
369
370 if (to_underlying(info.landscape) >= NUM_LANDSCAPE) info.landscape = LandscapeType::Temperate;
371 }
372
373 /* For older servers, estimate the ticks running based on the calendar date. */
374 if (game_info_version < 7) {
375 info.ticks_playing = static_cast<uint64_t>(std::max(0, info.calendar_date.base() - info.calendar_start.base())) * Ticks::DAY_TICKS;
376 }
377}
378
385{
386 p.Send_uint32(grf.grfid);
387 p.Send_bytes(grf.md5sum);
388}
389
396{
397 grf.grfid = p.Recv_uint32();
398 p.Recv_bytes(grf.md5sum);
399}
400
constexpr Timpl & Set()
Set all bits.
All static information from an Game like name, version, etc.
Definition game_info.hpp:16
static class GameInfo * GetInfo()
Get the current GameInfo.
Definition game.hpp:70
const std::string & GetName() const
Get the Name of the script.
int GetVersion() const
Get the version of the script.
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
static Date date
Current date in days (day counter).
static constexpr TimerGame< struct Calendar >::Date MAX_DATE
The date of the last day of the max year.
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
The date of the first day of the original base year.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
static const uint NETWORK_NAME_LENGTH
The maximum length of the server name and map name, in bytes including '\0'.
Definition config.h:51
static const uint NETWORK_GRF_NAME_LENGTH
Maximum length of the name of a GRF.
Definition config.h:71
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Definition config.h:88
static const uint NETWORK_REVISION_LENGTH
The maximum length of the revision, in bytes including '\0'.
Definition config.h:54
static const uint8_t NETWORK_GAME_INFO_VERSION
What version of game-info do we use?
Definition config.h:47
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
LandscapeType
Landscape types.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
bool _network_dedicated
are we a dedicated server?
Definition network.cpp:69
void SerializeNetworkGameInfo(Packet &p, const NetworkServerGameInfo &info, bool send_newgrf_names)
Serializes the NetworkGameInfo struct to the packet.
std::string_view GetNetworkRevisionString()
Get the network version string used by this build.
void CheckGameCompatibility(NetworkGameInfo &ngi)
Check if an game entry is compatible with our client.
void DeserializeGRFIdentifier(Packet &p, GRFIdentifier &grf)
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet.
static void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig &config, std::string_view name)
Function that is called for every GRFConfig that is read when receiving a NetworkGameInfo.
static std::string_view ExtractNetworkRevisionHash(std::string_view revision_string)
Extract the git hash from the revision string.
const NetworkServerGameInfo & GetCurrentNetworkServerGameInfo()
Get the NetworkServerGameInfo structure with the latest information of the server.
void SerializeGRFIdentifier(Packet &p, const GRFIdentifier &grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
void FillStaticNetworkServerGameInfo()
Fill a NetworkServerGameInfo structure with the static content, or things that are so static they can...
void DeserializeGRFIdentifierWithName(Packet &p, NamedGRFIdentifier &grf)
Deserializes the NamedGRFIdentifier (GRF ID, MD5 checksum and name) from the packet.
void DeserializeNetworkGameInfo(Packet &p, NetworkGameInfo &info, const GameInfoNewGRFLookupTable *newgrf_lookup_table)
Deserializes the NetworkGameInfo struct from the packet.
NetworkServerGameInfo _network_game_info
Information about our game.
static const uint GITHASH_SUFFIX_LEN
How many hex digits of the git hash to include in network revision string.
bool IsNetworkCompatibleVersion(std::string_view other)
Checks whether the given version string is compatible with our version.
Convert NetworkGameInfo to Packet and back.
std::unordered_map< uint32_t, NamedGRFIdentifier > GameInfoNewGRFLookupTable
Lookup table for the GameInfo in case of NST_LOOKUP_ID.
NewGRFSerializationType
The different types/ways a NewGRF can be serialized in the GameInfo since version 6.
@ NST_LOOKUP_ID
Unique ID into a lookup table that is sent before.
@ NST_END
The end of the list (period).
@ NST_GRFID_MD5_NAME
Unique GRF ID, MD5 checksum and name.
@ NST_GRFID_MD5
Unique GRF ID and MD5 checksum.
GRFConfigList _grfconfig
First item in list of current GRF set up.
void CopyGRFConfigList(GRFConfigList &dst, const GRFConfigList &src, bool init_only)
Copy a GRF Config list.
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.
@ Copy
The data is copied from a grf in _all_grfs.
@ Static
GRF file is used statically (can be used in any MP game)
@ FGCM_EXACT
Only find Grfs matching md5sum.
static void AddGRFTextToList(GRFTextList &list, uint8_t langid, std::string_view text_to_add)
Add a new text to a GRFText list.
Basic functions to create, fill and read packets.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
NetworkSettings network
settings related to the network
Information about GRF, used in the game and (part of it) in savegames.
GRFTextWrapper url
NOSAVE: URL belonging to this GRF.
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
GRFStatus status
NOSAVE: GRFStatus, enum.
GRFConfigFlags flags
NOSAVE: GCF_Flags, bitset.
std::string filename
Filename - either with or without full path.
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
Basic data to distinguish a GRF.
uint32_t grfid
GRF ID (defined by Action 0x08)
MD5Hash md5sum
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
LandscapeType landscape
the landscape we're currently in
TimerGameCalendar::Year starting_year
starting date
GameCreationSettings game_creation
settings used during the creation of a game (map)
static uint SizeX()
Get the size of the map along the X.
Definition map_func.h:272
static uint SizeY()
Get the size of the map along the Y.
Definition map_func.h:281
Container to hold the GRF identifier (GRF ID + MD5 checksum) and the name associated with that NewGRF...
std::string name
The name of the NewGRF.
GRFIdentifier ident
The unique identifier of the NewGRF.
The game information that is sent from the server to the clients with extra information only required...
bool version_compatible
Can we connect to this server or not? (based on server_revision)
bool compatible
Can we connect to this server or not? (based on server_revision and grf_match.
The game information that is sent from the server to the client.
TimerGameCalendar::Date calendar_start
When the game started.
bool dedicated
Is this a dedicated server?
std::string server_revision
The version number the server is using (e.g.: 'r304' or 0.5.0)
bool use_password
Is this server passworded?
uint8_t clients_max
Max clients allowed on server.
uint8_t spectators_on
How many spectators do we have?
GRFConfigList grfconfig
List of NewGRF files used.
uint16_t map_height
Map height.
std::string server_name
Server name.
uint16_t map_width
Map width.
TimerGameTick::TickCounter ticks_playing
Amount of ticks the game has been running unpaused.
LandscapeType landscape
The used landscape.
uint8_t companies_max
Max companies allowed on server.
std::string gamescript_name
Name of the gamescript.
TimerGameCalendar::Date calendar_date
Current calendar date.
int gamescript_version
Version of the gamescript.
uint8_t companies_on
How many started companies do we have.
uint8_t clients_on
Current count of clients on server.
uint8_t max_clients
maximum amount of clients
uint8_t max_companies
maximum amount of companies
std::string server_name
name of the server
std::string server_password
password for joining this server
Internal entity of a packet.
Definition packet.h:41
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
Definition packet.cpp:330
size_t Recv_bytes(std::span< uint8_t > span)
Extract at most the length of the span bytes from the packet into the span.
Definition packet.cpp:401
uint64_t Recv_uint64()
Read a 64 bits integer from the packet.
Definition packet.cpp:362
bool Recv_bool()
Read a boolean from the packet.
Definition packet.cpp:307
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
Definition packet.cpp:345
void Send_string(std::string_view data)
Sends a string over the network.
Definition packet.cpp:170
std::string Recv_string(size_t length, StringValidationSettings settings=StringValidationSetting::ReplaceWithQuestionMark)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
Definition packet.cpp:423
void Send_bool(bool data)
Package a boolean in the packet.
Definition packet.cpp:109
std::span< const uint8_t > Send_bytes(const std::span< const uint8_t > span)
Send as many of the bytes as possible in the packet.
Definition packet.cpp:195
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:316
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
Definition packet.cpp:118
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
Definition packet.cpp:139
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
Definition packet.cpp:128
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.
Definition packet.cpp:152
static size_t GetNumItems()
Returns number of valid items in the pool.
Templated helper to make a type-safe 'typedef' representing a single POD value.