10 #include "../textbuf_gui.h" |
10 #include "../textbuf_gui.h" |
11 #include "../variables.h" |
11 #include "../variables.h" |
12 #include "../newgrf.h" |
12 #include "../newgrf.h" |
13 #include "newgrf_download.h" |
13 #include "newgrf_download.h" |
14 #include "core/config.h" |
14 #include "core/config.h" |
|
15 #include "core/game.h" |
15 #include "../functions.h" |
16 #include "../functions.h" |
16 #include "../debug.h" |
17 #include "../debug.h" |
17 #include "../window_func.h" |
18 #include "../window_func.h" |
18 #include "../core/alloc_func.hpp" |
19 #include "../core/alloc_func.hpp" |
19 #include "../string_func.h" |
20 #include "../string_func.h" |
232 if (BSWAP32(c->grfid) == grfid && memcmp(c->md5sum, md5sum, 16) == 0) { |
233 if (BSWAP32(c->grfid) == grfid && memcmp(c->md5sum, md5sum, 16) == 0) { |
233 DEBUG(grfdl, 0, "Found %08X:%s at %s", buf_ptr + 0, buf_ptr + 8 + 1, url); |
234 DEBUG(grfdl, 0, "Found %08X:%s at %s", buf_ptr + 0, buf_ptr + 8 + 1, url); |
234 |
235 |
235 c->filename = url; |
236 c->filename = url; |
236 c->status = GCS_AVAILABLE; |
237 c->status = GCS_AVAILABLE; |
|
238 ClrBit(c->status, GCF_COMPATIBLE); |
237 |
239 |
238 found++; |
240 found++; |
239 |
241 |
240 break; |
242 break; |
241 } |
243 } |
273 CURLcode err; |
275 CURLcode err; |
274 FILE *fh = NULL; |
276 FILE *fh = NULL; |
275 char md5buf[64], filename[256], useragent[512]; |
277 char md5buf[64], filename[256], useragent[512]; |
276 long http_code; |
278 long http_code; |
277 int ret = -1; |
279 int ret = -1; |
|
280 int found = 0; |
278 |
281 |
279 // set up curl |
282 // set up curl |
280 if ((err = do_init_curl())) { |
283 if ((err = do_init_curl())) { |
281 DEBUG(grfdl, 0, "curl_global_init failed: %s", curl_easy_strerror(err)); |
284 DEBUG(grfdl, 0, "curl_global_init failed: %s", curl_easy_strerror(err)); |
282 goto error; |
285 goto error; |
459 DNGRFS_CHECK_AVAILABLE, |
465 DNGRFS_CHECK_AVAILABLE, |
460 DNGRFS_DOWNLOAD_AVAILABLE, |
466 DNGRFS_DOWNLOAD_AVAILABLE, |
461 DNGRFS_RESIZE, |
467 DNGRFS_RESIZE, |
462 }; |
468 }; |
463 |
469 |
464 // XXX: do we need a temp. copy? I think not |
470 struct NetworkGameInfo *info; ///< the server we are downloading NewGRFs for |
465 GRFConfig *list; ///< temporary grf list to which changes are made |
471 GRFConfig *list; ///< temporary grf list to which changes are made |
466 const GRFConfig *sel; |
472 const GRFConfig *sel; |
467 |
473 |
468 NewGRFDownloadWindow(const WindowDesc *desc, GRFConfig *list) : Window(desc, 0) |
474 NewGRFDownloadWindow(const WindowDesc *desc, struct NetworkGameInfo *info, GRFConfig *list) : Window(desc, 0) |
469 { |
475 { |
470 // resize magic |
476 // resize magic |
471 this->resize.step_height = 14; |
477 this->resize.step_height = 14; |
472 |
478 |
473 // the list of GRFs |
479 // the server info |
|
480 this->info = info; |
|
481 |
|
482 // the list of GRFs that we need |
474 this->list = list; |
483 this->list = list; |
475 |
484 |
476 // display window? |
485 // display window? |
477 this->FindWindowPlacementAndResize(desc); |
486 this->FindWindowPlacementAndResize(desc); |
478 |
487 |
480 this->SetupDownloadNewGRFWindow(); |
489 this->SetupDownloadNewGRFWindow(); |
481 } |
490 } |
482 |
491 |
483 ~NewGRFDownloadWindow() |
492 ~NewGRFDownloadWindow() |
484 { |
493 { |
|
494 // Apply changes from list to the config |
|
495 GRFConfig *config, *list; |
|
496 |
|
497 // default to true, set back to false if we find a still-missing NewGRF |
|
498 this->info->compatible = true; |
|
499 |
|
500 for (config = this->info->grfconfig, list = this->list; config != NULL && list != NULL; config = config->next, list = list->next) { |
|
501 // skip those entries that aren't in list |
|
502 if (config->grfid == list->grfid) { |
|
503 // changed? |
|
504 if (config->status != list->status && list->status != GCS_AVAILABLE) { |
|
505 config->status = list->status; |
|
506 ClrBit(config->flags, GCF_COMPATIBLE); |
|
507 } |
|
508 } |
|
509 |
|
510 // is this still NOT_FOUND? |
|
511 if (config->status == GCS_NOT_FOUND) |
|
512 this->info->compatible = false; |
|
513 |
|
514 } |
|
515 |
485 /* Remove the temporary copy of grf-list used in window */ |
516 /* Remove the temporary copy of grf-list used in window */ |
486 ClearGRFConfigList(&this->list); |
517 ClearGRFConfigList(&this->list); |
487 } |
518 } |
488 |
519 |
489 void SetupDownloadNewGRFWindow() |
520 void SetupDownloadNewGRFWindow() |
621 break; |
652 break; |
622 |
653 |
623 case DNGRFS_DOWNLOAD_AVAILABLE: |
654 case DNGRFS_DOWNLOAD_AVAILABLE: |
624 // do a bunch of HTTP requests |
655 // do a bunch of HTTP requests |
625 found = DownloadAvailableNewGRFs(this->list); |
656 found = DownloadAvailableNewGRFs(this->list); |
626 |
657 |
627 // disable the button, not useful anymore |
658 if (found < 0) { |
628 this->SetWidgetDisabledState(DNGRFS_DOWNLOAD_AVAILABLE, 1); |
659 // failed |
629 |
660 ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DOWNLOAD_FAILED, 0, 0); |
630 // redraw with new status |
661 |
631 this->SetDirty(); |
662 } else { |
632 |
663 if (found == 0) { |
|
664 // ??? |
|
665 // this shouldn't really happen, as we check for this in CHECK_AVAILABLE |
|
666 ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DOWNLOAD_CHECK_NONE, 0, 0); |
|
667 |
|
668 } else { |
|
669 /* XXX: not exported, so prototype here */ |
|
670 void ScanForTarFiles (); |
|
671 |
|
672 // load the newly downloaded NewGRF files |
|
673 ScanForTarFiles(); |
|
674 ScanNewGRFFiles(); |
|
675 } |
|
676 |
|
677 // disable the button, not useful anymore |
|
678 this->SetWidgetDisabledState(DNGRFS_DOWNLOAD_AVAILABLE, 1); |
|
679 |
|
680 // redraw with new status |
|
681 this->SetDirty(); |
|
682 } |
|
683 |
633 break; |
684 break; |
634 |
685 |
635 case DNGRFS_FILE_LIST: { // Select a GRF |
686 case DNGRFS_FILE_LIST: { // Select a GRF |
636 GRFConfig *c; |
687 GRFConfig *c; |
637 uint i = (pt.y - this->widget[DNGRFS_FILE_LIST].top) / 14 + this->vscroll.pos; |
688 uint i = (pt.y - this->widget[DNGRFS_FILE_LIST].top) / 14 + this->vscroll.pos; |
694 }; |
745 }; |
695 |
746 |
696 |
747 |
697 /** Setup the NewGRF download gui |
748 /** Setup the NewGRF download gui |
698 * @param config pointer to a linked-list of grfconfig's needed */ |
749 * @param config pointer to a linked-list of grfconfig's needed */ |
699 void ShowNewGRFDownload(GRFConfig **config) |
750 void ShowNewGRFDownload(struct NetworkGameInfo *info) |
700 { |
751 { |
701 // filter out those that we don't need to download |
752 // filter out those that we don't need to download |
702 GRFConfig *head = NULL, **item = &head; |
753 GRFConfig *head = NULL, **item = &head; |
703 const GRFConfig *src; |
754 const GRFConfig *src; |
704 |
755 |
705 for (src = *config; src != NULL; src = src->next) { |
756 for (src = info->grfconfig; src != NULL; src = src->next) { |
706 if (src->status == GCS_NOT_FOUND || HasBit(src->flags, GCF_COMPATIBLE)) { |
757 if (src->status == GCS_NOT_FOUND || HasBit(src->flags, GCF_COMPATIBLE)) { |
707 // copy-past from newgrf_config.cpp CopyGRFConfigList |
758 // copy-past from newgrf_config.cpp CopyGRFConfigList |
708 GRFConfig *c = CallocT<GRFConfig>(1); |
759 GRFConfig *c = CallocT<GRFConfig>(1); |
709 *c = *src; |
760 *c = *src; |
710 c->next = NULL; |
761 c->next = NULL; |