From 5737e27bf397cb2edc8f066e8d6e7af7920af8f8 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Tue, 16 Sep 2025 20:07:57 +0200 Subject: [PATCH] Use ini instead of registry for game dir --- src/Security/BeamNG.cpp | 54 ++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/Security/BeamNG.cpp b/src/Security/BeamNG.cpp index 43b835a..8897ccb 100644 --- a/src/Security/BeamNG.cpp +++ b/src/Security/BeamNG.cpp @@ -17,6 +17,7 @@ #include "Utils.h" #include +#include #include #include @@ -162,24 +163,43 @@ void FileList(std::vector& a, const std::string& Path) { } void LegitimacyCheck() { #if defined(_WIN32) - std::wstring Result; - std::string K3 = R"(Software\BeamNG\BeamNG.drive)"; - HKEY hKey; - LONG dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K3.c_str(), &hKey); - if (dwRegOPenKey == ERROR_SUCCESS) { - Result = QueryKey(hKey, 3); - if (Result.empty()) { - debug("Failed to QUERY key HKEY_CURRENT_USER\\Software\\BeamNG\\BeamNG.drive"); - lowExit(3); - } - GameDir = Result; - } else { - debug("Failed to OPEN key HKEY_CURRENT_USER\\Software\\BeamNG\\BeamNG.drive"); - lowExit(4); + wchar_t* appDataPath = new wchar_t[MAX_PATH]; + HRESULT result = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, appDataPath); + + if (!SUCCEEDED(result)) { + fatal("Cannot get Local Appdata directory"); } - K3.clear(); - Result.clear(); - RegCloseKey(hKey); + + auto BeamNGAppdataPath = std::filesystem::path(appDataPath) / "BeamNG"; + + if (const auto beamngIniPath = BeamNGAppdataPath / "BeamNG.Drive.ini"; exists(beamngIniPath)) { + if (std::ifstream beamngIni(beamngIniPath); beamngIni.is_open()) { + std::string contents((std::istreambuf_iterator(beamngIni)), std::istreambuf_iterator()); + beamngIni.close(); + + auto ini = Utils::ParseINI(contents); + if (ini.empty()) + lowExit(3); + else + debug("Successfully parsed BeamNG.Drive.ini"); + + if (ini.contains("installPath")) { + std::wstring installPath = Utils::ToWString(std::get(ini["installPath"])); + installPath.erase(0, installPath.find_first_not_of(L" \t")); + + if (installPath = std::filesystem::path(Utils::ExpandEnvVars(installPath)); std::filesystem::exists(installPath)) { + GameDir = installPath; + debug(L"GameDir from BeamNG.Drive.ini: " + installPath); + } else { + lowExit(4); + } + } else { + lowExit(5); + } + } + } + + delete[] appDataPath; #elif defined(__linux__) struct passwd* pw = getpwuid(getuid()); std::filesystem::path homeDir = pw->pw_dir;