2010-09-19 01:16:05 +02:00
/***************************************************************************
2010-09-24 02:48:03 +02:00
* Copyright ( C ) 2009
* by Dimok
*
* This software is provided ' as - is ' , without any express or implied
* warranty . In no event will the authors be held liable for any
* damages arising from the use of this software .
*
* Permission is granted to anyone to use this software for any
* purpose , including commercial applications , and to alter it and
* redistribute it freely , subject to the following restrictions :
*
* 1. The origin of this software must not be misrepresented ; you
* must not claim that you wrote the original software . If you use
* this software in a product , an acknowledgment in the product
* documentation would be appreciated but is not required .
*
* 2. Altered source versions must be plainly marked as such , and
* must not be misrepresented as being the original software .
*
* 3. This notice may not be removed or altered from any source
* distribution .
*
* update . cpp
*
* Update operations
* for Wii - Xplorer 2009
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-05-30 09:00:29 +02:00
# include <stdio.h>
# include <string.h>
# include <ogcsys.h>
2010-12-03 19:38:57 +01:00
# include <string>
2010-05-30 09:00:29 +02:00
2010-12-03 19:38:57 +01:00
# include "gecko.h"
# include "ZipFile.h"
2010-05-30 09:00:29 +02:00
# include "http.h"
# include "networkops.h"
# include "HTML_Stream.h"
2010-12-03 19:38:57 +01:00
# include "FileDownloader.h"
# include "settings/CSettings.h"
# include "settings/GameTitles.h"
2010-12-18 14:20:45 +01:00
# include "language/gettext.h"
# include "language/UpdateLanguage.h"
# include "homebrewboot/BootHomebrew.h"
# include "utils/StringTools.h"
# include "utils/ShowError.h"
# include "prompts/PromptWindows.h"
# include "FileOperations/fileops.h"
2011-10-21 20:48:10 +02:00
# include "xml/GameTDB.hpp"
2011-01-09 17:03:29 +01:00
# include "wad/nandtitle.h"
# include "wad/wad.h"
# include "sys.h"
2011-01-09 11:45:29 +01:00
# include "svnrev.h"
2010-12-03 19:38:57 +01:00
2011-10-22 12:37:58 +02:00
static const char * GameTDB_URL = " http://www.gametdb.com/wiitdb.zip " ;
2010-05-30 09:00:29 +02:00
/****************************************************************************
* Checking if an Update is available
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int CheckForBetaUpdate ( )
{
2011-07-26 00:28:22 +02:00
int revnumber = 0 ;
HTML_Stream HTML ( " http://code.google.com/p/usbloader-gui/downloads/list " ) ;
const char * HTML_Pos = NULL ;
do
{
HTML_Pos = HTML . FindStringEnd ( " href=' " ) ;
char * tmpLink = HTML . CopyString ( " ' \" " ) ;
if ( tmpLink )
{
char * fileext = strrchr ( tmpLink , ' . ' ) ;
if ( fileext )
{
if ( strcasecmp ( fileext , " .dol " ) = = 0 )
{
char revtxt [ 80 ] ;
char * filename = strrchr ( tmpLink , ' / ' ) + 2 ;
u8 n = 0 ;
for ( n = 0 ; n < strlen ( filename ) - 2 ; n + + )
revtxt [ n ] = filename [ n ] ;
revtxt [ n ] = 0 ;
int fileRev = atoi ( revtxt ) ;
if ( fileRev > revnumber )
{
revnumber = fileRev ;
}
}
}
free ( tmpLink ) ;
}
} while ( HTML_Pos ! = NULL ) ;
return revnumber ;
2010-05-30 09:00:29 +02:00
}
2010-12-03 19:38:57 +01:00
2011-10-21 20:48:10 +02:00
static bool CheckNewGameTDBVersion ( const char * url )
2010-12-03 19:38:57 +01:00
{
2011-07-26 00:28:22 +02:00
u64 Version = 0 ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
char * HEAD_Responde = HEAD_Request ( url ) ;
if ( ! HEAD_Responde )
return false ;
2010-12-03 19:38:57 +01:00
2011-10-21 20:48:10 +02:00
char * version_ptr = strstr ( HEAD_Responde , " X-GameTDB-Timestamp: " ) ;
2011-07-26 00:28:22 +02:00
if ( version_ptr )
{
2011-10-21 20:48:10 +02:00
version_ptr + = strlen ( " X-GameTDB-Timestamp: " ) ;
2011-07-26 00:28:22 +02:00
Version = strtoull ( version_ptr , NULL , 10 ) ;
}
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
free ( HEAD_Responde ) ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
std : : string Title ;
std : : string Filepath = Settings . titlestxt_path ;
if ( Settings . titlestxt_path [ Filepath . size ( ) - 1 ] ! = ' / ' )
Filepath + = ' / ' ;
Filepath + = " wiitdb.xml " ;
2010-12-03 19:38:57 +01:00
2011-10-21 20:48:10 +02:00
GameTDB XML_DB ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
if ( ! XML_DB . OpenFile ( ( Filepath . c_str ( ) ) ) )
return true ; //! If no file exists we need the file
2010-12-03 19:38:57 +01:00
2011-10-21 20:48:10 +02:00
u64 ExistingVersion = XML_DB . GetGameTDBVersion ( ) ;
2010-12-03 19:38:57 +01:00
2011-10-21 20:48:10 +02:00
gprintf ( " Existing GameTDB Version: %llu Online GameTDB Version: %llu \n " , ExistingVersion , Version ) ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
return ( ExistingVersion ! = Version ) ;
2010-12-03 19:38:57 +01:00
}
2011-10-21 20:48:10 +02:00
int UpdateGameTDB ( )
2010-12-03 19:38:57 +01:00
{
2011-10-21 20:48:10 +02:00
if ( CheckNewGameTDBVersion ( GameTDB_URL ) = = false )
2011-07-26 00:28:22 +02:00
{
2011-10-21 20:48:10 +02:00
gprintf ( " Not updating GameTDB: Version is the same \n " ) ;
2011-07-26 00:28:22 +02:00
return - 1 ;
}
2010-12-03 19:38:57 +01:00
2011-10-21 20:48:10 +02:00
gprintf ( " Updating GameTDB... \n " ) ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
string ZipPath = Settings . titlestxt_path ;
if ( Settings . titlestxt_path [ ZipPath . size ( ) - 1 ] ! = ' / ' )
ZipPath + = ' / ' ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
ZipPath + = " wiitdb.zip " ;
2010-12-03 19:38:57 +01:00
2011-10-21 20:48:10 +02:00
int filesize = DownloadFileToPath ( GameTDB_URL , ZipPath . c_str ( ) , false ) ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
if ( filesize < = 0 )
return - 1 ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
ZipFile zFile ( ZipPath . c_str ( ) ) ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
bool result = zFile . ExtractAll ( Settings . titlestxt_path ) ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
//! The zip file is not needed anymore so we can remove it
remove ( ZipPath . c_str ( ) ) ;
2010-12-03 19:38:57 +01:00
2012-01-08 19:24:46 +01:00
//! Reload all titles and reload cached titles because the file changed now.
GameTitles . SetDefault ( ) ;
GameTitles . LoadTitlesFromGameTDB ( Settings . titlestxt_path ) ;
2010-12-03 19:38:57 +01:00
2011-07-26 00:28:22 +02:00
return ( result ? filesize : - 1 ) ;
2010-12-03 19:38:57 +01:00
}
2010-12-18 14:20:45 +01:00
static void UpdateIconPng ( )
{
2011-07-26 00:28:22 +02:00
char iconpath [ 200 ] ;
2012-05-09 21:27:54 +02:00
struct block file = downloadfile ( " http://usbloader-gui.googlecode.com/svn/branches/updates/icon.png " ) ;
2011-07-26 00:28:22 +02:00
if ( file . data ! = NULL )
{
snprintf ( iconpath , sizeof ( iconpath ) , " %sicon.png " , Settings . update_path ) ;
FILE * pfile = fopen ( iconpath , " wb " ) ;
if ( pfile )
{
fwrite ( file . data , 1 , file . size , pfile ) ;
fclose ( pfile ) ;
}
free ( file . data ) ;
}
2010-12-18 14:20:45 +01:00
}
static void UpdateMetaXml ( )
{
2011-07-26 00:28:22 +02:00
char xmlpath [ 200 ] ;
2012-05-09 21:27:54 +02:00
struct block file = downloadfile ( " http://usbloader-gui.googlecode.com/svn/branches/updates/meta.xml " ) ;
2011-07-26 00:28:22 +02:00
if ( file . data ! = NULL )
{
snprintf ( xmlpath , sizeof ( xmlpath ) , " %smeta.xml " , Settings . update_path ) ;
FILE * pfile = fopen ( xmlpath , " wb " ) ;
if ( pfile )
{
fwrite ( file . data , 1 , file . size , pfile ) ;
fclose ( pfile ) ;
}
free ( file . data ) ;
}
2010-12-18 14:20:45 +01:00
}
2011-01-09 11:45:29 +01:00
int CheckUpdate ( )
{
2011-07-26 00:28:22 +02:00
if ( ! IsNetworkInit ( ) )
return - 1 ;
2011-01-09 11:45:29 +01:00
2011-07-26 00:28:22 +02:00
int revnumber = 0 ;
int currentrev = atoi ( GetRev ( ) ) ;
2011-01-09 11:45:29 +01:00
# ifdef FULLCHANNEL
2012-05-09 21:27:54 +02:00
struct block file = downloadfile ( " http://usbloader-gui.googlecode.com/svn/branches/updates/update_wad.txt " ) ;
2011-01-09 11:45:29 +01:00
# else
2012-05-09 21:27:54 +02:00
struct block file = downloadfile ( " http://usbloader-gui.googlecode.com/svn/branches/updates/update_dol.txt " ) ;
2011-01-09 11:45:29 +01:00
# endif
2011-07-26 00:28:22 +02:00
if ( file . data ! = NULL )
{
revnumber = atoi ( ( char * ) file . data ) ;
free ( file . data ) ;
}
2011-01-09 11:45:29 +01:00
2011-07-26 00:28:22 +02:00
if ( revnumber > currentrev )
return revnumber ;
2011-01-09 11:45:29 +01:00
2011-07-26 00:28:22 +02:00
return - 1 ;
2011-01-09 11:45:29 +01:00
}
2012-05-09 21:27:54 +02:00
static int ApplicationDownload ( void )
2010-12-18 14:20:45 +01:00
{
2012-05-09 21:27:54 +02:00
std : : string DownloadURL ;
int newrev = 0 ;
int currentrev = atoi ( GetRev ( ) ) ;
# ifdef FULLCHANNEL
struct block file = downloadfile ( " http://usbloader-gui.googlecode.com/svn/branches/updates/update_wad.txt " ) ;
# else
struct block file = downloadfile ( " http://usbloader-gui.googlecode.com/svn/branches/updates/update_dol.txt " ) ;
# endif
if ( file . data ! = NULL )
{
// first line of the text file is the revisionc
newrev = atoi ( ( char * ) file . data ) ;
// 2nd line of the text file is the url
char * ptr = strchr ( ( char * ) file . data , ' \n ' ) ;
while ( ptr & & ( * ptr = = ' \r ' | | * ptr = = ' \n ' | | * ptr = = ' ' ) )
ptr + + ;
while ( ptr & & * ptr ! = ' \0 ' & & * ptr ! = ' \r ' & & * ptr ! = ' \n ' )
{
DownloadURL . push_back ( * ptr ) ;
ptr + + ;
}
free ( file . data ) ;
}
if ( newrev < = currentrev )
{
WindowPrompt ( tr ( " No new updates. " ) , 0 , tr ( " OK " ) ) ;
return 0 ;
}
2011-07-26 00:28:22 +02:00
bool update_error = false ;
char tmppath [ 250 ] ;
# ifdef FULLCHANNEL
snprintf ( tmppath , sizeof ( tmppath ) , " %s/ULNR.wad " , Settings . BootDevice ) ;
# else
char realpath [ 250 ] ;
snprintf ( realpath , sizeof ( realpath ) , " %sboot.dol " , Settings . update_path ) ;
snprintf ( tmppath , sizeof ( tmppath ) , " %sboot.tmp " , Settings . update_path ) ;
# endif
int update_choice = WindowPrompt ( fmt ( " Rev%i %s. " , newrev , tr ( " available " ) ) , tr ( " How do you want to update? " ) , tr ( " Update DOL " ) , tr ( " Update All " ) , tr ( " Cancel " ) ) ;
if ( update_choice = = 0 )
return 0 ;
2012-05-09 21:27:54 +02:00
int ret = DownloadFileToPath ( DownloadURL . c_str ( ) , tmppath , false ) ;
2011-07-26 00:28:22 +02:00
if ( ret < 1024 * 1024 )
{
remove ( tmppath ) ;
WindowPrompt ( tr ( " Failed updating " ) , tr ( " Error while downloding file " ) , tr ( " OK " ) ) ;
if ( update_choice = = 1 )
return - 1 ;
update_error = true ;
}
else
{
# ifdef FULLCHANNEL
FILE * wadFile = fopen ( tmppath , " rb " ) ;
if ( ! wadFile )
{
update_error = true ;
WindowPrompt ( tr ( " Failed updating " ) , tr ( " Error opening downloaded file " ) , tr ( " OK " ) ) ;
return - 1 ;
}
int error = Wad_Install ( wadFile ) ;
if ( error )
{
update_error = true ;
ShowError ( tr ( " The wad installation failed with error %i " ) , error ) ;
}
else
WindowPrompt ( tr ( " Success " ) , tr ( " The wad file was installed " ) , tr ( " OK " ) ) ;
RemoveFile ( tmppath ) ;
# else
gprintf ( " %s \n %s \n " , realpath , tmppath ) ;
RemoveFile ( realpath ) ;
if ( ! RenameFile ( tmppath , realpath ) )
update_error = true ;
# endif
}
if ( update_choice = = 2 )
{
UpdateIconPng ( ) ;
UpdateMetaXml ( ) ;
2011-10-21 20:48:10 +02:00
UpdateGameTDB ( ) ;
2011-07-26 00:28:22 +02:00
DownloadAllLanguageFiles ( ) ;
}
if ( update_error )
{
ShowError ( tr ( " Error while updating USB Loader GX. " ) ) ;
return - 1 ;
}
if ( update_choice > 0 )
{
2012-05-09 21:27:54 +02:00
WindowPrompt ( tr ( " Successfully updated. " ) , tr ( " Restarting... " ) , 0 , 0 , 0 , 0 , 150 ) ;
2011-07-26 00:28:22 +02:00
RebootApp ( ) ;
}
return 0 ;
2010-12-18 14:20:45 +01:00
}
int UpdateApp ( )
{
2011-07-26 00:28:22 +02:00
if ( ! IsNetworkInit ( ) & & ! NetworkInitPrompt ( ) )
{
WindowPrompt ( tr ( " Error ! " ) , tr ( " Could not initialize network! " ) , tr ( " OK " ) ) ;
return - 1 ;
}
if ( ! CreateSubfolder ( Settings . update_path ) )
{
WindowPrompt ( tr ( " Error ! " ) , tr ( " Can't create directory " ) , tr ( " OK " ) ) ;
return - 1 ;
}
2011-10-22 12:37:58 +02:00
int choice = WindowPrompt ( tr ( " What do you want to update? " ) , 0 , " USB Loader GX " , tr ( " WiiTDB.xml " ) , tr ( " Language Files " ) , tr ( " Cancel " ) ) ;
2011-07-26 00:28:22 +02:00
if ( choice = = 0 )
2011-10-22 12:37:58 +02:00
return 0 ;
2011-07-26 00:28:22 +02:00
if ( choice = = 1 )
2012-05-09 21:27:54 +02:00
{
return ApplicationDownload ( ) ;
2011-07-26 00:28:22 +02:00
}
else if ( choice = = 2 )
{
2011-10-21 20:48:10 +02:00
if ( UpdateGameTDB ( ) < 0 )
2011-07-26 00:28:22 +02:00
{
2011-10-22 12:37:58 +02:00
WindowPrompt ( fmt ( " %s " , tr ( " WiiTDB.xml is up to date. " ) ) , 0 , tr ( " OK " ) ) ;
2011-07-26 00:28:22 +02:00
return 1 ;
}
else
{
WindowPrompt ( tr ( " Successfully Updated " ) , 0 , tr ( " OK " ) ) ;
return 1 ;
}
}
else if ( choice = = 3 )
{
if ( UpdateLanguageFiles ( ) > 0 )
WindowPrompt ( tr ( " Successfully Updated " ) , 0 , tr ( " OK " ) ) ;
}
return 1 ;
2010-12-18 14:20:45 +01:00
}