OpenTTD Source: src/network/network_content.cpp Source File

OpenTTD Source 14.0-beta1

network_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
10 #include "../stdafx.h"
11 #include "../rev.h"
12 #include "../ai/ai.hpp"
13 #include "../game/game.hpp"
14 #include "../window_func.h"
15 #include "../error.h"
16 #include "../base_media_base.h"
17 #include "../settings_type.h"
18 #include "network_content.h"
19
20 #include "table/strings.h"
21
22 #if defined(WITH_ZLIB)
23 #include <zlib.h>
24 #endif
25
26 #ifdef __EMSCRIPTEN__
27 # include <emscripten.h>
28 #endif
29
30 #include "../safeguards.h"
31
32 extern bool HasScenario(const ContentInfo *ci, bool md5sum);
33
35 ClientNetworkContentSocketHandler _network_content_client;
36
38 static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
39 {
40 return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? &ci->md5sum : nullptr) != nullptr;
41 }
42
50 typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);
51
52 bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p)
53 {
54 ContentInfo *ci = new ContentInfo();
55 ci->type = (ContentType)p->Recv_uint8();
56 ci->id = (ContentID)p->Recv_uint32();
57 ci->filesize = p->Recv_uint32();
58
59 ci->name = p->Recv_string(NETWORK_CONTENT_NAME_LENGTH);
60 ci->version = p->Recv_string(NETWORK_CONTENT_VERSION_LENGTH);
61 ci->url = p->Recv_string(NETWORK_CONTENT_URL_LENGTH);
62 ci->description = p->Recv_string(NETWORK_CONTENT_DESC_LENGTH, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
63
64 ci->unique_id = p->Recv_uint32();
65 for (size_t j = 0; j < ci->md5sum.size(); j++) {
66 ci->md5sum[j] = p->Recv_uint8();
67 }
68
69 uint dependency_count = p->Recv_uint8();
70 ci->dependencies.reserve(dependency_count);
71 for (uint i = 0; i < dependency_count; i++) {
72 ContentID dependency_cid = (ContentID)p->Recv_uint32();
73 ci->dependencies.push_back(dependency_cid);
74 this->reverse_dependency_map.insert({ dependency_cid, ci->id });
75 }
76
77 uint tag_count = p->Recv_uint8();
78 ci->tags.reserve(tag_count);
79 for (uint i = 0; i < tag_count; i++) ci->tags.push_back(p->Recv_string(NETWORK_CONTENT_TAG_LENGTH));
80
81 if (!ci->IsValid()) {
82 delete ci;
83 this->CloseConnection();
84 return false;
85 }
86
87 /* Find the appropriate check function */
88 HasProc proc = nullptr;
89 switch (ci->type) {
90 case CONTENT_TYPE_NEWGRF:
91 proc = HasGRFConfig;
92 break;
93
94 case CONTENT_TYPE_BASE_GRAPHICS:
95 proc = BaseGraphics::HasSet;
96 break;
97
98 case CONTENT_TYPE_BASE_MUSIC:
99 proc = BaseMusic::HasSet;
100 break;
101
102 case CONTENT_TYPE_BASE_SOUNDS:
103 proc = BaseSounds::HasSet;
104 break;
105
106 case CONTENT_TYPE_AI:
107 proc = AI::HasAI; break;
108 break;
109
110 case CONTENT_TYPE_AI_LIBRARY:
111 proc = AI::HasAILibrary; break;
112 break;
113
114 case CONTENT_TYPE_GAME:
115 proc = Game::HasGame; break;
116 break;
117
118 case CONTENT_TYPE_GAME_LIBRARY:
119 proc = Game::HasGameLibrary; break;
120 break;
121
122 case CONTENT_TYPE_SCENARIO:
123 case CONTENT_TYPE_HEIGHTMAP:
124 proc = HasScenario;
125 break;
126
127 default:
128 break;
129 }
130
131 if (proc != nullptr) {
132 if (proc(ci, true)) {
133 ci->state = ContentInfo::ALREADY_HERE;
134 } else {
135 ci->state = ContentInfo::UNSELECTED;
136 if (proc(ci, false)) ci->upgrade = true;
137 }
138 } else {
139 ci->state = ContentInfo::UNSELECTED;
140 }
141
142 /* Something we don't have and has filesize 0 does not exist in the system */
143 if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST;
144
145 /* Do we already have a stub for this? */
146 for (ContentInfo *ici : this->infos) {
147 if (ici->type == ci->type && ici->unique_id == ci->unique_id && ci->md5sum == ici->md5sum) {
148 /* Preserve the name if possible */
149 if (ci->name.empty()) ci->name = ici->name;
150 if (ici->IsSelected()) ci->state = ici->state;
151
152 /*
153 * As ici might be selected by the content window we cannot delete that.
154 * However, we want to keep most of the values of ci, except the values
155 * we (just) already preserved.
156 */
157 *ici = *ci;
158 delete ci;
159
160 this->OnReceiveContentInfo(ici);
161 return true;
162 }
163 }
164
165 /* Missing content info? Don't list it */
166 if (ci->filesize == 0) {
167 delete ci;
168 return true;
169 }
170
171 this->infos.push_back(ci);
172
173 /* Incoming data means that we might need to reconsider dependencies */
174 ConstContentVector parents;
175 this->ReverseLookupTreeDependency(parents, ci);
176 for (const ContentInfo *ici : parents) {
177 this->CheckDependencyState(const_cast<ContentInfo *>(ici));
178 }
179
180 this->OnReceiveContentInfo(ci);
181
182 return true;
183 }
184
189 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
190 {
191 if (type == CONTENT_TYPE_END) {
192 this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
193 this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
194 this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
195 this->RequestContentList(CONTENT_TYPE_SCENARIO);
196 this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
197 this->RequestContentList(CONTENT_TYPE_AI);
198 this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
199 this->RequestContentList(CONTENT_TYPE_GAME);
200 this->RequestContentList(CONTENT_TYPE_GAME_LIBRARY);
201 this->RequestContentList(CONTENT_TYPE_NEWGRF);
202 return;
203 }
204
205 this->Connect();
206
207 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_LIST);
208 p->Send_uint8 ((byte)type);
209 p->Send_uint32(0xffffffff);
210 p->Send_uint8 (1);
211 p->Send_string("vanilla");
212 p->Send_string(_openttd_content_version);
213
214 /* Patchpacks can extend the list with one. In BaNaNaS metadata you can
215 * add a branch in the 'compatibility' list, to filter on this. If you want
216 * your patchpack to be mentioned in the BaNaNaS web-interface, create an
217 * issue on https://github.com/OpenTTD/bananas-api asking for this.
218
219 p->Send_string("patchpack"); // Or what-ever the name of your patchpack is.
220 p->Send_string(_openttd_content_version_patchpack);
221
222 */
223
224 this->SendPacket(p);
225 }
226
232 void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
233 {
234 this->Connect();
235
236 while (count > 0) {
237 /* We can "only" send a limited number of IDs in a single packet.
238 * A packet begins with the packet size and a byte for the type.
239 * Then this packet adds a uint16_t for the count in this packet.
240 * The rest of the packet can be used for the IDs. */
241 uint p_count = std::min<uint>(count, (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16_t)) / sizeof(uint32_t));
242
243 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_ID, TCP_MTU);
244 p->Send_uint16(p_count);
245
246 for (uint i = 0; i < p_count; i++) {
247 p->Send_uint32(content_ids[i]);
248 }
249
250 this->SendPacket(p);
251 count -= p_count;
252 content_ids += p_count;
253 }
254 }
255
261 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
262 {
263 if (cv == nullptr) return;
264
265 this->Connect();
266
267 assert(cv->size() < 255);
268 assert(cv->size() < (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8_t)) /
269 (sizeof(uint8_t) + sizeof(uint32_t) + (send_md5sum ? MD5_HASH_BYTES : 0)));
270
271 Packet *p = new Packet(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID, TCP_MTU);
272 p->Send_uint8((uint8_t)cv->size());
273
274 for (const ContentInfo *ci : *cv) {
275 p->Send_uint8((byte)ci->type);
276 p->Send_uint32(ci->unique_id);
277 if (!send_md5sum) continue;
278
279 for (size_t j = 0; j < ci->md5sum.size(); j++) {
280 p->Send_uint8(ci->md5sum[j]);
281 }
282 }
283
284 this->SendPacket(p);
285
286 for (ContentInfo *ci : *cv) {
287 bool found = false;
288 for (ContentInfo *ci2 : this->infos) {
289 if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
290 (!send_md5sum || ci->md5sum == ci2->md5sum)) {
291 found = true;
292 break;
293 }
294 }
295 if (!found) {
296 this->infos.push_back(ci);
297 } else {
298 delete ci;
299 }
300 }
301 }
302
309 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
310 {
311 bytes = 0;
312
313 ContentIDList content;
314 for (const ContentInfo *ci : this->infos) {
315 if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
316
317 content.push_back(ci->id);
318 bytes += ci->filesize;
319 }
320
321 files = (uint)content.size();
322
323 /* If there's nothing to download, do nothing. */
324 if (files == 0) return;
325
326 this->isCancelled = false;
327
328 if (_settings_client.network.no_http_content_downloads || fallback) {
329 this->DownloadSelectedContentFallback(content);
330 } else {
331 this->DownloadSelectedContentHTTP(content);
332 }
333 }
334
339 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
340 {
341 std::string content_request;
342 for (const ContentID &id : content) {
343 content_request += std::to_string(id) + "\n";
344 }
345
346 this->http_response_index = -1;
347
348 NetworkHTTPSocketHandler::Connect(NetworkContentMirrorUriString(), this, content_request);
349 }
350
355 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
356 {
357 uint count = (uint)content.size();
358 const ContentID *content_ids = content.data();
359 this->Connect();
360
361 while (count > 0) {
362 /* We can "only" send a limited number of IDs in a single packet.
363 * A packet begins with the packet size and a byte for the type.
364 * Then this packet adds a uint16_t for the count in this packet.
365 * The rest of the packet can be used for the IDs. */
366 uint p_count = std::min<uint>(count, (TCP_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16_t)) / sizeof(uint32_t));
367
368 Packet *p = new Packet(PACKET_CONTENT_CLIENT_CONTENT, TCP_MTU);
369 p->Send_uint16(p_count);
370
371 for (uint i = 0; i < p_count; i++) {
372 p->Send_uint32(content_ids[i]);
373 }
374
375 this->SendPacket(p);
376 count -= p_count;
377 content_ids += p_count;
378 }
379 }
380
388 static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
389 {
390 Subdirectory dir = GetContentInfoSubDir(ci->type);
391 if (dir == NO_DIRECTORY) return {};
392
393 std::string buf = FioGetDirectory(SP_AUTODOWNLOAD_DIR, dir);
394 buf += ci->filename;
395 buf += compressed ? ".tar.gz" : ".tar";
396
397 return buf;
398 }
399
405 static bool GunzipFile(const ContentInfo *ci)
406 {
407 #if defined(WITH_ZLIB)
408 bool ret = true;
409
410 /* Need to open the file with fopen() to support non-ASCII on Windows. */
411 FILE *ftmp = fopen(GetFullFilename(ci, true).c_str(), "rb");
412 if (ftmp == nullptr) return false;
413 /* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
414 int fdup = dup(fileno(ftmp));
415 gzFile fin = gzdopen(fdup, "rb");
416 fclose(ftmp);
417
418 FILE *fout = fopen(GetFullFilename(ci, false).c_str(), "wb");
419
420 if (fin == nullptr || fout == nullptr) {
421 ret = false;
422 } else {
423 byte buff[8192];
424 for (;;) {
425 int read = gzread(fin, buff, sizeof(buff));
426 if (read == 0) {
427 /* If gzread() returns 0, either the end-of-file has been
428 * reached or an underlying read error has occurred.
429 *
430 * gzeof() can't be used, because:
431 * 1.2.5 - it is safe, 1 means 'everything was OK'
432 * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
433 * 1.2.3.3 - 1 is returned for truncated archive
434 *
435 * So we use gzerror(). When proper end of archive
436 * has been reached, then:
437 * errnum == Z_STREAM_END in 1.2.3.3,
438 * errnum == 0 in 1.2.4 and 1.2.5 */
439 int errnum;
440 gzerror(fin, &errnum);
441 if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
442 break;
443 }
444 if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
445 /* If gzread() returns -1, there was an error in archive */
446 ret = false;
447 break;
448 }
449 /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
450 * if (read < sizeof(buff)) break; */
451 }
452 }
453
454 if (fin != nullptr) {
455 gzclose(fin);
456 } else if (fdup != -1) {
457 /* Failing gzdopen does not close the passed file descriptor. */
458 close(fdup);
459 }
460 if (fout != nullptr) fclose(fout);
461
462 return ret;
463 #else
464 NOT_REACHED();
465 #endif /* defined(WITH_ZLIB) */
466 }
467
475 static inline ssize_t TransferOutFWrite(FILE *file, const char *buffer, size_t amount)
476 {
477 return fwrite(buffer, 1, amount, file);
478 }
479
480 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p)
481 {
482 if (this->curFile == nullptr) {
483 delete this->curInfo;
484 /* When we haven't opened a file this must be our first packet with metadata. */
485 this->curInfo = new ContentInfo;
486 this->curInfo->type = (ContentType)p->Recv_uint8();
487 this->curInfo->id = (ContentID)p->Recv_uint32();
488 this->curInfo->filesize = p->Recv_uint32();
489 this->curInfo->filename = p->Recv_string(NETWORK_CONTENT_FILENAME_LENGTH);
490
491 if (!this->BeforeDownload()) {
492 this->CloseConnection();
493 return false;
494 }
495 } else {
496 /* We have a file opened, thus are downloading internal content */
497 size_t toRead = p->RemainingBytesToTransfer();
498 if (toRead != 0 && (size_t)p->TransferOut(TransferOutFWrite, this->curFile) != toRead) {
499 CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
500 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
501 this->CloseConnection();
502 fclose(this->curFile);
503 this->curFile = nullptr;
504
505 return false;
506 }
507
508 this->OnDownloadProgress(this->curInfo, (int)toRead);
509
510 if (toRead == 0) this->AfterDownload();
511 }
512
513 return true;
514 }
515
520 bool ClientNetworkContentSocketHandler::BeforeDownload()
521 {
522 if (!this->curInfo->IsValid()) {
523 delete this->curInfo;
524 this->curInfo = nullptr;
525 return false;
526 }
527
528 if (this->curInfo->filesize != 0) {
529 /* The filesize is > 0, so we are going to download it */
530 std::string filename = GetFullFilename(this->curInfo, true);
531 if (filename.empty() || (this->curFile = fopen(filename.c_str(), "wb")) == nullptr) {
532 /* Unless that fails of course... */
533 CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
534 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
535 return false;
536 }
537 }
538 return true;
539 }
540
545 void ClientNetworkContentSocketHandler::AfterDownload()
546 {
547 /* We read nothing; that's our marker for end-of-stream.
548 * Now gunzip the tar and make it known. */
549 fclose(this->curFile);
550 this->curFile = nullptr;
551
552 if (GunzipFile(this->curInfo)) {
553 unlink(GetFullFilename(this->curInfo, true).c_str());
554
555 Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
556 if (sd == NO_DIRECTORY) NOT_REACHED();
557
558 TarScanner ts;
559 std::string fname = GetFullFilename(this->curInfo, false);
560 ts.AddFile(sd, fname);
561
562 if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
563 /* Music can't be in a tar. So extract the tar! */
564 ExtractTar(fname, BASESET_DIR);
565 unlink(fname.c_str());
566 }
567
568 #ifdef __EMSCRIPTEN__
569 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
570 #endif
571
572 this->OnDownloadComplete(this->curInfo->id);
573 } else {
574 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
575 }
576 }
577
578 bool ClientNetworkContentSocketHandler::IsCancelled() const
579 {
580 return this->isCancelled;
581 }
582
583 /* Also called to just clean up the mess. */
584 void ClientNetworkContentSocketHandler::OnFailure()
585 {
586 this->http_response.clear();
587 this->http_response.shrink_to_fit();
588 this->http_response_index = -2;
589
590 if (this->curFile != nullptr) {
591 this->OnDownloadProgress(this->curInfo, -1);
592
593 fclose(this->curFile);
594 this->curFile = nullptr;
595 }
596
597 /* If we fail, download the rest via the 'old' system. */
598 if (!this->isCancelled) {
599 uint files, bytes;
600
601 this->DownloadSelectedContent(files, bytes, true);
602 }
603 }
604
605 void ClientNetworkContentSocketHandler::OnReceiveData(std::unique_ptr<char[]> data, size_t length)
606 {
607 assert(data.get() == nullptr || length != 0);
608
609 /* Ignore any latent data coming from a connection we closed. */
610 if (this->http_response_index == -2) {
611 return;
612 }
613
614 this->lastActivity = std::chrono::steady_clock::now();
615
616 if (this->http_response_index == -1) {
617 if (data != nullptr) {
618 /* Append the rest of the response. */
619 this->http_response.insert(this->http_response.end(), data.get(), data.get() + length);
620 return;
621 } else {
622 /* Make sure the response is properly terminated. */
623 this->http_response.push_back('\0');
624
625 /* And prepare for receiving the rest of the data. */
626 this->http_response_index = 0;
627 }
628 }
629
630 if (data != nullptr) {
631 /* We have data, so write it to the file. */
632 if (fwrite(data.get(), 1, length, this->curFile) != length) {
633 /* Writing failed somehow, let try via the old method. */
634 this->OnFailure();
635 } else {
636 /* Just received the data. */
637 this->OnDownloadProgress(this->curInfo, (int)length);
638 }
639
640 /* Nothing more to do now. */
641 return;
642 }
643
644 if (this->curFile != nullptr) {
645 /* We've finished downloading a file. */
646 this->AfterDownload();
647 }
648
649 if ((uint)this->http_response_index >= this->http_response.size()) {
650 /* It's not a real failure, but if there's
651 * nothing more to download it helps with
652 * cleaning up the stuff we allocated. */
653 this->OnFailure();
654 return;
655 }
656
657 delete this->curInfo;
658 /* When we haven't opened a file this must be our first packet with metadata. */
659 this->curInfo = new ContentInfo;
660
662 #define check_not_null(p) { if ((p) == nullptr) { this->OnFailure(); return; } }
663
664 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
665
666 for (;;) {
667 char *str = this->http_response.data() + this->http_response_index;
668 char *p = strchr(str, '\n');
669 check_and_terminate(p);
670
671 /* Update the index for the next one */
672 this->http_response_index += (int)strlen(str) + 1;
673
674 /* Read the ID */
675 p = strchr(str, ',');
676 check_and_terminate(p);
677 this->curInfo->id = (ContentID)atoi(str);
678
679 /* Read the type */
680 str = p + 1;
681 p = strchr(str, ',');
682 check_and_terminate(p);
683 this->curInfo->type = (ContentType)atoi(str);
684
685 /* Read the file size */
686 str = p + 1;
687 p = strchr(str, ',');
688 check_and_terminate(p);
689 this->curInfo->filesize = atoi(str);
690
691 /* Read the URL */
692 str = p + 1;
693 /* Is it a fallback URL? If so, just continue with the next one. */
694 if (strncmp(str, "ottd", 4) == 0) {
695 if ((uint)this->http_response_index >= this->http_response.size()) {
696 /* Have we gone through all lines? */
697 this->OnFailure();
698 return;
699 }
700 continue;
701 }
702
703 p = strrchr(str, '/');
704 check_not_null(p);
705 p++; // Start after the '/'
706
707 std::string filename = p;
708 /* Remove the extension from the string. */
709 for (uint i = 0; i < 2; i++) {
710 auto pos = filename.find_last_of('.');
711 if (pos == std::string::npos) {
712 this->OnFailure();
713 return;
714 }
715 filename.erase(pos);
716 }
717
718 /* Copy the string, without extension, to the filename. */
719 this->curInfo->filename = std::move(filename);
720
721 /* Request the next file. */
722 if (!this->BeforeDownload()) {
723 this->OnFailure();
724 return;
725 }
726
727 NetworkHTTPSocketHandler::Connect(str, this);
728 return;
729 }
730
731 #undef check
732 #undef check_and_terminate
733 }
734
738 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
739 NetworkContentSocketHandler(),
740 http_response_index(-2),
741 curFile(nullptr),
742 curInfo(nullptr),
743 isConnecting(false),
744 isCancelled(false)
745 {
746 this->lastActivity = std::chrono::steady_clock::now();
747 }
748
750 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
751 {
752 delete this->curInfo;
753 if (this->curFile != nullptr) fclose(this->curFile);
754
755 for (ContentInfo *ci : this->infos) delete ci;
756 }
757
759 class NetworkContentConnecter : public TCPConnecter {
760 public:
765 NetworkContentConnecter(const std::string &connection_string) : TCPConnecter(connection_string, NETWORK_CONTENT_SERVER_PORT) {}
766
767 void OnFailure() override
768 {
769 _network_content_client.isConnecting = false;
770 _network_content_client.OnConnect(false);
771 }
772
773 void OnConnect(SOCKET s) override
774 {
775 assert(_network_content_client.sock == INVALID_SOCKET);
776 _network_content_client.lastActivity = std::chrono::steady_clock::now();
777 _network_content_client.isConnecting = false;
778 _network_content_client.sock = s;
779 _network_content_client.Reopen();
780 _network_content_client.OnConnect(true);
781 }
782 };
783
787 void ClientNetworkContentSocketHandler::Connect()
788 {
789 if (this->sock != INVALID_SOCKET || this->isConnecting) return;
790
791 this->isCancelled = false;
792 this->isConnecting = true;
793
794 TCPConnecter::Create<NetworkContentConnecter>(NetworkContentServerConnectionString());
795 }
796
800 NetworkRecvStatus ClientNetworkContentSocketHandler::CloseConnection(bool)
801 {
802 this->isCancelled = true;
803 NetworkContentSocketHandler::CloseConnection();
804
805 if (this->sock == INVALID_SOCKET) return NETWORK_RECV_STATUS_OKAY;
806
807 this->CloseSocket();
808 this->OnDisconnect();
809
810 return NETWORK_RECV_STATUS_OKAY;
811 }
812
817 void ClientNetworkContentSocketHandler::SendReceive()
818 {
819 if (this->sock == INVALID_SOCKET || this->isConnecting) return;
820
821 if (std::chrono::steady_clock::now() > this->lastActivity + IDLE_TIMEOUT) {
822 this->CloseConnection();
823 return;
824 }
825
826 if (this->CanSendReceive()) {
827 if (this->ReceivePackets()) {
828 /* Only update activity once a packet is received, instead of every time we try it. */
829 this->lastActivity = std::chrono::steady_clock::now();
830 }
831 }
832
833 this->SendPackets();
834 }
835
840 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
841 {
842 /* When we tried to download it already, don't try again */
843 if (std::find(this->requested.begin(), this->requested.end(), cid) != this->requested.end()) return;
844
845 this->requested.push_back(cid);
846 this->RequestContentList(1, &cid);
847 }
848
854 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid) const
855 {
856 for (ContentInfo *ci : this->infos) {
857 if (ci->id == cid) return ci;
858 }
859 return nullptr;
860 }
861
862
867 void ClientNetworkContentSocketHandler::Select(ContentID cid)
868 {
869 ContentInfo *ci = this->GetContent(cid);
870 if (ci == nullptr || ci->state != ContentInfo::UNSELECTED) return;
871
872 ci->state = ContentInfo::SELECTED;
873 this->CheckDependencyState(ci);
874 }
875
880 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
881 {
882 ContentInfo *ci = this->GetContent(cid);
883 if (ci == nullptr || !ci->IsSelected()) return;
884
885 ci->state = ContentInfo::UNSELECTED;
886 this->CheckDependencyState(ci);
887 }
888
890 void ClientNetworkContentSocketHandler::SelectAll()
891 {
892 for (ContentInfo *ci : this->infos) {
893 if (ci->state == ContentInfo::UNSELECTED) {
894 ci->state = ContentInfo::SELECTED;
895 this->CheckDependencyState(ci);
896 }
897 }
898 }
899
901 void ClientNetworkContentSocketHandler::SelectUpgrade()
902 {
903 for (ContentInfo *ci : this->infos) {
904 if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
905 ci->state = ContentInfo::SELECTED;
906 this->CheckDependencyState(ci);
907 }
908 }
909 }
910
912 void ClientNetworkContentSocketHandler::UnselectAll()
913 {
914 for (ContentInfo *ci : this->infos) {
915 if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
916 }
917 }
918
920 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
921 {
922 switch (ci->state) {
923 case ContentInfo::SELECTED:
924 case ContentInfo::AUTOSELECTED:
925 this->Unselect(ci->id);
926 break;
927
928 case ContentInfo::UNSELECTED:
929 this->Select(ci->id);
930 break;
931
932 default:
933 break;
934 }
935 }
936
942 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
943 {
944 auto range = this->reverse_dependency_map.equal_range(child->id);
945
946 for (auto iter = range.first; iter != range.second; ++iter) {
947 parents.push_back(GetContent(iter->second));
948 }
949 }
950
956 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
957 {
958 tree.push_back(child);
959
960 /* First find all direct parents. We can't use the "normal" iterator as
961 * we are including stuff into the vector and as such the vector's data
962 * store can be reallocated (and thus move), which means out iterating
963 * pointer gets invalid. So fall back to the indices. */
964 for (uint i = 0; i < tree.size(); i++) {
965 ConstContentVector parents;
966 this->ReverseLookupDependency(parents, tree[i]);
967
968 for (const ContentInfo *ci : parents) {
969 include(tree, ci);
970 }
971 }
972 }
973
978 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
979 {
980 if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
981 /* Selection is easy; just walk all children and set the
982 * autoselected state. That way we can see what we automatically
983 * selected and thus can unselect when a dependency is removed. */
984 for (auto &dependency : ci->dependencies) {
985 ContentInfo *c = this->GetContent(dependency);
986 if (c == nullptr) {
987 this->DownloadContentInfo(dependency);
988 } else if (c->state == ContentInfo::UNSELECTED) {
989 c->state = ContentInfo::AUTOSELECTED;
990 this->CheckDependencyState(c);
991 }
992 }
993 return;
994 }
995
996 if (ci->state != ContentInfo::UNSELECTED) return;
997
998 /* For unselection we need to find the parents of us. We need to
999 * unselect them. After that we unselect all children that we
1000 * depend on and are not used as dependency for us, but only when
1001 * we automatically selected them. */
1002 ConstContentVector parents;
1003 this->ReverseLookupDependency(parents, ci);
1004 for (const ContentInfo *c : parents) {
1005 if (!c->IsSelected()) continue;
1006
1007 this->Unselect(c->id);
1008 }
1009
1010 for (auto &dependency : ci->dependencies) {
1011 const ContentInfo *c = this->GetContent(dependency);
1012 if (c == nullptr) {
1013 DownloadContentInfo(dependency);
1014 continue;
1015 }
1016 if (c->state != ContentInfo::AUTOSELECTED) continue;
1017
1018 /* Only unselect when WE are the only parent. */
1019 parents.clear();
1020 this->ReverseLookupDependency(parents, c);
1021
1022 /* First check whether anything depends on us */
1023 int sel_count = 0;
1024 bool force_selection = false;
1025 for (const ContentInfo *parent_ci : parents) {
1026 if (parent_ci->IsSelected()) sel_count++;
1027 if (parent_ci->state == ContentInfo::SELECTED) force_selection = true;
1028 }
1029 if (sel_count == 0) {
1030 /* Nothing depends on us */
1031 this->Unselect(c->id);
1032 continue;
1033 }
1034 /* Something manually selected depends directly on us */
1035 if (force_selection) continue;
1036
1037 /* "Flood" search to find all items in the dependency graph*/
1038 parents.clear();
1039 this->ReverseLookupTreeDependency(parents, c);
1040
1041 /* Is there anything that is "force" selected?, if so... we're done. */
1042 for (const ContentInfo *parent_ci : parents) {
1043 if (parent_ci->state != ContentInfo::SELECTED) continue;
1044
1045 force_selection = true;
1046 break;
1047 }
1048
1049 /* So something depended directly on us */
1050 if (force_selection) continue;
1051
1052 /* Nothing depends on us, mark the whole graph as unselected.
1053 * After that's done run over them once again to test their children
1054 * to unselect. Don't do it immediately because it'll do exactly what
1055 * we're doing now. */
1056 for (const ContentInfo *parent : parents) {
1057 if (parent->state == ContentInfo::AUTOSELECTED) this->Unselect(parent->id);
1058 }
1059 for (const ContentInfo *parent : parents) {
1060 this->CheckDependencyState(this->GetContent(parent->id));
1061 }
1062 }
1063 }
1064
1066 void ClientNetworkContentSocketHandler::Clear()
1067 {
1068 for (ContentInfo *c : this->infos) delete c;
1069
1070 this->infos.clear();
1071 this->requested.clear();
1072 this->reverse_dependency_map.clear();
1073 }
1074
1075 /*** CALLBACK ***/
1076
1077 void ClientNetworkContentSocketHandler::OnConnect(bool success)
1078 {
1079 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1080 ContentCallback *cb = this->callbacks[i];
1081 /* the callback may remove itself from this->callbacks */
1082 cb->OnConnect(success);
1083 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1084 }
1085 }
1086
1087 void ClientNetworkContentSocketHandler::OnDisconnect()
1088 {
1089 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1090 ContentCallback *cb = this->callbacks[i];
1091 cb->OnDisconnect();
1092 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1093 }
1094 }
1095
1096 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
1097 {
1098 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1099 ContentCallback *cb = this->callbacks[i];
1100 /* the callback may add items and/or remove itself from this->callbacks */
1101 cb->OnReceiveContentInfo(ci);
1102 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1103 }
1104 }
1105
1106 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
1107 {
1108 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1109 ContentCallback *cb = this->callbacks[i];
1110 cb->OnDownloadProgress(ci, bytes);
1111 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1112 }
1113 }
1114
1115 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
1116 {
1117 ContentInfo *ci = this->GetContent(cid);
1118 if (ci != nullptr) {
1119 ci->state = ContentInfo::ALREADY_HERE;
1120 }
1121
1122 for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1123 ContentCallback *cb = this->callbacks[i];
1124 /* the callback may remove itself from this->callbacks */
1125 cb->OnDownloadComplete(cid);
1126 if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1127 }
1128 }
ContentInfo::IsSelected
bool IsSelected() const
Is the state either selected or autoselected?
Definition: tcp_content.cpp:27
network_content.h
ContentCallback::OnDownloadProgress
virtual void OnDownloadProgress([[maybe_unused]] const ContentInfo *ci, [[maybe_unused]] int bytes)
We have progress in the download of a file.
Definition: network_content.h:52
NetworkContentConnecter::NetworkContentConnecter
NetworkContentConnecter(const std::string &connection_string)
Initiate the connecting.
Definition: network_content.cpp:765
SP_AUTODOWNLOAD_DIR
@ SP_AUTODOWNLOAD_DIR
Search within the autodownload directory.
Definition: fileio_type.h:143
ContentCallback
Callbacks for notifying others about incoming data.
Definition: network_content.h:29
ContentCallback::OnReceiveContentInfo
virtual void OnReceiveContentInfo([[maybe_unused]] const ContentInfo *ci)
We received a content info.
Definition: network_content.h:45
ContentInfo::name
std::string name
Name of the content.
Definition: tcp_content_type.h:67
ContentCallback::OnConnect
virtual void OnConnect([[maybe_unused]] bool success)
Callback for when the connection has finished.
Definition: network_content.h:34
ExtractTar
bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition: fileio.cpp:677
ContentInfo::type
ContentType type
Type of content.
Definition: tcp_content_type.h:63
NetworkContentSocketHandler
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:22
ClientNetworkContentSocketHandler::ReverseLookupDependency
void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
Reverse lookup the dependencies of (direct) parents over a given child.
Definition: network_content.cpp:942
SVS_ALLOW_NEWLINE
@ SVS_ALLOW_NEWLINE
Allow newlines; replaces '\r ' with ' ' during processing.
Definition: string_type.h:47
TCPConnecter::connection_string
std::string connection_string
Current address we are connecting to (before resolving).
Definition: tcp.h:99
ClientNetworkContentSocketHandler::SendReceive
void SendReceive()
Check whether we received/can send some data from/to the content server and when that's the case hand...
Definition: network_content.cpp:817
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
Definition: error_gui.cpp:367
CONTENT_TYPE_GAME_LIBRARY
@ CONTENT_TYPE_GAME_LIBRARY
The content consists of a GS library.
Definition: tcp_content_type.h:29
ClientNetworkContentSocketHandler::OnReceiveData
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
Definition: network_content.cpp:605
ContentInfo::upgrade
bool upgrade
This item is an upgrade.
Definition: tcp_content_type.h:76
ContentInfo::DOES_NOT_EXIST
@ DOES_NOT_EXIST
The content does not exist in the content system.
Definition: tcp_content_type.h:59
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:70
ContentInfo::filesize
uint32_t filesize
Size of the file.
Definition: tcp_content_type.h:65
HasScenario
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition: fios.cpp:708
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1141
ClientNetworkContentSocketHandler::lastActivity
std::chrono::steady_clock::time_point lastActivity
The last time there was network activity.
Definition: network_content.h:81
ClientNetworkContentSocketHandler::http_response_index
int http_response_index
Where we are, in the response, with handling it.
Definition: network_content.h:75
CONTENT_TYPE_NEWGRF
@ CONTENT_TYPE_NEWGRF
The content consists of a NewGRF.
Definition: tcp_content_type.h:21
ContentVector
std::vector< ContentInfo * > ContentVector
Vector with content info.
Definition: network_content.h:19
NetworkHTTPSocketHandler::Connect
static void Connect(const std::string &uri, HTTPCallback *callback, const std::string data="")
Connect to the given URI.
Definition: http_curl.cpp:93
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:38
NetworkContentSocketHandler::ReceivePackets
bool ReceivePackets()
Receive a packet at TCP level.
Definition: tcp_content.cpp:128
ContentInfo::url
std::string url
URL related to the content.
Definition: tcp_content_type.h:69
ClientNetworkContentSocketHandler::CheckDependencyState
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
Definition: network_content.cpp:978
NETWORK_CONTENT_VERSION_LENGTH
static const uint NETWORK_CONTENT_VERSION_LENGTH
The maximum length of a content's version, in bytes including '\0'.
Definition: config.h:66
ClientNetworkContentSocketHandler
Socket handler for the content server connection.
Definition: network_content.h:67
ClientNetworkContentSocketHandler::reverse_dependency_map
std::unordered_multimap< ContentID, ContentID > reverse_dependency_map
Content reverse dependency map.
Definition: network_content.h:73
Packet::TransferOut
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
Definition: packet.h:141
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
CONTENT_TYPE_END
@ CONTENT_TYPE_END
Helper to mark the end of the types.
Definition: tcp_content_type.h:30
ContentInfo::version
std::string version
Version of the content.
Definition: tcp_content_type.h:68
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:39
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition: network_content.cpp:1066
ClientNetworkContentSocketHandler::isConnecting
bool isConnecting
Whether we're connecting.
Definition: network_content.h:79
ContentInfo::md5sum
MD5Hash md5sum
The MD5 checksum.
Definition: tcp_content_type.h:72
HasGRFConfig
static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
Wrapper function for the HasProc.
Definition: network_content.cpp:38
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:18
ClientNetworkContentSocketHandler::curInfo
ContentInfo * curInfo
Information about the currently downloaded file.
Definition: network_content.h:78
ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
Definition: network_content.cpp:339
GetContentInfoSubDir
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
Definition: tcp_content.cpp:186
NetworkSettings::no_http_content_downloads
bool no_http_content_downloads
do not do content downloads over HTTP
Definition: settings_type.h:333
Packet::Recv_string
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...
Definition: packet.cpp:408
ClientNetworkContentSocketHandler::RequestContentList
void RequestContentList(ContentType type)
Request the content list for the given type.
Definition: network_content.cpp:189
FGCM_ANY
@ FGCM_ANY
Use first found.
Definition: newgrf_config.h:196
include
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Definition: container_func.hpp:24
GetFullFilename
static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
Determine the full filename of a piece of content information.
Definition: network_content.cpp:388
CONTENT_TYPE_GAME
@ CONTENT_TYPE_GAME
The content consists of a game script.
Definition: tcp_content_type.h:28
ClientNetworkContentSocketHandler::OnDisconnect
void OnDisconnect() override
Callback for when the connection got disconnected.
Definition: network_content.cpp:1087
Packet::Send_uint32
void Send_uint32(uint32_t data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:150
ContentInfo::UNSELECTED
@ UNSELECTED
The content has not been selected.
Definition: tcp_content_type.h:55
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:37
Packet::Send_uint8
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:129
BSWAP32
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:345
NetworkContentConnecter::OnFailure
void OnFailure() override
Callback for when the connection attempt failed.
Definition: network_content.cpp:767
NetworkSocketHandler::Reopen
void Reopen()
Reopen the socket so we can send/receive stuff again.
Definition: core.h:73
NETWORK_CONTENT_DESC_LENGTH
static const uint NETWORK_CONTENT_DESC_LENGTH
The maximum length of a content's description, in bytes including '\0'.
Definition: config.h:68
ClientNetworkContentSocketHandler::infos
ContentVector infos
All content info we received.
Definition: network_content.h:72
ClientNetworkContentSocketHandler::DownloadContentInfo
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
Definition: network_content.cpp:840
Packet::Recv_uint32
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:346
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
Definition: network_content.cpp:750
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition: network_content.cpp:912
TransferOutFWrite
static ssize_t TransferOutFWrite(FILE *file, const char *buffer, size_t amount)
Simple wrapper around fwrite to be able to pass it to Packet's TransferOut.
Definition: network_content.cpp:475
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:52
ClientNetworkContentSocketHandler::Unselect
void Unselect(ContentID cid)
Unselect a specific content id.
Definition: network_content.cpp:880
ClientNetworkContentSocketHandler::isCancelled
bool isCancelled
Whether the download has been cancelled.
Definition: network_content.h:80
ContentCallback::OnDownloadComplete
virtual void OnDownloadComplete([[maybe_unused]] ContentID cid)
We have finished downloading a file.
Definition: network_content.h:58
AI::HasAI
static bool HasAI(const struct ContentInfo *ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
Definition: ai_core.cpp:342
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition: network_content.cpp:584
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:81
NetworkContentServerConnectionString
const char * NetworkContentServerConnectionString()
Get the connection string for the content server from the environment variable OTTD_CONTENT_SERVER_CS...
Definition: config.cpp:55
NetworkTCPSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition: tcp.cpp:65
ContentInfo::SELECTED
@ SELECTED
The content has been manually selected.
Definition: tcp_content_type.h:56
ClientNetworkContentSocketHandler::IDLE_TIMEOUT
static constexpr std::chrono::seconds IDLE_TIMEOUT
The idle timeout; when to close the connection because it's idle.
Definition: network_content.h:108
ClientNetworkContentSocketHandler::GetContent
ContentInfo * GetContent(ContentID cid) const
Get the content info based on a ContentID.
Definition: network_content.cpp:854
ClientNetworkContentSocketHandler::curFile
FILE * curFile
Currently downloaded file.
Definition: network_content.h:77
HasProc
bool(* HasProc)(const ContentInfo *ci, bool md5sum)
Check whether a function piece of content is locally known.
Definition: network_content.cpp:50
PACKET_CONTENT_CLIENT_CONTENT
@ PACKET_CONTENT_CLIENT_CONTENT
Request a content file given an internal ID.
Definition: tcp_content_type.h:41
ContentInfo::tags
StringList tags
Tags associated with the content.
Definition: tcp_content_type.h:74
Packet
Internal entity of a packet.
Definition: packet.h:42
ClientNetworkContentSocketHandler::DownloadSelectedContent
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
Definition: network_content.cpp:309
PacketSize
uint16_t PacketSize
Size of the whole packet.
Definition: packet.h:20
ClientNetworkContentSocketHandler::http_response
std::vector< char > http_response
The HTTP response to the requests we've been doing.
Definition: network_content.h:74
NetworkContentMirrorUriString
const char * NetworkContentMirrorUriString()
Get the URI string for the content mirror from the environment variable OTTD_CONTENT_MIRROR_URI,...
Definition: config.cpp:65
ContentInfo::dependencies
std::vector< ContentID > dependencies
The dependencies (unique server side ids)
Definition: tcp_content_type.h:73
TarScanner::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename={}) override
Add a file with the given filename.
NETWORK_CONTENT_FILENAME_LENGTH
static const uint NETWORK_CONTENT_FILENAME_LENGTH
The maximum length of a content's filename, in bytes including '\0'.
Definition: config.h:64
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:38
CONTENT_TYPE_AI
@ CONTENT_TYPE_AI
The content consists of an AI.
Definition: tcp_content_type.h:22
ClientNetworkContentSocketHandler::IsCancelled
bool IsCancelled() const override
Check if there is a request to cancel the transfer.
Definition: network_content.cpp:578
_network_content_client
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Definition: network_content.cpp:35
ClientNetworkContentSocketHandler::ContentIDList
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
Definition: network_content.h:69
CONTENT_TYPE_BASE_GRAPHICS
@ CONTENT_TYPE_BASE_GRAPHICS
The content consists of base graphics.
Definition: tcp_content_type.h:20
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:218
ContentInfo::ALREADY_HERE
@ ALREADY_HERE
The content is already at the client side.
Definition: tcp_content_type.h:58
NETWORK_CONTENT_SERVER_PORT
static const uint16_t NETWORK_CONTENT_SERVER_PORT
The default port of the content server (TCP)
Definition: config.h:24
ClientNetworkContentSocketHandler::ReverseLookupTreeDependency
void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
Reverse lookup the dependencies of all parents over a given child.
Definition: network_content.cpp:956
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition: network_content.cpp:787
ContentInfo::AUTOSELECTED
@ AUTOSELECTED
The content has been selected as dependency.
Definition: tcp_content_type.h:57
CONTENT_TYPE_AI_LIBRARY
@ CONTENT_TYPE_AI_LIBRARY
The content consists of an AI library.
Definition: tcp_content_type.h:23
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:126
ClientNetworkContentSocketHandler::ToggleSelectedState
void ToggleSelectedState(const ContentInfo *ci)
Toggle the state of a content info and check its dependencies.
Definition: network_content.cpp:920
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
WC_NETWORK_STATUS_WINDOW
@ WC_NETWORK_STATUS_WINDOW
Network status window; Window numbers:
Definition: window_type.h:485
ContentID
ContentID
Unique identifier for the content.
Definition: tcp_content_type.h:47
ContentInfo::filename
std::string filename
Filename (for the .tar.gz; only valid on download)
Definition: tcp_content_type.h:66
ClientNetworkContentSocketHandler::callbacks
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Definition: network_content.h:70
Game::HasGame
static bool HasGame(const struct ContentInfo *ci, bool md5sum)
Wrapper function for GameScanner::HasGame.
Definition: game_core.cpp:259
BaseMedia< GraphicsSet >::HasSet
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
Definition: base_media_func.h:337
ClientNetworkContentSocketHandler::Select
void Select(ContentID cid)
Select a specific content id.
Definition: network_content.cpp:867
GunzipFile
static bool GunzipFile(const ContentInfo *ci)
Gunzip a given file and remove the .gz if successful.
Definition: network_content.cpp:405
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
ClientNetworkContentSocketHandler::requested
ContentIDList requested
ContentIDs we already requested (so we don't do it again)
Definition: network_content.h:71
NetworkContentConnecter
Connect to the content server.
Definition: network_content.cpp:759
ClientNetworkContentSocketHandler::DownloadSelectedContentFallback
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
Definition: network_content.cpp:355
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:26
ContentInfo::state
State state
Whether the content info is selected (for download)
Definition: tcp_content_type.h:75
Packet::Send_string
void Send_string(const std::string_view data)
Sends a string over the network.
Definition: packet.cpp:181
CONTENT_TYPE_BASE_SOUNDS
@ CONTENT_TYPE_BASE_SOUNDS
The content consists of base sounds.
Definition: tcp_content_type.h:26
TarScanner
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:59
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:637
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition: network_content.cpp:890
NETWORK_CONTENT_NAME_LENGTH
static const uint NETWORK_CONTENT_NAME_LENGTH
The maximum length of a content's name, in bytes including '\0'.
Definition: config.h:65
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:36
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
TCP_MTU
static const uint16_t TCP_MTU
Number of bytes we can pack in a single TCP packet.
Definition: config.h:45
CONTENT_TYPE_SCENARIO
@ CONTENT_TYPE_SCENARIO
The content consists of a scenario.
Definition: tcp_content_type.h:24
ClientNetworkContentSocketHandler::BeforeDownload
bool BeforeDownload()
Handle the opening of the file before downloading.
Definition: network_content.cpp:520
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:192
NETWORK_CONTENT_TAG_LENGTH
static const uint NETWORK_CONTENT_TAG_LENGTH
The maximum length of a content's tag, in bytes including '\0'.
Definition: config.h:69
ClientNetworkContentSocketHandler::Receive_SERVER_INFO
bool Receive_SERVER_INFO(Packet *p) override
Server sending list of content info: byte type (invalid ID == does not exist) uint32_t id uint32_t fi...
Definition: network_content.cpp:52
ClientNetworkContentSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
Definition: network_content.cpp:800
NETWORK_CONTENT_URL_LENGTH
static const uint NETWORK_CONTENT_URL_LENGTH
The maximum length of a content's url, in bytes including '\0'.
Definition: config.h:67
ClientNetworkContentSocketHandler::SelectUpgrade
void SelectUpgrade()
Select everything that's an update for something we've got.
Definition: network_content.cpp:901
ContentInfo::id
ContentID id
Unique (server side) ID for the content.
Definition: tcp_content_type.h:64
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition: packet.cpp:430
ClientNetworkContentSocketHandler::AfterDownload
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
Definition: network_content.cpp:545
ContentInfo::description
std::string description
Description of the content.
Definition: tcp_content_type.h:70
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:690
CONTENT_TYPE_HEIGHTMAP
@ CONTENT_TYPE_HEIGHTMAP
The content consists of a heightmap.
Definition: tcp_content_type.h:25
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler
ClientNetworkContentSocketHandler()
Create a socket handler to handle the connection.
Definition: network_content.cpp:738
ContentCallback::OnDisconnect
virtual void OnDisconnect()
Callback for when the connection got disconnected.
Definition: network_content.h:39
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:46
ConstContentVector
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
Definition: network_content.h:21
ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT
bool Receive_SERVER_CONTENT(Packet *p) override
Server sending list of content info: uint32_t unique id uint32_t file size (0 == does not exist) stri...
Definition: network_content.cpp:480
WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
@ WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD
Network content download status.
Definition: window_type.h:40
NetworkTCPSocketHandler::CloseSocket
void CloseSocket()
Close the actual socket of the connection.
Definition: tcp.cpp:53
ContentInfo::unique_id
uint32_t unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content_type.h:71
Packet::Send_uint16
void Send_uint16(uint16_t data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:139
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:317
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
CONTENT_TYPE_BASE_MUSIC
@ CONTENT_TYPE_BASE_MUSIC
The content consists of base music.
Definition: tcp_content_type.h:27