2010-12-17 18:50:44 +01:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "network/networkops.h"
|
|
|
|
#include "network/http.h"
|
|
|
|
#include "prompts/PromptWindows.h"
|
|
|
|
#include "prompts/ProgressWindow.h"
|
2011-01-23 21:13:41 +01:00
|
|
|
#include "prompts/CheckboxPrompt.hpp"
|
2010-12-17 18:50:44 +01:00
|
|
|
#include "FileOperations/fileops.h"
|
|
|
|
#include "settings/CSettings.h"
|
|
|
|
#include "language/gettext.h"
|
|
|
|
#include "usbloader/GetMissingGameFiles.hpp"
|
|
|
|
#include "utils/StringTools.h"
|
|
|
|
|
|
|
|
#define VALID_IMAGE(x) (!(x.size == 36864 || x.size <= 1024 || x.size == 7386 || x.size <= 1174 || x.size == 4446 || x.data == NULL))
|
|
|
|
|
|
|
|
const char * serverURL3D = "http://wiitdb.com/wiitdb/artwork/cover3D/";
|
|
|
|
const char * serverURL2D = "http://wiitdb.com/wiitdb/artwork/cover/";
|
|
|
|
const char * serverURLOrigDiscs = "http://wiitdb.com/wiitdb/artwork/disc/";
|
|
|
|
const char * serverURLCustomDiscs = "http://wiitdb.com/wiitdb/artwork/disccustom/";
|
|
|
|
|
|
|
|
static bool AbortRequested = false;
|
|
|
|
|
|
|
|
static void AbortCallback(void)
|
|
|
|
{
|
|
|
|
AbortRequested = true;
|
|
|
|
}
|
|
|
|
|
2011-01-11 18:52:30 +01:00
|
|
|
static inline struct block DownloadImage(const char * url, const char * gameID)
|
|
|
|
{
|
|
|
|
char CheckedRegion[10];
|
|
|
|
char downloadURL[512];
|
|
|
|
bool PAL = false;
|
|
|
|
|
|
|
|
//Creates URL depending from which Country the game is
|
|
|
|
switch (gameID[3])
|
|
|
|
{
|
|
|
|
case 'J':
|
|
|
|
sprintf(downloadURL, "%sJA/%s.png", url, gameID);
|
|
|
|
sprintf(CheckedRegion, "JA");
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
sprintf(downloadURL, "%sZH/%s.png", url, gameID);
|
|
|
|
sprintf(CheckedRegion, "ZH");
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
sprintf(downloadURL, "%sKO/%s.png", url, gameID);
|
|
|
|
sprintf(CheckedRegion, "KO");
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
case 'D':
|
|
|
|
case 'F':
|
|
|
|
case 'I':
|
|
|
|
case 'S':
|
|
|
|
case 'H':
|
|
|
|
case 'U':
|
|
|
|
case 'X':
|
|
|
|
case 'Y':
|
|
|
|
case 'Z':
|
|
|
|
sprintf(downloadURL, "%s%s/%s.png", url, Settings.db_language, gameID);
|
|
|
|
sprintf(CheckedRegion, "%s", Settings.db_language);
|
|
|
|
PAL = true;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
sprintf(downloadURL, "%sUS/%s.png", url, gameID);
|
|
|
|
sprintf(CheckedRegion, "US");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strcpy(downloadURL, "");
|
|
|
|
strcpy(CheckedRegion, "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct block file = downloadfile(downloadURL);
|
|
|
|
if(VALID_IMAGE(file))
|
|
|
|
return file;
|
|
|
|
|
|
|
|
free(file.data);
|
|
|
|
file.data = NULL;
|
|
|
|
|
|
|
|
if(PAL && strcmp(CheckedRegion, "EN") != 0)
|
|
|
|
{
|
|
|
|
snprintf(downloadURL, sizeof(downloadURL), "%sEN/%s.png", url, gameID);
|
|
|
|
file = downloadfile(downloadURL);
|
|
|
|
if(VALID_IMAGE(file))
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
else if(strcmp(CheckedRegion, "") == 0)
|
|
|
|
{
|
|
|
|
const char * lang = Settings.db_language;
|
|
|
|
|
|
|
|
if(strcmp(lang, "EN") == 0 && CONF_GetRegion() == CONF_REGION_US)
|
|
|
|
lang = "US";
|
|
|
|
|
|
|
|
snprintf(downloadURL, sizeof(downloadURL), "%s%s/%s.png", url, lang, gameID);
|
|
|
|
file = downloadfile(downloadURL);
|
|
|
|
if(VALID_IMAGE(file))
|
|
|
|
return file;
|
|
|
|
|
|
|
|
free(file.data);
|
|
|
|
|
|
|
|
snprintf(downloadURL, sizeof(downloadURL), "%sOTHER/%s.png", url, gameID);
|
|
|
|
file = downloadfile(downloadURL);
|
|
|
|
if(VALID_IMAGE(file))
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(file.data);
|
|
|
|
|
|
|
|
memset(&file, 0, sizeof(struct block));
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
static int CoverDownloadWithProgress(const char * url, const char * progressTitle, const char * writepath, std::vector<std::string> & MissingFilesList)
|
2010-12-17 18:50:44 +01:00
|
|
|
{
|
|
|
|
if(!url || !writepath)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!CreateSubfolder(writepath))
|
|
|
|
{
|
|
|
|
WindowPrompt(tr( "Error !" ), fmt("%s %s", tr("Can't create directory"), writepath), tr( "OK" ));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char progressMsg[255];
|
|
|
|
int FilesSkipped = MissingFilesList.size();
|
2010-12-18 14:20:45 +01:00
|
|
|
ProgressSetAbortCallback(AbortCallback);
|
2010-12-17 18:50:44 +01:00
|
|
|
|
|
|
|
for(u32 i = 0; i < MissingFilesList.size(); ++i)
|
|
|
|
{
|
|
|
|
if(AbortRequested)
|
|
|
|
break;
|
|
|
|
|
|
|
|
snprintf(progressMsg, sizeof(progressMsg), "http://wiitdb.com : %s.png", MissingFilesList[i].c_str());
|
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
ShowProgress(progressTitle, fmt("%i %s", MissingFilesList.size() - i, tr( "files left" )), progressMsg, i, MissingFilesList.size());
|
2010-12-17 18:50:44 +01:00
|
|
|
|
2010-12-18 14:20:45 +01:00
|
|
|
if(MissingFilesList[i].size() < 4)
|
2010-12-17 18:50:44 +01:00
|
|
|
continue;
|
|
|
|
|
2011-01-11 18:52:30 +01:00
|
|
|
struct block file = DownloadImage(url, MissingFilesList[i].c_str());
|
|
|
|
if(!file.data)
|
|
|
|
continue;
|
2010-12-17 18:50:44 +01:00
|
|
|
|
|
|
|
char imgPath[200];
|
2010-12-18 14:20:45 +01:00
|
|
|
snprintf(imgPath, sizeof(imgPath), "%s/%s.png", writepath, MissingFilesList[i].c_str());
|
2010-12-17 18:50:44 +01:00
|
|
|
|
|
|
|
FILE *pfile = fopen(imgPath, "wb");
|
|
|
|
if (pfile != NULL)
|
|
|
|
{
|
|
|
|
fwrite(file.data, 1, file.size, pfile);
|
|
|
|
fclose(pfile);
|
|
|
|
FilesSkipped--;
|
|
|
|
}
|
|
|
|
free(file.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Reset progress values
|
|
|
|
ProgressStop();
|
|
|
|
ShowProgress(tr("Downloading file"), " ", 0, MissingFilesList.size(), MissingFilesList.size());
|
|
|
|
ProgressSetAbortCallback(NULL);
|
|
|
|
|
|
|
|
return FilesSkipped;
|
|
|
|
}
|
|
|
|
|
2011-01-23 21:13:41 +01:00
|
|
|
static int CoverDownloader(const char * downloadURL, const char *writepath, const char * progressTitle, const char * PromptText, bool skipPrompt)
|
2010-12-17 18:50:44 +01:00
|
|
|
{
|
|
|
|
std::vector<std::string> MissingFilesList;
|
|
|
|
GetMissingGameFiles(writepath, ".png", MissingFilesList);
|
|
|
|
|
|
|
|
if(MissingFilesList.size() == 0)
|
|
|
|
{
|
|
|
|
WindowPrompt(tr( "No file missing!" ), 0, tr("OK"));
|
2011-01-23 21:13:41 +01:00
|
|
|
return -1;
|
2010-12-17 18:50:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsNetworkInit() && !NetworkInitPrompt())
|
2011-01-23 21:13:41 +01:00
|
|
|
return -1;
|
2010-12-17 18:50:44 +01:00
|
|
|
|
2011-01-23 21:13:41 +01:00
|
|
|
if(!skipPrompt)
|
|
|
|
{
|
|
|
|
char tempCnt[80];
|
|
|
|
snprintf(tempCnt, sizeof(tempCnt), "%i %s", MissingFilesList.size(), tr( "Missing files" ));
|
2010-12-17 18:50:44 +01:00
|
|
|
|
2011-01-23 21:13:41 +01:00
|
|
|
if (WindowPrompt(PromptText, tempCnt, tr( "Yes" ), tr( "No" )) == 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2010-12-17 18:50:44 +01:00
|
|
|
|
|
|
|
AbortRequested = false;
|
|
|
|
|
2011-01-23 21:13:41 +01:00
|
|
|
return CoverDownloadWithProgress(downloadURL, progressTitle, writepath, MissingFilesList);
|
|
|
|
}
|
2010-12-17 18:50:44 +01:00
|
|
|
|
2011-01-23 21:13:41 +01:00
|
|
|
void CoverDownload()
|
|
|
|
{
|
|
|
|
int choice = CheckboxWindow(tr( "Cover Download" ), 0, tr( "3D Covers" ), tr( "Flat Covers" ), tr( "Original Disc Images" ), tr( "Custom Disc Images" )); // ask for download choice
|
|
|
|
if (choice == 0)
|
|
|
|
return;
|
2010-12-17 18:50:44 +01:00
|
|
|
|
2011-01-23 21:13:41 +01:00
|
|
|
bool skipPrompt = false;
|
|
|
|
int FileSkipped = 0;
|
|
|
|
int SkippedDiscArts = 0;
|
|
|
|
|
|
|
|
if(choice & CheckedBox1)
|
|
|
|
{
|
|
|
|
int ret = CoverDownloader(serverURL3D, Settings.covers_path, tr("Downloading 3D Covers"), tr("Download Boxart image?"), skipPrompt);
|
|
|
|
if(ret > 0)
|
|
|
|
FileSkipped += ret;
|
|
|
|
skipPrompt = true;
|
|
|
|
}
|
|
|
|
if(choice & CheckedBox2)
|
|
|
|
{
|
|
|
|
int ret = CoverDownloader(serverURL2D, Settings.covers2d_path, tr("Downloading Flat Covers"), tr("Download Boxart image?"), skipPrompt);
|
|
|
|
if(ret > 0)
|
|
|
|
FileSkipped += ret;
|
|
|
|
skipPrompt = true;
|
2010-12-17 18:50:44 +01:00
|
|
|
}
|
2011-01-23 21:13:41 +01:00
|
|
|
if(choice & CheckedBox3)
|
|
|
|
{
|
|
|
|
skipPrompt = true;
|
|
|
|
const char * downloadURL = (Settings.discart == DISCARTS_ORIGINALS_CUSTOMS) ? serverURLOrigDiscs : serverURLCustomDiscs;
|
|
|
|
const char * progressTitle = (Settings.discart == DISCARTS_ORIGINALS_CUSTOMS) ? tr("Downloading original Discarts") : tr("Downloading custom Discarts");
|
|
|
|
int ret = CoverDownloader(downloadURL, Settings.disc_path, progressTitle, tr("Download Discart image?"), skipPrompt);
|
|
|
|
if(ret > 0)
|
|
|
|
SkippedDiscArts = ret;
|
|
|
|
}
|
|
|
|
if(choice & CheckedBox4)
|
|
|
|
{
|
|
|
|
skipPrompt = true;
|
|
|
|
const char * downloadURL = (Settings.discart == DISCARTS_ORIGINALS_CUSTOMS) ? serverURLCustomDiscs : serverURLOrigDiscs;
|
|
|
|
const char * progressTitle = (Settings.discart == DISCARTS_ORIGINALS_CUSTOMS) ? tr("Downloading custom Discarts") : tr("Downloading original Discarts");
|
|
|
|
int ret = CoverDownloader(downloadURL, Settings.disc_path, progressTitle, tr("Download Discart image?"), skipPrompt);
|
|
|
|
if(ret > 0)
|
|
|
|
SkippedDiscArts = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSkipped += SkippedDiscArts;
|
2010-12-17 18:50:44 +01:00
|
|
|
|
|
|
|
if (FileSkipped == 0)
|
|
|
|
{
|
|
|
|
WindowPrompt(tr("Download finished"), tr("All images downloaded successfully."), tr( "OK" ));
|
|
|
|
}
|
2011-01-23 21:13:41 +01:00
|
|
|
else if(FileSkipped > 0)
|
2010-12-17 18:50:44 +01:00
|
|
|
{
|
2011-01-23 21:13:41 +01:00
|
|
|
char tempCnt[80];
|
2010-12-17 18:50:44 +01:00
|
|
|
sprintf(tempCnt, "%i %s", FileSkipped, tr( "files not found on the server!" ));
|
|
|
|
WindowPrompt(tr( "Download finished" ), tempCnt, tr( "OK" ));
|
|
|
|
}
|
|
|
|
}
|