From 7b59cb6f8793a0caa1a10daa6ae953fbf5c9b26c Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 7 Oct 2024 00:33:43 +0200 Subject: [PATCH] fix various commandline argument related things --- include/Options.h | 10 +++++++--- include/Startup.h | 3 +-- src/GameStart.cpp | 4 ++-- src/Network/Http.cpp | 1 - src/Options.cpp | 24 ++++++++++++++++++++---- src/Startup.cpp | 18 +++++++++++------- src/main.cpp | 2 +- 7 files changed, 42 insertions(+), 20 deletions(-) diff --git a/include/Options.h b/include/Options.h index 683a4efa..255a4ab5 100644 --- a/include/Options.h +++ b/include/Options.h @@ -3,18 +3,22 @@ #include struct Options { +#if defined(_WIN32) std::string executable_name = "BeamMP-Launcher.exe"; +#elif defined(__linux__) + std::string executable_name = "BeamMP-Launcher"; +#endif unsigned int port = 4444; bool verbose = false; bool no_download = false; bool no_update = false; bool no_launch = false; - char **game_arguments = nullptr; + const char **game_arguments = nullptr; int game_arguments_length = 0; - char** argv = nullptr; + const char** argv = nullptr; int argc = 0; }; -void InitOptions(int argc, char *argv[], Options &options); +void InitOptions(int argc, const char *argv[], Options &options); extern Options options; diff --git a/include/Startup.h b/include/Startup.h index b8b84b00..9fd6940a 100644 --- a/include/Startup.h +++ b/include/Startup.h @@ -11,10 +11,9 @@ #include void InitLauncher(); -std::string GetEP(char* P = nullptr); +std::string GetEP(const char* P = nullptr); std::string GetGamePath(); std::string GetVer(); std::string GetPatch(); std::string GetEN(); void ConfigInit(); - diff --git a/src/GameStart.cpp b/src/GameStart.cpp index d8c0cc34..194d13b9 100644 --- a/src/GameStart.cpp +++ b/src/GameStart.cpp @@ -107,7 +107,7 @@ void StartGame(std::string Dir) { void StartGame(std::string Dir) { int status; std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64"); - std::vector argv; + std::vector argv; argv.push_back(filename.data()); for (int i = 0; i < options.game_arguments_length; i++) { argv.push_back(options.game_arguments[i]); @@ -119,7 +119,7 @@ void StartGame(std::string Dir) { posix_spawn_file_actions_init(&spawn_actions); posix_spawn_file_actions_addclose(&spawn_actions, STDOUT_FILENO); posix_spawn_file_actions_addclose(&spawn_actions, STDERR_FILENO); - int result = posix_spawn(&pid, filename.c_str(), &spawn_actions, nullptr, argv.data(), environ); + int result = posix_spawn(&pid, filename.c_str(), &spawn_actions, nullptr, const_cast(argv.data()), environ); if (result != 0) { error("Failed to Launch the game! launcher closing soon"); diff --git a/src/Network/Http.cpp b/src/Network/Http.cpp index 2ef463f6..5406b65d 100644 --- a/src/Network/Http.cpp +++ b/src/Network/Http.cpp @@ -136,7 +136,6 @@ bool HTTP::Download(const std::string& IP, const std::string& Path) { if (File.is_open()) { File << Ret; File.close(); - std::cout << "\n"; info("Download Complete!"); } else { error("Failed to open file directory: " + Path); diff --git a/src/Options.cpp b/src/Options.cpp index 8dfa7880..1c0cadff 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -2,14 +2,15 @@ #include "Logger.h" #include +#include -void InitOptions(int argc, char *argv[], Options &options) { +void InitOptions(int argc, const char *argv[], Options &options) { int i = 1; options.argc = argc; options.argv = argv; - if (argc > 2) + if (argc > 2) { if (std::string(argv[1]) == "0" && std::string(argv[2]) == "0") { options.verbose = true; options.no_download = true; @@ -18,6 +19,7 @@ void InitOptions(int argc, char *argv[], Options &options) { warn("You are using deprecated commandline arguments, please use --dev instead"); return; } + } options.executable_name = std::string(argv[0]); @@ -38,7 +40,7 @@ void InitOptions(int argc, char *argv[], Options &options) { try { port = std::stoi(argv[i + 1]); - } catch (std::exception& e) { + } catch (std::exception& e) { error("Invalid port specified: " + std::string(argv[i + 1]) + " " + std::string(e.what())); } @@ -68,10 +70,24 @@ void InitOptions(int argc, char *argv[], Options &options) { options.no_download = true; options.no_launch = true; options.no_update = true; - } else if (argument == "--game") { + } else if (argument == "--" || argument == "--game") { options.game_arguments = &argv[i + 1]; options.game_arguments_length = argc - i - 1; break; + } else if (argument == "--help" || argument == "-h" || argument == "/?") { + std::cout << "USAGE:\n" + "\t" + std::filesystem::path(options.executable_name).filename().string() + " [OPTIONS] [-- ...]\n" + "\n" + "OPTIONS:\n" + "\t--port -p Change the default listen port to . This must be configured ingame, too\n" + "\t--verbose -v Verbose mode, prints debug messages\n" + "\t--no-download Skip downloading and installing the BeamMP Lua mod\n" + "\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--game Passes ALL following arguments to the game, see also `--`\n" + << std::flush; + exit(0); } else { warn("Unknown option: " + argument); } diff --git a/src/Startup.cpp b/src/Startup.cpp index aa272b23..922b4716 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -8,6 +8,7 @@ #include "zip_file.h" #include +#include #include #include #include @@ -87,7 +88,7 @@ std::string GetPatch() { return ".0"; } -std::string GetEP(char* P) { +std::string GetEP(const char* P) { static std::string Ret = [&]() { std::string path(P); return path.substr(0, path.find_last_of("\\/") + 1); @@ -128,17 +129,20 @@ void ReLaunch() { } info("Relaunch!"); system("clear"); - execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL); + int ret = execv(options.executable_name.c_str(), const_cast(options.argv)); + if (ret < 0) { + error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch"); + exit(1); + } std::this_thread::sleep_for(std::chrono::seconds(1)); exit(1); } void URelaunch() { - std::string Arg; - for (int c = 2; c <= options.argc; c++) { - Arg += options.argv[c - 1]; - Arg += " "; + int ret = execv(options.executable_name.c_str(), const_cast(options.argv)); + if (ret < 0) { + error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch"); + exit(1); } - execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL); std::this_thread::sleep_for(std::chrono::seconds(1)); exit(1); } diff --git a/src/main.cpp b/src/main.cpp index 8305f44e..7f77fba5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,7 +24,7 @@ Options options; } } -int main(int argc, char** argv) try { +int main(int argc, const char** argv) try { #if defined(_WIN32) system("cls"); #elif defined(__linux__)