fix the error reporting for DOWNLOAD_AVAILABLE, and change NewGRFDownload to take a NetworkGameInfo instead of the GRFConfig** - it updates the compatible/not compatible state.
authorTero Marttila <terom@fixme.fi>
Tue, 22 Jul 2008 22:33:49 +0300
changeset 11181 403550141f43
parent 11180 982e9f814f97
child 11182 22f2cfb1f1f0
fix the error reporting for DOWNLOAD_AVAILABLE, and change NewGRFDownload to take a NetworkGameInfo instead of the GRFConfig** - it updates the compatible/not compatible state.
src/lang/english.txt
src/network/network_gui.cpp
src/network/newgrf_download.cpp
src/network/newgrf_download.h
--- a/src/lang/english.txt	Tue Jul 22 21:51:14 2008 +0300
+++ b/src/lang/english.txt	Tue Jul 22 22:33:49 2008 +0300
@@ -3197,6 +3197,7 @@
 STR_NEWGRF_DOWNLOAD_AVAILABLE                                   :{BLACK}Download Available
 STR_NEWGRF_DOWNLOAD_CHECK_FAILED                                :{WHITE}Checking for available NewGRFs failed
 STR_NEWGRF_DOWNLOAD_CHECK_NONE                                  :{WHITE}No NewGRFs available for download
+STR_NEWGRF_DOWNLOAD_FAILED                                      :{WHITE}Downloading the NewGRFs failed
 
 STR_NEWGRF_NOT_FOUND                                            :{RED}Matching file not found
 STR_NEWGRF_DISABLED                                             :{RED}Disabled
--- a/src/network/network_gui.cpp	Tue Jul 22 21:51:14 2008 +0300
+++ b/src/network/network_gui.cpp	Tue Jul 22 22:33:49 2008 +0300
@@ -484,7 +484,7 @@
 				break;
 
             case NGWW_NEWGRF_DL: // NewGRF Download
-				if (this->server != NULL) ShowNewGRFDownload(&this->server->info.grfconfig);
+				if (this->server != NULL) ShowNewGRFDownload(&this->server->info);
                 break;
 		}
 	}
--- a/src/network/newgrf_download.cpp	Tue Jul 22 21:51:14 2008 +0300
+++ b/src/network/newgrf_download.cpp	Tue Jul 22 22:33:49 2008 +0300
@@ -12,6 +12,7 @@
 #include "../newgrf.h"
 #include "newgrf_download.h"
 #include "core/config.h"
+#include "core/game.h"
 #include "../functions.h"
 #include "../debug.h"
 #include "../window_func.h"
@@ -234,6 +235,7 @@
 
                 c->filename = url;
                 c->status = GCS_AVAILABLE;
+                ClrBit(c->status, GCF_COMPATIBLE);
 
                 found++;
 
@@ -275,6 +277,7 @@
     char md5buf[64], filename[256], useragent[512];
     long http_code;
     int ret = -1;
+    int found = 0;
 
     // set up curl
     if ((err = do_init_curl())) {
@@ -357,10 +360,13 @@
 
         // OK, great
         c->status = GCS_UNKNOWN;
+        ClrBit(c->status, GCF_COMPATIBLE);
+
+        found++;
     }
 
     // success
-    ret = 0;
+    ret = found;
 
 error:
     if (fh)
@@ -461,16 +467,19 @@
 		DNGRFS_RESIZE,
 	};
     
-    // XXX: do we need a temp. copy? I think not
-	GRFConfig *list;       ///< temporary grf list to which changes are made
+    struct NetworkGameInfo *info;   ///< the server we are downloading NewGRFs for
+	GRFConfig *list;        ///< temporary grf list to which changes are made
 	const GRFConfig *sel;
 
