Skip to content

Commit

Permalink
Merge branch 'pr/817' into nscn-v1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf109909 committed Nov 21, 2024
2 parents 57811ef + 4f1a834 commit 8d5933a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
38 changes: 27 additions & 11 deletions primedev/mods/autodownload/moddownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,9 @@ int GetModArchiveSize(unzFile file, unz_global_info64 info)
return totalSize;
}

void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform)
void ModDownloader::ExtractMod(fs::path modPath, fs::path destinationPath, VerifiedModPlatform platform)
{
unzFile file;
std::string name;
fs::path modDirectory;

file = unzOpen(modPath.generic_string().c_str());
ScopeGuard cleanup(
Expand Down Expand Up @@ -445,11 +443,6 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform)
return;
}

// 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];
Expand All @@ -459,7 +452,7 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform)
// 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
Expand Down Expand Up @@ -589,6 +582,9 @@ void ModDownloader::ExtractMod(fs::path modPath, VerifiedModPlatform platform)
}
}
}

// Mod extraction went fine
modState.state = DONE;
}

void ModDownloader::DownloadMod(std::string modName, std::string modVersion)
Expand All @@ -603,11 +599,14 @@ 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(
[&]
{
// Remove downloaded archive
try
{
remove(archiveLocation);
Expand All @@ -617,6 +616,19 @@ 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);
});

Expand All @@ -641,9 +653,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, fullVersion.platform);
modState.state = DONE;
ExtractMod(archiveLocation, modDirectory, fullVersion.platform);
});

requestThread.detach();
Expand Down
8 changes: 5 additions & 3 deletions primedev/mods/autodownload/moddownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ 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; the install folder is defined by the platform parameter.
* This extracts a downloaded mod archive from its original location, `modPath`,
* to the specified `destinationPath`; the way to decompress the archive is
* defined by the `platform` parameter.
*
* @param modPath location of the downloaded archive
* @param destinationPath destination of the extraction
* @param platform origin of the downloaded archive
* @returns nothing
*/
void ExtractMod(fs::path modPath, VerifiedModPlatform platform);
void ExtractMod(fs::path modPath, fs::path destinationPath, VerifiedModPlatform platform);

public:
ModDownloader();
Expand Down

0 comments on commit 8d5933a

Please sign in to comment.