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
|
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
* ProjT Launcher - Minecraft Launcher
* Copyright (C) 2026 Project Tick
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "InstanceCreationTask.h"
#include <QDebug>
#include <QFile>
#include "FileSystem.h"
#include "minecraft/MinecraftLoadAndCheck.h"
#include "tasks/SequentialTask.h"
bool InstanceCreationTask::abort()
{
if (!canAbort())
return false;
m_abort = true;
if (m_gameFilesTask)
return m_gameFilesTask->abort();
return true;
}
void InstanceCreationTask::executeTask()
{
setAbortable(true);
if (updateInstance())
{
emitSucceeded();
return;
}
// When the user aborted in the update stage.
if (m_abort)
{
emitAborted();
return;
}
m_createdInstance = createInstance();
if (!m_createdInstance)
{
if (m_abort)
return;
qWarning() << "Instance creation failed!";
if (!m_error_message.isEmpty())
{
qWarning() << "Reason: " << m_error_message;
emitFailed(tr("Error while creating new instance:\n%1").arg(m_error_message));
}
else
{
emitFailed(tr("Error while creating new instance."));
}
return;
}
// If this is set, it means we're updating an instance. So, we now need to remove the
// files scheduled to, and we'd better not let the user abort in the middle of it, since it'd
// put the instance in an invalid state.
if (shouldOverride())
{
bool deleteFailed = false;
setAbortable(false);
setStatus(tr("Removing old conflicting files..."));
qDebug() << "Removing old files";
for (const QString& path : m_files_to_remove)
{
if (!QFile::exists(path))
continue;
qDebug() << "Removing" << path;
if (!QFile::remove(path))
{
qCritical() << "Could not remove" << path;
deleteFailed = true;
}
}
if (deleteFailed)
{
emitFailed(tr("Failed to remove old conflicting files."));
return;
}
}
if (m_abort)
return;
m_createdInstance->saveNow();
setAbortable(true);
setAbortButtonText(tr("Skip"));
qDebug() << "Downloading game files";
auto updateTasks = m_createdInstance->createUpdateTask();
if (updateTasks.isEmpty())
{
emitSucceeded();
return;
}
auto task = makeShared<SequentialTask>();
task->addTask(makeShared<MinecraftLoadAndCheck>(m_createdInstance.get(), Net::Mode::Online));
for (const auto& updateTask : updateTasks)
{
task->addTask(updateTask);
}
connect(task.get(),
&Task::finished,
this,
[this, task]
{
if (task->wasSuccessful() || m_abort)
{
emitSucceeded();
}
else
{
emitFailed(tr("Could not download game files: %1").arg(task->failReason()));
}
});
connect(task.get(), &Task::progress, this, &InstanceCreationTask::setProgress);
connect(task.get(), &Task::stepProgress, this, &InstanceCreationTask::propagateStepProgress);
connect(task.get(), &Task::status, this, &InstanceCreationTask::setStatus);
connect(task.get(), &Task::details, this, &InstanceCreationTask::setDetails);
setDetails(tr("Downloading game files"));
m_gameFilesTask = task;
m_gameFilesTask->start();
}
|