Added CLI argument for user-path (#148)

Simple addition to the CLI args. It could probably do with some
validation, ie; making sure it ends in a slash.

cc: @WiserTixx
This commit is contained in:
Tixx
2025-05-05 23:47:11 +02:00
committed by GitHub
4 changed files with 156 additions and 20 deletions

View File

@@ -18,47 +18,86 @@
#endif
#include "Logger.h"
#include "Options.h"
#include "Startup.h"
#include "Utils.h"
#include <Security/Init.h>
#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;
return Path.string();
HKEY hKey;
LPCTSTR sk = "Software\\BeamNG\\BeamNG.drive";
LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey);
if (openRes != ERROR_SUCCESS) {
fatal("Please launch the game at least once!");
if (options.user_path) {
if (std::filesystem::exists(options.user_path)) {
Path = options.user_path;
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");
}
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 (const auto startupIniPath = std::filesystem::path(GetGameDir()) / "startup.ini"; exists(startupIniPath)) {
if (std::ifstream startupIni(startupIniPath); startupIni.is_open()) {
std::string contents((std::istreambuf_iterator(startupIni)), std::istreambuf_iterator<char>());
startupIni.close();
if (contents.size() > 3) {
contents.erase(0, 3);
}
auto ini = Utils::ParseINI(contents);
if (ini.empty()) {
warn("Failed to parse startup.ini");
} else
debug("Successfully parsed startup.ini");
std::string userPath;
if (ini.contains("filesystem") && ini["filesystem"].contains("UserPath"))
userPath = ini["filesystem"]["UserPath"];
if (!userPath.empty())
if (userPath = Utils::ExpandEnvVars(userPath); 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()) {
fatal("Cannot get Local Appdata directory");
}
HKEY hKey;
LPCTSTR sk = "Software\\BeamNG\\BeamNG.drive";
LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey);
if (openRes != ERROR_SUCCESS) {
fatal("Please launch the game at least once!");
}
Path = QueryKey(hKey, 4);
Path += "\\BeamNG.drive\\";
if (Path.empty()) {
char appDataPath[MAX_PATH];
HRESULT result = SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, appDataPath);
if (!SUCCEEDED(result)) {
fatal("Cannot get Local Appdata directory");
}
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() {

View File

@@ -86,6 +86,12 @@ void InitOptions(int argc, const char *argv[], Options &options) {
options.no_download = true;
options.no_launch = true;
options.no_update = true;
} else if (argument == "--user-path") {
if (i + 1 >= argc) {
error("You must specify a path after the `--user-path` argument");
}
options.user_path = argv[i + 1];
i++;
} else if (argument == "--" || argument == "--game") {
options.game_arguments = &argv[i + 1];
options.game_arguments_length = argc - i - 1;
@@ -101,6 +107,7 @@ void InitOptions(int argc, const char *argv[], Options &options) {
"\t--no-update Skip applying launcher updates (you must update manually)\n"
"\t--no-launch Skip launching the game (you must launch the game manually)\n"
"\t--dev Developer mode, same as --verbose --no-download --no-launch --no-update\n"
"\t--user-path <path> Path to BeamNG's User Path\n"
"\t--game <args...> Passes ALL following arguments to the game, see also `--`\n"
<< std::flush;
exit(0);