| 261 | } |
| 262 | |
| 263 | Task::Ptr EnsureMetadataTask::modrinthProjectsTask() |
| 264 | { |
| 265 | QHash<QString, QString> addonIds; |
| 266 | for (auto const& data : m_tempVersions) |
| 267 | addonIds.insert(data.addonId.toString(), data.hash); |
| 268 | |
| 269 | Task::Ptr proj_task; |
| 270 | QByteArray* response; |
| 271 | |
| 272 | if (addonIds.isEmpty()) { |
| 273 | qWarning() << "No addonId found!"; |
| 274 | } else if (addonIds.size() == 1) { |
| 275 | std::tie(proj_task, response) = modrinth_api.getProject(*addonIds.keyBegin()); |
| 276 | } else { |
| 277 | std::tie(proj_task, response) = modrinth_api.getProjects(addonIds.keys()); |
| 278 | } |
| 279 | |
| 280 | // Prevents unfortunate timings when aborting the task |
| 281 | if (!proj_task) |
| 282 | return Task::Ptr{ nullptr }; |
| 283 | |
| 284 | connect(proj_task.get(), &Task::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 | QJsonArray entries; |
| 295 | |
| 296 | try { |
| 297 | if (addonIds.size() == 1) |
| 298 | entries = { doc.object() }; |
| 299 | else |
| 300 | entries = Json::requireArray(doc); |
| 301 | } catch (Json::JsonException& e) { |
| 302 | qDebug() << e.cause(); |
| 303 | qDebug() << doc; |
| 304 | } |
| 305 | |
| 306 | for (auto entry : entries) { |
| 307 | ModPlatform::IndexedPack pack; |
| 308 | |
| 309 | try { |
| 310 | auto entry_obj = Json::requireObject(entry); |
| 311 | |
| 312 | Modrinth::loadIndexedPack(pack, entry_obj); |
| 313 | } catch (Json::JsonException& e) { |
| 314 | qDebug() << e.cause(); |
| 315 | qDebug() << doc; |
| 316 | |
| 317 | // Skip this entry, since it has problems |
| 318 | continue; |
| 319 | } |
| 320 |
nothing calls this directly
no test coverage detected