Check for update: * - Get latest version available * - Compare hash of the latest version with the current hash * - If equal, no updates, else, there's updates, so add to the list * */
| 26 | * - If equal, no updates, else, there's updates, so add to the list |
| 27 | * */ |
| 28 | void ModrinthCheckUpdate::executeTask() |
| 29 | { |
| 30 | setStatus(tr("Preparing mods for Modrinth...")); |
| 31 | setProgress(0, 3); |
| 32 | |
| 33 | QHash<QString, Mod*> mappings; |
| 34 | |
| 35 | // Create all hashes |
| 36 | QStringList hashes; |
| 37 | auto best_hash_type = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH).first(); |
| 38 | |
| 39 | ConcurrentTask hashing_task(this, "MakeModrinthHashesTask", 10); |
| 40 | for (auto* mod : m_mods) { |
| 41 | if (!mod->enabled()) { |
| 42 | emit checkFailed(mod, tr("Disabled mods won't be updated, to prevent mod duplication issues!")); |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | auto hash = mod->metadata()->hash; |
| 47 | |
| 48 | // Sadly the API can only handle one hash type per call, se we |
| 49 | // need to generate a new hash if the current one is innadequate |
| 50 | // (though it will rarely happen, if at all) |
| 51 | if (mod->metadata()->hash_format != best_hash_type) { |
| 52 | auto hash_task = Hashing::createModrinthHasher(mod->fileinfo().absoluteFilePath()); |
| 53 | connect(hash_task.get(), &Task::succeeded, [&] { |
| 54 | QString hash (hash_task->getResult()); |
| 55 | hashes.append(hash); |
| 56 | mappings.insert(hash, mod); |
| 57 | }); |
| 58 | connect(hash_task.get(), &Task::failed, [this, hash_task] { failed("Failed to generate hash"); }); |
| 59 | hashing_task.addTask(hash_task); |
| 60 | } else { |
| 61 | hashes.append(hash); |
| 62 | mappings.insert(hash, mod); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | QEventLoop loop; |
| 67 | connect(&hashing_task, &Task::finished, [&loop]{ loop.quit(); }); |
| 68 | hashing_task.start(); |
| 69 | loop.exec(); |
| 70 | |
| 71 | auto* response = new QByteArray(); |
| 72 | auto job = api.latestVersions(hashes, best_hash_type, m_game_versions, m_loaders, response); |
| 73 | |
| 74 | QEventLoop lock; |
| 75 | |
| 76 | connect(job.get(), &Task::succeeded, this, [this, response, &mappings, best_hash_type, job] { |
| 77 | QJsonParseError parse_error{}; |
| 78 | QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error); |
| 79 | if (parse_error.error != QJsonParseError::NoError) { |
| 80 | qWarning() << "Error while parsing JSON response from ModrinthCheckUpdate at " << parse_error.offset |
| 81 | << " reason: " << parse_error.errorString(); |
| 82 | qWarning() << *response; |
| 83 | |
| 84 | failed(parse_error.errorString()); |
| 85 | return; |
nothing calls this directly
no test coverage detected