Compare commits
3 Commits
0beaf68d7d
...
ade2a38d7c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ade2a38d7c | ||
|
|
ebf932882b | ||
|
|
13daff916c |
@@ -53,6 +53,10 @@
|
|||||||
{
|
{
|
||||||
"Name": "PerforceSourceControl",
|
"Name": "PerforceSourceControl",
|
||||||
"Enabled": false
|
"Enabled": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "GameLiftPlugin",
|
||||||
|
"Enabled": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -2,18 +2,12 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Game/ShooterGameMode.h"
|
#include "Game/ShooterGameMode.h"
|
||||||
|
#include "Logging/LogMacros.h"
|
||||||
#include "DSP/BufferDiagnostics.h"
|
|
||||||
#include "UObject/ConstructorHelpers.h"
|
|
||||||
#include "Kismet/GameplayStatics.h"
|
|
||||||
|
|
||||||
#if WITH_GAMELIFT
|
#if WITH_GAMELIFT
|
||||||
#include "GameLiftServerSDKModels.h"
|
#include "GameLiftServerSDK.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "GenericPlatform/GenericPlatformOutputDevices.h"
|
|
||||||
#include "Misc/TypeContainer.h"
|
|
||||||
|
|
||||||
DEFINE_LOG_CATEGORY(LogShooterGameMode)
|
DEFINE_LOG_CATEGORY(LogShooterGameMode)
|
||||||
|
|
||||||
AShooterGameMode::AShooterGameMode()
|
AShooterGameMode::AShooterGameMode()
|
||||||
@@ -24,272 +18,197 @@ AShooterGameMode::AShooterGameMode()
|
|||||||
void AShooterGameMode::BeginPlay()
|
void AShooterGameMode::BeginPlay()
|
||||||
{
|
{
|
||||||
Super::BeginPlay();
|
Super::BeginPlay();
|
||||||
|
|
||||||
#if WITH_GAMELIFT
|
#if WITH_GAMELIFT
|
||||||
InitGameLift();
|
InitGameLift();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WITH_GAMELIFT
|
void AShooterGameMode::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
|
||||||
|
|
||||||
// ReSharper disable once CppMemberFunctionMayBeConst - cannot be const due to FCommandLine::Get() and UE_LOG with non-const log category
|
|
||||||
bool AShooterGameMode::SetParametersFromCommandLine(FServerParameters& OutServerParameters,
|
|
||||||
FProcessParameters& OutProcessParameters)
|
|
||||||
{
|
{
|
||||||
bool bCriticalFault = false;
|
Super::InitGame(MapName, Options, ErrorMessage);
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("[%s] Parsing CLI"), *FDateTime::UtcNow().ToString(TEXT("%Y%m%d-%H%M%S")));
|
||||||
|
CachedCommandLine = FCommandLine::Get();
|
||||||
|
bool bIsCriticalError = false;
|
||||||
|
|
||||||
FString CommandLine = FCommandLine::Get();
|
// Parsing of Command Line
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT(">>> Command Line: %s"), *CommandLine);
|
bDebugMode = FParse::Param(*CachedCommandLine, TEXT("Debug"));
|
||||||
|
|
||||||
// Checks if param exists (returns bool)
|
if (bDebugMode)
|
||||||
|
|
||||||
// AuthToken returned from the "aws gamelift get-compute-auth-token" API. Note this will expire and require a new call to the API after 15 minutes.
|
|
||||||
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("authtoken"),OutServerParameters.m_authToken);
|
|
||||||
|
|
||||||
// The Host/compute-name of the GameLift Anywhere instance
|
|
||||||
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("hostid"),OutServerParameters.m_hostId);
|
|
||||||
|
|
||||||
// The AnywhereFleet ID.
|
|
||||||
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("fleetid"),OutServerParameters.m_fleetId);
|
|
||||||
|
|
||||||
// The WebSocket URL (GameLiftServiceSdkEndpoint)
|
|
||||||
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("websocketurl"),OutServerParameters.m_webSocketUrl);
|
|
||||||
|
|
||||||
// The port being used, uses UE system default if not entered as a switch
|
|
||||||
OutProcessParameters.port = FURL::UrlConfig.DefaultPort;
|
|
||||||
FParse::Value(*CommandLine, TEXT("port="), OutProcessParameters.port);
|
|
||||||
|
|
||||||
LogServerSummary(OutServerParameters,OutProcessParameters);
|
|
||||||
|
|
||||||
return bCriticalFault;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AShooterGameMode::LogServerParameters(const FServerParameters& InServerParameters)
|
|
||||||
{
|
{
|
||||||
// Log Server Parameters
|
UE_LOG(LogShooterGameMode, Log, TEXT("Debug mode: ENABLED"));
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_YELLOW);
|
#if UE_BUILD_DEBUG
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> WebSocket URL: %s"), *InServerParameters.m_webSocketUrl);
|
UE_LOG(LogShooterGameMode, Log, TEXT("Command Line Arguments: %s"), *CachedCommandLine);
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Fleet ID: %s"), *InServerParameters.m_fleetId);
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Process ID: %s"), *InServerParameters.m_processId);
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Host ID (Compute Name): %s"), *InServerParameters.m_hostId);
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Auth Token: %s"), *InServerParameters.m_authToken);
|
|
||||||
// UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Aws Region: %s"), *ServerParameters.m_awsRegion);
|
|
||||||
// UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Access Key: %s"), *ServerParameters.m_accessKey);
|
|
||||||
// UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Secret Key: %s"), *ServerParameters.m_secretKey);
|
|
||||||
// UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Session Token: %s"), *ServerParameters.m_sessionToken);
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_NONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AShooterGameMode::SetProcessIdOnServerParameters(FServerParameters& OutServerParameters)
|
|
||||||
{
|
|
||||||
// The PID of the running process
|
|
||||||
#if PLATFORM_WINDOWS
|
|
||||||
OutServerParameters.m_processId = FString::Printf(TEXT("%d"), GetCurrentProcessId());
|
|
||||||
#else
|
|
||||||
OutServerParameters.m_processId = FString::Printf(TEXT("%d"), FGenericPlatformProcess::GetCurrentProcessId());
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AShooterGameMode::ValidateServerParameters(const FString& CommandLine, const FString& ParameterName,
|
ServerPort = GetConfiguredOrDefaultPort();
|
||||||
FString& OutValue, const bool bCritical)
|
|
||||||
{
|
|
||||||
if (!FParse::Value(*CommandLine, *FString::Printf(TEXT("%s="), *ParameterName), OutValue))
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, Warning, TEXT("-%s not found"), *ParameterName);
|
|
||||||
return bCritical;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AShooterGameMode::LogServerSummary(const FServerParameters& InServerParameters,
|
bIsAnywhereFleet = FParse::Param(*CachedCommandLine, TEXT("glAnywhere"));
|
||||||
const FProcessParameters& InProcessParameters)
|
if (bIsAnywhereFleet)
|
||||||
{
|
{
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_YELLOW);
|
UE_LOG(LogShooterGameMode, Log, TEXT("GameLift Anywhere Fleet Command Line Parsing"));
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("=== SERVER STARTUP SUMMARY ==="));
|
UE_LOG(LogShooterGameMode, Log, TEXT("======Command Line Parameters======"));
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Port: %d"), InProcessParameters.port);
|
|
||||||
|
|
||||||
// Inline validation of ServerParams
|
if (!FParse::Value(*CachedCommandLine, TEXT("fleetID="), FleetId))
|
||||||
bool bAllValid = !InServerParameters.m_authToken.IsEmpty() &&
|
|
||||||
!InServerParameters.m_hostId.IsEmpty() &&
|
|
||||||
!InServerParameters.m_fleetId.IsEmpty() &&
|
|
||||||
!InServerParameters.m_webSocketUrl.IsEmpty();
|
|
||||||
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("All Anywhere params: %s"),
|
|
||||||
bAllValid ? TEXT("VALID ✓") : TEXT("MISSING ❌"));
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_NONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AShooterGameMode::BindGameLiftCallbackFunctions(FProcessParameters& ProcessParameters, FGameLiftServerSDKModule& GameLiftSdkModule)
|
|
||||||
{
|
{
|
||||||
// Implement callback function onStartGameSession
|
UE_LOG(LogShooterGameMode, Error, TEXT("FleetId Missing in Command Line"));
|
||||||
// GameLift sends a game session activation request to the game server
|
bIsCriticalError = true;
|
||||||
// and passes a game session object with game properties and other settings.
|
|
||||||
// Here is where a game server takes action based on the game session object.
|
|
||||||
// When the game server is ready to receive incoming player connections,
|
|
||||||
// it invokes the server SDK call ActivateGameSession().
|
|
||||||
|
|
||||||
|
|
||||||
ProcessParameters.OnStartGameSession.BindLambda([this, &GameLiftSdkModule](Aws::GameLift::Server::Model::GameSession InGameSession)
|
|
||||||
{
|
|
||||||
FString GameSessionId = FString(InGameSession.GetGameSessionId());
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("GameSession Initializing: %s"), *GameSessionId);
|
|
||||||
FGameLiftGenericOutcome ActivateGameSessionOutcome = GameLiftSdkModule.ActivateGameSession();
|
|
||||||
if (ActivateGameSessionOutcome.IsSuccess())
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Activate GameSession successful"));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const FGameLiftError& Error = ActivateGameSessionOutcome.GetError();
|
UE_LOG(LogShooterGameMode, Log, TEXT("Fleet ID: %s"), *FleetId);
|
||||||
UE_LOG(LogShooterGameMode, Error, TEXT("ActivateGameSession() failed. Error: %s"), Error.m_errorMessage.IsEmpty() ? TEXT("Unknown Error") : *Error.m_errorMessage);
|
}
|
||||||
|
|
||||||
|
if (!FParse::Value(*CachedCommandLine, TEXT("authtoken="), AuthToken))
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Error, TEXT("AuthToken Missing in Command Line"));
|
||||||
|
bIsCriticalError = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (bDebugMode)
|
||||||
|
{
|
||||||
|
FString TokenHash = GetSHA256Hash(AuthToken);
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("AuthToken: %s"), *TokenHash);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("AuthToken Length: %d"), AuthToken.Len());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!FParse::Value(*CachedCommandLine, TEXT("hostId="), HostId))
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Error, TEXT("HostId Missing in Command Line"));
|
||||||
|
bIsCriticalError = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("Host ID: %s"), *HostId);
|
||||||
|
}
|
||||||
|
if (!FParse::Value(*CachedCommandLine, TEXT("websocketUrl="), WebSocketUrl))
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Error, TEXT("WebSocketUrl Missing in Command Line"));
|
||||||
|
bIsCriticalError = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("Websocket URL: %s"), *WebSocketUrl);
|
||||||
|
|
||||||
|
bool bValidWebSocket = WebSocketUrl.StartsWith(TEXT("wss://")) || WebSocketUrl.StartsWith(TEXT("ws://"));
|
||||||
|
|
||||||
|
if (bValidWebSocket)
|
||||||
|
{
|
||||||
|
int32 ColonPos = WebSocketUrl.Find(TEXT(":"), ESearchCase::CaseSensitive);
|
||||||
|
int32 SlashPos = WebSocketUrl.Find(TEXT("/"), ESearchCase::CaseSensitive, ESearchDir::FromStart, ColonPos + 1);
|
||||||
|
|
||||||
|
int32 ParsedPort = 443; // Default wss
|
||||||
|
if (WebSocketUrl.StartsWith(TEXT("ws://"))) ParsedPort = 80;
|
||||||
|
|
||||||
|
if (ColonPos != INDEX_NONE && SlashPos != INDEX_NONE)
|
||||||
|
{
|
||||||
|
FString PortStr = WebSocketUrl.Mid(ColonPos + 1, SlashPos - ColonPos - 1);
|
||||||
|
int32 ExplicitPort = FCString::Atoi(*PortStr);
|
||||||
|
if (ExplicitPort > 1024 && ExplicitPort <= 65535) // Privileged ports OK for servers
|
||||||
|
{
|
||||||
|
ParsedPort = ExplicitPort;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bValidWebSocket = false; // Invalid explicit port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("WebSocket Parsed Port: %d"), ParsedPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bValidWebSocket)
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Error, TEXT("Invalid WebSocketUrl: %s"), *WebSocketUrl);
|
||||||
|
bIsCriticalError = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ServerPort == 0)
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Error, TEXT("Invalid or Missing Server Port Number. Shutting Down."));
|
||||||
|
bIsCriticalError = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("Port: %d"), ServerPort);
|
||||||
|
}
|
||||||
|
if (!FParse::Value(*CachedCommandLine, TEXT("serverRegion="), ServerRegion))
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Warning, TEXT("ServerRegion Missing in Command Line"));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT(">>>Server Region: %s"), *ServerRegion);
|
||||||
|
}
|
||||||
|
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("==================================="));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("GameLift EC2 Fleet"));
|
||||||
|
if (ServerPort == 0)
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Error, TEXT("Invalid or Missing Server Port Number. Shutting Down."));
|
||||||
|
bIsCriticalError = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("Port: %d"), ServerPort);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bIsCriticalError)
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Error, TEXT("Critical Missing or Invalid Arguments in Command Line. Shutting Down."));
|
||||||
FPlatformMisc::RequestExit(true);
|
FPlatformMisc::RequestExit(true);
|
||||||
}
|
}
|
||||||
});
|
else
|
||||||
|
{
|
||||||
|
UE_LOG(LogShooterGameMode, Log, TEXT("Command Line Parsed Successfully."));
|
||||||
|
}
|
||||||
|
|
||||||
ProcessParameters.OnTerminate.BindLambda([this, &GameLiftSdkModule]()
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int32 AShooterGameMode::GetConfiguredOrDefaultPort() const
|
||||||
{
|
{
|
||||||
FGameLiftGenericOutcome ProcessEndingOutcome = GameLiftSdkModule.ProcessEnding();
|
// Default Unreal Engine listen/dedicated server port
|
||||||
FGameLiftGenericOutcome DestroyOutcome = GameLiftSdkModule.Destroy();
|
int32 Port = 7777;
|
||||||
if (ProcessEndingOutcome.IsSuccess() && DestroyOutcome.IsSuccess())
|
|
||||||
|
// Check if a port was passed via command line: -port=xxxx
|
||||||
|
int32 CmdPort = 0;
|
||||||
|
if (FParse::Value(*CachedCommandLine, TEXT("port="), CmdPort))
|
||||||
{
|
{
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Server process ending successfully"));
|
if (CmdPort > 1024 && CmdPort <= 65535)
|
||||||
FPlatformMisc::RequestExit(false);
|
{
|
||||||
|
Port = CmdPort;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!ProcessEndingOutcome.IsSuccess())
|
Port = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Port;
|
||||||
|
}
|
||||||
|
|
||||||
|
FString AShooterGameMode::GetSHA256Hash(const FString& Input)
|
||||||
{
|
{
|
||||||
const FGameLiftError& Error = ProcessEndingOutcome.GetError();
|
FTCHARToUTF8 Utf8Input(Input);
|
||||||
UE_LOG(LogShooterGameMode, Error, TEXT("ProcessEnding() failed. Error: %s"), Error.m_errorMessage.IsEmpty() ? TEXT("Unknown Error") : *Error.m_errorMessage);
|
FSHA256Signature Hash;
|
||||||
}
|
if (FPlatformMisc::GetSHA256Signature(reinterpret_cast<const uint8*>(Utf8Input.Get()), Utf8Input.Length(), Hash))
|
||||||
if (!DestroyOutcome.IsSuccess())
|
|
||||||
{
|
{
|
||||||
const FGameLiftError& Error = DestroyOutcome.GetError();
|
return Hash.ToString();
|
||||||
UE_LOG(LogShooterGameMode, Error, TEXT("Destroy() failed. Error: %s"), Error.m_errorMessage.IsEmpty() ? TEXT("Unknown error") : *Error.m_errorMessage);
|
|
||||||
}
|
}
|
||||||
FPlatformMisc::RequestExit(true);
|
return FString::Printf(TEXT("Fail_%dbytes"), Input.Len());
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
ProcessParameters.OnHealthCheck.BindLambda([=]()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Here, the game server tells GameLift where to find game session Log files.
|
|
||||||
// At the end of a game session, GameLift uploads everything in the specified
|
|
||||||
// location and stores it in the cloud for access later.
|
|
||||||
|
|
||||||
TArray<FString> LogFiles;
|
|
||||||
FString LogDirectory =FPaths::ProjectSavedDir() / TEXT("Logs");
|
|
||||||
FString LogFile = LogDirectory / TEXT("GameLiftServer.log");
|
|
||||||
LogFiles.Add(LogFile);
|
|
||||||
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Before Adding Log Files"));
|
|
||||||
ProcessParameters.logParameters = LogFiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AShooterGameMode::InitiateProcessReady(FProcessParameters& ProcessParameters, FGameLiftServerSDKModule& GameLiftSdkModule)
|
|
||||||
{
|
|
||||||
// The game server calls ProcessReady() to tell Amazon GameLift Servers it's ready to host game sessions.
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Calling Process Ready..."));
|
|
||||||
FGameLiftGenericOutcome ProcessReadyOutcome = GameLiftSdkModule.ProcessReady(ProcessParameters);
|
|
||||||
|
|
||||||
if (ProcessReadyOutcome.IsSuccess())
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_GREEN);
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Process Ready!"));
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_NONE);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_RED);
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("ERROR: Process Ready Failed!"));
|
|
||||||
FGameLiftError ProcessReadyError = ProcessReadyOutcome.GetError();
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("ERROR: %s"), *ProcessReadyError.m_errorMessage);
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_NONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("InitGameLift completed!"));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AShooterGameMode::InitiateConnectionWithGameLiftAgent(FServerParameters& ServerParameters, FGameLiftServerSDKModule& GameLiftSdkModule)
|
|
||||||
{
|
|
||||||
// InitSDK will establish a local connection with GameLift's agent to enable further communication
|
|
||||||
|
|
||||||
FGameLiftGenericOutcome InitSdkOutcome = GameLiftSdkModule.InitSDK(ServerParameters);
|
|
||||||
if (InitSdkOutcome.IsSuccess())
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_GREEN);
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("GameLift Init SDK Succeeded"));
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_NONE);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_RED);
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("ERROR: InitSDK failed : ("));
|
|
||||||
FGameLiftError GameLiftError = InitSdkOutcome.GetError();
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("ERROR: %s"), *GameLiftError.m_errorMessage);
|
|
||||||
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_NONE);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void AShooterGameMode::InitGameLift()
|
void AShooterGameMode::InitGameLift()
|
||||||
{
|
{
|
||||||
#if WITH_GAMELIFT
|
//TODO: Need to write later, working on parser first.
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Calling InitGameLift"));
|
|
||||||
|
|
||||||
FServerParameters ServerParameters;
|
|
||||||
FProcessParameters ProcessParameters;
|
|
||||||
|
|
||||||
// Getting the module first.
|
|
||||||
FGameLiftServerSDKModule& GameLiftSdkModule = FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK"));
|
|
||||||
|
|
||||||
// Define the server parameters for a GameLift Anywhere fleet. These are not needed for a GameLift managed EC2 fleet.
|
|
||||||
bool bIsAnywhereActive = false;
|
|
||||||
if (FParse::Param(FCommandLine::Get(), TEXT("-glAnywhere")))
|
|
||||||
{
|
|
||||||
bIsAnywhereActive = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bIsAnywhereActive)
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Configuring server parameters for Anywhere..."));
|
|
||||||
if (SetParametersFromCommandLine(ServerParameters, ProcessParameters))
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, Error, TEXT("Critical Error, shutting server down..."));
|
|
||||||
FPlatformMisc::RequestExit(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SetProcessIdOnServerParameters(ServerParameters);
|
|
||||||
if (ServerParameters.m_processId.IsEmpty())
|
|
||||||
{
|
|
||||||
UE_LOG(LogShooterGameMode, Warning, TEXT("ProcessId is empty"));
|
|
||||||
FPlatformMisc::RequestExit(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LogServerParameters(ServerParameters);
|
|
||||||
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Initializing the GameLift Server..."));
|
|
||||||
|
|
||||||
BindGameLiftCallbackFunctions(ProcessParameters, GameLiftSdkModule);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Using EC2
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Using Gamelift EC2 Managed Fleet"));
|
|
||||||
|
|
||||||
// EC2: Bind Callbacks (populates logParameters automatically)
|
|
||||||
BindGameLiftCallbackFunctions(ProcessParameters, GameLiftSdkModule);
|
|
||||||
|
|
||||||
// EC2: Empty ServerParameters = GameLift agent provies all config
|
|
||||||
UE_LOG(LogShooterGameMode, Log, TEXT("Initializing EC2 GameLift Server..."));
|
|
||||||
}
|
|
||||||
if (InitiateConnectionWithGameLiftAgent(ServerParameters, GameLiftSdkModule)) return;
|
|
||||||
InitiateProcessReady(ProcessParameters, GameLiftSdkModule);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,214 +5,13 @@
|
|||||||
#include "CoreMinimal.h"
|
#include "CoreMinimal.h"
|
||||||
#include "ShooterGameModeBase.h"
|
#include "ShooterGameModeBase.h"
|
||||||
|
|
||||||
#if WITH_GAMELIFT
|
|
||||||
#include "GameLiftServerSDK.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "ShooterGameMode.generated.h"
|
#include "ShooterGameMode.generated.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
struct FCommandLineArgs
|
|
||||||
{
|
|
||||||
TMap<FString,FString> Options;
|
|
||||||
TArray<FString> CmdArguments;
|
|
||||||
|
|
||||||
void ParseCommandLine()
|
|
||||||
{
|
|
||||||
const TCHAR* CommandLine = FCommandLine::Get();
|
|
||||||
|
|
||||||
// Reserves estimated space to avoid reallocations
|
|
||||||
Options.Reserve(10);
|
|
||||||
CmdArguments.Reserve(10);
|
|
||||||
|
|
||||||
FString Key, Value, Token;
|
|
||||||
FString FlagName = FString(TEXT(""));
|
|
||||||
while (FParse::Token(CommandLine, Token, true))
|
|
||||||
{
|
|
||||||
// Check if token starts with '-'
|
|
||||||
if (Token.StartsWith(TEXT("-")))
|
|
||||||
{
|
|
||||||
// Remove the leading '-' and store the flag name
|
|
||||||
FlagName = Token.Mid(1);
|
|
||||||
|
|
||||||
// If it's in the form Key=Value, split it
|
|
||||||
if (FlagName.Split(TEXT("="), &Key, &Value))
|
|
||||||
{
|
|
||||||
Key=Key.ToLower();
|
|
||||||
Value=Value.ToLower();
|
|
||||||
// Normalize boolean values
|
|
||||||
if (Value.Equals(TEXT("true"), ESearchCase::IgnoreCase) || Value.Equals(TEXT("1")))
|
|
||||||
{
|
|
||||||
Options.Add(Key, TEXT("true"));
|
|
||||||
}
|
|
||||||
else if (Value.Equals(TEXT("false"), ESearchCase::IgnoreCase) || Value.Equals(TEXT("0")))
|
|
||||||
{
|
|
||||||
Options.Add(Key, TEXT("false"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Options.Add(Key, Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Bare flag (for
|
|
||||||
// example -Debug or -NoSound)
|
|
||||||
|
|
||||||
// The convention is flags starting with "No" are false
|
|
||||||
if (FlagName.StartsWith(TEXT("No")) && FlagName.Len() > 2)
|
|
||||||
{
|
|
||||||
Key = FlagName.Mid(2).ToLower();
|
|
||||||
|
|
||||||
Options.Add(Key, TEXT("false"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Key = FlagName.ToLower();
|
|
||||||
Options.Add(Key, TEXT("true"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Also add to Arguments array for backward compatibility
|
|
||||||
CmdArguments.Add(FlagName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CmdArguments.Add(Token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FString GetValueFromKey(const FString& Key, const FString& DefaultValue = TEXT("")) const
|
|
||||||
{
|
|
||||||
FString NormalizedKey = Key.ToLower();
|
|
||||||
const FString* ValuePtr = Options.Find(NormalizedKey);
|
|
||||||
return ValuePtr ? *ValuePtr : DefaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TryGetValue(const FString& Key, FString& OutValue) const
|
|
||||||
{
|
|
||||||
FString NormalizedKey = Key.ToLower();
|
|
||||||
if (const FString* ValuePtr = Options.Find(NormalizedKey))
|
|
||||||
{
|
|
||||||
OutValue = *ValuePtr;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
FString ToString() const
|
|
||||||
{
|
|
||||||
FString Result;
|
|
||||||
for (const auto& Pair : Options)
|
|
||||||
{
|
|
||||||
Result += FString::Printf(TEXT("-%s=%s "), *Pair.Key, *Pair.Value);
|
|
||||||
}
|
|
||||||
for (const FString& Arg : CmdArguments)
|
|
||||||
{
|
|
||||||
Result += Arg + TEXT(" ");
|
|
||||||
}
|
|
||||||
return Result.TrimEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a boolean value, returns Default if not found or invalid
|
|
||||||
bool GetBool(const FString& Key, bool DefaultValue = false) const
|
|
||||||
{
|
|
||||||
FString Value;
|
|
||||||
if (TryGetValue(Key, Value))
|
|
||||||
{
|
|
||||||
if (Value.Equals(TEXT("true"), ESearchCase::IgnoreCase) ||
|
|
||||||
Value.Equals(TEXT("1")) ||
|
|
||||||
Value.Equals(TEXT("yes"), ESearchCase::IgnoreCase))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (Value.Equals(TEXT("false"), ESearchCase::IgnoreCase) ||
|
|
||||||
Value.Equals(TEXT("0")) ||
|
|
||||||
Value.Equals(TEXT("no"), ESearchCase::IgnoreCase))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return DefaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get an integer value, returns Default if not found or not numeric
|
|
||||||
int32 GetInt(const FString& Key, int32 DefaultValue = 0) const
|
|
||||||
{
|
|
||||||
FString Value;
|
|
||||||
if (TryGetValue(Key, Value))
|
|
||||||
{
|
|
||||||
return FCString::Atoi(*Value);
|
|
||||||
}
|
|
||||||
return DefaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a float value
|
|
||||||
float GetFloat(const FString& Key, float DefaultValue = 0.0f) const
|
|
||||||
{
|
|
||||||
FString Value;
|
|
||||||
if (TryGetValue(Key, Value))
|
|
||||||
{
|
|
||||||
return FCString::Atof(*Value);
|
|
||||||
}
|
|
||||||
return DefaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get as FName (useful for modes, map names, etc.)
|
|
||||||
FName GetName(const FString& Key, const FName& DefaultValue = NAME_None) const
|
|
||||||
{
|
|
||||||
FString Value;
|
|
||||||
if (TryGetValue(Key, Value))
|
|
||||||
{
|
|
||||||
return FName(*Value);
|
|
||||||
}
|
|
||||||
return DefaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a string value (core setter used by others)
|
|
||||||
void SetValue(const FString& Key, const FString& Value)
|
|
||||||
{
|
|
||||||
Options.Add(Key.ToLower(), Value.ToLower());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a boolean option
|
|
||||||
void SetBool(const FString& Key, bool bValue)
|
|
||||||
{
|
|
||||||
Options.Add(Key.ToLower(), bValue ? TEXT("true") : TEXT("false"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set an integer option
|
|
||||||
void SetInt(const FString& Key, int32 Value)
|
|
||||||
{
|
|
||||||
Options.Add(Key.ToLower(), FString::FromInt(Value));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a float option (with minimal formatting)
|
|
||||||
void SetFloat(const FString& Key, float Value, int32 Precision = 2)
|
|
||||||
{
|
|
||||||
FString FloatStr = FString::SanitizeFloat(Value, Precision);
|
|
||||||
Options.Add(Key.ToLower(), FloatStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set an FName option
|
|
||||||
void SetName(const FString& Key, const FName& NameValue)
|
|
||||||
{
|
|
||||||
Options.Add(Key.ToLower(), NameValue.ToString().ToLower());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a bare flag (true/false toggle stored for ToString output)
|
|
||||||
void SetFlag(const FString& Flag, bool bEnabled = true)
|
|
||||||
{
|
|
||||||
FString Key = Flag.ToLower();
|
|
||||||
Options.Add(Key, bEnabled ? TEXT("true") : TEXT("false"));
|
|
||||||
CmdArguments.Add(Flag); // maintain compatibility with older flag parsing
|
|
||||||
}
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
DECLARE_LOG_CATEGORY_EXTERN(LogShooterGameMode, Log, All);
|
DECLARE_LOG_CATEGORY_EXTERN(LogShooterGameMode, Log, All);
|
||||||
|
|
||||||
|
struct FProcessParameters;
|
||||||
|
struct FServerParameters;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -225,22 +24,43 @@ public:
|
|||||||
AShooterGameMode();
|
AShooterGameMode();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
UPROPERTY(Config, BlueprintReadOnly)
|
||||||
|
bool bIsAnywhereFleet = false;
|
||||||
|
|
||||||
|
UPROPERTY(Config, BlueprintReadOnly)
|
||||||
|
FString FleetId;
|
||||||
|
|
||||||
|
UPROPERTY(Config, BlueprintReadOnly)
|
||||||
|
FString AuthToken;
|
||||||
|
|
||||||
|
UPROPERTY(Config, BlueprintReadOnly)
|
||||||
|
FString HostId;
|
||||||
|
|
||||||
|
UPROPERTY(Config, BlueprintReadOnly)
|
||||||
|
FString WebSocketUrl;
|
||||||
|
|
||||||
|
UPROPERTY(Config)
|
||||||
|
FString ServerRegion;
|
||||||
|
|
||||||
|
UPROPERTY(Config, BlueprintReadOnly)
|
||||||
|
int32 ServerPort;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite)
|
||||||
|
bool bDebugMode = false;
|
||||||
|
|
||||||
virtual void BeginPlay() override;
|
virtual void BeginPlay() override;
|
||||||
|
virtual void InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
FString CachedCommandLine;
|
||||||
|
TSharedPtr<FProcessParameters> ProcessParameters;
|
||||||
|
TSharedPtr<FServerParameters> ServerParameters;
|
||||||
|
|
||||||
|
int32 GetConfiguredOrDefaultPort() const;
|
||||||
|
static FString GetSHA256Hash(const FString& Input);
|
||||||
|
|
||||||
void InitGameLift();
|
|
||||||
#if WITH_GAMELIFT
|
#if WITH_GAMELIFT
|
||||||
bool SetParametersFromCommandLine(FServerParameters& OutServerParameters, FProcessParameters& OutProcessParameters);
|
void InitGameLift();
|
||||||
static void LogServerParameters(const FServerParameters& InServerParameters);
|
|
||||||
void SetProcessIdOnServerParameters(FServerParameters& OutServerParameters);
|
|
||||||
bool ValidateServerParameters(const FString& CommandLine, const FString& ParameterName, FString& OutValue, const bool bCritical = true);
|
|
||||||
void LogServerSummary(const FServerParameters& InServerParameters, const FProcessParameters& InProcessParameters);
|
|
||||||
void BindGameLiftCallbackFunctions(FProcessParameters& ProcessParameters,
|
|
||||||
FGameLiftServerSDKModule& GameLiftSdkModule);
|
|
||||||
void InitiateProcessReady(FProcessParameters& ProcessParameters, FGameLiftServerSDKModule& GameLiftSdkModule);
|
|
||||||
bool InitiateConnectionWithGameLiftAgent(FServerParameters& ServerParameters,
|
|
||||||
FGameLiftServerSDKModule& GameLiftSdkModule);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user