Skip to content

Commit

Permalink
fix some mod auto downloading issues
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf109909 committed Jul 28, 2024
1 parent e187919 commit e01944f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion primedev/masterserver/masterserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ void MasterServerManager::AuthenticateWithServer(
nlohmann::json connection_info_json = nlohmann::json::parse(res->body);
if (connection_info_json.at("success") == true)
{
spdlog::info("[auth_with_server] body: {}", res->body);
//spdlog::info("[auth_with_server] body: {}", res->body);
m_pendingConnectionInfo.ip.S_un.S_addr = inet_addr(std::string(connection_info_json.at("ip")).c_str());
m_pendingConnectionInfo.port = static_cast<unsigned short>(connection_info_json.at("port"));
m_pendingConnectionInfo.authToken = connection_info_json.at("authToken");
Expand Down
60 changes: 43 additions & 17 deletions primedev/mods/autodownload/moddownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,46 +555,72 @@ void ModDownloader::DownloadMod(std::string modName, std::string modVersion)
return;
}

if(this->isDownloadingMod)
return;

modState.state = IDLE;

std::thread requestThread(
[this, modName, modVersion]()
{
fs::path archiveLocation;
{


ScopeGuard cleanup(
[&]
{
try
{
remove(archiveLocation);
}
catch (const std::exception& a)
{
spdlog::error("Error while removing downloaded archive: {}", a.what());
}

modState.state = DONE;
spdlog::info("Done downloading {}.", modName);
});
this->isDownloadingMod = true;
fs::path archiveLocation;

// Download mod archive

std::string expectedHash = verifiedMods[modName].versions[modVersion].checksum;
std::optional<fs::path> fetchingResult = FetchModFromDistantStore(std::string_view(modName), std::string_view(modVersion));


if (!fetchingResult.has_value())
{
spdlog::error("Something went wrong while fetching archive, aborting.");
modState.state = MOD_FETCHING_FAILED;
this->isDownloadingMod = false;
try
{
remove(archiveLocation);
}
catch (const std::exception& a)
{
spdlog::error("Error while removing downloaded archive: {}", a.what());
}
return;
}
archiveLocation = fetchingResult.value();
if (!IsModLegit(archiveLocation, std::string_view(expectedHash)))
{
spdlog::warn("Archive hash does not match expected checksum, aborting.");
modState.state = MOD_CORRUPTED;
this->isDownloadingMod = false;
try
{
remove(archiveLocation);
}
catch (const std::exception& a)
{
spdlog::error("Error while removing downloaded archive: {}", a.what());
}
return;
}

// Extract downloaded mod archive
ExtractMod(archiveLocation);
try
{
remove(archiveLocation);
}
catch (const std::exception& a)
{
spdlog::error("Error while removing downloaded archive: {}", a.what());
}

modState.state = DONE;
this->isDownloadingMod = false;
spdlog::info("Done downloading {}.", modName);
return;
});

requestThread.detach();
Expand Down
8 changes: 7 additions & 1 deletion primedev/mods/autodownload/moddownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class ModDownloader
const char* VERIFICATION_FLAG = "-disablemodverification";
const char* CUSTOM_MODS_URL_FLAG = "-customverifiedurl=";
const char* STORE_URL = "https://gcdn.thunderstore.io/live/repository/packages/";
const char* DEFAULT_MODS_LIST_URL = "https://raw.githubusercontent.com/R2Northstar/VerifiedMods/main/verified-mods.json";
const char* DEFAULT_MODS_LIST_URL = "https://gitee.com/R2NorthstarCN/VerifiedMods/raw/master/verified-mods.json";
char* modsListUrl;


struct VerifiedModVersion
{
Expand Down Expand Up @@ -118,6 +119,8 @@ class ModDownloader

enum ModInstallState
{
// Indicate UI to not immediately return false
IDLE,
// Normal installation process
DOWNLOADING,
CHECKSUMING,
Expand Down Expand Up @@ -152,4 +155,7 @@ class ModDownloader
* @returns nothing
*/
void CancelDownload();


bool isDownloadingMod = false;
};

0 comments on commit e01944f

Please sign in to comment.