From a8cd20820848c5f09eac3f8edd9a6c2ff00e0ca3 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sat, 7 Jun 2025 21:55:49 +0200 Subject: [PATCH] Make duplicate mod detection more readable Co-authored-by: SaltySnail <51403141+SaltySnail@users.noreply.github.com> --- src/Network/Resources.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index a71c41f..1cfa90e 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -478,8 +478,9 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector> CachedMods = {}; if (deleteDuplicateMods) { for (const auto& entry : fs::directory_iterator(CachingDirectory)) { - if (entry.is_regular_file() && entry.path().extension() == ".zip" && entry.path().filename().string().length() > 10) { - CachedMods.push_back(std::make_pair(entry.path().filename().string().substr(0, entry.path().filename().string().length() - 13) + ".zip", entry.path())); + const std::string filename = entry.path().filename().string(); + if (entry.is_regular_file() && entry.path().extension() == ".zip" && filename.length() > 10) { + CachedMods.push_back(std::make_pair(filename.substr(0, filename.length() - 13) + ".zip", entry.path())); } } } @@ -490,7 +491,9 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vectorFileName && CachedMod.second.stem().string() + ".zip" != std::filesystem::path(ModInfoIter->FileName).stem().string() + "-" + ModInfoIter->Hash.substr(0, 8) + ".zip") { + const bool cachedModExists = CachedMod.first == ModInfoIter->FileName; + const bool cachedModIsNotNewestVersion = CachedMod.second.stem().string() + ".zip" != std::filesystem::path(ModInfoIter->FileName).stem().string() + "-" + ModInfoIter->Hash.substr(0, 8) + ".zip"; + if (cachedModExists && cachedModIsNotNewestVersion) { debug("Found duplicate mod '" + CachedMod.second.stem().string() + ".zip" + "' in cache, removing it"); std::filesystem::remove(CachedMod.second); break;