summaryrefslogtreecommitdiffstats
path: root/api/logic/minecraft/MinecraftUpdate.cpp
blob: e62ff745c407a4672e3c78a4275e2e44184fa538 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Copyright 2013-2018 MultiMC Contributors
 *
 * 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.
 */

#include "Env.h"
#include <minecraft/forge/ForgeXzDownload.h>
#include "MinecraftUpdate.h"
#include "MinecraftInstance.h"

#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QDataStream>

#include "BaseInstance.h"
#include "minecraft/ComponentList.h"
#include "minecraft/Library.h"
#include "net/URLConstants.h"
#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>

OneSixUpdate::OneSixUpdate(MinecraftInstance *inst, QObject *parent) : Task(parent), m_inst(inst)
{
}

void OneSixUpdate::executeTask()
{
    m_tasks.clear();
    // create folders
    {
        m_tasks.append(std::make_shared<FoldersTask>(m_inst));
    }

    // add metadata update task if necessary
    {
        auto components = m_inst->getComponentList();
        components->reload(Net::Mode::Online);
        auto task = components->getCurrentTask();
        if(task)
        {
            m_tasks.append(task.unwrap());
        }
    }

    // libraries download
    {
        m_tasks.append(std::make_shared<LibrariesTask>(m_inst));
    }

    // FML libraries download and copy into the instance
    {
        m_tasks.append(std::make_shared<FMLLibrariesTask>(m_inst));
    }

    // assets update
    {
        m_tasks.append(std::make_shared<AssetUpdateTask>(m_inst));
    }

    if(!m_preFailure.isEmpty())
    {
        emitFailed(m_preFailure);
        return;
    }
    next();
}

void OneSixUpdate::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)
    {
        auto task = m_tasks[m_currentTask - 1];
        disconnect(task.get(), &Task::succeeded, this, &OneSixUpdate::subtaskSucceeded);
        disconnect(task.get(), &Task::failed, this, &OneSixUpdate::subtaskFailed);
        disconnect(task.get(), &Task::progress, this, &OneSixUpdate::progress);
        disconnect(task.get(), &Task::status, this, &OneSixUpdate::setStatus);
    }
    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() << "OneSixUpdate: Skipping finished subtask" << m_currentTask << ":" << task.get();
        next();
    }
    connect(task.get(), &Task::succeeded, this, &OneSixUpdate::subtaskSucceeded);
    connect(task.get(), &Task::failed, this, &OneSixUpdate::subtaskFailed);
    connect(task.get(), &Task::progress, this, &OneSixUpdate::progress);
    connect(task.get(), &Task::status, this, &OneSixUpdate::setStatus);
    // if the task is already running, do not start it again
    if(!task->isRunning())
    {
        task->start();
    }
}

void OneSixUpdate::subtaskSucceeded()
{
    if(isFinished())
    {
        qCritical() << "OneSixUpdate: Subtask" << sender() << "succeeded, but work was already done!";
        return;
    }
    auto senderTask = QObject::sender();
    auto currentTask = m_tasks[m_currentTask].get();
    if(senderTask != currentTask)
    {
        qDebug() << "OneSixUpdate: Subtask" << sender() << "succeeded out of order.";
        return;
    }
    next();
}

void OneSixUpdate::subtaskFailed(QString error)
{
    if(isFinished())
    {
        qCritical() << "OneSixUpdate: Subtask" << sender() << "failed, but work was already done!";
        return;
    }
    auto senderTask = QObject::sender();
    auto currentTask = m_tasks[m_currentTask].get();
    if(senderTask != currentTask)
    {
        qDebug() << "OneSixUpdate: Subtask" << sender() << "failed out of order.";
        m_failed_out_of_order = true;
        m_fail_reason = error;
        return;
    }
    emitFailed(error);
}


bool OneSixUpdate::abort()
{
    if(!m_abort)
    {
        m_abort = true;
        auto task = m_tasks[m_currentTask];
        if(task->canAbort())
        {
            return task->abort();
        }
    }
    return true;
}

bool OneSixUpdate::canAbort() const
{
    return true;
}