-	NewGRFDownloadWindow(const WindowDesc *desc, GRFConfig *list) : Window(desc, 0)
+	NewGRFDownloadWindow(const WindowDesc *desc, struct NetworkGameInfo *info, GRFConfig *list) : Window(desc, 0)
 	{
         // resize magic
 		this->resize.step_height = 14;
+        
+        // the server info
+        this->info = info;
 
-        // the list of GRFs
+        // the list of GRFs that we need
         this->list = list;
         
         // display window?
@@ -482,6 +491,28 @@
 
 	~NewGRFDownloadWindow()
 	{
+        // Apply changes from list to the config
+        GRFConfig *config, *list;
+
+        // default to true, set back to false if we find a still-missing NewGRF
+        this->info->compatible = true;
+        
+        for (config = this->info->grfconfig, list = this->list; config != NULL && list != NULL; config = config->next, list = list->next) {
+            // skip those entries that aren't in list
+            if (config->grfid == list->grfid) {
+                // changed?
+                if (config->status != list->status && list->status != GCS_AVAILABLE) {
+                    config->status = list->status;
+                    ClrBit(config->flags, GCF_COMPATIBLE);
+                }
+            }
+            
+            // is this still NOT_FOUND?
+			if (config->status == GCS_NOT_FOUND) 
+                this->info->compatible = false;
+
+        }            
+
 		/* Remove the temporary copy of grf-list used in window */
 		ClearGRFConfigList(&this->list);
     }
@@ -623,13 +654,33 @@
             case DNGRFS_DOWNLOAD_AVAILABLE:
                 // do a bunch of HTTP requests
                 found = DownloadAvailableNewGRFs(this->list);
-                
-                // disable the button, not useful anymore
-                this->SetWidgetDisabledState(DNGRFS_DOWNLOAD_AVAILABLE, 1);
-                
-                // redraw with new status
-                this->SetDirty();
 
+                if (found < 0) {
+                    // failed
+                    ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DOWNLOAD_FAILED, 0, 0);
+
+                } else {
+                    if (found == 0) {
+                        // ???
+                        // this shouldn't really happen, as we check for this in CHECK_AVAILABLE
+                        ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DOWNLOAD_CHECK_NONE, 0, 0);
+
+                    } else {
+                        /* XXX: not exported, so prototype here */
+                        void ScanForTarFiles ();
+
+                        // load the newly downloaded NewGRF files
+                        ScanForTarFiles();
+                        ScanNewGRFFiles();
+                    } 
+
+                    // disable the button, not useful anymore
+                    this->SetWidgetDisabledState(DNGRFS_DOWNLOAD_AVAILABLE, 1);
+                    
+                    // redraw with new status
+                    this->SetDirty();
+                }   
+               
                 break;
 
 			case DNGRFS_FILE_LIST: { // Select a GRF
@@ -696,13 +747,13 @@
 
 /** Setup the NewGRF download gui
  * @param config pointer to a linked-list of grfconfig's needed */
-void ShowNewGRFDownload(GRFConfig **config)
+void ShowNewGRFDownload(struct NetworkGameInfo *info)
 {
     // filter out those that we don't need to download
     GRFConfig *head = NULL, **item = &head;
     const GRFConfig *src;
     
-    for (src = *config; src != NULL; src = src->next) {
+    for (src = info->grfconfig; src != NULL; src = src->next) {
         if (src->status == GCS_NOT_FOUND || HasBit(src->flags, GCF_COMPATIBLE)) {
             // copy-past from newgrf_config.cpp CopyGRFConfigList
             GRFConfig *c = CallocT<GRFConfig>(1);
@@ -725,6 +776,6 @@
     }
 
 	DeleteWindowByClass(WC_GAME_OPTIONS);
-	new NewGRFDownloadWindow(&_newgrf_download_desc, head);
+	new NewGRFDownloadWindow(&_newgrf_download_desc, info, head);
 }
 
--- a/src/network/newgrf_download.h	Tue Jul 22 21:51:14 2008 +0300
+++ b/src/network/newgrf_download.h	Tue Jul 22 22:33:49 2008 +0300
@@ -6,6 +6,7 @@
 #define NEWGRF_DOWNLOAD_H
 
 #include "../newgrf_config.h"
+#include "core/game.h"
 
 // URL to the Master NewGRF database API
 #define NETWORK_NEWGRF_MASTER_API "http://localhost:8000/openttd-api/%s"
@@ -14,7 +15,7 @@
 #define NETWORK_HTTP_USER_AGENT "OpenTTD (%s)"
 
 /* In newgrf_download.cpp */
-void ShowNewGRFDownload(GRFConfig **config);
+void ShowNewGRFDownload(struct NetworkGameInfo *info);
 
 #endif /* NEWGRF_DOWNLOAD_H */