From 345e24d4727f0c7b4e30353b302cf2735ef2db4e Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 00:54:06 +0200 Subject: [PATCH 1/7] feat: add ModInstallState.ABORTED enum value --- primedev/mods/autodownload/moddownloader.h | 1 + 1 file changed, 1 insertion(+) diff --git a/primedev/mods/autodownload/moddownloader.h b/primedev/mods/autodownload/moddownloader.h index 98fc27ae3..9166afdf1 100644 --- a/primedev/mods/autodownload/moddownloader.h +++ b/primedev/mods/autodownload/moddownloader.h @@ -123,6 +123,7 @@ class ModDownloader CHECKSUMING, EXTRACTING, DONE, // Everything went great, mod can be used in-game + ABORTED, // User cancelled mod install process // Errors FAILED, // Generic error message, should be avoided as much as possible From 5ce546e5455fc2786921614db86a8ecf229aa321 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 00:54:29 +0200 Subject: [PATCH 2/7] feat: add ModDownloader::CancelDownload implementation --- primedev/mods/autodownload/moddownloader.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 8e533decb..3c083b068 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -621,6 +621,12 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) requestThread.detach(); } +void ModDownloader::CancelDownload() +{ + modState.state = ABORTED; +} + + ON_DLL_LOAD_RELIESON("engine.dll", ModDownloader, (ConCommand), (CModule module)) { g_pModDownloader = new ModDownloader(); From 52781ecc7a29341a9d78de5f85685b1783e39fd4 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 00:54:49 +0200 Subject: [PATCH 3/7] feat: expose CancelDownload function to Squirrel VM --- primedev/mods/autodownload/moddownloader.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 3c083b068..2fd3cf036 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -683,3 +683,9 @@ ADD_SQFUNC("ModInstallState", NSGetModInstallState, "", "", ScriptContext::SERVE return SQRESULT_NOTNULL; } + +ADD_SQFUNC("void", NSCancelModDownload, "", "", ScriptContext::SERVER | ScriptContext::CLIENT | ScriptContext::UI) +{ + g_pModDownloader->CancelDownload(); + return SQRESULT_NULL; +} From 6f45e16962c4cc24062eecb247b99c6724bad28e Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 02:34:37 +0200 Subject: [PATCH 4/7] feat: calling CancelDownload interrupts FetchModFromDistantStore curl request --- primedev/mods/autodownload/moddownloader.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 2fd3cf036..e079ebc49 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -152,6 +152,14 @@ int ModDownloader::ModFetchingProgressCallback( { NOTE_UNUSED(totalToUpload); NOTE_UNUSED(nowUploaded); + + // Abort download + ModDownloader* instance = static_cast(ptr); + if (instance->modState.state == ABORTED) + { + return 1; + } + if (totalDownloadSize != 0 && finishedDownloadSize != 0) { ModDownloader* instance = static_cast(ptr); @@ -603,7 +611,10 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) if (!fetchingResult.has_value()) { spdlog::error("Something went wrong while fetching archive, aborting."); - modState.state = MOD_FETCHING_FAILED; + if (modState.state != ABORTED) + { + modState.state = MOD_FETCHING_FAILED; + } return; } archiveLocation = fetchingResult.value(); From 83a563ea2df7cd2f180a494b1f8f99cfac5eeb6e Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 21:54:50 +0200 Subject: [PATCH 5/7] feat: calling CancelDownload interrupts ExtractMod archive browsing --- primedev/mods/autodownload/moddownloader.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index e079ebc49..5282b77c8 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -562,6 +562,13 @@ void ModDownloader::ExtractMod(fs::path modPath) } } + // Abort mod extraction if needed + if (modState.state == ABORTED) + { + spdlog::info("User cancelled mod installation, aborting mod extraction."); + return; + } + // Go to next file if ((i + 1) < gi.number_entry) { From c2446ee3d64497a1a22838528a073ff0e1032a27 Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 21:55:49 +0200 Subject: [PATCH 6/7] style: apply clang formatting --- primedev/mods/autodownload/moddownloader.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/primedev/mods/autodownload/moddownloader.cpp b/primedev/mods/autodownload/moddownloader.cpp index 5282b77c8..c9eaab51e 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -644,7 +644,6 @@ void ModDownloader::CancelDownload() modState.state = ABORTED; } - ON_DLL_LOAD_RELIESON("engine.dll", ModDownloader, (ConCommand), (CModule module)) { g_pModDownloader = new ModDownloader(); From be8766986794b0ac6104ae9f1fddab77ce62723d Mon Sep 17 00:00:00 2001 From: Remy Raes Date: Wed, 4 Sep 2024 23:04:57 +0200 Subject: [PATCH 7/7] 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 c9eaab51e..cfd9eccb7 100644 --- a/primedev/mods/autodownload/moddownloader.cpp +++ b/primedev/mods/autodownload/moddownloader.cpp @@ -608,8 +608,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 @@ -634,6 +633,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion) // Extract downloaded mod archive ExtractMod(archiveLocation); + modState.state = DONE; }); requestThread.detach();