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
|
// 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) ==============================
*
*
*
*
*
* ======================================================================== */
#include "FlameAPI.h"
#include <memory>
#include <optional>
#include "BuildConfig.h"
#include "FlameModIndex.h"
#include "Application.h"
#include "Json.h"
#include "modplatform/ModIndex.h"
#include "net/ApiDownload.h"
#include "net/ApiUpload.h"
#include "net/NetJob.h"
Task::Ptr FlameAPI::matchFingerprints(const QList<uint>& fingerprints, std::shared_ptr<QByteArray> response)
{
auto netJob = makeShared<NetJob>(QString("Flame::MatchFingerprints"), APPLICATION->network());
QJsonObject body_obj;
QJsonArray fingerprints_arr;
for (auto& fp : fingerprints)
{
fingerprints_arr.append(QString("%1").arg(fp));
}
body_obj["fingerprints"] = fingerprints_arr;
QJsonDocument body(body_obj);
auto body_raw = body.toJson();
netJob->addNetAction(
Net::ApiUpload::makeByteArray(QString(BuildConfig.FLAME_BASE_URL + "/fingerprints"), response, body_raw));
return netJob;
}
QString FlameAPI::getModFileChangelog(int modId, int fileId)
{
QEventLoop lock;
QString changelog;
auto netJob = makeShared<NetJob>(QString("Flame::FileChangelog"), APPLICATION->network());
auto response = std::make_shared<QByteArray>();
netJob->addNetAction(Net::ApiDownload::makeByteArray(
QString(BuildConfig.FLAME_BASE_URL + "/mods/%1/files/%2/changelog")
.arg(QString::fromStdString(std::to_string(modId)), QString::fromStdString(std::to_string(fileId))),
response));
QObject::connect(netJob.get(),
&NetJob::succeeded,
[&netJob, response, &changelog]
{
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response from Flame::FileChangelog at "
<< parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *response;
netJob->failed(parse_error.errorString());
return;
}
changelog = Json::ensureString(doc.object(), "data");
});
QObject::connect(netJob.get(), &NetJob::finished, [&lock] { lock.quit(); });
netJob->start();
lock.exec();
return changelog;
}
QString FlameAPI::getModDescription(int modId)
{
QEventLoop lock;
QString description;
auto netJob = makeShared<NetJob>(QString("Flame::ModDescription"), APPLICATION->network());
auto response = std::make_shared<QByteArray>();
netJob->addNetAction(Net::ApiDownload::makeByteArray(
QString(BuildConfig.FLAME_BASE_URL + "/mods/%1/description").arg(QString::number(modId)),
response));
QObject::connect(netJob.get(),
&NetJob::succeeded,
[&netJob, response, &description]
{
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response from Flame::ModDescription at "
<< parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *response;
netJob->failed(parse_error.errorString());
return;
}
description = Json::ensureString(doc.object(), "data");
});
QObject::connect(netJob.get(), &NetJob::finished, [&lock] { lock.quit(); });
netJob->start();
lock.exec();
return description;
}
Task::Ptr FlameAPI::getProjects(QStringList addonIds, std::shared_ptr<QByteArray> response) const
{
auto netJob = makeShared<NetJob>(QString("Flame::GetProjects"), APPLICATION->network());
QJsonObject body_obj;
QJsonArray addons_arr;
for (auto& addonId : addonIds)
{
addons_arr.append(addonId);
}
body_obj["modIds"] = addons_arr;
QJsonDocument body(body_obj);
auto body_raw = body.toJson();
netJob->addNetAction(
Net::ApiUpload::makeByteArray(QString(BuildConfig.FLAME_BASE_URL + "/mods"), response, body_raw));
QObject::connect(netJob.get(), &NetJob::failed, [body_raw] { qDebug() << body_raw; });
return netJob;
}
Task::Ptr FlameAPI::getFiles(const QStringList& fileIds, std::shared_ptr<QByteArray> response) const
{
auto netJob = makeShared<NetJob>(QString("Flame::GetFiles"), APPLICATION->network());
QJsonObject body_obj;
QJsonArray files_arr;
for (auto& fileId : fileIds)
{
files_arr.append(fileId);
}
body_obj["fileIds"] = files_arr;
QJsonDocument body(body_obj);
auto body_raw = body.toJson();
netJob->addNetAction(
Net::ApiUpload::makeByteArray(QString(BuildConfig.FLAME_BASE_URL + "/mods/files"), response, body_raw));
QObject::connect(netJob.get(), &NetJob::failed, [body_raw] { qDebug() << body_raw; });
return netJob;
}
Task::Ptr FlameAPI::getFile(const QString& addonId, const QString& fileId, std::shared_ptr<QByteArray> response) const
{
auto netJob = makeShared<NetJob>(QString("Flame::GetFile"), APPLICATION->network());
netJob->addNetAction(Net::ApiDownload::makeByteArray(
QUrl(QString(BuildConfig.FLAME_BASE_URL + "/mods/%1/files/%2").arg(addonId, fileId)),
response));
QObject::connect(netJob.get(),
&NetJob::failed,
[addonId, fileId] { qDebug() << "Flame API file failure" << addonId << fileId; });
return netJob;
}
QList<ResourceAPI::SortingMethod> FlameAPI::getSortingMethods() const
{
// https://docs.curseforge.com/?python#tocS_ModsSearchSortField
return { { 1, "Featured", QObject::tr("Sort by Featured") },
{ 2, "Popularity", QObject::tr("Sort by Popularity") },
{ 3, "LastUpdated", QObject::tr("Sort by Last Updated") },
{ 4, "Name", QObject::tr("Sort by Name") },
{ 5, "Author", QObject::tr("Sort by Author") },
{ 6, "TotalDownloads", QObject::tr("Sort by Downloads") },
{ 7, "Category", QObject::tr("Sort by Category") },
{ 8, "GameVersion", QObject::tr("Sort by Game Version") } };
}
Task::Ptr FlameAPI::getCategories(std::shared_ptr<QByteArray> response, ModPlatform::ResourceType type)
{
auto netJob = makeShared<NetJob>(QString("Flame::GetCategories"), APPLICATION->network());
netJob->addNetAction(Net::ApiDownload::makeByteArray(
QUrl(QString(BuildConfig.FLAME_BASE_URL + "/categories?gameId=432&classId=%1").arg(getClassId(type))),
response));
QObject::connect(netJob.get(),
&Task::failed,
[](QString msg) { qDebug() << "Flame failed to get categories:" << msg; });
return netJob;
}
Task::Ptr FlameAPI::getModCategories(std::shared_ptr<QByteArray> response)
{
return getCategories(response, ModPlatform::ResourceType::Mod);
}
QList<ModPlatform::Category> FlameAPI::loadModCategories(std::shared_ptr<QByteArray> response)
{
QList<ModPlatform::Category> categories;
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response from categories at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;
return categories;
}
try
{
auto obj = Json::requireObject(doc);
auto arr = Json::requireArray(obj, "data");
for (auto val : arr)
{
auto cat = Json::requireObject(val);
auto id = Json::requireInteger(cat, "id");
auto name = Json::requireString(cat, "name");
categories.push_back({ name, QString::number(id) });
}
}
catch (Json::JsonException& e)
{
qCritical() << "Failed to parse response from a version request.";
qCritical() << e.what();
qDebug() << doc;
}
return categories;
};
std::optional<ModPlatform::IndexedVersion> FlameAPI::getLatestVersion(QList<ModPlatform::IndexedVersion> versions,
QList<ModPlatform::ModLoaderType> instanceLoaders,
ModPlatform::ModLoaderTypes modLoaders,
bool checkLoaders)
{
static const auto noLoader = ModPlatform::ModLoaderType(0);
if (!checkLoaders)
{
std::optional<ModPlatform::IndexedVersion> ver;
for (auto file_tmp : versions)
{
if (!ver.has_value() || file_tmp.date > ver->date)
{
ver = file_tmp;
}
}
return ver;
}
QHash<ModPlatform::ModLoaderType, ModPlatform::IndexedVersion> bestMatch;
auto checkVersion =
[&bestMatch](const ModPlatform::IndexedVersion& version, const ModPlatform::ModLoaderType& loader)
{
if (bestMatch.contains(loader))
{
auto best = bestMatch.value(loader);
if (version.date > best.date)
{
bestMatch[loader] = version;
}
}
else
{
bestMatch[loader] = version;
}
};
for (auto file_tmp : versions)
{
auto loaders = ModPlatform::modLoaderTypesToList(file_tmp.loaders);
if (loaders.isEmpty())
{
checkVersion(file_tmp, noLoader);
}
else
{
for (auto loader : loaders)
{
checkVersion(file_tmp, loader);
}
}
}
// edge case: mod has installed for forge but the instance is fabric => fabric version will be prioritizated on
// update
auto currentLoaders = instanceLoaders + ModPlatform::modLoaderTypesToList(modLoaders);
currentLoaders.append(noLoader); // add a fallback in case the versions do not define a loader
for (auto loader : currentLoaders)
{
if (bestMatch.contains(loader))
{
auto bestForLoader = bestMatch.value(loader);
// awkward case where the mod has only two loaders and one of them is not specified
if (loader != noLoader && bestMatch.contains(noLoader) && bestMatch.size() == 2)
{
auto bestForNoLoader = bestMatch.value(noLoader);
if (bestForNoLoader.date > bestForLoader.date)
{
return bestForNoLoader;
}
}
return bestForLoader;
}
}
return {};
}
|