1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include "runner.hpp"
#include <utility>
#include <QCoreApplication>
#include <QDir>
#include <QProcess>
#include <QString>
#include <QStringList>
#include <QTemporaryFile>
#include <QTextStream>
#include <components/files/qtconversion.hpp>
#include "operationholder.hpp"
CSMDoc::Runner::Runner(std::filesystem::path projectPath)
: mRunning(false)
, mStartup(nullptr)
, mProjectPath(std::move(projectPath))
{
connect(&mProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &Runner::finished);
connect(&mProcess, &QProcess::readyReadStandardOutput, this, &Runner::readyReadStandardOutput);
mProcess.setProcessChannelMode(QProcess::MergedChannels);
mProfile.blank();
}
CSMDoc::Runner::~Runner()
{
if (mRunning)
{
disconnect(&mProcess, nullptr, this, nullptr);
mProcess.kill();
mProcess.waitForFinished();
}
}
void CSMDoc::Runner::start(bool delayed)
{
if (mStartup)
{
delete mStartup;
mStartup = nullptr;
}
if (!delayed)
{
mLog.clear();
QString path = "openmw";
#ifdef Q_OS_WIN
path.append(QLatin1String(".exe"));
#endif
QDir dir(QCoreApplication::applicationDirPath());
#ifdef Q_OS_MAC
// the CS and engine are in separate .app directories
dir.cdUp();
dir.cdUp();
dir.cdUp();
path.prepend("OpenMW.app/Contents/MacOS/");
#endif
path = dir.absoluteFilePath(path);
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();
QStringList arguments;
arguments << "--skip-menu";
if (mProfile.mFlags & ESM::DebugProfile::Flag_BypassNewGame)
arguments << "--new-game=0";
else
arguments << "--new-game=1";
arguments << ("--script-run=" + mStartup->fileName());
arguments << "--data=\"" + Files::pathToQString(mProjectPath.parent_path()) + "\"";
arguments << "--replace=content";
for (const auto& mContentFile : mContentFiles)
{
arguments << "--content=" + Files::pathToQString(mContentFile);
}
arguments << "--content=" + Files::pathToQString(mProjectPath.filename());
mProcess.start(path, arguments);
}
mRunning = true;
emit runStateChanged();
}
void CSMDoc::Runner::stop()
{
delete mStartup;
mStartup = nullptr;
if (mProcess.state() == QProcess::NotRunning)
{
mRunning = false;
emit runStateChanged();
}
else
mProcess.kill();
}
bool CSMDoc::Runner::isRunning() const
{
return mRunning;
}
void CSMDoc::Runner::configure(const ESM::DebugProfile& profile, const std::vector<std::filesystem::path>& contentFiles,
const std::string& startupInstruction)
{
mProfile = profile;
mContentFiles = contentFiles;
mStartupInstruction = startupInstruction;
}
void CSMDoc::Runner::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
mRunning = false;
emit runStateChanged();
}
QTextDocument* CSMDoc::Runner::getLog()
{
return &mLog;
}
void CSMDoc::Runner::readyReadStandardOutput()
{
mLog.setPlainText(mLog.toPlainText() + QString::fromUtf8(mProcess.readAllStandardOutput()));
}
CSMDoc::SaveWatcher::SaveWatcher(Runner* runner, OperationHolder* operation)
: QObject(runner)
, mRunner(runner)
{
connect(operation, &OperationHolder::done, this, &SaveWatcher::saveDone);
}
void CSMDoc::SaveWatcher::saveDone(int type, bool failed)
{
if (failed)
mRunner->stop();
else
mRunner->start();
deleteLater();
}
|