2012-11-22 12:30:02 +00:00
# include "editor.hpp"
2013-08-21 22:14:29 +00:00
# include <QApplication>
2013-09-03 06:44:21 +00:00
# include <QLocalServer>
# include <QLocalSocket>
2013-09-08 12:31:20 +00:00
# include <QMessageBox>
2012-11-22 12:30:02 +00:00
2015-03-19 16:49:41 +00:00
# include <components/vfs/manager.hpp>
# include <components/vfs/registerarchives.hpp>
2014-03-09 11:32:21 +00:00
2015-03-23 00:57:03 +00:00
# include <components/nifosg/nifloader.hpp>
2012-11-26 11:29:22 +00:00
# include "model/doc/document.hpp"
# include "model/world/data.hpp"
2015-06-08 13:41:39 +00:00
# ifdef _WIN32
# include <Windows.h>
# endif
2015-05-23 18:33:44 +00:00
2015-03-19 16:21:15 +00:00
CS : : Editor : : Editor ( )
: mUserSettings ( mCfgMgr ) , mDocumentManager ( mCfgMgr ) ,
2015-05-01 00:24:27 +00:00
mViewManager ( mDocumentManager ) , mPid ( " " ) ,
mLock ( ) , mIpcServerName ( " org.openmw.OpenCS " ) , mServer ( NULL ) , mClientSocket ( NULL )
2012-11-22 12:30:02 +00:00
{
2014-03-09 11:32:21 +00:00
std : : pair < Files : : PathContainer , std : : vector < std : : string > > config = readConfig ( ) ;
2014-02-25 10:58:32 +00:00
2014-03-09 11:32:21 +00:00
setupDataFiles ( config . first ) ;
2014-02-25 10:58:32 +00:00
2014-05-05 10:56:03 +00:00
CSMSettings : : UserSettings : : instance ( ) . loadSettings ( " opencs.ini " ) ;
2014-09-17 07:13:21 +00:00
mSettings . setModel ( CSMSettings : : UserSettings : : instance ( ) ) ;
2013-09-03 09:12:19 +00:00
2015-03-23 00:57:03 +00:00
NifOsg : : Loader : : setShowMarkers ( true ) ;
2013-09-10 14:45:01 +00:00
2015-03-19 16:49:41 +00:00
mVFS . reset ( new VFS : : Manager ( mFsStrict ) ) ;
2014-03-09 11:32:21 +00:00
2015-03-19 16:49:41 +00:00
VFS : : registerArchives ( mVFS . get ( ) , Files : : Collections ( config . first , ! mFsStrict ) , config . second , true ) ;
mDocumentManager . setVFS ( mVFS . get ( ) ) ;
2014-07-04 10:46:57 +00:00
2013-09-10 14:45:01 +00:00
mNewGame . setLocalData ( mLocal ) ;
2013-10-27 03:55:44 +00:00
mFileDialog . setLocalData ( mLocal ) ;
2013-09-10 14:45:01 +00:00
2014-04-21 07:02:58 +00:00
connect ( & mDocumentManager , SIGNAL ( documentAdded ( CSMDoc : : Document * ) ) ,
this , SLOT ( documentAdded ( CSMDoc : : Document * ) ) ) ;
2014-04-26 11:11:27 +00:00
connect ( & mDocumentManager , SIGNAL ( lastDocumentDeleted ( ) ) ,
this , SLOT ( lastDocumentDeleted ( ) ) ) ;
2014-04-21 07:02:58 +00:00
2013-09-08 10:06:28 +00:00
connect ( & mViewManager , SIGNAL ( newGameRequest ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mViewManager , SIGNAL ( newAddonRequest ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 15:14:58 +00:00
connect ( & mViewManager , SIGNAL ( loadDocumentRequest ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 07:33:45 +00:00
connect ( & mViewManager , SIGNAL ( editSettingsRequest ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-02 15:14:58 +00:00
2013-09-08 10:06:28 +00:00
connect ( & mStartup , SIGNAL ( createGame ( ) ) , this , SLOT ( createGame ( ) ) ) ;
connect ( & mStartup , SIGNAL ( createAddon ( ) ) , this , SLOT ( createAddon ( ) ) ) ;
2013-02-02 15:14:58 +00:00
connect ( & mStartup , SIGNAL ( loadDocument ( ) ) , this , SLOT ( loadDocument ( ) ) ) ;
2013-09-08 07:26:43 +00:00
connect ( & mStartup , SIGNAL ( editConfig ( ) ) , this , SLOT ( showSettings ( ) ) ) ;
2013-02-07 11:52:01 +00:00
2013-10-27 03:55:44 +00: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 & ) ) ) ;
2015-05-29 22:37:58 +00:00
connect ( & mFileDialog , SIGNAL ( rejected ( ) ) , this , SLOT ( cancelFileDialog ( ) ) ) ;
2013-03-07 02:00:59 +00:00
2013-09-10 14:45:01 +00:00
connect ( & mNewGame , SIGNAL ( createRequest ( const boost : : filesystem : : path & ) ) ,
2013-10-19 16:43:47 +00:00
this , SLOT ( createNewGame ( const boost : : filesystem : : path & ) ) ) ;
2015-05-29 22:37:58 +00:00
connect ( & mNewGame , SIGNAL ( cancelCreateGame ( ) ) , this , SLOT ( cancelCreateGame ( ) ) ) ;
2012-11-22 12:30:02 +00:00
}
2014-10-14 20:02:19 +00:00
CS : : Editor : : ~ Editor ( )
2014-12-04 14:09:42 +00:00
{
2014-12-04 20:50:03 +00:00
mPidFile . close ( ) ;
2014-12-04 14:09:42 +00:00
if ( mServer & & boost : : filesystem : : exists ( mPid ) )
2015-01-15 11:13:53 +00:00
static_cast < void > ( // silence coverity warning
remove ( mPid . string ( ) . c_str ( ) ) ) ; // ignore any error
2014-12-04 14:09:42 +00:00
}
2014-10-14 20:02:19 +00:00
2014-02-25 10:58:32 +00:00
void CS : : Editor : : setupDataFiles ( const Files : : PathContainer & dataDirs )
{
for ( Files : : PathContainer : : const_iterator iter = dataDirs . begin ( ) ; iter ! = dataDirs . end ( ) ; + + iter )
{
2014-03-21 10:56:48 +00:00
QString path = QString : : fromUtf8 ( iter - > string ( ) . c_str ( ) ) ;
2014-02-25 10:58:32 +00:00
mFileDialog . addFiles ( path ) ;
}
}
2015-06-16 02:48:45 +00:00
std : : pair < Files : : PathContainer , std : : vector < std : : string > > CS : : Editor : : readConfig ( bool quiet )
2012-11-22 12:30:02 +00:00
{
2013-03-07 02:00:59 +00:00
boost : : program_options : : variables_map variables ;
2015-02-03 12:18:03 +00:00
boost : : program_options : : options_description desc ( " Syntax: openmw-cs <options> \n Allowed options " ) ;
2013-02-02 15:14:58 +00:00
2013-03-07 02:00:59 +00:00
desc . add_options ( )
2014-09-13 18:41:57 +00:00
( " data " , boost : : program_options : : value < Files : : PathContainer > ( ) - > default_value ( Files : : PathContainer ( ) , " data " ) - > multitoken ( ) - > composing ( ) )
2013-03-07 02:00:59 +00: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 20:11:14 +00:00
( " encoding " , boost : : program_options : : value < std : : string > ( ) - > default_value ( " win1252 " ) )
2014-03-09 11:32:21 +00:00
( " resources " , boost : : program_options : : value < std : : string > ( ) - > default_value ( " resources " ) )
( " fallback-archive " , boost : : program_options : : value < std : : vector < std : : string > > ( ) - >
2014-07-21 10:15:21 +00: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 15:14:58 +00:00
2013-03-07 02:00:59 +00:00
boost : : program_options : : notify ( variables ) ;
2012-11-23 13:05:49 +00:00
2015-06-16 03:09:44 +00:00
mCfgMgr . readConfiguration ( variables , desc , quiet ) ;
2012-11-23 13:05:49 +00:00
2014-05-12 08:32:57 +00:00
mDocumentManager . setEncoding (
ToUTF8 : : calculateEncoding ( variables [ " encoding " ] . as < std : : string > ( ) ) ) ;
2014-03-16 11:44:01 +00:00
mDocumentManager . setResourceDir ( mResources = variables [ " resources " ] . as < std : : string > ( ) ) ;
2014-02-25 10:58:32 +00:00
2014-07-21 10:15:21 +00:00
if ( variables [ " script-blacklist-use " ] . as < bool > ( ) )
mDocumentManager . setBlacklistedScripts (
variables [ " script-blacklist " ] . as < std : : vector < std : : string > > ( ) ) ;
2014-03-09 11:32:21 +00:00
mFsStrict = variables [ " fs-strict " ] . as < bool > ( ) ;
2013-09-08 12:31:20 +00:00
Files : : PathContainer dataDirs , dataLocal ;
2013-03-07 02:00:59 +00:00
if ( ! variables [ " data " ] . empty ( ) ) {
2013-09-08 12:31:20 +00:00
dataDirs = Files : : PathContainer ( variables [ " data " ] . as < Files : : PathContainer > ( ) ) ;
2013-03-07 02:00:59 +00:00
}
2013-02-04 12:46:54 +00:00
2013-03-07 02:00:59 +00:00
std : : string local = variables [ " data-local " ] . as < std : : string > ( ) ;
if ( ! local . empty ( ) ) {
2013-09-08 12:31:20 +00:00
dataLocal . push_back ( Files : : PathContainer : : value_type ( local ) ) ;
2013-03-07 02:00:59 +00:00
}
2012-11-26 11:29:22 +00:00
2013-09-08 12:31:20 +00: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 02:00:59 +00:00
2013-09-08 12:31:20 +00:00
dataDirs . insert ( dataDirs . end ( ) , dataLocal . begin ( ) , dataLocal . end ( ) ) ;
2013-03-07 02:00:59 +00:00
2014-04-23 03:19:53 +00: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 11:16:58 +00:00
QString path = QString : : fromUtf8 ( iter - > string ( ) . c_str ( ) ) ;
2014-04-23 03:19:53 +00:00
mFileDialog . addFiles ( path ) ;
}
2014-03-09 11:32:21 +00:00
return std : : make_pair ( dataDirs , variables [ " fallback-archive " ] . as < std : : vector < std : : string > > ( ) ) ;
2013-03-07 02:00:59 +00:00
}
2013-09-08 10:06:28 +00:00
void CS : : Editor : : createGame ( )
{
mStartup . hide ( ) ;
if ( mNewGame . isHidden ( ) )
mNewGame . show ( ) ;
mNewGame . raise ( ) ;
mNewGame . activateWindow ( ) ;
}
2015-05-29 22:37:58 +00:00
void CS : : Editor : : cancelCreateGame ( )
{
if ( ! mDocumentManager . isEmpty ( ) )
return ;
mNewGame . hide ( ) ;
if ( mStartup . isHidden ( ) )
mStartup . show ( ) ;
mStartup . raise ( ) ;
mStartup . activateWindow ( ) ;
}
2013-09-08 10:06:28 +00:00
void CS : : Editor : : createAddon ( )
2013-03-07 02:00:59 +00:00
{
mStartup . hide ( ) ;
2015-06-16 02:48:45 +00:00
mFileDialog . clearFiles ( ) ;
std : : pair < Files : : PathContainer , std : : vector < std : : string > > config = readConfig ( /*quiet*/ true ) ;
setupDataFiles ( config . first ) ;
2013-10-31 23:12:13 +00:00
mFileDialog . showDialog ( CSVDoc : : ContentAction_New ) ;
2013-02-02 15:14:58 +00:00
}
2015-05-29 22:37:58 +00:00
void CS : : Editor : : cancelFileDialog ( )
{
if ( ! mDocumentManager . isEmpty ( ) )
return ;
mFileDialog . hide ( ) ;
if ( mStartup . isHidden ( ) )
mStartup . show ( ) ;
mStartup . raise ( ) ;
mStartup . activateWindow ( ) ;
}
2013-02-02 15:14:58 +00:00
void CS : : Editor : : loadDocument ( )
{
2013-02-07 12:11:41 +00:00
mStartup . hide ( ) ;
2015-06-16 02:48:45 +00:00
mFileDialog . clearFiles ( ) ;
std : : pair < Files : : PathContainer , std : : vector < std : : string > > config = readConfig ( /*quiet*/ true ) ;
setupDataFiles ( config . first ) ;
2013-10-31 23:12:13 +00:00
mFileDialog . showDialog ( CSVDoc : : ContentAction_Edit ) ;
2013-02-05 21:06:36 +00:00
}
2013-02-02 15:14:58 +00:00
2013-10-27 03:55:44 +00:00
void CS : : Editor : : openFiles ( const boost : : filesystem : : path & savePath )
2013-02-05 21:06:36 +00:00
{
2013-03-07 02:00:59 +00:00
std : : vector < boost : : filesystem : : path > files ;
2013-10-27 03:55:44 +00:00
foreach ( const QString & path , mFileDialog . selectedFilePaths ( ) )
2014-03-21 10:56:48 +00:00
files . push_back ( path . toUtf8 ( ) . constData ( ) ) ;
2013-10-07 03:10:38 +00:00
2014-04-21 07:02:58 +00:00
mDocumentManager . addDocument ( files , savePath , false ) ;
2013-02-07 11:52:01 +00:00
2013-10-23 03:20:21 +00:00
mFileDialog . hide ( ) ;
2013-03-07 02:00:59 +00:00
}
2013-10-27 03:55:44 +00:00
void CS : : Editor : : createNewFile ( const boost : : filesystem : : path & savePath )
2013-03-07 02:00:59 +00:00
{
std : : vector < boost : : filesystem : : path > files ;
2013-10-02 02:29:45 +00:00
foreach ( const QString & path , mFileDialog . selectedFilePaths ( ) ) {
2014-03-21 10:56:48 +00:00
files . push_back ( path . toUtf8 ( ) . constData ( ) ) ;
2013-03-07 02:00:59 +00:00
}
2014-09-15 21:44:07 +00:00
files . push_back ( savePath ) ;
2013-03-07 02:00:59 +00:00
2014-04-21 07:02:58 +00:00
mDocumentManager . addDocument ( files , savePath , true ) ;
2013-02-07 11:52:01 +00:00
2013-10-23 03:20:21 +00:00
mFileDialog . hide ( ) ;
2012-11-22 12:30:02 +00:00
}
2013-09-10 14:45:01 +00:00
void CS : : Editor : : createNewGame ( const boost : : filesystem : : path & file )
2013-09-08 12:31:20 +00:00
{
std : : vector < boost : : filesystem : : path > files ;
2013-09-10 14:45:01 +00:00
files . push_back ( file ) ;
2013-09-08 12:31:20 +00:00
2014-04-21 07:02:58 +00:00
mDocumentManager . addDocument ( files , file , true ) ;
2013-09-08 12:31:20 +00:00
mNewGame . hide ( ) ;
}
2013-09-03 09:12:19 +00:00
void CS : : Editor : : showStartup ( )
{
2013-09-06 04:02:51 +00:00
if ( mStartup . isHidden ( ) )
mStartup . show ( ) ;
mStartup . raise ( ) ;
mStartup . activateWindow ( ) ;
2013-09-03 09:12:19 +00:00
}
2013-09-08 07:26:43 +00:00
void CS : : Editor : : showSettings ( )
{
if ( mSettings . isHidden ( ) )
mSettings . show ( ) ;
2015-01-02 03:50:35 +00:00
mSettings . move ( QCursor : : pos ( ) ) ;
2013-09-08 07:26:43 +00:00
mSettings . raise ( ) ;
mSettings . activateWindow ( ) ;
}
2013-09-03 06:44:21 +00:00
bool CS : : Editor : : makeIPCServer ( )
{
2014-12-04 14:09:42 +00:00
try
{
mPid = boost : : filesystem : : temp_directory_path ( ) ;
2015-02-03 12:18:03 +00:00
mPid / = " openmw-cs.pid " ;
2014-12-04 14:09:42 +00:00
bool pidExists = boost : : filesystem : : exists ( mPid ) ;
2014-12-04 20:50:03 +00:00
mPidFile . open ( mPid ) ;
2014-12-04 14:09:42 +00: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-04 20:50:03 +00:00
mPidFile < < GetCurrentProcessId ( ) < < std : : endl ;
2014-12-04 14:09:42 +00:00
# else
2014-12-04 20:50:03 +00:00
mPidFile < < getpid ( ) < < std : : endl ;
2014-12-04 14:09:42 +00: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 ;
2015-06-11 20:36:19 +00:00
if ( boost : : filesystem : : exists ( fullPath . toUtf8 ( ) . constData ( ) ) )
2014-12-04 14:09:42 +00:00
{
// TODO: compare pid of the current process with that in the file
std : : cout < < " Detected unclean shutdown. " < < std : : endl ;
// delete the stale file
2015-06-11 20:36:19 +00:00
if ( remove ( fullPath . toUtf8 ( ) . constData ( ) ) )
2014-12-04 14:09:42 +00:00
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 09:12:19 +00:00
2013-09-06 04:02:51 +00:00
if ( mServer - > listen ( mIpcServerName ) )
{
connect ( mServer , SIGNAL ( newConnection ( ) ) , this , SLOT ( showStartup ( ) ) ) ;
return true ;
}
2013-09-03 09:12:19 +00:00
2013-09-06 04:02:51 +00:00
mServer - > close ( ) ;
2014-12-04 14:09:42 +00:00
mServer = NULL ;
2013-09-06 04:02:51 +00:00
return false ;
2013-09-03 06:44:21 +00:00
}
2013-09-03 09:12:19 +00:00
void CS : : Editor : : connectToIPCServer ( )
{
2013-09-06 04:02:51 +00:00
mClientSocket = new QLocalSocket ( this ) ;
mClientSocket - > connectToServer ( mIpcServerName ) ;
mClientSocket - > close ( ) ;
2013-09-03 09:12:19 +00:00
}
2012-11-22 12:30:02 +00:00
int CS : : Editor : : run ( )
{
2013-09-08 12:31:20 +00:00
if ( mLocal . empty ( ) )
return 1 ;
2014-03-16 11:44:01 +00:00
mStartup . show ( ) ;
QApplication : : setQuitOnLastWindowClosed ( true ) ;
return QApplication : : exec ( ) ;
}
2014-04-21 07:02:58 +00:00
void CS : : Editor : : documentAdded ( CSMDoc : : Document * document )
{
mViewManager . addView ( document ) ;
2014-04-26 11:11:27 +00:00
}
void CS : : Editor : : lastDocumentDeleted ( )
{
2014-11-21 19:59:23 +00:00
QApplication : : quit ( ) ;
2014-05-03 12:36:06 +00:00
}