| 219 | }; |
| 220 | |
| 221 | std::optional<ModPlatform::IndexedVersion> FlameAPI::getLatestVersion(QList<ModPlatform::IndexedVersion> versions, |
| 222 | QList<ModPlatform::ModLoaderType> instanceLoaders, |
| 223 | ModPlatform::ModLoaderTypes modLoaders, |
| 224 | bool checkLoaders) |
| 225 | { |
| 226 | static const auto noLoader = ModPlatform::ModLoaderType(0); |
| 227 | if (!checkLoaders) { |
| 228 | std::optional<ModPlatform::IndexedVersion> ver; |
| 229 | for (auto file_tmp : versions) { |
| 230 | if (!ver.has_value() || file_tmp.date > ver->date) { |
| 231 | ver = file_tmp; |
| 232 | } |
| 233 | } |
| 234 | return ver; |
| 235 | } |
| 236 | QHash<ModPlatform::ModLoaderType, ModPlatform::IndexedVersion> bestMatch; |
| 237 | auto checkVersion = [&bestMatch](const ModPlatform::IndexedVersion& version, const ModPlatform::ModLoaderType& loader) { |
| 238 | if (bestMatch.contains(loader)) { |
| 239 | auto best = bestMatch.value(loader); |
| 240 | if (version.date > best.date) { |
| 241 | bestMatch[loader] = version; |
| 242 | } |
| 243 | } else { |
| 244 | bestMatch[loader] = version; |
| 245 | } |
| 246 | }; |
| 247 | for (auto file_tmp : versions) { |
| 248 | auto loaders = ModPlatform::modLoaderTypesToList(file_tmp.loaders); |
| 249 | if (loaders.isEmpty()) { |
| 250 | checkVersion(file_tmp, noLoader); |
| 251 | } else { |
| 252 | for (auto loader : loaders) { |
| 253 | checkVersion(file_tmp, loader); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | // edge case: mod has installed for forge but the instance is fabric => fabric version will be prioritizated on update |
| 258 | auto currentLoaders = instanceLoaders + ModPlatform::modLoaderTypesToList(modLoaders); |
| 259 | currentLoaders.append(noLoader); // add a fallback in case the versions do not define a loader |
| 260 | |
| 261 | for (auto loader : currentLoaders) { |
| 262 | if (bestMatch.contains(loader)) { |
| 263 | auto bestForLoader = bestMatch.value(loader); |
| 264 | // awkward case where the mod has only two loaders and one of them is not specified |
| 265 | if (loader != noLoader && bestMatch.contains(noLoader) && bestMatch.size() == 2) { |
| 266 | auto bestForNoLoader = bestMatch.value(noLoader); |
| 267 | if (bestForNoLoader.date > bestForLoader.date) { |
| 268 | return bestForNoLoader; |
| 269 | } |
| 270 | } |
| 271 | return bestForLoader; |
| 272 | } |
| 273 | } |
| 274 | return {}; |
| 275 | } |
no test coverage detected