OpenTTD Source 20260721-master-g25ec12c62d
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../../stdafx.h"
11#include "../../textfile_gui.h"
12#include "../../newgrf_config.h"
13#include "../../newgrf.h"
18#include "../../ai/ai.hpp"
19#include "../../game/game.hpp"
20#include "../../fios.h"
21#include "tcp_content.h"
22
23#include "table/strings.h"
24
25#include "../../safeguards.h"
26
32{
33 switch (this->state) {
37 return true;
38
39 default:
40 return false;
41 }
42}
43
49{
50 return this->state < ContentInfo::State::Invalid && this->type >= ContentType::Begin && this->type < ContentType::End;
51}
52
58std::optional<std::string> ContentInfo::GetTextfile(TextfileType type) const
59{
60 if (this->state == ContentInfo::State::Invalid) return std::nullopt;
61 std::optional<std::string_view> tmp;
62 switch (this->type) {
63 default: NOT_REACHED();
64 case ContentType::Ai:
65 tmp = AI::GetScannerInfo()->FindMainScript(*this, true);
66 break;
68 tmp = AI::GetScannerLibrary()->FindMainScript(*this, true);
69 break;
70 case ContentType::Gs:
71 tmp = Game::GetScannerInfo()->FindMainScript(*this, true);
72 break;
74 tmp = Game::GetScannerLibrary()->FindMainScript(*this, true);
75 break;
78 if (gc != nullptr) tmp = gc->filename;
79 break;
80 }
83 break;
86 break;
89 break;
92 tmp = FindScenario(*this, true);
93 break;
94 }
95 if (!tmp.has_value()) return std::nullopt;
96 return ::GetTextfile(type, GetContentInfoSubDir(this->type), *tmp);
97}
98
106{
107 PacketContentType type = static_cast<PacketContentType>(p.Recv_uint8());
108
109 switch (type) {
117
118 default:
119 Debug(net, 0, "[tcp/content] Received invalid packet type {}", type);
120 return false;
121 }
122}
123
129{
130 /*
131 * We read only a few of the packets. This as receiving packets can be expensive
132 * due to the re-resolving of the parent/child relations and checking the toggle
133 * state of all bits. We cannot do this all in one go, as we want to show the
134 * user what we already received. Otherwise, it can take very long before any
135 * progress is shown to the end user that something has been received.
136 * It is also the case that we request extra content from the content server in
137 * case there is an unknown (in the content list) piece of content. These will
138 * come in after the main lists have been requested. As a result, we won't be
139 * getting everything reliably in one batch. Thus, we need to make subsequent
140 * updates in that case as well.
141 *
142 * As a result, we simple handle an arbitrary number of packets in one cycle,
143 * and let the rest be handled in subsequent cycles. These are ran, almost,
144 * immediately after this cycle so in speed it does not matter much, except
145 * that the user interface will appear better responding.
146 *
147 * What arbitrary number to choose is the ultimate question though.
148 */
149 std::unique_ptr<Packet> p;
150 static const int MAX_PACKETS_TO_RECEIVE = 42;
151 int i = MAX_PACKETS_TO_RECEIVE;
152 while (--i != 0 && (p = this->ReceivePacket()) != nullptr) {
153 bool cont = this->HandlePacket(*p);
154 if (!cont) return true;
155 }
156
157 return i != MAX_PACKETS_TO_RECEIVE - 1;
158}
159
160
167{
168 Debug(net, 0, "[tcp/content] Received illegal packet type {}", type);
169 return false;
170}
171
179
Base functions for all AIs.
Generic functions for replacing base data (graphics, sounds).
std::optional< std::string_view > TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, std::span< const std::unique_ptr< Tbase_set > > sets)
Check whether there's a base set matching some information.
Generic functions for replacing base graphics data.
Generic functions for replacing base music data.
Generic functions for replacing base sounds data.
static AIScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find AI Libraries.
Definition ai_core.cpp:360
static AIScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find AIs.
Definition ai_core.cpp:351
static std::span< const std::unique_ptr< GraphicsSet > > GetAvailableSets()
static GameScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find Game Libraries.
static GameScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find Game scripts.
virtual bool ReceiveClientContent(Packet &p)
Client requesting the actual content: uint16_t count of unique ids uint32_t unique id (count times).
virtual bool ReceiveClientInfoID(Packet &p)
Client requesting a list of content info: uint16_t count of ids uint32_t id (count times).
bool ReceivePackets()
Receive a packet at TCP level.
virtual bool ReceiveServerInfo(Packet &p)
Server sending list of content info: uint8_t type (invalid ID == does not exist) uint32_t id uint32_t...
virtual bool ReceiveClientInfoList(Packet &p)
Client requesting a list of content info: uint8_t type uint32_t openttd version (or 0xFFFFFFFF if usi...
virtual bool ReceiveServerContent(Packet &p)
Server sending list of content info: uint32_t unique id uint32_t file size (0 == does not exist) stri...
virtual bool ReceiveClientInfoExternalIDMD5(Packet &p)
Client requesting a list of content info based on an external 'unique' id; GRF ID + MD5 checksum for ...
virtual bool ReceiveClientInfoExternalID(Packet &p)
Client requesting a list of content info based on an external 'unique' id; GRF ID for NewGRFS,...
bool ReceiveInvalidPacket(PacketContentType type)
Helper for logging receiving invalid packets.
bool HandlePacket(Packet &p)
Handle the given packet, i.e.
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
Definition tcp.cpp:118
std::optional< std::string_view > FindMainScript(const ContentInfo &ci, bool md5sum)
Find a script of a ContentInfo.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
@ NewGrf
Subdirectory for all NewGRFs.
Definition fileio_type.h:97
@ GsLibrary
Subdirectory for all GS libraries.
@ Scenario
Base directory for all scenarios.
Definition fileio_type.h:92
@ Heightmap
Subdirectory of scenario for heightmaps.
Definition fileio_type.h:93
@ None
A path without any base directory.
@ Ai
Subdirectory for all AI files.
Definition fileio_type.h:99
@ Gs
Subdirectory for all game scripts.
@ Baseset
Subdirectory for all base data (base sets, intro game).
Definition fileio_type.h:96
@ AiLibrary
Subdirectory for all AI libraries.
std::optional< std::string_view > FindScenario(const ContentInfo &ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition fios.cpp:653
Declarations for savegames operations.
Base functions for all Games.
Base for the NewGRF implementation.
constexpr T UnflattenNewGRFLabel(uint32_t value)
Unflatten a NewGRF related label from a 32 bits integer.
Definition newgrf.h:267
const GRFConfig * FindGRFConfig(GrfID grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
Functions to find and configure NewGRFs.
@ Exact
Only find Grfs matching md5sum.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
uint32_t unique_id
Unique ID; either GRF ID or shortname.
bool IsValid() const
Is the information from this content info valid?
MD5Hash md5sum
The MD5 checksum.
State state
Whether the content info is selected (for download).
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this file in the content list.
bool IsSelected() const
Is the state either selected or autoselected?
ContentType type
Type of content.
@ Invalid
The content's invalid.
@ AlreadyHere
The content is already at the client side.
@ Selected
The content has been manually selected.
@ Autoselected
The content has been selected as dependency.
Information about GRF, used in the game and (part of it) in savegames.
std::string filename
Filename - either with or without full path.
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:316
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
Basic functions to receive and send TCP packets to/from the content server.
PacketContentType
Enum with all types of TCP content packets.
Definition tcp_content.h:23
@ ClientContent
Request a content file given an internal ID.
Definition tcp_content.h:29
@ ClientInfoExternalID
Queries the content server for information about a list of external IDs.
Definition tcp_content.h:26
@ ClientInfoID
Queries the content server for information about a list of internal IDs.
Definition tcp_content.h:25
@ ClientInfoExternalIDMD5
Queries the content server for information about a list of external IDs and MD5.
Definition tcp_content.h:27
@ ServerInfo
Reply of content server with information about content.
Definition tcp_content.h:28
@ ServerContent
Reply with the content of the given ID.
Definition tcp_content.h:30
@ ClientInfoList
Queries the content server for a list of info of a given content type.
Definition tcp_content.h:24
ContentType
The values in the enum are important; they are received over the network from the content servers.
@ BaseSounds
The content consists of base sounds.
@ Begin
Helper to mark the begin of the types.
@ GsLibrary
The content consists of a GS library.
@ Scenario
The content consists of a scenario.
@ Heightmap
The content consists of a heightmap.
@ BaseGraphics
The content consists of base graphics.
@ Ai
The content consists of an AI.
@ End
Helper to mark the end of the types.
@ BaseMusic
The content consists of base music.
@ Gs
The content consists of a game script.
@ NewGRF
The content consists of a NewGRF.
@ AiLibrary
The content consists of an AI library.
GUI functions related to textfiles.
TextfileType
Additional text files accompanying Tar archives.