From cf8f10b949c2fca1828164c5a9950ddeb9fa967a Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sat, 30 Dec 2023 11:53:47 +0100 Subject: [PATCH] add --port=value argument --- include/TConfig.h | 2 +- src/TConfig.cpp | 1 - src/main.cpp | 25 ++++++++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/TConfig.h b/include/TConfig.h index c0b9902..0701076 100644 --- a/include/TConfig.h +++ b/include/TConfig.h @@ -35,11 +35,11 @@ public: [[nodiscard]] bool Failed() const { return mFailed; } void FlushToFile(); + void PrintDebug(); private: void CreateConfigFile(); void ParseFromFile(std::string_view name); - void PrintDebug(); void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, std::string& OutValue); void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, bool& OutValue); void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, int& OutValue); diff --git a/src/TConfig.cpp b/src/TConfig.cpp index 1223ec7..7192f87 100644 --- a/src/TConfig.cpp +++ b/src/TConfig.cpp @@ -259,7 +259,6 @@ void TConfig::ParseFromFile(std::string_view name) { Application::SetSubsystemStatus("Config", Application::Status::Bad); return; } - PrintDebug(); // Update in any case if (!mDisableConfig) { diff --git a/src/main.cpp b/src/main.cpp index 311fa26..309eef7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,6 +30,7 @@ #include "TResourceManager.h" #include "TServer.h" +#include #include #include @@ -40,6 +41,9 @@ USAGE: ARGUMENTS: --help Displays this help and exits. + --port=1234 + Sets the server's listening TCP and + UDP port. Overrides ENV and ServerConfig. --config=/path/to/ServerConfig.toml Absolute or relative path to the Server Config file, including the @@ -91,6 +95,7 @@ int BeamMPServerMain(MainArguments Arguments) { Parser.RegisterArgument({ "help" }, ArgsParser::NONE); Parser.RegisterArgument({ "version" }, ArgsParser::NONE); Parser.RegisterArgument({ "config" }, ArgsParser::HAS_VALUE); + Parser.RegisterArgument({ "port" }, ArgsParser::HAS_VALUE); Parser.RegisterArgument({ "working-directory" }, ArgsParser::HAS_VALUE); Parser.Parse(Arguments.List); if (!Parser.Verify()) { @@ -124,7 +129,7 @@ int BeamMPServerMain(MainArguments Arguments) { } } } - + TConfig Config(ConfigPath); if (Config.Failed()) { @@ -135,6 +140,24 @@ int BeamMPServerMain(MainArguments Arguments) { } return 1; } + + // override port if provided via arguments + if (Parser.FoundArgument({ "port" })) { + auto Port = Parser.GetValueOfArgument({ "port" }); + if (Port.has_value()) { + auto P = int(std::strtoul(Port.value().c_str(), nullptr, 10)); + if (P == 0 || P < 0 || P > UINT16_MAX) { + beammp_errorf("Custom port requested via --port is invalid: '{}'", Port.value()); + return 1; + } else { + Application::Settings.Port = P; + beammp_info("Custom port requested via commandline arguments: " + Port.value()); + } + } + } + + Config.PrintDebug(); + Application::InitializeConsole(); Application::Console().StartLoggingToFile();