Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command-line options #90

Merged
merged 13 commits into from
Oct 6, 2024
1 change: 0 additions & 1 deletion include/Network/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ extern int ClientID;
extern int LastPort;
extern bool ModLoaded;
extern bool Terminate;
extern int DEFAULT_PORT;
extern uint64_t UDPSock;
extern uint64_t TCPSock;
extern std::string Branch;
Expand Down
20 changes: 20 additions & 0 deletions include/Options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <string>

struct Options {
std::string executable_name = "BeamMP-Launcher.exe";
unsigned int port = 4444;
bool verbose = false;
bool no_download = false;
bool no_update = false;
bool no_launch = false;
char **game_arguments = nullptr;
int game_arguments_length = 0;
char** argv = nullptr;
int argc = 0;
};

void InitOptions(int argc, 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 @@ -10,12 +10,11 @@
#include <string>
#include <vector>

void InitLauncher(int argc, char* argv[]);
void InitLauncher();
std::string GetEP(char* P = nullptr);
std::string GetGamePath();
std::string GetVer();
std::string GetPatch();
std::string GetEN();
void ConfigInit();
extern bool Dev;

11 changes: 10 additions & 1 deletion src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include "Options.h"
namespace fs = std::filesystem;

std::string Branch;
std::string CachingDirectory = "./Resources";

void ParseConfig(const nlohmann::json& d) {
if (d["Port"].is_number()) {
DEFAULT_PORT = d["Port"].get<int>();
options.port = d["Port"].get<int>();
}
// Default -1
// Release 1
Expand All @@ -31,6 +32,14 @@ void ParseConfig(const nlohmann::json& d) {
CachingDirectory = d["CachingDirectory"].get<std::string>();
info("Mod caching directory: " + CachingDirectory);
}

if (d.contains("Dev") && d["Dev"].is_boolean()) {
bool dev = d["Dev"].get<bool>();
options.verbose = dev;
options.no_download = dev;
options.no_launch = dev;
options.no_update = dev;
}
}

void ConfigInit() {
Expand Down
22 changes: 18 additions & 4 deletions src/GameStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <Security/Init.h>
#include <filesystem>
#include <thread>
#include "Options.h"

unsigned long GamePID = 0;
#if defined(_WIN32)
Expand Down Expand Up @@ -83,7 +84,14 @@ void StartGame(std::string Dir) {
std::string BaseDir = Dir; //+"\\Bin64";
// Dir += R"(\Bin64\BeamNG.drive.x64.exe)";
Dir += "\\BeamNG.drive.exe";
bSuccess = CreateProcessA(Dir.c_str(), nullptr, nullptr, nullptr, TRUE, 0, nullptr, BaseDir.c_str(), &si, &pi);
std::string gameArgs = "";

for (int i = 0; i < options.game_arguments_length; i++) {
gameArgs += " ";
gameArgs += options.game_arguments[i];
}

bSuccess = CreateProcessA(nullptr, (LPSTR)(Dir + gameArgs).c_str(), nullptr, nullptr, TRUE, 0, nullptr, BaseDir.c_str(), &si, &pi);
if (bSuccess) {
info("Game Launched!");
GamePID = pi.dwProcessId;
Expand All @@ -99,13 +107,19 @@ void StartGame(std::string Dir) {
void StartGame(std::string Dir) {
int status;
std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64");
char* argv[] = { filename.data(), NULL };
std::vector<char*> argv;
argv.push_back(filename.data());
for (int i = 0; i < options.game_arguments_length; i++) {
argv.push_back(options.game_arguments[i]);
}

argv.push_back(nullptr);
pid_t pid;
posix_spawn_file_actions_t 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, STDERR_FILENO);
int result = posix_spawn(&pid, filename.c_str(), &spawn_actions, nullptr, argv, environ);
int result = posix_spawn(&pid, filename.c_str(), &spawn_actions, nullptr, argv.data(), environ);

if (result != 0) {
error("Failed to Launch the game! launcher closing soon");
Expand All @@ -121,7 +135,7 @@ void StartGame(std::string Dir) {
#endif

void InitGame(const std::string& Dir) {
if (!Dev) {
if (!options.no_launch) {
std::thread Game(StartGame, Dir);
Game.detach();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fstream>
#include <sstream>
#include <thread>
#include "Options.h"

std::string getDate() {
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
Expand Down Expand Up @@ -55,7 +56,7 @@ void info(const std::string& toPrint) {
}
void debug(const std::string& toPrint) {
std::string Print = getDate() + "[DEBUG] " + toPrint + "\n";
if (Dev) {
if (options.verbose) {
std::cout << Print;
}
addToLog(Print);
Expand Down
6 changes: 3 additions & 3 deletions src/Network/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
#include <nlohmann/json.hpp>
#include <set>
#include <thread>
#include "Options.h"

extern int TraceBack;
std::set<std::string>* ConfList = nullptr;
bool TCPTerminate = false;
int DEFAULT_PORT = 4444;
bool Terminate = false;
bool LoginAuth = false;
std::string Username = "";
Expand Down Expand Up @@ -288,7 +288,7 @@ void localRes() {
ConfList = new std::set<std::string>;
}
void CoreMain() {
debug("Core Network on start!");
debug("Core Network on start! port: " + std::to_string(options.port));
SOCKET LSocket, CSocket;
struct addrinfo* res = nullptr;
struct addrinfo hints { };
Expand All @@ -306,7 +306,7 @@ void CoreMain() {
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iRes = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &res);
iRes = getaddrinfo(nullptr, std::to_string(options.port).c_str(), &hints, &res);
if (iRes) {
debug("(Core) addr info failed with error: " + std::to_string(iRes));
WSACleanup();
Expand Down
3 changes: 2 additions & 1 deletion src/Network/GlobalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <mutex>
#include <string>
#include <thread>
#include "Options.h"

std::chrono::time_point<std::chrono::high_resolution_clock> PingStart, PingEnd;
bool GConnected = false;
Expand Down Expand Up @@ -161,7 +162,7 @@ SOCKET SetupListener() {
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iRes = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT + 1).c_str(), &hints, &result);
iRes = getaddrinfo(nullptr, std::to_string(options.port + 1).c_str(), &hints, &result);
if (iRes != 0) {
error("(Proxy) info failed with error: " + std::to_string(iRes));
WSACleanup();
Expand Down
81 changes: 81 additions & 0 deletions src/Options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "Options.h"

#include "Logger.h"
#include <cstdlib>

void InitOptions(int argc, char *argv[], Options &options) {
int i = 1;

options.argc = argc;
options.argv = argv;

if (argc > 2)
if (std::string(argv[1]) == "0" && std::string(argv[2]) == "0") {
options.verbose = true;
options.no_download = true;
options.no_launch = true;
options.no_update = true;
warn("You are using deprecated commandline arguments, please use --dev instead");
return;
}

options.executable_name = std::string(argv[0]);

while (i < argc) {
std::string argument(argv[i]);
if (argument == "-p" || argument == "--port") {
if (argc > i) {
std::string error_message =
"No port specified, resorting to default (";
error_message += std::to_string(options.port);
error_message += ")";
error(error_message);
i++;
continue;
}

int port = options.port;

try {
port = std::stoi(argv[i + 1]);
} catch (std::exception& e) {
error("Invalid port specified: " + std::string(argv[i + 1]) + " " + std::string(e.what()));
}

if (port <= 0) {
std::string error_message =
"Port invalid, must be a non-zero positive "
"integer, resorting to default (";
error_message += options.port;
error_message += ")";
error(error_message);
i++;
continue;
}

options.port = port;
i++;
} else if (argument == "-v" || argument == "--verbose") {
options.verbose = true;
} else if (argument == "--no-download") {
options.no_download = true;
} else if (argument == "--no-update") {
options.no_update = true;
} else if (argument == "--no-launch") {
options.no_launch = true;
} else if (argument == "--dev") {
options.verbose = true;
options.no_download = true;
options.no_launch = true;
options.no_update = true;
} else if (argument == "--game") {
options.game_arguments = &argv[i + 1];
options.game_arguments_length = argc - i - 1;
break;
} else {
warn("Unknown option: " + argument);
}

i++;
}
}
Loading
Loading