Skip to content

Commit

Permalink
fix various commandline argument related things
Browse files Browse the repository at this point in the history
  • Loading branch information
lionkor committed Oct 6, 2024
1 parent 0eba745 commit 7b59cb6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 20 deletions.
10 changes: 7 additions & 3 deletions include/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 1 addition & 2 deletions include/Startup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

4 changes: 2 additions & 2 deletions src/GameStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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");
Expand Down
1 change: 0 additions & 1 deletion src/Network/Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 20 additions & 4 deletions src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);

Expand All @@ -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()));
}

Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 11 additions & 7 deletions src/Startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "zip_file.h"
#include <charconv>
#include <cstring>
#include <httplib.h>
#include <nlohmann/json.hpp>
#include <string>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down

0 comments on commit 7b59cb6

Please sign in to comment.