From 4ce2afe75a1af12da57ea18968d522cc6f465358 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 23:04:57 +0200 Subject: [PATCH 1/6] fix: don't set state to DONE in cleanup handle --- primedev/mods/autodownload/moddownloader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 8e533decb..7be842525 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -593,8 +593,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) spdlog::error("Error while removing downloaded archive: {}", a.what()); } - modState.state = DONE; - spdlog::info("Done downloading {}.", modName); + spdlog::info("Done cleaning after downloading {}.", modName); }); // Download mod archive @@ -616,6 +615,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) // Extract downloaded mod archive ExtractMod(archiveLocation); + modState.state = DONE; }); requestThread.detach(); From 78956b34a6028dfa1811eb34b8a893c2680f6e0b Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Sat, 7 Sep 2024 17:00:25 +0200 Subject: [PATCH 2/6] refactor: extract destination path computing logic from ExtractMod function --- primedev/mods/autodownload/moddownloader.cpp | 20 ++++++++++---------- primedev/mods/autodownload/moddownloader.h | 7 ++++--- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 7be842525..c720dcab5 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -390,11 +390,9 @@ int GetModArchiveSize(unzFile file, unz_global_info64 info) return totalSize; } -void ModDownloader::ExtractMod(fs::path modPath) +void ModDownloader::ExtractMod(fs::path modPath, fs::path destinationPath) { unzFile file; - std::string name; - fs::path modDirectory; file = unzOpen(modPath.generic_string().c_str()); ScopeGuard cleanup( @@ -428,11 +426,6 @@ void ModDownloader::ExtractMod(fs::path modPath) modState.total = GetModArchiveSize(file, gi); modState.progress = 0; - // Mod directory name (removing the ".zip" fom the archive name) - name = modPath.filename().string(); - name = name.substr(0, name.length() - 4); - modDirectory = GetRemoteModFolderPath() / name; - for (int i = 0; i < gi.number_entry; i++) { char zipFilename[256]; @@ -442,7 +435,7 @@ void ModDownloader::ExtractMod(fs::path modPath) // Extract file { std::error_code ec; - fs::path fileDestination = modDirectory / zipFilename; + fs::path fileDestination = destinationPath / zipFilename; spdlog::info("=> {}", fileDestination.generic_string()); // Create parent directory if needed @@ -579,7 +572,9 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) std::thread requestThread( [this, modName, modVersion]() { + std::string name; fs::path archiveLocation; + fs::path modDirectory; ScopeGuard cleanup( [&] @@ -613,8 +608,13 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) return; } + // Mod directory name (removing the ".zip" fom the archive name) + name = archiveLocation.filename().string(); + name = name.substr(0, name.length() - 4); + modDirectory = GetRemoteModFolderPath() / name; + // Extract downloaded mod archive - ExtractMod(archiveLocation); + ExtractMod(archiveLocation, modDirectory); modState.state = DONE; }); diff --git a/primedev/mods/autodownload/moddownloader.h b/primedev/mods/autodownload/moddownloader.h index 98fc27ae3..1c02d80b1 100644 --- a/primedev/mods/autodownload/moddownloader.h +++ b/primedev/mods/autodownload/moddownloader.h @@ -64,13 +64,14 @@ class ModDownloader /** * Extracts a mod archive to the game folder. * - * This extracts a downloaded mod archive from its original location to the - * current game profile, in the remote mods folder. + * This extracts a downloaded mod archive from its original location, `modPath`, + * to the specified `destinationPath`. * * @param modPath location of the downloaded archive + * @param destinationPath destination of the extraction * @returns nothing */ - void ExtractMod(fs::path modPath); + void ExtractMod(fs::path modPath, fs::path destinationPath); public: ModDownloader(); From 4ff2af716cac8ece497edc5f6ebc81f64e468b0b Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Sat, 7 Sep 2024 17:13:50 +0200 Subject: [PATCH 3/6] fix: move DONE state attribution into ExtractMod function --- primedev/mods/autodownload/moddownloader.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index c720dcab5..55c413811 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -558,6 +558,9 @@ void ModDownloader::ExtractMod(fs::path modPath, fs::path destinationPath) } } } + + // Mod extraction went fine + modState.state = DONE; } void ModDownloader::DownloadMod(std::string modName, std::string modVersion) @@ -615,7 +618,6 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) // Extract downloaded mod archive ExtractMod(archiveLocation, modDirectory); - modState.state = DONE; }); requestThread.detach(); From 1205f27dab61b02efa278ffc2b92c771a574510b Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Sat, 7 Sep 2024 17:31:47 +0200 Subject: [PATCH 4/6] feat: remove mod if auto-download process failed --- primedev/mods/autodownload/moddownloader.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 55c413811..973edb65e 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -582,6 +582,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) ScopeGuard cleanup( [&] { + // Remove downloaded archive try { remove(archiveLocation); @@ -591,6 +592,20 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) spdlog::error("Error while removing downloaded archive: {}", a.what()); } + // Remove mod if auto-download process failed + if (modState.state != DONE) + { + try + { + remove(modDirectory); + } + catch(const std::exception& e) + { + spdlog::error("Error while removing downloaded mod: {}", e.what()); + } + + } + spdlog::info("Done cleaning after downloading {}.", modName); }); From 5a9afd6e04d440e8cf5f3eac515919ce161ae778 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Sat, 7 Sep 2024 20:36:12 +0200 Subject: [PATCH 5/6] style: apply clang formatting --- primedev/mods/autodownload/moddownloader.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 973edb65e..a15fddfec 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -599,11 +599,10 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) { remove(modDirectory); } - catch(const std::exception& e) + catch (const std::exception& e) { spdlog::error("Error while removing downloaded mod: {}", e.what()); } - } spdlog::info("Done cleaning after downloading {}.", modName); From c0987453ad84c85e2151dcd1dc161262dbbf4cd2 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Sat, 23 Nov 2024 00:34:49 +0100 Subject: [PATCH 6/6] refactor: review logic to remove mod archive and directory if needed --- primedev/mods/autodownload/moddownloader.cpp | 22 ++++++++++++-------- primedev/mods/autodownload/moddownloader.h | 6 ++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index f34cffa51..de95a0c2c 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -176,7 +176,7 @@ int ModDownloader::ModFetchingProgressCallback( return 0; } -std::optional ModDownloader::FetchModFromDistantStore(std::string_view modName, VerifiedModVersion version) +std::tuple ModDownloader::FetchModFromDistantStore(std::string_view modName, VerifiedModVersion version) { std::string url = version.downloadLink; std::string archiveName = fs::path(url).filename().generic_string(); @@ -190,7 +190,7 @@ std::optional ModDownloader::FetchModFromDistantStore(std::string_view modState.state = DOWNLOADING; // Download the actual archive - bool failed = false; + bool success = false; FILE* fp = fopen(downloadPath.generic_string().c_str(), "wb"); CURLcode result; CURL* easyhandle; @@ -219,13 +219,14 @@ std::optional ModDownloader::FetchModFromDistantStore(std::string_view if (result == CURLcode::CURLE_OK) { spdlog::info("Mod archive successfully fetched."); - return std::optional(downloadPath); + success = true; } else { spdlog::error("Fetching mod archive failed: {}", curl_easy_strerror(result)); - return std::optional(); } + + return {downloadPath, success}; } bool ModDownloader::IsModLegit(fs::path modPath, std::string_view expectedChecksum) @@ -621,7 +622,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) { try { - remove(modDirectory); + remove_all(modDirectory); } catch (const std::exception& e) { @@ -635,8 +636,12 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) // Download mod archive VerifiedModVersion fullVersion = verifiedMods[modName].versions[modVersion]; std::string expectedHash = fullVersion.checksum; - std::optional fetchingResult = FetchModFromDistantStore(std::string_view(modName), fullVersion); - if (!fetchingResult.has_value()) + + const std::tuple downloadResult = FetchModFromDistantStore(std::string_view(modName), fullVersion); + archiveLocation = get<0>(downloadResult); + bool downloadSuccessful = get<1>(downloadResult); + + if (!downloadSuccessful) { spdlog::error("Something went wrong while fetching archive, aborting."); if (modState.state != ABORTED) @@ -645,7 +650,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) } return; } - archiveLocation = fetchingResult.value(); + if (!IsModLegit(archiveLocation, std::string_view(expectedHash))) { spdlog::warn("Archive hash does not match expected checksum, aborting."); @@ -660,7 +665,6 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) // Extract downloaded mod archive ExtractMod(archiveLocation, modDirectory, fullVersion.platform); - modState.state = DONE; }); requestThread.detach(); diff --git a/primedev/mods/autodownload/moddownloader.h b/primedev/mods/autodownload/moddownloader.h index 6be1dacc2..f8c0c6646 100644 --- a/primedev/mods/autodownload/moddownloader.h +++ b/primedev/mods/autodownload/moddownloader.h @@ -43,14 +43,12 @@ class ModDownloader * input mod name as mod dependency string if bypass flag is set up; fetched * archive is then stored in a temporary location. * - * If something went wrong during archive download, this will return an empty - * optional object. * * @param modName name of the mod to be downloaded * @param modVersion version of the mod to be downloaded - * @returns location of the downloaded archive + * @returns tuple containing location of the downloaded archive and whether download completed successfully */ - std::optional FetchModFromDistantStore(std::string_view modName, VerifiedModVersion modVersion); + std::tuple FetchModFromDistantStore(std::string_view modName, VerifiedModVersion modVersion); /** * Tells if a mod archive has not been corrupted.