mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-03 16:25:35 +00:00
Allow provider to define server port ENV via BEAMMP_PROVIDER_PORT_ENV (#244)
This commit is contained in:
commit
a3c4b82a5d
@ -195,6 +195,7 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE ${PRJ_DEFINITIONS} ${PRJ_WARN
|
|||||||
)
|
)
|
||||||
|
|
||||||
if(MSVC)
|
if(MSVC)
|
||||||
|
target_compile_options(${PROJECT_NAME} PUBLIC "/bigobj")
|
||||||
target_link_options(${PROJECT_NAME} PRIVATE "/SUBSYSTEM:CONSOLE")
|
target_link_options(${PROJECT_NAME} PRIVATE "/SUBSYSTEM:CONSOLE")
|
||||||
endif(MSVC)
|
endif(MSVC)
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@ enum class Key {
|
|||||||
// provider settings
|
// provider settings
|
||||||
PROVIDER_UPDATE_MESSAGE,
|
PROVIDER_UPDATE_MESSAGE,
|
||||||
PROVIDER_DISABLE_CONFIG,
|
PROVIDER_DISABLE_CONFIG,
|
||||||
|
PROVIDER_PORT_ENV,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::optional<std::string> Get(Key key);
|
std::optional<std::string> Get(Key key);
|
||||||
|
@ -35,11 +35,11 @@ public:
|
|||||||
[[nodiscard]] bool Failed() const { return mFailed; }
|
[[nodiscard]] bool Failed() const { return mFailed; }
|
||||||
|
|
||||||
void FlushToFile();
|
void FlushToFile();
|
||||||
|
void PrintDebug();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateConfigFile();
|
void CreateConfigFile();
|
||||||
void ParseFromFile(std::string_view name);
|
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, 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, bool& OutValue);
|
||||||
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, int& OutValue);
|
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, const std::string_view& Env, int& OutValue);
|
||||||
|
@ -36,6 +36,9 @@ std::string_view Env::ToString(Env::Key key) {
|
|||||||
case Key::PROVIDER_DISABLE_CONFIG:
|
case Key::PROVIDER_DISABLE_CONFIG:
|
||||||
return "BEAMMP_PROVIDER_DISABLE_CONFIG";
|
return "BEAMMP_PROVIDER_DISABLE_CONFIG";
|
||||||
break;
|
break;
|
||||||
|
case Key::PROVIDER_PORT_ENV:
|
||||||
|
return "BEAMMP_PROVIDER_PORT_ENV";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -234,7 +234,11 @@ void TConfig::ParseFromFile(std::string_view name) {
|
|||||||
// GENERAL
|
// GENERAL
|
||||||
TryReadValue(data, "General", StrDebug, EnvStrDebug, Application::Settings.DebugModeEnabled);
|
TryReadValue(data, "General", StrDebug, EnvStrDebug, Application::Settings.DebugModeEnabled);
|
||||||
TryReadValue(data, "General", StrPrivate, EnvStrPrivate, Application::Settings.Private);
|
TryReadValue(data, "General", StrPrivate, EnvStrPrivate, Application::Settings.Private);
|
||||||
|
if (Env::Get(Env::Key::PROVIDER_PORT_ENV).has_value()) {
|
||||||
|
TryReadValue(data, "General", StrPort, Env::Get(Env::Key::PROVIDER_PORT_ENV).value(), Application::Settings.Port);
|
||||||
|
} else {
|
||||||
TryReadValue(data, "General", StrPort, EnvStrPort, Application::Settings.Port);
|
TryReadValue(data, "General", StrPort, EnvStrPort, Application::Settings.Port);
|
||||||
|
}
|
||||||
TryReadValue(data, "General", StrMaxCars, EnvStrMaxCars, Application::Settings.MaxCars);
|
TryReadValue(data, "General", StrMaxCars, EnvStrMaxCars, Application::Settings.MaxCars);
|
||||||
TryReadValue(data, "General", StrMaxPlayers, EnvStrMaxPlayers, Application::Settings.MaxPlayers);
|
TryReadValue(data, "General", StrMaxPlayers, EnvStrMaxPlayers, Application::Settings.MaxPlayers);
|
||||||
TryReadValue(data, "General", StrMap, EnvStrMap, Application::Settings.MapName);
|
TryReadValue(data, "General", StrMap, EnvStrMap, Application::Settings.MapName);
|
||||||
@ -255,7 +259,6 @@ void TConfig::ParseFromFile(std::string_view name) {
|
|||||||
Application::SetSubsystemStatus("Config", Application::Status::Bad);
|
Application::SetSubsystemStatus("Config", Application::Status::Bad);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PrintDebug();
|
|
||||||
|
|
||||||
// Update in any case
|
// Update in any case
|
||||||
if (!mDisableConfig) {
|
if (!mDisableConfig) {
|
||||||
|
23
src/main.cpp
23
src/main.cpp
@ -30,6 +30,7 @@
|
|||||||
#include "TResourceManager.h"
|
#include "TResourceManager.h"
|
||||||
#include "TServer.h"
|
#include "TServer.h"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
@ -40,6 +41,9 @@ USAGE:
|
|||||||
ARGUMENTS:
|
ARGUMENTS:
|
||||||
--help
|
--help
|
||||||
Displays this help and exits.
|
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
|
--config=/path/to/ServerConfig.toml
|
||||||
Absolute or relative path to the
|
Absolute or relative path to the
|
||||||
Server Config file, including the
|
Server Config file, including the
|
||||||
@ -91,6 +95,7 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
|
Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
|
||||||
Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
|
Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
|
||||||
Parser.RegisterArgument({ "config" }, ArgsParser::HAS_VALUE);
|
Parser.RegisterArgument({ "config" }, ArgsParser::HAS_VALUE);
|
||||||
|
Parser.RegisterArgument({ "port" }, ArgsParser::HAS_VALUE);
|
||||||
Parser.RegisterArgument({ "working-directory" }, ArgsParser::HAS_VALUE);
|
Parser.RegisterArgument({ "working-directory" }, ArgsParser::HAS_VALUE);
|
||||||
Parser.Parse(Arguments.List);
|
Parser.Parse(Arguments.List);
|
||||||
if (!Parser.Verify()) {
|
if (!Parser.Verify()) {
|
||||||
@ -135,6 +140,24 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
}
|
}
|
||||||
return 1;
|
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::InitializeConsole();
|
||||||
Application::Console().StartLoggingToFile();
|
Application::Console().StartLoggingToFile();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user