From aca61886d057e578b352565e07b202440e27fbd3 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:02:36 +0200 Subject: [PATCH 01/13] add command-line options --- include/Network/network.hpp | 1 - include/Options.h | 17 ++++++++ include/Startup.h | 3 +- src/Config.cpp | 3 +- src/GameStart.cpp | 3 +- src/Logger.cpp | 3 ++ src/Network/Core.cpp | 4 +- src/Network/GlobalHandler.cpp | 3 +- src/Options.cpp | 57 ++++++++++++++++++++++++++ src/Startup.cpp | 75 +++++++++++++++-------------------- src/main.cpp | 6 ++- 11 files changed, 123 insertions(+), 52 deletions(-) create mode 100644 include/Options.h create mode 100644 src/Options.cpp diff --git a/include/Network/network.hpp b/include/Network/network.hpp index 406f914e..287c6259 100644 --- a/include/Network/network.hpp +++ b/include/Network/network.hpp @@ -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; diff --git a/include/Options.h b/include/Options.h new file mode 100644 index 00000000..dd161a9a --- /dev/null +++ b/include/Options.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +struct Options { + std::string executable_name = "BeamMP-Launcher.exe"; + unsigned int port = 4444; + bool verbose = false; + bool no_download = false; + bool no_launch = false; + char **game_arguments = nullptr; + int game_arguments_length = 0; +}; + +void InitOptions(int argc, char *argv[], Options &options); + +extern Options options; diff --git a/include/Startup.h b/include/Startup.h index 17e5bff3..b8b84b00 100644 --- a/include/Startup.h +++ b/include/Startup.h @@ -10,12 +10,11 @@ #include #include -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; diff --git a/src/Config.cpp b/src/Config.cpp index 6bfcd5db..87bcec8c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "Options.h" namespace fs = std::filesystem; std::string Branch; @@ -15,7 +16,7 @@ std::string CachingDirectory = "./Resources"; void ParseConfig(const nlohmann::json& d) { if (d["Port"].is_number()) { - DEFAULT_PORT = d["Port"].get(); + options.port = d["Port"].get(); } // Default -1 // Release 1 diff --git a/src/GameStart.cpp b/src/GameStart.cpp index 48c7d814..ab5645ed 100644 --- a/src/GameStart.cpp +++ b/src/GameStart.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "Options.h" unsigned long GamePID = 0; #if defined(_WIN32) @@ -121,7 +122,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(); } diff --git a/src/Logger.cpp b/src/Logger.cpp index 601702c8..71623189 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "Options.h" std::string getDate() { time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); @@ -54,6 +55,8 @@ void info(const std::string& toPrint) { addToLog(Print); } void debug(const std::string& toPrint) { + if (!options.verbose) + return; std::string Print = getDate() + "[DEBUG] " + toPrint + "\n"; if (Dev) { std::cout << Print; diff --git a/src/Network/Core.cpp b/src/Network/Core.cpp index c3d18c40..dc41b1ae 100644 --- a/src/Network/Core.cpp +++ b/src/Network/Core.cpp @@ -30,11 +30,11 @@ #include #include #include +#include "Options.h" extern int TraceBack; std::set* ConfList = nullptr; bool TCPTerminate = false; -int DEFAULT_PORT = 4444; bool Terminate = false; bool LoginAuth = false; std::string Username = ""; @@ -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(); diff --git a/src/Network/GlobalHandler.cpp b/src/Network/GlobalHandler.cpp index 272128da..cde06848 100644 --- a/src/Network/GlobalHandler.cpp +++ b/src/Network/GlobalHandler.cpp @@ -26,6 +26,7 @@ #include #include #include +#include "Options.h" std::chrono::time_point PingStart, PingEnd; bool GConnected = false; @@ -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(); diff --git a/src/Options.cpp b/src/Options.cpp new file mode 100644 index 00000000..64a631c8 --- /dev/null +++ b/src/Options.cpp @@ -0,0 +1,57 @@ +#include "Options.h" + +#include "Logger.h" +#include + +void InitOptions(int argc, char *argv[], Options &options) { + int i = 1; + + options.executable_name = std::string(argv[0]); + + while (i < argc) { + std::string argument(argv[i]); + if (argument == "-p" || argument == "--port") { + if (argc > i + 1) { + std::string error_message = + "No port specified, resorting to default ("; + error_message += options.port; + error_message += ")"; + error(error_message); + continue; + } + + int port = atoi(argv[i + 1]); + + 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); + 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-launch") { + options.no_launch = true; + } else if (argument == "--dev") { + options.verbose = true; + options.no_download = true; + options.no_launch = true; + } else if (argument == "--") { + options.game_arguments = &argv[i + 1]; + options.game_arguments_length = argc - i - 1; + break; + } else { + warn("Unknown option: " + argument); + } + + i++; + } +} diff --git a/src/Startup.cpp b/src/Startup.cpp index 321d8a86..84509f54 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -25,9 +25,9 @@ #include #include #include +#include "Options.h" extern int TraceBack; -bool Dev = false; int ProxyPort = 0; namespace fs = std::filesystem; @@ -95,11 +95,11 @@ std::string GetEP(char* P) { return Ret; } #if defined(_WIN32) -void ReLaunch(int argc, char* args[]) { +void ReLaunch() { std::string Arg; - for (int c = 2; c <= argc; c++) { + for (int c = 0; c <= options.game_arguments_length; c++) { Arg += " "; - Arg += args[c - 1]; + Arg += options.game_arguments[c - 1]; } info("Relaunch!"); system("cls"); @@ -108,11 +108,11 @@ void ReLaunch(int argc, char* args[]) { std::this_thread::sleep_for(std::chrono::seconds(1)); exit(1); } -void URelaunch(int argc, char* args[]) { +void URelaunch() { std::string Arg; - for (int c = 2; c <= argc; c++) { + for (int c = 0; c <= options.game_arguments_length; c++) { Arg += " "; - Arg += args[c - 1]; + Arg += options.game_arguments[c - 1]; } ShellExecute(nullptr, "open", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL); ShowWindow(GetConsoleWindow(), 0); @@ -120,11 +120,11 @@ void URelaunch(int argc, char* args[]) { exit(1); } #elif defined(__linux__) -void ReLaunch(int argc, char* args[]) { +void ReLaunch() { std::string Arg; - for (int c = 2; c <= argc; c++) { + for (int c = 0; c <= options.game_arguments_length; c++) { Arg += " "; - Arg += args[c - 1]; + Arg += options.game_arguments[c - 1]; } info("Relaunch!"); system("clear"); @@ -132,11 +132,11 @@ void ReLaunch(int argc, char* args[]) { std::this_thread::sleep_for(std::chrono::seconds(1)); exit(1); } -void URelaunch(int argc, char* args[]) { +void URelaunch() { std::string Arg; - for (int c = 2; c <= argc; c++) { + for (int c = 0; c <= options.game_arguments_length; c++) { Arg += " "; - Arg += args[c - 1]; + Arg += options.game_arguments[c - 1]; } execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL); std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -144,23 +144,23 @@ void URelaunch(int argc, char* args[]) { } #endif -void CheckName(int argc, char* args[]) { +void CheckName() { #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__) - 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 if (FN != DN) { if (fs::exists(DN)) remove(DN.c_str()); if (fs::exists(DN)) - ReLaunch(argc, args); + ReLaunch(); 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 LatestVersion = HTTP::Get( "https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey); @@ -170,7 +170,7 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) { 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))) && !options.no_download) { info("Launcher update found!"); #if defined(__linux__) error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches."); @@ -183,26 +183,13 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) { "&pk=" + PublicKey + "&branch=" + Branch, EP); - URelaunch(argc, args); + URelaunch(); #endif } else info("Launcher version is up to date"); 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 void LinuxPatch() { @@ -234,25 +221,27 @@ void LinuxPatch() { #endif #if defined(_WIN32) -void InitLauncher(int argc, char* argv[]) { + +void InitLauncher() { + system("cls"); SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str()); InitLog(); - CheckName(argc, argv); + CheckName(); LinuxPatch(); CheckLocalKey(); ConfigInit(); - CustomPort(argc, argv); - CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch()); + CheckForUpdates(std::string(GetVer()) + GetPatch()); } #elif defined(__linux__) -void InitLauncher(int argc, char* argv[]) { + +void InitLauncher() { + system("clear"); InitLog(); info("BeamMP Launcher v" + GetVer() + GetPatch()); - CheckName(argc, argv); + CheckName(); CheckLocalKey(); ConfigInit(); - CustomPort(argc, argv); - CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch()); + CheckForUpdates(std::string(GetVer()) + GetPatch()); } #endif @@ -316,7 +305,7 @@ void PreGame(const std::string& GamePath) { CheckMP(GetGamePath() + "mods/multiplayer"); 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); transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower); LatestHash.erase(std::remove_if(LatestHash.begin(), LatestHash.end(), diff --git a/src/main.cpp b/src/main.cpp index d3b20c8e..325ffec3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,9 @@ #include #include #include +#include "Options.h" + +Options options; [[noreturn]] void flush() { while (true) { @@ -45,7 +48,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!"); From 06686688fc0e3445fa68a84b1da6253d77a33e26 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sun, 22 Sep 2024 23:57:26 +0200 Subject: [PATCH 02/13] Move console clear to main to avoid clearing logs --- src/Startup.cpp | 2 -- src/main.cpp | 6 ++++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Startup.cpp b/src/Startup.cpp index 84509f54..bdce1889 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -223,7 +223,6 @@ void LinuxPatch() { #if defined(_WIN32) void InitLauncher() { - system("cls"); SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str()); InitLog(); CheckName(); @@ -235,7 +234,6 @@ void InitLauncher() { #elif defined(__linux__) void InitLauncher() { - system("clear"); InitLog(); info("BeamMP Launcher v" + GetVer() + GetPatch()); CheckName(); diff --git a/src/main.cpp b/src/main.cpp index 325ffec3..8305f44e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,12 @@ Options options; } int main(int argc, char** argv) try { +#if defined(_WIN32) + system("cls"); +#elif defined(__linux__) + system("clear"); +#endif + #ifdef DEBUG std::thread th(flush); th.detach(); From e242057583774d24bafc3dd3e574d6f950bef4a8 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sun, 22 Sep 2024 23:58:32 +0200 Subject: [PATCH 03/13] Improve port cli flag --- src/Options.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index 64a631c8..9cd9b7cd 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -11,16 +11,23 @@ void InitOptions(int argc, char *argv[], Options &options) { while (i < argc) { std::string argument(argv[i]); if (argument == "-p" || argument == "--port") { - if (argc > i + 1) { + if (argc > i) { std::string error_message = "No port specified, resorting to default ("; - error_message += options.port; + error_message += std::to_string(options.port); error_message += ")"; error(error_message); + i++; continue; } - int port = atoi(argv[i + 1]); + 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 = @@ -29,6 +36,7 @@ void InitOptions(int argc, char *argv[], Options &options) { error_message += options.port; error_message += ")"; error(error_message); + i++; continue; } From 2ddb576e7291aee3e933f7338ae982a73ba07421 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sun, 22 Sep 2024 23:59:00 +0200 Subject: [PATCH 04/13] Log core port --- src/Network/Core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Core.cpp b/src/Network/Core.cpp index dc41b1ae..d8eefc01 100644 --- a/src/Network/Core.cpp +++ b/src/Network/Core.cpp @@ -288,7 +288,7 @@ void localRes() { ConfList = new std::set; } 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 { }; From 467c8dc5848bad31fd94b0fe902e03e5d2db2035 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Mon, 23 Sep 2024 00:00:27 +0200 Subject: [PATCH 05/13] Add no-update flag --- include/Options.h | 1 + src/Options.cpp | 3 +++ src/Startup.cpp | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/Options.h b/include/Options.h index dd161a9a..73a5c68b 100644 --- a/include/Options.h +++ b/include/Options.h @@ -7,6 +7,7 @@ struct Options { 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; diff --git a/src/Options.cpp b/src/Options.cpp index 9cd9b7cd..37acf48b 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -46,12 +46,15 @@ void InitOptions(int argc, char *argv[], Options &options) { 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 == "--") { options.game_arguments = &argv[i + 1]; options.game_arguments_length = argc - i - 1; diff --git a/src/Startup.cpp b/src/Startup.cpp index bdce1889..67c94198 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -170,7 +170,7 @@ void CheckForUpdates(const std::string& CV) { std::string FileHash = hashpp::get::getFileHash(hashpp::ALGORITHMS::SHA2_256, EP); - if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !options.no_download) { + if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !options.no_update) { info("Launcher update found!"); #if defined(__linux__) error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches."); From 57422a61053ee06e0512dbae3a62324d3d5c6554 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Mon, 23 Sep 2024 00:00:39 +0200 Subject: [PATCH 06/13] Add optional dev config value --- src/Config.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Config.cpp b/src/Config.cpp index 87bcec8c..09009154 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -32,6 +32,14 @@ void ParseConfig(const nlohmann::json& d) { CachingDirectory = d["CachingDirectory"].get(); info("Mod caching directory: " + CachingDirectory); } + + if (d.contains("Dev") && d["Dev"].is_boolean()) { + bool dev = d["Dev"].get(); + options.verbose = dev; + options.no_download = dev; + options.no_launch = dev; + options.no_update = dev; + } } void ConfigInit() { From d26e0320e47e18027e0d6101773a5837734f5823 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:07:58 +0200 Subject: [PATCH 07/13] Add back support for old dev argument with warning --- src/Options.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Options.cpp b/src/Options.cpp index 37acf48b..665d1ff5 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -6,6 +6,16 @@ void InitOptions(int argc, char *argv[], Options &options) { int i = 1; + 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) { From c99fecfa1cde647f7d3b4ec6b70aecef18878d34 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Fri, 4 Oct 2024 23:19:26 +0200 Subject: [PATCH 08/13] Fix debug log --- src/Logger.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Logger.cpp b/src/Logger.cpp index 71623189..860ed46d 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -55,10 +55,8 @@ void info(const std::string& toPrint) { addToLog(Print); } void debug(const std::string& toPrint) { - if (!options.verbose) - return; std::string Print = getDate() + "[DEBUG] " + toPrint + "\n"; - if (Dev) { + if (options.verbose) { std::cout << Print; } addToLog(Print); From 47681cda50d3817520d18dd3b21fe241a7c14ef0 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sat, 5 Oct 2024 18:01:36 +0200 Subject: [PATCH 09/13] Let user know about update even if --no-update was specified --- src/Startup.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Startup.cpp b/src/Startup.cpp index 67c94198..73394e9a 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -170,20 +170,24 @@ void CheckForUpdates(const std::string& CV) { std::string FileHash = hashpp::get::getFileHash(hashpp::ALGORITHMS::SHA2_256, EP); - if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion))) && !options.no_update) { - info("Launcher update found!"); + if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) { + if (!options.no_update) { + info("Launcher update found!"); #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 - fs::remove(Back); - fs::rename(EP, Back); - info("Downloading Launcher update " + LatestHash); - HTTP::Download( - "https://backend.beammp.com/builds/launcher?download=true" - "&pk=" - + PublicKey + "&branch=" + Branch, - EP); - URelaunch(); + fs::remove(Back); + fs::rename(EP, Back); + info("Downloading Launcher update " + LatestHash); + HTTP::Download( + "https://backend.beammp.com/builds/launcher?download=true" + "&pk=" + + PublicKey + "&branch=" + Branch, + EP); + URelaunch(); + } else { + warn("Launcher update was found, but not updating because --no-update or --dev was specified."); + } #endif } else info("Launcher version is up to date"); From 5bdd8c11da7dbb56ef078865dcd08d96d6d85071 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sat, 5 Oct 2024 18:01:47 +0200 Subject: [PATCH 10/13] Fix relaunch --- include/Options.h | 2 ++ src/Options.cpp | 3 +++ src/Startup.cpp | 16 ++++++++-------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/Options.h b/include/Options.h index 73a5c68b..683a4efa 100644 --- a/include/Options.h +++ b/include/Options.h @@ -11,6 +11,8 @@ struct Options { 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); diff --git a/src/Options.cpp b/src/Options.cpp index 665d1ff5..caa0db84 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -6,6 +6,9 @@ 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; diff --git a/src/Startup.cpp b/src/Startup.cpp index 73394e9a..6c688f6e 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -97,9 +97,9 @@ std::string GetEP(char* P) { #if defined(_WIN32) void ReLaunch() { std::string Arg; - for (int c = 0; c <= options.game_arguments_length; c++) { + for (int c = 2; c <= options.argc; c++) { + Arg += options.argv[c - 1]; Arg += " "; - Arg += options.game_arguments[c - 1]; } info("Relaunch!"); system("cls"); @@ -110,9 +110,9 @@ void ReLaunch() { } void URelaunch() { std::string Arg; - for (int c = 0; c <= options.game_arguments_length; c++) { + for (int c = 2; c <= options.argc; c++) { + Arg += options.argv[c - 1]; Arg += " "; - Arg += options.game_arguments[c - 1]; } ShellExecute(nullptr, "open", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL); ShowWindow(GetConsoleWindow(), 0); @@ -122,9 +122,9 @@ void URelaunch() { #elif defined(__linux__) void ReLaunch() { std::string Arg; - for (int c = 0; c <= options.game_arguments_length; c++) { + for (int c = 2; c <= options.argc; c++) { + Arg += options.argv[c - 1]; Arg += " "; - Arg += options.game_arguments[c - 1]; } info("Relaunch!"); system("clear"); @@ -134,9 +134,9 @@ void ReLaunch() { } void URelaunch() { std::string Arg; - for (int c = 0; c <= options.game_arguments_length; c++) { + for (int c = 2; c <= options.argc; c++) { + Arg += options.argv[c - 1]; Arg += " "; - Arg += options.game_arguments[c - 1]; } execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL); std::this_thread::sleep_for(std::chrono::seconds(1)); From 68a4d64387b1d424e9e84a6c52421391d5a5fa59 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sat, 5 Oct 2024 18:08:25 +0200 Subject: [PATCH 11/13] Fix linux relauch --- src/Startup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Startup.cpp b/src/Startup.cpp index 6c688f6e..865e9759 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -185,10 +185,10 @@ void CheckForUpdates(const std::string& CV) { + PublicKey + "&branch=" + Branch, EP); URelaunch(); +#endif } else { warn("Launcher update was found, but not updating because --no-update or --dev was specified."); } -#endif } else info("Launcher version is up to date"); TraceBack++; From 0c53ff4cd44125bef6e108e5374c7c88f118395f Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sat, 5 Oct 2024 18:34:39 +0200 Subject: [PATCH 12/13] Pass game arguments to beamng on windows --- src/GameStart.cpp | 9 ++++++++- src/Options.cpp | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/GameStart.cpp b/src/GameStart.cpp index ab5645ed..844e20b2 100644 --- a/src/GameStart.cpp +++ b/src/GameStart.cpp @@ -84,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; diff --git a/src/Options.cpp b/src/Options.cpp index caa0db84..8dfa7880 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -68,7 +68,7 @@ void InitOptions(int argc, char *argv[], Options &options) { options.no_download = true; options.no_launch = true; options.no_update = true; - } else if (argument == "--") { + } else if (argument == "--game") { options.game_arguments = &argv[i + 1]; options.game_arguments_length = argc - i - 1; break; From afac7295050457fcb005b2634a1eed1f27bcc40e Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sun, 6 Oct 2024 15:52:31 +0200 Subject: [PATCH 13/13] Ixmplement game arguments for linux --- src/GameStart.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/GameStart.cpp b/src/GameStart.cpp index 844e20b2..d8c0cc34 100644 --- a/src/GameStart.cpp +++ b/src/GameStart.cpp @@ -107,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 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");