MCPcopy Create free account
hub / github.com/PolyMC/PolyMC / modrinthProjectsTask

Method modrinthProjectsTask

launcher/modplatform/EnsureMetadataTask.cpp:263–335  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

261}
262
263NetJob::Ptr EnsureMetadataTask::modrinthProjectsTask()
264{
265 QHash<QString, QString> addonIds;
266 for (auto const& data : m_temp_versions)
267 addonIds.insert(data.addonId.toString(), data.hash);
268
269 auto response = new QByteArray();
270 NetJob::Ptr proj_task;
271
272 if (addonIds.isEmpty()) {
273 qWarning() << "No addonId found!";
274 } else if (addonIds.size() == 1) {
275 proj_task = modrinth_api.getProject(*addonIds.keyBegin(), response);
276 } else {
277 proj_task = modrinth_api.getProjects(addonIds.keys(), response);
278 }
279
280 // Prevents unfortunate timings when aborting the task
281 if (!proj_task)
282 return {};
283
284 connect(proj_task.get(), &NetJob::succeeded, this, [this, response, addonIds] {
285 QJsonParseError parse_error{};
286 auto doc = QJsonDocument::fromJson(*response, &parse_error);
287 if (parse_error.error != QJsonParseError::NoError) {
288 qWarning() << "Error while parsing JSON response from Modrinth projects task at " << parse_error.offset
289 << " reason: " << parse_error.errorString();
290 qWarning() << *response;
291 return;
292 }
293
294 try {
295 QJsonArray entries;
296 if (addonIds.size() == 1)
297 entries = { doc.object() };
298 else
299 entries = Json::requireArray(doc);
300
301 for (auto entry : entries) {
302 auto entry_obj = Json::requireObject(entry);
303
304 ModPlatform::IndexedPack pack;
305 Modrinth::loadIndexedPack(pack, entry_obj);
306
307 auto hash = addonIds.find(pack.addonId.toString()).value();
308
309 auto mod_iter = m_mods.find(hash);
310 if (mod_iter == m_mods.end()) {
311 qWarning() << "Invalid project id from the API response.";
312 continue;
313 }
314
315 auto* mod = mod_iter.value();
316
317 try {
318 setStatus(tr("Parsing API response from Modrinth for '%1'...").arg(mod->name()));
319
320 modrinthCallback(pack, m_temp_versions.find(hash).value(), mod);

Callers

nothing calls this directly

Calls 15

fromJsonFunction · 0.85
requireArrayFunction · 0.85
requireObjectFunction · 0.85
loadIndexedPackFunction · 0.85
insertMethod · 0.80
isEmptyMethod · 0.80
getProjectMethod · 0.80
objectMethod · 0.80
endMethod · 0.80
causeMethod · 0.80
toStringMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected