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>
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;

View File

@ -11,10 +11,9 @@
#include <vector>
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();

View File

@ -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<char*> argv;
std::vector<const char*> 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<char**>(argv.data()), environ);
if (result != 0) {
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()) {
File << Ret;
File.close();
std::cout << "\n";
info("Download Complete!");
} else {
error("Failed to open file directory: " + Path);

View File

@ -2,14 +2,15 @@
#include "Logger.h"
#include <cstdlib>
#include <filesystem>
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] [-- <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 {
warn("Unknown option: " + argument);
}

View File

@ -8,6 +8,7 @@
#include "zip_file.h"
#include <charconv>
#include <cstring>
#include <httplib.h>
#include <nlohmann/json.hpp>
#include <string>
@ -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<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));
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<char**>(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);
}

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)
system("cls");
#elif defined(__linux__)