Look for a userfolder specficied in the game's ini config

This commit is contained in:
Tixx 2024-12-06 19:51:24 +01:00
parent a63f1bd27c
commit f9d347bd9b
No known key found for this signature in database
GPG Key ID: EC6E7A2BAABF0B8C

View File

@ -23,17 +23,41 @@
#include <filesystem>
#include <thread>
#include "Options.h"
#include <fstream>
unsigned long GamePID = 0;
#if defined(_WIN32)
std::string QueryKey(HKEY hKey, int ID);
std::string GetGamePath() {
static std::string Path;
static std::filesystem::path Path;
if (!Path.empty())
return Path;
if (options.user_path && std::filesystem::exists(options.user_path)) {
return Path.string();
if (options.user_path) {
if (std::filesystem::exists(options.user_path)) {
Path = options.user_path;
} else {
debug("Using custom user folder path: " + Path.string());
} else
warn("Invalid or non-existent path (" + std::string(options.user_path) + ") specified using --user-path, skipping");
}
if (std::string startupIniPath = GetGameDir() + "\\startup.ini"; std::filesystem::exists(startupIniPath)) {
std::ifstream startupIni(startupIniPath);
std::string line;
while (std::getline(startupIni, line))
if (line.find("UserPath = ") == 0) {
std::string userPath = line.substr(11);
if (!userPath.empty())
if (std::filesystem::exists(userPath)) {
Path = userPath;
debug("Using custom user folder path from startup.ini: " + Path.string());
} else
warn("Found custom user folder path ("+ userPath + ") in startup.ini but it doesn't exist, skipping");
}
if (Path.empty()) {
HKEY hKey;
LPCTSTR sk = "Software\\BeamNG\\BeamNG.drive";
LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey);
@ -43,25 +67,22 @@ std::string GetGamePath() {
Path = QueryKey(hKey, 4);
if (Path.empty()) {
Path = "";
char appDataPath[MAX_PATH];
HRESULT result = SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, appDataPath);
if (SUCCEEDED(result)) {
Path = appDataPath;
}
if (Path.empty()) {
if (!SUCCEEDED(result)) {
fatal("Cannot get Local Appdata directory");
}
Path += "\\BeamNG.drive\\";
Path = std::filesystem::path(appDataPath) / "BeamNG.drive";
}
}
}
std::string Ver = CheckVer(GetGameDir());
Ver = Ver.substr(0, Ver.find('.', Ver.find('.') + 1));
Path += Ver + "\\";
return Path;
Path = Path / (Ver + "\\");
return Path.string();
}
#elif defined(__linux__)
std::string GetGamePath() {