Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make MAD process cancellable #815

Merged
merged 19 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions primedev/mods/autodownload/moddownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ int ModDownloader::ModFetchingProgressCallback(
{
NOTE_UNUSED(totalToUpload);
NOTE_UNUSED(nowUploaded);

// Abort download
ModDownloader* instance = static_cast<ModDownloader*>(ptr);
if (instance->modState.state == ABORTED)
{
return 1;
}

if (totalDownloadSize != 0 && finishedDownloadSize != 0)
{
ModDownloader* instance = static_cast<ModDownloader*>(ptr);
Expand Down Expand Up @@ -563,6 +571,13 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform)
}
}

// 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)
{
Expand Down Expand Up @@ -602,8 +617,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
Expand All @@ -613,7 +627,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();
Expand All @@ -626,11 +643,17 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion)

// Extract downloaded mod archive
ExtractMod(archiveLocation, fullVersion.platform);
modState.state = DONE;
});

requestThread.detach();
}

void ModDownloader::CancelDownload()
{
modState.state = ABORTED;
}

ON_DLL_LOAD_RELIESON("engine.dll", ModDownloader, (ConCommand), (CModule module))
{
g_pModDownloader = new ModDownloader();
Expand Down Expand Up @@ -687,3 +710,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;
}
1 change: 1 addition & 0 deletions primedev/mods/autodownload/moddownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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
Expand Down