OpenTTD Source  12.0-beta2
tcp_content.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 
12 #include "../../stdafx.h"
13 #include "../../textfile_gui.h"
14 #include "../../newgrf_config.h"
15 #include "../../base_media_base.h"
16 #include "../../ai/ai.hpp"
17 #include "../../game/game.hpp"
18 #include "../../fios.h"
19 #include "tcp_content.h"
20 
21 #include "../../safeguards.h"
22 
28 {
29  switch (this->state) {
33  return true;
34 
35  default:
36  return false;
37  }
38 }
39 
45 {
46  return this->state < ContentInfo::INVALID && this->type >= CONTENT_TYPE_BEGIN && this->type < CONTENT_TYPE_END;
47 }
48 
54 const char *ContentInfo::GetTextfile(TextfileType type) const
55 {
56  if (this->state == INVALID) return nullptr;
57  const char *tmp;
58  switch (this->type) {
59  default: NOT_REACHED();
60  case CONTENT_TYPE_AI:
61  tmp = AI::GetScannerInfo()->FindMainScript(this, true);
62  break;
64  tmp = AI::GetScannerLibrary()->FindMainScript(this, true);
65  break;
66  case CONTENT_TYPE_GAME:
67  tmp = Game::GetScannerInfo()->FindMainScript(this, true);
68  break;
70  tmp = Game::GetScannerLibrary()->FindMainScript(this, true);
71  break;
72  case CONTENT_TYPE_NEWGRF: {
73  const GRFConfig *gc = FindGRFConfig(BSWAP32(this->unique_id), FGCM_EXACT, this->md5sum);
74  tmp = gc != nullptr ? gc->filename : nullptr;
75  break;
76  }
79  break;
82  break;
85  break;
88  extern const char *FindScenario(const ContentInfo *ci, bool md5sum);
89  tmp = FindScenario(this, true);
90  break;
91  }
92  if (tmp == nullptr) return nullptr;
94 }
95 
103 {
105 
106  switch (this->HasClientQuit() ? PACKET_CONTENT_END : type) {
111  case PACKET_CONTENT_SERVER_INFO: return this->Receive_SERVER_INFO(p);
114 
115  default:
116  if (this->HasClientQuit()) {
117  Debug(net, 0, "[tcp/content] Received invalid packet type {}", type);
118  } else {
119  Debug(net, 0, "[tcp/content] Received illegal packet");
120  }
121  return false;
122  }
123 }
124 
130 {
131  /*
132  * We read only a few of the packets. This as receiving packets can be expensive
133  * due to the re-resolving of the parent/child relations and checking the toggle
134  * state of all bits. We cannot do this all in one go, as we want to show the
135  * user what we already received. Otherwise, it can take very long before any
136  * progress is shown to the end user that something has been received.
137  * It is also the case that we request extra content from the content server in
138  * case there is an unknown (in the content list) piece of content. These will
139  * come in after the main lists have been requested. As a result, we won't be
140  * getting everything reliably in one batch. Thus, we need to make subsequent
141  * updates in that case as well.
142  *
143  * As a result, we simple handle an arbitrary number of packets in one cycle,
144  * and let the rest be handled in subsequent cycles. These are ran, almost,
145  * immediately after this cycle so in speed it does not matter much, except
146  * that the user inferface will appear better responding.
147  *
148  * What arbitrary number to choose is the ultimate question though.
149  */
150  Packet *p;
151  static const int MAX_PACKETS_TO_RECEIVE = 42;
152  int i = MAX_PACKETS_TO_RECEIVE;
153  while (--i != 0 && (p = this->ReceivePacket()) != nullptr) {
154  bool cont = this->HandlePacket(p);
155  delete p;
156  if (!cont) return true;
157  }
158 
159  return i != MAX_PACKETS_TO_RECEIVE - 1;
160 }
161 
162 
169 {
170  Debug(net, 0, "[tcp/content] Received illegal packet type {}", type);
171  return false;
172 }
173 
181 
188 {
189  switch (type) {
190  default: return NO_DIRECTORY;
191  case CONTENT_TYPE_AI: return AI_DIR;
193  case CONTENT_TYPE_GAME: return GAME_DIR;
195  case CONTENT_TYPE_NEWGRF: return NEWGRF_DIR;
196 
200  return BASESET_DIR;
201 
202  case CONTENT_TYPE_SCENARIO: return SCENARIO_DIR;
204  }
205 }
ContentInfo::IsSelected
bool IsSelected() const
Is the state either selected or autoselected?
Definition: tcp_content.cpp:27
BaseMedia< GraphicsSet >::GetAvailableSets
static GraphicsSet * GetAvailableSets()
Return the available sets.
Definition: base_media_func.h:367
ContentInfo::GetTextfile
const char * GetTextfile(TextfileType type) const
Search a textfile file next to this file in the content list.
Definition: tcp_content.cpp:54
ContentInfo::type
ContentType type
Type of content.
Definition: tcp_content_type.h:61
NetworkContentSocketHandler::Receive_SERVER_CONTENT
virtual bool Receive_SERVER_CONTENT(Packet *p)
Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string f...
Definition: tcp_content.cpp:180
AI::GetScannerInfo
static AIScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find AIs.
Definition: ai_core.cpp:377
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:736
CONTENT_TYPE_GAME_LIBRARY
@ CONTENT_TYPE_GAME_LIBRARY
The content consists of a GS library.
Definition: tcp_content_type.h:27
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
GAME_LIBRARY_DIR
@ GAME_LIBRARY_DIR
Subdirectory for all GS libraries.
Definition: fileio_type.h:122
TryGetBaseSetFile
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
Definition: base_media_func.h:279
CONTENT_TYPE_NEWGRF
@ CONTENT_TYPE_NEWGRF
The content consists of a NewGRF.
Definition: tcp_content_type.h:19
HEIGHTMAP_DIR
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:113
NetworkContentSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition: tcp_content.cpp:129
PACKET_CONTENT_SERVER_CONTENT
@ PACKET_CONTENT_SERVER_CONTENT
Reply with the content of the given ID.
Definition: tcp_content_type.h:40
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition: tcp_content_type.h:28
PACKET_CONTENT_CLIENT_INFO_EXTID_MD5
@ PACKET_CONTENT_CLIENT_INFO_EXTID_MD5
Queries the content server for information about a list of external IDs and MD5.
Definition: tcp_content_type.h:37
NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID_MD5
virtual bool Receive_CLIENT_INFO_EXTID_MD5(Packet *p)
Client requesting a list of content info based on an external 'unique' id; GRF ID + MD5 checksum for ...
Definition: tcp_content.cpp:177
ContentInfo::IsValid
bool IsValid() const
Is the information from this content info valid?
Definition: tcp_content.cpp:44
ContentType
ContentType
The values in the enum are important; they are used as database 'keys'.
Definition: tcp_content_type.h:16
PacketContentType
PacketContentType
Enum with all types of TCP content packets.
Definition: tcp_content_type.h:33
GetContentInfoSubDir
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
Definition: tcp_content.cpp:187
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:144
NetworkContentSocketHandler::Receive_CLIENT_INFO_EXTID
virtual bool Receive_CLIENT_INFO_EXTID(Packet *p)
Client requesting a list of content info based on an external 'unique' id; GRF ID for NewGRFS,...
Definition: tcp_content.cpp:176
PACKET_CONTENT_END
@ PACKET_CONTENT_END
Must ALWAYS be on the end of this list!! (period)
Definition: tcp_content_type.h:41
Game::GetScannerLibrary
static GameScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find Game Libraries.
Definition: game_core.cpp:278
tcp_content.h
CONTENT_TYPE_GAME
@ CONTENT_TYPE_GAME
The content consists of a game script.
Definition: tcp_content_type.h:26
PACKET_CONTENT_CLIENT_INFO_ID
@ PACKET_CONTENT_CLIENT_INFO_ID
Queries the content server for information about a list of internal IDs.
Definition: tcp_content_type.h:35
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:155
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition: fileio_type.h:119
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:50
ContentInfo::md5sum
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content_type.h:70
AI::GetScannerLibrary
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
Definition: ai_core.cpp:382
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition: fileio_type.h:121
SCENARIO_DIR
@ SCENARIO_DIR
Base directory for all scenarios.
Definition: fileio_type.h:112
ContentInfo::SELECTED
@ SELECTED
The content has been manually selected.
Definition: tcp_content_type.h:54
PACKET_CONTENT_CLIENT_CONTENT
@ PACKET_CONTENT_CLIENT_CONTENT
Request a content file given an internal ID.
Definition: tcp_content_type.h:39
Packet
Internal entity of a packet.
Definition: packet.h:44
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:390
NEWGRF_DIR
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Definition: fileio_type.h:117
NetworkContentSocketHandler::ReceiveInvalidPacket
bool ReceiveInvalidPacket(PacketContentType type)
Helper for logging receiving invalid packets.
Definition: tcp_content.cpp:168
PACKET_CONTENT_CLIENT_INFO_EXTID
@ PACKET_CONTENT_CLIENT_INFO_EXTID
Queries the content server for information about a list of external IDs.
Definition: tcp_content_type.h:36
CONTENT_TYPE_AI
@ CONTENT_TYPE_AI
The content consists of an AI.
Definition: tcp_content_type.h:20
NetworkSocketHandler::HasClientQuit
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
Definition: core.h:68
CONTENT_TYPE_BASE_GRAPHICS
@ CONTENT_TYPE_BASE_GRAPHICS
The content consists of base graphics.
Definition: tcp_content_type.h:18
GetTextfile
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:416
ContentInfo::ALREADY_HERE
@ ALREADY_HERE
The content is already at the client side.
Definition: tcp_content_type.h:56
NetworkContentSocketHandler::Receive_CLIENT_CONTENT
virtual bool Receive_CLIENT_CONTENT(Packet *p)
Client requesting the actual content: uint16 count of unique ids uint32 unique id (count times)
Definition: tcp_content.cpp:179
ContentInfo::AUTOSELECTED
@ AUTOSELECTED
The content has been selected as dependency.
Definition: tcp_content_type.h:55
CONTENT_TYPE_AI_LIBRARY
@ CONTENT_TYPE_AI_LIBRARY
The content consists of an AI library.
Definition: tcp_content_type.h:21
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
NetworkContentSocketHandler::Receive_CLIENT_INFO_LIST
virtual bool Receive_CLIENT_INFO_LIST(Packet *p)
Client requesting a list of content info: byte type uint32 openttd version.
Definition: tcp_content.cpp:174
NetworkContentSocketHandler::Receive_CLIENT_INFO_ID
virtual bool Receive_CLIENT_INFO_ID(Packet *p)
Client requesting a list of content info: uint16 count of ids uint32 id (count times)
Definition: tcp_content.cpp:175
CONTENT_TYPE_BEGIN
@ CONTENT_TYPE_BEGIN
Helper to mark the begin of the types.
Definition: tcp_content_type.h:17
PACKET_CONTENT_SERVER_INFO
@ PACKET_CONTENT_SERVER_INFO
Reply of content server with information about content.
Definition: tcp_content_type.h:38
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:306
ContentInfo::state
State state
Whether the content info is selected (for download)
Definition: tcp_content_type.h:73
CONTENT_TYPE_BASE_SOUNDS
@ CONTENT_TYPE_BASE_SOUNDS
The content consists of base sounds.
Definition: tcp_content_type.h:24
ContentInfo::unique_id
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content_type.h:69
AI_LIBRARY_DIR
@ AI_LIBRARY_DIR
Subdirectory for all AI libraries.
Definition: fileio_type.h:120
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
Game::GetScannerInfo
static GameScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find Game scripts.
Definition: game_core.cpp:274
PACKET_CONTENT_CLIENT_INFO_LIST
@ PACKET_CONTENT_CLIENT_INFO_LIST
Queries the content server for a list of info of a given content type.
Definition: tcp_content_type.h:34
CONTENT_TYPE_SCENARIO
@ CONTENT_TYPE_SCENARIO
The content consists of a scenario.
Definition: tcp_content_type.h:22
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
NetworkContentSocketHandler::HandlePacket
bool HandlePacket(Packet *p)
Handle the given packet, i.e.
Definition: tcp_content.cpp:102
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:199
ScriptScanner::FindMainScript
const char * FindMainScript(const ContentInfo *ci, bool md5sum)
Find a script of a ContentInfo.
Definition: script_scanner.cpp:258
GRFConfig::filename
char * filename
Filename - either with or without full path.
Definition: newgrf_config.h:165
ContentInfo::INVALID
@ INVALID
The content's invalid.
Definition: tcp_content_type.h:58
NetworkContentSocketHandler::Receive_SERVER_INFO
virtual bool Receive_SERVER_INFO(Packet *p)
Server sending list of content info: byte type (invalid ID == does not exist) uint32 id uint32 file_s...
Definition: tcp_content.cpp:178
FindScenario
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:717
CONTENT_TYPE_HEIGHTMAP
@ CONTENT_TYPE_HEIGHTMAP
The content consists of a heightmap.
Definition: tcp_content_type.h:23
CONTENT_TYPE_BASE_MUSIC
@ CONTENT_TYPE_BASE_MUSIC
The content consists of base music.
Definition: tcp_content_type.h:25