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
390
391
392
393
|
// 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 "modplatform/ResourceAPI.h"
#include "Application.h"
#include "Json.h"
#include "net/NetJob.h"
#include "modplatform/ModIndex.h"
#include "net/ApiDownload.h"
Task::Ptr ResourceAPI::searchProjects(SearchArgs&& args,
Callback<QList<ModPlatform::IndexedPack::Ptr>>&& callbacks) const
{
auto search_url_optional = getSearchURL(args);
if (!search_url_optional.has_value())
{
callbacks.on_fail("Failed to create search URL", -1);
return nullptr;
}
auto search_url = search_url_optional.value();
auto response = std::make_shared<QByteArray>();
auto netJob = makeShared<NetJob>(QString("%1::Search").arg(debugName()), APPLICATION->network());
netJob->addNetAction(Net::ApiDownload::makeByteArray(QUrl(search_url), response));
QObject::connect(netJob.get(),
&NetJob::succeeded,
[this, response, callbacks]
{
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response from " << debugName() << " at "
<< parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *response;
callbacks.on_fail(parse_error.errorString(), -1);
return;
}
QList<ModPlatform::IndexedPack::Ptr> newList;
auto packs = documentToArray(doc);
for (auto packRaw : packs)
{
auto packObj = packRaw.toObject();
ModPlatform::IndexedPack::Ptr pack = std::make_shared<ModPlatform::IndexedPack>();
try
{
loadIndexedPack(*pack, packObj);
newList << pack;
}
catch (const JSONValidationError& e)
{
qWarning() << "Error while loading resource from " << debugName() << ": " << e.cause();
continue;
}
}
callbacks.on_succeed(newList);
});
// Capture a weak_ptr instead of a shared_ptr to avoid circular dependency issues.
// This prevents the lambda from extending the lifetime of the shared resource,
// as it only temporarily locks the resource when needed.
auto weak = netJob.toWeakRef();
QObject::connect(netJob.get(),
&NetJob::failed,
[weak, callbacks](const QString& reason)
{
int network_error_code = -1;
if (auto netJob = weak.lock())
{
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action)
network_error_code = failed_action->replyStatusCode();
}
callbacks.on_fail(reason, network_error_code);
});
QObject::connect(netJob.get(),
&NetJob::aborted,
[callbacks]
{
if (callbacks.on_abort != nullptr)
callbacks.on_abort();
});
return netJob;
}
Task::Ptr ResourceAPI::getProjectVersions(VersionSearchArgs&& args,
Callback<QVector<ModPlatform::IndexedVersion>>&& callbacks) const
{
auto versions_url_optional = getVersionsURL(args);
if (!versions_url_optional.has_value())
return nullptr;
auto versions_url = versions_url_optional.value();
auto netJob = makeShared<NetJob>(QString("%1::Versions").arg(args.pack->name), APPLICATION->network());
auto response = std::make_shared<QByteArray>();
netJob->addNetAction(Net::ApiDownload::makeByteArray(versions_url, response));
QObject::connect(netJob.get(),
&NetJob::succeeded,
[this, response, callbacks, args]
{
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response for getting versions at "
<< parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *response;
return;
}
QVector<ModPlatform::IndexedVersion> unsortedVersions;
try
{
auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
for (auto versionIter : arr)
{
auto obj = versionIter.toObject();
auto file = loadIndexedPackVersion(obj, args.resourceType);
if (!file.addonId.isValid())
file.addonId = args.pack->addonId;
if (file.fileId.isValid() && !file.downloadUrl.isEmpty()) // Heuristic to check if the
// returned value is valid
unsortedVersions.append(file);
}
auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a,
const ModPlatform::IndexedVersion& b) -> bool
{
// dates are in RFC 3339 format
return a.date > b.date;
};
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
}
catch (const JSONValidationError& e)
{
qDebug() << doc;
qWarning() << "Error while reading " << debugName() << " resource version: " << e.cause();
}
callbacks.on_succeed(unsortedVersions);
});
// Capture a weak_ptr instead of a shared_ptr to avoid circular dependency issues.
// This prevents the lambda from extending the lifetime of the shared resource,
// as it only temporarily locks the resource when needed.
auto weak = netJob.toWeakRef();
QObject::connect(netJob.get(),
&NetJob::failed,
[weak, callbacks](const QString& reason)
{
int network_error_code = -1;
if (auto netJob = weak.lock())
{
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action)
network_error_code = failed_action->replyStatusCode();
}
callbacks.on_fail(reason, network_error_code);
});
QObject::connect(netJob.get(),
&NetJob::aborted,
[callbacks]
{
if (callbacks.on_abort != nullptr)
callbacks.on_abort();
});
return netJob;
}
Task::Ptr ResourceAPI::getProjectInfo(ProjectInfoArgs&& args, Callback<ModPlatform::IndexedPack::Ptr>&& callbacks) const
{
auto response = std::make_shared<QByteArray>();
auto job = getProject(args.pack->addonId.toString(), response);
QObject::connect(job.get(),
&NetJob::succeeded,
[this, response, callbacks, args]
{
auto pack = args.pack;
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response for mod info at " << parse_error.offset
<< " reason: " << parse_error.errorString();
qWarning() << *response;
return;
}
try
{
auto obj = Json::requireObject(doc);
if (obj.contains("data"))
obj = Json::requireObject(obj, "data");
loadIndexedPack(*pack, obj);
loadExtraPackInfo(*pack, obj);
}
catch (const JSONValidationError& e)
{
qDebug() << doc;
qWarning() << "Error while reading " << debugName() << " resource info: " << e.cause();
}
callbacks.on_succeed(pack);
});
// Capture a weak_ptr instead of a shared_ptr to avoid circular dependency issues.
// This prevents the lambda from extending the lifetime of the shared resource,
// as it only temporarily locks the resource when needed.
auto weak = job.toWeakRef();
QObject::connect(job.get(),
&NetJob::failed,
[weak, callbacks](const QString& reason)
{
int network_error_code = -1;
if (auto job = weak.lock())
{
if (auto netJob = qSharedPointerDynamicCast<NetJob>(job))
{
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action)
{
network_error_code = failed_action->replyStatusCode();
}
}
}
callbacks.on_fail(reason, network_error_code);
});
QObject::connect(job.get(),
&NetJob::aborted,
[callbacks]
{
if (callbacks.on_abort != nullptr)
callbacks.on_abort();
});
return job;
}
Task::Ptr ResourceAPI::getDependencyVersion(DependencySearchArgs&& args,
Callback<ModPlatform::IndexedVersion>&& callbacks) const
{
auto versions_url_optional = getDependencyURL(args);
if (!versions_url_optional.has_value())
return nullptr;
auto versions_url = versions_url_optional.value();
auto netJob =
makeShared<NetJob>(QString("%1::Dependency").arg(args.dependency.addonId.toString()), APPLICATION->network());
auto response = std::make_shared<QByteArray>();
netJob->addNetAction(Net::ApiDownload::makeByteArray(versions_url, response));
QObject::connect(netJob.get(),
&NetJob::succeeded,
[this, response, callbacks, args]
{
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError)
{
qWarning() << "Error while parsing JSON response for getting versions at "
<< parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *response;
return;
}
QJsonArray arr;
if (args.dependency.version.length() != 0 && doc.isObject())
{
arr.append(doc.object());
}
else
{
arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
}
QVector<ModPlatform::IndexedVersion> versions;
for (auto versionIter : arr)
{
auto obj = versionIter.toObject();
auto file = loadIndexedPackVersion(obj, ModPlatform::ResourceType::Mod);
if (!file.addonId.isValid())
file.addonId = args.dependency.addonId;
if (file.fileId.isValid() && (!file.loaders || args.loader & file.loaders)) // Heuristic to
// check if the
// returned
// value is
// valid
versions.append(file);
}
auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a,
const ModPlatform::IndexedVersion& b) -> bool
{
// dates are in RFC 3339 format
return a.date > b.date;
};
std::sort(versions.begin(), versions.end(), orderSortPredicate);
auto bestMatch = versions.size() != 0 ? versions.front() : ModPlatform::IndexedVersion();
callbacks.on_succeed(bestMatch);
});
// Capture a weak_ptr instead of a shared_ptr to avoid circular dependency issues.
// This prevents the lambda from extending the lifetime of the shared resource,
// as it only temporarily locks the resource when needed.
auto weak = netJob.toWeakRef();
QObject::connect(netJob.get(),
&NetJob::failed,
[weak, callbacks](const QString& reason)
{
int network_error_code = -1;
if (auto netJob = weak.lock())
{
if (auto* failed_action = netJob->getFailedActions().at(0); failed_action)
network_error_code = failed_action->replyStatusCode();
}
callbacks.on_fail(reason, network_error_code);
});
return netJob;
}
QString ResourceAPI::getGameVersionsString(std::list<Version> mcVersions) const
{
QString s;
for (auto& ver : mcVersions)
{
s += QString("\"%1\",").arg(mapMCVersionToModrinth(ver));
}
s.remove(s.length() - 1, 1); // remove last comma
return s;
}
QString ResourceAPI::mapMCVersionToModrinth(Version v) const
{
static const QString preString = " Pre-Release ";
auto verStr = v.toString();
if (verStr.contains(preString))
{
verStr.replace(preString, "-pre");
}
verStr.replace(" ", "-");
return verStr;
}
Task::Ptr ResourceAPI::getProject(QString addonId, std::shared_ptr<QByteArray> response) const
{
auto project_url_optional = getInfoURL(addonId);
if (!project_url_optional.has_value())
return nullptr;
auto project_url = project_url_optional.value();
auto netJob = makeShared<NetJob>(QString("%1::GetProject").arg(addonId), APPLICATION->network());
netJob->addNetAction(Net::ApiDownload::makeByteArray(QUrl(project_url), response));
return netJob;
}
|