game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
proton_cache.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// proton_cache.cpp
4// ==============================================================================
5// Implementation of Proton version caching
6// ==============================================================================
7
8#include "proton_cache.h"
9#include "logger.h"
10#include "steam_constants.h"
11#include "steam_integration.h"
12#include <algorithm>
13
14// ==============================================================================
15// Static Member Initialization
16// ==============================================================================
17
18std::vector<std::pair<std::string, std::filesystem::path>> ProtonCache::cachedVersions_;
19// Default time_point ctor is constexpr noexcept ([time.point.ctor]/1).
20// NOLINTNEXTLINE(bugprone-throwing-static-initialization) - see comment above.
21std::chrono::system_clock::time_point ProtonCache::cacheTime_;
22std::mutex ProtonCache::cacheMutex_;
23bool ProtonCache::cacheInitialized_ = false;
24
25// ==============================================================================
26// Cache Access
27// ==============================================================================
28
29std::vector<std::pair<std::string, std::filesystem::path>> ProtonCache::getVersions()
30{
31 std::lock_guard<std::mutex> lock(cacheMutex_);
32
33 // Check if cache needs update
34 if (!cacheInitialized_ || isCacheExpired())
35 {
36 updateCache();
37 }
38
39 return cachedVersions_;
40}
41
42std::vector<std::string> ProtonCache::getSortedVersionNames()
43{
44 auto versions = getVersions();
45
46 std::vector<std::string> names;
47 names.reserve(versions.size());
48
49 for (const auto& [name, path] : versions)
50 {
51 names.push_back(name);
52 }
53
54 // Sort with CachyOS first, then Experimental, then by version number
55 std::sort(names.begin(), names.end(),
56 [](const std::string& a, const std::string& b)
57 {
58 bool aCachy = (a.find("cachyos") != std::string::npos ||
59 a.find("CachyOS") != std::string::npos);
60 bool bCachy = (b.find("cachyos") != std::string::npos ||
61 b.find("CachyOS") != std::string::npos);
62 bool aExperimental = (a.find("Experimental") != std::string::npos);
63 bool bExperimental = (b.find("Experimental") != std::string::npos);
64
65 // CachyOS versions first
66 if (aCachy && !bCachy)
67 return true;
68 if (!aCachy && bCachy)
69 return false;
70
71 // Then Experimental
72 if (aExperimental && !bExperimental)
73 return true;
74 if (!aExperimental && bExperimental)
75 return false;
76
77 // Otherwise alphabetical
78 return a < b;
79 });
80
81 return names;
82}
83
84// ==============================================================================
85// Cache Management
86// ==============================================================================
87
89{
90 std::lock_guard<std::mutex> lock(cacheMutex_);
91 cacheInitialized_ = false;
92 cachedVersions_.clear();
93 Logger::info("[ProtonCache] Cache invalidated");
94}
95
97{
98 std::lock_guard<std::mutex> lock(cacheMutex_);
99 return cacheInitialized_ && !isCacheExpired();
100}
101
103{
104 std::lock_guard<std::mutex> lock(cacheMutex_);
105
106 if (!cacheInitialized_)
107 {
108 return -1;
109 }
110
111 auto now = std::chrono::system_clock::now();
112 auto age = std::chrono::duration_cast<std::chrono::seconds>(now - cacheTime_);
113 return static_cast<int>(age.count());
114}
115
116// ==============================================================================
117// Internal Helpers
118// ==============================================================================
119
120void ProtonCache::updateCache()
121{
122 Logger::info("[ProtonCache] Scanning for Proton versions...");
123
124 // Call the actual scanner (from steam_integration.cpp)
125 cachedVersions_ = SteamLibraryScanner::scanProtonVersions();
126
127 // Update cache metadata
128 cacheTime_ = std::chrono::system_clock::now();
129 cacheInitialized_ = true;
130
131 Logger::info("[ProtonCache] Found {} Proton versions (cached for {} seconds)",
132 cachedVersions_.size(), SteamConstants::PROTON_CACHE_VALIDITY_SEC);
133}
134
135bool ProtonCache::isCacheExpired()
136{
137 if (!cacheInitialized_)
138 {
139 return true;
140 }
141
142 auto now = std::chrono::system_clock::now();
143 auto age = std::chrono::duration_cast<std::chrono::seconds>(now - cacheTime_);
144
145 return age.count() >= SteamConstants::PROTON_CACHE_VALIDITY_SEC;
146}
static void info(const std::string &message, Args &&... args)
Log info message (general information).
Definition logger.h:108
static int getCacheAge()
Get cache age in seconds.
static std::vector< std::string > getSortedVersionNames()
Get sorted Proton version names only.
static std::vector< std::pair< std::string, std::filesystem::path > > getVersions()
Get cached Proton versions.
static void invalidate()
Invalidate the cache.
static bool isValid()
Check if cache is currently valid.
static std::vector< std::pair< std::string, std::filesystem::path > > scanProtonVersions()
Scan for all installed Proton versions.
constexpr int PROTON_CACHE_VALIDITY_SEC
Validity period for Proton version cache (seconds).