Check for and remove cached hashes not in folder

This commit is contained in:
Tixx 2025-03-31 23:55:10 +02:00
parent 7a439bb5b9
commit 0bb18de9f6
No known key found for this signature in database
GPG Key ID: EC6E7A2BAABF0B8C

View File

@ -94,10 +94,12 @@ void TResourceManager::RefreshFiles() {
} }
if (modsDB.contains(entry.path().filename().string())) { if (modsDB.contains(entry.path().filename().string())) {
auto dbEntry = modsDB[entry.path().filename().string()]; auto& dbEntry = modsDB[entry.path().filename().string()];
if (entry.last_write_time().time_since_epoch().count() > dbEntry["lastwrite"] || std::filesystem::file_size(File) != dbEntry["filesize"].get<size_t>()) { if (entry.last_write_time().time_since_epoch().count() > dbEntry["lastwrite"] || std::filesystem::file_size(File) != dbEntry["filesize"].get<size_t>()) {
beammp_infof("File '{}' has been modified, rehashing", File); beammp_infof("File '{}' has been modified, rehashing", File);
} else { } else {
dbEntry["exists"] = true;
mMods.push_back(nlohmann::json { mMods.push_back(nlohmann::json {
{ "file_name", std::filesystem::path(File).filename() }, { "file_name", std::filesystem::path(File).filename() },
{ "file_size", std::filesystem::file_size(File) }, { "file_size", std::filesystem::file_size(File) },
@ -178,7 +180,8 @@ void TResourceManager::RefreshFiles() {
{ "lastwrite", entry.last_write_time().time_since_epoch().count() }, { "lastwrite", entry.last_write_time().time_since_epoch().count() },
{ "hash", result }, { "hash", result },
{ "filesize", std::filesystem::file_size(File) }, { "filesize", std::filesystem::file_size(File) },
{ "protected", false } { "protected", false },
{ "exists", true }
}; };
} catch (const std::exception& e) { } catch (const std::exception& e) {
@ -186,6 +189,15 @@ void TResourceManager::RefreshFiles() {
} }
} }
for (auto it = modsDB.begin(); it != modsDB.end(); ) {
if (!it.value().contains("exists")) {
it = modsDB.erase(it);
} else {
it.value().erase("exists");
++it;
}
}
try { try {
std::ofstream stream(Path + "/mods.json"); std::ofstream stream(Path + "/mods.json");