Skip to content

Commit

Permalink
refactor: review logic to remove mod archive and directory if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alystrasz committed Nov 22, 2024
1 parent 75d9418 commit c098745
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 13 additions & 9 deletions primedev/mods/autodownload/moddownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ int ModDownloader::ModFetchingProgressCallback(
return 0;
}

std::optional<fs::path> ModDownloader::FetchModFromDistantStore(std::string_view modName, VerifiedModVersion version)
std::tuple<fs::path, bool> ModDownloader::FetchModFromDistantStore(std::string_view modName, VerifiedModVersion version)
{
std::string url = version.downloadLink;
std::string archiveName = fs::path(url).filename().generic_string();
Expand All @@ -190,7 +190,7 @@ std::optional<fs::path> 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;
Expand Down Expand Up @@ -219,13 +219,14 @@ std::optional<fs::path> ModDownloader::FetchModFromDistantStore(std::string_view
if (result == CURLcode::CURLE_OK)
{
spdlog::info("Mod archive successfully fetched.");
return std::optional<fs::path>(downloadPath);
success = true;
}
else
{
spdlog::error("Fetching mod archive failed: {}", curl_easy_strerror(result));
return std::optional<fs::path>();
}

return {downloadPath, success};
}

bool ModDownloader::IsModLegit(fs::path modPath, std::string_view expectedChecksum)
Expand Down Expand Up @@ -621,7 +622,7 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion)
{
try
{
remove(modDirectory);
remove_all(modDirectory);
}
catch (const std::exception& e)
{
Expand All @@ -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<fs::path> fetchingResult = FetchModFromDistantStore(std::string_view(modName), fullVersion);
if (!fetchingResult.has_value())

const std::tuple<fs::path, bool> 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)
Expand All @@ -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.");
Expand All @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions primedev/mods/autodownload/moddownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<fs::path> FetchModFromDistantStore(std::string_view modName, VerifiedModVersion modVersion);
std::tuple<fs::path, bool> FetchModFromDistantStore(std::string_view modName, VerifiedModVersion modVersion);

/**
* Tells if a mod archive has not been corrupted.
Expand Down

0 comments on commit c098745

Please sign in to comment.