OpenTTD Source  14.1
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 
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 
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 
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) {
91  proc = HasGRFConfig;
92  break;
93 
95  proc = BaseGraphics::HasSet;
96  break;
97 
99  proc = BaseMusic::HasSet;
100  break;
101 
103  proc = BaseSounds::HasSet;
104  break;
105 
106  case CONTENT_TYPE_AI:
107  proc = AI::HasAI; break;
108  break;
109 
111  proc = AI::HasAILibrary; break;
112  break;
113 
114  case CONTENT_TYPE_GAME:
115  proc = Game::HasGame; break;
116  break;
117 
119  proc = Game::HasGameLibrary; break;
120  break;
121 
124  proc = HasScenario;
125  break;
126 
127  default:
128  break;
129  }
130 
131  if (proc != nullptr) {
132  if (proc(ci, true)) {
134  } else {
136  if (proc(ci, false)) ci->upgrade = true;
137  }
138  } else {
140  }
141 
142  /* Something we don't have and has filesize 0 does not exist in the system */
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 
190 {
191  if (type == CONTENT_TYPE_END) {
202  return;
203  }
204 
205  this->Connect();
206 
207  auto p = std::make_unique<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(std::move(p));
225 }
226 
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  auto p = std::make_unique<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(std::move(p));
251  count -= p_count;
252  content_ids += p_count;
253  }
254 }
255 
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  auto p = std::make_unique<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(std::move(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 (fallback) {
329  this->DownloadSelectedContentFallback(content);
330  } else {
331  this->DownloadSelectedContentHTTP(content);
332  }
333 }
334 
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 
349 }
350 
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  auto p = std::make_unique<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(std::move(p));
376  count -= p_count;
377  content_ids += p_count;
378  }
379 }
380 
388 static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
389 {
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 
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();
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) {
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 
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... */
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 
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 
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 
579 {
580  return this->isCancelled;
581 }
582 
583 /* Also called to just clean up the mess. */
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  if (this->http_response_index == -1) {
615  if (data != nullptr) {
616  /* Append the rest of the response. */
617  this->http_response.insert(this->http_response.end(), data.get(), data.get() + length);
618  return;
619  } else {
620  /* Make sure the response is properly terminated. */
621  this->http_response.push_back('\0');
622 
623  /* And prepare for receiving the rest of the data. */
624  this->http_response_index = 0;
625  }
626  }
627 
628  if (data != nullptr) {
629  /* We have data, so write it to the file. */
630  if (fwrite(data.get(), 1, length, this->curFile) != length) {
631  /* Writing failed somehow, let try via the old method. */
632  this->OnFailure();
633  } else {
634  /* Just received the data. */
635  this->OnDownloadProgress(this->curInfo, (int)length);
636  }
637 
638  /* Nothing more to do now. */
639  return;
640  }
641 
642  if (this->curFile != nullptr) {
643  /* We've finished downloading a file. */
644  this->AfterDownload();
645  }
646 
647  if ((uint)this->http_response_index >= this->http_response.size()) {
648  /* It's not a real failure, but if there's
649  * nothing more to download it helps with
650  * cleaning up the stuff we allocated. */
651  this->OnFailure();
652  return;
653  }
654 
655  delete this->curInfo;
656  /* When we haven't opened a file this must be our first packet with metadata. */
657  this->curInfo = new ContentInfo;
658 
660 #define check_not_null(p) { if ((p) == nullptr) { this->OnFailure(); return; } }
661 
662 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
663 
664  for (;;) {
665  char *str = this->http_response.data() + this->http_response_index;
666  char *p = strchr(str, '\n');
667  check_and_terminate(p);
668 
669  /* Update the index for the next one */
670  this->http_response_index += (int)strlen(str) + 1;
671 
672  /* Read the ID */
673  p = strchr(str, ',');
674  check_and_terminate(p);
675  this->curInfo->id = (ContentID)atoi(str);
676 
677  /* Read the type */
678  str = p + 1;
679  p = strchr(str, ',');
680  check_and_terminate(p);
681  this->curInfo->type = (ContentType)atoi(str);
682 
683  /* Read the file size */
684  str = p + 1;
685  p = strchr(str, ',');
686  check_and_terminate(p);
687  this->curInfo->filesize = atoi(str);
688 
689  /* Read the URL */
690  str = p + 1;
691  /* Is it a fallback URL? If so, just continue with the next one. */
692  if (strncmp(str, "ottd", 4) == 0) {
693  if ((uint)this->http_response_index >= this->http_response.size()) {
694  /* Have we gone through all lines? */
695  this->OnFailure();
696  return;
697  }
698  continue;
699  }
700 
701  p = strrchr(str, '/');
702  check_not_null(p);
703  p++; // Start after the '/'
704 
705  std::string filename = p;
706  /* Remove the extension from the string. */
707  for (uint i = 0; i < 2; i++) {
708  auto pos = filename.find_last_of('.');
709  if (pos == std::string::npos) {
710  this->OnFailure();
711  return;
712  }
713  filename.erase(pos);
714  }
715 
716  /* Copy the string, without extension, to the filename. */
717  this->curInfo->filename = std::move(filename);
718 
719  /* Request the next file. */
720  if (!this->BeforeDownload()) {
721  this->OnFailure();
722  return;
723  }
724 
726  return;
727  }
728 
729 #undef check
730 #undef check_and_terminate
731 }
732 
738  http_response_index(-2),
739  curFile(nullptr),
740  curInfo(nullptr),
741  isConnecting(false),
742  isCancelled(false)
743 {
744  this->lastActivity = std::chrono::steady_clock::now();
745 }
746 
749 {
750  delete this->curInfo;
751  if (this->curFile != nullptr) fclose(this->curFile);
752 
753  for (ContentInfo *ci : this->infos) delete ci;
754 }
755 
758 public:
764 
765  void OnFailure() override
766  {
768  _network_content_client.OnConnect(false);
769  }
770 
771  void OnConnect(SOCKET s) override
772  {
773  assert(_network_content_client.sock == INVALID_SOCKET);
774  _network_content_client.lastActivity = std::chrono::steady_clock::now();
778  _network_content_client.OnConnect(true);
779  }
780 };
781 
786 {
787  if (this->sock != INVALID_SOCKET || this->isConnecting) return;
788 
789  this->isCancelled = false;
790  this->isConnecting = true;
791 
792  TCPConnecter::Create<NetworkContentConnecter>(NetworkContentServerConnectionString());
793 }
794 
799 {
801 
802  if (this->sock == INVALID_SOCKET) return NETWORK_RECV_STATUS_OKAY;
803 
804  this->CloseSocket();
805  this->OnDisconnect();
806 
808 }
809 
814 {
815  this->isCancelled = true;
816  this->CloseConnection();
817 }
818 
824 {
825  if (this->sock == INVALID_SOCKET || this->isConnecting) return;
826 
827  /* Close the connection to the content server after inactivity; there can still be downloads pending via HTTP. */
828  if (std::chrono::steady_clock::now() > this->lastActivity + IDLE_TIMEOUT) {
829  this->CloseConnection();
830  return;
831  }
832 
833  if (this->CanSendReceive()) {
834  if (this->ReceivePackets()) {
835  /* Only update activity once a packet is received, instead of every time we try it. */
836  this->lastActivity = std::chrono::steady_clock::now();
837  }
838  }
839 
840  this->SendPackets();
841 }
842 
848 {
849  /* When we tried to download it already, don't try again */
850  if (std::find(this->requested.begin(), this->requested.end(), cid) != this->requested.end()) return;
851 
852  this->requested.push_back(cid);
853  this->RequestContentList(1, &cid);
854 }
855 
862 {
863  for (ContentInfo *ci : this->infos) {
864  if (ci->id == cid) return ci;
865  }
866  return nullptr;
867 }
868 
869 
875 {
876  ContentInfo *ci = this->GetContent(cid);
877  if (ci == nullptr || ci->state != ContentInfo::UNSELECTED) return;
878 
880  this->CheckDependencyState(ci);
881 }
882 
888 {
889  ContentInfo *ci = this->GetContent(cid);
890  if (ci == nullptr || !ci->IsSelected()) return;
891 
893  this->CheckDependencyState(ci);
894 }
895 
898 {
899  for (ContentInfo *ci : this->infos) {
900  if (ci->state == ContentInfo::UNSELECTED) {
901  ci->state = ContentInfo::SELECTED;
902  this->CheckDependencyState(ci);
903  }
904  }
905 }
906 
909 {
910  for (ContentInfo *ci : this->infos) {
911  if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
912  ci->state = ContentInfo::SELECTED;
913  this->CheckDependencyState(ci);
914  }
915  }
916 }
917 
920 {
921  for (ContentInfo *ci : this->infos) {
922  if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
923  }
924 }
925 
928 {
929  switch (ci->state) {
932  this->Unselect(ci->id);
933  break;
934 
936  this->Select(ci->id);
937  break;
938 
939  default:
940  break;
941  }
942 }
943 
950 {
951  auto range = this->reverse_dependency_map.equal_range(child->id);
952 
953  for (auto iter = range.first; iter != range.second; ++iter) {
954  parents.push_back(GetContent(iter->second));
955  }
956 }
957 
964 {
965  tree.push_back(child);
966 
967  /* First find all direct parents. We can't use the "normal" iterator as
968  * we are including stuff into the vector and as such the vector's data
969  * store can be reallocated (and thus move), which means out iterating
970  * pointer gets invalid. So fall back to the indices. */
971  for (uint i = 0; i < tree.size(); i++) {
972  ConstContentVector parents;
973  this->ReverseLookupDependency(parents, tree[i]);
974 
975  for (const ContentInfo *ci : parents) {
976  include(tree, ci);
977  }
978  }
979 }
980 
986 {
987  if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
988  /* Selection is easy; just walk all children and set the
989  * autoselected state. That way we can see what we automatically
990  * selected and thus can unselect when a dependency is removed. */
991  for (auto &dependency : ci->dependencies) {
992  ContentInfo *c = this->GetContent(dependency);
993  if (c == nullptr) {
994  this->DownloadContentInfo(dependency);
995  } else if (c->state == ContentInfo::UNSELECTED) {
997  this->CheckDependencyState(c);
998  }
999  }
1000  return;
1001  }
1002 
1003  if (ci->state != ContentInfo::UNSELECTED) return;
1004 
1005  /* For unselection we need to find the parents of us. We need to
1006  * unselect them. After that we unselect all children that we
1007  * depend on and are not used as dependency for us, but only when
1008  * we automatically selected them. */
1009  ConstContentVector parents;
1010  this->ReverseLookupDependency(parents, ci);
1011  for (const ContentInfo *c : parents) {
1012  if (!c->IsSelected()) continue;
1013 
1014  this->Unselect(c->id);
1015  }
1016 
1017  for (auto &dependency : ci->dependencies) {
1018  const ContentInfo *c = this->GetContent(dependency);
1019  if (c == nullptr) {
1020  DownloadContentInfo(dependency);
1021  continue;
1022  }
1023  if (c->state != ContentInfo::AUTOSELECTED) continue;
1024 
1025  /* Only unselect when WE are the only parent. */
1026  parents.clear();
1027  this->ReverseLookupDependency(parents, c);
1028 
1029  /* First check whether anything depends on us */
1030  int sel_count = 0;
1031  bool force_selection = false;
1032  for (const ContentInfo *parent_ci : parents) {
1033  if (parent_ci->IsSelected()) sel_count++;
1034  if (parent_ci->state == ContentInfo::SELECTED) force_selection = true;
1035  }
1036  if (sel_count == 0) {
1037  /* Nothing depends on us */
1038  this->Unselect(c->id);
1039  continue;
1040  }
1041  /* Something manually selected depends directly on us */
1042  if (force_selection) continue;
1043 
1044  /* "Flood" search to find all items in the dependency graph*/
1045  parents.clear();
1046  this->ReverseLookupTreeDependency(parents, c);
1047 
1048  /* Is there anything that is "force" selected?, if so... we're done. */
1049  for (const ContentInfo *parent_ci : parents) {
1050  if (parent_ci->state != ContentInfo::SELECTED) continue;
1051 
1052  force_selection = true;
1053  break;
1054  }
1055 
1056  /* So something depended directly on us */
1057  if (force_selection) continue;
1058 
1059  /* Nothing depends on us, mark the whole graph as unselected.
1060  * After that's done run over them once again to test their children
1061  * to unselect. Don't do it immediately because it'll do exactly what
1062  * we're doing now. */
1063  for (const ContentInfo *parent : parents) {
1064  if (parent->state == ContentInfo::AUTOSELECTED) this->Unselect(parent->id);
1065  }
1066  for (const ContentInfo *parent : parents) {
1067  this->CheckDependencyState(this->GetContent(parent->id));
1068  }
1069  }
1070 }
1071 
1074 {
1075  for (ContentInfo *c : this->infos) delete c;
1076 
1077  this->infos.clear();
1078  this->requested.clear();
1079  this->reverse_dependency_map.clear();
1080 }
1081 
1082 /*** CALLBACK ***/
1083 
1084 void ClientNetworkContentSocketHandler::OnConnect(bool success)
1085 {
1086  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1087  ContentCallback *cb = this->callbacks[i];
1088  /* the callback may remove itself from this->callbacks */
1089  cb->OnConnect(success);
1090  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1091  }
1092 }
1093 
1095 {
1096  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1097  ContentCallback *cb = this->callbacks[i];
1098  cb->OnDisconnect();
1099  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1100  }
1101 }
1102 
1103 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
1104 {
1105  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1106  ContentCallback *cb = this->callbacks[i];
1107  /* the callback may add items and/or remove itself from this->callbacks */
1108  cb->OnReceiveContentInfo(ci);
1109  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1110  }
1111 }
1112 
1113 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
1114 {
1115  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1116  ContentCallback *cb = this->callbacks[i];
1117  cb->OnDownloadProgress(ci, bytes);
1118  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1119  }
1120 }
1121 
1122 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
1123 {
1124  ContentInfo *ci = this->GetContent(cid);
1125  if (ci != nullptr) {
1127  }
1128 
1129  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1130  ContentCallback *cb = this->callbacks[i];
1131  /* the callback may remove itself from this->callbacks */
1132  cb->OnDownloadComplete(cid);
1133  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1134  }
1135 }
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:763
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:687
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:949
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:823
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(std::unique_ptr< Packet > &&packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:68
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
Definition: error_gui.cpp:367
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_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
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
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:985
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:136
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
ClientNetworkContentSocketHandler::Clear
void Clear()
Clear all downloaded content information.
Definition: network_content.cpp:1073
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:185
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:383
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:1094
ContentInfo::UNSELECTED
@ UNSELECTED
The content has not been selected.
Definition: tcp_content_type.h:55
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:765
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:847
Packet::Recv_uint32
uint32_t Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:321
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
Definition: network_content.cpp:748
ClientNetworkContentSocketHandler::UnselectAll
void UnselectAll()
Unselect everything that we've not downloaded so far.
Definition: network_content.cpp:919
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:887
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:355
ClientNetworkContentSocketHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition: network_content.cpp:584
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:51
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:861
TCP_MTU
static const size_t TCP_MTU
Number of bytes we can pack in a single TCP packet.
Definition: config.h:45
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
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
PACKET_CONTENT_CLIENT_CONTENT
@ PACKET_CONTENT_CLIENT_CONTENT
Request a content file given an internal ID.
Definition: tcp_content_type.h:41
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
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
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.
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_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
CONTENT_TYPE_AI
@ CONTENT_TYPE_AI
The content consists of an AI.
Definition: tcp_content_type.h:22
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
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:86
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:200
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:963
ClientNetworkContentSocketHandler::Connect
void Connect()
Connect with the content server.
Definition: network_content.cpp:785
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:927
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:258
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:874
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:757
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
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
ClientNetworkContentSocketHandler::SelectAll
void SelectAll()
Select everything we can select.
Definition: network_content.cpp:897
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::Cancel
void Cancel()
Cancel the current download.
Definition: network_content.cpp:813
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
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
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::CloseConnection
NetworkRecvStatus CloseConnection(bool error=true) override
Disconnect from the content server.
Definition: network_content.cpp:798
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:908
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:405
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:736
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
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:39
ContentInfo::unique_id
uint32_t unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content_type.h:71
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:292
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