| 267 | }; |
| 268 | |
| 269 | std::optional<ModPlatform::IndexedVersion> FlameAPI::getLatestVersion(QList<ModPlatform::IndexedVersion> versions, |
| 270 | QList<ModPlatform::ModLoaderType> instanceLoaders, |
| 271 | ModPlatform::ModLoaderTypes modLoaders) |
| 272 | { |
| 273 | static const auto noLoader = ModPlatform::ModLoaderType(0); |
| 274 | QHash<ModPlatform::ModLoaderType, ModPlatform::IndexedVersion> bestMatch; |
| 275 | auto checkVersion = [&bestMatch](const ModPlatform::IndexedVersion& version, const ModPlatform::ModLoaderType& loader) { |
| 276 | if (bestMatch.contains(loader)) { |
| 277 | auto best = bestMatch.value(loader); |
| 278 | if (version.date > best.date) { |
| 279 | bestMatch[loader] = version; |
| 280 | } |
| 281 | } else { |
| 282 | bestMatch[loader] = version; |
| 283 | } |
| 284 | }; |
| 285 | for (auto file_tmp : versions) { |
| 286 | auto loaders = ModPlatform::modLoaderTypesToList(file_tmp.loaders); |
| 287 | if (loaders.isEmpty()) { |
| 288 | checkVersion(file_tmp, noLoader); |
| 289 | } else { |
| 290 | for (auto loader : loaders) { |
| 291 | checkVersion(file_tmp, loader); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | // edge case: mod has installed for forge but the instance is fabric => fabric version will be prioritizated on update |
| 296 | auto currentLoaders = instanceLoaders + ModPlatform::modLoaderTypesToList(modLoaders); |
| 297 | currentLoaders.append(noLoader); // add a fallback in case the versions do not define a loader |
| 298 | |
| 299 | for (auto loader : currentLoaders) { |
| 300 | if (bestMatch.contains(loader)) { |
| 301 | auto bestForLoader = bestMatch.value(loader); |
| 302 | // awkward case where the mod has only two loaders and one of them is not specified |
| 303 | if (loader != noLoader && bestMatch.contains(noLoader) && bestMatch.size() == 2) { |
| 304 | auto bestForNoLoader = bestMatch.value(noLoader); |
| 305 | if (bestForNoLoader.date > bestForLoader.date) { |
| 306 | return bestForNoLoader; |
| 307 | } |
| 308 | } |
| 309 | return bestForLoader; |
| 310 | } |
| 311 | } |
| 312 | return {}; |
| 313 | } |
no test coverage detected