2013-03-15 21:46:16 +01:00
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law . You can redistribute it
* and / or modify it under the terms of the Do What The Fuck You Want
* To Public License , Version 2 , as published by Sam Hocevar . See
2013-03-24 22:56:57 +01:00
* http : //www.wtfpl.net/ for more details. */
2013-03-15 21:46:16 +01:00
# include "downloader.h"
# include "config.h"
# include "util.h"
2013-03-23 19:12:49 +01:00
# include "globalconstants.h"
2013-03-15 21:46:16 +01:00
# include <fstream>
# include <boost/filesystem.hpp>
# include <boost/program_options.hpp>
# if __GNUC__
# if __x86_64__ || __ppc64__ || __LP64__
# define ENVIRONMENT64
# else
# define ENVIRONMENT32
# endif
# endif
2013-03-19 18:29:58 +01:00
# define VERSION_NUMBER "2.3"
# ifndef VERSION_STRING
# define VERSION_STRING "LGOGDownloader " VERSION_NUMBER
# endif
2013-03-15 21:46:16 +01:00
namespace bpo = boost : : program_options ;
int main ( int argc , char * argv [ ] )
{
Config config ;
2013-03-19 18:29:58 +01:00
config . sVersionString = VERSION_STRING ;
2013-03-15 21:46:16 +01:00
config . sHome = ( std : : string ) getenv ( " HOME " ) ;
config . sCookiePath = config . sHome + " /.gogdownloader/cookies.txt " ;
config . sConfigFilePath = config . sHome + " /.gogdownloader/config.cfg " ;
config . sXMLDirectory = config . sHome + " /.gogdownloader/xml " ;
2013-03-16 22:35:57 +01:00
// Create gogdownloader directories
boost : : filesystem : : path path = config . sXMLDirectory ;
if ( ! boost : : filesystem : : exists ( path ) )
{
if ( ! boost : : filesystem : : create_directories ( path ) )
{
std : : cout < < " Failed to create directory: " < < path < < std : : endl ;
return 1 ;
}
}
2013-03-15 21:46:16 +01:00
// Create help text for --platform option
2013-03-23 19:12:49 +01:00
std : : string platform_text = " Select which installers are downloaded \n " ;
unsigned int platform_sum = 0 ;
for ( unsigned int i = 0 ; i < GlobalConstants : : INSTALLERS . size ( ) ; + + i )
{
platform_text + = std : : to_string ( GlobalConstants : : INSTALLERS [ i ] . installerId ) + " = " + GlobalConstants : : INSTALLERS [ i ] . installerString + " \n " ;
platform_sum + = GlobalConstants : : LANGUAGES [ i ] . languageId ;
}
platform_text + = std : : to_string ( platform_sum ) + " = All " ;
2013-03-15 21:46:16 +01:00
// Create help text for --language option
2013-03-23 19:12:49 +01:00
std : : string language_text = " Select which language installers are downloaded \n " ;
unsigned int language_sum = 0 ;
for ( unsigned int i = 0 ; i < GlobalConstants : : LANGUAGES . size ( ) ; + + i )
{
language_text + = std : : to_string ( GlobalConstants : : LANGUAGES [ i ] . languageId ) + " = " + GlobalConstants : : LANGUAGES [ i ] . languageString + " \n " ;
language_sum + = GlobalConstants : : LANGUAGES [ i ] . languageId ;
}
language_text + = " Add the values to download multiple languages \n All = " + std : : to_string ( language_sum ) + " \n "
+ " French + Polish = " + std : : to_string ( GlobalConstants : : LANGUAGE_FR ) + " + " + std : : to_string ( GlobalConstants : : LANGUAGE_PL ) + " = " + std : : to_string ( GlobalConstants : : LANGUAGE_FR | GlobalConstants : : LANGUAGE_PL ) ;
2013-03-15 21:46:16 +01:00
bpo : : variables_map vm ;
bpo : : options_description desc ( " Options " ) ;
bpo : : options_description config_file_options ( " Configuration " ) ;
try
{
desc . add_options ( )
( " help,h " , " Print help message " )
( " login " , bpo : : value < bool > ( & config . bLogin ) - > zero_tokens ( ) - > default_value ( false ) , " Login " )
( " list " , bpo : : value < bool > ( & config . bList ) - > zero_tokens ( ) - > default_value ( false ) , " List games " )
( " list-details " , bpo : : value < bool > ( & config . bListDetails ) - > zero_tokens ( ) - > default_value ( false ) , " List games with detailed info " )
( " download " , bpo : : value < bool > ( & config . bDownload ) - > zero_tokens ( ) - > default_value ( false ) , " Download " )
2013-04-05 20:19:03 +02:00
( " repair " , bpo : : value < bool > ( & config . bRepair ) - > zero_tokens ( ) - > default_value ( false ) , " Repair downloaded files \n Use --repair --download to redownload files when filesizes don't match (possibly different version). This will delete the old file " )
2013-03-15 21:46:16 +01:00
( " game " , bpo : : value < std : : string > ( & config . sGameRegex ) - > default_value ( " " ) , " Set regular expression filter \n for download/list/repair (Perl syntax) \n Aliases: \" all \" , \" free \" " )
( " directory " , bpo : : value < std : : string > ( & config . sDirectory ) - > default_value ( " " ) , " Set download directory " )
# ifndef ENVIRONMENT32
( " limit-rate " , bpo : : value < curl_off_t > ( & config . iDownloadRate ) - > default_value ( 0 ) , " Limit download rate to value in kB \n 0 = unlimited " )
# endif
( " create-xml " , bpo : : value < std : : string > ( & config . sXMLFile ) - > default_value ( " " ) , " Create GOG XML for file \n \" automatic \" to enable automatic XML creation " )
( " xml-directory " , bpo : : value < std : : string > ( & config . sXMLDirectory ) , " Set directory for GOG XML files " )
( " chunk-size " , bpo : : value < size_t > ( & config . iChunkSize ) - > default_value ( 10 ) , " Chunk size (in MB) when creating XML " )
( " update-check " , bpo : : value < bool > ( & config . bUpdateCheck ) - > zero_tokens ( ) - > default_value ( false ) , " Check for update notifications " )
2013-03-23 19:12:49 +01:00
( " platform " , bpo : : value < unsigned int > ( & config . iInstallerType ) - > default_value ( GlobalConstants : : INSTALLER_WINDOWS ) , platform_text . c_str ( ) )
( " language " , bpo : : value < unsigned int > ( & config . iInstallerLanguage ) - > default_value ( GlobalConstants : : LANGUAGE_EN ) , language_text . c_str ( ) )
2013-03-15 21:46:16 +01:00
( " no-installers " , bpo : : value < bool > ( & config . bNoInstallers ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download/list/repair installers " )
( " no-extras " , bpo : : value < bool > ( & config . bNoExtras ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download/list/repair extras " )
( " no-cover " , bpo : : value < bool > ( & config . bNoCover ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download cover images " )
( " no-remote-xml " , bpo : : value < bool > ( & config . bNoRemoteXML ) - > zero_tokens ( ) - > default_value ( false ) , " Don't use remote XML for repair " )
( " no-unicode " , bpo : : value < bool > ( & config . bNoUnicode ) - > zero_tokens ( ) - > default_value ( false ) , " Don't use Unicode in the progress bar " )
( " no-color " , bpo : : value < bool > ( & config . bNoColor ) - > zero_tokens ( ) - > default_value ( false ) , " Don't use coloring in the progress bar " )
( " verbose " , bpo : : value < bool > ( & config . bVerbose ) - > zero_tokens ( ) - > default_value ( false ) , " Print lots of information " )
;
bpo : : store ( bpo : : parse_command_line ( argc , argv , desc ) , vm ) ;
bpo : : notify ( vm ) ;
// Read token and secret from config file
config_file_options . add_options ( )
( " token " , bpo : : value < std : : string > ( & config . sToken ) - > default_value ( " " ) , " oauth token " )
( " secret " , bpo : : value < std : : string > ( & config . sSecret ) - > default_value ( " " ) , " oauth secret " )
;
if ( boost : : filesystem : : exists ( config . sConfigFilePath ) )
{
std : : ifstream ifs ( config . sConfigFilePath . c_str ( ) ) ;
if ( ! ifs )
{
std : : cout < < " Could not open config file: " < < config . sConfigFilePath < < std : : endl ;
return 1 ;
}
else
{
bpo : : store ( bpo : : parse_config_file ( ifs , config_file_options ) , vm ) ;
bpo : : notify ( vm ) ;
ifs . close ( ) ;
}
}
if ( vm . count ( " help " ) )
{
std : : cout < < config . sVersionString < < std : : endl
< < desc < < std : : endl ;
return 0 ;
}
if ( vm . count ( " chunk-size " ) )
config . iChunkSize < < = 20 ; // Convert chunk size from bytes to megabytes
if ( vm . count ( " limit-rate " ) )
config . iDownloadRate < < = 10 ; // Convert download rate from bytes to kilobytes
}
catch ( std : : exception & e )
{
std : : cerr < < " Error: " < < e . what ( ) < < std : : endl ;
return 1 ;
}
catch ( . . . )
{
std : : cerr < < " Exception of unknown type! " < < std : : endl ;
return 1 ;
}
2013-03-23 19:12:49 +01:00
if ( config . iInstallerType < GlobalConstants : : INSTALLER_WINDOWS | | config . iInstallerType > platform_sum )
2013-03-15 21:46:16 +01:00
{
std : : cout < < " Invalid value for --platform " < < std : : endl ;
return 1 ;
}
2013-03-23 19:12:49 +01:00
if ( config . iInstallerType < GlobalConstants : : LANGUAGE_EN | | config . iInstallerType > language_sum )
{
std : : cout < < " Invalid value for --language " < < std : : endl ;
return 1 ;
}
2013-03-15 21:46:16 +01:00
if ( ! config . sXMLDirectory . empty ( ) )
{
// Make sure that xml directory doesn't have trailing slash
if ( config . sXMLDirectory . at ( config . sXMLDirectory . length ( ) - 1 ) = = ' / ' )
config . sXMLDirectory . assign ( config . sXMLDirectory . begin ( ) , config . sXMLDirectory . end ( ) - 1 ) ;
}
// Create GOG XML for a file
if ( ! config . sXMLFile . empty ( ) & & ( config . sXMLFile ! = " automatic " ) )
{
Util : : createXML ( config . sXMLFile , config . iChunkSize , config . sXMLDirectory ) ;
return 0 ;
}
// Make sure that directory has trailing slash
if ( ! config . sDirectory . empty ( ) )
if ( config . sDirectory . at ( config . sDirectory . length ( ) - 1 ) ! = ' / ' )
2013-03-16 22:35:57 +01:00
config . sDirectory + = " / " ;
2013-03-15 21:46:16 +01:00
Downloader downloader ( config ) ;
int result = downloader . init ( ) ;
if ( config . bLogin )
return result ;
2013-03-16 02:22:45 +01:00
else if ( config . bUpdateCheck ) // Update check has priority over download and list
downloader . updateCheck ( ) ;
2013-03-15 21:46:16 +01:00
else if ( config . bRepair ) // Repair file
downloader . repair ( ) ;
2013-04-05 20:19:03 +02:00
else if ( config . bDownload ) // Download games
downloader . download ( ) ;
2013-03-15 21:46:16 +01:00
else if ( config . bListDetails | | config . bList ) // Detailed list of games/extras
downloader . listGames ( ) ;
else
{ // Show help message
std : : cout < < config . sVersionString < < std : : endl
< < desc < < std : : endl ;
}
return 0 ;
}