OpenTTD
network_content.cpp
Go to the documentation of this file.
1 /* $Id: network_content.cpp 27576 2016-05-22 10:45:46Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #if defined(ENABLE_NETWORK)
13 
14 #include "../stdafx.h"
15 #include "../rev.h"
16 #include "../ai/ai.hpp"
17 #include "../game/game.hpp"
18 #include "../window_func.h"
19 #include "../error.h"
20 #include "../base_media_base.h"
21 #include "../settings_type.h"
22 #include "network_content.h"
23 
24 #include "table/strings.h"
25 
26 #if defined(WITH_ZLIB)
27 #include <zlib.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 : NULL) != NULL;
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 
59  p->Recv_string(ci->name, lengthof(ci->name));
60  p->Recv_string(ci->version, lengthof(ci->version));
61  p->Recv_string(ci->url, lengthof(ci->url));
63 
64  ci->unique_id = p->Recv_uint32();
65  for (uint j = 0; j < sizeof(ci->md5sum); j++) {
66  ci->md5sum[j] = p->Recv_uint8();
67  }
68 
69  ci->dependency_count = p->Recv_uint8();
70  ci->dependencies = MallocT<ContentID>(ci->dependency_count);
71  for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
72 
73  ci->tag_count = p->Recv_uint8();
74  ci->tags = MallocT<char[32]>(ci->tag_count);
75  for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
76 
77  if (!ci->IsValid()) {
78  delete ci;
79  this->Close();
80  return false;
81  }
82 
83  /* Find the appropriate check function */
84  HasProc proc = NULL;
85  switch (ci->type) {
87  proc = HasGRFConfig;
88  break;
89 
91  proc = BaseGraphics::HasSet;
92  break;
93 
95  proc = BaseMusic::HasSet;
96  break;
97 
99  proc = BaseSounds::HasSet;
100  break;
101 
102  case CONTENT_TYPE_AI:
103  proc = AI::HasAI; break;
104  break;
105 
107  proc = AI::HasAILibrary; break;
108  break;
109 
110  case CONTENT_TYPE_GAME:
111  proc = Game::HasGame; break;
112  break;
113 
115  proc = Game::HasGameLibrary; break;
116  break;
117 
120  proc = HasScenario;
121  break;
122 
123  default:
124  break;
125  }
126 
127  if (proc != NULL) {
128  if (proc(ci, true)) {
130  } else {
132  if (proc(ci, false)) ci->upgrade = true;
133  }
134  } else {
136  }
137 
138  /* Something we don't have and has filesize 0 does not exist in te system */
140 
141  /* Do we already have a stub for this? */
142  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
143  ContentInfo *ici = *iter;
144  if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
145  memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
146  /* Preserve the name if possible */
147  if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
148  if (ici->IsSelected()) ci->state = ici->state;
149 
150  /*
151  * As ici might be selected by the content window we cannot delete that.
152  * However, we want to keep most of the values of ci, except the values
153  * we (just) already preserved.
154  * So transfer data and ownership of allocated memory from ci to ici.
155  */
156  ici->TransferFrom(ci);
157  delete ci;
158 
159  this->OnReceiveContentInfo(ici);
160  return true;
161  }
162  }
163 
164  /* Missing content info? Don't list it */
165  if (ci->filesize == 0) {
166  delete ci;
167  return true;
168  }
169 
170  *this->infos.Append() = ci;
171 
172  /* Incoming data means that we might need to reconsider dependencies */
173  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
174  this->CheckDependencyState(*iter);
175  }
176 
177  this->OnReceiveContentInfo(ci);
178 
179  return true;
180 }
181 
187 {
188  if (type == CONTENT_TYPE_END) {
199  return;
200  }
201 
202  this->Connect();
203 
205  p->Send_uint8 ((byte)type);
206  p->Send_uint32(_openttd_newgrf_version);
207 
208  this->SendPacket(p);
209 }
210 
217 {
218  this->Connect();
219 
220  while (count > 0) {
221  /* We can "only" send a limited number of IDs in a single packet.
222  * A packet begins with the packet size and a byte for the type.
223  * Then this packet adds a uint16 for the count in this packet.
224  * The rest of the packet can be used for the IDs. */
225  uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
226 
228  p->Send_uint16(p_count);
229 
230  for (uint i = 0; i < p_count; i++) {
231  p->Send_uint32(content_ids[i]);
232  }
233 
234  this->SendPacket(p);
235  count -= p_count;
236  content_ids += p_count;
237  }
238 }
239 
246 {
247  if (cv == NULL) return;
248 
249  this->Connect();
250 
251  assert(cv->Length() < 255);
252  assert(cv->Length() < (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) /
253  (sizeof(uint8) + sizeof(uint32) + (send_md5sum ? /*sizeof(ContentInfo::md5sum)*/16 : 0)));
254 
256  p->Send_uint8(cv->Length());
257 
258  for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
259  const ContentInfo *ci = *iter;
260  p->Send_uint8((byte)ci->type);
261  p->Send_uint32(ci->unique_id);
262  if (!send_md5sum) continue;
263 
264  for (uint j = 0; j < sizeof(ci->md5sum); j++) {
265  p->Send_uint8(ci->md5sum[j]);
266  }
267  }
268 
269  this->SendPacket(p);
270 
271  for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
272  ContentInfo *ci = *iter;
273  bool found = false;
274  for (ContentIterator iter2 = this->infos.Begin(); iter2 != this->infos.End(); iter2++) {
275  ContentInfo *ci2 = *iter2;
276  if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
277  (!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
278  found = true;
279  break;
280  }
281  }
282  if (!found) {
283  *this->infos.Append() = ci;
284  } else {
285  delete ci;
286  }
287  }
288 }
289 
296 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
297 {
298  bytes = 0;
299 
300  ContentIDList content;
301  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
302  const ContentInfo *ci = *iter;
303  if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
304 
305  *content.Append() = ci->id;
306  bytes += ci->filesize;
307  }
308 
309  files = content.Length();
310 
311  /* If there's nothing to download, do nothing. */
312  if (files == 0) return;
313 
315  this->DownloadSelectedContentFallback(content);
316  } else {
317  this->DownloadSelectedContentHTTP(content);
318  }
319 }
320 
326 {
327  uint count = content.Length();
328 
329  /* Allocate memory for the whole request.
330  * Requests are "id\nid\n..." (as strings), so assume the maximum ID,
331  * which is uint32 so 10 characters long. Then the newlines and
332  * multiply that all with the count and then add the '\0'. */
333  uint bytes = (10 + 1) * count + 1;
334  char *content_request = MallocT<char>(bytes);
335  const char *lastof = content_request + bytes - 1;
336 
337  char *p = content_request;
338  for (const ContentID *id = content.Begin(); id != content.End(); id++) {
339  p += seprintf(p, lastof, "%d\n", *id);
340  }
341 
342  this->http_response_index = -1;
343 
345  new NetworkHTTPContentConnecter(address, this, NETWORK_CONTENT_MIRROR_URL, content_request);
346  /* NetworkHTTPContentConnecter takes over freeing of content_request! */
347 }
348 
354 {
355  uint count = content.Length();
356  const ContentID *content_ids = content.Begin();
357  this->Connect();
358 
359  while (count > 0) {
360  /* We can "only" send a limited number of IDs in a single packet.
361  * A packet begins with the packet size and a byte for the type.
362  * Then this packet adds a uint16 for the count in this packet.
363  * The rest of the packet can be used for the IDs. */
364  uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
365 
367  p->Send_uint16(p_count);
368 
369  for (uint i = 0; i < p_count; i++) {
370  p->Send_uint32(content_ids[i]);
371  }
372 
373  this->SendPacket(p);
374  count -= p_count;
375  content_ids += p_count;
376  }
377 }
378 
386 static char *GetFullFilename(const ContentInfo *ci, bool compressed)
387 {
389  if (dir == NO_DIRECTORY) return NULL;
390 
391  static char buf[MAX_PATH];
392  FioGetFullPath(buf, lastof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename);
393  strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf));
394 
395  return buf;
396 }
397 
403 static bool GunzipFile(const ContentInfo *ci)
404 {
405 #if defined(WITH_ZLIB)
406  bool ret = true;
407  FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
408  if (ftmp == NULL) return false;
409 
410  gzFile fin = gzdopen(fileno(ftmp), "rb");
411  FILE *fout = fopen(GetFullFilename(ci, false), "wb");
412 
413  if (fin == NULL || fout == NULL) {
414  ret = false;
415  } else {
416  byte buff[8192];
417  for (;;) {
418  int read = gzread(fin, buff, sizeof(buff));
419  if (read == 0) {
420  /* If gzread() returns 0, either the end-of-file has been
421  * reached or an underlying read error has occurred.
422  *
423  * gzeof() can't be used, because:
424  * 1.2.5 - it is safe, 1 means 'everything was OK'
425  * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
426  * 1.2.3.3 - 1 is returned for truncated archive
427  *
428  * So we use gzerror(). When proper end of archive
429  * has been reached, then:
430  * errnum == Z_STREAM_END in 1.2.3.3,
431  * errnum == 0 in 1.2.4 and 1.2.5 */
432  int errnum;
433  gzerror(fin, &errnum);
434  if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
435  break;
436  }
437  if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
438  /* If gzread() returns -1, there was an error in archive */
439  ret = false;
440  break;
441  }
442  /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
443  * if (read < sizeof(buff)) break; */
444  }
445  }
446 
447  if (fin != NULL) {
448  /* Closes ftmp too! */
449  gzclose(fin);
450  } else if (ftmp != NULL) {
451  /* In case the gz stream was opened correctly this will
452  * be closed by gzclose. */
453  fclose(ftmp);
454  }
455  if (fout != NULL) fclose(fout);
456 
457  return ret;
458 #else
459  NOT_REACHED();
460 #endif /* defined(WITH_ZLIB) */
461 }
462 
464 {
465  if (this->curFile == NULL) {
466  delete this->curInfo;
467  /* When we haven't opened a file this must be our first packet with metadata. */
468  this->curInfo = new ContentInfo;
469  this->curInfo->type = (ContentType)p->Recv_uint8();
470  this->curInfo->id = (ContentID)p->Recv_uint32();
471  this->curInfo->filesize = p->Recv_uint32();
472  p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
473 
474  if (!this->BeforeDownload()) {
475  this->Close();
476  return false;
477  }
478  } else {
479  /* We have a file opened, thus are downloading internal content */
480  size_t toRead = (size_t)(p->size - p->pos);
481  if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
483  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
484  this->Close();
485  fclose(this->curFile);
486  this->curFile = NULL;
487 
488  return false;
489  }
490 
491  this->OnDownloadProgress(this->curInfo, (int)toRead);
492 
493  if (toRead == 0) this->AfterDownload();
494  }
495 
496  return true;
497 }
498 
504 {
505  if (!this->curInfo->IsValid()) {
506  delete this->curInfo;
507  this->curInfo = NULL;
508  return false;
509  }
510 
511  if (this->curInfo->filesize != 0) {
512  /* The filesize is > 0, so we are going to download it */
513  const char *filename = GetFullFilename(this->curInfo, true);
514  if (filename == NULL || (this->curFile = fopen(filename, "wb")) == NULL) {
515  /* Unless that fails of course... */
517  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
518  return false;
519  }
520  }
521  return true;
522 }
523 
529 {
530  /* We read nothing; that's our marker for end-of-stream.
531  * Now gunzip the tar and make it known. */
532  fclose(this->curFile);
533  this->curFile = NULL;
534 
535  if (GunzipFile(this->curInfo)) {
536  unlink(GetFullFilename(this->curInfo, true));
537 
539  if (sd == NO_DIRECTORY) NOT_REACHED();
540 
541  TarScanner ts;
542  ts.AddFile(sd, GetFullFilename(this->curInfo, false));
543 
544  if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
545  /* Music can't be in a tar. So extract the tar! */
547  unlink(GetFullFilename(this->curInfo, false));
548  }
549 
550  this->OnDownloadComplete(this->curInfo->id);
551  } else {
552  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
553  }
554 }
555 
556 /* Also called to just clean up the mess. */
558 {
559  /* If we fail, download the rest via the 'old' system. */
560  uint files, bytes;
561  this->DownloadSelectedContent(files, bytes, true);
562 
563  this->http_response.Reset();
564  this->http_response_index = -2;
565 
566  if (this->curFile != NULL) {
567  /* Revert the download progress when we are going for the old system. */
568  long size = ftell(this->curFile);
569  if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
570 
571  fclose(this->curFile);
572  this->curFile = NULL;
573  }
574 }
575 
576 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
577 {
578  assert(data == NULL || length != 0);
579 
580  /* Ignore any latent data coming from a connection we closed. */
581  if (this->http_response_index == -2) return;
582 
583  if (this->http_response_index == -1) {
584  if (data != NULL) {
585  /* Append the rest of the response. */
586  memcpy(this->http_response.Append((uint)length), data, length);
587  return;
588  } else {
589  /* Make sure the response is properly terminated. */
590  *this->http_response.Append() = '\0';
591 
592  /* And prepare for receiving the rest of the data. */
593  this->http_response_index = 0;
594  }
595  }
596 
597  if (data != NULL) {
598  /* We have data, so write it to the file. */
599  if (fwrite(data, 1, length, this->curFile) != length) {
600  /* Writing failed somehow, let try via the old method. */
601  this->OnFailure();
602  } else {
603  /* Just received the data. */
604  this->OnDownloadProgress(this->curInfo, (int)length);
605  }
606  /* Nothing more to do now. */
607  return;
608  }
609 
610  if (this->curFile != NULL) {
611  /* We've finished downloading a file. */
612  this->AfterDownload();
613  }
614 
615  if ((uint)this->http_response_index >= this->http_response.Length()) {
616  /* It's not a real failure, but if there's
617  * nothing more to download it helps with
618  * cleaning up the stuff we allocated. */
619  this->OnFailure();
620  return;
621  }
622 
623  delete this->curInfo;
624  /* When we haven't opened a file this must be our first packet with metadata. */
625  this->curInfo = new ContentInfo;
626 
628 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
629 
630 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
631 
632  for (;;) {
633  char *str = this->http_response.Begin() + this->http_response_index;
634  char *p = strchr(str, '\n');
635  check_and_terminate(p);
636 
637  /* Update the index for the next one */
638  this->http_response_index += (int)strlen(str) + 1;
639 
640  /* Read the ID */
641  p = strchr(str, ',');
642  check_and_terminate(p);
643  this->curInfo->id = (ContentID)atoi(str);
644 
645  /* Read the type */
646  str = p + 1;
647  p = strchr(str, ',');
648  check_and_terminate(p);
649  this->curInfo->type = (ContentType)atoi(str);
650 
651  /* Read the file size */
652  str = p + 1;
653  p = strchr(str, ',');
654  check_and_terminate(p);
655  this->curInfo->filesize = atoi(str);
656 
657  /* Read the URL */
658  str = p + 1;
659  /* Is it a fallback URL? If so, just continue with the next one. */
660  if (strncmp(str, "ottd", 4) == 0) {
661  if ((uint)this->http_response_index >= this->http_response.Length()) {
662  /* Have we gone through all lines? */
663  this->OnFailure();
664  return;
665  }
666  continue;
667  }
668 
669  p = strrchr(str, '/');
670  check_not_null(p);
671  p++; // Start after the '/'
672 
673  char tmp[MAX_PATH];
674  if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
675  this->OnFailure();
676  return;
677  }
678  /* Remove the extension from the string. */
679  for (uint i = 0; i < 2; i++) {
680  p = strrchr(tmp, '.');
681  check_and_terminate(p);
682  }
683 
684  /* Copy the string, without extension, to the filename. */
685  strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
686 
687  /* Request the next file. */
688  if (!this->BeforeDownload()) {
689  this->OnFailure();
690  return;
691  }
692 
694  return;
695  }
696 
697 #undef check
698 #undef check_and_terminate
699 }
700 
707  curFile(NULL),
708  curInfo(NULL),
709  isConnecting(false),
711 {
712 }
713 
716 {
717  delete this->curInfo;
718  if (this->curFile != NULL) fclose(this->curFile);
719 
720  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
721 }
722 
725 public:
731 
732  virtual void OnFailure()
733  {
734  _network_content_client.isConnecting = false;
735  _network_content_client.OnConnect(false);
736  }
737 
738  virtual void OnConnect(SOCKET s)
739  {
740  assert(_network_content_client.sock == INVALID_SOCKET);
741  _network_content_client.isConnecting = false;
742  _network_content_client.sock = s;
743  _network_content_client.Reopen();
744  _network_content_client.OnConnect(true);
745  }
746 };
747 
752 {
754 
755  if (this->sock != INVALID_SOCKET || this->isConnecting) return;
756  this->isConnecting = true;
758 }
759 
764 {
765  if (this->sock == INVALID_SOCKET) return;
767 
768  this->OnDisconnect();
769 }
770 
776 {
777  if (this->sock == INVALID_SOCKET || this->isConnecting) return;
778 
779  if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
780  this->Close();
781  return;
782  }
783 
784  if (this->CanSendReceive()) {
785  if (this->ReceivePackets()) {
786  /* Only update activity once a packet is received, instead of everytime we try it. */
788  }
789  }
790 
791  this->SendPackets();
792 }
793 
799 {
800  /* When we tried to download it already, don't try again */
801  if (this->requested.Contains(cid)) return;
802 
803  *this->requested.Append() = cid;
804  assert(this->requested.Contains(cid));
805  this->RequestContentList(1, &cid);
806 }
807 
814 {
815  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
816  ContentInfo *ci = *iter;
817  if (ci->id == cid) return ci;
818  }
819  return NULL;
820 }
821 
822 
828 {
829  ContentInfo *ci = this->GetContent(cid);
830  if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
831 
833  this->CheckDependencyState(ci);
834 }
835 
841 {
842  ContentInfo *ci = this->GetContent(cid);
843  if (ci == NULL || !ci->IsSelected()) return;
844 
846  this->CheckDependencyState(ci);
847 }
848 
851 {
852  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
853  ContentInfo *ci = *iter;
854  if (ci->state == ContentInfo::UNSELECTED) {
856  this->CheckDependencyState(ci);
857  }
858  }
859 }
860 
863 {
864  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
865  ContentInfo *ci = *iter;
866  if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
868  this->CheckDependencyState(ci);
869  }
870  }
871 }
872 
875 {
876  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
877  ContentInfo *ci = *iter;
879  }
880 }
881 
884 {
885  switch (ci->state) {
888  this->Unselect(ci->id);
889  break;
890 
892  this->Select(ci->id);
893  break;
894 
895  default:
896  break;
897  }
898 }
899 
906 {
907  for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
908  const ContentInfo *ci = *iter;
909  if (ci == child) continue;
910 
911  for (uint i = 0; i < ci->dependency_count; i++) {
912  if (ci->dependencies[i] == child->id) {
913  *parents.Append() = ci;
914  break;
915  }
916  }
917  }
918 }
919 
926 {
927  *tree.Append() = child;
928 
929  /* First find all direct parents. We can't use the "normal" iterator as
930  * we are including stuff into the vector and as such the vector's data
931  * store can be reallocated (and thus move), which means out iterating
932  * pointer gets invalid. So fall back to the indices. */
933  for (uint i = 0; i < tree.Length(); i++) {
934  ConstContentVector parents;
935  this->ReverseLookupDependency(parents, tree[i]);
936 
937  for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
938  tree.Include(*piter);
939  }
940  }
941 }
942 
948 {
949  if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
950  /* Selection is easy; just walk all children and set the
951  * autoselected state. That way we can see what we automatically
952  * selected and thus can unselect when a dependency is removed. */
953  for (uint i = 0; i < ci->dependency_count; i++) {
954  ContentInfo *c = this->GetContent(ci->dependencies[i]);
955  if (c == NULL) {
956  this->DownloadContentInfo(ci->dependencies[i]);
957  } else if (c->state == ContentInfo::UNSELECTED) {
959  this->CheckDependencyState(c);
960  }
961  }
962  return;
963  }
964 
965  if (ci->state != ContentInfo::UNSELECTED) return;
966 
967  /* For unselection we need to find the parents of us. We need to
968  * unselect them. After that we unselect all children that we
969  * depend on and are not used as dependency for us, but only when
970  * we automatically selected them. */
971  ConstContentVector parents;
972  this->ReverseLookupDependency(parents, ci);
973  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
974  const ContentInfo *c = *iter;
975  if (!c->IsSelected()) continue;
976 
977  this->Unselect(c->id);
978  }
979 
980  for (uint i = 0; i < ci->dependency_count; i++) {
981  const ContentInfo *c = this->GetContent(ci->dependencies[i]);
982  if (c == NULL) {
984  continue;
985  }
986  if (c->state != ContentInfo::AUTOSELECTED) continue;
987 
988  /* Only unselect when WE are the only parent. */
989  parents.Clear();
990  this->ReverseLookupDependency(parents, c);
991 
992  /* First check whether anything depends on us */
993  int sel_count = 0;
994  bool force_selection = false;
995  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
996  if ((*iter)->IsSelected()) sel_count++;
997  if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
998  }
999  if (sel_count == 0) {
1000  /* Nothing depends on us */
1001  this->Unselect(c->id);
1002  continue;
1003  }
1004  /* Something manually selected depends directly on us */
1005  if (force_selection) continue;
1006 
1007  /* "Flood" search to find all items in the dependency graph*/
1008  parents.Clear();
1009  this->ReverseLookupTreeDependency(parents, c);
1010 
1011  /* Is there anything that is "force" selected?, if so... we're done. */
1012  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1013  if ((*iter)->state != ContentInfo::SELECTED) continue;
1014 
1015  force_selection = true;
1016  break;
1017  }
1018 
1019  /* So something depended directly on us */
1020  if (force_selection) continue;
1021 
1022  /* Nothing depends on us, mark the whole graph as unselected.
1023  * After that's done run over them once again to test their children
1024  * to unselect. Don't do it immediately because it'll do exactly what
1025  * we're doing now. */
1026  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1027  const ContentInfo *c = *iter;
1028  if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
1029  }
1030  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1031  this->CheckDependencyState(this->GetContent((*iter)->id));
1032  }
1033  }
1034 }
1035 
1038 {
1039  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
1040 
1041  this->infos.Clear();
1042  this->requested.Clear();
1043 }
1044 
1045 /*** CALLBACK ***/
1046 
1048 {
1049  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1050  ContentCallback *cb = *iter;
1051  cb->OnConnect(success);
1052  if (iter != this->callbacks.End() && *iter == cb) iter++;
1053  }
1054 }
1055 
1057 {
1058  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1059  ContentCallback *cb = *iter;
1060  cb->OnDisconnect();
1061  if (iter != this->callbacks.End() && *iter == cb) iter++;
1062  }
1063 }
1064 
1066 {
1067  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1068  ContentCallback *cb = *iter;
1069  cb->OnReceiveContentInfo(ci);
1070  if (iter != this->callbacks.End() && *iter == cb) iter++;
1071  }
1072 }
1073 
1075 {
1076  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1077  ContentCallback *cb = *iter;
1078  cb->OnDownloadProgress(ci, bytes);
1079  if (iter != this->callbacks.End() && *iter == cb) iter++;
1080  }
1081 }
1082 
1084 {
1085  ContentInfo *ci = this->GetContent(cid);
1086  if (ci != NULL) {
1088  }
1089 
1090  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1091  ContentCallback *cb = *iter;
1092  cb->OnDownloadComplete(cid);
1093  if (iter != this->callbacks.End() && *iter == cb) iter++;
1094  }
1095 }
1096 
1097 #endif /* ENABLE_NETWORK */
Helper to mark the end of the types.
Definition: tcp_content.h:37
ContentVector infos
All content info we received.
void CheckDependencyState(ContentInfo *ci)
Check the dependencies (recursively) of this content info.
const ContentInfo *const * ConstContentIterator
Iterator for the constant content vector.
The content consists of base graphics.
Definition: tcp_content.h:27
void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
bool IsSelected() const
Is the state either selected or autoselected?
Definition: tcp_content.cpp:77
Connect with a HTTP server and do ONE query.
Definition: tcp_http.h:79
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:72
void OnConnect(bool success)
Callback for when the connection has finished.
SOCKET sock
The socket currently connected to.
Definition: tcp.h:36
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:77
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:48
Internal entity of a packet.
Definition: packet.h:44
Queries the content server for information about a list of external IDs.
Definition: tcp_content.h:44
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
Socket handler for the content server connection.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
void Close()
Disconnect from the content server.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:250
SmallVector< char, 1024 > http_response
The HTTP response to the requests we&#39;ve been doing.
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:54
virtual void OnFailure()
Callback for when the connection attempt failed.
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:66
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:100
void Reset()
Remove all items from the list and free allocated memory.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
Callbacks for notifying others about incoming data.
uint32 filesize
Size of the file.
Definition: tcp_content.h:71
void Connect()
Connect with the content server.
The content consists of a scenario.
Definition: tcp_content.h:31
void DownloadContentInfo(ContentID cid)
Download information of a given Content ID if not already tried.
uint8 dependency_count
Number of dependencies.
Definition: tcp_content.h:79
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:100
char(* tags)[32]
Malloced array of tags (strings)
Definition: tcp_content.h:82
static bool HasGame(const struct ContentInfo *ci, bool md5sum)
Wrapper function for GameScanner::HasGame.
Definition: game_core.cpp:259
bool(* HasProc)(const ContentInfo *ci, bool md5sum)
Check whether a function piece of content is locally known.
void Clear()
Remove all items from the list.
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.
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:31
const T * Begin() const
Get the pointer to the first item (const)
ContentID * dependencies
Malloced array of dependencies (unique server side ids)
Definition: tcp_content.h:80
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Helper for scanning for files with tar as extension.
Definition: fileio_func.h:95
The content has been selected as dependency.
Definition: tcp_content.h:63
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:121
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
SmallVector< ContentCallback *, 2 > callbacks
Callbacks to notify "the world".
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:118
void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
ContentID
Unique identifier for the content.
Definition: tcp_content.h:53
The content has not been selected.
Definition: tcp_content.h:61
Queries the content server for information about a list of internal IDs.
Definition: tcp_content.h:43
void Reopen()
Reopen the socket so we can send/receive stuff again.
Definition: core.h:76
const T * End() const
Get the pointer behind the last valid item (const)
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
void Unselect(ContentID cid)
Unselect a specific content id.
T * Append(uint to_add=1)
Append an item and return it.
The content does not exist in the content system.
Definition: tcp_content.h:65
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.
virtual bool Receive_SERVER_CONTENT(Packet *p)
Server sending list of content info: uint32 unique id uint32 file size (0 == does not exist) string f...
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=NULL, uint textref_stack_size=0, const uint32 *textref_stack=NULL)
Display an error message in a window.
Definition: error_gui.cpp:378
char name[32]
Name of the content.
Definition: tcp_content.h:73
virtual void OnDisconnect()
Callback for when the connection got disconnected.
The content consists of a game script.
Definition: tcp_content.h:35
NetworkSettings network
settings related to the network
~ClientNetworkContentSocketHandler()
Clear up the mess ;)
uint Length() const
Get the number of items in the list.
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:56
uint8 tag_count
Number of tags.
Definition: tcp_content.h:81
char version[16]
Version of the content.
Definition: tcp_content.h:74
bool Contains(const T &item) const
Tests whether a item is present in the vector.
void SelectUpgrade()
Select everything that&#39;s an update for something we&#39;ve got.
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename=NULL)
Add a file with the given filename.
Definition: fileio.cpp:694
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:110
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:20
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:76
A path without any base directory.
Definition: fileio_type.h:127
The content is already at the client side.
Definition: tcp_content.h:64
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:70
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:22
State state
Whether the content info is selected (for download)
Definition: tcp_content.h:83
byte * buf
Buffer we&#39;re going to write to/read from.
Search within the autodownload directory.
Definition: fileio_type.h:144
Part of the network protocol handling content distribution.
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:52
void OnReceiveData(const char *data, size_t length)
We&#39;re receiving data.
void TransferFrom(ContentInfo *other)
Copy data from other ContentInfo and take ownership of allocated stuff.
Definition: tcp_content.cpp:46
void OnDownloadProgress(const ContentInfo *ci, int bytes)
We have progress in the download of a file.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
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:42
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:78
The content consists of a GS library.
Definition: tcp_content.h:36
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:368
ContentInfo * GetContent(ContentID cid)
Get the content info based on a ContentID.
The content consists of a NewGRF.
Definition: tcp_content.h:28
ContentType
The values in the enum are important; they are used as database &#39;keys&#39;.
Definition: tcp_content.h:25
Network status window; Window numbers:
Definition: window_type.h:487
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:1137
Queries the content server for information about a list of external IDs and MD5.
Definition: tcp_content.h:45
void Select(ContentID cid)
Select a specific content id.
The content consists of an AI library.
Definition: tcp_content.h:30
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
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:47
static const char *const NETWORK_CONTENT_MIRROR_URL
URL of the HTTP mirror system.
Definition: config.h:24
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
The content consists of a heightmap.
Definition: tcp_content.h:32
The content consists of an AI.
Definition: tcp_content.h:29
ContentType type
Type of content.
Definition: tcp_content.h:69
bool upgrade
This item is an upgrade.
Definition: tcp_content.h:84
void DownloadSelectedContentFallback(const ContentIDList &content)
Initiate downloading the content over the fallback protocol.
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:30
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
virtual bool Receive_SERVER_INFO(Packet *p)
Server sending list of content info: byte type (invalid ID == does not exist) uint32 id uint32 file_s...
The content consists of base music.
Definition: tcp_content.h:34
bool Include(const T &item)
Tests whether a item is present in the vector, and appends it to the end if not.
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:766
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:35
NetworkContentConnecter(const NetworkAddress &address)
Initiate the connecting.
virtual void Close()
Really close the socket.
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:19
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:924
static int Connect(char *uri, HTTPCallback *callback, const char *data=NULL, int depth=0)
Connect to the given URI.
Definition: tcp_http.cpp:197
The content has been manually selected.
Definition: tcp_content.h:62
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:65
void OnFailure()
An error has occurred and the connection has been closed.
static const uint16 NETWORK_CONTENT_SERVER_PORT
The default port of the content server (TCP)
Definition: config.h:29
int http_response_index
Where we are, in the response, with handling it.
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:229
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:75
static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
Wrapper function for the HasProc.
Errors (eg. saving/loading failed)
Definition: error.h:25
void DownloadSelectedContentHTTP(const ContentIDList &content)
Initiate downloading the content over HTTP.
char description[512]
Description of the content.
Definition: tcp_content.h:76
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:23
FILE * curFile
Currently downloaded file.
Container for all important information about a piece of content.
Definition: tcp_content.h:58
bool IsValid() const
Is the information from this content info valid?
Definition: tcp_content.cpp:94
Network content download status.
Definition: window_type.h:35
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:290
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:33
void OnDisconnect()
Callback for when the connection got disconnected.