2022-03-27 12:13:32 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-08-04 18:41:47 +01:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-03-27 12:13:32 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2015-05-11 21:21:37 +01:00
|
|
|
*
|
2022-03-27 12:13:32 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2015-05-11 21:21:37 +01:00
|
|
|
*
|
2022-03-27 12:13:32 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2015-05-11 21:21:37 +01:00
|
|
|
*
|
2022-03-27 12:13:32 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2015-05-11 21:21:37 +01:00
|
|
|
*
|
2022-03-27 12:13:32 +01:00
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 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-05-11 21:21:37 +01:00
|
|
|
*/
|
|
|
|
|
2015-07-10 00:11:06 +01:00
|
|
|
#include "launch/LaunchTask.h"
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <QCoreApplication>
|
2015-05-11 21:21:37 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QEventLoop>
|
2015-06-16 08:19:55 +01:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QStandardPaths>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include "MessageLevel.h"
|
|
|
|
#include "java/JavaChecker.h"
|
|
|
|
#include "tasks/Task.h"
|
2015-07-04 19:02:43 +01:00
|
|
|
|
2015-07-10 00:11:06 +01:00
|
|
|
void LaunchTask::init()
|
2015-07-04 19:02:43 +01:00
|
|
|
{
|
|
|
|
m_instance->setRunning(true);
|
|
|
|
}
|
|
|
|
|
2019-04-07 22:59:04 +01:00
|
|
|
shared_qobject_ptr<LaunchTask> LaunchTask::create(InstancePtr inst)
|
2015-06-16 08:19:55 +01:00
|
|
|
{
|
2019-04-07 22:59:04 +01:00
|
|
|
shared_qobject_ptr<LaunchTask> proc(new LaunchTask(inst));
|
2015-06-16 08:19:55 +01:00
|
|
|
proc->init();
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
LaunchTask::LaunchTask(InstancePtr instance) : m_instance(instance) {}
|
2015-05-11 21:21:37 +01:00
|
|
|
|
2019-04-07 22:59:04 +01:00
|
|
|
void LaunchTask::appendStep(shared_qobject_ptr<LaunchStep> step)
|
2015-06-16 08:19:55 +01:00
|
|
|
{
|
2015-07-21 01:38:15 +01:00
|
|
|
m_steps.append(step);
|
|
|
|
}
|
2015-06-16 08:19:55 +01:00
|
|
|
|
2019-04-07 22:59:04 +01:00
|
|
|
void LaunchTask::prependStep(shared_qobject_ptr<LaunchStep> step)
|
2015-07-21 01:38:15 +01:00
|
|
|
{
|
|
|
|
m_steps.prepend(step);
|
|
|
|
}
|
2015-06-16 08:19:55 +01:00
|
|
|
|
2015-07-21 01:38:15 +01:00
|
|
|
void LaunchTask::executeTask()
|
|
|
|
{
|
2016-11-04 00:17:28 +00:00
|
|
|
m_instance->setCrashed(false);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_steps.size()) {
|
2015-07-21 01:38:15 +01:00
|
|
|
state = LaunchTask::Finished;
|
|
|
|
emitSucceeded();
|
2015-06-16 08:19:55 +01:00
|
|
|
}
|
2015-07-21 01:38:15 +01:00
|
|
|
state = LaunchTask::Running;
|
|
|
|
onStepFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LaunchTask::onReadyForLaunch()
|
|
|
|
{
|
|
|
|
state = LaunchTask::Waiting;
|
|
|
|
emit readyForLaunch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LaunchTask::onStepFinished()
|
|
|
|
{
|
|
|
|
// initial -> just start the first step
|
2023-08-02 17:35:35 +01:00
|
|
|
if (currentStep == -1) {
|
|
|
|
currentStep++;
|
2015-07-21 01:38:15 +01:00
|
|
|
m_steps[currentStep]->start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto step = m_steps[currentStep];
|
2023-08-02 17:35:35 +01:00
|
|
|
if (step->wasSuccessful()) {
|
2015-07-21 01:38:15 +01:00
|
|
|
// end?
|
2023-08-02 17:35:35 +01:00
|
|
|
if (currentStep == m_steps.size() - 1) {
|
2016-11-15 01:51:22 +00:00
|
|
|
finalizeSteps(true, QString());
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
|
|
|
currentStep++;
|
2015-07-21 01:38:15 +01:00
|
|
|
step = m_steps[currentStep];
|
|
|
|
step->start();
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2016-11-15 01:51:22 +00:00
|
|
|
finalizeSteps(false, step->failReason());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LaunchTask::finalizeSteps(bool successful, const QString& error)
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto step = currentStep; step >= 0; step--) {
|
2016-11-15 01:51:22 +00:00
|
|
|
m_steps[step]->finalize();
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (successful) {
|
2016-11-15 01:51:22 +00:00
|
|
|
emitSucceeded();
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2016-11-15 01:51:22 +00:00
|
|
|
emitFailed(error);
|
2015-07-21 01:38:15 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-16 08:19:55 +01:00
|
|
|
|
2015-07-26 16:55:29 +01:00
|
|
|
void LaunchTask::onProgressReportingRequested()
|
|
|
|
{
|
|
|
|
state = LaunchTask::Waiting;
|
|
|
|
emit requestProgress(m_steps[currentStep].get());
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:38:15 +01:00
|
|
|
void LaunchTask::setCensorFilter(QMap<QString, QString> filter)
|
|
|
|
{
|
|
|
|
m_censorFilter = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LaunchTask::censorPrivateInfo(QString in)
|
|
|
|
{
|
|
|
|
auto iter = m_censorFilter.begin();
|
2023-08-02 17:35:35 +01:00
|
|
|
while (iter != m_censorFilter.end()) {
|
2015-07-21 01:38:15 +01:00
|
|
|
in.replace(iter.key(), iter.value());
|
|
|
|
iter++;
|
|
|
|
}
|
2015-06-16 08:19:55 +01:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:38:15 +01:00
|
|
|
void LaunchTask::proceed()
|
2015-06-16 08:19:55 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (state != LaunchTask::Waiting) {
|
2015-07-21 01:38:15 +01:00
|
|
|
return;
|
2015-06-16 08:19:55 +01:00
|
|
|
}
|
2015-07-21 01:38:15 +01:00
|
|
|
m_steps[currentStep]->proceed();
|
2015-06-16 08:19:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-26 17:06:08 +00:00
|
|
|
bool LaunchTask::canAbort() const
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (state) {
|
2016-11-26 17:06:08 +00:00
|
|
|
case LaunchTask::Aborted:
|
|
|
|
case LaunchTask::Failed:
|
|
|
|
case LaunchTask::Finished:
|
|
|
|
return false;
|
|
|
|
case LaunchTask::NotStarted:
|
|
|
|
return true;
|
|
|
|
case LaunchTask::Running:
|
2023-08-02 17:35:35 +01:00
|
|
|
case LaunchTask::Waiting: {
|
2016-11-26 17:06:08 +00:00
|
|
|
auto step = m_steps[currentStep];
|
|
|
|
return step->canAbort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:38:15 +01:00
|
|
|
bool LaunchTask::abort()
|
2015-06-16 08:19:55 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (state) {
|
2015-07-21 01:38:15 +01:00
|
|
|
case LaunchTask::Aborted:
|
|
|
|
case LaunchTask::Failed:
|
|
|
|
case LaunchTask::Finished:
|
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
case LaunchTask::NotStarted: {
|
2015-07-21 01:38:15 +01:00
|
|
|
state = LaunchTask::Aborted;
|
|
|
|
emitFailed("Aborted");
|
|
|
|
return true;
|
2015-06-16 08:19:55 +01:00
|
|
|
}
|
2015-07-21 01:38:15 +01:00
|
|
|
case LaunchTask::Running:
|
2023-08-02 17:35:35 +01:00
|
|
|
case LaunchTask::Waiting: {
|
2015-07-21 01:38:15 +01:00
|
|
|
auto step = m_steps[currentStep];
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!step->canAbort()) {
|
2015-07-21 01:38:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (step->abort()) {
|
2015-07-21 01:38:15 +01:00
|
|
|
state = LaunchTask::Aborted;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2015-06-16 08:19:55 +01:00
|
|
|
}
|
2015-07-21 01:38:15 +01:00
|
|
|
return false;
|
2015-07-04 19:02:43 +01:00
|
|
|
}
|
2015-06-16 08:19:55 +01:00
|
|
|
|
2016-08-18 20:31:37 +01:00
|
|
|
shared_qobject_ptr<LogModel> LaunchTask::getLogModel()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_logModel) {
|
2016-08-18 20:31:37 +01:00
|
|
|
m_logModel.reset(new LogModel());
|
2017-02-08 19:01:42 +00:00
|
|
|
m_logModel->setMaxLines(m_instance->getConsoleMaxLines());
|
|
|
|
m_logModel->setStopOnOverflow(m_instance->shouldStopOnConsoleOverflow());
|
|
|
|
// FIXME: should this really be here?
|
2022-03-26 21:21:08 +00:00
|
|
|
m_logModel->setOverflowMessage(tr("Stopped watching the game log because the log length surpassed %1 lines.\n"
|
2023-08-02 17:35:35 +01:00
|
|
|
"You may have to fix your mods because the game is still logging to files and"
|
|
|
|
" likely wasting harddrive space at an alarming rate!")
|
|
|
|
.arg(m_logModel->getMaxLines()));
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
return m_logModel;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void LaunchTask::onLogLines(const QStringList& lines, MessageLevel::Enum defaultLevel)
|
2015-05-11 21:21:37 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto& line : lines) {
|
2015-07-21 01:38:15 +01:00
|
|
|
onLogLine(line, defaultLevel);
|
2015-05-11 21:21:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:38:15 +01:00
|
|
|
void LaunchTask::onLogLine(QString line, MessageLevel::Enum level)
|
2015-05-11 21:21:37 +01:00
|
|
|
{
|
|
|
|
// if the launcher part set a log level, use it
|
|
|
|
auto innerLevel = MessageLevel::fromLine(line);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (innerLevel != MessageLevel::Unknown) {
|
2015-05-11 21:21:37 +01:00
|
|
|
level = innerLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the level is still undetermined, guess level
|
2023-08-02 17:35:35 +01:00
|
|
|
if (level == MessageLevel::StdErr || level == MessageLevel::StdOut || level == MessageLevel::Unknown) {
|
2015-07-22 08:01:04 +01:00
|
|
|
level = m_instance->guessLevel(line, level);
|
2015-05-11 21:21:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// censor private user info
|
|
|
|
line = censorPrivateInfo(line);
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
auto& model = *getLogModel();
|
2016-08-18 20:31:37 +01:00
|
|
|
model.append(level, line);
|
2015-05-11 21:21:37 +01:00
|
|
|
}
|
|
|
|
|
2015-07-10 00:11:06 +01:00
|
|
|
void LaunchTask::emitSucceeded()
|
2015-07-05 00:54:30 +01:00
|
|
|
{
|
|
|
|
m_instance->setRunning(false);
|
|
|
|
Task::emitSucceeded();
|
|
|
|
}
|
|
|
|
|
2015-07-10 00:11:06 +01:00
|
|
|
void LaunchTask::emitFailed(QString reason)
|
2015-07-05 00:54:30 +01:00
|
|
|
{
|
|
|
|
m_instance->setRunning(false);
|
2016-11-04 00:17:28 +00:00
|
|
|
m_instance->setCrashed(true);
|
2015-07-05 00:54:30 +01:00
|
|
|
Task::emitFailed(reason);
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void LaunchTask::substituteVariables(QStringList& args) const
|
2022-05-02 18:10:45 +01:00
|
|
|
{
|
2022-07-23 16:51:58 +01:00
|
|
|
auto env = m_instance->createEnvironment();
|
2022-05-02 18:10:45 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto key : env.keys()) {
|
2022-07-23 16:51:58 +01:00
|
|
|
args.replaceInStrings("$" + key, env.value(key));
|
2022-05-02 18:10:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void LaunchTask::substituteVariables(QString& cmd) const
|
2015-05-11 21:21:37 +01:00
|
|
|
{
|
2022-07-23 16:51:58 +01:00
|
|
|
auto env = m_instance->createEnvironment();
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto key : env.keys()) {
|
2022-07-23 16:51:58 +01:00
|
|
|
cmd.replace("$" + key, env.value(key));
|
2015-05-11 21:21:37 +01:00
|
|
|
}
|
|
|
|
}
|