Use cached sizes if the feature is enabled

This commit is contained in:
kalaposfos13 2025-01-12 10:17:12 +01:00
parent 91fc8e38d1
commit 32f0b60105

View File

@ -62,15 +62,46 @@ public:
QDir dir(dirPath); QDir dir(dirPath);
QDirIterator it(dir.absolutePath(), QDirIterator::Subdirectories); QDirIterator it(dir.absolutePath(), QDirIterator::Subdirectories);
qint64 total = 0; qint64 total = 0;
if (!Config::GetLoadGameSizeEnabled()) { if (!Config::GetLoadGameSizeEnabled()) {
game.size = FormatSize(0).toStdString(); game.size = FormatSize(0).toStdString();
return; return;
} }
// Cache path
QFile size_cache_file(Common::FS::GetUserPath(Common::FS::PathType::MetaDataDir) /
game.serial / "size_cache.txt");
QFileInfo cacheInfo(size_cache_file);
QFileInfo dirInfo(dirPath);
// Check if cache file exists and is valid
if (size_cache_file.exists() && cacheInfo.lastModified() >= dirInfo.lastModified()) {
if (size_cache_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&size_cache_file);
QString cachedSize = in.readLine();
size_cache_file.close();
if (!cachedSize.isEmpty()) {
game.size = cachedSize.toStdString();
return;
}
}
}
// Cache is invalid or does not exist; calculate size
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
total += it.fileInfo().size(); total += it.fileInfo().size();
} }
game.size = FormatSize(total).toStdString(); game.size = FormatSize(total).toStdString();
// Save new cache
if (size_cache_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&size_cache_file);
out << QString::fromStdString(game.size) << "\n";
size_cache_file.close();
}
} }
static QString GetRegion(char region) { static QString GetRegion(char region) {