2012-11-22 13:30:02 +01:00
# include "editor.hpp"
2014-12-01 14:08:27 +11:00
# include <openengine/bullet/BulletShapeLoader.h>
2013-08-21 17:14:29 -05:00
# include <QApplication>
2013-09-03 01:44:21 -05:00
# include <QLocalServer>
# include <QLocalSocket>
2013-09-08 14:31:20 +02:00
# include <QMessageBox>
2012-11-22 13:30:02 +01:00
2013-11-02 02:48:30 +01:00
# include <OgreRoot.h>
# include <OgreRenderWindow.h>
2014-03-16 12:44:01 +01:00
# include <extern/shiny/Main/Factory.hpp>
# include <extern/shiny/Platforms/Ogre/OgrePlatform.hpp>
2014-02-25 11:58:32 +01:00
# include <components/ogreinit/ogreinit.hpp>
2015-01-13 17:29:25 +01:00
# include <components/nifogre/ogrenifloader.hpp>
2014-03-09 12:32:21 +01:00
2012-11-26 12:29:22 +01:00
# include "model/doc/document.hpp"
# include "model/world/data.hpp"
2015-03-19 17:21:15 +01:00
CS : : Editor : : Editor ( )
: mUserSettings ( mCfgMgr ) , mDocumentManager ( mCfgMgr ) ,
2014-12-01 06:44:12 +11:00
mViewManager ( mDocumentManager ) ,
2014-12-05 01:09:42 +11:00
mIpcServerName ( " org.openmw.OpenCS " ) , mServer ( NULL ) , mClientSocket ( NULL ) , mPid ( " " ) , mLock ( )
2012-11-22 13:30:02 +01:00
{
2014-03-09 12:32:21 +01:00
std : : pair < Files : : PathContainer , std : : vector < std : : string > > config = readConfig ( ) ;
2014-02-25 11:58:32 +01:00
2014-03-09 12:32:21 +01:00
setupDataFiles ( config . first ) ;
2014-02-25 11:58:32 +01:00
2014-05-05 05:56:03 -05:00
CSMSettings : : UserSettings : : instance ( ) . loadSettings ( " opencs.ini " ) ;
2014-09-17 17:13:21 +10:00
mSettings . setModel ( CSMSettings : : UserSettings : : instance ( ) ) ;
2013-09-03 04:12:19 -05:00
2015-03-19 17:21:15 +01:00
//NifOgre::Loader::setShowMarkers(true);
2013-09-10 16:45:01 +02:00
2015-03-19 17:21:15 +01:00
//Bsa::registerResources (Files::Collections (config.first, !mFsStrict), config.second, true,
// mFsStrict);
2014-03-09 12:32:21 +01:00
2014-07-04 12:46:57 +02:00
mDocumentManager . listResources ( ) ;
2013-09-10 16:45:01 +02:00
mNewGame . setLocalData ( mLocal ) ;
2013-10-26 22:55:44 -05:00
mFileDialog . setLocalData ( mLocal ) ;
2013-09-10 16:45:01 +02:00
2014-04-21 09:02:58 +02:00
connect ( & mDocumentManager , SIGNAL ( documentAdded ( CSMDoc : : Document * ) ) ,
this , SLOT ( documentAdded ( CSMDoc : : Document * ) ) ) ;
2014-04-26 13:11:27 +02:00
connect ( & mDocumentManager , SIGNAL ( lastDocumentDeleted ( ) ) ,
this , SLOT ( lastDocumentDeleted ( ) ) ) ;
2014-04-21 09:02:58 +02:00
2013-09-08 12:06:28 +02:00
connect ( & mViewManager , SIGNAL ( newGameRequest ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mViewManager , SIGNAL ( newAddonRequest ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 16:14:58 +01:00
connect ( & mViewManager , SIGNAL ( loadDocumentRequest ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 09:33:45 +02:00
connect ( & mViewManager , SIGNAL ( editSettingsRequest ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-02 16:14:58 +01:00
2013-09-08 12:06:28 +02:00
connect ( & mStartup , SIGNAL ( createGame ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mStartup , SIGNAL ( createAddon ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 16:14:58 +01:00
connect ( & mStartup , SIGNAL ( loadDocument ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 09:26:43 +02:00
connect ( & mStartup , SIGNAL ( editConfig ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-07 12:52:01 +01:00
2013-10-26 22:55:44 -05:00
connect ( & mFileDialog , SIGNAL ( signalOpenFiles ( const boost : : filesystem : : path & ) ) ,
this , SLOT ( openFiles ( const boost : : filesystem : : path & ) ) ) ;
connect ( & mFileDialog , SIGNAL ( signalCreateNewFile ( const boost : : filesystem : : path & ) ) ,
this , SLOT ( createNewFile ( const boost : : filesystem : : path & ) ) ) ;
2013-03-07 03:00:59 +01:00
2013-09-10 16:45:01 +02:00
connect ( & mNewGame , SIGNAL ( createRequest ( const boost : : filesystem : : path & ) ) ,
2013-10-19 18:43:47 +02:00
this , SLOT ( createNewGame ( const boost : : filesystem : : path & ) ) ) ;
2012-11-22 13:30:02 +01:00
}
2014-10-15 07:02:19 +11:00
CS : : Editor : : ~ Editor ( )
2014-12-05 01:09:42 +11:00
{
2014-12-05 07:50:03 +11:00
mPidFile . close ( ) ;
2014-12-05 01:09:42 +11:00
if ( mServer & & boost : : filesystem : : exists ( mPid ) )
2015-01-15 12:13:53 +01:00
static_cast < void > ( // silence coverity warning
remove ( mPid . string ( ) . c_str ( ) ) ) ; // ignore any error
2014-12-05 07:38:33 +11:00
2014-12-01 14:08:27 +11:00
// cleanup global resources used by OEngine
delete OEngine : : Physic : : BulletShapeManager : : getSingletonPtr ( ) ;
2014-12-05 01:09:42 +11:00
}
2014-10-15 07:02:19 +11:00
2014-02-25 11:58:32 +01:00
void CS : : Editor : : setupDataFiles ( const Files : : PathContainer & dataDirs )
{
for ( Files : : PathContainer : : const_iterator iter = dataDirs . begin ( ) ; iter ! = dataDirs . end ( ) ; + + iter )
{
2014-03-21 11:56:48 +01:00
QString path = QString : : fromUtf8 ( iter - > string ( ) . c_str ( ) ) ;
2014-02-25 11:58:32 +01:00
mFileDialog . addFiles ( path ) ;
}
}
2014-03-09 12:32:21 +01:00
std : : pair < Files : : PathContainer , std : : vector < std : : string > > CS : : Editor : : readConfig ( )
2012-11-22 13:30:02 +01:00
{
2013-03-07 03:00:59 +01:00
boost : : program_options : : variables_map variables ;
2015-02-03 13:18:03 +01:00
boost : : program_options : : options_description desc ( " Syntax: openmw-cs <options> \n Allowed options " ) ;
2013-02-02 16:14:58 +01:00
2013-03-07 03:00:59 +01:00
desc . add_options ( )
2014-09-13 20:41:57 +02:00
( " data " , boost : : program_options : : value < Files : : PathContainer > ( ) - > default_value ( Files : : PathContainer ( ) , " data " ) - > multitoken ( ) - > composing ( ) )
2013-03-07 03:00:59 +01:00
( " data-local " , boost : : program_options : : value < std : : string > ( ) - > default_value ( " " ) )
( " fs-strict " , boost : : program_options : : value < bool > ( ) - > implicit_value ( true ) - > default_value ( false ) )
2013-10-18 22:11:14 +02:00
( " encoding " , boost : : program_options : : value < std : : string > ( ) - > default_value ( " win1252 " ) )
2014-03-09 12:32:21 +01:00
( " resources " , boost : : program_options : : value < std : : string > ( ) - > default_value ( " resources " ) )
( " fallback-archive " , boost : : program_options : : value < std : : vector < std : : string > > ( ) - >
2014-07-21 12:15:21 +02:00
default_value ( std : : vector < std : : string > ( ) , " fallback-archive " ) - > multitoken ( ) )
( " script-blacklist " , boost : : program_options : : value < std : : vector < std : : string > > ( ) - > default_value ( std : : vector < std : : string > ( ) , " " )
- > multitoken ( ) , " exclude specified script from the verifier (if the use of the blacklist is enabled) " )
( " script-blacklist-use " , boost : : program_options : : value < bool > ( ) - > implicit_value ( true )
- > default_value ( true ) , " enable script blacklisting " ) ;
2013-02-02 16:14:58 +01:00
2013-03-07 03:00:59 +01:00
boost : : program_options : : notify ( variables ) ;
2012-11-23 14:05:49 +01:00
2013-03-07 03:00:59 +01:00
mCfgMgr . readConfiguration ( variables , desc ) ;
2012-11-23 14:05:49 +01:00
2014-05-12 10:32:57 +02:00
mDocumentManager . setEncoding (
ToUTF8 : : calculateEncoding ( variables [ " encoding " ] . as < std : : string > ( ) ) ) ;
2014-03-16 12:44:01 +01:00
mDocumentManager . setResourceDir ( mResources = variables [ " resources " ] . as < std : : string > ( ) ) ;
2014-02-25 11:58:32 +01:00
2014-07-21 12:15:21 +02:00
if ( variables [ " script-blacklist-use " ] . as < bool > ( ) )
mDocumentManager . setBlacklistedScripts (
variables [ " script-blacklist " ] . as < std : : vector < std : : string > > ( ) ) ;
2014-03-09 12:32:21 +01:00
mFsStrict = variables [ " fs-strict " ] . as < bool > ( ) ;
2013-09-08 14:31:20 +02:00
Files : : PathContainer dataDirs , dataLocal ;
2013-03-07 03:00:59 +01:00
if ( ! variables [ " data " ] . empty ( ) ) {
2013-09-08 14:31:20 +02:00
dataDirs = Files : : PathContainer ( variables [ " data " ] . as < Files : : PathContainer > ( ) ) ;
2013-03-07 03:00:59 +01:00
}
2013-02-04 13:46:54 +01:00
2013-03-07 03:00:59 +01:00
std : : string local = variables [ " data-local " ] . as < std : : string > ( ) ;
if ( ! local . empty ( ) ) {
2013-09-08 14:31:20 +02:00
dataLocal . push_back ( Files : : PathContainer : : value_type ( local ) ) ;
2013-03-07 03:00:59 +01:00
}
2012-11-26 12:29:22 +01:00
2013-09-08 14:31:20 +02:00
mCfgMgr . processPaths ( dataDirs ) ;
mCfgMgr . processPaths ( dataLocal , true ) ;
if ( ! dataLocal . empty ( ) )
mLocal = dataLocal [ 0 ] ;
else
{
QMessageBox messageBox ;
messageBox . setWindowTitle ( tr ( " No local data path available " ) ) ;
messageBox . setIcon ( QMessageBox : : Critical ) ;
messageBox . setStandardButtons ( QMessageBox : : Ok ) ;
messageBox . setText ( tr ( " <br><b>OpenCS is unable to access the local data directory. This may indicate a faulty configuration or a broken install.</b> " ) ) ;
messageBox . exec ( ) ;
QApplication : : exit ( 1 ) ;
}
2013-03-07 03:00:59 +01:00
2013-09-08 14:31:20 +02:00
dataDirs . insert ( dataDirs . end ( ) , dataLocal . begin ( ) , dataLocal . end ( ) ) ;
2013-03-07 03:00:59 +01:00
2014-04-22 22:19:53 -05:00
//iterate the data directories and add them to the file dialog for loading
for ( Files : : PathContainer : : const_iterator iter = dataDirs . begin ( ) ; iter ! = dataDirs . end ( ) ; + + iter )
{
2014-04-29 13:16:58 +02:00
QString path = QString : : fromUtf8 ( iter - > string ( ) . c_str ( ) ) ;
2014-04-22 22:19:53 -05:00
mFileDialog . addFiles ( path ) ;
}
2014-03-09 12:32:21 +01:00
return std : : make_pair ( dataDirs , variables [ " fallback-archive " ] . as < std : : vector < std : : string > > ( ) ) ;
2013-03-07 03:00:59 +01:00
}
2013-09-08 12:06:28 +02:00
void CS : : Editor : : createGame ( )
{
mStartup . hide ( ) ;
if ( mNewGame . isHidden ( ) )
mNewGame . show ( ) ;
mNewGame . raise ( ) ;
mNewGame . activateWindow ( ) ;
}
void CS : : Editor : : createAddon ( )
2013-03-07 03:00:59 +01:00
{
mStartup . hide ( ) ;
2013-10-31 18:12:13 -05:00
mFileDialog . showDialog ( CSVDoc : : ContentAction_New ) ;
2013-02-02 16:14:58 +01:00
}
void CS : : Editor : : loadDocument ( )
{
2013-02-07 13:11:41 +01:00
mStartup . hide ( ) ;
2013-10-31 18:12:13 -05:00
mFileDialog . showDialog ( CSVDoc : : ContentAction_Edit ) ;
2013-02-05 22:06:36 +01:00
}
2013-02-02 16:14:58 +01:00
2013-10-26 22:55:44 -05:00
void CS : : Editor : : openFiles ( const boost : : filesystem : : path & savePath )
2013-02-05 22:06:36 +01:00
{
2013-03-07 03:00:59 +01:00
std : : vector < boost : : filesystem : : path > files ;
2013-10-26 22:55:44 -05:00
foreach ( const QString & path , mFileDialog . selectedFilePaths ( ) )
2014-03-21 11:56:48 +01:00
files . push_back ( path . toUtf8 ( ) . constData ( ) ) ;
2013-10-06 22:10:38 -05:00
2014-04-21 09:02:58 +02:00
mDocumentManager . addDocument ( files , savePath , false ) ;
2013-02-07 12:52:01 +01:00
2013-10-22 22:20:21 -05:00
mFileDialog . hide ( ) ;
2013-03-07 03:00:59 +01:00
}
2013-10-26 22:55:44 -05:00
void CS : : Editor : : createNewFile ( const boost : : filesystem : : path & savePath )
2013-03-07 03:00:59 +01:00
{
std : : vector < boost : : filesystem : : path > files ;
2013-10-01 21:29:45 -05:00
foreach ( const QString & path , mFileDialog . selectedFilePaths ( ) ) {
2014-03-21 11:56:48 +01:00
files . push_back ( path . toUtf8 ( ) . constData ( ) ) ;
2013-03-07 03:00:59 +01:00
}
2014-09-16 07:44:07 +10:00
files . push_back ( savePath ) ;
2013-03-07 03:00:59 +01:00
2014-04-21 09:02:58 +02:00
mDocumentManager . addDocument ( files , savePath , true ) ;
2013-02-07 12:52:01 +01:00
2013-10-22 22:20:21 -05:00
mFileDialog . hide ( ) ;
2012-11-22 13:30:02 +01:00
}
2013-09-10 16:45:01 +02:00
void CS : : Editor : : createNewGame ( const boost : : filesystem : : path & file )
2013-09-08 14:31:20 +02:00
{
std : : vector < boost : : filesystem : : path > files ;
2013-09-10 16:45:01 +02:00
files . push_back ( file ) ;
2013-09-08 14:31:20 +02:00
2014-04-21 09:02:58 +02:00
mDocumentManager . addDocument ( files , file , true ) ;
2013-09-08 14:31:20 +02:00
mNewGame . hide ( ) ;
}
2013-09-03 04:12:19 -05:00
void CS : : Editor : : showStartup ( )
{
2013-09-05 23:02:51 -05:00
if ( mStartup . isHidden ( ) )
mStartup . show ( ) ;
mStartup . raise ( ) ;
mStartup . activateWindow ( ) ;
2013-09-03 04:12:19 -05:00
}
2013-09-08 09:26:43 +02:00
void CS : : Editor : : showSettings ( )
{
if ( mSettings . isHidden ( ) )
mSettings . show ( ) ;
2015-01-01 22:50:35 -05:00
mSettings . move ( QCursor : : pos ( ) ) ;
2013-09-08 09:26:43 +02:00
mSettings . raise ( ) ;
mSettings . activateWindow ( ) ;
}
2013-09-03 01:44:21 -05:00
bool CS : : Editor : : makeIPCServer ( )
{
2014-12-05 01:09:42 +11:00
try
{
mPid = boost : : filesystem : : temp_directory_path ( ) ;
2015-02-03 13:18:03 +01:00
mPid / = " openmw-cs.pid " ;
2014-12-05 01:09:42 +11:00
bool pidExists = boost : : filesystem : : exists ( mPid ) ;
2014-12-05 07:50:03 +11:00
mPidFile . open ( mPid ) ;
2014-12-05 01:09:42 +11:00
mLock = boost : : interprocess : : file_lock ( mPid . string ( ) . c_str ( ) ) ;
if ( ! mLock . try_lock ( ) )
{
std : : cerr < < " OpenCS already running. " < < std : : endl ;
return false ;
}
# ifdef _WIN32
2014-12-05 07:50:03 +11:00
mPidFile < < GetCurrentProcessId ( ) < < std : : endl ;
2014-12-05 01:09:42 +11:00
# else
2014-12-05 07:50:03 +11:00
mPidFile < < getpid ( ) < < std : : endl ;
2014-12-05 01:09:42 +11:00
# endif
mServer = new QLocalServer ( this ) ;
if ( pidExists )
{
// hack to get the temp directory path
mServer - > listen ( " dummy " ) ;
QString fullPath = mServer - > fullServerName ( ) ;
mServer - > close ( ) ;
fullPath . remove ( QRegExp ( " dummy$ " ) ) ;
fullPath + = mIpcServerName ;
if ( boost : : filesystem : : exists ( fullPath . toStdString ( ) . c_str ( ) ) )
{
// TODO: compare pid of the current process with that in the file
std : : cout < < " Detected unclean shutdown. " < < std : : endl ;
// delete the stale file
if ( remove ( fullPath . toStdString ( ) . c_str ( ) ) )
std : : cerr < < " ERROR removing stale connection file " < < std : : endl ;
}
}
}
catch ( const std : : exception & e )
{
std : : cerr < < " ERROR " < < e . what ( ) < < std : : endl ;
return false ;
}
2013-09-03 04:12:19 -05:00
2013-09-05 23:02:51 -05:00
if ( mServer - > listen ( mIpcServerName ) )
{
connect ( mServer , SIGNAL ( newConnection ( ) ) , this , SLOT ( showStartup ( ) ) ) ;
return true ;
}
2013-09-03 04:12:19 -05:00
2013-09-05 23:02:51 -05:00
mServer - > close ( ) ;
2014-12-05 01:09:42 +11:00
mServer = NULL ;
2013-09-05 23:02:51 -05:00
return false ;
2013-09-03 01:44:21 -05:00
}
2013-09-03 04:12:19 -05:00
void CS : : Editor : : connectToIPCServer ( )
{
2013-09-05 23:02:51 -05:00
mClientSocket = new QLocalSocket ( this ) ;
mClientSocket - > connectToServer ( mIpcServerName ) ;
mClientSocket - > close ( ) ;
2013-09-03 04:12:19 -05:00
}
2012-11-22 13:30:02 +01:00
int CS : : Editor : : run ( )
{
2013-09-08 14:31:20 +02:00
if ( mLocal . empty ( ) )
return 1 ;
2014-03-16 12:44:01 +01:00
mStartup . show ( ) ;
QApplication : : setQuitOnLastWindowClosed ( true ) ;
return QApplication : : exec ( ) ;
}
2014-10-10 13:35:14 +11:00
// for font used in overlays
2015-03-19 17:21:15 +01:00
//Ogre::Root::getSingleton().addResourceLocation ((mResources / "mygui").string(),
// "FileSystem", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true);
2014-03-16 12:44:01 +01:00
2014-04-21 09:02:58 +02:00
void CS : : Editor : : documentAdded ( CSMDoc : : Document * document )
{
mViewManager . addView ( document ) ;
2014-04-26 13:11:27 +02:00
}
void CS : : Editor : : lastDocumentDeleted ( )
{
2014-11-22 06:59:23 +11:00
QApplication : : quit ( ) ;
2014-05-03 14:36:06 +02:00
}