2014-09-02 08:21:17 +00:00
|
|
|
#include "runner.hpp"
|
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2022-10-09 10:39:43 +00:00
|
|
|
#if defined(Q_OS_MAC)
|
2022-06-20 14:26:52 +00:00
|
|
|
#include <QCoreApplication>
|
2014-09-20 09:33:28 +00:00
|
|
|
#include <QDir>
|
2022-10-09 10:39:43 +00:00
|
|
|
#endif
|
|
|
|
|
2022-10-19 17:02:00 +00:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
2014-09-05 09:40:01 +00:00
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
2022-08-19 22:33:51 +00:00
|
|
|
#include <components/files/qtconversion.hpp>
|
|
|
|
|
2015-03-14 11:00:24 +00:00
|
|
|
#include "operationholder.hpp"
|
2014-09-02 08:21:17 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
CSMDoc::Runner::Runner(std::filesystem::path projectPath)
|
|
|
|
: mRunning(false)
|
|
|
|
, mStartup(nullptr)
|
|
|
|
, mProjectPath(std::move(projectPath))
|
2014-09-02 08:21:17 +00:00
|
|
|
{
|
2022-08-23 02:28:58 +00:00
|
|
|
connect(&mProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &Runner::finished);
|
2014-09-05 09:03:16 +00:00
|
|
|
|
2022-08-23 02:28:58 +00:00
|
|
|
connect(&mProcess, &QProcess::readyReadStandardOutput, this, &Runner::readyReadStandardOutput);
|
2014-09-05 11:49:34 +00:00
|
|
|
|
|
|
|
mProcess.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
|
2014-09-05 09:03:16 +00:00
|
|
|
mProfile.blank();
|
2014-09-02 08:21:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 18:06:49 +00:00
|
|
|
CSMDoc::Runner::~Runner()
|
|
|
|
{
|
|
|
|
if (mRunning)
|
|
|
|
{
|
2020-11-13 07:39:47 +00:00
|
|
|
disconnect(&mProcess, nullptr, this, nullptr);
|
2014-09-03 18:06:49 +00:00
|
|
|
mProcess.kill();
|
|
|
|
mProcess.waitForFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 17:56:52 +00:00
|
|
|
void CSMDoc::Runner::start(bool delayed)
|
2014-09-02 08:21:17 +00:00
|
|
|
{
|
2014-09-05 09:40:01 +00:00
|
|
|
if (mStartup)
|
|
|
|
{
|
|
|
|
delete mStartup;
|
2020-11-13 07:39:47 +00:00
|
|
|
mStartup = nullptr;
|
2014-09-05 09:40:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 17:56:52 +00:00
|
|
|
if (!delayed)
|
|
|
|
{
|
2014-09-05 11:49:34 +00:00
|
|
|
mLog.clear();
|
|
|
|
|
2014-09-03 17:56:52 +00:00
|
|
|
QString path = "openmw";
|
2014-09-02 08:21:17 +00:00
|
|
|
#ifdef Q_OS_WIN
|
2014-09-03 17:56:52 +00:00
|
|
|
path.append(QString(".exe"));
|
2014-09-02 08:21:17 +00:00
|
|
|
#elif defined(Q_OS_MAC)
|
2014-09-03 17:56:52 +00:00
|
|
|
QDir dir(QCoreApplication::applicationDirPath());
|
2014-09-20 09:33:28 +00:00
|
|
|
dir.cdUp();
|
|
|
|
dir.cdUp();
|
|
|
|
dir.cdUp();
|
|
|
|
path = dir.absoluteFilePath(path.prepend("OpenMW.app/Contents/MacOS/"));
|
2014-09-02 08:21:17 +00:00
|
|
|
#else
|
2014-09-03 17:56:52 +00:00
|
|
|
path.prepend(QString("./"));
|
2014-09-02 08:21:17 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-05 09:40:01 +00: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 09:03:16 +00:00
|
|
|
QStringList arguments;
|
|
|
|
arguments << "--skip-menu";
|
|
|
|
|
|
|
|
if (mProfile.mFlags & ESM::DebugProfile::Flag_BypassNewGame)
|
|
|
|
arguments << "--new-game=0";
|
|
|
|
else
|
|
|
|
arguments << "--new-game=1";
|
|
|
|
|
2022-08-19 22:33:51 +00:00
|
|
|
arguments << ("--script-run=" + mStartup->fileName());
|
2014-09-13 18:48:24 +00:00
|
|
|
|
2022-08-19 22:33:51 +00:00
|
|
|
arguments << "--data=\"" + Files::pathToQString(mProjectPath.parent_path()) + "\"";
|
2014-09-05 09:40:01 +00:00
|
|
|
|
2021-08-01 01:47:10 +00:00
|
|
|
arguments << "--replace=content";
|
2022-11-05 15:30:44 +00:00
|
|
|
arguments << "--content=builtin.omwscripts";
|
2021-08-01 01:47:10 +00:00
|
|
|
|
2022-08-19 22:33:51 +00:00
|
|
|
for (const auto& mContentFile : mContentFiles)
|
2014-09-06 10:24:09 +00:00
|
|
|
{
|
2022-08-19 22:33:51 +00:00
|
|
|
arguments << "--content=" + Files::pathToQString(mContentFile);
|
2014-09-06 10:24:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:33:51 +00:00
|
|
|
arguments << "--content=" + Files::pathToQString(mProjectPath.filename());
|
2014-09-13 18:48:24 +00:00
|
|
|
|
2022-08-19 22:33:51 +00:00
|
|
|
mProcess.start(path, arguments);
|
2014-09-03 17:56:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 09:56:35 +00:00
|
|
|
mRunning = true;
|
|
|
|
emit runStateChanged();
|
2014-09-02 08:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSMDoc::Runner::stop()
|
|
|
|
{
|
2014-09-05 09:40:01 +00:00
|
|
|
delete mStartup;
|
2020-11-13 07:39:47 +00:00
|
|
|
mStartup = nullptr;
|
2014-09-05 09:40:01 +00:00
|
|
|
|
2014-09-03 17:56:52 +00:00
|
|
|
if (mProcess.state() == QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
mRunning = false;
|
|
|
|
emit runStateChanged();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mProcess.kill();
|
2014-09-02 08:21:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 09:56:35 +00:00
|
|
|
bool CSMDoc::Runner::isRunning() const
|
|
|
|
{
|
|
|
|
return mRunning;
|
|
|
|
}
|
|
|
|
|
2014-09-05 09:40:01 +00:00
|
|
|
void CSMDoc::Runner::configure(const ESM::DebugProfile& profile, const std::vector<std::filesystem::path>& contentFiles,
|
2022-07-02 22:02:29 +00:00
|
|
|
const std::string& startupInstruction)
|
2014-09-05 09:03:16 +00:00
|
|
|
{
|
|
|
|
mProfile = profile;
|
2014-09-06 10:24:09 +00:00
|
|
|
mContentFiles = contentFiles;
|
2014-09-05 09:40:01 +00:00
|
|
|
mStartupInstruction = startupInstruction;
|
2014-09-05 09:03:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 08:21:17 +00:00
|
|
|
void CSMDoc::Runner::finished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
|
|
{
|
2014-09-02 09:56:35 +00:00
|
|
|
mRunning = false;
|
|
|
|
emit runStateChanged();
|
2014-09-02 08:21:17 +00:00
|
|
|
}
|
2014-09-03 17:56:52 +00:00
|
|
|
|
2014-09-05 11:49:34 +00:00
|
|
|
QTextDocument* CSMDoc::Runner::getLog()
|
|
|
|
{
|
|
|
|
return &mLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMDoc::Runner::readyReadStandardOutput()
|
|
|
|
{
|
|
|
|
mLog.setPlainText(mLog.toPlainText() + QString::fromUtf8(mProcess.readAllStandardOutput()));
|
|
|
|
}
|
|
|
|
|
2015-03-14 11:00:24 +00:00
|
|
|
CSMDoc::SaveWatcher::SaveWatcher(Runner* runner, OperationHolder* operation)
|
2014-09-03 17:56:52 +00:00
|
|
|
: QObject(runner)
|
|
|
|
, mRunner(runner)
|
|
|
|
{
|
2022-08-23 02:28:58 +00:00
|
|
|
connect(operation, &OperationHolder::done, this, &SaveWatcher::saveDone);
|
2014-09-03 17:56:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSMDoc::SaveWatcher::saveDone(int type, bool failed)
|
|
|
|
{
|
|
|
|
if (failed)
|
|
|
|
mRunner->stop();
|
|
|
|
else
|
|
|
|
mRunner->start();
|
|
|
|
|
|
|
|
deleteLater();
|
|
|
|
}
|