mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-07-11 07:36:21 -07:00
Updated SpoilerFileExists to cache the results. (#5606)
This commit is contained in:
parent
17613d1f50
commit
afde504a0f
1 changed files with 55 additions and 9 deletions
|
@ -359,26 +359,72 @@ std::unordered_map<s16, s16> getItemIdToItemId = {
|
||||||
#pragma GCC push_options
|
#pragma GCC push_options
|
||||||
#pragma GCC optimize("O0")
|
#pragma GCC optimize("O0")
|
||||||
bool Randomizer::SpoilerFileExists(const char* spoilerFileName) {
|
bool Randomizer::SpoilerFileExists(const char* spoilerFileName) {
|
||||||
if (strcmp(spoilerFileName, "") != 0) {
|
static std::unordered_map<std::string, bool> existsCache;
|
||||||
std::ifstream spoilerFileStream(SohUtils::Sanitize(spoilerFileName));
|
static std::unordered_map<std::string, std::filesystem::file_time_type> lastModifiedCache;
|
||||||
|
|
||||||
|
if (strcmp(spoilerFileName, "") == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string sanitizedFileName = SohUtils::Sanitize(spoilerFileName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Check if file exists and get last modified time
|
||||||
|
std::filesystem::path filePath(sanitizedFileName);
|
||||||
|
if (!std::filesystem::exists(filePath)) {
|
||||||
|
// Cache and return false if file doesn't exist
|
||||||
|
existsCache[sanitizedFileName] = false;
|
||||||
|
lastModifiedCache.erase(sanitizedFileName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto currentLastModified = std::filesystem::last_write_time(filePath);
|
||||||
|
|
||||||
|
// Check cache first
|
||||||
|
auto existsCacheIt = existsCache.find(sanitizedFileName);
|
||||||
|
auto lastModifiedCacheIt = lastModifiedCache.find(sanitizedFileName);
|
||||||
|
|
||||||
|
// If we have a valid cache entry and the file hasn't been modified
|
||||||
|
if (existsCacheIt != existsCache.end() && lastModifiedCacheIt != lastModifiedCache.end() &&
|
||||||
|
lastModifiedCacheIt->second == currentLastModified) {
|
||||||
|
return existsCacheIt->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache miss or file modified - need to check contents
|
||||||
|
std::ifstream spoilerFileStream(sanitizedFileName);
|
||||||
if (spoilerFileStream) {
|
if (spoilerFileStream) {
|
||||||
nlohmann::json contents;
|
nlohmann::json contents;
|
||||||
spoilerFileStream >> contents;
|
spoilerFileStream >> contents;
|
||||||
spoilerFileStream.close();
|
spoilerFileStream.close();
|
||||||
if (contents.contains("version") &&
|
|
||||||
strcmp(std::string(contents["version"]).c_str(), (char*)gBuildVersion) == 0) {
|
bool isValid = contents.contains("version") &&
|
||||||
return true;
|
strcmp(std::string(contents["version"]).c_str(), (char*)gBuildVersion) == 0;
|
||||||
} else {
|
|
||||||
|
if (!isValid) {
|
||||||
SohGui::RegisterPopup(
|
SohGui::RegisterPopup(
|
||||||
"Old Spoiler Version",
|
"Old Spoiler Version",
|
||||||
"The spoiler file located at\n" + std::string(spoilerFileName) +
|
"The spoiler file located at\n" + std::string(spoilerFileName) +
|
||||||
"\nwas made by a version that doesn't match the currently running version.\n" +
|
"\nwas made by a version that doesn't match the currently running version.\n" +
|
||||||
"Loading for this file has been cancelled.");
|
"Loading for this file has been cancelled.");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
// Update cache
|
||||||
|
existsCache[sanitizedFileName] = isValid;
|
||||||
|
lastModifiedCache[sanitizedFileName] = currentLastModified;
|
||||||
|
return isValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
// File couldn't be opened
|
||||||
|
existsCache[sanitizedFileName] = false;
|
||||||
|
lastModifiedCache.erase(sanitizedFileName);
|
||||||
|
return false;
|
||||||
|
|
||||||
|
} catch (const std::filesystem::filesystem_error&) {
|
||||||
|
// Handle filesystem errors by invalidating cache
|
||||||
|
existsCache[sanitizedFileName] = false;
|
||||||
|
lastModifiedCache.erase(sanitizedFileName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#pragma GCC pop_options
|
#pragma GCC pop_options
|
||||||
#pragma optimize("", on)
|
#pragma optimize("", on)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue