game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
steam_paths.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// steam_paths.cpp
4// ==============================================================================
5// Implementation of centralized Steam path management
6// ==============================================================================
7
8#include "steam_paths.h"
9#include "steam_constants.h"
10#include <cstdlib>
11
12// ==============================================================================
13// Static Member Initialization
14// ==============================================================================
15
16std::filesystem::path SteamPaths::cachedSteamRoot_;
17std::filesystem::path SteamPaths::cachedHomeDir_;
18bool SteamPaths::cacheInitialized_ = false;
19std::mutex SteamPaths::cacheMutex_;
20
21// ==============================================================================
22// Core Directory Resolution
23// ==============================================================================
24
25std::filesystem::path SteamPaths::getHomeDirectory()
26{
27 std::lock_guard<std::mutex> lock(cacheMutex_);
28
29 if (!cachedHomeDir_.empty())
30 {
31 return cachedHomeDir_;
32 }
33
34 const char* home = std::getenv("HOME");
35 if (!home || home[0] == '\0')
36 {
37 return {};
38 }
39
40 cachedHomeDir_ = std::filesystem::path(home);
41 return cachedHomeDir_;
42}
43
44std::vector<std::filesystem::path> SteamPaths::getAllSteamRoots()
45{
46 std::vector<std::filesystem::path> roots;
47 std::filesystem::path home = getHomeDirectory();
48 if (home.empty())
49 {
50 return roots;
51 }
52
53 // Standard installation
54 std::filesystem::path standardPath = home / SteamConstants::STEAM_ROOT_PATH;
55 if (isValidSteamRoot(standardPath))
56 {
57 roots.push_back(standardPath);
58 }
59
60 // Alternative symlink locations
61 std::filesystem::path alt1 = home / SteamConstants::STEAM_ROOT_ALT1;
62 if (isValidSteamRoot(alt1) && alt1 != standardPath)
63 {
64 roots.push_back(alt1);
65 }
66
67 std::filesystem::path alt2 = home / SteamConstants::STEAM_ROOT_ALT2;
68 if (isValidSteamRoot(alt2) && alt2 != standardPath && alt2 != alt1)
69 {
70 roots.push_back(alt2);
71 }
72
73 // Flatpak installation
74 std::filesystem::path flatpakPath = home / SteamConstants::FLATPAK_STEAM_PATH;
75 if (isValidSteamRoot(flatpakPath))
76 {
77 roots.push_back(flatpakPath);
78 }
79
80 // Snap installation
81 std::filesystem::path snapPath = home / SteamConstants::SNAP_STEAM_PATH;
82 if (isValidSteamRoot(snapPath))
83 {
84 roots.push_back(snapPath);
85 }
86
87 return roots;
88}
89
90std::filesystem::path SteamPaths::getPrimarySteamRoot()
91{
92 std::unique_lock<std::mutex> lock(cacheMutex_);
93
94 if (cacheInitialized_)
95 {
96 return cachedSteamRoot_;
97 }
98
99 // Unlock to call getAllSteamRoots (which may also need the lock)
100 lock.unlock();
101 std::vector<std::filesystem::path> roots = getAllSteamRoots();
102 lock.lock();
103
104 // Re-check after re-acquiring lock — another thread may have initialized
105 if (!cacheInitialized_)
106 {
107 if (!roots.empty())
108 cachedSteamRoot_ = roots[0];
109 cacheInitialized_ = true; // Always mark initialized, even when Steam is absent
110 }
111
112 return cachedSteamRoot_;
113}
114
115// ==============================================================================
116// Configuration Paths
117// ==============================================================================
118
119std::filesystem::path SteamPaths::getUserDataDirectory()
120{
121 std::filesystem::path steamRoot = getPrimarySteamRoot();
122 if (steamRoot.empty())
123 {
124 return {};
125 }
126
127 return steamRoot / SteamConstants::USERDATA_DIR;
128}
129
130std::filesystem::path SteamPaths::getLocalConfigPath(const std::string& userId)
131{
132 std::filesystem::path userDataDir = getUserDataDirectory();
133 if (userDataDir.empty())
134 {
135 return {};
136 }
137
138 return userDataDir / userId / SteamConstants::CONFIG_DIR / SteamConstants::LOCAL_CONFIG_VDF;
139}
140
141std::filesystem::path SteamPaths::getConfigPath()
142{
143 std::filesystem::path steamRoot = getPrimarySteamRoot();
144 if (steamRoot.empty())
145 {
146 return {};
147 }
148
150}
151
152std::filesystem::path SteamPaths::getLoginUsersPath()
153{
154 std::filesystem::path steamRoot = getPrimarySteamRoot();
155 if (steamRoot.empty())
156 {
157 return {};
158 }
159
161}
162
163// ==============================================================================
164// Library & Game Paths
165// ==============================================================================
166
168{
169 std::filesystem::path steamRoot = getPrimarySteamRoot();
170 if (steamRoot.empty())
171 {
172 return {};
173 }
174
176}
177
178std::filesystem::path SteamPaths::getSteamAppsPath(const std::filesystem::path& libraryRoot)
179{
180 return libraryRoot / SteamConstants::STEAMAPPS_DIR;
181}
182
183std::filesystem::path SteamPaths::getAppManifestPath(const std::filesystem::path& libraryRoot,
184 uint32_t appId)
185{
186 std::string filename =
187 std::string(SteamConstants::APPMANIFEST_PREFIX) + std::to_string(appId) + ".acf";
188 return getSteamAppsPath(libraryRoot) / filename;
189}
190
191std::filesystem::path SteamPaths::getCompatDataPath(const std::filesystem::path& libraryRoot,
192 uint32_t appId)
193{
194 return getSteamAppsPath(libraryRoot) / SteamConstants::COMPATDATA_DIR / std::to_string(appId);
195}
196
197std::filesystem::path SteamPaths::getGameInstallPath(const std::filesystem::path& libraryRoot,
198 const std::string& installDir)
199{
200 return getSteamAppsPath(libraryRoot) / SteamConstants::COMMON_DIR / installDir;
201}
202
203// ==============================================================================
204// Compatibility Tools Paths
205// ==============================================================================
206
207std::vector<std::filesystem::path> SteamPaths::getCompatToolsPaths()
208{
209 std::vector<std::filesystem::path> paths;
210
211 // User-installed compatibility tools
212 std::filesystem::path steamRoot = getPrimarySteamRoot();
213 if (!steamRoot.empty())
214 {
215 std::filesystem::path userCompatTools = steamRoot / SteamConstants::COMPAT_TOOLS_DIR;
216 if (std::filesystem::exists(userCompatTools))
217 {
218 paths.push_back(userCompatTools);
219 }
220 }
221
222 // Flatpak compatibility tools
223 if (isFlatpakSteam())
224 {
225 std::filesystem::path home = getHomeDirectory();
226 if (!home.empty())
227 {
228 std::filesystem::path flatpakCompatTools =
230 if (std::filesystem::exists(flatpakCompatTools))
231 {
232 paths.push_back(flatpakCompatTools);
233 }
234 }
235 }
236
237 // Snap compatibility tools
238 if (isSnapSteam())
239 {
240 std::filesystem::path home = getHomeDirectory();
241 if (!home.empty())
242 {
243 std::filesystem::path snapCompatTools =
245 if (std::filesystem::exists(snapCompatTools))
246 {
247 paths.push_back(snapCompatTools);
248 }
249 }
250 }
251
252 // System-wide compatibility tools
253 std::filesystem::path systemPath1(SteamConstants::SYSTEM_COMPAT_TOOLS_PATH1);
254 if (std::filesystem::exists(systemPath1))
255 {
256 paths.push_back(systemPath1);
257 }
258
259 std::filesystem::path systemPath2(SteamConstants::SYSTEM_COMPAT_TOOLS_PATH2);
260 if (std::filesystem::exists(systemPath2))
261 {
262 paths.push_back(systemPath2);
263 }
264
265 return paths;
266}
267
268std::vector<std::filesystem::path> SteamPaths::getProtonInstallPaths()
269{
270 std::vector<std::filesystem::path> paths;
271
272 // Get all Steam library roots
273 std::vector<std::filesystem::path> roots = getAllSteamRoots();
274
275 // Note: Full libraryfolders.vdf parsing is available in SteamLibraryScanner::getLibraryPaths()
276 // This simplified version covers common use cases by checking all known Steam roots
277
278 for (const auto& root : roots)
279 {
280 std::filesystem::path commonDir =
282 if (std::filesystem::exists(commonDir))
283 {
284 paths.push_back(commonDir);
285 }
286 }
287
288 return paths;
289}
290
291// ==============================================================================
292// Installation Type Detection
293// ==============================================================================
294
296{
297 std::filesystem::path home = getHomeDirectory();
298 if (home.empty())
299 {
300 return false;
301 }
302 return isValidSteamRoot(home / SteamConstants::FLATPAK_STEAM_PATH);
303}
304
306{
307 std::filesystem::path home = getHomeDirectory();
308 if (home.empty())
309 {
310 return false;
311 }
312 return isValidSteamRoot(home / SteamConstants::SNAP_STEAM_PATH);
313}
314
316{
317 std::filesystem::path steamRoot = getPrimarySteamRoot();
318 return !steamRoot.empty();
319}
320
321// ==============================================================================
322// Cache Management
323// ==============================================================================
324
326{
327 std::lock_guard<std::mutex> lock(cacheMutex_);
328 cachedSteamRoot_.clear();
329 cachedHomeDir_.clear();
330 cacheInitialized_ = false;
331}
332
334{
335 std::lock_guard<std::mutex> lock(cacheMutex_);
336 return cacheInitialized_;
337}
338
339// ==============================================================================
340// Private Helper Methods
341// ==============================================================================
342
343bool SteamPaths::isValidSteamRoot(const std::filesystem::path& path)
344{
345 if (!std::filesystem::exists(path))
346 {
347 return false;
348 }
349
350 // Check for steamapps directory (definitive marker of Steam installation)
351 std::filesystem::path steamapps = path / SteamConstants::STEAMAPPS_DIR;
352 return std::filesystem::exists(steamapps) && std::filesystem::is_directory(steamapps);
353}
static bool isCacheValid()
Check if path cache is initialized.
static std::vector< std::filesystem::path > getCompatToolsPaths()
Get all compatibility tools directories.
static std::filesystem::path getGameInstallPath(const std::filesystem::path &libraryRoot, const std::string &installDir)
Get path to game's installation directory.
static std::filesystem::path getLocalConfigPath(const std::string &userId)
Get path to user's localconfig.vdf.
static std::filesystem::path getAppManifestPath(const std::filesystem::path &libraryRoot, uint32_t appId)
Get path to an appmanifest file.
static std::filesystem::path getConfigPath()
Get path to global config.vdf.
static std::filesystem::path getHomeDirectory()
Get user's home directory.
static std::filesystem::path getUserDataDirectory()
Get Steam userdata directory.
static std::vector< std::filesystem::path > getAllSteamRoots()
Get all possible Steam installation directories.
static std::filesystem::path getCompatDataPath(const std::filesystem::path &libraryRoot, uint32_t appId)
Get path to game's compatdata directory (Proton prefix).
static bool isFlatpakSteam()
Check if Steam is installed via Flatpak.
static void invalidateCache()
Invalidate cached paths.
static std::filesystem::path getPrimarySteamRoot()
Get primary Steam installation directory.
static std::vector< std::filesystem::path > getProtonInstallPaths()
Get paths where official Proton versions are installed.
static bool isSteamInstalled()
Check if Steam is installed (any method).
static std::filesystem::path getLoginUsersPath()
Get path to loginusers.vdf.
static std::filesystem::path getSteamAppsPath(const std::filesystem::path &libraryRoot)
Get steamapps directory for a library.
static std::filesystem::path getLibraryFoldersPath()
Get path to libraryfolders.vdf.
static bool isSnapSteam()
Check if Steam is installed via Snap.
constexpr const char * APPMANIFEST_PREFIX
Prefix for game manifest files.
constexpr const char * STEAM_ROOT_PATH
Primary Steam installation directory.
constexpr const char * STEAM_ROOT_ALT1
Alternative Steam installation (symlink).
constexpr const char * SNAP_STEAM_PATH
Snap Steam installation path.
constexpr const char * COMPATDATA_DIR
Directory containing Proton compatibility data.
constexpr const char * SYSTEM_COMPAT_TOOLS_PATH1
System compatibility tools directory (distro-provided).
constexpr const char * STEAM_ROOT_ALT2
Alternative Steam installation (legacy).
constexpr const char * SYSTEM_COMPAT_TOOLS_PATH2
Alternative system compatibility tools directory.
constexpr const char * LOGIN_USERS_VDF
File containing user login information.
constexpr const char * LIBRARY_FOLDERS_VDF
File containing library folder paths.
constexpr const char * CONFIG_DIR
Directory containing global Steam configuration.
constexpr const char * LOCAL_CONFIG_VDF
File containing per-user game settings (launch options, playtime).
constexpr const char * COMMON_DIR
Directory containing installed games (within steamapps).
constexpr const char * COMPAT_TOOLS_DIR
Directory containing compatibility tools.
constexpr const char * CONFIG_VDF
File containing global Steam configuration (Proton mappings).
constexpr const char * FLATPAK_STEAM_PATH
Flatpak Steam installation path.
constexpr const char * STEAMAPPS_DIR
Directory containing installed games.
constexpr const char * USERDATA_DIR
Directory containing user-specific data.