2017-07-23 09:20:28 +00:00
|
|
|
#include "hecl/SteamFinder.hpp"
|
|
|
|
#include "hecl/hecl.hpp"
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <winreg.h>
|
2021-06-30 18:20:45 +00:00
|
|
|
#define PATH_SEP '\\'
|
2017-07-23 09:20:28 +00:00
|
|
|
#else
|
|
|
|
#define PATH_SEP '/'
|
|
|
|
#endif
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
namespace hecl {
|
2017-07-23 09:20:28 +00:00
|
|
|
|
|
|
|
/* Used to extract alternate steam install directories from libraryfolders.vdf */
|
2018-12-08 05:18:42 +00:00
|
|
|
static const std::regex regSteamPath(R"(^\s+\"[0-9]+\"\s+\"(.*)\")", std::regex::ECMAScript | std::regex::optimize);
|
2017-07-23 09:20:28 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string FindCommonSteamApp(const char* name) {
|
|
|
|
std::string steamInstallDir;
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::Sstat theStat;
|
2017-07-23 09:20:28 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2017-12-06 03:22:31 +00:00
|
|
|
#if !WINDOWS_STORE
|
2018-12-08 05:18:42 +00:00
|
|
|
HKEY hkey;
|
2021-06-30 18:20:45 +00:00
|
|
|
wchar_t _steamInstallDir[MAX_PATH] = {0};
|
|
|
|
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Valve\\Steam", 0, KEY_QUERY_VALUE, &hkey) !=
|
2018-12-08 05:18:42 +00:00
|
|
|
ERROR_SUCCESS) {
|
2021-06-30 18:20:45 +00:00
|
|
|
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Valve\\Steam", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY,
|
2018-12-08 05:18:42 +00:00
|
|
|
&hkey) != ERROR_SUCCESS)
|
|
|
|
return {};
|
|
|
|
}
|
2017-07-23 09:20:28 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
DWORD size = MAX_PATH;
|
2021-06-30 18:20:45 +00:00
|
|
|
if (RegQueryValueEx(hkey, L"InstallPath", nullptr, nullptr, (LPBYTE)_steamInstallDir, &size) ==
|
2018-12-08 05:18:42 +00:00
|
|
|
ERROR_SUCCESS)
|
2021-06-30 18:20:45 +00:00
|
|
|
steamInstallDir = nowide::narrow(_steamInstallDir);
|
2018-12-08 05:18:42 +00:00
|
|
|
RegCloseKey(hkey);
|
2017-07-23 09:20:28 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
if (steamInstallDir.empty())
|
2017-12-06 03:22:31 +00:00
|
|
|
return {};
|
2018-12-08 05:18:42 +00:00
|
|
|
#else
|
|
|
|
return {};
|
2017-12-06 03:22:31 +00:00
|
|
|
#endif
|
2017-07-23 09:20:28 +00:00
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
2018-12-08 05:18:42 +00:00
|
|
|
steamInstallDir = getenv("HOME");
|
|
|
|
steamInstallDir += "/Library/Application Support/Steam";
|
|
|
|
if (hecl::Stat(steamInstallDir.c_str(), &theStat) || !S_ISDIR(theStat.st_mode))
|
|
|
|
return {};
|
2017-07-23 09:20:28 +00:00
|
|
|
|
|
|
|
#else
|
2018-12-08 05:18:42 +00:00
|
|
|
steamInstallDir = getenv("HOME");
|
|
|
|
steamInstallDir += "/.local/share/Steam";
|
|
|
|
if (hecl::Stat(steamInstallDir.c_str(), &theStat) || !S_ISDIR(theStat.st_mode)) {
|
2017-07-23 09:20:28 +00:00
|
|
|
steamInstallDir = getenv("HOME");
|
2018-12-08 05:18:42 +00:00
|
|
|
steamInstallDir += "/.steam/steam";
|
2017-07-23 09:20:28 +00:00
|
|
|
if (hecl::Stat(steamInstallDir.c_str(), &theStat) || !S_ISDIR(theStat.st_mode))
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-07-23 09:20:28 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
const std::string appPath = std::string("common") + PATH_SEP + name;
|
2017-07-23 09:20:28 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
/* Try main steam install directory first */
|
2021-06-30 18:20:45 +00:00
|
|
|
const std::string steamAppsMain = steamInstallDir + PATH_SEP + "steamapps";
|
|
|
|
const std::string mainAppPath = steamAppsMain + PATH_SEP + appPath;
|
2019-08-21 23:33:25 +00:00
|
|
|
if (!hecl::Stat(mainAppPath.c_str(), &theStat) && S_ISDIR(theStat.st_mode)) {
|
2018-12-08 05:18:42 +00:00
|
|
|
return mainAppPath;
|
2019-08-21 23:33:25 +00:00
|
|
|
}
|
2017-07-23 09:20:28 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
/* Iterate alternate steam install dirs */
|
2021-06-30 18:20:45 +00:00
|
|
|
const std::string libraryFoldersVdfPath = steamAppsMain + PATH_SEP + "libraryfolders.vdf";
|
|
|
|
auto fp = hecl::FopenUnique(libraryFoldersVdfPath.c_str(), "r");
|
2019-08-21 23:33:25 +00:00
|
|
|
if (fp == nullptr) {
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
2019-08-21 23:33:25 +00:00
|
|
|
}
|
|
|
|
hecl::FSeek(fp.get(), 0, SEEK_END);
|
|
|
|
const int64_t fileLen = hecl::FTell(fp.get());
|
2018-12-08 05:18:42 +00:00
|
|
|
if (fileLen <= 0) {
|
|
|
|
return {};
|
|
|
|
}
|
2019-08-21 23:33:25 +00:00
|
|
|
hecl::FSeek(fp.get(), 0, SEEK_SET);
|
|
|
|
std::string fileBuf(fileLen, '\0');
|
|
|
|
if (std::fread(fileBuf.data(), 1, fileLen, fp.get()) != fileLen) {
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-07-23 09:20:28 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
std::smatch dirMatch;
|
|
|
|
auto begin = fileBuf.cbegin();
|
2019-08-21 23:33:25 +00:00
|
|
|
const auto end = fileBuf.cend();
|
2018-12-08 05:18:42 +00:00
|
|
|
while (std::regex_search(begin, end, dirMatch, regSteamPath)) {
|
2019-08-21 23:33:25 +00:00
|
|
|
const std::string match = dirMatch[1].str();
|
|
|
|
const auto otherAppPath =
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string(match.c_str()) + PATH_SEP + "steamapps" + PATH_SEP + appPath;
|
2019-08-21 23:33:25 +00:00
|
|
|
|
|
|
|
if (!hecl::Stat(otherAppPath.c_str(), &theStat) && S_ISDIR(theStat.st_mode)) {
|
2018-12-08 05:18:42 +00:00
|
|
|
return otherAppPath;
|
2019-08-21 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
begin = dirMatch.suffix().first;
|
|
|
|
}
|
2017-07-23 09:20:28 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
return {};
|
2017-07-23 09:20:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
} // namespace hecl
|