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>
2015-09-05 10:15:15 +02:00
# define VERSION_NUMBER "2.25"
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 ;
2014-07-06 16:20:06 +02:00
template < typename T > void set_vm_value ( std : : map < std : : string , bpo : : variable_value > & vm , const std : : string & option , const T & value )
{
vm [ option ] . value ( ) = boost : : any ( value ) ;
}
2015-10-03 18:09:50 +02:00
// Parse the options string
void parseOptionString ( const std : : string & option_string , std : : vector < unsigned int > & priority , unsigned int & type , const std : : vector < GlobalConstants : : optionsStruct > & options )
2014-10-25 17:57:04 +02:00
{
2015-10-03 18:09:50 +02:00
type = 0 ;
std : : vector < std : : string > tokens_priority = Util : : tokenize ( option_string , " , " ) ;
for ( std : : vector < std : : string > : : iterator it_priority = tokens_priority . begin ( ) ; it_priority ! = tokens_priority . end ( ) ; it_priority + + )
2015-08-29 15:18:20 +02:00
{
2015-10-03 18:09:50 +02:00
unsigned int value = 0 ;
std : : vector < std : : string > tokens_value = Util : : tokenize ( * it_priority , " + " ) ;
for ( std : : vector < std : : string > : : iterator it_value = tokens_value . begin ( ) ; it_value ! = tokens_value . end ( ) ; it_value + + )
{
value | = Util : : getOptionValue ( * it_value , options ) ;
}
priority . push_back ( value ) ;
type | = value ;
2015-09-01 13:45:34 +02:00
}
}
2013-03-15 21:46:16 +01:00
int main ( int argc , char * argv [ ] )
{
Config config ;
2013-03-19 18:29:58 +01:00
config . sVersionString = VERSION_STRING ;
2015-10-03 18:00:51 +02:00
config . sVersionNumber = VERSION_NUMBER ;
2014-10-16 10:05:57 +02:00
2015-08-29 13:26:36 +02:00
config . sCacheDirectory = Util : : getCacheHome ( ) + " /lgogdownloader " ;
2014-10-16 10:05:57 +02:00
config . sXMLDirectory = config . sCacheDirectory + " /xml " ;
2015-08-29 13:26:36 +02:00
config . sConfigDirectory = Util : : getConfigHome ( ) + " /lgogdownloader " ;
config . sCookiePath = config . sConfigDirectory + " /cookies.txt " ;
config . sConfigFilePath = config . sConfigDirectory + " /config.cfg " ;
config . sBlacklistFilePath = config . sConfigDirectory + " /blacklist.txt " ;
2015-10-03 18:09:50 +02:00
std : : string priority_help_text = " Set priority by separating values with \" , \" \n Combine values by separating with \" + \" " ;
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 " ;
2015-09-01 13:45:34 +02:00
unsigned int platform_all = Util : : getOptionValue ( " all " , GlobalConstants : : PLATFORMS ) ;
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
{
2015-09-02 10:16:00 +02:00
platform_text + = GlobalConstants : : PLATFORMS [ i ] . str + " = " + GlobalConstants : : PLATFORMS [ i ] . regexp + " | " + std : : to_string ( GlobalConstants : : PLATFORMS [ i ] . id ) + " \n " ;
2013-03-23 19:12:49 +01:00
}
2015-09-02 10:16:00 +02:00
platform_text + = " All = all| " + std : : to_string ( platform_all ) ;
2015-10-03 18:09:50 +02:00
platform_text + = " \n \n " + priority_help_text ;
platform_text + = " \n Example: Linux if available otherwise Windows and Mac: l,w+m " ;
2013-03-23 19:12:49 +01:00
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 " ;
2015-09-01 13:45:34 +02:00
unsigned int language_all = Util : : getOptionValue ( " all " , GlobalConstants : : LANGUAGES ) ;
2013-03-23 19:12:49 +01:00
for ( unsigned int i = 0 ; i < GlobalConstants : : LANGUAGES . size ( ) ; + + i )
{
2015-09-02 10:16:00 +02:00
language_text + = GlobalConstants : : LANGUAGES [ i ] . str + " = " + GlobalConstants : : LANGUAGES [ i ] . regexp + " | " + std : : to_string ( GlobalConstants : : LANGUAGES [ i ] . id ) + " \n " ;
2013-03-23 19:12:49 +01:00
}
2015-09-02 10:16:00 +02:00
language_text + = " Add the values to download multiple languages \n All = all| " + std : : to_string ( language_all ) + " \n "
2015-10-03 18:09:50 +02:00
+ " French + Polish = \" fr+pl \" | " + std : : to_string ( GlobalConstants : : LANGUAGE_FR | GlobalConstants : : LANGUAGE_PL ) + " ( " + std : : to_string ( GlobalConstants : : LANGUAGE_FR ) + " + " + std : : to_string ( GlobalConstants : : LANGUAGE_PL ) + " = " + std : : to_string ( GlobalConstants : : LANGUAGE_FR | GlobalConstants : : LANGUAGE_PL ) + " ) " ;
language_text + = " \n \n " + priority_help_text ;
language_text + = " \n Example: German if available otherwise English and French: de,en+fr " ;
2013-03-15 21:46:16 +01:00
2013-12-22 12:02:48 +01:00
// Create help text for --check-orphans
2015-08-14 19:36:23 +02:00
std : : string orphans_regex_default = " .* \\ .(zip|exe|bin|dmg|old|deb|tar \\ .gz|pkg|sh)$ " ; // Limit to files with these extensions (".old" is for renamed older version files)
2013-12-22 12:02:48 +01:00
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 + " ' " ;
2014-09-19 21:46:03 +02:00
// Help text for subdir options
std : : string subdir_help_text = " \n Templates: \n - %platform% \n - %gamename% \n - %dlcname% " ;
2015-09-03 12:18:30 +02:00
std : : vector < std : : string > vFileIdStrings ;
2014-09-05 13:02:08 +02:00
std : : vector < std : : string > unrecognized_options_cfg ;
2015-08-15 03:16:26 +02:00
std : : vector < std : : string > unrecognized_options_cli ;
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 bNoInstallers = false ;
bool bNoExtras = false ;
bool bNoPatches = false ;
bool bNoLanguagePacks = false ;
2014-03-29 00:51:39 +01:00
bool bNoDLC = false ;
2013-11-19 11:47:10 +01:00
bool bNoRemoteXML = false ;
2014-02-23 02:16:10 +01:00
bool bNoSubDirectories = false ;
2014-09-05 19:25:40 +02:00
bool bNoCover = false ;
2015-03-28 11:52:41 +01:00
bool bNoPlatformDetection = false ;
2015-08-12 15:42:54 +02:00
bool bLogin = false ;
2015-09-01 13:45:34 +02:00
std : : string sInstallerPlatform ;
std : : string sInstallerLanguage ;
2014-09-17 11:41:01 +02:00
config . bReport = 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 " )
2015-08-12 15:42:54 +02:00
( " login " , bpo : : value < bool > ( & bLogin ) - > zero_tokens ( ) - > default_value ( false ) , " Login " )
2013-03-15 21:46:16 +01:00
( " 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 " )
2014-09-17 12:05:04 +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 rename the old file (appends .old to filename) " )
2014-11-17 15:28:43 +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 \" \n Alias \" free \" doesn't work with cached details " )
2015-08-29 13:18:10 +02:00
( " create-xml " , bpo : : value < std : : string > ( & config . sXMLFile ) - > implicit_value ( " automatic " ) , " Create GOG XML for file \n \" automatic \" to enable automatic XML creation " )
2014-02-03 18:28:48 +01:00
( " 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 ( ) )
2015-07-02 02:01:21 +02:00
( " 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 \n FS - File size mismatch, incomplete download " )
2014-02-03 18:28:48 +01:00
( " 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-09-17 11:41:01 +02:00
( " report " , bpo : : value < std : : string > ( & config . sReportFilePath ) - > implicit_value ( " lgogdownloader-report.log " ) , " Save report of downloaded/repaired files to specified file \n Default filename: lgogdownloader-report.log " )
2014-09-05 19:25:40 +02:00
( " no-cover " , bpo : : value < bool > ( & bNoCover ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download cover images. Overrides --cover option. \n Useful for making exceptions when \" cover \" is set to true in config file. " )
2014-10-16 10:05:57 +02:00
( " update-cache " , bpo : : value < bool > ( & config . bUpdateCache ) - > zero_tokens ( ) - > default_value ( false ) , " Update game details cache " )
2015-03-28 11:52:41 +01:00
( " no-platform-detection " , bpo : : value < bool > ( & bNoPlatformDetection ) - > zero_tokens ( ) - > default_value ( false ) , " Don't try to detect supported platforms from game shelf. \n Skips the initial fast platform detection and detects the supported platforms from game details which is slower but more accurate. \n Useful in case platform identifier is missing for some games in the game shelf. \n Using --platform with --list doesn't work with this option. " )
2015-09-05 10:11:12 +02:00
( " download-file " , bpo : : value < std : : string > ( & config . sFileIdString ) - > default_value ( " " ) , " Download files using fileid \n \n Format: \n \" gamename/fileid \" \n or: \" gogdownloader://gamename/fileid \" \n \n Multiple files: \n \" gamename1/fileid1,gamename2/fileid2 \" \n or: \" gogdownloader://gamename1/fileid1,gamename2/fileid2 \" \n \n This option ignores all subdir options. The files are downloaded to directory specified with --directory option. " )
2015-08-15 05:15:15 +02:00
( " output-file,o " , bpo : : value < std : : string > ( & config . sOutputFilename ) - > default_value ( " " ) , " Set filename of file downloaded with --download-file. " )
2015-05-17 17:47:04 +02:00
( " wishlist " , bpo : : value < bool > ( & config . bShowWishlist ) - > zero_tokens ( ) - > default_value ( false ) , " Show wishlist " )
2015-08-12 15:42:54 +02:00
( " login-api " , bpo : : value < bool > ( & config . bLoginAPI ) - > zero_tokens ( ) - > default_value ( false ) , " Login (API only) " )
( " login-website " , bpo : : value < bool > ( & config . bLoginHTTP ) - > zero_tokens ( ) - > default_value ( false ) , " Login (website only) " )
2014-02-03 18:28:48 +01:00
;
// Commandline options (config file)
options_cli_cfg . add_options ( )
2014-09-19 21:46:03 +02: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 " )
2015-10-03 18:09:50 +02:00
( " platform " , bpo : : value < std : : string > ( & sInstallerPlatform ) - > default_value ( " w+l " ) , platform_text . c_str ( ) )
( " language " , bpo : : value < std : : string > ( & sInstallerLanguage ) - > default_value ( " 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 " )
2014-03-29 00:51:39 +01:00
( " no-dlc " , bpo : : value < bool > ( & bNoDLC ) - > zero_tokens ( ) - > default_value ( false ) , " Don't download/list/repair DLCs " )
2014-08-15 15:51:48 +02:00
( " cover " , bpo : : value < bool > ( & config . bCover ) - > zero_tokens ( ) - > default_value ( false ) , " Download cover images " )
2013-11-19 11:47:10 +01:00
( " 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 " )
2014-02-23 02:16:10 +01:00
( " no-subdirectories " , bpo : : value < bool > ( & bNoSubDirectories ) - > zero_tokens ( ) - > default_value ( false ) , " Don't create subdirectories for extras, patches and language packs " )
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 " )
2014-02-26 13:59:23 +01:00
( " retries " , bpo : : value < int > ( & config . iRetries ) - > default_value ( 3 ) , " Set maximum number of retries on failed download " )
2014-06-10 11:16:49 +02:00
( " wait " , bpo : : value < int > ( & config . iWait ) - > default_value ( 0 ) , " Time to wait between requests (milliseconds) " )
2014-08-28 15:00:24 +02:00
( " cover-list " , bpo : : value < std : : string > ( & config . sCoverList ) - > default_value ( " https://sites.google.com/site/gogdownloader/covers.xml " ) , " Set URL for cover list " )
2014-09-19 21:46:03 +02:00
( " subdir-installers " , bpo : : value < std : : string > ( & config . sInstallersSubdir ) - > default_value ( " " ) , ( " Set subdirectory for extras " + subdir_help_text ) . c_str ( ) )
( " subdir-extras " , bpo : : value < std : : string > ( & config . sExtrasSubdir ) - > default_value ( " extras " ) , ( " Set subdirectory for extras " + subdir_help_text ) . c_str ( ) )
( " subdir-patches " , bpo : : value < std : : string > ( & config . sPatchesSubdir ) - > default_value ( " patches " ) , ( " Set subdirectory for patches " + subdir_help_text ) . c_str ( ) )
( " subdir-language-packs " , bpo : : value < std : : string > ( & config . sLanguagePackSubdir ) - > default_value ( " languagepacks " ) , ( " Set subdirectory for language packs " + subdir_help_text ) . c_str ( ) )
( " subdir-dlc " , bpo : : value < std : : string > ( & config . sDLCSubdir ) - > default_value ( " dlc/%dlcname% " ) , ( " Set subdirectory for dlc " + subdir_help_text ) . c_str ( ) )
( " subdir-game " , bpo : : value < std : : string > ( & config . sGameSubdir ) - > default_value ( " %gamename% " ) , ( " Set subdirectory for game " + subdir_help_text ) . c_str ( ) )
2014-10-16 10:05:57 +02:00
( " use-cache " , bpo : : value < bool > ( & config . bUseCache ) - > zero_tokens ( ) - > default_value ( false ) , ( " Use game details cache " ) )
( " cache-valid " , bpo : : value < int > ( & config . iCacheValid ) - > default_value ( 2880 ) , ( " Set how long cached game details are valid (in minutes) \n Default: 2880 minutes (48 hours) " ) )
2015-03-19 08:50:50 +01:00
( " save-serials " , bpo : : value < bool > ( & config . bSaveSerials ) - > zero_tokens ( ) - > default_value ( false ) , " Save serial numbers when downloading " )
2015-06-21 16:25:45 +02:00
( " ignore-dlc-count " , bpo : : value < std : : string > ( & config . sIgnoreDLCCountRegex ) - > implicit_value ( " .* " ) , " Set regular expression filter for games to ignore DLC count information \n Ignoring DLC count information helps in situations where the account page doesn't provide accurate information about DLCs " )
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 ) ;
2015-08-15 03:16:26 +02:00
bpo : : parsed_options parsed = bpo : : parse_command_line ( argc , argv , options_cli_all ) ;
bpo : : store ( parsed , vm ) ;
unrecognized_options_cli = bpo : : collect_unrecognized ( parsed . options , bpo : : include_positional ) ;
2014-02-03 18:28:48 +01:00
bpo : : notify ( vm ) ;
2014-09-11 23:38:23 +02:00
if ( vm . count ( " help " ) )
{
std : : cout < < config . sVersionString < < std : : endl
< < options_cli_all < < std : : endl ;
return 0 ;
}
if ( vm . count ( " version " ) )
{
std : : cout < < VERSION_STRING < < std : : endl ;
return 0 ;
}
// Create lgogdownloader 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 ;
}
}
path = config . sConfigDirectory ;
if ( ! boost : : filesystem : : exists ( path ) )
{
if ( ! boost : : filesystem : : create_directories ( path ) )
{
std : : cout < < " Failed to create directory: " < < path < < std : : endl ;
return 1 ;
}
}
2014-10-16 10:31:08 +02:00
path = config . sCacheDirectory ;
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
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-09-05 13:02:08 +02:00
bpo : : parsed_options parsed = bpo : : parse_config_file ( ifs , options_cfg_all , true ) ;
bpo : : store ( parsed , vm ) ;
2013-03-15 21:46:16 +01:00
bpo : : notify ( vm ) ;
ifs . close ( ) ;
2014-09-05 13:02:08 +02:00
unrecognized_options_cfg = bpo : : collect_unrecognized ( parsed . options , bpo : : include_positional ) ;
2013-03-15 21:46:16 +01:00
}
}
2014-06-20 17:30:40 +02:00
if ( boost : : filesystem : : exists ( config . sBlacklistFilePath ) )
{
std : : ifstream ifs ( config . sBlacklistFilePath . c_str ( ) ) ;
if ( ! ifs )
{
std : : cout < < " Could not open blacklist file: " < < config . sBlacklistFilePath < < std : : endl ;
return 1 ;
}
else
{
std : : string line ;
std : : vector < std : : string > lines ;
while ( ! ifs . eof ( ) )
{
std : : getline ( ifs , line ) ;
lines . push_back ( std : : move ( line ) ) ;
}
config . blacklist . initialize ( lines ) ;
}
}
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 ;
2014-09-17 11:41:01 +02:00
if ( vm . count ( " report " ) )
config . bReport = true ;
2014-06-10 11:16:49 +02:00
if ( config . iWait > 0 )
config . iWait * = 1000 ;
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 . bInstallers = ! bNoInstallers ;
config . bExtras = ! bNoExtras ;
config . bPatches = ! bNoPatches ;
config . bLanguagePacks = ! bNoLanguagePacks ;
2014-03-29 00:51:39 +01:00
config . bDLC = ! bNoDLC ;
2013-11-19 11:47:10 +01:00
config . bRemoteXML = ! bNoRemoteXML ;
2014-02-23 02:16:10 +01:00
config . bSubDirectories = ! bNoSubDirectories ;
2015-03-28 11:52:41 +01:00
config . bPlatformDetection = ! bNoPlatformDetection ;
2014-09-05 19:25:40 +02:00
2015-08-15 03:16:26 +02:00
for ( auto i = unrecognized_options_cli . begin ( ) ; i ! = unrecognized_options_cli . end ( ) ; + + i )
if ( i - > compare ( 0 , GlobalConstants : : PROTOCOL_PREFIX . length ( ) , GlobalConstants : : PROTOCOL_PREFIX ) = = 0 )
config . sFileIdString = * i ;
2015-09-03 12:18:30 +02:00
if ( ! config . sFileIdString . empty ( ) )
{
if ( config . sFileIdString . compare ( 0 , GlobalConstants : : PROTOCOL_PREFIX . length ( ) , GlobalConstants : : PROTOCOL_PREFIX ) = = 0 )
{
config . sFileIdString . replace ( 0 , GlobalConstants : : PROTOCOL_PREFIX . length ( ) , " " ) ;
}
vFileIdStrings = Util : : tokenize ( config . sFileIdString , " , " ) ;
}
if ( ! config . sOutputFilename . empty ( ) & & vFileIdStrings . size ( ) > 1 )
2015-08-15 05:42:00 +02:00
{
std : : cerr < < " Cannot specify an output file name when downloading multiple files. " < < std : : endl ;
return 1 ;
}
2014-09-05 19:25:40 +02:00
// Override cover option
if ( bNoCover )
config . bCover = false ;
2014-10-25 17:57:04 +02:00
2015-08-12 15:42:54 +02:00
if ( bLogin )
{
config . bLoginAPI = true ;
config . bLoginHTTP = true ;
}
2015-10-03 18:09:50 +02:00
parseOptionString ( sInstallerLanguage , config . vLanguagePriority , config . iInstallerLanguage , GlobalConstants : : LANGUAGES ) ;
parseOptionString ( sInstallerPlatform , config . vPlatformPriority , config . iInstallerPlatform , GlobalConstants : : PLATFORMS ) ;
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 ;
}
2015-09-01 13:45:34 +02:00
if ( config . iInstallerPlatform < GlobalConstants : : PLATFORMS [ 0 ] . id | | config . iInstallerPlatform > platform_all )
2013-03-15 21:46:16 +01:00
{
std : : cout < < " Invalid value for --platform " < < std : : endl ;
return 1 ;
}
2015-09-01 13:45:34 +02:00
if ( config . iInstallerLanguage < GlobalConstants : : LANGUAGES [ 0 ] . id | | config . iInstallerLanguage > language_all )
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 ( ) )
2014-09-19 22:07:16 +02:00
{
2013-03-15 21:46:16 +01:00
if ( config . sDirectory . at ( config . sDirectory . length ( ) - 1 ) ! = ' / ' )
2013-03-16 22:35:57 +01:00
config . sDirectory + = " / " ;
2014-09-19 22:07:16 +02:00
}
else
{
config . sDirectory = " ./ " ; // Directory wasn't specified, use current directory
}
2013-03-15 21:46:16 +01:00
2014-09-05 13:02:08 +02:00
if ( ! unrecognized_options_cfg . empty ( ) & & ( ! config . bSaveConfig | | ! config . bResetConfig ) )
{
std : : cerr < < " Unrecognized options in " < < config . sConfigFilePath < < std : : endl ;
for ( unsigned int i = 0 ; i < unrecognized_options_cfg . size ( ) ; i + = 2 )
{
std : : cerr < < unrecognized_options_cfg [ i ] < < " = " < < unrecognized_options_cfg [ i + 1 ] < < std : : endl ;
}
std : : cerr < < std : : endl ;
}
2013-03-15 21:46:16 +01:00
Downloader downloader ( config ) ;
2014-07-07 14:57:07 +02:00
int initResult = downloader . init ( ) ;
2013-03-15 21:46:16 +01:00
2014-07-06 16:20:06 +02:00
int iLoginResult = 0 ;
2015-08-12 15:42:54 +02:00
if ( config . bLoginAPI | | config . bLoginHTTP | | initResult = = 1 )
2014-02-03 18:28:48 +01:00
{
2015-08-18 01:20:57 +02:00
if ( ! config . bLoginAPI & & ! config . bLoginHTTP )
downloader . config . bLoginAPI = true ;
2014-07-07 14:57:07 +02:00
iLoginResult = downloader . login ( ) ;
2014-07-06 16:20:06 +02:00
if ( iLoginResult = = 0 )
return 1 ;
}
2014-10-28 20:03:02 +01:00
// Make sure that config file and cookie file are only readable/writable by owner
Util : : setFilePermissions ( config . sConfigFilePath , boost : : filesystem : : owner_read | boost : : filesystem : : owner_write ) ;
Util : : setFilePermissions ( config . sCookiePath , boost : : filesystem : : owner_read | boost : : filesystem : : owner_write ) ;
2014-07-06 16:20:06 +02:00
if ( config . bSaveConfig | | iLoginResult = = 1 )
{
if ( iLoginResult = = 1 )
{
set_vm_value ( vm , " token " , downloader . config . sToken ) ;
set_vm_value ( vm , " secret " , downloader . config . sSecret ) ;
bpo : : notify ( vm ) ;
}
2014-02-03 18:28:48 +01:00
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 ( ) )
{
ofs < < option < < " = " < < option_value_string < < std : : endl ;
}
}
ofs . close ( ) ;
2014-10-28 20:03:02 +01:00
Util : : setFilePermissions ( config . sConfigFilePath , boost : : filesystem : : owner_read | boost : : filesystem : : owner_write ) ;
2015-08-18 01:27:23 +02:00
if ( config . bSaveConfig )
return 0 ;
2014-02-03 18:28:48 +01:00
}
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 ( ) ;
2014-10-28 20:03:02 +01:00
Util : : setFilePermissions ( config . sConfigFilePath , boost : : filesystem : : owner_read | boost : : filesystem : : owner_write ) ;
2014-02-03 18:28:48 +01:00
return 0 ;
}
else
{
std : : cout < < " Failed to create config: " < < config . sConfigFilePath < < std : : endl ;
return 1 ;
}
}
2015-08-18 01:27:23 +02:00
if ( config . bShowWishlist )
2015-05-13 16:13:30 +02:00
downloader . showWishlist ( ) ;
2014-10-16 10:05:57 +02:00
else if ( config . bUpdateCache )
downloader . updateCache ( ) ;
2013-03-16 02:22:45 +01:00
else if ( config . bUpdateCheck ) // Update check has priority over download and list
downloader . updateCheck ( ) ;
2015-09-03 12:18:30 +02:00
else if ( ! vFileIdStrings . empty ( ) )
2015-08-15 03:08:40 +02:00
{
2015-09-03 12:18:30 +02:00
for ( std : : vector < std : : string > : : iterator it = vFileIdStrings . begin ( ) ; it ! = vFileIdStrings . end ( ) ; it + + )
2015-08-15 03:28:14 +02:00
{
2015-09-03 12:18:30 +02:00
downloader . downloadFileWithId ( * it , config . sOutputFilename ) ;
2015-08-15 03:28:14 +02:00
}
2015-08-15 03:08:40 +02:00
}
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
}
2014-02-28 11:54:40 +01:00
// Orphan check was called at the same time as download. Perform it after download has finished
if ( ! config . sOrphanRegex . empty ( ) & & config . bDownload )
downloader . checkOrphans ( ) ;
2013-03-15 21:46:16 +01:00
return 0 ;
}