2010-02-28 13:51:17 +00:00
# include <iostream>
2012-01-21 00:14:35 +00:00
# include <components/files/configurationmanager.hpp>
2011-04-28 07:39:40 +00:00
2010-06-16 10:13:21 +00:00
# include "engine.hpp"
2010-02-28 13:51:17 +00:00
2010-09-19 00:01:01 +00:00
# if defined(_WIN32) && !defined(_CONSOLE)
2011-01-04 00:34:55 +00:00
# include <boost/iostreams/concepts.hpp>
2010-09-19 00:01:01 +00:00
# include <boost/iostreams/stream_buffer.hpp>
// For OutputDebugString
# include <Windows.h>
// makes __argc and __argv available on windows
2012-07-17 07:44:24 +00:00
# include <cstdlib>
2010-09-19 00:01:01 +00:00
# endif
2011-06-25 15:29:11 +00:00
// for Ogre::macBundlePath
# if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
# include <OSX/macUtils.h>
# endif
2011-07-08 14:16:20 +00:00
# include "config.hpp"
2012-02-24 19:19:32 +00:00
# include <boost/version.hpp>
/**
* Workaround for problems with whitespaces in paths in older versions of Boost library
*/
# if (BOOST_VERSION <= 104600)
namespace boost
{
template < >
inline boost : : filesystem : : path lexical_cast < boost : : filesystem : : path , std : : string > ( const std : : string & arg )
{
return boost : : filesystem : : path ( arg ) ;
}
} /* namespace boost */
# endif /* (BOOST_VERSION <= 104600) */
2012-04-02 23:47:43 +00:00
struct FallbackMap {
std : : map < std : : string , std : : string > mMap ;
2012-04-03 00:14:39 +00:00
} ;
2012-04-02 23:47:43 +00:00
2012-04-03 00:14:39 +00:00
void validate ( boost : : any & v , std : : vector < std : : string > const & tokens , FallbackMap * , int )
{
if ( v . empty ( ) )
2012-04-02 23:47:43 +00:00
{
2012-04-03 00:14:39 +00:00
v = boost : : any ( FallbackMap ( ) ) ;
}
FallbackMap * map = boost : : any_cast < FallbackMap > ( & v ) ;
std : : map < std : : string , std : : string > : : iterator mapIt ;
for ( std : : vector < std : : string > : : const_iterator it = tokens . begin ( ) ; it ! = tokens . end ( ) ; it + + )
{
int sep = it - > find ( " , " ) ;
2012-04-04 21:51:22 +00:00
if ( sep < 1 | | sep = = ( int ) it - > length ( ) - 1 )
2012-04-04 21:52:42 +00:00
# if (BOOST_VERSION < 104200)
2012-04-04 21:51:22 +00:00
throw boost : : program_options : : validation_error ( " invalid value " ) ;
# else
2012-04-04 13:23:14 +00:00
throw boost : : program_options : : validation_error ( boost : : program_options : : validation_error : : invalid_option_value ) ;
2012-04-04 22:16:44 +00:00
# endif
2012-04-03 00:14:39 +00:00
std : : string key ( it - > substr ( 0 , sep ) ) ;
std : : string value ( it - > substr ( sep + 1 ) ) ;
if ( ( mapIt = map - > mMap . find ( key ) ) = = map - > mMap . end ( ) )
2012-04-02 23:47:43 +00:00
{
2012-10-09 15:10:25 +00:00
map - > mMap . insert ( std : : make_pair ( key , value ) ) ;
2012-04-02 23:47:43 +00:00
}
2012-04-03 00:14:39 +00:00
}
}
2012-04-02 23:47:43 +00:00
2011-08-19 19:06:09 +00:00
/**
* \ brief Parses application command line and calls \ ref Cfg : : ConfigurationManager
* to parse configuration files .
*
* Results are directly written to \ ref Engine class .
*
* \ retval true - Everything goes OK
* \ retval false - Error
*/
2012-01-21 00:14:35 +00:00
bool parseOptions ( int argc , char * * argv , OMW : : Engine & engine , Files : : ConfigurationManager & cfgMgr )
2010-02-28 13:51:17 +00:00
{
2010-06-27 23:44:15 +00:00
// Create a local alias for brevity
2010-10-05 16:23:53 +00:00
namespace bpo = boost : : program_options ;
2011-08-19 19:06:09 +00:00
typedef std : : vector < std : : string > StringsVector ;
2010-06-27 23:44:15 +00:00
2011-08-19 19:06:09 +00:00
bpo : : options_description desc ( " Syntax: openmw <options> \n Allowed options " ) ;
2010-06-10 08:31:50 +00:00
desc . add_options ( )
2011-08-19 19:06:09 +00:00
( " help " , " print help message " )
2011-07-08 14:16:20 +00:00
( " version " , " print version information and quit " )
2011-09-02 20:45:21 +00:00
( " data " , bpo : : value < Files : : PathContainer > ( ) - > default_value ( Files : : PathContainer ( ) , " data " )
2011-08-19 19:06:09 +00:00
- > multitoken ( ) , " set data directories (later directories have higher priority) " )
( " data-local " , bpo : : value < std : : string > ( ) - > default_value ( " " ) ,
2011-05-05 17:50:28 +00:00
" set local data directory (highest priority) " )
2011-08-19 19:06:09 +00:00
( " resources " , bpo : : value < std : : string > ( ) - > default_value ( " resources " ) ,
2011-01-04 00:34:55 +00:00
" set resources directory " )
2011-08-19 19:06:09 +00:00
( " start " , bpo : : value < std : : string > ( ) - > default_value ( " Beshara " ) ,
2010-10-05 16:23:53 +00:00
" set initial cell " )
2011-08-19 19:06:09 +00:00
( " master " , bpo : : value < StringsVector > ( ) - > default_value ( StringsVector ( ) , " " )
- > multitoken ( ) , " master file(s) " )
( " plugin " , bpo : : value < StringsVector > ( ) - > default_value ( StringsVector ( ) , " " )
- > multitoken ( ) , " plugin file(s) " )
2012-04-02 18:47:09 +00:00
( " anim-verbose " , bpo : : value < bool > ( ) - > implicit_value ( true )
2012-01-10 05:34:29 +00:00
- > default_value ( false ) , " output animation indices files " )
2012-04-02 18:47:09 +00:00
( " debug " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 19:06:09 +00:00
- > default_value ( false ) , " debug mode " )
2012-04-02 18:47:09 +00:00
( " nosound " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 19:06:09 +00:00
- > default_value ( false ) , " disable all sounds " )
2012-04-02 18:47:09 +00:00
( " script-verbose " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 19:06:09 +00:00
- > default_value ( false ) , " verbose script output " )
2012-04-02 18:47:09 +00:00
( " script-all " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 19:06:09 +00:00
- > default_value ( false ) , " compile all scripts (excluding dialogue scripts) at startup " )
2012-07-30 09:43:28 +00:00
( " script-console " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( false ) , " enable console-only script functionality " )
2012-07-30 10:37:46 +00:00
( " script-run " , bpo : : value < std : : string > ( ) - > default_value ( " " ) ,
2012-11-27 10:50:49 +00:00
" select a file containing a list of console commands that is executed on startup " )
2012-07-30 10:37:46 +00:00
2012-07-30 09:43:28 +00:00
( " new-game " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( false ) , " activate char gen/new game mechanics " )
2012-04-02 18:47:09 +00:00
( " fs-strict " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 19:06:09 +00:00
- > default_value ( false ) , " strict file system handling (no case folding) " )
Added new command line option: "encoding"
Added new command line option: "encoding" which allow to
change font encoding used in game messages.
Currently there are three evailable encodings:
win1250 - Central and Eastern European (languages
that use Latin script, such as Polish,
Czech, Slovak, Hungarian, Slovene, Bosnian,
Croatian, Serbian (Latin script),
Romanian and Albanian)
win1251 - languages that use the Cyrillic alphabet
such as Russian, Bulgarian, Serbian Cyrillic
and others
win1252 - Western European (Latin) - default
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
2011-07-17 20:16:50 +00:00
2012-04-02 18:47:09 +00:00
( " encoding " , bpo : : value < std : : string > ( ) - >
Added new command line option: "encoding"
Added new command line option: "encoding" which allow to
change font encoding used in game messages.
Currently there are three evailable encodings:
win1250 - Central and Eastern European (languages
that use Latin script, such as Polish,
Czech, Slovak, Hungarian, Slovene, Bosnian,
Croatian, Serbian (Latin script),
Romanian and Albanian)
win1251 - languages that use the Cyrillic alphabet
such as Russian, Bulgarian, Serbian Cyrillic
and others
win1252 - Western European (Latin) - default
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
2011-07-17 20:16:50 +00:00
default_value ( " win1252 " ) ,
2011-07-17 20:53:20 +00:00
" Character encoding used in OpenMW game messages: \n "
Added new command line option: "encoding"
Added new command line option: "encoding" which allow to
change font encoding used in game messages.
Currently there are three evailable encodings:
win1250 - Central and Eastern European (languages
that use Latin script, such as Polish,
Czech, Slovak, Hungarian, Slovene, Bosnian,
Croatian, Serbian (Latin script),
Romanian and Albanian)
win1251 - languages that use the Cyrillic alphabet
such as Russian, Bulgarian, Serbian Cyrillic
and others
win1252 - Western European (Latin) - default
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
2011-07-17 20:16:50 +00:00
" \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 " )
2011-10-08 08:31:23 +00:00
2012-04-04 17:59:46 +00:00
( " fallback " , bpo : : value < FallbackMap > ( ) - > default_value ( FallbackMap ( ) , " " )
2012-04-02 18:47:09 +00:00
- > multitoken ( ) - > composing ( ) , " fallback values " )
2010-06-16 10:21:02 +00:00
;
2010-10-05 16:23:53 +00:00
2011-08-19 19:06:09 +00:00
bpo : : parsed_options valid_opts = bpo : : command_line_parser ( argc , argv )
. options ( desc ) . allow_unregistered ( ) . run ( ) ;
2011-01-04 00:34:55 +00:00
2011-08-19 19:06:09 +00:00
bpo : : variables_map variables ;
2010-06-11 17:53:00 +00:00
2011-08-19 19:06:09 +00:00
// Runtime options override settings from all configs
2010-07-04 21:16:57 +00:00
bpo : : store ( valid_opts , variables ) ;
bpo : : notify ( variables ) ;
2010-06-10 08:31:50 +00:00
2011-09-02 18:01:24 +00:00
cfgMgr . readConfiguration ( variables , desc ) ;
2011-07-08 14:16:20 +00:00
bool run = true ;
2010-06-10 08:31:50 +00:00
if ( variables . count ( " help " ) )
{
2010-06-16 10:21:02 +00:00
std : : cout < < desc < < std : : endl ;
2011-07-08 14:16:20 +00:00
run = false ;
}
if ( variables . count ( " version " ) )
{
std : : cout < < " OpenMW version " < < OPENMW_VERSION < < std : : endl ;
run = false ;
2010-06-10 08:31:50 +00:00
}
2010-06-16 10:21:02 +00:00
2011-07-08 14:16:20 +00:00
if ( ! run )
2010-06-16 10:21:02 +00:00
return false ;
2011-07-08 14:16:20 +00:00
Added new command line option: "encoding"
Added new command line option: "encoding" which allow to
change font encoding used in game messages.
Currently there are three evailable encodings:
win1250 - Central and Eastern European (languages
that use Latin script, such as Polish,
Czech, Slovak, Hungarian, Slovene, Bosnian,
Croatian, Serbian (Latin script),
Romanian and Albanian)
win1251 - languages that use the Cyrillic alphabet
such as Russian, Bulgarian, Serbian Cyrillic
and others
win1252 - Western European (Latin) - default
Signed-off-by: Lukasz Gromanowski <lgromanowski@gmail.com>
2011-07-17 20:16:50 +00:00
// Font encoding settings
std : : string encoding ( variables [ " encoding " ] . as < std : : string > ( ) ) ;
2012-12-26 15:19:59 +00:00
std : : cout < < ToUTF8 : : encodingUsingMessage ( encoding ) < < std : : endl ;
engine . setEncoding ( ToUTF8 : : calculateEncoding ( encoding ) ) ;
2010-06-16 10:21:02 +00:00
2011-03-29 11:57:56 +00:00
// directory settings
2011-08-19 19:06:09 +00:00
engine . enableFSStrict ( variables [ " fs-strict " ] . as < bool > ( ) ) ;
2011-05-05 17:56:16 +00:00
2011-09-02 20:45:21 +00:00
Files : : PathContainer dataDirs ( variables [ " data " ] . as < Files : : PathContainer > ( ) ) ;
2011-05-05 17:50:28 +00:00
2011-08-19 19:06:09 +00:00
std : : string local ( variables [ " data-local " ] . as < std : : string > ( ) ) ;
2011-05-05 17:50:28 +00:00
if ( ! local . empty ( ) )
2011-08-19 19:06:09 +00:00
{
2012-08-16 10:59:28 +00:00
dataDirs . push_back ( Files : : PathContainer : : value_type ( local ) ) ;
2011-08-19 19:06:09 +00:00
}
2011-05-05 17:39:11 +00:00
2012-02-22 22:56:07 +00:00
cfgMgr . processPaths ( dataDirs ) ;
2011-08-19 19:06:09 +00:00
engine . setDataDirs ( dataDirs ) ;
engine . setResourceDir ( variables [ " resources " ] . as < std : : string > ( ) ) ;
2011-03-29 11:57:56 +00:00
// master and plugin
2011-08-19 19:06:09 +00:00
StringsVector master = variables [ " master " ] . as < StringsVector > ( ) ;
2011-03-29 11:57:56 +00:00
if ( master . empty ( ) )
{
std : : cout < < " No master file given. Assuming Morrowind.esm " < < std : : endl ;
2011-08-19 19:06:09 +00:00
master . push_back ( " Morrowind " ) ;
2011-03-29 11:57:56 +00:00
}
2011-08-19 19:06:09 +00:00
if ( master . size ( ) > 1 )
2011-03-29 11:57:56 +00:00
{
std : : cout
< < " Ignoring all but the first master file (multiple master files not yet supported). "
< < std : : endl ;
}
2011-08-19 19:06:09 +00:00
engine . addMaster ( master [ 0 ] ) ;
2011-03-29 11:57:56 +00:00
2011-08-19 19:06:09 +00:00
StringsVector plugin = variables [ " plugin " ] . as < StringsVector > ( ) ;
2011-03-29 11:57:56 +00:00
if ( ! plugin . empty ( ) )
2011-08-19 19:06:09 +00:00
{
2011-03-29 11:57:56 +00:00
std : : cout < < " Ignoring plugin files (plugins not yet supported). " < < std : : endl ;
2011-08-19 19:06:09 +00:00
}
2011-03-29 11:57:56 +00:00
// startup-settings
2011-08-19 19:06:09 +00:00
engine . setCell ( variables [ " start " ] . as < std : : string > ( ) ) ;
engine . setNewGame ( variables [ " new-game " ] . as < bool > ( ) ) ;
2011-03-29 11:57:56 +00:00
// other settings
2011-08-19 19:06:09 +00:00
engine . setDebugMode ( variables [ " debug " ] . as < bool > ( ) ) ;
engine . setSoundUsage ( ! variables [ " nosound " ] . as < bool > ( ) ) ;
engine . setScriptsVerbosity ( variables [ " script-verbose " ] . as < bool > ( ) ) ;
engine . setCompileAll ( variables [ " script-all " ] . as < bool > ( ) ) ;
2012-01-10 05:34:29 +00:00
engine . setAnimationVerbose ( variables [ " anim-verbose " ] . as < bool > ( ) ) ;
2012-04-04 17:58:04 +00:00
engine . setFallbackValues ( variables [ " fallback " ] . as < FallbackMap > ( ) . mMap ) ;
2012-07-30 09:43:28 +00:00
engine . setScriptConsoleMode ( variables [ " script-console " ] . as < bool > ( ) ) ;
2012-07-30 10:37:46 +00:00
engine . setStartupScript ( variables [ " script-run " ] . as < std : : string > ( ) ) ;
2010-10-06 12:52:53 +00:00
2010-06-23 00:52:17 +00:00
return true ;
2010-06-16 10:21:02 +00:00
}
int main ( int argc , char * * argv )
{
2011-03-08 22:42:04 +00:00
# if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
2011-03-12 00:00:42 +00:00
// set current dir to bundle path
2011-06-26 16:15:42 +00:00
boost : : filesystem : : path bundlePath = boost : : filesystem : : path ( Ogre : : macBundlePath ( ) ) . parent_path ( ) ;
2011-03-12 00:00:42 +00:00
boost : : filesystem : : current_path ( bundlePath ) ;
2011-03-08 22:42:04 +00:00
# endif
2010-09-19 00:01:01 +00:00
2010-06-16 10:21:02 +00:00
try
2010-02-28 13:51:17 +00:00
{
2012-01-21 00:14:35 +00:00
Files : : ConfigurationManager cfgMgr ;
2011-08-19 19:06:09 +00:00
OMW : : Engine engine ( cfgMgr ) ;
2011-03-12 00:00:42 +00:00
2011-08-19 19:06:09 +00:00
if ( parseOptions ( argc , argv , engine , cfgMgr ) )
2010-06-16 10:21:02 +00:00
{
engine . go ( ) ;
}
}
2011-08-19 19:06:09 +00:00
catch ( std : : exception & e )
2010-06-16 10:21:02 +00:00
{
2011-08-19 19:06:09 +00:00
std : : cout < < " \n ERROR: " < < e . what ( ) < < std : : endl ;
2010-06-16 10:21:02 +00:00
return 1 ;
2010-02-28 13:51:17 +00:00
}
2010-10-05 16:23:53 +00:00
2010-06-16 10:21:02 +00:00
return 0 ;
2010-02-28 13:51:17 +00:00
}
2010-09-19 00:01:01 +00:00
// Platform specific for Windows when there is no console built into the executable.
// Windows will call the WinMain function instead of main in this case, the normal
// main function is then called with the __argc and __argv parameters.
// In addition if it is a debug build it will redirect cout to the debug console in Visual Studio
# if defined(_WIN32) && !defined(_CONSOLE)
# if defined(_DEBUG)
class DebugOutput : public boost : : iostreams : : sink
{
public :
std : : streamsize write ( const char * str , std : : streamsize size )
{
// Make a copy for null termination
std : : string tmp ( str , size ) ;
// Write string to Visual Studio Debug output
OutputDebugString ( tmp . c_str ( ) ) ;
return size ;
}
} ;
# else
class Logger : public boost : : iostreams : : sink
{
public :
Logger ( std : : ofstream & stream )
: out ( stream )
{
}
std : : streamsize write ( const char * str , std : : streamsize size )
{
out . write ( str , size ) ;
out . flush ( ) ;
return size ;
}
private :
std : : ofstream & out ;
} ;
# endif
int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nShowCmd )
{
std : : streambuf * old_rdbuf = std : : cout . rdbuf ( ) ;
int ret = 0 ;
# if defined(_DEBUG)
// Redirect cout to VS debug output when running in debug mode
{
boost : : iostreams : : stream_buffer < DebugOutput > sb ;
sb . open ( DebugOutput ( ) ) ;
# else
// Redirect cout to openmw.log
std : : ofstream logfile ( " openmw.log " ) ;
{
boost : : iostreams : : stream_buffer < Logger > sb ;
sb . open ( Logger ( logfile ) ) ;
# endif
std : : cout . rdbuf ( & sb ) ;
ret = main ( __argc , __argv ) ;
std : : cout . rdbuf ( old_rdbuf ) ;
}
return ret ;
}
# endif