2014-09-02 10:21:17 +02:00
# include "runner.hpp"
2022-06-08 23:25:50 +02:00
# include <utility>
2014-09-20 13:33:28 +04:00
# include <QDir>
2014-09-05 11:40:01 +02:00
# include <QTemporaryFile>
# include <QTextStream>
2022-06-20 16:26:52 +02:00
# include <QCoreApplication>
2014-09-05 11:40:01 +02:00
2015-03-14 12:00:24 +01:00
# include "operationholder.hpp"
2014-09-02 10:21:17 +02:00
2022-06-08 23:25:50 +02:00
CSMDoc : : Runner : : Runner ( std : : filesystem : : path projectPath )
: mRunning ( false ) , mStartup ( nullptr ) , mProjectPath ( std : : move ( projectPath ) )
2014-09-02 10:21:17 +02:00
{
2022-08-22 23:28:58 -03:00
connect ( & mProcess , qOverload < int , QProcess : : ExitStatus > ( & QProcess : : finished ) ,
this , & Runner : : finished ) ;
2014-09-05 11:03:16 +02:00
2022-08-22 23:28:58 -03:00
connect ( & mProcess , & QProcess : : readyReadStandardOutput ,
this , & Runner : : readyReadStandardOutput ) ;
2014-09-05 13:49:34 +02:00
mProcess . setProcessChannelMode ( QProcess : : MergedChannels ) ;
2014-09-05 11:03:16 +02:00
mProfile . blank ( ) ;
2014-09-02 10:21:17 +02:00
}
2014-09-03 20:06:49 +02:00
CSMDoc : : Runner : : ~ Runner ( )
{
if ( mRunning )
{
2020-11-13 11:39:47 +04:00
disconnect ( & mProcess , nullptr , this , nullptr ) ;
2014-09-03 20:06:49 +02:00
mProcess . kill ( ) ;
mProcess . waitForFinished ( ) ;
}
}
2014-09-03 19:56:52 +02:00
void CSMDoc : : Runner : : start ( bool delayed )
2014-09-02 10:21:17 +02:00
{
2014-09-05 11:40:01 +02:00
if ( mStartup )
{
delete mStartup ;
2020-11-13 11:39:47 +04:00
mStartup = nullptr ;
2014-09-05 11:40:01 +02:00
}
2014-09-03 19:56:52 +02:00
if ( ! delayed )
{
2014-09-05 13:49:34 +02:00
mLog . clear ( ) ;
2014-09-03 19:56:52 +02:00
QString path = " openmw " ;
2014-09-02 10:21:17 +02:00
# ifdef Q_OS_WIN
2014-09-03 19:56:52 +02:00
path . append ( QString ( " .exe " ) ) ;
2014-09-02 10:21:17 +02:00
# elif defined(Q_OS_MAC)
2014-09-03 19:56:52 +02:00
QDir dir ( QCoreApplication : : applicationDirPath ( ) ) ;
2014-09-20 13:33:28 +04:00
dir . cdUp ( ) ;
dir . cdUp ( ) ;
dir . cdUp ( ) ;
path = dir . absoluteFilePath ( path . prepend ( " OpenMW.app/Contents/MacOS/ " ) ) ;
2014-09-02 10:21:17 +02:00
# else
2014-09-03 19:56:52 +02:00
path . prepend ( QString ( " ./ " ) ) ;
2014-09-02 10:21:17 +02:00
# endif
2014-09-05 11:40:01 +02:00
mStartup = new QTemporaryFile ( this ) ;
mStartup - > open ( ) ;
{
QTextStream stream ( mStartup ) ;
if ( ! mStartupInstruction . empty ( ) )
stream < < QString : : fromUtf8 ( mStartupInstruction . c_str ( ) ) < < ' \n ' ;
stream < < QString : : fromUtf8 ( mProfile . mScriptText . c_str ( ) ) ;
}
mStartup - > close ( ) ;
2014-09-05 11:03:16 +02:00
QStringList arguments ;
arguments < < " --skip-menu " ;
if ( mProfile . mFlags & ESM : : DebugProfile : : Flag_BypassNewGame )
arguments < < " --new-game=0 " ;
else
arguments < < " --new-game=1 " ;
2021-07-05 13:11:54 +02:00
arguments < < ( " --script-run= " + mStartup - > fileName ( ) ) ;
2014-09-13 20:48:24 +02:00
arguments < <
2022-06-19 13:28:33 +02:00
QString : : fromUtf8 ( ( " --data= \" " + mProjectPath . parent_path ( ) . string ( ) + " \" " ) . c_str ( ) ) ; //TODO(Project579): This will probably break in windows with unicode paths
2014-09-05 11:40:01 +02:00
2021-08-01 02:47:10 +01:00
arguments < < " --replace=content " ;
2014-09-06 12:24:09 +02:00
for ( std : : vector < std : : string > : : const_iterator iter ( mContentFiles . begin ( ) ) ;
iter ! = mContentFiles . end ( ) ; + + iter )
{
arguments < < QString : : fromUtf8 ( ( " --content= " + * iter ) . c_str ( ) ) ;
}
2014-09-13 20:48:24 +02:00
arguments
2022-06-19 13:28:33 +02:00
< < QString : : fromUtf8 ( ( " --content= " + mProjectPath . filename ( ) . string ( ) ) . c_str ( ) ) ; //TODO(Project579): let's hope unicode characters are never used in these filenames on windows or this will break
2014-09-13 20:48:24 +02:00
2014-09-05 11:03:16 +02:00
mProcess . start ( path , arguments ) ;
2014-09-03 19:56:52 +02:00
}
2014-09-02 11:56:35 +02:00
mRunning = true ;
emit runStateChanged ( ) ;
2014-09-02 10:21:17 +02:00
}
void CSMDoc : : Runner : : stop ( )
{
2014-09-05 11:40:01 +02:00
delete mStartup ;
2020-11-13 11:39:47 +04:00
mStartup = nullptr ;
2014-09-05 11:40:01 +02:00
2014-09-03 19:56:52 +02:00
if ( mProcess . state ( ) = = QProcess : : NotRunning )
{
mRunning = false ;
emit runStateChanged ( ) ;
}
else
mProcess . kill ( ) ;
2014-09-02 10:21:17 +02:00
}
2014-09-02 11:56:35 +02:00
bool CSMDoc : : Runner : : isRunning ( ) const
{
return mRunning ;
}
2014-09-05 11:40:01 +02:00
void CSMDoc : : Runner : : configure ( const ESM : : DebugProfile & profile ,
2014-09-06 12:24:09 +02:00
const std : : vector < std : : string > & contentFiles , const std : : string & startupInstruction )
2014-09-05 11:03:16 +02:00
{
mProfile = profile ;
2014-09-06 12:24:09 +02:00
mContentFiles = contentFiles ;
2014-09-05 11:40:01 +02:00
mStartupInstruction = startupInstruction ;
2014-09-05 11:03:16 +02:00
}
2014-09-02 10:21:17 +02:00
void CSMDoc : : Runner : : finished ( int exitCode , QProcess : : ExitStatus exitStatus )
{
2014-09-02 11:56:35 +02:00
mRunning = false ;
emit runStateChanged ( ) ;
2014-09-02 10:21:17 +02:00
}
2014-09-03 19:56:52 +02:00
2014-09-05 13:49:34 +02:00
QTextDocument * CSMDoc : : Runner : : getLog ( )
{
return & mLog ;
}
void CSMDoc : : Runner : : readyReadStandardOutput ( )
{
mLog . setPlainText (
mLog . toPlainText ( ) + QString : : fromUtf8 ( mProcess . readAllStandardOutput ( ) ) ) ;
}
2014-09-03 19:56:52 +02:00
2015-03-14 12:00:24 +01:00
CSMDoc : : SaveWatcher : : SaveWatcher ( Runner * runner , OperationHolder * operation )
2014-09-03 19:56:52 +02:00
: QObject ( runner ) , mRunner ( runner )
{
2022-08-22 23:28:58 -03:00
connect ( operation , & OperationHolder : : done , this , & SaveWatcher : : saveDone ) ;
2014-09-03 19:56:52 +02:00
}
void CSMDoc : : SaveWatcher : : saveDone ( int type , bool failed )
{
if ( failed )
mRunner - > stop ( ) ;
else
mRunner - > start ( ) ;
deleteLater ( ) ;
}