fix various commandline argument related things

This commit is contained in:
Lion Kortlepel 2024-10-07 00:33:43 +02:00
parent 0eba745d4c
commit 7b59cb6f87
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
7 changed files with 42 additions and 20 deletions

View File

@ -3,18 +3,22 @@
#include <string> #include <string>
struct Options { struct Options {
#if defined(_WIN32)
std::string executable_name = "BeamMP-Launcher.exe"; std::string executable_name = "BeamMP-Launcher.exe";
#elif defined(__linux__)
std::string executable_name = "BeamMP-Launcher";
#endif
unsigned int port = 4444; unsigned int port = 4444;
bool verbose = false; bool verbose = false;
bool no_download = false; bool no_download = false;
bool no_update = false; bool no_update = false;
bool no_launch = false; bool no_launch = false;
char **game_arguments = nullptr; const char **game_arguments = nullptr;
int game_arguments_length = 0; int game_arguments_length = 0;
char** argv = nullptr; const char** argv = nullptr;
int argc = 0; int argc = 0;
}; };
void InitOptions(int argc, char *argv[], Options &options); void InitOptions(int argc, const char *argv[], Options &options);
extern Options options; extern Options options;

View File

@ -11,10 +11,9 @@
#include <vector> #include <vector>
void InitLauncher(); void InitLauncher();
std::string GetEP(char* P = nullptr); std::string GetEP(const char* P = nullptr);
std::string GetGamePath(); std::string GetGamePath();
std::string GetVer(); std::string GetVer();
std::string GetPatch(); std::string GetPatch();
std::string GetEN(); std::string GetEN();
void ConfigInit(); void ConfigInit();

View File

@ -107,7 +107,7 @@ void StartGame(std::string Dir) {
void StartGame(std::string Dir) { void StartGame(std::string Dir) {
int status; int status;
std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64"); std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64");
std::vector<char*> argv; std::vector<const char*> argv;
argv.push_back(filename.data()); argv.push_back(filename.data());
for (int i = 0; i < options.game_arguments_length; i++) { for (int i = 0; i < options.game_arguments_length; i++) {
argv.push_back(options.game_arguments[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_init(&spawn_actions);
posix_spawn_file_actions_addclose(&spawn_actions, STDOUT_FILENO); posix_spawn_file_actions_addclose(&spawn_actions, STDOUT_FILENO);
posix_spawn_file_actions_addclose(&spawn_actions, STDERR_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<char**>(argv.data()), environ);
if (result != 0) { if (result != 0) {
error("Failed to Launch the game! launcher closing soon"); error("Failed to Launch the game! launcher closing soon");

View File

@ -136,7 +136,6 @@ bool HTTP::Download(const std::string& IP, const std::string& Path) {
if (File.is_open()) { if (File.is_open()) {
File << Ret; File << Ret;
File.close(); File.close();
std::cout << "\n";
info("Download Complete!"); info("Download Complete!");
} else { } else {
error("Failed to open file directory: " + Path); error("Failed to open file directory: " + Path);

View File

@ -2,14 +2,15 @@
#include "Logger.h" #include "Logger.h"
#include <cstdlib> #include <cstdlib>
#include <filesystem>
void InitOptions(int argc, char *argv[], Options &options) { void InitOptions(int argc, const char *argv[], Options &options) {
int i = 1; int i = 1;
options.argc = argc; options.argc = argc;
options.argv = argv; options.argv = argv;
if (argc > 2) if (argc > 2) {
if (std::string(argv[1]) == "0" && std::string(argv[2]) == "0") { if (std::string(argv[1]) == "0" && std::string(argv[2]) == "0") {
options.verbose = true; options.verbose = true;
options.no_download = 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"); warn("You are using deprecated commandline arguments, please use --dev instead");
return; return;
} }
}
options.executable_name = std::string(argv[0]); options.executable_name = std::string(argv[0]);
@ -38,7 +40,7 @@ void InitOptions(int argc, char *argv[], Options &options) {
try { try {
port = std::stoi(argv[i + 1]); 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())); 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_download = true;
options.no_launch = true; options.no_launch = true;
options.no_update = true; options.no_update = true;
} else if (argument == "--game") { } else if (argument == "--" || argument == "--game") {
options.game_arguments = &argv[i + 1]; options.game_arguments = &argv[i + 1];
options.game_arguments_length = argc - i - 1; options.game_arguments_length = argc - i - 1;
break; break;
} else if (argument == "--help" || argument == "-h" || argument == "/?") {
std::cout << "USAGE:\n"
"\t" + std::filesystem::path(options.executable_name).filename().string() + " [OPTIONS] [-- <GAME ARGS>...]\n"
"\n"
"OPTIONS:\n"
"\t--port <port> -p Change the default listen port to <port>. 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 <args...> Passes ALL following arguments to the game, see also `--`\n"
<< std::flush;
exit(0);
} else { } else {
warn("Unknown option: " + argument); warn("Unknown option: " + argument);
} }

View File

@ -8,6 +8,7 @@
#include "zip_file.h" #include "zip_file.h"
#include <charconv> #include <charconv>
#include <cstring>
#include <httplib.h> #include <httplib.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string> #include <string>
@ -87,7 +88,7 @@ std::string GetPatch() {
return ".0"; return ".0";
} }
std::string GetEP(char* P) { std::string GetEP(const char* P) {
static std::string Ret = [&]() { static std::string Ret = [&]() {
std::string path(P); std::string path(P);
return path.substr(0, path.find_last_of("\\/") + 1); return path.substr(0, path.find_last_of("\\/") + 1);
@ -128,17 +129,20 @@ void ReLaunch() {
} }
info("Relaunch!"); info("Relaunch!");
system("clear"); system("clear");
execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL); int ret = execv(options.executable_name.c_str(), const_cast<char**>(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)); std::this_thread::sleep_for(std::chrono::seconds(1));
exit(1); exit(1);
} }
void URelaunch() { void URelaunch() {
std::string Arg; int ret = execv(options.executable_name.c_str(), const_cast<char**>(options.argv));
for (int c = 2; c <= options.argc; c++) { if (ret < 0) {
Arg += options.argv[c - 1]; error(std::string("execv() failed with: ") + strerror(errno) + ". Failed to relaunch");
Arg += " "; exit(1);
} }
execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
exit(1); exit(1);
} }

View File

@ -24,7 +24,7 @@ Options options;
} }
} }
int main(int argc, char** argv) try { int main(int argc, const char** argv) try {
#if defined(_WIN32) #if defined(_WIN32)
system("cls"); system("cls");
#elif defined(__linux__) #elif defined(__linux__)