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>
2014-01-05 15:06:19 +01:00
# define VERSION_NUMBER "2.11"
2013-03-19 18:29:58 +01:00
# 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-12-11 10:27:53 +01:00
char * xdgconfig = getenv ( " XDG_CONFIG_HOME " ) ;
char * xdgcache = getenv ( " XDG_CACHE_HOME " ) ;
std : : string home = ( std : : string ) getenv ( " HOME " ) ;
2013-03-15 21:46:16 +01:00
2013-12-11 10:27:53 +01:00
if ( xdgconfig )
{
config . sConfigDirectory = ( std : : string ) xdgconfig + " /lgogdownloader " ;
config . sCookiePath = config . sConfigDirectory + " /cookies.txt " ;
config . sConfigFilePath = config . sConfigDirectory + " /config.cfg " ;
}
else
{
config . sConfigDirectory = home + " /.config/lgogdownloader " ;
config . sCookiePath = config . sConfigDirectory + " /cookies.txt " ;
config . sConfigFilePath = config . sConfigDirectory + " /config.cfg " ;
}
if ( xdgcache )
config . sXMLDirectory = ( std : : string ) xdgcache + " /lgogdownloader/xml " ;
else
config . sXMLDirectory = home + " /.cache/lgogdownloader/xml " ;
// Create lgogdownloader directories
2013-03-16 22:35:57 +01:00
boost : : filesystem : : path path = config . sXMLDirectory ;
if ( ! boost : : filesystem : : exists ( path ) )
{
if ( ! boost : : filesystem : : create_directories ( path ) )
2013-12-11 10:27:53 +01:00
{
std : : cout < < " Failed to create directory: " < < path < < std : : endl ;
return 1 ;
}
}
path = config . sConfigDirectory ;
if ( ! boost : : filesystem : : exists ( path ) )
{
if ( ! boost : : filesystem : : create_directories ( path ) )
2013-03-16 22:35:57 +01:00
{
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 ;
2013-04-25 09:48:10 +02:00
for ( unsigned int i = 0 ; i < GlobalConstants : : PLATFORMS . size ( ) ; + + i )
2013-03-23 19:12:49 +01:00
{
2013-04-25 09:48:10 +02:00
platform_text + = std : : to_string ( GlobalConstants : : PLATFORMS [ i ] . platformId ) + " = " + GlobalConstants : : PLATFORMS [ i ] . platformString + " \n " ;
2013-03-23 19:12:49 +01:00
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
2013-12-22 12:02:48 +01:00
// Create help text for --check-orphans
std : : string orphans_regex_default = " .* \\ .(zip|exe|bin|dmg|old)$ " ; // Limit to files with these extensions (".old" is for renamed older version files)
std : : string check_orphans_text = " Check for orphaned files (files found on local filesystem that are not found on GOG servers). Sets regular expression filter (Perl syntax) for files to check. If no argument is given then the regex defaults to ' " + orphans_regex_default + " ' " ;
2013-03-15 21:46:16 +01:00
bpo : : variables_map vm ;
2014-02-03 18:28:48 +01:00
bpo : : options_description options_cli_all ( " Options " ) ;
bpo : : options_description options_cli_no_cfg ;
bpo : : options_description options_cli_cfg ;
bpo : : options_description options_cfg_only ;
bpo : : options_description options_cfg_all ( " Configuration " ) ;
2013-03-15 21:46:16 +01:00
try
{
2013-08-02 14:54:37 +02:00
bool bInsecure = false ;
2013-11-14 14:40:59 +01:00
bool bNoColor = false ;
bool bNoUnicode = false ;
2013-11-18 12:19:02 +01:00
bool bNoDuplicateHandler = false ;
2013-11-19 11:47:10 +01:00
bool bNoCover = false ;
bool bNoInstallers = false ;
bool bNoExtras = false ;
bool bNoPatches = false ;
bool bNoLanguagePacks = false ;
bool bNoRemoteXML = false ;
2014-02-03 18:28:48 +01:00
// Commandline options (no config file)
options_cli_no_cfg . add_options ( )
2013-03-15 21:46:16 +01:00
( " help,h " , " Print help message " )
2014-02-06 08:27:30 +01:00
( " version " , " Print version information " )
2013-03-15 21:46:16 +01:00
( " 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-11 12:49:24 +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). Redownload 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 \" " )
2014-02-03 18:28:48 +01:00
( " create-xml " , bpo : : value < std : : string > ( & config . sXMLFile ) - > default_value ( " " ) , " Create GOG XML for file \n \" automatic \" to enable automatic XML creation " )
( " update-check " , bpo : : value < bool > ( & config . bUpdateCheck ) - > zero_tokens ( ) - > default_value ( false ) , " Check for update notifications " )
( " check-orphans " , bpo : : value < std : : string > ( & config . sOrphanRegex ) - > implicit_value ( " " ) , check_orphans_text . c_str ( ) )
( " status " , bpo : : value < bool > ( & config . bCheckStatus ) - > zero_tokens ( ) - > default_value ( false ) , " Show status of files \n \n Output format: \n statuscode gamename filename filesize filehash \n \n Status codes: \n OK - File is OK \n ND - File is not downloaded \n MD5 - MD5 mismatch, different version " )
( " save-config " , bpo : : value < bool > ( & config . bSaveConfig ) - > zero_tokens ( ) - > default_value ( false ) , " Create config file with current settings " )
( " reset-config " , bpo : : value < bool > ( & config . bResetConfig ) - > zero_tokens ( ) - > default_value ( false ) , " Reset config settings to default " )
2014-02-13 09:05:16 +01:00
( " report " , bpo : : value < bool > ( & config . bReport ) - > zero_tokens ( ) - > default_value ( false ) , " Save report of downloaded/repaired files " )
2014-02-03 18:28:48 +01:00
;
// Commandline options (config file)
options_cli_cfg . add_options ( )
2013-03-15 21:46:16 +01:00
( " directory " , bpo : : value < std : : string > ( & config . sDirectory ) - > default_value ( " " ) , " Set download directory " )
2013-06-28 14:49:00 +02:00
( " limit-rate " , bpo : : value < curl_off_t > ( & config . iDownloadRate ) - > default_value ( 0 ) , " Limit download rate to value in kB \n 0 = unlimited " )
2013-03-15 21:46:16 +01:00
( " 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 " )
2013-04-25 09:48:10 +02:00
( " platform " , bpo : : value < unsigned int > ( & config . iInstallerType ) - > default_value ( GlobalConstants : : PLATFORM_WINDOWS ) , platform_text . c_str ( ) )
2013-03-23 19:12:49 +01:00
( " language " , bpo : : value < unsigned int > ( & config . iInstallerLanguage ) - > default_value ( GlobalConstants : : LANGUAGE_EN ) , language_text . c_str ( ) )
2013-11-19 11:47:10 +01:00
( " no-installers " , bpo : : value < bool > ( & bNoInstallers ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download/list/repair installers " )
( " no-extras " , bpo : : value < bool > ( & bNoExtras ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download/list/repair extras " )
( " no-patches " , bpo : : value < bool > ( & bNoPatches ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download/list/repair patches " )
( " no-language-packs " , bpo : : value < bool > ( & bNoLanguagePacks ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download/list/repair language packs " )
( " no-cover " , bpo : : value < bool > ( & bNoCover ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download cover images " )
( " no-remote-xml " , bpo : : value < bool > ( & bNoRemoteXML ) - > zero_tokens ( ) - > default_value ( false ) , " Don't use remote XML for repair " )
2013-11-14 14:40:59 +01:00
( " no-unicode " , bpo : : value < bool > ( & bNoUnicode ) - > zero_tokens ( ) - > default_value ( false ) , " Don't use Unicode in the progress bar " )
( " no-color " , bpo : : value < bool > ( & bNoColor ) - > zero_tokens ( ) - > default_value ( false ) , " Don't use coloring in the progress bar " )
2013-11-18 12:19:02 +01:00
( " no-duplicate-handling " , bpo : : value < bool > ( & bNoDuplicateHandler ) - > zero_tokens ( ) - > default_value ( false ) , " Don't use duplicate handler for installers \n Duplicate installers from different languages are handled separately " )
2013-03-15 21:46:16 +01:00
( " verbose " , bpo : : value < bool > ( & config . bVerbose ) - > zero_tokens ( ) - > default_value ( false ) , " Print lots of information " )
2013-08-02 14:54:37 +02:00
( " insecure " , bpo : : value < bool > ( & bInsecure ) - > zero_tokens ( ) - > default_value ( false ) , " Don't verify authenticity of SSL certificates " )
2013-06-10 14:15:08 +02:00
( " timeout " , bpo : : value < long int > ( & config . iTimeout ) - > default_value ( 10 ) , " Set timeout for connection \n Maximum time in seconds that connection phase is allowed to take " )
2013-03-15 21:46:16 +01:00
;
2014-02-03 18:28:48 +01:00
// Options read from config file
options_cfg_only . add_options ( )
2013-03-15 21:46:16 +01:00
( " token " , bpo : : value < std : : string > ( & config . sToken ) - > default_value ( " " ) , " oauth token " )
( " secret " , bpo : : value < std : : string > ( & config . sSecret ) - > default_value ( " " ) , " oauth secret " )
;
2014-02-03 18:28:48 +01:00
options_cli_all . add ( options_cli_no_cfg ) . add ( options_cli_cfg ) ;
options_cfg_all . add ( options_cfg_only ) . add ( options_cli_cfg ) ;
bpo : : store ( bpo : : parse_command_line ( argc , argv , options_cli_all ) , vm ) ;
bpo : : notify ( vm ) ;
2013-03-15 21:46:16 +01:00
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
{
2014-02-03 18:28:48 +01:00
bpo : : store ( bpo : : parse_config_file ( ifs , options_cfg_all ) , vm ) ;
2013-03-15 21:46:16 +01:00
bpo : : notify ( vm ) ;
ifs . close ( ) ;
}
}
if ( vm . count ( " help " ) )
{
std : : cout < < config . sVersionString < < std : : endl
2014-02-03 18:28:48 +01:00
< < options_cli_all < < std : : endl ;
2013-03-15 21:46:16 +01:00
return 0 ;
}
2014-02-06 08:27:30 +01:00
if ( vm . count ( " version " ) )
{
std : : cout < < VERSION_STRING < < std : : endl ;
return 0 ;
}
2013-03-15 21:46:16 +01:00
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
2013-04-25 11:05:17 +02:00
2013-12-22 12:02:48 +01:00
if ( vm . count ( " check-orphans " ) )
if ( config . sOrphanRegex . empty ( ) )
config . sOrphanRegex = orphans_regex_default ;
2013-08-02 14:54:37 +02:00
config . bVerifyPeer = ! bInsecure ;
2013-11-14 14:40:59 +01:00
config . bColor = ! bNoColor ;
config . bUnicode = ! bNoUnicode ;
2013-11-18 12:19:02 +01:00
config . bDuplicateHandler = ! bNoDuplicateHandler ;
2013-11-19 11:47:10 +01:00
config . bCover = ! bNoCover ;
config . bInstallers = ! bNoInstallers ;
config . bExtras = ! bNoExtras ;
config . bPatches = ! bNoPatches ;
config . bLanguagePacks = ! bNoLanguagePacks ;
config . bRemoteXML = ! bNoRemoteXML ;
2013-03-15 21:46:16 +01:00
}
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-04-25 11:12:37 +02:00
if ( config . iInstallerType < GlobalConstants : : PLATFORMS [ 0 ] . platformId | | config . iInstallerType > platform_sum )
2013-03-15 21:46:16 +01:00
{
std : : cout < < " Invalid value for --platform " < < std : : endl ;
return 1 ;
}
2013-04-25 11:12:37 +02:00
if ( config . iInstallerLanguage < GlobalConstants : : LANGUAGES [ 0 ] . languageId | | config . iInstallerLanguage > language_sum )
2013-03-23 19:12:49 +01:00
{
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 ;
2014-02-03 18:28:48 +01:00
else if ( config . bSaveConfig )
{
std : : ofstream ofs ( config . sConfigFilePath . c_str ( ) ) ;
if ( ofs )
{
std : : cout < < " Saving config: " < < config . sConfigFilePath < < std : : endl ;
for ( bpo : : variables_map : : iterator it = vm . begin ( ) ; it ! = vm . end ( ) ; + + it )
{
std : : string option = it - > first ;
std : : string option_value_string ;
const bpo : : variable_value & option_value = it - > second ;
try
{
if ( options_cfg_all . find ( option , false ) . long_name ( ) = = option )
{
if ( ! option_value . empty ( ) )
{
const std : : type_info & type = option_value . value ( ) . type ( ) ;
if ( type = = typeid ( std : : string ) )
option_value_string = option_value . as < std : : string > ( ) ;
else if ( type = = typeid ( int ) )
option_value_string = std : : to_string ( option_value . as < int > ( ) ) ;
else if ( type = = typeid ( size_t ) )
option_value_string = std : : to_string ( option_value . as < size_t > ( ) ) ;
else if ( type = = typeid ( unsigned int ) )
option_value_string = std : : to_string ( option_value . as < unsigned int > ( ) ) ;
else if ( type = = typeid ( long int ) )
option_value_string = std : : to_string ( option_value . as < long int > ( ) ) ;
else if ( type = = typeid ( bool ) )
{
if ( option_value . as < bool > ( ) = = true )
option_value_string = " true " ;
else
option_value_string = " false " ;
}
}
}
}
catch ( . . . )
{
continue ;
}
if ( ! option_value_string . empty ( ) )
{
std : : cout < < option < < " = " < < option_value_string < < std : : endl ;
ofs < < option < < " = " < < option_value_string < < std : : endl ;
}
}
ofs . close ( ) ;
return 0 ;
}
else
{
std : : cout < < " Failed to create config: " < < config . sConfigFilePath < < std : : endl ;
return 1 ;
}
}
else if ( config . bResetConfig )
{
std : : ofstream ofs ( config . sConfigFilePath . c_str ( ) ) ;
if ( ofs )
{
if ( ! config . sToken . empty ( ) & & ! config . sSecret . empty ( ) )
{
ofs < < " token = " < < config . sToken < < std : : endl ;
ofs < < " secret = " < < config . sSecret < < std : : endl ;
}
ofs . close ( ) ;
return 0 ;
}
else
{
std : : cout < < " Failed to create config: " < < config . sConfigFilePath < < std : : endl ;
return 1 ;
}
}
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 ( ) ;
2013-12-22 12:02:48 +01:00
else if ( ! config . sOrphanRegex . empty ( ) ) // Check for orphaned files if regex for orphans is set
2013-10-14 21:31:12 +02:00
downloader . checkOrphans ( ) ;
2013-11-14 14:40:59 +01:00
else if ( config . bCheckStatus )
downloader . checkStatus ( ) ;
2013-03-15 21:46:16 +01:00
else
{ // Show help message
std : : cout < < config . sVersionString < < std : : endl
2014-02-03 18:28:48 +01:00
< < options_cli_all < < std : : endl ;
2013-03-15 21:46:16 +01:00
}
return 0 ;
}