diff --git a/include/Network/network.hpp b/include/Network/network.hpp index 5b1ec0f..f0d3d4a 100644 --- a/include/Network/network.hpp +++ b/include/Network/network.hpp @@ -30,6 +30,7 @@ extern uint64_t UDPSock; extern uint64_t TCPSock; extern std::string Branch; extern std::string CachingDirectory; +extern bool deleteDuplicateMods; extern bool TCPTerminate; extern std::string LastIP; extern std::string MStatus; diff --git a/src/Config.cpp b/src/Config.cpp index 562656a..9e9d3d9 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -15,6 +15,7 @@ namespace fs = std::filesystem; std::string Branch; std::string CachingDirectory = "./Resources"; +bool deleteDuplicateMods = false; void ParseConfig(const nlohmann::json& d) { if (d["Port"].is_number()) { @@ -42,6 +43,11 @@ void ParseConfig(const nlohmann::json& d) { options.no_launch = dev; options.no_update = dev; } + + if (d.contains(("DeleteDuplicateMods")) && d["DeleteDuplicateMods"].is_boolean()) { + deleteDuplicateMods = d["DeleteDuplicateMods"].get(); + } + } void ConfigInit() { diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index c5e0cc4..a71c41f 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -475,10 +475,28 @@ 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())); + } + } + } + int ModNo = 0; int TotalMods = ModInfos.size(); for (auto ModInfoIter = ModInfos.begin(), AlsoModInfoIter = ModInfos.begin(); ModInfoIter != ModInfos.end() && !Terminate; ++ModInfoIter, ++AlsoModInfoIter) { ++ModNo; + if (deleteDuplicateMods) { + for (auto& CachedMod : CachedMods) { + if (CachedMod.first == ModInfoIter->FileName && CachedMod.second.stem().string() + ".zip" != std::filesystem::path(ModInfoIter->FileName).stem().string() + "-" + ModInfoIter->Hash.substr(0, 8) + ".zip") { + debug("Found duplicate mod '" + CachedMod.second.stem().string() + ".zip" + "' in cache, removing it"); + std::filesystem::remove(CachedMod.second); + break; + } + } + } if (ModInfoIter->Hash.length() < 8 || ModInfoIter->HashAlgorithm != "sha256") { error("Unsupported hash algorithm or invalid hash for '" + ModInfoIter->FileName + "'"); Terminate = true;