Add command-line options (#90)

This PR adds command-line options as outlined in #74 

Closes #74
This commit is contained in:
Lion 2024-10-06 23:49:47 +02:00 committed by GitHub
commit 259b21502e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 192 additions and 67 deletions

View File

@ -26,7 +26,6 @@ extern int ClientID;
extern int LastPort; extern int LastPort;
extern bool ModLoaded; extern bool ModLoaded;
extern bool Terminate; extern bool Terminate;
extern int DEFAULT_PORT;
extern uint64_t UDPSock; extern uint64_t UDPSock;
extern uint64_t TCPSock; extern uint64_t TCPSock;
extern std::string Branch; extern std::string Branch;

20
include/Options.h Normal file
View File

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

View File

@ -10,12 +10,11 @@
#include <string> #include <string>
#include <vector> #include <vector>
void InitLauncher(int argc, char* argv[]); void InitLauncher();
std::string GetEP(char* P = nullptr); std::string GetEP(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();
extern bool Dev;

View File

@ -8,6 +8,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "Options.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
std::string Branch; std::string Branch;
@ -15,7 +16,7 @@ std::string CachingDirectory = "./Resources";
void ParseConfig(const nlohmann::json& d) { void ParseConfig(const nlohmann::json& d) {
if (d["Port"].is_number()) { if (d["Port"].is_number()) {
DEFAULT_PORT = d["Port"].get<int>(); options.port = d["Port"].get<int>();
} }
// Default -1 // Default -1
// Release 1 // Release 1
@ -31,6 +32,14 @@ void ParseConfig(const nlohmann::json& d) {
CachingDirectory = d["CachingDirectory"].get<std::string>(); CachingDirectory = d["CachingDirectory"].get<std::string>();
info("Mod caching directory: " + CachingDirectory); 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() { void ConfigInit() {

View File

@ -23,6 +23,7 @@
#include <Security/Init.h> #include <Security/Init.h>
#include <filesystem> #include <filesystem>
#include <thread> #include <thread>
#include "Options.h"
unsigned long GamePID = 0; unsigned long GamePID = 0;
#if defined(_WIN32) #if defined(_WIN32)
@ -83,7 +84,14 @@ void StartGame(std::string Dir) {
std::string BaseDir = Dir; //+"\\Bin64"; std::string BaseDir = Dir; //+"\\Bin64";
// Dir += R"(\Bin64\BeamNG.drive.x64.exe)"; // Dir += R"(\Bin64\BeamNG.drive.x64.exe)";
Dir += "\\BeamNG.drive.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) { if (bSuccess) {
info("Game Launched!"); info("Game Launched!");
GamePID = pi.dwProcessId; GamePID = pi.dwProcessId;
@ -99,13 +107,19 @@ 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");
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; pid_t pid;
posix_spawn_file_actions_t spawn_actions; posix_spawn_file_actions_t spawn_actions;
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, environ); int result = posix_spawn(&pid, filename.c_str(), &spawn_actions, nullptr, 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");
@ -121,7 +135,7 @@ void StartGame(std::string Dir) {
#endif #endif
void InitGame(const std::string& Dir) { void InitGame(const std::string& Dir) {
if (!Dev) { if (!options.no_launch) {
std::thread Game(StartGame, Dir); std::thread Game(StartGame, Dir);
Game.detach(); Game.detach();
} }

View File

@ -12,6 +12,7 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include "Options.h"
std::string getDate() { std::string getDate() {
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
@ -55,7 +56,7 @@ void info(const std::string& toPrint) {
} }
void debug(const std::string& toPrint) { void debug(const std::string& toPrint) {
std::string Print = getDate() + "[DEBUG] " + toPrint + "\n"; std::string Print = getDate() + "[DEBUG] " + toPrint + "\n";
if (Dev) { if (options.verbose) {
std::cout << Print; std::cout << Print;
} }
addToLog(Print); addToLog(Print);

View File

@ -30,11 +30,11 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <set> #include <set>
#include <thread> #include <thread>
#include "Options.h"
extern int TraceBack; extern int TraceBack;
std::set<std::string>* ConfList = nullptr; std::set<std::string>* ConfList = nullptr;
bool TCPTerminate = false; bool TCPTerminate = false;
int DEFAULT_PORT = 4444;
bool Terminate = false; bool Terminate = false;
bool LoginAuth = false; bool LoginAuth = false;
std::string Username = ""; std::string Username = "";
@ -288,7 +288,7 @@ void localRes() {
ConfList = new std::set<std::string>; ConfList = new std::set<std::string>;
} }
void CoreMain() { void CoreMain() {
debug("Core Network on start!"); debug("Core Network on start! port: " + std::to_string(options.port));
SOCKET LSocket, CSocket; SOCKET LSocket, CSocket;
struct addrinfo* res = nullptr; struct addrinfo* res = nullptr;
struct addrinfo hints { }; struct addrinfo hints { };
@ -306,7 +306,7 @@ void CoreMain() {
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; 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) { if (iRes) {
debug("(Core) addr info failed with error: " + std::to_string(iRes)); debug("(Core) addr info failed with error: " + std::to_string(iRes));
WSACleanup(); WSACleanup();

View File

@ -26,6 +26,7 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include "Options.h"
std::chrono::time_point<std::chrono::high_resolution_clock> PingStart, PingEnd; std::chrono::time_point<std::chrono::high_resolution_clock> PingStart, PingEnd;
bool GConnected = false; bool GConnected = false;
@ -161,7 +162,7 @@ SOCKET SetupListener() {
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; 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) { if (iRes != 0) {
error("(Proxy) info failed with error: " + std::to_string(iRes)); error("(Proxy) info failed with error: " + std::to_string(iRes));
WSACleanup(); WSACleanup();

81
src/Options.cpp Normal file
View File

@ -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++;
}
}

View File

@ -25,9 +25,9 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <thread> #include <thread>
#include "Options.h"
extern int TraceBack; extern int TraceBack;
bool Dev = false;
int ProxyPort = 0; int ProxyPort = 0;
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -95,11 +95,11 @@ std::string GetEP(char* P) {
return Ret; return Ret;
} }
#if defined(_WIN32) #if defined(_WIN32)
void ReLaunch(int argc, char* args[]) { void ReLaunch() {
std::string Arg; std::string Arg;
for (int c = 2; c <= argc; c++) { for (int c = 2; c <= options.argc; c++) {
Arg += options.argv[c - 1];
Arg += " "; Arg += " ";
Arg += args[c - 1];
} }
info("Relaunch!"); info("Relaunch!");
system("cls"); system("cls");
@ -108,11 +108,11 @@ void ReLaunch(int argc, char* args[]) {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
exit(1); exit(1);
} }
void URelaunch(int argc, char* args[]) { void URelaunch() {
std::string Arg; std::string Arg;
for (int c = 2; c <= argc; c++) { for (int c = 2; c <= options.argc; c++) {
Arg += options.argv[c - 1];
Arg += " "; Arg += " ";
Arg += args[c - 1];
} }
ShellExecute(nullptr, "open", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL); ShellExecute(nullptr, "open", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0); ShowWindow(GetConsoleWindow(), 0);
@ -120,11 +120,11 @@ void URelaunch(int argc, char* args[]) {
exit(1); exit(1);
} }
#elif defined(__linux__) #elif defined(__linux__)
void ReLaunch(int argc, char* args[]) { void ReLaunch() {
std::string Arg; std::string Arg;
for (int c = 2; c <= argc; c++) { for (int c = 2; c <= options.argc; c++) {
Arg += options.argv[c - 1];
Arg += " "; Arg += " ";
Arg += args[c - 1];
} }
info("Relaunch!"); info("Relaunch!");
system("clear"); system("clear");
@ -132,11 +132,11 @@ void ReLaunch(int argc, char* args[]) {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
exit(1); exit(1);
} }
void URelaunch(int argc, char* args[]) { void URelaunch() {
std::string Arg; std::string Arg;
for (int c = 2; c <= argc; c++) { for (int c = 2; c <= options.argc; c++) {
Arg += options.argv[c - 1];
Arg += " "; Arg += " ";
Arg += args[c - 1];
} }
execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL); 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));
@ -144,23 +144,23 @@ void URelaunch(int argc, char* args[]) {
} }
#endif #endif
void CheckName(int argc, char* args[]) { void CheckName() {
#if defined(_WIN32) #if defined(_WIN32)
std::string DN = GetEN(), CDir = args[0], FN = CDir.substr(CDir.find_last_of('\\') + 1); std::string DN = GetEN(), CDir = options.executable_name, FN = CDir.substr(CDir.find_last_of('\\') + 1);
#elif defined(__linux__) #elif defined(__linux__)
std::string DN = GetEN(), CDir = args[0], FN = CDir.substr(CDir.find_last_of('/') + 1); std::string DN = GetEN(), CDir = options.executable_name, FN = CDir.substr(CDir.find_last_of('/') + 1);
#endif #endif
if (FN != DN) { if (FN != DN) {
if (fs::exists(DN)) if (fs::exists(DN))
remove(DN.c_str()); remove(DN.c_str());
if (fs::exists(DN)) if (fs::exists(DN))
ReLaunch(argc, args); ReLaunch();
std::rename(FN.c_str(), DN.c_str()); std::rename(FN.c_str(), DN.c_str());
URelaunch(argc, args); URelaunch();
} }
} }
void CheckForUpdates(int argc, char* args[], const std::string& CV) { void CheckForUpdates(const std::string& CV) {
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey); std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
std::string LatestVersion = HTTP::Get( std::string LatestVersion = HTTP::Get(
"https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey); "https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
@ -170,39 +170,30 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) {
std::string FileHash = hashpp::get::getFileHash(hashpp::ALGORITHMS::SHA2_256, EP); std::string FileHash = hashpp::get::getFileHash(hashpp::ALGORITHMS::SHA2_256, EP);
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !Dev) { if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) {
info("Launcher update found!"); if (!options.no_update) {
info("Launcher update found!");
#if defined(__linux__) #if defined(__linux__)
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches."); error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
#else #else
fs::remove(Back); fs::remove(Back);
fs::rename(EP, Back); fs::rename(EP, Back);
info("Downloading Launcher update " + LatestHash); info("Downloading Launcher update " + LatestHash);
HTTP::Download( HTTP::Download(
"https://backend.beammp.com/builds/launcher?download=true" "https://backend.beammp.com/builds/launcher?download=true"
"&pk=" "&pk="
+ PublicKey + "&branch=" + Branch, + PublicKey + "&branch=" + Branch,
EP); EP);
URelaunch(argc, args); URelaunch();
#endif #endif
} else {
warn("Launcher update was found, but not updating because --no-update or --dev was specified.");
}
} else } else
info("Launcher version is up to date"); info("Launcher version is up to date");
TraceBack++; TraceBack++;
} }
void CustomPort(int argc, char* argv[]) {
if (argc > 1) {
std::string Port = argv[1];
if (Port.find_first_not_of("0123456789") == std::string::npos) {
if (std::stoi(Port) > 1000) {
DEFAULT_PORT = std::stoi(Port);
warn("Running on custom port : " + std::to_string(DEFAULT_PORT));
}
}
if (argc > 2)
Dev = true;
}
}
#ifdef _WIN32 #ifdef _WIN32
void LinuxPatch() { void LinuxPatch() {
@ -234,25 +225,25 @@ void LinuxPatch() {
#endif #endif
#if defined(_WIN32) #if defined(_WIN32)
void InitLauncher(int argc, char* argv[]) {
void InitLauncher() {
SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str()); SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str());
InitLog(); InitLog();
CheckName(argc, argv); CheckName();
LinuxPatch(); LinuxPatch();
CheckLocalKey(); CheckLocalKey();
ConfigInit(); ConfigInit();
CustomPort(argc, argv); CheckForUpdates(std::string(GetVer()) + GetPatch());
CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch());
} }
#elif defined(__linux__) #elif defined(__linux__)
void InitLauncher(int argc, char* argv[]) {
void InitLauncher() {
InitLog(); InitLog();
info("BeamMP Launcher v" + GetVer() + GetPatch()); info("BeamMP Launcher v" + GetVer() + GetPatch());
CheckName(argc, argv); CheckName();
CheckLocalKey(); CheckLocalKey();
ConfigInit(); ConfigInit();
CustomPort(argc, argv); CheckForUpdates(std::string(GetVer()) + GetPatch());
CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch());
} }
#endif #endif
@ -316,7 +307,7 @@ void PreGame(const std::string& GamePath) {
CheckMP(GetGamePath() + "mods/multiplayer"); CheckMP(GetGamePath() + "mods/multiplayer");
info("Game user path: " + GetGamePath()); info("Game user path: " + GetGamePath());
if (!Dev) { if (!options.no_download) {
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey); std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower); transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
LatestHash.erase(std::remove_if(LatestHash.begin(), LatestHash.end(), LatestHash.erase(std::remove_if(LatestHash.begin(), LatestHash.end(),

View File

@ -13,6 +13,9 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include "Options.h"
Options options;
[[noreturn]] void flush() { [[noreturn]] void flush() {
while (true) { while (true) {
@ -22,6 +25,12 @@
} }
int main(int argc, char** argv) try { int main(int argc, char** argv) try {
#if defined(_WIN32)
system("cls");
#elif defined(__linux__)
system("clear");
#endif
#ifdef DEBUG #ifdef DEBUG
std::thread th(flush); std::thread th(flush);
th.detach(); th.detach();
@ -45,7 +54,8 @@ int main(int argc, char** argv) try {
} }
} }
InitLauncher(argc, argv); InitOptions(argc, argv, options);
InitLauncher();
info("IMPORTANT: You MUST keep this window open to play BeamMP!"); info("IMPORTANT: You MUST keep this window open to play BeamMP!");