2010-02-28 14:51:17 +01:00
# include <iostream>
2013-12-05 14:28:39 +01:00
# include <cstdio>
2010-02-28 14:51:17 +01:00
2014-01-22 17:33:55 +01:00
# include <components/version/version.hpp>
2012-01-21 01:14:35 +01:00
# include <components/files/configurationmanager.hpp>
2011-04-28 09:39:40 +02:00
2014-06-10 04:10:34 +02:00
# include <SDL_messagebox.h>
# include <SDL_main.h>
2010-06-16 12:13:21 +02:00
# include "engine.hpp"
2010-02-28 14:51:17 +01:00
2011-01-04 01:34:55 +01:00
# include <boost/iostreams/concepts.hpp>
2010-09-19 02:01:01 +02:00
# include <boost/iostreams/stream_buffer.hpp>
2014-06-11 16:17:48 +02:00
# include <boost/filesystem/fstream.hpp>
2010-09-19 02:01:01 +02:00
2015-04-25 15:19:17 +02:00
# include <OgrePlatform.h>
2014-06-10 02:22:58 +02:00
# if defined(_WIN32)
2010-09-19 02:01:01 +02:00
// For OutputDebugString
2014-01-04 07:30:43 +02:00
# define WIN32_LEAN_AND_MEAN
2010-09-19 02:01:01 +02:00
# include <Windows.h>
// makes __argc and __argv available on windows
2012-07-17 09:44:24 +02:00
# include <cstdlib>
2010-09-19 02:01:01 +02:00
# endif
2013-11-16 15:56:15 +01:00
# if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
2013-11-16 17:00:26 +01:00
# include <csignal>
2013-11-16 15:56:15 +01:00
extern int cc_install_handlers ( int argc , char * * argv , int num_signals , int * sigs , const char * logfile , int ( * user_info ) ( char * , char * ) ) ;
extern int is_debugger_attached ( void ) ;
# endif
2011-06-25 19:29:11 +04:00
// for Ogre::macBundlePath
# if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
# include <OSX/macUtils.h>
# endif
2012-02-24 20:19:32 +01: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-03 01:47:43 +02:00
struct FallbackMap {
std : : map < std : : string , std : : string > mMap ;
2012-04-03 02:14:39 +02:00
} ;
2012-04-03 01:47:43 +02:00
2012-04-03 02:14:39 +02:00
void validate ( boost : : any & v , std : : vector < std : : string > const & tokens , FallbackMap * , int )
{
if ( v . empty ( ) )
2012-04-03 01:47:43 +02:00
{
2012-04-03 02:14:39 +02:00
v = boost : : any ( FallbackMap ( ) ) ;
}
FallbackMap * map = boost : : any_cast < FallbackMap > ( & v ) ;
2013-07-31 18:46:32 +02:00
for ( std : : vector < std : : string > : : const_iterator it = tokens . begin ( ) ; it ! = tokens . end ( ) ; + + it )
2012-04-03 02:14:39 +02:00
{
int sep = it - > find ( " , " ) ;
2012-04-04 23:51:22 +02:00
if ( sep < 1 | | sep = = ( int ) it - > length ( ) - 1 )
2012-04-04 23:52:42 +02:00
# if (BOOST_VERSION < 104200)
2012-04-04 23:51:22 +02:00
throw boost : : program_options : : validation_error ( " invalid value " ) ;
# else
2012-04-04 15:23:14 +02:00
throw boost : : program_options : : validation_error ( boost : : program_options : : validation_error : : invalid_option_value ) ;
2012-04-05 00:16:44 +02:00
# endif
2012-04-03 02:14:39 +02:00
std : : string key ( it - > substr ( 0 , sep ) ) ;
std : : string value ( it - > substr ( sep + 1 ) ) ;
2014-09-26 17:12:48 +02:00
if ( map - > mMap . find ( key ) = = map - > mMap . end ( ) )
2012-04-03 01:47:43 +02:00
{
2012-10-09 17:10:25 +02:00
map - > mMap . insert ( std : : make_pair ( key , value ) ) ;
2012-04-03 01:47:43 +02:00
}
2012-04-03 02:14:39 +02:00
}
}
2012-04-03 01:47:43 +02:00
2011-08-19 21:06:09 +02: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 01:14:35 +01:00
bool parseOptions ( int argc , char * * argv , OMW : : Engine & engine , Files : : ConfigurationManager & cfgMgr )
2010-02-28 14:51:17 +01:00
{
2010-06-27 16:44:15 -07:00
// Create a local alias for brevity
2010-10-05 18:23:53 +02:00
namespace bpo = boost : : program_options ;
2011-08-19 21:06:09 +02:00
typedef std : : vector < std : : string > StringsVector ;
2010-06-27 16:44:15 -07:00
2011-08-19 21:06:09 +02:00
bpo : : options_description desc ( " Syntax: openmw <options> \n Allowed options " ) ;
2010-06-10 10:31:50 +02:00
desc . add_options ( )
2011-08-19 21:06:09 +02:00
( " help " , " print help message " )
2011-07-08 16:16:20 +02:00
( " version " , " print version information and quit " )
2011-09-02 22:45:21 +02:00
( " data " , bpo : : value < Files : : PathContainer > ( ) - > default_value ( Files : : PathContainer ( ) , " data " )
2014-09-13 20:41:57 +02:00
- > multitoken ( ) - > composing ( ) , " set data directories (later directories have higher priority) " )
2011-08-19 21:06:09 +02:00
( " data-local " , bpo : : value < std : : string > ( ) - > default_value ( " " ) ,
2011-05-05 19:50:28 +02:00
" set local data directory (highest priority) " )
2011-08-19 21:06:09 +02:00
2013-03-09 21:08:08 +01:00
( " fallback-archive " , bpo : : value < StringsVector > ( ) - > default_value ( StringsVector ( ) , " fallback-archive " )
- > multitoken ( ) , " set fallback BSA archives (later archives have higher priority) " )
2011-08-19 21:06:09 +02:00
( " resources " , bpo : : value < std : : string > ( ) - > default_value ( " resources " ) ,
2011-01-04 01:34:55 +01:00
" set resources directory " )
2011-08-19 21:06:09 +02:00
2013-11-16 12:08:00 +01:00
( " start " , bpo : : value < std : : string > ( ) - > default_value ( " " ) ,
2010-10-05 18:23:53 +02:00
" set initial cell " )
2011-08-19 21:06:09 +02:00
2013-09-29 09:11:57 +02:00
( " content " , bpo : : value < StringsVector > ( ) - > default_value ( StringsVector ( ) , " " )
- > multitoken ( ) , " content file(s): esm/esp, or omwgame/omwaddon " )
2011-08-19 21:06:09 +02:00
2013-12-26 02:24:12 +01:00
( " no-sound " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 21:06:09 +02:00
- > default_value ( false ) , " disable all sounds " )
2012-04-02 20:47:09 +02:00
( " script-verbose " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 21:06:09 +02:00
- > default_value ( false ) , " verbose script output " )
2012-04-02 20:47:09 +02:00
( " script-all " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 21:06:09 +02:00
- > default_value ( false ) , " compile all scripts (excluding dialogue scripts) at startup " )
2014-12-13 02:47:04 +01:00
( " script-all-dialogue " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( false ) , " compile all dialogue scripts at startup " )
2012-07-30 11:43:28 +02:00
( " script-console " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( false ) , " enable console-only script functionality " )
2012-07-30 12:37:46 +02:00
( " script-run " , bpo : : value < std : : string > ( ) - > default_value ( " " ) ,
2012-11-27 04:50:49 -06:00
" select a file containing a list of console commands that is executed on startup " )
2012-07-30 12:37:46 +02:00
2014-02-02 14:09:59 +01:00
( " script-warn " , bpo : : value < int > ( ) - > implicit_value ( 1 )
- > default_value ( 1 ) ,
" handling of warnings when compiling scripts \n "
" \t 0 - ignore warning \n "
" \t 1 - show warning but consider script as correctly compiled anyway \n "
" \t 2 - treat warnings as errors " )
2014-07-21 09:34:10 +02:00
( " script-blacklist " , bpo : : value < StringsVector > ( ) - > default_value ( StringsVector ( ) , " " )
- > multitoken ( ) , " ignore the specified script (if the use of the blacklist is enabled) " )
( " script-blacklist-use " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( true ) , " enable script blacklisting " )
2015-01-07 03:03:56 +01:00
( " load-savegame " , bpo : : value < std : : string > ( ) - > default_value ( " " ) ,
2015-02-04 22:17:30 +01:00
" load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory) " )
2015-01-07 03:03:56 +01:00
2013-11-16 11:33:20 +01:00
( " skip-menu " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( false ) , " skip main menu on game startup " )
2014-09-01 11:55:12 +02:00
( " new-game " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( false ) , " run new game sequence (ignored if skip-menu=0) " )
2012-04-02 20:47:09 +02:00
( " fs-strict " , bpo : : value < bool > ( ) - > implicit_value ( true )
2011-08-19 21:06:09 +02: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 22:16:50 +02:00
2012-04-02 20:47:09 +02: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 22:16:50 +02:00
default_value ( " win1252 " ) ,
2011-07-17 22:53:20 +02: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 22:16:50 +02: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 10:31:23 +02:00
2012-04-04 19:59:46 +02:00
( " fallback " , bpo : : value < FallbackMap > ( ) - > default_value ( FallbackMap ( ) , " " )
2012-04-02 20:47:09 +02:00
- > multitoken ( ) - > composing ( ) , " fallback values " )
2013-11-29 20:06:54 +01:00
( " no-grab " , " Don't grab mouse cursor " )
2014-08-11 20:37:29 +02:00
( " export-fonts " , bpo : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( false ) , " Export Morrowind .fnt fonts to PNG image and XML file in current directory " )
2013-01-08 19:52:18 -08:00
( " activate-dist " , bpo : : value < int > ( ) - > default_value ( - 1 ) , " activation distance override " ) ;
2011-08-19 21:06:09 +02:00
bpo : : parsed_options valid_opts = bpo : : command_line_parser ( argc , argv )
. options ( desc ) . allow_unregistered ( ) . run ( ) ;
2011-01-04 01:34:55 +01:00
2011-08-19 21:06:09 +02:00
bpo : : variables_map variables ;
2010-06-11 19:53:00 +02:00
2011-08-19 21:06:09 +02:00
// Runtime options override settings from all configs
2010-07-04 17:16:57 -04:00
bpo : : store ( valid_opts , variables ) ;
bpo : : notify ( variables ) ;
2010-06-10 10:31:50 +02:00
if ( variables . count ( " help " ) )
{
2010-06-16 12:21:02 +02:00
std : : cout < < desc < < std : : endl ;
2015-01-31 18:36:53 +01:00
return false ;
2011-07-08 16:16:20 +02:00
}
2015-01-31 18:36:53 +01:00
std : : cout < < " OpenMW version " < < OPENMW_VERSION ;
std : : string rev = OPENMW_VERSION_COMMITHASH ;
std : : string tag = OPENMW_VERSION_TAGHASH ;
if ( ! rev . empty ( ) & & ! tag . empty ( ) )
2011-07-08 16:16:20 +02:00
{
2015-01-31 18:36:53 +01:00
rev = rev . substr ( 0 , 10 ) ;
std : : cout < < " (revision " < < rev < < " ) " ;
2010-06-10 10:31:50 +02:00
}
2015-01-31 18:36:53 +01:00
std : : cout < < std : : endl ;
2010-06-16 12:21:02 +02:00
2015-01-31 18:36:53 +01:00
if ( variables . count ( " version " ) )
2010-06-16 12:21:02 +02:00
return false ;
2011-07-08 16:16:20 +02:00
2013-12-23 23:55:40 +01:00
cfgMgr . readConfiguration ( variables , desc ) ;
2013-11-29 20:06:54 +01:00
engine . setGrabMouse ( ! variables . count ( " no-grab " ) ) ;
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 22:16:50 +02:00
// Font encoding settings
std : : string encoding ( variables [ " encoding " ] . as < std : : string > ( ) ) ;
2012-12-26 16:19:59 +01:00
std : : cout < < ToUTF8 : : encodingUsingMessage ( encoding ) < < std : : endl ;
engine . setEncoding ( ToUTF8 : : calculateEncoding ( encoding ) ) ;
2010-06-16 12:21:02 +02:00
2011-03-29 13:57:56 +02:00
// directory settings
2011-08-19 21:06:09 +02:00
engine . enableFSStrict ( variables [ " fs-strict " ] . as < bool > ( ) ) ;
2011-05-05 19:56:16 +02:00
2011-09-02 22:45:21 +02:00
Files : : PathContainer dataDirs ( variables [ " data " ] . as < Files : : PathContainer > ( ) ) ;
2011-05-05 19:50:28 +02:00
2011-08-19 21:06:09 +02:00
std : : string local ( variables [ " data-local " ] . as < std : : string > ( ) ) ;
2011-05-05 19:50:28 +02:00
if ( ! local . empty ( ) )
2011-08-19 21:06:09 +02:00
{
2012-08-16 12:59:28 +02:00
dataDirs . push_back ( Files : : PathContainer : : value_type ( local ) ) ;
2011-08-19 21:06:09 +02:00
}
2011-05-05 19:39:11 +02:00
2012-02-22 23:56:07 +01:00
cfgMgr . processPaths ( dataDirs ) ;
2011-08-19 21:06:09 +02:00
engine . setDataDirs ( dataDirs ) ;
2013-03-09 21:08:08 +01:00
// fallback archives
StringsVector archives = variables [ " fallback-archive " ] . as < StringsVector > ( ) ;
2013-07-31 18:46:32 +02:00
for ( StringsVector : : const_iterator it = archives . begin ( ) ; it ! = archives . end ( ) ; + + it )
2013-03-09 21:08:08 +01:00
{
engine . addArchive ( * it ) ;
}
2011-08-19 21:06:09 +02:00
engine . setResourceDir ( variables [ " resources " ] . as < std : : string > ( ) ) ;
2011-03-29 13:57:56 +02:00
2013-09-29 09:11:57 +02:00
StringsVector content = variables [ " content " ] . as < StringsVector > ( ) ;
if ( content . empty ( ) )
2011-03-29 13:57:56 +02:00
{
2013-09-29 09:11:57 +02:00
std : : cout < < " No content file given (esm/esp, nor omwgame/omwaddon). Aborting... " < < std : : endl ;
return false ;
2011-03-29 13:57:56 +02:00
}
2013-09-29 09:11:57 +02:00
StringsVector : : const_iterator it ( content . begin ( ) ) ;
StringsVector : : const_iterator end ( content . end ( ) ) ;
for ( ; it ! = end ; + + it )
2011-08-19 21:06:09 +02:00
{
2013-09-29 09:11:57 +02:00
engine . addContentFile ( * it ) ;
2011-08-19 21:06:09 +02:00
}
2011-03-29 13:57:56 +02:00
// startup-settings
2011-08-19 21:06:09 +02:00
engine . setCell ( variables [ " start " ] . as < std : : string > ( ) ) ;
2014-09-01 11:55:12 +02:00
engine . setSkipMenu ( variables [ " skip-menu " ] . as < bool > ( ) , variables [ " new-game " ] . as < bool > ( ) ) ;
if ( ! variables [ " skip-menu " ] . as < bool > ( ) & & variables [ " new-game " ] . as < bool > ( ) )
std : : cerr < < " new-game used without skip-menu -> ignoring it " < < std : : endl ;
2011-03-29 13:57:56 +02:00
2014-07-21 09:34:10 +02:00
// scripts
2011-08-19 21:06:09 +02:00
engine . setCompileAll ( variables [ " script-all " ] . as < bool > ( ) ) ;
2014-12-13 02:47:04 +01:00
engine . setCompileAllDialogue ( variables [ " script-all-dialogue " ] . as < bool > ( ) ) ;
2014-07-21 09:34:10 +02:00
engine . setScriptsVerbosity ( variables [ " script-verbose " ] . as < bool > ( ) ) ;
2012-07-30 11:43:28 +02:00
engine . setScriptConsoleMode ( variables [ " script-console " ] . as < bool > ( ) ) ;
2012-07-30 12:37:46 +02:00
engine . setStartupScript ( variables [ " script-run " ] . as < std : : string > ( ) ) ;
2014-02-02 14:09:59 +01:00
engine . setWarningsMode ( variables [ " script-warn " ] . as < int > ( ) ) ;
2014-07-21 09:34:10 +02:00
engine . setScriptBlacklist ( variables [ " script-blacklist " ] . as < StringsVector > ( ) ) ;
engine . setScriptBlacklistUse ( variables [ " script-blacklist-use " ] . as < bool > ( ) ) ;
2015-01-07 03:03:56 +01:00
engine . setSaveGameFile ( variables [ " load-savegame " ] . as < std : : string > ( ) ) ;
2014-07-21 09:34:10 +02:00
// other settings
engine . setSoundUsage ( ! variables [ " no-sound " ] . as < bool > ( ) ) ;
engine . setFallbackValues ( variables [ " fallback " ] . as < FallbackMap > ( ) . mMap ) ;
engine . setActivationDistanceOverride ( variables [ " activate-dist " ] . as < int > ( ) ) ;
2014-08-11 20:37:29 +02:00
engine . enableFontExport ( variables [ " export-fonts " ] . as < bool > ( ) ) ;
2010-10-06 14:52:53 +02:00
2010-06-22 17:52:17 -07:00
return true ;
2010-06-16 12:21:02 +02:00
}
2014-06-10 02:22:58 +02:00
# if defined(_WIN32) && defined(_DEBUG)
2010-09-19 02:01:01 +02:00
class DebugOutput : public boost : : iostreams : : sink
{
public :
std : : streamsize write ( const char * str , std : : streamsize size )
{
// Make a copy for null termination
2015-03-15 08:49:03 +13:00
std : : string tmp ( str , static_cast < unsigned int > ( size ) ) ;
2010-09-19 02:01:01 +02:00
// Write string to Visual Studio Debug output
OutputDebugString ( tmp . c_str ( ) ) ;
return size ;
}
} ;
# else
2014-06-10 02:22:58 +02:00
class Tee : public boost : : iostreams : : sink
2010-09-19 02:01:01 +02:00
{
public :
2014-06-10 02:22:58 +02:00
Tee ( std : : ostream & stream , std : : ostream & stream2 )
: out ( stream ) , out2 ( stream2 )
2010-09-19 02:01:01 +02:00
{
}
std : : streamsize write ( const char * str , std : : streamsize size )
{
out . write ( str , size ) ;
out . flush ( ) ;
2014-06-10 02:22:58 +02:00
out2 . write ( str , size ) ;
out2 . flush ( ) ;
2010-09-19 02:01:01 +02:00
return size ;
}
private :
2014-06-10 02:22:58 +02:00
std : : ostream & out ;
std : : ostream & out2 ;
2010-09-19 02:01:01 +02:00
} ;
# endif
2014-06-10 02:22:58 +02:00
int main ( int argc , char * * argv )
2010-09-19 02:01:01 +02:00
{
2014-06-13 22:03:52 +02:00
// Some objects used to redirect cout and cerr
// Scope must be here, so this still works inside the catch block for logging exceptions
2014-06-10 02:22:58 +02:00
std : : streambuf * cout_rdbuf = std : : cout . rdbuf ( ) ;
std : : streambuf * cerr_rdbuf = std : : cerr . rdbuf ( ) ;
2010-09-19 02:01:01 +02:00
2014-06-21 21:15:41 -05:00
# if !(defined(_WIN32) && defined(_DEBUG))
2014-06-13 22:03:52 +02:00
boost : : iostreams : : stream_buffer < Tee > coutsb ;
boost : : iostreams : : stream_buffer < Tee > cerrsb ;
2014-06-21 21:15:41 -05:00
# endif
2014-06-13 22:03:52 +02:00
std : : ostream oldcout ( cout_rdbuf ) ;
std : : ostream oldcerr ( cerr_rdbuf ) ;
boost : : filesystem : : ofstream logfile ;
2014-07-26 22:29:04 +02:00
std : : auto_ptr < OMW : : Engine > engine ;
2010-09-19 02:01:01 +02:00
int ret = 0 ;
2014-06-10 02:22:58 +02:00
try
2010-09-19 02:01:01 +02:00
{
2014-06-10 02:22:58 +02:00
Files : : ConfigurationManager cfgMgr ;
# if defined(_WIN32) && defined(_DEBUG)
// Redirect cout and cerr to VS debug output when running in debug mode
2010-09-19 02:01:01 +02:00
boost : : iostreams : : stream_buffer < DebugOutput > sb ;
sb . open ( DebugOutput ( ) ) ;
2014-06-10 02:22:58 +02:00
std : : cout . rdbuf ( & sb ) ;
std : : cerr . rdbuf ( & sb ) ;
2010-09-19 02:01:01 +02:00
# else
2014-06-10 02:22:58 +02:00
// Redirect cout and cerr to openmw.log
2014-06-13 22:03:52 +02:00
logfile . open ( boost : : filesystem : : path ( cfgMgr . getLogPath ( ) / " /openmw.log " ) ) ;
2014-06-10 02:22:58 +02:00
coutsb . open ( Tee ( logfile , oldcout ) ) ;
cerrsb . open ( Tee ( logfile , oldcerr ) ) ;
2014-06-13 22:03:52 +02:00
std : : cout . rdbuf ( & coutsb ) ;
2014-06-10 02:22:58 +02:00
std : : cerr . rdbuf ( & cerrsb ) ;
2010-09-19 02:01:01 +02:00
# endif
2014-06-13 22:03:52 +02:00
2014-06-10 02:22:58 +02:00
# if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
// Unix crash catcher
if ( ( argc = = 2 & & strcmp ( argv [ 1 ] , " --cc-handle-crash " ) = = 0 ) | | ! is_debugger_attached ( ) )
{
int s [ 5 ] = { SIGSEGV , SIGILL , SIGFPE , SIGBUS , SIGABRT } ;
2014-06-19 01:10:33 +02:00
cc_install_handlers ( argc , argv , 5 , s , ( cfgMgr . getLogPath ( ) / " crash.log " ) . string ( ) . c_str ( ) , NULL ) ;
2014-06-10 02:22:58 +02:00
std : : cout < < " Installing crash catcher " < < std : : endl ;
}
else
std : : cout < < " Running in a debugger, not installing crash catcher " < < std : : endl ;
# endif
# if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
// set current dir to bundle path
boost : : filesystem : : path bundlePath = boost : : filesystem : : path ( Ogre : : macBundlePath ( ) ) . parent_path ( ) ;
boost : : filesystem : : current_path ( bundlePath ) ;
# endif
2014-07-26 22:29:04 +02:00
engine . reset ( new OMW : : Engine ( cfgMgr ) ) ;
2014-06-10 02:22:58 +02:00
2014-07-26 22:29:04 +02:00
if ( parseOptions ( argc , argv , * engine , cfgMgr ) )
2014-06-10 02:22:58 +02:00
{
2014-07-26 22:29:04 +02:00
engine - > go ( ) ;
2014-06-10 02:22:58 +02:00
}
}
catch ( std : : exception & e )
{
# if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
2015-02-10 22:01:57 +01:00
if ( ! isatty ( fileno ( stdin ) ) )
2014-06-10 02:22:58 +02:00
# endif
SDL_ShowSimpleMessageBox ( 0 , " OpenMW: Fatal error " , e . what ( ) , NULL ) ;
2010-09-19 02:01:01 +02:00
2015-02-10 22:01:57 +01:00
std : : cerr < < " \n ERROR: " < < e . what ( ) < < std : : endl ;
2014-06-10 02:22:58 +02:00
ret = 1 ;
2010-09-19 02:01:01 +02:00
}
2014-06-10 02:22:58 +02:00
// Restore cout and cerr
std : : cout . rdbuf ( cout_rdbuf ) ;
std : : cerr . rdbuf ( cerr_rdbuf ) ;
2010-09-19 02:01:01 +02:00
return ret ;
}
2014-06-10 02:22:58 +02: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.
# if defined(_WIN32) && !defined(_CONSOLE)
int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nShowCmd )
{
return main ( __argc , __argv ) ;
}
2010-09-19 02:01:01 +02:00
# endif