Skip to content

Commit

Permalink
add command-line options
Browse files Browse the repository at this point in the history
  • Loading branch information
WiserTixx committed Sep 22, 2024
1 parent 2d43e11 commit 5f84a55
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 53 deletions.
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
17 changes: 17 additions & 0 deletions include/Options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#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_launch = false;
char **game_arguments = nullptr;
int game_arguments_length = 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;

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

std::string Branch;
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 Down
3 changes: 2 additions & 1 deletion src/GameStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <Security/Init.h>
#include <filesystem>
#include <thread>
#include "Options.h"

unsigned long GamePID = 0;
#if defined(_WIN32)
Expand Down Expand Up @@ -112,7 +113,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 @@ -54,7 +55,7 @@ void info(const std::string& toPrint) {
addToLog(Print);
}
void debug(const std::string& toPrint) {
if (!Dev)
if (!options.verbose)
return;
std::string Print = getDate() + "[DEBUG] " + toPrint + "\n";
std::cout << Print;
Expand Down
4 changes: 2 additions & 2 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 @@ -273,7 +273,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 @@ -25,6 +25,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 @@ -160,7 +161,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
57 changes: 57 additions & 0 deletions src/Options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "Options.h"

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

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++;
}
}
71 changes: 28 additions & 43 deletions src/Startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#include <filesystem>
#include <fstream>
#include <thread>
#include "Options.h"

extern int TraceBack;
bool Dev = false;
int ProxyPort = 0;

namespace fs = std::filesystem;
Expand Down Expand Up @@ -95,70 +95,70 @@ 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];
}
system("cls");
ShellExecute(nullptr, "runas", (GetEP() + GetEN()).c_str(), Arg.c_str(), nullptr, SW_SHOWNORMAL);
ShowWindow(GetConsoleWindow(), 0);
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);
std::this_thread::sleep_for(std::chrono::seconds(1));
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];
}
system("clear");
execl((GetEP() + GetEN()).c_str(), Arg.c_str(), NULL);
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));
exit(1);
}
#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);
Expand All @@ -172,7 +172,7 @@ void CheckForUpdates(int argc, char* args[], const std::string& CV) {
system("clear");
#endif

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.");
Expand All @@ -185,26 +185,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() {
Expand Down Expand Up @@ -236,26 +223,24 @@ 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();
CheckName(argc, argv);
CheckName();
CheckLocalKey();
ConfigInit();
CustomPort(argc, argv);
CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch());
CheckForUpdates(std::string(GetVer()) + GetPatch());
}
#endif

Expand Down Expand Up @@ -318,7 +303,7 @@ void PreGame(const std::string& GamePath) {

CheckMP(GetGamePath() + "mods/multiplayer");

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(),
Expand Down
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "Startup.h"
#include <iostream>
#include <thread>
#include "Options.h"

Options options;

[[noreturn]] void flush() {
while (true) {
Expand All @@ -28,7 +31,8 @@ int main(int argc, char* argv[]) {

GetEP(argv[0]);

InitLauncher(argc, argv);
InitOptions(argc, argv, options);
InitLauncher();

try {
LegitimacyCheck();
Expand Down

0 comments on commit 5f84a55

Please sign in to comment.