diff --git a/Source/FPSTemplate/Private/Game/ShooterGameMode.cpp b/Source/FPSTemplate/Private/Game/ShooterGameMode.cpp index 82225f6e..cef45cb5 100644 --- a/Source/FPSTemplate/Private/Game/ShooterGameMode.cpp +++ b/Source/FPSTemplate/Private/Game/ShooterGameMode.cpp @@ -32,7 +32,7 @@ namespace GameLiftValidators { const int32 WebSocketPortMin = 1; const int32 WebSocketPortMax = 60000; - bool ValidateString(const FString& Value, const FRegexPattern* OptionalPattern, const int32 Min, const int32 Max) + bool IsStringValid(const FString& Value, const FRegexPattern* OptionalPattern, const int32 Min, const int32 Max) { if (Value.IsEmpty()) return false; @@ -48,7 +48,7 @@ namespace GameLiftValidators { } } - bool ValidateNumber(const int32 Value, const int32 Min, const int32 Max) + bool IsNumberValid(const int32 Value, const int32 Min, const int32 Max) { return Value >= Min && Value <= Max; } @@ -76,7 +76,6 @@ void AShooterGameMode::InitGame(const FString& MapName, const FString& Options, CachedCommandLine = FCommandLine::Get(); // Parsing of Command Line - bDebugMode = FParse::Param(*CachedCommandLine, TEXT("Debug")); GameLiftConfig.bDebugMode = FParse::Param(*CachedCommandLine, TEXT("Debug")); if (GameLiftConfig.bDebugMode) @@ -256,22 +255,20 @@ void AShooterGameMode::InitGame_original(const FString& MapName, const FString& int32 AShooterGameMode::GetConfiguredOrDefaultPort() const { // Default Unreal Engine listen/dedicated server port - int32 Port = 7777; + constexpr int32 DefaultPort = 7777; + int32 CmdPort = DefaultPort; + // Check if a port was passed via command line: -port=xxxx - int32 CmdPort = 0; - if (FParse::Value(*CachedCommandLine, TEXT("port="), CmdPort)) + + if (FParse::Value(*CachedCommandLine, TEXT("port="), CmdPort) && + GameLiftValidators::IsNumberValid(CmdPort, GameLiftValidators::ServerPortMin, GameLiftValidators::ServerPortMax)) { - if (CmdPort > 1024 && CmdPort <= 65535) - { - Port = CmdPort; - } - else - { - Port = 0; - } + return CmdPort; } - return Port; + + UE_LOGFMT(LogShooterGameMode, Warning, "Invalid/Missing port in command line - using {0}", DefaultPort); + return DefaultPort; } FString AShooterGameMode::GetSHA256Hash(const FString& Input) diff --git a/Source/FPSTemplate/Public/Game/ShooterGameMode.h b/Source/FPSTemplate/Public/Game/ShooterGameMode.h index cd59e5fe..d14010af 100644 --- a/Source/FPSTemplate/Public/Game/ShooterGameMode.h +++ b/Source/FPSTemplate/Public/Game/ShooterGameMode.h @@ -50,8 +50,8 @@ namespace GameLiftValidators extern const int32 WebSocketPortMin; extern const int32 WebSocketPortMax; - bool ValidateString(const FString& Value, const FRegexPattern& OptionalPattern, int32 Min, int32 Max); - bool ValidateNumber(int32 Value, int32 Min, int32 Max); + bool IsStringValid(const FString& Value, const FRegexPattern& OptionalPattern, int32 Min, int32 Max); + bool IsNumberValid(int32 Value, int32 Min, int32 Max); } /**