|
OpenTTD Source
14.0-beta1
|
12 #include "../../stdafx.h"
13 #include "network_game_info.h"
14 #include "../../core/bitmath_func.hpp"
15 #include "../../company_base.h"
16 #include "../../timer/timer_game_calendar.h"
17 #include "../../timer/timer_game_tick.h"
18 #include "../../debug.h"
19 #include "../../map_func.h"
20 #include "../../game/game.hpp"
21 #include "../../game/game_info.hpp"
22 #include "../../settings_type.h"
23 #include "../../string_func.h"
24 #include "../../rev.h"
25 #include "../network_func.h"
26 #include "../network.h"
29 #include "../../safeguards.h"
36 static const uint GITHASH_SUFFIX_LEN = 12;
44 std::string_view GetNetworkRevisionString()
46 static std::string network_revision;
48 if (network_revision.empty()) {
49 network_revision = _openttd_revision;
50 if (_openttd_revision_tagged) {
55 assert(_openttd_revision_modified < 3);
56 std::string githash_suffix = fmt::format(
"-{}{}",
"gum"[_openttd_revision_modified], _openttd_revision_hash);
57 if (githash_suffix.size() > GITHASH_SUFFIX_LEN) githash_suffix.resize(GITHASH_SUFFIX_LEN);
60 size_t hash_end = network_revision.find_last_of(
'-');
61 if (hash_end == std::string::npos) hash_end = network_revision.size();
65 network_revision.replace(hash_end, std::string::npos, githash_suffix);
68 Debug(net, 3,
"Network revision name: {}", network_revision);
71 return network_revision;
79 static std::string_view ExtractNetworkRevisionHash(std::string_view revision_string)
81 size_t index = revision_string.find_last_of(
'-');
82 if (index == std::string::npos)
return {};
83 return revision_string.substr(index);
91 bool IsNetworkCompatibleVersion(std::string_view other)
93 if (GetNetworkRevisionString() == other)
return true;
98 if (_openttd_revision_tagged)
return false;
100 std::string_view hash1 = ExtractNetworkRevisionHash(GetNetworkRevisionString());
101 std::string_view hash2 = ExtractNetworkRevisionHash(other);
102 return hash1 == hash2;
124 void FillStaticNetworkServerGameInfo()
155 return &_network_game_info;
166 static void HandleIncomingNetworkGameInfoGRFConfig(
GRFConfig *config, std::string name)
203 p->
Send_uint8(send_newgrf_names ? NST_GRFID_MD5_NAME : NST_GRFID_MD5);
229 SerializeGRFIdentifier(p, &c->
ident);
261 void DeserializeNetworkGameInfo(
Packet *p,
NetworkGameInfo *info,
const GameInfoNewGRFLookupTable *newgrf_lookup_table)
264 NewGRFSerializationType newgrf_serialisation = NST_GRFID_MD5;
274 switch (game_info_version) {
280 newgrf_serialisation = (NewGRFSerializationType)p->
Recv_uint8();
281 if (newgrf_serialisation >= NST_END)
return;
298 for (uint i = 0; i < num_grfs; i++) {
300 switch (newgrf_serialisation) {
302 DeserializeGRFIdentifier(p, &grf.
ident);
305 case NST_GRFID_MD5_NAME:
306 DeserializeGRFIdentifierWithName(p, &grf);
309 case NST_LOOKUP_ID: {
310 if (newgrf_lookup_table ==
nullptr)
return;
311 auto it = newgrf_lookup_table->find(p->
Recv_uint32());
312 if (it == newgrf_lookup_table->end())
return;
323 HandleIncomingNetworkGameInfoGRFConfig(c, grf.
name);
351 if (game_info_version < 3) {
355 if (game_info_version < 6)
while (p->
Recv_uint8() != 0) {}
365 if (game_info_version < 7) {
378 for (
size_t j = 0; j < grf->
md5sum.size(); j++) {
391 for (
size_t j = 0; j < grf->
md5sum.size(); j++) {
403 DeserializeGRFIdentifier(p, &grf->
ident);
uint64_t Recv_uint64()
Read a 64 bits integer from the packet.
int gamescript_version
Version of the gamescript.
The game information that is sent from the server to the clients with extra information only required...
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
void Send_uint64(uint64_t data)
Package a 64 bits integer in the packet.
std::string gamescript_name
Name of the gamescript.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
The date of the first day of the original base year.
byte landscape
the landscape we're currently in
static const uint NETWORK_NAME_LENGTH
The maximum length of the server name and map name, in bytes including '\0'.
std::string server_revision
The version number the server is using (e.g.: 'r304' or 0.5.0)
static class GameInfo * GetInfo()
Get the current GameInfo.
byte clients_max
Max clients allowed on server.
std::string name
The name of the NewGRF.
std::string filename
Filename - either with or without full path.
The game information that is sent from the server to the client.
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
ClientSettings _settings_client
The current settings for this game.
@ GCF_COPY
The data is copied from a grf in _all_grfs.
GRFIdentifier ident
The unique identifier of the NewGRF.
GRFStatus status
NOSAVE: GRFStatus, enum.
bool version_compatible
Can we connect to this server or not? (based on server_revision)
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
GameCreationSettings game_creation
settings used during the creation of a game (map)
std::string Recv_string(size_t length, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads characters (bytes) from the packet until it finds a '\0', or reaches a maximum of length charac...
static constexpr TimerGame< struct Calendar >::Date MAX_DATE
The date of the last day of the max year.
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Basic data to distinguish a GRF.
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
Information about GRF, used in the game and (part of it) in savegames.
uint16_t map_height
Map height.
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
MD5Hash md5sum
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
const std::string & GetName() const
Get the Name of the script.
TimerGameCalendar::Year starting_year
starting date
byte companies_on
How many started companies do we have.
byte companies_max
Max companies allowed on server.
bool _network_dedicated
are we a dedicated server?
Internal entity of a packet.
static const uint NETWORK_REVISION_LENGTH
The maximum length of the revision, in bytes including '\0'.
void Send_bool(bool data)
Package a boolean in the packet.
static debug_inline uint SizeX()
Get the size of the map along the X.
std::string server_name
name of the server
uint8_t flags
NOSAVE: GCF_Flags, bitset.
int GetVersion() const
Get the version of the script.
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
static size_t GetNumItems()
Returns number of valid items in the pool.
bool Recv_bool()
Read a boolean from the packet.
bool dedicated
Is this a dedicated server?
bool use_password
Is this server passworded?
static const uint NETWORK_GRF_NAME_LENGTH
Maximum length of the name of a GRF.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
TimerGameCalendar::Date calendar_date
Current calendar date.
uint8_t max_clients
maximum amount of clients
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
std::string server_password
password for joining this server
uint8_t max_companies
maximum amount of companies
uint16_t map_width
Map width.
All static information from an Game like name, version, etc.
GRFTextWrapper url
NOSAVE: URL belonging to this GRF.
byte spectators_on
How many spectators do we have?
GRFConfig * _grfconfig
First item in list of current GRF set up.
void Send_string(const std::string_view data)
Sends a string over the network.
NetworkSettings network
settings related to the network
uint32_t grfid
GRF ID (defined by Action 0x08)
static Date date
Current date in days (day counter).
byte landscape
The used landscape.
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
std::string server_name
Server name.
@ FGCM_EXACT
Only find Grfs matching md5sum.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
uint16_t Recv_uint16()
Read a 16 bits integer from the packet.
static const byte NETWORK_GAME_INFO_VERSION
What version of game-info do we use?
byte clients_on
Current count of clients on server.
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
GRFConfig * grfconfig
List of NewGRF files used.
TimerGameCalendar::Date calendar_start
When the game started.
Container to hold the GRF identifier (GRF ID + MD5 checksum) and the name associated with that NewGRF...
bool compatible
Can we connect to this server or not? (based on server_revision and grf_match.
static void AddGRFTextToList(GRFTextList &list, byte langid, const std::string &text_to_add)
Add a new text to a GRFText list.
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
static uint SizeY()
Get the size of the map along the Y.
const char * GetName() const
Get the name of this grf.
TimerGameTick::TickCounter ticks_playing
Amount of ticks the game has been running unpaused.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.