OpenTTD
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 #include "../safeguards.h"
27 
28 extern bool HasScenario(const ContentInfo *ci, bool md5sum);
29 
32 
34 static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
35 {
36  return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? ci->md5sum : nullptr) != nullptr;
37 }
38 
46 typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);
47 
49 {
50  ContentInfo *ci = new ContentInfo();
51  ci->type = (ContentType)p->Recv_uint8();
52  ci->id = (ContentID)p->Recv_uint32();
53  ci->filesize = p->Recv_uint32();
54 
55  p->Recv_string(ci->name, lengthof(ci->name));
56  p->Recv_string(ci->version, lengthof(ci->version));
57  p->Recv_string(ci->url, lengthof(ci->url));
59 
60  ci->unique_id = p->Recv_uint32();
61  for (uint j = 0; j < sizeof(ci->md5sum); j++) {
62  ci->md5sum[j] = p->Recv_uint8();
63  }
64 
65  ci->dependency_count = p->Recv_uint8();
66  ci->dependencies = MallocT<ContentID>(ci->dependency_count);
67  for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
68 
69  ci->tag_count = p->Recv_uint8();
70  ci->tags = MallocT<char[32]>(ci->tag_count);
71  for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
72 
73  if (!ci->IsValid()) {
74  delete ci;
75  this->Close();
76  return false;
77  }
78 
79  /* Find the appropriate check function */
80  HasProc proc = nullptr;
81  switch (ci->type) {
83  proc = HasGRFConfig;
84  break;
85 
87  proc = BaseGraphics::HasSet;
88  break;
89 
91  proc = BaseMusic::HasSet;
92  break;
93 
95  proc = BaseSounds::HasSet;
96  break;
97 
98  case CONTENT_TYPE_AI:
99  proc = AI::HasAI; break;
100  break;
101 
103  proc = AI::HasAILibrary; break;
104  break;
105 
106  case CONTENT_TYPE_GAME:
107  proc = Game::HasGame; break;
108  break;
109 
111  proc = Game::HasGameLibrary; break;
112  break;
113 
116  proc = HasScenario;
117  break;
118 
119  default:
120  break;
121  }
122 
123  if (proc != nullptr) {
124  if (proc(ci, true)) {
126  } else {
128  if (proc(ci, false)) ci->upgrade = true;
129  }
130  } else {
132  }
133 
134  /* Something we don't have and has filesize 0 does not exist in the system */
136 
137  /* Do we already have a stub for this? */
138  for (ContentInfo *ici : this->infos) {
139  if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
140  memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
141  /* Preserve the name if possible */
142  if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
143  if (ici->IsSelected()) ci->state = ici->state;
144 
145  /*
146  * As ici might be selected by the content window we cannot delete that.
147  * However, we want to keep most of the values of ci, except the values
148  * we (just) already preserved.
149  * So transfer data and ownership of allocated memory from ci to ici.
150  */
151  ici->TransferFrom(ci);
152  delete ci;
153 
154  this->OnReceiveContentInfo(ici);
155  return true;
156  }
157  }
158 
159  /* Missing content info? Don't list it */
160  if (ci->filesize == 0) {
161  delete ci;
162  return true;
163  }
164 
165  this->infos.push_back(ci);
166 
167  /* Incoming data means that we might need to reconsider dependencies */
168  for (ContentInfo *ici : this->infos) {
169  this->CheckDependencyState(ici);
170  }
171 
172  this->OnReceiveContentInfo(ci);
173 
174  return true;
175 }
176 
182 {
183  if (type == CONTENT_TYPE_END) {
194  return;
195  }
196 
197  this->Connect();
198 
200  p->Send_uint8 ((byte)type);
201  p->Send_uint32(_openttd_newgrf_version);
202 
203  this->SendPacket(p);
204 }
205 
212 {
213  this->Connect();
214 
215  while (count > 0) {
216  /* We can "only" send a limited number of IDs in a single packet.
217  * A packet begins with the packet size and a byte for the type.
218  * Then this packet adds a uint16 for the count in this packet.
219  * The rest of the packet can be used for the IDs. */
220  uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
221 
223  p->Send_uint16(p_count);
224 
225  for (uint i = 0; i < p_count; i++) {
226  p->Send_uint32(content_ids[i]);
227  }
228 
229  this->SendPacket(p);
230  count -= p_count;
231  content_ids += p_count;
232  }
233 }
234 
241 {
242  if (cv == nullptr) return;
243 
244  this->Connect();
245 
246  assert(cv->size() < 255);
247  assert(cv->size() < (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) /
248  (sizeof(uint8) + sizeof(uint32) + (send_md5sum ? /*sizeof(ContentInfo::md5sum)*/16 : 0)));
249 
251  p->Send_uint8((uint8)cv->size());
252 
253  for (const ContentInfo *ci : *cv) {
254  p->Send_uint8((byte)ci->type);
255  p->Send_uint32(ci->unique_id);
256  if (!send_md5sum) continue;
257 
258  for (uint j = 0; j < sizeof(ci->md5sum); j++) {
259  p->Send_uint8(ci->md5sum[j]);
260  }
261  }
262 
263  this->SendPacket(p);
264 
265  for (ContentInfo *ci : *cv) {
266  bool found = false;
267  for (ContentInfo *ci2 : this->infos) {
268  if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
269  (!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
270  found = true;
271  break;
272  }
273  }
274  if (!found) {
275  this->infos.push_back(ci);
276  } else {
277  delete ci;
278  }
279  }
280 }
281 
288 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
289 {
290  bytes = 0;
291 
292  ContentIDList content;
293  for (const ContentInfo *ci : this->infos) {
294  if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
295 
296  content.push_back(ci->id);
297  bytes += ci->filesize;
298  }
299 
300  files = (uint)content.size();
301 
302  /* If there's nothing to download, do nothing. */
303  if (files == 0) return;
304 
306  this->DownloadSelectedContentFallback(content);
307  } else {
308  this->DownloadSelectedContentHTTP(content);
309  }
310 }
311 
317 {
318  uint count = (uint)content.size();
319 
320  /* Allocate memory for the whole request.
321  * Requests are "id\nid\n..." (as strings), so assume the maximum ID,
322  * which is uint32 so 10 characters long. Then the newlines and
323  * multiply that all with the count and then add the '\0'. */
324  uint bytes = (10 + 1) * count + 1;
325  char *content_request = MallocT<char>(bytes);
326  const char *lastof = content_request + bytes - 1;
327 
328  char *p = content_request;
329  for (const ContentID &id : content) {
330  p += seprintf(p, lastof, "%d\n", id);
331  }
332 
333  this->http_response_index = -1;
334 
336  new NetworkHTTPContentConnecter(address, this, NETWORK_CONTENT_MIRROR_URL, content_request);
337  /* NetworkHTTPContentConnecter takes over freeing of content_request! */
338 }
339 
345 {
346  uint count = (uint)content.size();
347  const ContentID *content_ids = content.data();
348  this->Connect();
349 
350  while (count > 0) {
351  /* We can "only" send a limited number of IDs in a single packet.
352  * A packet begins with the packet size and a byte for the type.
353  * Then this packet adds a uint16 for the count in this packet.
354  * The rest of the packet can be used for the IDs. */
355  uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
356 
358  p->Send_uint16(p_count);
359 
360  for (uint i = 0; i < p_count; i++) {
361  p->Send_uint32(content_ids[i]);
362  }
363 
364  this->SendPacket(p);
365  count -= p_count;
366  content_ids += p_count;
367  }
368 }
369 
377 static char *GetFullFilename(const ContentInfo *ci, bool compressed)
378 {
380  if (dir == NO_DIRECTORY) return nullptr;
381 
382  static char buf[MAX_PATH];
383  FioGetFullPath(buf, lastof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename);
384  strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf));
385 
386  return buf;
387 }
388 
394 static bool GunzipFile(const ContentInfo *ci)
395 {
396 #if defined(WITH_ZLIB)
397  bool ret = true;
398 
399  /* Need to open the file with fopen() to support non-ASCII on Windows. */
400  FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
401  if (ftmp == nullptr) return false;
402  /* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
403  gzFile fin = gzdopen(dup(fileno(ftmp)), "rb");
404  fclose(ftmp);
405 
406  FILE *fout = fopen(GetFullFilename(ci, false), "wb");
407 
408  if (fin == nullptr || fout == nullptr) {
409  ret = false;
410  } else {
411  byte buff[8192];
412  for (;;) {
413  int read = gzread(fin, buff, sizeof(buff));
414  if (read == 0) {
415  /* If gzread() returns 0, either the end-of-file has been
416  * reached or an underlying read error has occurred.
417  *
418  * gzeof() can't be used, because:
419  * 1.2.5 - it is safe, 1 means 'everything was OK'
420  * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
421  * 1.2.3.3 - 1 is returned for truncated archive
422  *
423  * So we use gzerror(). When proper end of archive
424  * has been reached, then:
425  * errnum == Z_STREAM_END in 1.2.3.3,
426  * errnum == 0 in 1.2.4 and 1.2.5 */
427  int errnum;
428  gzerror(fin, &errnum);
429  if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
430  break;
431  }
432  if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
433  /* If gzread() returns -1, there was an error in archive */
434  ret = false;
435  break;
436  }
437  /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
438  * if (read < sizeof(buff)) break; */
439  }
440  }
441 
442  if (fin != nullptr) gzclose(fin);
443  if (fout != nullptr) fclose(fout);
444 
445  return ret;
446 #else
447  NOT_REACHED();
448 #endif /* defined(WITH_ZLIB) */
449 }
450 
452 {
453  if (this->curFile == nullptr) {
454  delete this->curInfo;
455  /* When we haven't opened a file this must be our first packet with metadata. */
456  this->curInfo = new ContentInfo;
457  this->curInfo->type = (ContentType)p->Recv_uint8();
458  this->curInfo->id = (ContentID)p->Recv_uint32();
459  this->curInfo->filesize = p->Recv_uint32();
460  p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
461 
462  if (!this->BeforeDownload()) {
463  this->Close();
464  return false;
465  }
466  } else {
467  /* We have a file opened, thus are downloading internal content */
468  size_t toRead = (size_t)(p->size - p->pos);
469  if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
471  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
472  this->Close();
473  fclose(this->curFile);
474  this->curFile = nullptr;
475 
476  return false;
477  }
478 
479  this->OnDownloadProgress(this->curInfo, (int)toRead);
480 
481  if (toRead == 0) this->AfterDownload();
482  }
483 
484  return true;
485 }
486 
492 {
493  if (!this->curInfo->IsValid()) {
494  delete this->curInfo;
495  this->curInfo = nullptr;
496  return false;
497  }
498 
499  if (this->curInfo->filesize != 0) {
500  /* The filesize is > 0, so we are going to download it */
501  const char *filename = GetFullFilename(this->curInfo, true);
502  if (filename == nullptr || (this->curFile = fopen(filename, "wb")) == nullptr) {
503  /* Unless that fails of course... */
505  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
506  return false;
507  }
508  }
509  return true;
510 }
511 
517 {
518  /* We read nothing; that's our marker for end-of-stream.
519  * Now gunzip the tar and make it known. */
520  fclose(this->curFile);
521  this->curFile = nullptr;
522 
523  if (GunzipFile(this->curInfo)) {
524  unlink(GetFullFilename(this->curInfo, true));
525 
527  if (sd == NO_DIRECTORY) NOT_REACHED();
528 
529  TarScanner ts;
530  ts.AddFile(sd, GetFullFilename(this->curInfo, false));
531 
532  if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
533  /* Music can't be in a tar. So extract the tar! */
535  unlink(GetFullFilename(this->curInfo, false));
536  }
537 
538  this->OnDownloadComplete(this->curInfo->id);
539  } else {
540  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
541  }
542 }
543 
544 /* Also called to just clean up the mess. */
546 {
547  /* If we fail, download the rest via the 'old' system. */
548  uint files, bytes;
549  this->DownloadSelectedContent(files, bytes, true);
550 
551  this->http_response.clear();
552  this->http_response.shrink_to_fit();
553  this->http_response_index = -2;
554 
555  if (this->curFile != nullptr) {
556  /* Revert the download progress when we are going for the old system. */
557  long size = ftell(this->curFile);
558  if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
559 
560  fclose(this->curFile);
561  this->curFile = nullptr;
562  }
563 }
564 
565 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
566 {
567  assert(data == nullptr || length != 0);
568 
569  /* Ignore any latent data coming from a connection we closed. */
570  if (this->http_response_index == -2) return;
571 
572  if (this->http_response_index == -1) {
573  if (data != nullptr) {
574  /* Append the rest of the response. */
575  this->http_response.insert(this->http_response.end(), data, data + length);
576  return;
577  } else {
578  /* Make sure the response is properly terminated. */
579  this->http_response.push_back('\0');
580 
581  /* And prepare for receiving the rest of the data. */
582  this->http_response_index = 0;
583  }
584  }
585 
586  if (data != nullptr) {
587  /* We have data, so write it to the file. */
588  if (fwrite(data, 1, length, this->curFile) != length) {
589  /* Writing failed somehow, let try via the old method. */
590  this->OnFailure();
591  } else {
592  /* Just received the data. */
593  this->OnDownloadProgress(this->curInfo, (int)length);
594  }
595  /* Nothing more to do now. */
596  return;
597  }
598 
599  if (this->curFile != nullptr) {
600  /* We've finished downloading a file. */
601  this->AfterDownload();
602  }
603 
604  if ((uint)this->http_response_index >= this->http_response.size()) {
605  /* It's not a real failure, but if there's
606  * nothing more to download it helps with
607  * cleaning up the stuff we allocated. */
608  this->OnFailure();
609  return;
610  }
611 
612  delete this->curInfo;
613  /* When we haven't opened a file this must be our first packet with metadata. */
614  this->curInfo = new ContentInfo;
615 
617 #define check_not_null(p) { if ((p) == nullptr) { this->OnFailure(); return; } }
618 
619 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
620 
621  for (;;) {
622  char *str = this->http_response.data() + this->http_response_index;
623  char *p = strchr(str, '\n');
624  check_and_terminate(p);
625 
626  /* Update the index for the next one */
627  this->http_response_index += (int)strlen(str) + 1;
628 
629  /* Read the ID */
630  p = strchr(str, ',');
631  check_and_terminate(p);
632  this->curInfo->id = (ContentID)atoi(str);
633 
634  /* Read the type */
635  str = p + 1;
636  p = strchr(str, ',');
637  check_and_terminate(p);
638  this->curInfo->type = (ContentType)atoi(str);
639 
640  /* Read the file size */
641  str = p + 1;
642  p = strchr(str, ',');
643  check_and_terminate(p);
644  this->curInfo->filesize = atoi(str);
645 
646  /* Read the URL */
647  str = p + 1;
648  /* Is it a fallback URL? If so, just continue with the next one. */
649  if (strncmp(str, "ottd", 4) == 0) {
650  if ((uint)this->http_response_index >= this->http_response.size()) {
651  /* Have we gone through all lines? */
652  this->OnFailure();
653  return;
654  }
655  continue;
656  }
657 
658  p = strrchr(str, '/');
659  check_not_null(p);
660  p++; // Start after the '/'
661 
662  char tmp[MAX_PATH];
663  if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
664  this->OnFailure();
665  return;
666  }
667  /* Remove the extension from the string. */
668  for (uint i = 0; i < 2; i++) {
669  p = strrchr(tmp, '.');
670  check_and_terminate(p);
671  }
672 
673  /* Copy the string, without extension, to the filename. */
674  strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
675 
676  /* Request the next file. */
677  if (!this->BeforeDownload()) {
678  this->OnFailure();
679  return;
680  }
681 
683  return;
684  }
685 
686 #undef check
687 #undef check_and_terminate
688 }
689 
696  curFile(nullptr),
697  curInfo(nullptr),
698  isConnecting(false),
700 {
701 }
702 
705 {
706  delete this->curInfo;
707  if (this->curFile != nullptr) fclose(this->curFile);
708 
709  for (ContentInfo *ci : this->infos) delete ci;
710 }
711 
714 public:
720 
721  void OnFailure() override
722  {
723  _network_content_client.isConnecting = false;
724  _network_content_client.OnConnect(false);
725  }
726 
727  void OnConnect(SOCKET s) override
728  {
729  assert(_network_content_client.sock == INVALID_SOCKET);
730  _network_content_client.isConnecting = false;
731  _network_content_client.sock = s;
732  _network_content_client.Reopen();
733  _network_content_client.OnConnect(true);
734  }
735 };
736 
741 {
743 
744  if (this->sock != INVALID_SOCKET || this->isConnecting) return;
745  this->isConnecting = true;
747 }
748 
753 {
754  if (this->sock == INVALID_SOCKET) return;
756 
757  this->OnDisconnect();
758 }
759 
765 {
766  if (this->sock == INVALID_SOCKET || this->isConnecting) return;
767 
768  if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
769  this->Close();
770  return;
771  }
772 
773  if (this->CanSendReceive()) {
774  if (this->ReceivePackets()) {
775  /* Only update activity once a packet is received, instead of every time we try it. */
777  }
778  }
779 
780  this->SendPackets();
781 }
782 
788 {
789  /* When we tried to download it already, don't try again */
790  if (std::find(this->requested.begin(), this->requested.end(), cid) != this->requested.end()) return;
791 
792  this->requested.push_back(cid);
793  this->RequestContentList(1, &cid);
794 }
795 
802 {
803  for (ContentInfo *ci : this->infos) {
804  if (ci->id == cid) return ci;
805  }
806  return nullptr;
807 }
808 
809 
815 {
816  ContentInfo *ci = this->GetContent(cid);
817  if (ci == nullptr || ci->state != ContentInfo::UNSELECTED) return;
818 
820  this->CheckDependencyState(ci);
821 }
822 
828 {
829  ContentInfo *ci = this->GetContent(cid);
830  if (ci == nullptr || !ci->IsSelected()) return;
831 
833  this->CheckDependencyState(ci);
834 }
835 
838 {
839  for (ContentInfo *ci : this->infos) {
840  if (ci->state == ContentInfo::UNSELECTED) {
841  ci->state = ContentInfo::SELECTED;
842  this->CheckDependencyState(ci);
843  }
844  }
845 }
846 
849 {
850  for (ContentInfo *ci : this->infos) {
851  if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
852  ci->state = ContentInfo::SELECTED;
853  this->CheckDependencyState(ci);
854  }
855  }
856 }
857 
860 {
861  for (ContentInfo *ci : this->infos) {
862  if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
863  }
864 }
865 
868 {
869  switch (ci->state) {
872  this->Unselect(ci->id);
873  break;
874 
876  this->Select(ci->id);
877  break;
878 
879  default:
880  break;
881  }
882 }
883 
890 {
891  for (const ContentInfo * const &ci : this->infos) {
892  if (ci == child) continue;
893 
894  for (uint i = 0; i < ci->dependency_count; i++) {
895  if (ci->dependencies[i] == child->id) {
896  parents.push_back(ci);
897  break;
898  }
899  }
900  }
901 }
902 
909 {
910  tree.push_back(child);
911 
912  /* First find all direct parents. We can't use the "normal" iterator as
913  * we are including stuff into the vector and as such the vector's data
914  * store can be reallocated (and thus move), which means out iterating
915  * pointer gets invalid. So fall back to the indices. */
916  for (uint i = 0; i < tree.size(); i++) {
917  ConstContentVector parents;
918  this->ReverseLookupDependency(parents, tree[i]);
919 
920  for (const ContentInfo *ci : parents) {
921  include(tree, ci);
922  }
923  }
924 }
925 
931 {
932  if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
933  /* Selection is easy; just walk all children and set the
934  * autoselected state. That way we can see what we automatically
935  * selected and thus can unselect when a dependency is removed. */
936  for (uint i = 0; i < ci->dependency_count; i++) {
937  ContentInfo *c = this->GetContent(ci->dependencies[i]);
938  if (c == nullptr) {
939  this->DownloadContentInfo(ci->dependencies[i]);
940  } else if (c->state == ContentInfo::UNSELECTED) {
942  this->CheckDependencyState(c);
943  }
944  }
945  return;
946  }
947 
948  if (ci->state != ContentInfo::UNSELECTED) return;
949 
950  /* For unselection we need to find the parents of us. We need to
951  * unselect them. After that we unselect all children that we
952  * depend on and are not used as dependency for us, but only when
953  * we automatically selected them. */
954  ConstContentVector parents;
955  this->ReverseLookupDependency(parents, ci);
956  for (const ContentInfo *c : parents) {
957  if (!c->IsSelected()) continue;
958 
959  this->Unselect(c->id);
960  }
961 
962  for (uint i = 0; i < ci->dependency_count; i++) {
963  const ContentInfo *c = this->GetContent(ci->dependencies[i]);
964  if (c == nullptr) {
966  continue;
967  }
968  if (c->state != ContentInfo::AUTOSELECTED) continue;
969 
970  /* Only unselect when WE are the only parent. */
971  parents.clear();
972  this->ReverseLookupDependency(parents, c);
973 
974  /* First check whether anything depends on us */
975  int sel_count = 0;
976  bool force_selection = false;
977  for (const ContentInfo *ci : parents) {
978  if (ci->IsSelected()) sel_count++;
979  if (ci->state == ContentInfo::SELECTED) force_selection = true;
980  }
981  if (sel_count == 0) {
982  /* Nothing depends on us */
983  this->Unselect(c->id);
984  continue;
985  }
986  /* Something manually selected depends directly on us */
987  if (force_selection) continue;
988 
989  /* "Flood" search to find all items in the dependency graph*/
990  parents.clear();
991  this->ReverseLookupTreeDependency(parents, c);
992 
993  /* Is there anything that is "force" selected?, if so... we're done. */
994  for (const ContentInfo *ci : parents) {
995  if (ci->state != ContentInfo::SELECTED) continue;
996 
997  force_selection = true;
998  break;
999  }
1000 
1001  /* So something depended directly on us */
1002  if (force_selection) continue;
1003 
1004  /* Nothing depends on us, mark the whole graph as unselected.
1005  * After that's done run over them once again to test their children
1006  * to unselect. Don't do it immediately because it'll do exactly what
1007  * we're doing now. */
1008  for (const ContentInfo *c : parents) {
1009  if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
1010  }
1011  for (const ContentInfo *c : parents) {
1012  this->CheckDependencyState(this->GetContent(c->id));
1013  }
1014  }
1015 }
1016 
1019 {
1020  for (ContentInfo *c : this->infos) delete c;
1021 
1022  this->infos.clear();
1023  this->requested.clear();
1024 }
1025 
1026 /*** CALLBACK ***/
1027 
1029 {
1030  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1031  ContentCallback *cb = this->callbacks[i];
1032  /* the callback may remove itself from this->callbacks */
1033  cb->OnConnect(success);
1034  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1035  }
1036 }
1037 
1039 {
1040  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1041  ContentCallback *cb = this->callbacks[i];
1042  cb->OnDisconnect();
1043  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1044  }
1045 }
1046 
1048 {
1049  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1050  ContentCallback *cb = this->callbacks[i];
1051  /* the callback may add items and/or remove itself from this->callbacks */
1052  cb->OnReceiveContentInfo(ci);
1053  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1054  }
1055 }
1056 
1058 {
1059  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1060  ContentCallback *cb = this->callbacks[i];
1061  cb->OnDownloadProgress(ci, bytes);
1062  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1063  }
1064 }
1065 
1067 {
1068  ContentInfo *ci = this->GetContent(cid);
1069  if (ci != nullptr) {
1071  }
1072 
1073  for (size_t i = 0; i < this->callbacks.size(); /* nothing */) {
1074  ContentCallback *cb = this->callbacks[i];
1075  /* the callback may remove itself from this->callbacks */
1076  cb->OnDownloadComplete(cid);
1077  if (i != this->callbacks.size() && this->callbacks[i] == cb) i++;
1078  }
1079 }
void Close() override
Disconnect from the content server.
Helper to mark the end of the types.
Definition: tcp_content.h:33
ContentVector infos
All content info we received.
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
The content consists of base graphics.
Definition: tcp_content.h:23
bool IsSelected() const
Is the state either selected or autoselected?
Definition: tcp_content.cpp:73
Connect with a HTTP server and do ONE query.
Definition: tcp_http.h:75
void DownloadSelectedContent(uint &files, uint &bytes, bool fallback=false)
Actually begin downloading the content we selected.
char filename[48]
Filename (for the .tar.gz; only valid on download)
Definition: tcp_content.h:68
SOCKET sock
The socket currently connected to.
Definition: tcp.h:32
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:73
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:48
Internal entity of a packet.
Definition: packet.h:40
std::vector< char > http_response
The HTTP response to the requests we&#39;ve been doing.
Queries the content server for information about a list of external IDs.
Definition: tcp_content.h:40
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:97
bool Receive_SERVER_INFO(Packet *p) override
Server sending list of content info: byte type (invalid ID == does not exist) uint32 id uint32 file_s...
Socket handler for the content server connection.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:246
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:50
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:62
static bool GunzipFile(const ContentInfo *ci)
Gunzip a given file and remove the .gz if successful.
Base socket handler for all Content TCP sockets.
Definition: tcp_content.h:96
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
std::vector< ContentCallback * > callbacks
Callbacks to notify "the world".
Callbacks for notifying others about incoming data.
uint32 filesize
Size of the file.
Definition: tcp_content.h:67
void Connect()
Connect with the content server.
The content consists of a scenario.
Definition: tcp_content.h:27
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
void OnDownloadComplete(ContentID cid) override
We have finished downloading a file.
uint8 dependency_count
Number of dependencies.
Definition: tcp_content.h:75
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:96
char(* tags)[32]
Malloced array of tags (strings)
Definition: tcp_content.h:78
static bool HasGame(const struct ContentInfo *ci, bool md5sum)
Wrapper function for GameScanner::HasGame.
Definition: game_core.cpp:264
bool(* HasProc)(const ContentInfo *ci, bool md5sum)
Check whether a function piece of content is locally known.
void SendReceive()
Check whether we received/can send some data from/to the content server and when that&#39;s the case hand...
void ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
Reverse lookup the dependencies of all parents over a given child.
void OnDisconnect() override
Callback for when the connection got disconnected.
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:27
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:380
ContentID * dependencies
Malloced array of dependencies (unique server side ids)
Definition: tcp_content.h:76
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:92
The content has been selected as dependency.
Definition: tcp_content.h:59
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:117
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
void OnDownloadProgress(const ContentInfo *ci, int bytes) override
We have progress in the download of a file.
void OnConnect(bool success) override
Callback for when the connection has finished.
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
void OnFailure() override
An error has occurred and the connection has been closed.
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
ContentID
Unique identifier for the content.
Definition: tcp_content.h:49
The content has not been selected.
Definition: tcp_content.h:57
Queries the content server for information about a list of internal IDs.
Definition: tcp_content.h:39
void Reopen()
Reopen the socket so we can send/receive stuff again.
Definition: core.h:72
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:95
void Unselect(ContentID cid)
Unselect a specific content id.
The content does not exist in the content system.
Definition: tcp_content.h:61
bool ReceivePackets()
Receive a packet at TCP level.
void UnselectAll()
Unselect everything that we&#39;ve not downloaded so far.
bool isConnecting
Whether we&#39;re connecting.
static int Connect(char *uri, HTTPCallback *callback, const char *data=nullptr, int depth=0)
Connect to the given URI.
Definition: tcp_http.cpp:193
char name[32]
Name of the content.
Definition: tcp_content.h:69
virtual void OnDisconnect()
Callback for when the connection got disconnected.
The content consists of a game script.
Definition: tcp_content.h:31
NetworkSettings network
settings related to the network
std::vector< ContentID > ContentIDList
List of content IDs to (possibly) select.
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:52
std::vector< ContentInfo * > ContentVector
Vector with content info.
uint8 tag_count
Number of tags.
Definition: tcp_content.h:77
char version[16]
Version of the content.
Definition: tcp_content.h:70
void SelectUpgrade()
Select everything that&#39;s an update for something we&#39;ve got.
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:106
uint32 lastActivity
The last time there was network activity.
static const char *const NETWORK_CONTENT_SERVER_HOST
DNS hostname of the content server.
Definition: config.h:18
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
static char * GetFullFilename(const ContentInfo *ci, bool compressed)
Determine the full filename of a piece of content information.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
A path without any base directory.
Definition: fileio_type.h:125
The content is already at the client side.
Definition: tcp_content.h:60
ContentIDList requested
ContentIDs we already requested (so we don&#39;t do it again)
Allow newlines.
Definition: string_type.h:51
ContentID id
Unique (server side) ID for the content.
Definition: tcp_content.h:66
Connect to the content server.
void Clear()
Clear all downloaded content information.
void RequestContentList(ContentType type)
Request the content list for the given type.
static const char *const NETWORK_CONTENT_MIRROR_HOST
DNS hostname of the HTTP-content mirror server.
Definition: config.h:20
State state
Whether the content info is selected (for download)
Definition: tcp_content.h:79
byte * buf
Buffer we&#39;re going to write to/read from.
Search within the autodownload directory.
Definition: fileio_type.h:142
Part of the network protocol handling content distribution.
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:48
void OnReceiveContentInfo(const ContentInfo *ci) override
We received a content info.
void Close() override
Really close the socket.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
static const int IDLE_TIMEOUT
The idle timeout; when to close the connection because it&#39;s idle.
Queries the content server for a list of info of a given content type.
Definition: tcp_content.h:38
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:74
The content consists of a GS library.
Definition: tcp_content.h:32
bool BeforeDownload()
Handle the opening of the file before downloading.
static bool HasAI(const struct ContentInfo *ci, bool md5sum)
Wrapper function for AIScanner::HasAI.
Definition: ai_core.cpp:367
ContentInfo * GetContent(ContentID cid)
Get the content info based on a ContentID.
The content consists of a NewGRF.
Definition: tcp_content.h:24
bool Receive_SERVER_CONTENT(Packet *p) override
Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string f...
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename=nullptr) override
Add a file with the given filename.
Definition: fileio.cpp:656
ContentType
The values in the enum are important; they are used as database &#39;keys&#39;.
Definition: tcp_content.h:21
Network status window; Window numbers:
Definition: window_type.h:485
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1162
Queries the content server for information about a list of external IDs and MD5.
Definition: tcp_content.h:41
void OnConnect(SOCKET s) override
Callback when the connection succeeded.
void Select(ContentID cid)
Select a specific content id.
The content consists of an AI library.
Definition: tcp_content.h:26
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:217
void SelectAll()
Select everything we can select.
void ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
Reverse lookup the dependencies of (direct) parents over a given child.
Request a content file given an internal ID.
Definition: tcp_content.h:43
static const char *const NETWORK_CONTENT_MIRROR_URL
URL of the HTTP mirror system.
Definition: config.h:22
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
The content consists of a heightmap.
Definition: tcp_content.h:28
The content consists of an AI.
Definition: tcp_content.h:25
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set...
ContentType type
Type of content.
Definition: tcp_content.h:65
bool upgrade
This item is an upgrade.
Definition: tcp_content.h:80
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
void OnFailure() override
Callback for when the connection attempt failed.
void AfterDownload()
Handle the closing and extracting of a file after downloading it has been done.
static const uint16 NETWORK_CONTENT_MIRROR_PORT
The default port of the content mirror (TCP)
Definition: config.h:28
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
The content consists of base music.
Definition: tcp_content.h:30
bool no_http_content_downloads
do not do content downloads over HTTP
virtual void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we&#39;ve got a given scenario based on its unique ID.
Definition: fios.cpp:761
ClientNetworkContentSocketHandler()
Create a socket handler to handle the connection.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Definition: config.h:33
NetworkContentConnecter(const NetworkAddress &address)
Initiate the connecting.
ContentInfo * curInfo
Information about the currently downloaded file.
virtual void OnConnect(bool success)
Callback for when the connection has finished.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
bool ExtractTar(const char *tar_filename, Subdirectory subdir)
Extract the tar with the given filename in the directory where the tar resides.
Definition: fileio.cpp:886
The content has been manually selected.
Definition: tcp_content.h:58
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:61
static const uint16 NETWORK_CONTENT_SERVER_PORT
The default port of the content server (TCP)
Definition: config.h:27
int http_response_index
Where we are, in the response, with handling it.
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
void OnReceiveData(const char *data, size_t length) override
We&#39;re receiving data.
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:225
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Only find Grfs matching md5sum.
Subdirectory GetContentInfoSubDir(ContentType type)
Helper to get the subdirectory a ContentInfo is located in.
char url[96]
URL related to the content.
Definition: tcp_content.h:71
static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
Wrapper function for the HasProc.
Errors (eg. saving/loading failed)
Definition: error.h:23
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
char description[512]
Description of the content.
Definition: tcp_content.h:72
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:19
FILE * curFile
Currently downloaded file.
Container for all important information about a piece of content.
Definition: tcp_content.h:54
bool IsValid() const
Is the information from this content info valid?
Definition: tcp_content.cpp:90
Network content download status.
Definition: window_type.h:33
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
Use first found.
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a &#39;\0&#39; in the stream.
Definition: packet.cpp:286
std::vector< const ContentInfo * > ConstContentVector
Vector with constant content info.
void ToggleSelectedState(const ContentInfo *ci)
Toggle the state of a content info and check its dependencies.
The content consists of base sounds.
Definition: tcp_content.h:29