2012-03-30 18:59:44 +00:00
# include "importer.hpp"
2012-03-30 20:58:54 +00:00
# include <iostream>
# include <string>
# include <boost/program_options.hpp>
# include <boost/filesystem.hpp>
namespace bpo = boost : : program_options ;
2012-03-30 18:59:44 +00:00
int main ( int argc , char * argv [ ] ) {
2012-04-04 13:11:30 +00:00
bpo : : options_description desc ( " Syntax: mwiniimporter <options> inifile configfile \n Allowed options " ) ;
bpo : : positional_options_description p_desc ;
2012-03-30 18:59:44 +00:00
desc . add_options ( )
( " help,h " , " produce help message " )
2012-03-30 20:58:54 +00:00
( " verbose,v " , " verbose output " )
2012-03-31 09:36:51 +00:00
( " ini,i " , bpo : : value < std : : string > ( ) , " morrowind.ini file " )
( " cfg,c " , bpo : : value < std : : string > ( ) , " openmw.cfg file " )
2012-03-30 20:58:54 +00:00
( " output,o " , bpo : : value < std : : string > ( ) - > default_value ( " " ) , " openmw.cfg file " )
2012-03-31 16:24:43 +00:00
( " game-files,g " , " import esm and esp files " )
2013-02-03 16:42:58 +00:00
( " no-archives,A " , " disable bsa archives import " )
2013-01-01 20:59:30 +00:00
( " encoding,e " , bpo : : value < std : : string > ( ) - > default_value ( " win1252 " ) ,
" Character encoding used in OpenMW game messages: \n "
" \n \t win1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages \n "
" \n \t win1251 - Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages \n "
" \n \t win1252 - Western European (Latin) alphabet, used by default " )
2012-03-30 18:59:44 +00:00
;
2012-04-04 13:11:30 +00:00
p_desc . add ( " ini " , 1 ) . add ( " cfg " , 1 ) ;
2012-03-31 09:36:51 +00:00
2012-03-30 18:59:44 +00:00
bpo : : variables_map vm ;
2012-04-04 13:11:30 +00:00
2013-06-26 16:29:09 +00:00
try
{
bpo : : parsed_options parsed = bpo : : command_line_parser ( argc , argv )
. options ( desc )
. positional ( p_desc )
. run ( ) ;
bpo : : store ( parsed , vm ) ;
}
catch ( boost : : program_options : : unknown_option & x )
{
std : : cerr < < " ERROR: " < < x . what ( ) < < std : : endl ;
return false ;
}
2013-06-26 16:43:27 +00:00
catch ( boost : : program_options : : invalid_command_line_syntax & x )
{
std : : cerr < < " ERROR: " < < x . what ( ) < < std : : endl ;
return false ;
}
2012-03-31 09:36:51 +00:00
2012-04-02 12:34:21 +00:00
if ( vm . count ( " help " ) | | ! vm . count ( " ini " ) | | ! vm . count ( " cfg " ) ) {
std : : cout < < desc ;
return 0 ;
2012-03-30 18:59:44 +00:00
}
2012-03-31 09:36:51 +00:00
2012-04-02 18:47:09 +00:00
bpo : : notify ( vm ) ;
2012-03-30 20:58:54 +00:00
std : : string iniFile = vm [ " ini " ] . as < std : : string > ( ) ;
std : : string cfgFile = vm [ " cfg " ] . as < std : : string > ( ) ;
2012-03-31 09:36:51 +00:00
2012-03-30 20:58:54 +00:00
// if no output is given, write back to cfg file
std : : string outputFile ( vm [ " output " ] . as < std : : string > ( ) ) ;
if ( vm [ " output " ] . defaulted ( ) ) {
outputFile = vm [ " cfg " ] . as < std : : string > ( ) ;
2012-03-30 18:59:44 +00:00
}
2012-03-31 09:36:51 +00:00
2012-03-30 20:58:54 +00:00
if ( ! boost : : filesystem : : exists ( iniFile ) ) {
std : : cerr < < " ini file does not exist " < < std : : endl ;
return - 3 ;
}
2013-06-17 22:43:31 +00:00
if ( ! boost : : filesystem : : exists ( cfgFile ) )
2012-03-30 20:58:54 +00:00
std : : cerr < < " cfg file does not exist " < < std : : endl ;
2012-04-02 15:07:18 +00:00
2012-03-30 18:59:44 +00:00
MwIniImporter importer ;
2012-03-30 20:58:54 +00:00
importer . setVerbose ( vm . count ( " verbose " ) ) ;
2012-03-31 09:36:51 +00:00
2013-01-01 20:59:30 +00:00
// Font encoding settings
std : : string encoding ( vm [ " encoding " ] . as < std : : string > ( ) ) ;
importer . setInputEncoding ( ToUTF8 : : calculateEncoding ( encoding ) ) ;
2012-03-31 20:48:50 +00:00
MwIniImporter : : multistrmap ini = importer . loadIniFile ( iniFile ) ;
MwIniImporter : : multistrmap cfg = importer . loadCfgFile ( cfgFile ) ;
2012-03-31 09:36:51 +00:00
2012-03-30 20:58:54 +00:00
importer . merge ( cfg , ini ) ;
2012-04-02 22:44:26 +00:00
importer . mergeFallback ( cfg , ini ) ;
2012-04-02 15:07:18 +00:00
2012-03-31 16:24:43 +00:00
if ( vm . count ( " game-files " ) ) {
2012-03-31 20:48:50 +00:00
importer . importGameFiles ( cfg , ini ) ;
2012-03-31 16:24:43 +00:00
}
2012-03-31 09:36:51 +00:00
2013-02-03 16:42:58 +00:00
if ( ! vm . count ( " no-archives " ) ) {
importer . importArchives ( cfg , ini ) ;
}
2012-03-30 20:58:54 +00:00
std : : cout < < " write to: " < < outputFile < < std : : endl ;
2012-04-02 12:42:01 +00:00
boost : : iostreams : : stream < boost : : iostreams : : file_sink > file ( outputFile ) ;
2012-03-31 19:06:48 +00:00
importer . writeToFile ( file , cfg ) ;
2012-03-31 09:36:51 +00:00
2012-03-30 18:59:44 +00:00
return 0 ;
}