Implement modified date check on geode files for unzip

This commit is contained in:
altalk23 2023-10-02 14:15:20 +03:00
parent 777cf38df0
commit 5c765c6798
3 changed files with 43 additions and 9 deletions

View file

@ -41,9 +41,6 @@ void Loader::Impl::createDirectories() {
ghc::filesystem::create_directory(dirs::getSaveDir()); ghc::filesystem::create_directory(dirs::getSaveDir());
#endif #endif
// try deleting geode/unzipped if it already exists
try { ghc::filesystem::remove_all(dirs::getModRuntimeDir()); } catch(...) {}
(void) utils::file::createDirectoryAll(dirs::getGeodeResourcesDir()); (void) utils::file::createDirectoryAll(dirs::getGeodeResourcesDir());
(void) utils::file::createDirectoryAll(dirs::getModConfigDir()); (void) utils::file::createDirectoryAll(dirs::getModConfigDir());
(void) utils::file::createDirectoryAll(dirs::getModsDir()); (void) utils::file::createDirectoryAll(dirs::getModsDir());
@ -411,6 +408,7 @@ void Loader::Impl::loadModGraph(Mod* node, bool early) {
m_currentlyLoadingMod = node; m_currentlyLoadingMod = node;
m_refreshingModCount += 1; m_refreshingModCount += 1;
m_refreshedModCount += 1; m_refreshedModCount += 1;
m_lateRefreshedModCount += early ? 0 : 1;
auto unzipFunction = [this, node]() { auto unzipFunction = [this, node]() {
log::debug("Unzip"); log::debug("Unzip");
@ -631,10 +629,16 @@ void Loader::Impl::continueRefreshModGraph() {
return; return;
} }
if (m_lateRefreshedModCount > 0) {
auto end = std::chrono::high_resolution_clock::now();
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - m_timerBegin).count();
log::info("Took {}s", static_cast<float>(time) / 1000.f);
}
log::info("Continuing mod graph refresh..."); log::info("Continuing mod graph refresh...");
log::pushNest(); log::pushNest();
auto begin = std::chrono::high_resolution_clock::now(); m_timerBegin = std::chrono::high_resolution_clock::now();
switch (m_loadingState) { switch (m_loadingState) {
case LoadingState::Mods: case LoadingState::Mods:
@ -652,6 +656,11 @@ void Loader::Impl::continueRefreshModGraph() {
this->findProblems(); this->findProblems();
log::popNest(); log::popNest();
m_loadingState = LoadingState::Done; m_loadingState = LoadingState::Done;
{
auto end = std::chrono::high_resolution_clock::now();
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - m_timerBegin).count();
log::info("Took {}s", static_cast<float>(time) / 1000.f);
}
break; break;
default: default:
m_loadingState = LoadingState::Done; m_loadingState = LoadingState::Done;
@ -660,10 +669,6 @@ void Loader::Impl::continueRefreshModGraph() {
break; break;
} }
auto end = std::chrono::high_resolution_clock::now();
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
log::info("Took {}s", static_cast<float>(time) / 1000.f);
if (m_loadingState != LoadingState::Done) { if (m_loadingState != LoadingState::Done) {
queueInMainThread([&]() { queueInMainThread([&]() {
this->continueRefreshModGraph(); this->continueRefreshModGraph();

View file

@ -87,6 +87,9 @@ namespace geode {
int m_refreshingModCount = 0; int m_refreshingModCount = 0;
int m_refreshedModCount = 0; int m_refreshedModCount = 0;
int m_lateRefreshedModCount = 0;
std::chrono::time_point<std::chrono::high_resolution_clock> m_timerBegin;
void provideNextMod(Mod* mod); void provideNextMod(Mod* mod);
Mod* takeNextMod(); Mod* takeNextMod();

View file

@ -3,6 +3,7 @@
#include "ModMetadataImpl.hpp" #include "ModMetadataImpl.hpp"
#include "about.hpp" #include "about.hpp"
#include <hash/hash.hpp>
#include <Geode/loader/Dirs.hpp> #include <Geode/loader/Dirs.hpp>
#include <Geode/loader/Hook.hpp> #include <Geode/loader/Hook.hpp>
#include <Geode/loader/Loader.hpp> #include <Geode/loader/Loader.hpp>
@ -576,13 +577,38 @@ Result<> Mod::Impl::createTempDir() {
Result<> Mod::Impl::unzipGeodeFile(ModMetadata metadata) { Result<> Mod::Impl::unzipGeodeFile(ModMetadata metadata) {
// Unzip .geode file into temp dir // Unzip .geode file into temp dir
auto tempDir = dirs::getModRuntimeDir() / metadata.getID();
auto datePath = tempDir / "modified-at";
std::string currentHash = file::readString(datePath).unwrapOr("");
auto modifiedDate = ghc::filesystem::last_write_time(metadata.getPath());
auto modifiedCount = std::chrono::duration_cast<std::chrono::milliseconds>(modifiedDate.time_since_epoch());
auto modifiedHash = std::to_string(modifiedCount.count());
if (currentHash == modifiedHash) {
log::debug("Same hash detected, skipping unzip");
return Ok();
}
log::debug("Hash mismatch detected, unzipping");
std::error_code ec;
ghc::filesystem::remove_all(tempDir, ec);
if (ec) {
return Err("Unable to delete temp dir: " + ec.message());
}
auto res = file::writeString(datePath, modifiedHash);
if (!res) {
log::warn("Failed to write modified date of geode zip");
}
GEODE_UNWRAP_INTO(auto unzip, file::Unzip::create(metadata.getPath())); GEODE_UNWRAP_INTO(auto unzip, file::Unzip::create(metadata.getPath()));
if (!unzip.hasEntry(metadata.getBinaryName())) { if (!unzip.hasEntry(metadata.getBinaryName())) {
return Err( return Err(
fmt::format("Unable to find platform binary under the name \"{}\"", metadata.getBinaryName()) fmt::format("Unable to find platform binary under the name \"{}\"", metadata.getBinaryName())
); );
} }
GEODE_UNWRAP(unzip.extractAllTo(dirs::getModRuntimeDir() / metadata.getID())); GEODE_UNWRAP(unzip.extractAllTo(tempDir));
return Ok(); return Ok();
} }