game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
steam_finder_windows.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#include "platform/paths.h"
3#include "platform/platform.h"
6
7#ifdef PLATFORM_WINDOWS
8
9#include <fstream>
10#include <regex>
11#include <shellapi.h>
12#include <sstream>
13#include <windows.h>
14
15namespace platform
16{
17
18namespace
19{
20
21std::wstring toWideString(const std::string& str)
22{
23 if (str.empty())
24 return L"";
25 int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
26 std::wstring result(len - 1, L'\0');
27 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, result.data(), len);
28 return result;
29}
30
31std::string toNarrowString(const std::wstring& wstr)
32{
33 if (wstr.empty())
34 return "";
35 int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
36 std::string result(len - 1, '\0');
37 WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, result.data(), len, nullptr, nullptr);
38 return result;
39}
40
41} // anonymous namespace
42
43class WindowsSteamFinder : public SteamFinder
44{
45 public:
46 std::optional<std::filesystem::path> findSteamRoot() const override
47 {
48 // Try registry first (most reliable on Windows)
49 HKEY hKey;
50 if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", 0, KEY_READ, &hKey) ==
51 ERROR_SUCCESS)
52 {
53 wchar_t path[MAX_PATH];
54 DWORD size = sizeof(path);
55 DWORD type;
56 if (RegQueryValueExW(hKey, L"SteamPath", nullptr, &type, reinterpret_cast<LPBYTE>(path),
57 &size) == ERROR_SUCCESS)
58 {
59 RegCloseKey(hKey);
60 std::filesystem::path steamPath(path);
61 // Registry stores with forward slashes, convert
62 if (std::filesystem::exists(steamPath / "steam.exe"))
63 {
64 return steamPath;
65 }
66 }
67 RegCloseKey(hKey);
68 }
69
70 // Also try HKLM for system-wide installs
71 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Valve\\Steam", 0, KEY_READ, &hKey) ==
72 ERROR_SUCCESS)
73 {
74 wchar_t path[MAX_PATH];
75 DWORD size = sizeof(path);
76 DWORD type;
77 if (RegQueryValueExW(hKey, L"InstallPath", nullptr, &type,
78 reinterpret_cast<LPBYTE>(path), &size) == ERROR_SUCCESS)
79 {
80 RegCloseKey(hKey);
81 std::filesystem::path steamPath(path);
82 if (std::filesystem::exists(steamPath / "steam.exe"))
83 {
84 return steamPath;
85 }
86 }
87 RegCloseKey(hKey);
88 }
89
90 // Fallback to common installation paths
91 std::vector<std::filesystem::path> candidates = {"C:\\Program Files (x86)\\Steam",
92 "C:\\Program Files\\Steam",
93 "D:\\Steam",
94 "D:\\Program Files\\Steam",
95 "D:\\Program Files (x86)\\Steam",
96 "E:\\Steam",
97 "D:\\Games\\Steam",
98 "E:\\Games\\Steam"};
99
100 for (const auto& path : candidates)
101 {
102 if (std::filesystem::exists(path / "steam.exe"))
103 {
104 return path;
105 }
106 }
107
108 return std::nullopt;
109 }
110
111 std::vector<SteamLibraryFolder> getLibraryFolders() const override
112 {
113 std::vector<SteamLibraryFolder> folders;
114
115 auto steamRoot = findSteamRoot();
116 if (!steamRoot)
117 return folders;
118
119 // Main Steam folder
120 SteamLibraryFolder mainFolder;
121 mainFolder.path = *steamRoot;
122 mainFolder.label = "Main";
123 folders.push_back(mainFolder);
124
125 // Parse libraryfolders.vdf
126 std::filesystem::path vdfPath = *steamRoot / "steamapps" / "libraryfolders.vdf";
127 if (!std::filesystem::exists(vdfPath))
128 {
129 vdfPath = *steamRoot / "config" / "libraryfolders.vdf";
130 }
131
132 if (std::filesystem::exists(vdfPath))
133 {
134 std::ifstream file(vdfPath);
135 std::string content((std::istreambuf_iterator<char>(file)),
136 std::istreambuf_iterator<char>());
137
138 // Parse "path" entries
139 std::regex pathRegex(R"("path"\s+"([^"]+)") ");
140 std::smatch match;
141 std::string::const_iterator searchStart(content.cbegin());
142
143 while (std::regex_search(searchStart, content.cend(), match, pathRegex))
144 {
145 std::string pathStr = match[1].str();
146 // Handle escaped backslashes in VDF
147 std::string fixedPath;
148 for (size_t i = 0; i < pathStr.size(); ++i)
149 {
150 if (pathStr[i] == '\\' && i + 1 < pathStr.size() && pathStr[i + 1] == '\\')
151 {
152 fixedPath += '\\';
153 ++i;
154 }
155 else
156 {
157 fixedPath += pathStr[i];
158 }
159 }
160
161 std::filesystem::path libPath(fixedPath);
162 if (std::filesystem::exists(libPath) && libPath != *steamRoot)
163 {
164 SteamLibraryFolder folder;
165 folder.path = libPath;
166 folder.label = libPath.root_name().string();
167 folders.push_back(folder);
168 }
169 searchStart = match.suffix().first;
170 }
171 }
172
173 return folders;
174 }
175
176 std::vector<std::filesystem::path> getCompatToolsDirs() const override
177 {
178 std::vector<std::filesystem::path> dirs;
179
180 auto steamRoot = findSteamRoot();
181 if (steamRoot)
182 {
183 // Proton and compatibility tools on Windows (rare but possible)
184 dirs.push_back(*steamRoot / "compatibilitytools.d");
185 dirs.push_back(*steamRoot / "steamapps" / "common");
186 }
187
188 // Filter to existing
189 std::vector<std::filesystem::path> existing;
190 for (const auto& dir : dirs)
191 {
192 if (std::filesystem::exists(dir))
193 {
194 existing.push_back(dir);
195 }
196 }
197
198 return existing;
199 }
200
201 std::optional<std::filesystem::path> findGameInstall(uint32_t appId) const override
202 {
203 auto libraries = getLibraryFolders();
204
205 for (const auto& lib : libraries)
206 {
207 std::filesystem::path manifestPath =
208 lib.path / "steamapps" / ("appmanifest_" + std::to_string(appId) + ".acf");
209
210 if (std::filesystem::exists(manifestPath))
211 {
212 std::ifstream file(manifestPath);
213 std::string content((std::istreambuf_iterator<char>(file)),
214 std::istreambuf_iterator<char>());
215
216 std::regex installDirRegex(R"("installdir"\s+"([^"]+)") ");
217 std::smatch match;
218 if (std::regex_search(content, match, installDirRegex))
219 {
220 return lib.path / "steamapps" / "common" / match[1].str();
221 }
222 }
223 }
224
225 return std::nullopt;
226 }
227
228 bool isSteamRunning() const override
229 {
230 auto pm = ProcessManager::create();
231 auto procs = pm->findByName("steam.exe");
232 return !procs.empty();
233 }
234
235 bool startSteam(bool silent) const override
236 {
237 auto steamExe = getSteamExecutable();
238 if (!steamExe)
239 return false;
240
241 std::wstring args = silent ? L"-silent" : L"";
242
243 SHELLEXECUTEINFOW sei = {sizeof(sei)};
244 sei.fMask = SEE_MASK_NOCLOSEPROCESS;
245 sei.lpVerb = L"open";
246 sei.lpFile = steamExe->wstring().c_str();
247 sei.lpParameters = args.c_str();
248 sei.nShow = SW_SHOWNORMAL;
249
250 return ShellExecuteExW(&sei) != 0;
251 }
252
253 bool shutdownSteam() const override
254 {
255 auto steamExe = getSteamExecutable();
256 if (!steamExe)
257 return false;
258
259 SHELLEXECUTEINFOW sei = {sizeof(sei)};
260 sei.fMask = 0;
261 sei.lpVerb = L"open";
262 sei.lpFile = steamExe->wstring().c_str();
263 sei.lpParameters = L"-shutdown";
264 sei.nShow = SW_HIDE;
265
266 return ShellExecuteExW(&sei) != 0;
267 }
268
269 bool launchGame(uint32_t appId) const override
270 {
271 // Use steam:// protocol
272 std::wstring url = L"steam://rungameid/" + std::to_wstring(appId);
273
274 HINSTANCE result =
275 ShellExecuteW(nullptr, L"open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
276 return reinterpret_cast<INT_PTR>(result) > 32;
277 }
278
279 bool launchGameWithArgs(uint32_t appId, const std::string& args) const override
280 {
281 auto steamExe = getSteamExecutable();
282 if (!steamExe)
283 return false;
284
285 std::wstring params = L"-applaunch " + std::to_wstring(appId);
286 if (!args.empty())
287 {
288 params += L" " + toWideString(args);
289 }
290
291 SHELLEXECUTEINFOW sei = {sizeof(sei)};
292 sei.lpVerb = L"open";
293 sei.lpFile = steamExe->wstring().c_str();
294 sei.lpParameters = params.c_str();
295 sei.nShow = SW_SHOWNORMAL;
296
297 return ShellExecuteExW(&sei) != 0;
298 }
299
300 std::optional<std::filesystem::path> getSteamExecutable() const override
301 {
302 auto steamRoot = findSteamRoot();
303 if (steamRoot)
304 {
305 std::filesystem::path exePath = *steamRoot / "steam.exe";
306 if (std::filesystem::exists(exePath))
307 {
308 return exePath;
309 }
310 }
311 return std::nullopt;
312 }
313
314 bool isSteamInstalled() const override
315 {
316 return findSteamRoot().has_value();
317 }
318};
319
320// Factory implementation
321std::unique_ptr<SteamFinder> SteamFinder::create()
322{
323 return std::make_unique<WindowsSteamFinder>();
324}
325
326} // namespace platform
327
328#endif // PLATFORM_WINDOWS
static std::unique_ptr< ProcessManager > create()