2015-05-12 01:51:37 +05:30
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Authors: Orochimarufan <orochimarufan.x3@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
#include "launch/LaunchTask.h"
|
2015-05-12 01:51:37 +05:30
|
|
|
#include "MessageLevel.h"
|
2015-06-16 12:49:55 +05:30
|
|
|
#include "MMCStrings.h"
|
|
|
|
#include "java/JavaChecker.h"
|
2015-07-04 23:32:43 +05:30
|
|
|
#include "tasks/Task.h"
|
2015-05-12 01:51:37 +05:30
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QEventLoop>
|
2015-06-16 12:49:55 +05:30
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QStandardPaths>
|
2015-07-21 06:08:15 +05:30
|
|
|
#include <assert.h>
|
2015-07-04 23:32:43 +05:30
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
void LaunchTask::init()
|
2015-07-04 23:32:43 +05:30
|
|
|
{
|
|
|
|
m_instance->setRunning(true);
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
std::shared_ptr<LaunchTask> LaunchTask::create(InstancePtr inst)
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-10 04:41:06 +05:30
|
|
|
std::shared_ptr<LaunchTask> proc(new LaunchTask(inst));
|
2015-06-16 12:49:55 +05:30
|
|
|
proc->init();
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
LaunchTask::LaunchTask(InstancePtr instance): m_instance(instance)
|
2015-05-12 01:51:37 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
void LaunchTask::appendStep(std::shared_ptr<LaunchStep> step)
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
m_steps.append(step);
|
|
|
|
}
|
2015-06-16 12:49:55 +05:30
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
void LaunchTask::prependStep(std::shared_ptr<LaunchStep> step)
|
|
|
|
{
|
|
|
|
m_steps.prepend(step);
|
|
|
|
}
|
2015-06-16 12:49:55 +05:30
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
void LaunchTask::executeTask()
|
|
|
|
{
|
2016-11-04 05:47:28 +05:30
|
|
|
m_instance->setCrashed(false);
|
2015-07-21 06:08:15 +05:30
|
|
|
if(!m_steps.size())
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
state = LaunchTask::Finished;
|
|
|
|
emitSucceeded();
|
2015-06-16 12:49:55 +05:30
|
|
|
}
|
2015-07-21 06:08:15 +05:30
|
|
|
state = LaunchTask::Running;
|
|
|
|
onStepFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LaunchTask::onReadyForLaunch()
|
|
|
|
{
|
|
|
|
state = LaunchTask::Waiting;
|
|
|
|
emit readyForLaunch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LaunchTask::onStepFinished()
|
|
|
|
{
|
|
|
|
// initial -> just start the first step
|
|
|
|
if(currentStep == -1)
|
|
|
|
{
|
|
|
|
currentStep ++;
|
|
|
|
m_steps[currentStep]->start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto step = m_steps[currentStep];
|
|
|
|
if(step->successful())
|
|
|
|
{
|
|
|
|
// end?
|
|
|
|
if(currentStep == m_steps.size() - 1)
|
|
|
|
{
|
2016-11-15 07:21:22 +05:30
|
|
|
finalizeSteps(true, QString());
|
2015-07-21 06:08:15 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentStep ++;
|
|
|
|
step = m_steps[currentStep];
|
|
|
|
step->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-15 07:21:22 +05:30
|
|
|
finalizeSteps(false, step->failReason());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LaunchTask::finalizeSteps(bool successful, const QString& error)
|
|
|
|
{
|
|
|
|
for(auto step = currentStep; step >= 0; step--)
|
|
|
|
{
|
|
|
|
m_steps[step]->finalize();
|
|
|
|
}
|
|
|
|
if(successful)
|
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emitFailed(error);
|
2015-07-21 06:08:15 +05:30
|
|
|
}
|
|
|
|
}
|
2015-06-16 12:49:55 +05:30
|
|
|
|
2015-07-26 21:25:29 +05:30
|
|
|
void LaunchTask::onProgressReportingRequested()
|
|
|
|
{
|
|
|
|
state = LaunchTask::Waiting;
|
|
|
|
emit requestProgress(m_steps[currentStep].get());
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
void LaunchTask::setCensorFilter(QMap<QString, QString> filter)
|
|
|
|
{
|
|
|
|
m_censorFilter = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LaunchTask::censorPrivateInfo(QString in)
|
|
|
|
{
|
|
|
|
auto iter = m_censorFilter.begin();
|
|
|
|
while (iter != m_censorFilter.end())
|
|
|
|
{
|
|
|
|
in.replace(iter.key(), iter.value());
|
|
|
|
iter++;
|
|
|
|
}
|
2015-06-16 12:49:55 +05:30
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
void LaunchTask::proceed()
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
if(state != LaunchTask::Waiting)
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
return;
|
2015-06-16 12:49:55 +05:30
|
|
|
}
|
2015-07-21 06:08:15 +05:30
|
|
|
m_steps[currentStep]->proceed();
|
2015-06-16 12:49:55 +05:30
|
|
|
}
|
|
|
|
|
2016-11-26 22:36:08 +05:30
|
|
|
bool LaunchTask::canAbort() const
|
|
|
|
{
|
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case LaunchTask::Aborted:
|
|
|
|
case LaunchTask::Failed:
|
|
|
|
case LaunchTask::Finished:
|
|
|
|
return false;
|
|
|
|
case LaunchTask::NotStarted:
|
|
|
|
return true;
|
|
|
|
case LaunchTask::Running:
|
|
|
|
case LaunchTask::Waiting:
|
|
|
|
{
|
|
|
|
auto step = m_steps[currentStep];
|
|
|
|
return step->canAbort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
bool LaunchTask::abort()
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
switch(state)
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
case LaunchTask::Aborted:
|
|
|
|
case LaunchTask::Failed:
|
|
|
|
case LaunchTask::Finished:
|
|
|
|
return true;
|
|
|
|
case LaunchTask::NotStarted:
|
2015-06-16 12:49:55 +05:30
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
state = LaunchTask::Aborted;
|
|
|
|
emitFailed("Aborted");
|
|
|
|
return true;
|
2015-06-16 12:49:55 +05:30
|
|
|
}
|
2015-07-21 06:08:15 +05:30
|
|
|
case LaunchTask::Running:
|
|
|
|
case LaunchTask::Waiting:
|
|
|
|
{
|
|
|
|
auto step = m_steps[currentStep];
|
|
|
|
if(!step->canAbort())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(step->abort())
|
|
|
|
{
|
|
|
|
state = LaunchTask::Aborted;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2015-06-16 12:49:55 +05:30
|
|
|
}
|
2015-07-21 06:08:15 +05:30
|
|
|
return false;
|
2015-07-04 23:32:43 +05:30
|
|
|
}
|
2015-06-16 12:49:55 +05:30
|
|
|
|
2016-08-19 01:01:37 +05:30
|
|
|
shared_qobject_ptr<LogModel> LaunchTask::getLogModel()
|
|
|
|
{
|
|
|
|
if(!m_logModel)
|
|
|
|
{
|
|
|
|
m_logModel.reset(new LogModel());
|
|
|
|
}
|
|
|
|
return m_logModel;
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
void LaunchTask::onLogLines(const QStringList &lines, MessageLevel::Enum defaultLevel)
|
2015-05-12 01:51:37 +05:30
|
|
|
{
|
|
|
|
for (auto & line: lines)
|
|
|
|
{
|
2015-07-21 06:08:15 +05:30
|
|
|
onLogLine(line, defaultLevel);
|
2015-05-12 01:51:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:08:15 +05:30
|
|
|
void LaunchTask::onLogLine(QString line, MessageLevel::Enum level)
|
2015-05-12 01:51:37 +05:30
|
|
|
{
|
|
|
|
// if the launcher part set a log level, use it
|
|
|
|
auto innerLevel = MessageLevel::fromLine(line);
|
|
|
|
if(innerLevel != MessageLevel::Unknown)
|
|
|
|
{
|
|
|
|
level = innerLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the level is still undetermined, guess level
|
|
|
|
if (level == MessageLevel::StdErr || level == MessageLevel::StdOut || level == MessageLevel::Unknown)
|
|
|
|
{
|
2015-07-22 12:31:04 +05:30
|
|
|
level = m_instance->guessLevel(line, level);
|
2015-05-12 01:51:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// censor private user info
|
|
|
|
line = censorPrivateInfo(line);
|
|
|
|
|
2016-08-19 01:01:37 +05:30
|
|
|
auto &model = *getLogModel();
|
|
|
|
model.append(level, line);
|
2015-05-12 01:51:37 +05:30
|
|
|
}
|
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
void LaunchTask::emitSucceeded()
|
2015-07-05 05:24:30 +05:30
|
|
|
{
|
|
|
|
m_instance->setRunning(false);
|
|
|
|
Task::emitSucceeded();
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
void LaunchTask::emitFailed(QString reason)
|
2015-07-05 05:24:30 +05:30
|
|
|
{
|
|
|
|
m_instance->setRunning(false);
|
2016-11-04 05:47:28 +05:30
|
|
|
m_instance->setCrashed(true);
|
2015-07-05 05:24:30 +05:30
|
|
|
Task::emitFailed(reason);
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
QString LaunchTask::substituteVariables(const QString &cmd) const
|
2015-05-12 01:51:37 +05:30
|
|
|
{
|
|
|
|
QString out = cmd;
|
2015-07-21 06:08:15 +05:30
|
|
|
auto variables = m_instance->getVariables();
|
2015-05-12 01:51:37 +05:30
|
|
|
for (auto it = variables.begin(); it != variables.end(); ++it)
|
|
|
|
{
|
|
|
|
out.replace("$" + it.key(), it.value());
|
|
|
|
}
|
|
|
|
auto env = QProcessEnvironment::systemEnvironment();
|
|
|
|
for (auto var : env.keys())
|
|
|
|
{
|
|
|
|
out.replace("$" + var, env.value(var));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|