OpenTTD
network_content.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 
408  /* Need to open the file with fopen() to support non-ASCII on Windows. */
409  FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
410  if (ftmp == NULL) return false;
411  /* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
412  gzFile fin = gzdopen(dup(fileno(ftmp)), "rb");
413  fclose(ftmp);
414 
415  FILE *fout = fopen(GetFullFilename(ci, false), "wb");
416 
417  if (fin == NULL || fout == NULL) {
418  ret = false;
419  } else {
420  byte buff[8192];
421  for (;;) {
422  int read = gzread(fin, buff, sizeof(buff));
423  if (read == 0) {
424  /* If gzread() returns 0, either the end-of-file has been
425  * reached or an underlying read error has occurred.
426  *
427  * gzeof() can't be used, because:
428  * 1.2.5 - it is safe, 1 means 'everything was OK'
429  * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
430  * 1.2.3.3 - 1 is returned for truncated archive
431  *
432  * So we use gzerror(). When proper end of archive
433  * has been reached, then:
434  * errnum == Z_STREAM_END in 1.2.3.3,
435  * errnum == 0 in 1.2.4 and 1.2.5 */
436  int errnum;
437  gzerror(fin, &errnum);
438  if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
439  break;
440  }
441  if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
442  /* If gzread() returns -1, there was an error in archive */
443  ret = false;
444  break;
445  }
446  /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
447  * if (read < sizeof(buff)) break; */
448  }
449  }
450 
451  if (fin != NULL) gzclose(fin);
452  if (fout != NULL) fclose(fout);
453 
454  return ret;
455 #else
456  NOT_REACHED();
457 #endif /* defined(WITH_ZLIB) */
458 }
459 
461 {
462  if (this->curFile == NULL) {
463  delete this->curInfo;
464  /* When we haven't opened a file this must be our first packet with metadata. */
465  this->curInfo = new ContentInfo;
466  this->curInfo->type = (ContentType)p->Recv_uint8();
467  this->curInfo->id = (ContentID)p->Recv_uint32();
468  this->curInfo->filesize = p->Recv_uint32();
469  p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
470 
471  if (!this->BeforeDownload()) {
472  this->Close();
473  return false;
474  }
475  } else {
476  /* We have a file opened, thus are downloading internal content */
477  size_t toRead = (size_t)(p->size - p->pos);
478  if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
480  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
481  this->Close();
482  fclose(this->curFile);
483  this->curFile = NULL;
484 
485  return false;
486  }
487 
488  this->OnDownloadProgress(this->curInfo, (int)toRead);
489 
490  if (toRead == 0) this->AfterDownload();
491  }
492 
493  return true;
494 }
495 
501 {
502  if (!this->curInfo->IsValid()) {
503  delete this->curInfo;
504  this->curInfo = NULL;
505  return false;
506  }
507 
508  if (this->curInfo->filesize != 0) {
509  /* The filesize is > 0, so we are going to download it */
510  const char *filename = GetFullFilename(this->curInfo, true);
511  if (filename == NULL || (this->curFile = fopen(filename, "wb")) == NULL) {
512  /* Unless that fails of course... */
514  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
515  return false;
516  }
517  }
518  return true;
519 }
520 
526 {
527  /* We read nothing; that's our marker for end-of-stream.
528  * Now gunzip the tar and make it known. */
529  fclose(this->curFile);
530  this->curFile = NULL;
531 
532  if (GunzipFile(this->curInfo)) {
533  unlink(GetFullFilename(this->curInfo, true));
534 
536  if (sd == NO_DIRECTORY) NOT_REACHED();
537 
538  TarScanner ts;
539  ts.AddFile(sd, GetFullFilename(this->curInfo, false));
540 
541  if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
542  /* Music can't be in a tar. So extract the tar! */
544  unlink(GetFullFilename(this->curInfo, false));
545  }
546 
547  this->OnDownloadComplete(this->curInfo->id);
548  } else {
549  ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
550  }
551 }
552 
553 /* Also called to just clean up the mess. */
555 {
556  /* If we fail, download the rest via the 'old' system. */
557  uint files, bytes;
558  this->DownloadSelectedContent(files, bytes, true);
559 
560  this->http_response.Reset();
561  this->http_response_index = -2;
562 
563  if (this->curFile != NULL) {
564  /* Revert the download progress when we are going for the old system. */
565  long size = ftell(this->curFile);
566  if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
567 
568  fclose(this->curFile);
569  this->curFile = NULL;
570  }
571 }
572 
573 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
574 {
575  assert(data == NULL || length != 0);
576 
577  /* Ignore any latent data coming from a connection we closed. */
578  if (this->http_response_index == -2) return;
579 
580  if (this->http_response_index == -1) {
581  if (data != NULL) {
582  /* Append the rest of the response. */
583  memcpy(this->http_response.Append((uint)length), data, length);
584  return;
585  } else {
586  /* Make sure the response is properly terminated. */
587  *this->http_response.Append() = '\0';
588 
589  /* And prepare for receiving the rest of the data. */
590  this->http_response_index = 0;
591  }
592  }
593 
594  if (data != NULL) {
595  /* We have data, so write it to the file. */
596  if (fwrite(data, 1, length, this->curFile) != length) {
597  /* Writing failed somehow, let try via the old method. */
598  this->OnFailure();
599  } else {
600  /* Just received the data. */
601  this->OnDownloadProgress(this->curInfo, (int)length);
602  }
603  /* Nothing more to do now. */
604  return;
605  }
606 
607  if (this->curFile != NULL) {
608  /* We've finished downloading a file. */
609  this->AfterDownload();
610  }
611 
612  if ((uint)this->http_response_index >= this->http_response.Length()) {
613  /* It's not a real failure, but if there's
614  * nothing more to download it helps with
615  * cleaning up the stuff we allocated. */
616  this->OnFailure();
617  return;
618  }
619 
620  delete this->curInfo;
621  /* When we haven't opened a file this must be our first packet with metadata. */
622  this->curInfo = new ContentInfo;
623 
625 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
626 
627 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
628 
629  for (;;) {
630  char *str = this->http_response.Begin() + this->http_response_index;
631  char *p = strchr(str, '\n');
632  check_and_terminate(p);
633 
634  /* Update the index for the next one */
635  this->http_response_index += (int)strlen(str) + 1;
636 
637  /* Read the ID */
638  p = strchr(str, ',');
639  check_and_terminate(p);
640  this->curInfo->id = (ContentID)atoi(str);
641 
642  /* Read the type */
643  str = p + 1;
644  p = strchr(str, ',');
645  check_and_terminate(p);
646  this->curInfo->type = (ContentType)atoi(str);
647 
648  /* Read the file size */
649  str = p + 1;
650  p = strchr(str, ',');
651  check_and_terminate(p);
652  this->curInfo->filesize = atoi(str);
653 
654  /* Read the URL */
655  str = p + 1;
656  /* Is it a fallback URL? If so, just continue with the next one. */
657  if (strncmp(str, "ottd", 4) == 0) {
658  if ((uint)this->http_response_index >= this->http_response.Length()) {
659  /* Have we gone through all lines? */
660  this->OnFailure();
661  return;
662  }
663  continue;
664  }
665 
666  p = strrchr(str, '/');
667  check_not_null(p);
668  p++; // Start after the '/'
669 
670  char tmp[MAX_PATH];
671  if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
672  this->OnFailure();
673  return;
674  }
675  /* Remove the extension from the string. */
676  for (uint i = 0; i < 2; i++) {
677  p = strrchr(tmp, '.');
678  check_and_terminate(p);
679  }
680 
681  /* Copy the string, without extension, to the filename. */
682  strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
683 
684  /* Request the next file. */
685  if (!this->BeforeDownload()) {
686  this->OnFailure();
687  return;
688  }
689 
691  return;
692  }
693 
694 #undef check
695 #undef check_and_terminate
696 }
697 
704  curFile(NULL),
705  curInfo(NULL),
706  isConnecting(false),
708 {
709 }
710 
713 {
714  delete this->curInfo;
715  if (this->curFile != NULL) fclose(this->curFile);
716 
717  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
718 }
719 
722 public:
728 
729  virtual void OnFailure()
730  {
731  _network_content_client.isConnecting = false;
732  _network_content_client.OnConnect(false);
733  }
734 
735  virtual void OnConnect(SOCKET s)
736  {
737  assert(_network_content_client.sock == INVALID_SOCKET);
738  _network_content_client.isConnecting = false;
739  _network_content_client.sock = s;
740  _network_content_client.Reopen();
741  _network_content_client.OnConnect(true);
742  }
743 };
744 
749 {
751 
752  if (this->sock != INVALID_SOCKET || this->isConnecting) return;
753  this->isConnecting = true;
755 }
756 
761 {
762  if (this->sock == INVALID_SOCKET) return;
764 
765  this->OnDisconnect();
766 }
767 
773 {
774  if (this->sock == INVALID_SOCKET || this->isConnecting) return;
775 
776  if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
777  this->Close();
778  return;
779  }
780 
781  if (this->CanSendReceive()) {
782  if (this->ReceivePackets()) {
783  /* Only update activity once a packet is received, instead of everytime we try it. */
785  }
786  }
787 
788  this->SendPackets();
789 }
790 
796 {
797  /* When we tried to download it already, don't try again */
798  if (this->requested.Contains(cid)) return;
799 
800  *this->requested.Append() = cid;
801  assert(this->requested.Contains(cid));
802  this->RequestContentList(1, &cid);
803 }
804 
811 {
812  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
813  ContentInfo *ci = *iter;
814  if (ci->id == cid) return ci;
815  }
816  return NULL;
817 }
818 
819 
825 {
826  ContentInfo *ci = this->GetContent(cid);
827  if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
828 
830  this->CheckDependencyState(ci);
831 }
832 
838 {
839  ContentInfo *ci = this->GetContent(cid);
840  if (ci == NULL || !ci->IsSelected()) return;
841 
843  this->CheckDependencyState(ci);
844 }
845 
848 {
849  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
850  ContentInfo *ci = *iter;
851  if (ci->state == ContentInfo::UNSELECTED) {
853  this->CheckDependencyState(ci);
854  }
855  }
856 }
857 
860 {
861  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
862  ContentInfo *ci = *iter;
863  if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
865  this->CheckDependencyState(ci);
866  }
867  }
868 }
869 
872 {
873  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
874  ContentInfo *ci = *iter;
876  }
877 }
878 
881 {
882  switch (ci->state) {
885  this->Unselect(ci->id);
886  break;
887 
889  this->Select(ci->id);
890  break;
891 
892  default:
893  break;
894  }
895 }
896 
903 {
904  for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
905  const ContentInfo *ci = *iter;
906  if (ci == child) continue;
907 
908  for (uint i = 0; i < ci->dependency_count; i++) {
909  if (ci->dependencies[i] == child->id) {
910  *parents.Append() = ci;
911  break;
912  }
913  }
914  }
915 }
916 
923 {
924  *tree.Append() = child;
925 
926  /* First find all direct parents. We can't use the "normal" iterator as
927  * we are including stuff into the vector and as such the vector's data
928  * store can be reallocated (and thus move), which means out iterating
929  * pointer gets invalid. So fall back to the indices. */
930  for (uint i = 0; i < tree.Length(); i++) {
931  ConstContentVector parents;
932  this->ReverseLookupDependency(parents, tree[i]);
933 
934  for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
935  tree.Include(*piter);
936  }
937  }
938 }
939 
945 {
946  if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
947  /* Selection is easy; just walk all children and set the
948  * autoselected state. That way we can see what we automatically
949  * selected and thus can unselect when a dependency is removed. */
950  for (uint i = 0; i < ci->dependency_count; i++) {
951  ContentInfo *c = this->GetContent(ci->dependencies[i]);
952  if (c == NULL) {
953  this->DownloadContentInfo(ci->dependencies[i]);
954  } else if (c->state == ContentInfo::UNSELECTED) {
956  this->CheckDependencyState(c);
957  }
958  }
959  return;
960  }
961 
962  if (ci->state != ContentInfo::UNSELECTED) return;
963 
964  /* For unselection we need to find the parents of us. We need to
965  * unselect them. After that we unselect all children that we
966  * depend on and are not used as dependency for us, but only when
967  * we automatically selected them. */
968  ConstContentVector parents;
969  this->ReverseLookupDependency(parents, ci);
970  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
971  const ContentInfo *c = *iter;
972  if (!c->IsSelected()) continue;
973 
974  this->Unselect(c->id);
975  }
976 
977  for (uint i = 0; i < ci->dependency_count; i++) {
978  const ContentInfo *c = this->GetContent(ci->dependencies[i]);
979  if (c == NULL) {
981  continue;
982  }
983  if (c->state != ContentInfo::AUTOSELECTED) continue;
984 
985  /* Only unselect when WE are the only parent. */
986  parents.Clear();
987  this->ReverseLookupDependency(parents, c);
988 
989  /* First check whether anything depends on us */
990  int sel_count = 0;
991  bool force_selection = false;
992  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
993  if ((*iter)->IsSelected()) sel_count++;
994  if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
995  }
996  if (sel_count == 0) {
997  /* Nothing depends on us */
998  this->Unselect(c->id);
999  continue;
1000  }
1001  /* Something manually selected depends directly on us */
1002  if (force_selection) continue;
1003 
1004  /* "Flood" search to find all items in the dependency graph*/
1005  parents.Clear();
1006  this->ReverseLookupTreeDependency(parents, c);
1007 
1008  /* Is there anything that is "force" selected?, if so... we're done. */
1009  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1010  if ((*iter)->state != ContentInfo::SELECTED) continue;
1011 
1012  force_selection = true;
1013  break;
1014  }
1015 
1016  /* So something depended directly on us */
1017  if (force_selection) continue;
1018 
1019  /* Nothing depends on us, mark the whole graph as unselected.
1020  * After that's done run over them once again to test their children
1021  * to unselect. Don't do it immediately because it'll do exactly what
1022  * we're doing now. */
1023  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1024  const ContentInfo *c = *iter;
1025  if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
1026  }
1027  for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1028  this->CheckDependencyState(this->GetContent((*iter)->id));
1029  }
1030  }
1031 }
1032 
1035 {
1036  for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
1037 
1038  this->infos.Clear();
1039  this->requested.Clear();
1040 }
1041 
1042 /*** CALLBACK ***/
1043 
1045 {
1046  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1047  ContentCallback *cb = *iter;
1048  cb->OnConnect(success);
1049  if (iter != this->callbacks.End() && *iter == cb) iter++;
1050  }
1051 }
1052 
1054 {
1055  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1056  ContentCallback *cb = *iter;
1057  cb->OnDisconnect();
1058  if (iter != this->callbacks.End() && *iter == cb) iter++;
1059  }
1060 }
1061 
1063 {
1064  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1065  ContentCallback *cb = *iter;
1066  cb->OnReceiveContentInfo(ci);
1067  if (iter != this->callbacks.End() && *iter == cb) iter++;
1068  }
1069 }
1070 
1072 {
1073  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1074  ContentCallback *cb = *iter;
1075  cb->OnDownloadProgress(ci, bytes);
1076  if (iter != this->callbacks.End() && *iter == cb) iter++;
1077  }
1078 }
1079 
1081 {
1082  ContentInfo *ci = this->GetContent(cid);
1083  if (ci != NULL) {
1085  }
1086 
1087  for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1088  ContentCallback *cb = *iter;
1089  cb->OnDownloadComplete(cid);
1090  if (iter != this->callbacks.End() && *iter == cb) iter++;
1091  }
1092 }
1093 
1094 #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:52
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:409
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:268
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:96
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:685
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:373
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:1146
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:780
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:915
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.