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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
// 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.
*
* === Upstream License Block (Do Not Modify) ==============================
*
*
*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* 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 "FileResolvingTask.h"
#include <algorithm>
#include "Application.h"
#include "Json.h"
#include "modplatform/ModIndex.h"
#include "modplatform/flame/FlameAPI.h"
#include "modplatform/flame/FlameModIndex.h"
#include "modplatform/modrinth/ModrinthAPI.h"
#include "modplatform/modrinth/ModrinthPackIndex.h"
#include "net/NetJob.h"
#include "tasks/Task.h"
static const FlameAPI flameAPI;
static ModrinthAPI modrinthAPI;
Flame::FileResolvingTask::FileResolvingTask(Flame::Manifest& toProcess) : m_manifest(toProcess)
{}
bool Flame::FileResolvingTask::abort()
{
bool aborted = true;
if (m_task)
{
aborted = m_task->abort();
}
return aborted ? Task::abort() : false;
}
void Flame::FileResolvingTask::executeTask()
{
if (m_manifest.files.isEmpty())
{ // no file to resolve so leave it empty and emit success immediately
emitSucceeded();
return;
}
setStatus(tr("Resolving mod IDs..."));
setProgress(0, 3);
m_result.reset(new QByteArray());
QStringList fileIds;
for (auto file : m_manifest.files)
{
fileIds.push_back(QString::number(file.fileId));
}
m_task = flameAPI.getFiles(fileIds, m_result);
auto step_progress = std::make_shared<TaskStepProgress>();
connect(m_task.get(),
&Task::succeeded,
this,
[this, step_progress]()
{
step_progress->state = TaskStepState::Succeeded;
stepProgress(*step_progress);
netJobFinished();
});
connect(m_task.get(),
&Task::failed,
this,
[this, step_progress](QString reason)
{
step_progress->state = TaskStepState::Failed;
stepProgress(*step_progress);
emitFailed(reason);
});
connect(m_task.get(), &Task::stepProgress, this, &FileResolvingTask::propagateStepProgress);
connect(m_task.get(),
&Task::progress,
this,
[this, step_progress](qint64 current, qint64 total)
{
qDebug() << "Resolve slug progress" << current << total;
step_progress->update(current, total);
stepProgress(*step_progress);
});
connect(m_task.get(),
&Task::status,
this,
[this, step_progress](QString status)
{
step_progress->status = status;
stepProgress(*step_progress);
});
m_task->start();
}
ModPlatform::ResourceType getResourceType(int classId)
{
switch (classId)
{
case 17: // Worlds
return ModPlatform::ResourceType::World;
case 6: // Mods
return ModPlatform::ResourceType::Mod;
case 12: // Resource Packs
// return ModPlatform::ResourceType::ResourcePack; // not really a resourcepack
/* fallthrough */
case 4546: // Customization
// return ModPlatform::ResourceType::ShaderPack; // not really a shaderPack
/* fallthrough */
case 4471: // Modpacks
/* fallthrough */
case 5: // Bukkit Plugins
/* fallthrough */
case 4559: // Addons
/* fallthrough */
default: return ModPlatform::ResourceType::Unknown;
}
}
void Flame::FileResolvingTask::netJobFinished()
{
setProgress(1, 3);
// job to check modrinth for blocked projects
QJsonDocument doc;
QJsonArray array;
try
{
doc = Json::requireDocument(*m_result);
array = Json::requireArray(doc.object()["data"]);
}
catch (Json::JsonException& e)
{
qCritical() << "Non-JSON data returned from the CF API";
qCritical() << e.cause();
emitFailed(tr("Invalid data returned from the API."));
return;
}
QStringList hashes;
for (QJsonValueRef file : array)
{
try
{
auto obj = Json::requireObject(file);
auto version = FlameMod::loadIndexedPackVersion(obj);
auto fileid = version.fileId.toInt();
Q_ASSERT(fileid != 0);
Q_ASSERT(m_manifest.files.contains(fileid));
m_manifest.files[fileid].version = version;
auto url = QUrl(version.downloadUrl, QUrl::TolerantMode);
if (!url.isValid() && "sha1" == version.hash_type && !version.hash.isEmpty())
{
hashes.push_back(version.hash);
}
}
catch (Json::JsonException& e)
{
qCritical() << "Non-JSON data returned from the CF API";
qCritical() << e.cause();
emitFailed(tr("Invalid data returned from the API."));
return;
}
}
if (hashes.isEmpty() || !APPLICATION->settings()->get("FallbackMRBlockedMods").toBool())
{
getFlameProjects();
return;
}
m_result.reset(new QByteArray());
m_task = modrinthAPI.currentVersions(hashes, "sha1", m_result);
(dynamic_cast<NetJob*>(m_task.get()))->setAskRetry(false);
auto step_progress = std::make_shared<TaskStepProgress>();
connect(m_task.get(),
&Task::succeeded,
this,
[this, step_progress]()
{
step_progress->state = TaskStepState::Succeeded;
stepProgress(*step_progress);
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*m_result, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response from Modrinth::CurrentVersions at "
<< parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *m_result;
getFlameProjects();
return;
}
try
{
auto entries = Json::requireObject(doc);
for (auto& out : m_manifest.files)
{
auto url = QUrl(out.version.downloadUrl, QUrl::TolerantMode);
if (!url.isValid() && "sha1" == out.version.hash_type && !out.version.hash.isEmpty())
{
try
{
auto entry = Json::requireObject(entries, out.version.hash);
auto file = Modrinth::loadIndexedPackVersion(entry);
out.version.downloadUrl = file.downloadUrl;
qDebug() << "Found alternative on modrinth " << out.version.fileName;
}
catch (Json::JsonException& e)
{
qDebug() << e.cause();
qDebug() << entries;
}
}
}
}
catch (Json::JsonException& e)
{
qDebug() << e.cause();
qDebug() << doc;
}
getFlameProjects();
});
connect(m_task.get(),
&Task::failed,
this,
[this, step_progress](QString reason)
{
step_progress->state = TaskStepState::Failed;
stepProgress(*step_progress);
getFlameProjects();
});
connect(m_task.get(), &Task::stepProgress, this, &FileResolvingTask::propagateStepProgress);
connect(m_task.get(),
&Task::progress,
this,
[this, step_progress](qint64 current, qint64 total)
{
qDebug() << "Resolve slug progress" << current << total;
step_progress->update(current, total);
stepProgress(*step_progress);
});
connect(m_task.get(),
&Task::status,
this,
[this, step_progress](QString status)
{
step_progress->status = status;
stepProgress(*step_progress);
});
m_task->start();
}
void Flame::FileResolvingTask::getFlameProjects()
{
setProgress(2, 3);
m_result.reset(new QByteArray());
QStringList addonIds;
for (auto file : m_manifest.files)
{
addonIds.push_back(QString::number(file.projectId));
}
m_task = flameAPI.getProjects(addonIds, m_result);
auto step_progress = std::make_shared<TaskStepProgress>();
connect(m_task.get(),
&Task::succeeded,
this,
[this, step_progress]
{
QJsonParseError parse_error{};
auto doc = QJsonDocument::fromJson(*m_result, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response from Modrinth projects task at "
<< parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *m_result;
return;
}
try
{
QJsonArray entries;
entries = Json::requireArray(Json::requireObject(doc), "data");
for (auto entry : entries)
{
auto entry_obj = Json::requireObject(entry);
auto id = Json::requireInteger(entry_obj, "id");
auto file = std::find_if(m_manifest.files.begin(),
m_manifest.files.end(),
[id](const Flame::File& file) { return file.projectId == id; });
if (file == m_manifest.files.end())
{
continue;
}
setStatus(tr("Parsing API response from CurseForge for '%1'...").arg(file->version.fileName));
FlameMod::loadIndexedPack(file->pack, entry_obj);
file->resourceType = getResourceType(Json::requireInteger(entry_obj, "classId", "modClassId"));
if (file->resourceType == ModPlatform::ResourceType::World)
{
file->targetFolder = "saves";
}
}
}
catch (Json::JsonException& e)
{
qDebug() << e.cause();
qDebug() << doc;
}
step_progress->state = TaskStepState::Succeeded;
stepProgress(*step_progress);
emitSucceeded();
});
connect(m_task.get(),
&Task::failed,
this,
[this, step_progress](QString reason)
{
step_progress->state = TaskStepState::Failed;
stepProgress(*step_progress);
emitFailed(reason);
});
connect(m_task.get(), &Task::stepProgress, this, &FileResolvingTask::propagateStepProgress);
connect(m_task.get(),
&Task::progress,
this,
[this, step_progress](qint64 current, qint64 total)
{
qDebug() << "Resolve slug progress" << current << total;
step_progress->update(current, total);
stepProgress(*step_progress);
});
connect(m_task.get(),
&Task::status,
this,
[this, step_progress](QString status)
{
step_progress->status = status;
stepProgress(*step_progress);
});
m_task->start();
}
|