Skip to content

Commit

Permalink
add --port=value argument
Browse files Browse the repository at this point in the history
  • Loading branch information
lionkor committed Dec 30, 2023
1 parent 3464cf9 commit 262705b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/TConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class TConfig {
[[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);
Expand Down
1 change: 0 additions & 1 deletion src/TConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ void TConfig::ParseFromFile(std::string_view name) {
Application::SetSubsystemStatus("Config", Application::Status::Bad);
return;
}
PrintDebug();

// Update in any case
if (!mDisableConfig) {
Expand Down
25 changes: 24 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "TResourceManager.h"
#include "TServer.h"

#include <cstdint>
#include <iostream>
#include <thread>

Expand All @@ -22,6 +23,9 @@ static const std::string sCommandlineArguments = R"(
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
Expand Down Expand Up @@ -73,6 +77,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()) {
Expand Down Expand Up @@ -106,7 +111,7 @@ int BeamMPServerMain(MainArguments Arguments) {
}
}
}

TConfig Config(ConfigPath);

if (Config.Failed()) {
Expand All @@ -117,6 +122,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();

Expand Down

0 comments on commit 262705b

Please sign in to comment.