game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
steam_finder_linux.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#include "platform/paths.h"
3#include "platform/platform.h"
6#include "util/shell_utils.h"
7
8#ifdef PLATFORM_LINUX
9
10#include <cstdlib>
11#include <fstream>
12#include <regex>
13#include <sstream>
14
15namespace platform
16{
17
18class LinuxSteamFinder : public SteamFinder
19{
20 public:
21 std::optional<std::filesystem::path> findSteamRoot() const override
22 {
23 auto paths = Paths::create();
24 std::filesystem::path home = paths->getHomeDir();
25
26 // Check common Steam installation paths
27 std::vector<std::filesystem::path> candidates = {
28 home / ".local" / "share" / "Steam",
29 home / ".steam" / "root",
30 home / ".steam" / "steam",
31 home / ".steam" / "debian-installation",
32 // Flatpak Steam
33 home / ".var" / "app" / "com.valvesoftware.Steam" / ".local" / "share" / "Steam",
34 home / ".var" / "app" / "com.valvesoftware.Steam" / ".steam" / "steam",
35 // Snap Steam
36 home / "snap" / "steam" / "common" / ".local" / "share" / "Steam",
37 };
38
39 for (const auto& path : candidates)
40 {
41 if (std::filesystem::exists(path / "steam.sh") ||
42 std::filesystem::exists(path / "steamapps"))
43 {
44 // Resolve symlinks to get the real path
45 try
46 {
47 return std::filesystem::canonical(path);
48 }
49 catch (...)
50 {
51 return path;
52 }
53 }
54 }
55
56 return std::nullopt;
57 }
58
59 std::vector<SteamLibraryFolder> getLibraryFolders() const override
60 {
61 std::vector<SteamLibraryFolder> folders;
62
63 auto steamRoot = findSteamRoot();
64 if (!steamRoot)
65 return folders;
66
67 // The main Steam folder is always a library folder
68 SteamLibraryFolder mainFolder;
69 mainFolder.path = *steamRoot;
70 mainFolder.label = "Main";
71 folders.push_back(mainFolder);
72
73 // Parse libraryfolders.vdf for additional folders
74 std::filesystem::path vdfPath = *steamRoot / "steamapps" / "libraryfolders.vdf";
75 if (!std::filesystem::exists(vdfPath))
76 {
77 // Try older location
78 vdfPath = *steamRoot / "config" / "libraryfolders.vdf";
79 }
80
81 if (std::filesystem::exists(vdfPath))
82 {
83 std::ifstream file(vdfPath);
84 std::string content((std::istreambuf_iterator<char>(file)),
85 std::istreambuf_iterator<char>());
86
87 // Simple VDF parsing - look for "path" entries
88 std::regex pathRegex(R"re("path"\s+"([^"]+)")re");
89 std::smatch match;
90 std::string::const_iterator searchStart(content.cbegin());
91
92 while (std::regex_search(searchStart, content.cend(), match, pathRegex))
93 {
94 std::filesystem::path libPath = match[1].str();
95 if (std::filesystem::exists(libPath) && libPath != *steamRoot)
96 {
97 SteamLibraryFolder folder;
98 folder.path = libPath;
99 folder.label = libPath.filename().string();
100 folders.push_back(folder);
101 }
102 searchStart = match.suffix().first;
103 }
104 }
105
106 return folders;
107 }
108
109 std::vector<std::filesystem::path> getCompatToolsDirs() const override
110 {
111 std::vector<std::filesystem::path> dirs;
112
113 auto steamRoot = findSteamRoot();
114 auto paths = Paths::create();
115 std::filesystem::path home = paths->getHomeDir();
116
117 // Steam's built-in compatibility tools
118 if (steamRoot)
119 {
120 dirs.push_back(*steamRoot / "steamapps" / "common" / "Proton - Experimental");
121 dirs.push_back(*steamRoot / "steamapps" / "common");
122 dirs.push_back(*steamRoot / "compatibilitytools.d");
123 }
124
125 // User's custom compatibility tools
126 dirs.push_back(home / ".steam" / "root" / "compatibilitytools.d");
127 dirs.push_back(home / ".local" / "share" / "Steam" / "compatibilitytools.d");
128
129 // System-wide compatibility tools
130 dirs.push_back("/usr/share/steam/compatibilitytools.d");
131 dirs.push_back("/opt/steam/compatibilitytools.d");
132
133 // Filter to existing directories
134 std::vector<std::filesystem::path> existing;
135 for (const auto& dir : dirs)
136 {
137 if (std::filesystem::exists(dir))
138 {
139 existing.push_back(dir);
140 }
141 }
142
143 return existing;
144 }
145
146 std::optional<std::filesystem::path> findGameInstall(uint32_t appId) const override
147 {
148 auto libraries = getLibraryFolders();
149
150 for (const auto& lib : libraries)
151 {
152 // Check for appmanifest
153 std::filesystem::path manifestPath =
154 lib.path / "steamapps" / ("appmanifest_" + std::to_string(appId) + ".acf");
155
156 if (std::filesystem::exists(manifestPath))
157 {
158 // Parse manifest to get installdir
159 std::ifstream file(manifestPath);
160 std::string content((std::istreambuf_iterator<char>(file)),
161 std::istreambuf_iterator<char>());
162
163 std::regex installDirRegex(R"re("installdir"\s+"([^"]+)")re");
164 std::smatch match;
165 if (std::regex_search(content, match, installDirRegex))
166 {
167 return lib.path / "steamapps" / "common" / match[1].str();
168 }
169 }
170 }
171
172 return std::nullopt;
173 }
174
175 bool isSteamRunning() const override
176 {
177 auto pm = ProcessManager::create();
178 auto procs = pm->findByName("steam");
179 return !procs.empty();
180 }
181
182 bool startSteam(bool silent) const override
183 {
184 auto pm = ProcessManager::create();
185
186 std::string command = "steam";
187 if (silent)
188 {
189 command += " -silent";
190 }
191
192 auto pid = pm->launchShell(command);
193 return pid.has_value();
194 }
195
196 bool shutdownSteam() const override
197 {
198 auto pm = ProcessManager::create();
199 auto result = pm->executeAndCapture("steam -shutdown", 5000);
200 return result.first == 0;
201 }
202
203 bool launchGame(uint32_t appId) const override
204 {
205 auto pm = ProcessManager::create();
206 std::string command = "steam steam://rungameid/" + std::to_string(appId);
207 auto pid = pm->launchShell(command);
208 return pid.has_value();
209 }
210
211 bool launchGameWithArgs(uint32_t appId, const std::string& args) const override
212 {
213 auto pm = ProcessManager::create();
214 std::string command = "steam -applaunch " + std::to_string(appId);
215 if (!args.empty())
216 {
217 // Escape each whitespace-separated argument to prevent shell injection
218 std::istringstream argStream(args);
219 std::string arg;
220 while (argStream >> arg)
221 {
222 command += " " + ShellUtils::escapeArg(arg);
223 }
224 }
225 auto pid = pm->launchShell(command);
226 return pid.has_value();
227 }
228
229 std::optional<std::filesystem::path> getSteamExecutable() const override
230 {
231 // Try to find steam in PATH
232 auto pm = ProcessManager::create();
233 auto result = pm->executeAndCapture("which steam", 1000);
234 if (result.first == 0 && !result.second.empty())
235 {
236 // Remove trailing newline
237 std::string path = result.second;
238 while (!path.empty() && (path.back() == '\n' || path.back() == '\r'))
239 {
240 path.pop_back();
241 }
242 if (std::filesystem::exists(path))
243 {
244 return path;
245 }
246 }
247
248 // Check common locations
249 std::vector<std::filesystem::path> candidates = {
250 "/usr/bin/steam",
251 "/usr/games/steam",
252 "/usr/local/bin/steam",
253 };
254
255 for (const auto& path : candidates)
256 {
257 if (std::filesystem::exists(path))
258 {
259 return path;
260 }
261 }
262
263 // Check if steam.sh exists in Steam root
264 auto steamRoot = findSteamRoot();
265 if (steamRoot && std::filesystem::exists(*steamRoot / "steam.sh"))
266 {
267 return *steamRoot / "steam.sh";
268 }
269
270 return std::nullopt;
271 }
272
273 bool isSteamInstalled() const override
274 {
275 return findSteamRoot().has_value() || getSteamExecutable().has_value();
276 }
277};
278
279// Factory implementation
280std::unique_ptr<SteamFinder> SteamFinder::create()
281{
282 return std::make_unique<LinuxSteamFinder>();
283}
284
285} // namespace platform
286
287#endif // PLATFORM_LINUX
static std::string escapeArg(const std::string &arg)
Escape a string for safe use as a shell argument.
Definition shell_utils.h:65
static std::unique_ptr< SteamFinder > create()