Modified ShooterGameMode - At Lesson 35 - Working

This commit is contained in:
Norman Lansing
2026-03-01 09:29:41 -05:00
parent 4fde462bce
commit 36050d4ea1
3 changed files with 397 additions and 159 deletions

View File

@@ -30,210 +30,169 @@ void AShooterGameMode::BeginPlay()
}
#if WITH_GAMELIFT
void AShooterGameMode::SetServerParameters(TSharedPtr<FServerParameters> OutServerParameters)
// 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;
FString CommandLine = FCommandLine::Get();
UE_LOG(LogShooterGameMode, Log, TEXT(">>> Command Line: %s"), *CommandLine);
// Checks if param exists (returns bool)
// 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.
FString GameLiftAnywhereAuthToken = "";
if (FParse::Value(FCommandLine::Get(), TEXT("-authtoken="), GameLiftAnywhereAuthToken))
{
OutServerParameters->m_authToken = GameLiftAnywhereAuthToken;
}
else
{
UE_LOG(LogShooterGameMode, Warning, TEXT("-authtoken not found in command line."));
}
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("authtoken"),OutServerParameters.m_authToken);
// The Host/compute-name of the GameLift Anywhere instance
FString GameLiftAnywhereHostId = "";
if (FParse::Value(FCommandLine::Get(), TEXT("-hostid="), GameLiftAnywhereHostId))
{
OutServerParameters->m_hostId = GameLiftAnywhereHostId;
}
else
{
UE_LOG(LogShooterGameMode, Warning, TEXT("-hostid not found in command line."));
}
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("hostid"),OutServerParameters.m_hostId);
// The AnywhereFleet ID.
FString GameLiftAnywhereFleetId = "";
if (FParse::Value(FCommandLine::Get(), TEXT("-fleetid="), GameLiftAnywhereFleetId))
{
OutServerParameters->m_fleetId = GameLiftAnywhereFleetId;
}
else
{
UE_LOG(LogShooterGameMode, Warning, TEXT("-fleetid not found in command line."));
}
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("fleetid"),OutServerParameters.m_fleetId);
// The WebSocket URL (GameLiftServiceSdkEndpoint)
FString GameLiftAnywhereWebSocketUrl = "";
if (FParse::Value(FCommandLine::Get(), TEXT("-websocketurl="), GameLiftAnywhereWebSocketUrl))
{
OutServerParameters->m_webSocketUrl = GameLiftAnywhereWebSocketUrl;
}
else
{
UE_LOG(LogShooterGameMode, Warning, TEXT("-websocketurl not found in command line."));
}
bCriticalFault |= ValidateServerParameters(CommandLine, TEXT("websocketurl"),OutServerParameters.m_webSocketUrl);
// 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
// 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(TSharedPtr<FServerParameters> ServerParameters)
void AShooterGameMode::LogServerParameters(const FServerParameters& InServerParameters)
{
// Log Server Parameters
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_YELLOW);
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> WebSocket URL: %s"), *ServerParameters->m_webSocketUrl);
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Fleet ID: %s"), *ServerParameters->m_fleetId);
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Process ID: %s"), *ServerParameters->m_processId);
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Host ID (Compute Name): %s"), *ServerParameters->m_hostId);
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> Auth Token: %s"), *ServerParameters->m_authToken);
UE_LOG(LogShooterGameMode, Log, TEXT(">>>> WebSocket URL: %s"), *InServerParameters.m_webSocketUrl);
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);
}
#endif
void AShooterGameMode::ParseCommandLinePort(int32& OutPort)
void AShooterGameMode::SetProcessIdOnServerParameters(FServerParameters& OutServerParameters)
{
TArray<FString> CommandLineTokens;
TArray<FString> CommandLineSwitches;
FCommandLine::Parse(FCommandLine::Get(), CommandLineTokens, CommandLineSwitches);
for (FString SwitchStr : CommandLineSwitches)
{
FString Key;
FString Value;
if (SwitchStr.Split("=", &Key, &Value))
{
if (Key.Equals(TEXT("port"), ESearchCase::IgnoreCase))
{
OutPort = FCString::Atoi(*Value);
return;
}
}
}
// 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
}
void AShooterGameMode::InitGameLift()
bool AShooterGameMode::ValidateServerParameters(const FString& CommandLine, const FString& ParameterName,
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;
}
#if WITH_GAMELIFT
UE_LOG(LogShooterGameMode, Log, TEXT("Calling InitGameLift"));
FProcessParameters LocalProcessParameters;
void AShooterGameMode::LogServerSummary(const FServerParameters& InServerParameters,
const FProcessParameters& InProcessParameters)
{
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_YELLOW);
UE_LOG(LogShooterGameMode, Log, TEXT("=== SERVER STARTUP SUMMARY ==="));
UE_LOG(LogShooterGameMode, Log, TEXT("Port: %d"), InProcessParameters.port);
// Getting the module first.
FGameLiftServerSDKModule* GameLiftSdkModule = &FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK"));
// Inline validation of ServerParams
bool bAllValid = !InServerParameters.m_authToken.IsEmpty() &&
!InServerParameters.m_hostId.IsEmpty() &&
!InServerParameters.m_fleetId.IsEmpty() &&
!InServerParameters.m_webSocketUrl.IsEmpty();
//Define the server parameters for a GameLift Anywhere fleet. These are not needed for a GameLift managed EC2 fleet.
FServerParameters ServerParameters;
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 GameLift Anywhere is enabled, parse command line arguments and pass them in the ServerParameters object.
TSharedPtr<FServerParameters> ServerParams = MakeShared<FServerParameters>(ServerParameters);
SetServerParameters(ServerParams);
LogServerParameters(ServerParams);
ServerParameters = *ServerParams;
}
UE_LOG(LogShooterGameMode, Log, TEXT("All Anywhere params: %s"),
bAllValid ? TEXT("VALID ✓") : TEXT("MISSING ❌"));
UE_LOG(LogShooterGameMode, SetColor, TEXT("%s"), COLOR_NONE);
}
UE_LOG(LogShooterGameMode, Log, TEXT("Initializing the GameLift Server..."));
// InitSDK will establish a local connection with GameLift's agent to enable futher 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;
}
void AShooterGameMode::BindGameLiftCallbackFunctions(FProcessParameters& ProcessParameters, FGameLiftServerSDKModule& GameLiftSdkModule)
{
// Implement callback function onStartGameSession
// GameLift sends a game session activation request to the game server
// 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().
auto OnGameSession = [=](const Aws::GameLift::Server::Model::GameSession& InGameSession)
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);
GameLiftSdkModule->ActivateGameSession();
};
LocalProcessParameters.OnStartGameSession.BindLambda(OnGameSession);
FGameLiftGenericOutcome ActivateGameSessionOutcome = GameLiftSdkModule.ActivateGameSession();
if (ActivateGameSessionOutcome.IsSuccess())
{
UE_LOG(LogShooterGameMode, Log, TEXT("Activate GameSession successful"));
}
else
{
const FGameLiftError& Error = ActivateGameSessionOutcome.GetError();
UE_LOG(LogShooterGameMode, Error, TEXT("ActivateGameSession() failed. Error: %s"), Error.m_errorMessage.IsEmpty() ? TEXT("Unknown Error") : *Error.m_errorMessage);
FPlatformMisc::RequestExit(true);
}
});
auto OnTerminate = [=]()
ProcessParameters.OnTerminate.BindLambda([this, &GameLiftSdkModule]()
{
UE_LOG(LogShooterGameMode, Log, TEXT("Game Server Process is terminating"));
GameLiftSdkModule->ProcessEnding();
};
FGameLiftGenericOutcome ProcessEndingOutcome = GameLiftSdkModule.ProcessEnding();
FGameLiftGenericOutcome DestroyOutcome = GameLiftSdkModule.Destroy();
if (ProcessEndingOutcome.IsSuccess() && DestroyOutcome.IsSuccess())
{
UE_LOG(LogShooterGameMode, Log, TEXT("Server process ending successfully"));
FPlatformMisc::RequestExit(false);
}
else
{
if (!ProcessEndingOutcome.IsSuccess())
{
const FGameLiftError& Error = ProcessEndingOutcome.GetError();
UE_LOG(LogShooterGameMode, Error, TEXT("ProcessEnding() failed. Error: %s"), Error.m_errorMessage.IsEmpty() ? TEXT("Unknown Error") : *Error.m_errorMessage);
}
if (!DestroyOutcome.IsSuccess())
{
const FGameLiftError& Error = DestroyOutcome.GetError();
UE_LOG(LogShooterGameMode, Error, TEXT("Destroy() failed. Error: %s"), Error.m_errorMessage.IsEmpty() ? TEXT("Unknown error") : *Error.m_errorMessage);
}
FPlatformMisc::RequestExit(true);
}
});
LocalProcessParameters.OnTerminate.BindLambda(OnTerminate);
auto OnHealthCheck = [=]()
ProcessParameters.OnHealthCheck.BindLambda([=]()
{
UE_LOG(LogShooterGameMode, Log, TEXT("Performing Health Check"));
return true;
};
LocalProcessParameters.OnHealthCheck.BindLambda(OnHealthCheck);
int32 Port = FURL::UrlConfig.DefaultPort;
ParseCommandLinePort(Port);
LocalProcessParameters.port = Port;
});
// 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;
LogFiles.Add(TEXT("FPSTemplate/Saved/Logs/FPSTemplate.log"));
FString LogDirectory =FPaths::ProjectSavedDir() / TEXT("Logs");
FString LogFile = LogDirectory / TEXT("GameLiftServer.log");
LogFiles.Add(LogFile);
UE_LOG(LogShooterGameMode, Log, TEXT("Before Adding Log Files"));
LocalProcessParameters.logParameters = LogFiles;
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(LocalProcessParameters);
FGameLiftGenericOutcome ProcessReadyOutcome = GameLiftSdkModule.ProcessReady(ProcessParameters);
if (ProcessReadyOutcome.IsSuccess())
{
@@ -251,8 +210,82 @@ void AShooterGameMode::InitGameLift()
}
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()
{
#if WITH_GAMELIFT
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..."));
if (InitiateConnectionWithGameLiftAgent(ServerParameters, GameLiftSdkModule)) return;
BindGameLiftCallbackFunctions(ProcessParameters, GameLiftSdkModule);
InitiateProcessReady(ProcessParameters, GameLiftSdkModule);
}
else
{
// Using EC2
UE_LOG(LogShooterGameMode, Log, TEXT("Using Gamelift EC2 Managed Fleet"));
//TODO: Implement EC2 later.
UE_LOG(LogShooterGameMode, Log, TEXT("Not Implemented"));
}
#endif
}