21std::wstring toWideString(
const std::string& str)
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);
31std::string toNarrowString(
const std::wstring& wstr)
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);
46 std::optional<std::filesystem::path> findSteamRoot()
const override
50 if (RegOpenKeyExW(HKEY_CURRENT_USER, L
"Software\\Valve\\Steam", 0, KEY_READ, &hKey) ==
53 wchar_t path[MAX_PATH];
54 DWORD size =
sizeof(path);
56 if (RegQueryValueExW(hKey, L
"SteamPath",
nullptr, &type,
reinterpret_cast<LPBYTE
>(path),
57 &size) == ERROR_SUCCESS)
60 std::filesystem::path steamPath(path);
62 if (std::filesystem::exists(steamPath /
"steam.exe"))
71 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L
"Software\\Valve\\Steam", 0, KEY_READ, &hKey) ==
74 wchar_t path[MAX_PATH];
75 DWORD size =
sizeof(path);
77 if (RegQueryValueExW(hKey, L
"InstallPath",
nullptr, &type,
78 reinterpret_cast<LPBYTE
>(path), &size) == ERROR_SUCCESS)
81 std::filesystem::path steamPath(path);
82 if (std::filesystem::exists(steamPath /
"steam.exe"))
91 std::vector<std::filesystem::path> candidates = {
"C:\\Program Files (x86)\\Steam",
92 "C:\\Program Files\\Steam",
94 "D:\\Program Files\\Steam",
95 "D:\\Program Files (x86)\\Steam",
100 for (
const auto& path : candidates)
102 if (std::filesystem::exists(path /
"steam.exe"))
111 std::vector<SteamLibraryFolder> getLibraryFolders()
const override
113 std::vector<SteamLibraryFolder> folders;
115 auto steamRoot = findSteamRoot();
120 SteamLibraryFolder mainFolder;
121 mainFolder.path = *steamRoot;
122 mainFolder.label =
"Main";
123 folders.push_back(mainFolder);
126 std::filesystem::path vdfPath = *steamRoot /
"steamapps" /
"libraryfolders.vdf";
127 if (!std::filesystem::exists(vdfPath))
129 vdfPath = *steamRoot /
"config" /
"libraryfolders.vdf";
132 if (std::filesystem::exists(vdfPath))
134 std::ifstream file(vdfPath);
135 std::string content((std::istreambuf_iterator<char>(file)),
136 std::istreambuf_iterator<char>());
139 std::regex pathRegex(R
"("path"\s+"([^"]+)") ");
141 std::string::const_iterator searchStart(content.cbegin());
143 while (std::regex_search(searchStart, content.cend(), match, pathRegex))
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)
150 if (pathStr[i] == '\\' && i + 1 < pathStr.size() && pathStr[i + 1] == '\\')
157 fixedPath += pathStr[i];
161 std::filesystem::path libPath(fixedPath);
162 if (std::filesystem::exists(libPath) && libPath != *steamRoot)
164 SteamLibraryFolder folder;
165 folder.path = libPath;
166 folder.label = libPath.root_name().string();
167 folders.push_back(folder);
169 searchStart = match.suffix().first;
176 std::vector<std::filesystem::path> getCompatToolsDirs() const override
178 std::vector<std::filesystem::path> dirs;
180 auto steamRoot = findSteamRoot();
183 // Proton and compatibility tools on Windows (rare but possible)
184 dirs.push_back(*steamRoot / "compatibilitytools.d
");
185 dirs.push_back(*steamRoot / "steamapps
" / "common
");
188 // Filter to existing
189 std::vector<std::filesystem::path> existing;
190 for (const auto& dir : dirs)
192 if (std::filesystem::exists(dir))
194 existing.push_back(dir);
201 std::optional<std::filesystem::path> findGameInstall(uint32_t appId) const override
203 auto libraries = getLibraryFolders();
205 for (const auto& lib : libraries)
207 std::filesystem::path manifestPath =
208 lib.path / "steamapps
" / ("appmanifest_
" + std::to_string(appId) + ".acf
");
210 if (std::filesystem::exists(manifestPath))
212 std::ifstream file(manifestPath);
213 std::string content((std::istreambuf_iterator<char>(file)),
214 std::istreambuf_iterator<char>());
216 std::regex installDirRegex(R"(
"installdir"\s+
"([^"]+)
") ");
218 if (std::regex_search(content, match, installDirRegex))
220 return lib.path /
"steamapps" /
"common" / match[1].str();
228 bool isSteamRunning()
const override
231 auto procs = pm->findByName(
"steam.exe");
232 return !procs.empty();
235 bool startSteam(
bool silent)
const override
237 auto steamExe = getSteamExecutable();
241 std::wstring args = silent ? L
"-silent" : L
"";
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;
250 return ShellExecuteExW(&sei) != 0;
253 bool shutdownSteam()
const override
255 auto steamExe = getSteamExecutable();
259 SHELLEXECUTEINFOW sei = {
sizeof(sei)};
261 sei.lpVerb = L
"open";
262 sei.lpFile = steamExe->wstring().c_str();
263 sei.lpParameters = L
"-shutdown";
266 return ShellExecuteExW(&sei) != 0;
269 bool launchGame(uint32_t appId)
const override
272 std::wstring url = L
"steam://rungameid/" + std::to_wstring(appId);
275 ShellExecuteW(
nullptr, L
"open", url.c_str(),
nullptr,
nullptr, SW_SHOWNORMAL);
276 return reinterpret_cast<INT_PTR
>(result) > 32;
279 bool launchGameWithArgs(uint32_t appId,
const std::string& args)
const override
281 auto steamExe = getSteamExecutable();
285 std::wstring params = L
"-applaunch " + std::to_wstring(appId);
288 params += L
" " + toWideString(args);
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;
297 return ShellExecuteExW(&sei) != 0;
300 std::optional<std::filesystem::path> getSteamExecutable()
const override
302 auto steamRoot = findSteamRoot();
305 std::filesystem::path exePath = *steamRoot /
"steam.exe";
306 if (std::filesystem::exists(exePath))
314 bool isSteamInstalled()
const override
316 return findSteamRoot().has_value();
321std::unique_ptr<SteamFinder> SteamFinder::create()
323 return std::make_unique<WindowsSteamFinder>();