PrismLauncher/launcher/minecraft/MinecraftUpdate.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

188 lines
5.2 KiB
C++
Raw Normal View History

2021-01-18 07:28:54 +00:00
/* Copyright 2013-2021 MultiMC Contributors
2013-02-22 00:10:17 +00:00
*
* 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
*
2013-02-22 00:10:17 +00:00
* 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.
*/
#include "MinecraftUpdate.h"
#include "MinecraftInstance.h"
2013-02-22 00:10:17 +00:00
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QDataStream>
2015-02-09 00:51:14 +00:00
#include "BaseInstance.h"
#include "minecraft/PackProfile.h"
#include "minecraft/Library.h"
2015-10-05 00:47:27 +01:00
#include <FileSystem.h>
#include "update/FoldersTask.h"
#include "update/LibrariesTask.h"
#include "update/FMLLibrariesTask.h"
#include "update/AssetUpdateTask.h"
#include <meta/Index.h>
#include <meta/Version.h>
MinecraftUpdate::MinecraftUpdate(MinecraftInstance *inst, QObject *parent) : Task(parent), m_inst(inst)
{
}
void MinecraftUpdate::executeTask()
{
m_tasks.clear();
// create folders
{
m_tasks.append(makeShared<FoldersTask>(m_inst));
}
2018-07-15 13:51:05 +01:00
// add metadata update task if necessary
2014-02-21 18:15:59 +00:00
{
auto components = m_inst->getPackProfile();
components->reload(Net::Mode::Online);
auto task = components->getCurrentTask();
if(task)
{
m_tasks.append(task);
}
2014-02-21 18:15:59 +00:00
}
2018-07-15 13:51:05 +01:00
// libraries download
{
m_tasks.append(makeShared<LibrariesTask>(m_inst));
}
2018-07-15 13:51:05 +01:00
// FML libraries download and copy into the instance
2013-12-10 06:12:52 +00:00
{
m_tasks.append(makeShared<FMLLibrariesTask>(m_inst));
2013-12-10 06:12:52 +00:00
}
2018-07-15 13:51:05 +01:00
// assets update
2013-12-10 06:12:52 +00:00
{
m_tasks.append(makeShared<AssetUpdateTask>(m_inst));
2013-12-10 06:12:52 +00:00
}
2018-07-15 13:51:05 +01:00
if(!m_preFailure.isEmpty())
{
emitFailed(m_preFailure);
return;
2013-08-05 02:29:50 +01:00
}
next();
}
void MinecraftUpdate::next()
{
if(m_abort)
{
emitFailed(tr("Aborted by user."));
return;
}
if(m_failed_out_of_order)
{
emitFailed(m_fail_reason);
return;
}
m_currentTask ++;
if(m_currentTask > 0)
2013-08-05 02:29:50 +01:00
{
auto task = m_tasks[m_currentTask - 1];
disconnect(task.get(), &Task::succeeded, this, &MinecraftUpdate::subtaskSucceeded);
disconnect(task.get(), &Task::failed, this, &MinecraftUpdate::subtaskFailed);
disconnect(task.get(), &Task::aborted, this, &Task::abort);
disconnect(task.get(), &Task::progress, this, &MinecraftUpdate::progress);
disconnect(task.get(), &Task::stepProgress, this, &MinecraftUpdate::propagateStepProgress);
disconnect(task.get(), &Task::status, this, &MinecraftUpdate::setStatus);
disconnect(task.get(), &Task::details, this, &MinecraftUpdate::setDetails);
2013-08-05 02:29:50 +01:00
}
if(m_currentTask == m_tasks.size())
{
emitSucceeded();
return;
}
auto task = m_tasks[m_currentTask];
// if the task is already finished by the time we look at it, skip it
if(task->isFinished())
{
qCritical() << "MinecraftUpdate: Skipping finished subtask" << m_currentTask << ":" << task.get();
next();
}
connect(task.get(), &Task::succeeded, this, &MinecraftUpdate::subtaskSucceeded);
connect(task.get(), &Task::failed, this, &MinecraftUpdate::subtaskFailed);
connect(task.get(), &Task::aborted, this, &Task::abort);
connect(task.get(), &Task::progress, this, &MinecraftUpdate::progress);
connect(task.get(), &Task::stepProgress, this, &MinecraftUpdate::propagateStepProgress);
connect(task.get(), &Task::status, this, &MinecraftUpdate::setStatus);
connect(task.get(), &Task::details, this, &MinecraftUpdate::setDetails);
// if the task is already running, do not start it again
if(!task->isRunning())
{
task->start();
}
}
void MinecraftUpdate::subtaskSucceeded()
{
if(isFinished())
{
qCritical() << "MinecraftUpdate: Subtask" << sender() << "succeeded, but work was already done!";
return;
}
auto senderTask = QObject::sender();
auto currentTask = m_tasks[m_currentTask].get();
if(senderTask != currentTask)
{
qDebug() << "MinecraftUpdate: Subtask" << sender() << "succeeded out of order.";
return;
}
next();
}
void MinecraftUpdate::subtaskFailed(QString error)
{
if(isFinished())
{
qCritical() << "MinecraftUpdate: Subtask" << sender() << "failed, but work was already done!";
return;
}
auto senderTask = QObject::sender();
auto currentTask = m_tasks[m_currentTask].get();
if(senderTask != currentTask)
{
qDebug() << "MinecraftUpdate: Subtask" << sender() << "failed out of order.";
m_failed_out_of_order = true;
m_fail_reason = error;
return;
}
emitFailed(error);
}
2014-05-04 23:10:59 +01:00
bool MinecraftUpdate::abort()
2014-05-04 23:10:59 +01:00
{
if(!m_abort)
2014-05-04 23:10:59 +01:00
{
m_abort = true;
auto task = m_tasks[m_currentTask];
if(task->canAbort())
2014-05-04 23:10:59 +01:00
{
return task->abort();
2014-05-04 23:10:59 +01:00
}
}
return true;
2014-05-04 23:10:59 +01:00
}
bool MinecraftUpdate::canAbort() const
2014-05-04 23:10:59 +01:00
{
return true;
}