26 return std::filesystem::exists(path, ec);
32 return std::filesystem::is_directory(path, ec);
38 std::filesystem::create_directories(path, ec);
44 const char* home = std::getenv(
"HOME");
47 return std::filesystem::path(home);
54 const char* xdgCache = std::getenv(
"XDG_CACHE_HOME");
57 return std::filesystem::path(xdgCache);
64 const char* xdgData = std::getenv(
"XDG_DATA_HOME");
67 return std::filesystem::path(xdgData);
74 const char* units[] = {
"B",
"KB",
"MB",
"GB"};
76 double size =
static_cast<double>(bytes);
78 while (size >= 1024 && unitIndex < 3)
85 snprintf(buffer,
sizeof(buffer),
"%.1f %s", size, units[unitIndex]);
89std::chrono::system_clock::time_point
getModTime(
const std::filesystem::path& path)
92 auto ftime = std::filesystem::last_write_time(path, ec);
95 return std::chrono::system_clock::now();
97 auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
98 ftime - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now());
110 return formatBytes(
size);
154 return "Mesa (AMD/Intel)";
160 return "VKD3D-Proton";
162 return "Steam Shader Cache";
188 backupDir = getXDGDataHome() /
"gamelauncher" /
"shader_cache_backups";
192 auto home = getHomeDir();
214 std::lock_guard<std::mutex> lock(impl_->mutex);
215 return impl_->caches;
220 std::lock_guard<std::mutex> lock(impl_->mutex);
221 std::vector<ShaderCacheInfo> result;
223 for (
const auto& cache : impl_->caches)
227 result.push_back(cache);
234std::optional<ShaderCacheInfo>
237 if (!fileExists(path))
249 info.
type = *cacheType;
252 info.
size = calculateDirectorySize(path);
262 std::lock_guard<std::mutex> lock(impl_->mutex);
263 impl_->caches.clear();
268 if (progressCallback)
269 progressCallback((step * 100) / totalSteps,
"Scanning Mesa caches...");
273 if (progressCallback)
274 progressCallback((step * 100) / totalSteps,
"Scanning NVIDIA caches...");
278 if (progressCallback)
279 progressCallback((step * 100) / totalSteps,
"Scanning DXVK caches...");
283 if (progressCallback)
284 progressCallback((step * 100) / totalSteps,
"Scanning VKD3D caches...");
288 if (progressCallback)
289 progressCallback((step * 100) / totalSteps,
"Scanning Steam shader caches...");
293 if (progressCallback)
294 progressCallback((step * 100) / totalSteps,
"Scanning Fossilize caches...");
295 scanFossilizeCaches();
298 if (progressCallback)
299 progressCallback((step * 100) / totalSteps,
"Scanning Wine state caches...");
300 scanWineStateCaches();
302 if (progressCallback)
303 progressCallback(100,
"Scan complete");
305 return static_cast<int>(impl_->caches.size());
313 const std::filesystem::path& winePrefix,
314 uint32_t steamAppId)
const
320 if (!winePrefix.empty())
325 result.
caches.push_back(*dxvk);
333 result.
caches.push_back(*vkd3d);
345 result.
caches.push_back(*steam);
354std::optional<ShaderCacheInfo>
358 std::filesystem::path cachePath = winePrefix /
"drive_c" /
"dxvk_cache";
361 std::vector<std::filesystem::path> searchPaths = {
363 winePrefix /
"drive_c" /
"users" /
"steamuser" /
"Local Settings" /
"Application Data",
366 for (
const auto& path : searchPaths)
368 if (fileExists(path))
376 for (
const auto& entry : std::filesystem::recursive_directory_iterator(winePrefix, ec))
378 if (entry.is_regular_file())
380 std::string filename = entry.path().filename().string();
381 if (filename.find(
".dxvk-cache") != std::string::npos)
385 info.
name =
"DXVK Cache";
386 info.
path = entry.path().parent_path();
387 info.
size = calculateDirectorySize(info.
path);
397 Logger::warn(
"ShaderCacheManager: error scanning {} for DXVK cache: {}",
398 winePrefix.string(), ec.message());
404std::optional<ShaderCacheInfo>
408 std::filesystem::path cachePath = winePrefix /
"vkd3d-proton";
410 if (fileExists(cachePath))
416 std::filesystem::path xdgPath = getXDGCacheHome() /
"vkd3d-proton";
417 if (fileExists(xdgPath))
427 for (
const auto& steamPath : impl_->steamLibraryPaths)
429 std::filesystem::path shaderPath =
430 steamPath /
"steamapps" /
"shadercache" / std::to_string(steamAppId);
432 if (fileExists(shaderPath))
436 info.
name =
"Steam Shader Cache (" + std::to_string(steamAppId) +
")";
437 info.
path = shaderPath;
438 info.
size = calculateDirectorySize(shaderPath);
455 std::lock_guard<std::mutex> lock(impl_->mutex);
458 for (
const auto& cache : impl_->caches)
504 for (
const auto& cache : caches)
519 if (!fileExists(cachePath))
521 result.
errors.push_back(
"Cache path does not exist: " + cachePath.string());
527 result.
bytesCleared = calculateDirectorySize(cachePath);
531 if (isDirectory(cachePath))
534 for (
const auto& entry : std::filesystem::directory_iterator(cachePath, ec))
536 std::filesystem::remove_all(entry.path(), ec);
539 result.
errors.push_back(
"Failed to remove: " + entry.path().string());
546 std::filesystem::remove(cachePath, ec);
549 result.
errors.push_back(
"Failed to remove: " + cachePath.string());
558 std::lock_guard<std::mutex> lock(impl_->mutex);
559 impl_->caches.erase(std::remove_if(impl_->caches.begin(), impl_->caches.end(),
561 { return c.path == cachePath; }),
562 impl_->caches.end());
574 for (
size_t i = 0; i < caches.size(); ++i)
576 if (progressCallback)
578 int percent =
static_cast<int>(((i + 1) * 100) / caches.size());
579 progressCallback(percent,
"Clearing: " + caches[i].path.string());
586 totalResult.
errors.insert(totalResult.
errors.end(), result.errors.begin(),
587 result.errors.end());
592 if (progressCallback)
594 progressCallback(100,
"Complete");
601 const std::filesystem::path& winePrefix,
604 auto gameCache =
getGameCache(gameId, winePrefix, steamAppId);
607 for (
const auto& cache : gameCache.caches)
618 totalResult.
errors.insert(totalResult.
errors.end(), result.errors.begin(),
619 result.errors.end());
631 for (
size_t i = 0; i < caches.size(); ++i)
633 if (progressCallback)
635 int percent =
static_cast<int>(((i + 1) * 100) / caches.size());
636 progressCallback(percent,
"Clearing: " + caches[i].path.string());
643 totalResult.
errors.insert(totalResult.
errors.end(), result.errors.begin(),
644 result.errors.end());
649 if (progressCallback)
651 progressCallback(100,
"All caches cleared");
661 auto now = std::chrono::system_clock::now();
663 size_t processed = 0;
665 for (
const auto& cache : caches)
667 auto age = std::chrono::duration_cast<std::chrono::hours>(now - cache.lastModified);
670 if (progressCallback)
672 int percent =
static_cast<int>((processed * 100) / caches.size());
673 progressCallback(percent,
"Clearing stale: " + cache.path.string());
680 totalResult.
errors.insert(totalResult.
errors.end(), result.errors.begin(),
681 result.errors.end());
688 if (progressCallback)
690 progressCallback(100,
"Stale caches cleared");
701 const std::filesystem::path& backupDir)
703 if (!fileExists(cachePath))
708 std::filesystem::path destDir = backupDir.empty() ? impl_->backupDir : backupDir;
709 createDirectories(destDir);
712 auto now = std::chrono::system_clock::now();
713 auto time = std::chrono::system_clock::to_time_t(now);
716 localtime_r(&time, &tm);
717 std::strftime(timestamp,
sizeof(timestamp),
"%Y%m%d_%H%M%S", &tm);
719 std::string backupName = cachePath.filename().string() +
"_" + timestamp;
720 std::filesystem::path backupPath = destDir / backupName;
725 std::filesystem::copy(cachePath, backupPath,
726 std::filesystem::copy_options::recursive |
727 std::filesystem::copy_options::skip_symlinks,
739 const std::filesystem::path& targetPath)
741 if (!fileExists(backupPath))
747 if (fileExists(targetPath))
752 createDirectories(targetPath.parent_path());
757 std::filesystem::copy(backupPath, targetPath,
758 std::filesystem::copy_options::recursive |
759 std::filesystem::copy_options::skip_symlinks,
767 std::vector<std::filesystem::path> backups;
769 if (!fileExists(impl_->backupDir))
775 for (
const auto& entry : std::filesystem::directory_iterator(impl_->backupDir, ec))
777 if (entry.is_directory())
779 backups.push_back(entry.path());
784 Logger::warn(
"ShaderCacheManager: error listing backups in {}: {}",
785 impl_->backupDir.string(), ec.message());
789 std::sort(backups.begin(), backups.end(),
790 [](
const auto& a,
const auto& b) { return getModTime(a) > getModTime(b); });
800 if (
static_cast<int>(backups.size()) <= keepCount)
806 for (
size_t i =
static_cast<size_t>(keepCount); i < backups.size(); ++i)
809 std::filesystem::remove_all(backups[i], ec);
826 std::lock_guard<std::mutex> lock(impl_->mutex);
827 impl_->backupDir = path;
829 createDirectories(path);
834 return impl_->backupDir;
840 std::lock_guard<std::mutex> lock(impl_->mutex);
841 impl_->customLocations.emplace_back(path, type);
846 std::lock_guard<std::mutex> lock(impl_->mutex);
847 impl_->steamLibraryPaths = paths;
854std::optional<ShaderCacheType>
857 std::string pathStr = path.string();
858 std::string filename = path.filename().string();
861 if (pathStr.find(
"mesa_shader_cache") != std::string::npos ||
862 pathStr.find(
"GLCache") != std::string::npos ||
863 pathStr.find(
"shader_cache") != std::string::npos)
868 if (pathStr.find(
"nvidia") != std::string::npos ||
869 pathStr.find(
"NVIDIA") != std::string::npos || pathStr.find(
"GLCache") != std::string::npos)
874 if (pathStr.find(
"dxvk") != std::string::npos ||
875 filename.find(
".dxvk-cache") != std::string::npos)
880 if (pathStr.find(
"vkd3d") != std::string::npos)
885 if (pathStr.find(
"shadercache") != std::string::npos ||
886 pathStr.find(
"shader_cache") != std::string::npos)
889 if (pathStr.find(
"Steam") != std::string::npos ||
890 pathStr.find(
"steam") != std::string::npos)
896 if (pathStr.find(
"fossilize") != std::string::npos)
901 if (pathStr.find(
"wine") != std::string::npos || pathStr.find(
"proton") != std::string::npos)
903 if (pathStr.find(
"state") != std::string::npos)
912std::vector<std::filesystem::path>
915 std::vector<std::filesystem::path> paths;
916 auto home = getHomeDir();
917 auto cache = getXDGCacheHome();
922 paths.push_back(cache /
"mesa_shader_cache");
923 paths.push_back(cache /
"mesa_shader_cache_db");
924 paths.push_back(home /
".cache" /
"mesa_shader_cache");
928 paths.push_back(cache /
"nvidia" /
"GLCache");
929 paths.push_back(home /
".nv" /
"GLCache");
937 paths.push_back(cache /
"vkd3d-proton");
941 for (
const auto& steamPath : impl_->steamLibraryPaths)
943 paths.push_back(steamPath /
"steamapps" /
"shadercache");
948 paths.push_back(cache /
"fossilize");
949 for (
const auto& steamPath : impl_->steamLibraryPaths)
951 paths.push_back(steamPath /
"steamapps" /
"shadercache" /
"fozpipelinesv6");
970void ShaderCacheManager::scanMesaCaches()
973 for (
const auto& path : locations)
975 if (fileExists(path))
979 info.
name =
"Mesa Shader Cache";
981 info.
size = calculateDirectorySize(path);
985 impl_->caches.push_back(info);
990void ShaderCacheManager::scanNVIDIACaches()
993 for (
const auto& path : locations)
997 ShaderCacheInfo info;
999 info.
name =
"NVIDIA GL Cache";
1001 info.
size = calculateDirectorySize(path);
1005 impl_->caches.push_back(info);
1010void ShaderCacheManager::scanDXVKCaches()
1014 std::vector<std::filesystem::path> prefixLocations = {
1017 home /
".local" /
"share" /
"Steam" /
"steamapps" /
"compatdata",
1018 home /
".steam" /
"steam" /
"steamapps" /
"compatdata",
1021 for (
const auto& prefixRoot : prefixLocations)
1027 for (
const auto& entry : std::filesystem::directory_iterator(prefixRoot, ec))
1029 if (!entry.is_directory())
1035 impl_->caches.push_back(*dxvk);
1041void ShaderCacheManager::scanVKD3DCaches()
1044 for (
const auto& path : locations)
1048 ShaderCacheInfo info;
1050 info.
name =
"VKD3D-Proton Cache";
1052 info.
size = calculateDirectorySize(path);
1056 impl_->caches.push_back(info);
1061void ShaderCacheManager::scanSteamCaches()
1063 for (
const auto& steamPath : impl_->steamLibraryPaths)
1065 std::filesystem::path shaderDir = steamPath /
"steamapps" /
"shadercache";
1070 for (
const auto& entry : std::filesystem::directory_iterator(shaderDir, ec))
1072 if (!entry.is_directory())
1075 ShaderCacheInfo info;
1077 info.
name =
"Steam Shader Cache (" + entry.path().filename().string() +
")";
1078 info.
path = entry.path();
1079 info.
size = calculateDirectorySize(entry.path());
1080 info.
fileCount = countFiles(entry.path());
1083 impl_->caches.push_back(info);
1088void ShaderCacheManager::scanFossilizeCaches()
1091 for (
const auto& path : locations)
1095 ShaderCacheInfo info;
1097 info.
name =
"Fossilize Pipeline Cache";
1099 info.
size = calculateDirectorySize(path);
1103 impl_->caches.push_back(info);
1108void ShaderCacheManager::scanWineStateCaches()
1112 std::vector<std::filesystem::path> prefixLocations = {
1117 for (
const auto& prefixRoot : prefixLocations)
1123 std::filesystem::path statePath = prefixRoot /
"drive_c" /
"windows" /
"syswow64";
1132int64_t ShaderCacheManager::calculateDirectorySize(
const std::filesystem::path& path)
const
1140 std::error_code typeEc;
1141 if (std::filesystem::is_regular_file(path, typeEc))
1143 std::error_code sizeEc;
1144 return static_cast<int64_t
>(std::filesystem::file_size(path, sizeEc));
1148 std::error_code iterEc;
1149 for (
const auto& entry : std::filesystem::recursive_directory_iterator(path, iterEc))
1151 std::error_code fileTypeEc;
1152 if (entry.is_regular_file(fileTypeEc))
1154 std::error_code fileSizeEc;
1155 const auto bytes = entry.file_size(fileSizeEc);
1158 size +=
static_cast<int64_t
>(bytes);
1166int ShaderCacheManager::countFiles(
const std::filesystem::path& path)
const
1169 std::error_code typeEc;
1170 if (std::filesystem::is_regular_file(path, typeEc))
1176 std::error_code iterEc;
1177 for (
const auto& entry : std::filesystem::recursive_directory_iterator(path, iterEc))
1179 std::error_code fileTypeEc;
1180 if (entry.is_regular_file(fileTypeEc))
static void warn(const std::string &message, Args &&... args)
Log warning message (non-critical issue).
std::optional< ShaderCacheInfo > findSteamShaderCache(uint32_t steamAppId) const
Find Steam shader cache for a game.
std::vector< ShaderCacheInfo > getCachesByType(ShaderCacheType type) const
Get caches by type.
std::filesystem::path backupCache(const std::filesystem::path &cachePath, const std::filesystem::path &backupDir={})
Backup a shader cache before clearing.
std::filesystem::path getBackupDirectory() const
Get the backup directory.
void setBackupDirectory(const std::filesystem::path &path)
Set the backup directory.
std::optional< ShaderCacheType > identifyCacheType(const std::filesystem::path &path) const
Check if a path is a valid shader cache.
CacheClearResult clearCache(const std::filesystem::path &cachePath)
Clear a specific cache.
int scanCaches(const ProgressCallback &progressCallback=nullptr)
Scan for all shader caches on the system.
int64_t getTotalCacheSize() const
Get total cache size.
void addCustomCacheLocation(const std::filesystem::path &path, ShaderCacheType type)
Add a custom cache location to scan.
std::vector< std::filesystem::path > getDefaultCacheLocations(ShaderCacheType type) const
Get the default cache location for a type.
void setSteamLibraryPaths(const std::vector< std::filesystem::path > &paths)
Set Steam library paths.
ShaderCacheStats getStats() const
Get overall shader cache statistics.
CacheClearResult clearCachesByType(ShaderCacheType type, const ProgressCallback &progressCallback=nullptr)
Clear all caches of a specific type.
CacheClearResult clearAllCaches(const ProgressCallback &progressCallback=nullptr)
Clear all shader caches (use with caution!).
std::optional< ShaderCacheInfo > findDXVKCache(const std::filesystem::path &winePrefix) const
Find DXVK cache for a game.
std::vector< std::filesystem::path > getCacheBackups() const
Get list of cache backups.
std::optional< ShaderCacheInfo > findVKD3DCache(const std::filesystem::path &winePrefix) const
Find VKD3D cache for a game.
GameShaderCache getGameCache(const std::string &gameId, const std::filesystem::path &winePrefix={}, uint32_t steamAppId=0) const
Get shader caches for a specific game.
int64_t getCacheSizeByType(ShaderCacheType type) const
Get cache size by type.
int pruneBackups(int keepCount=3)
Delete old cache backups.
std::function< void(int percent, const std::string &status)> ProgressCallback
std::optional< ShaderCacheInfo > getCacheInfo(const std::filesystem::path &path) const
Get cache info for a specific path.
std::vector< ShaderCacheInfo > getAllCaches() const
Get all detected shader caches.
CacheClearResult clearGameCache(const std::string &gameId, ShaderCacheType type=ShaderCacheType::All, const std::filesystem::path &winePrefix={}, uint32_t steamAppId=0)
Clear shader cache for a specific game.
bool restoreCache(const std::filesystem::path &backupPath, const std::filesystem::path &targetPath)
Restore a shader cache from backup.
CacheClearResult clearStaleCaches(std::chrono::hours olderThan=std::chrono::hours(720), const ProgressCallback &progressCallback=nullptr)
Clear old/stale caches.
std::chrono::system_clock::time_point getModTime(const std::filesystem::path &path)
bool isDirectory(const std::filesystem::path &path)
std::filesystem::path getHomeDir()
std::filesystem::path getXDGDataHome()
std::string formatBytes(int64_t bytes)
bool fileExists(const std::filesystem::path &path)
bool createDirectories(const std::filesystem::path &path)
std::filesystem::path getXDGCacheHome()
bool fileExists(const std::filesystem::path &path)
std::string shaderCacheTypeName(ShaderCacheType type)
Get human-readable cache type name.
ShaderCacheType
Types of shader caches.
@ WineState
Wine/Proton state cache.
@ Fossilize
Fossilize pipeline cache.
@ Mesa
Mesa (AMD/Intel) GLCache and shader_cache.
@ NVIDIA
NVIDIA proprietary driver cache.
@ DXVK
DXVK Vulkan-based D3D implementation.
@ VKD3D
VKD3D-Proton D3D12 implementation.
@ SteamShader
Steam's shader pre-caching.
std::string shaderCacheTypeName(ShaderCacheType type)
Get human-readable cache type name.
Result of a cache clearing operation.
std::vector< std::string > errors
std::string getFormattedBytesCleared() const
Shader cache information for a specific game.
std::string getFormattedTotalSize() const
std::vector< ShaderCacheInfo > caches
Information about a shader cache.
bool exists
Whether the cache exists.
std::string name
Human-readable name.
int64_t size
Size in bytes.
int fileCount
Number of cache files.
std::string getTypeName() const
std::chrono::system_clock::time_point lastModified
std::string getFormattedSize() const
std::filesystem::path path
Path to cache directory/file.
std::vector< std::filesystem::path > steamLibraryPaths
std::filesystem::path backupDir
std::vector< std::pair< std::filesystem::path, ShaderCacheType > > customLocations
std::vector< ShaderCacheInfo > caches
Overall shader cache statistics.
std::string getFormattedTotalSize() const