|
|
|
|
| #include "LyraGameMode.h"
|
| #include "AssetRegistry/AssetData.h"
|
| #include "Engine/GameInstance.h"
|
| #include "Engine/World.h"
|
| #include "LyraLogChannels.h"
|
| #include "Misc/CommandLine.h"
|
| #include "System/LyraAssetManager.h"
|
| #include "LyraGameState.h"
|
| #include "System/LyraGameSession.h"
|
| #include "Player/LyraPlayerController.h"
|
| #include "Player/LyraPlayerBotController.h"
|
| #include "Player/LyraPlayerState.h"
|
| #include "Character/LyraCharacter.h"
|
| #include "UI/LyraHUD.h"
|
| #include "Character/LyraPawnExtensionComponent.h"
|
| #include "Character/LyraPawnData.h"
|
| #include "GameModes/LyraWorldSettings.h"
|
| #include "GameModes/LyraExperienceDefinition.h"
|
| #include "GameModes/LyraExperienceManagerComponent.h"
|
| #include "GameModes/LyraUserFacingExperienceDefinition.h"
|
| #include "Kismet/GameplayStatics.h"
|
| #include "Development/LyraDeveloperSettings.h"
|
| #include "Player/LyraPlayerSpawningManagerComponent.h"
|
| #include "CommonUserSubsystem.h"
|
| #include "CommonSessionSubsystem.h"
|
| #include "TimerManager.h"
|
| #include "GameMapsSettings.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraGameMode)
|
|
|
| ALyraGameMode::ALyraGameMode(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
| GameStateClass = ALyraGameState::StaticClass();
|
| GameSessionClass = ALyraGameSession::StaticClass();
|
| PlayerControllerClass = ALyraPlayerController::StaticClass();
|
| ReplaySpectatorPlayerControllerClass = ALyraReplayPlayerController::StaticClass();
|
| PlayerStateClass = ALyraPlayerState::StaticClass();
|
| DefaultPawnClass = ALyraCharacter::StaticClass();
|
| HUDClass = ALyraHUD::StaticClass();
|
| }
|
|
|
| const ULyraPawnData* ALyraGameMode::GetPawnDataForController(const AController* InController) const
|
| {
|
|
|
| if (InController != nullptr)
|
| {
|
| if (const ALyraPlayerState* LyraPS = InController->GetPlayerState<ALyraPlayerState>())
|
| {
|
| if (const ULyraPawnData* PawnData = LyraPS->GetPawnData<ULyraPawnData>())
|
| {
|
| return PawnData;
|
| }
|
| }
|
| }
|
|
|
|
|
| check(GameState);
|
| ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
|
| check(ExperienceComponent);
|
|
|
| if (ExperienceComponent->IsExperienceLoaded())
|
| {
|
| const ULyraExperienceDefinition* Experience = ExperienceComponent->GetCurrentExperienceChecked();
|
| if (Experience->DefaultPawnData != nullptr)
|
| {
|
| return Experience->DefaultPawnData;
|
| }
|
|
|
|
|
| return ULyraAssetManager::Get().GetDefaultPawnData();
|
| }
|
|
|
|
|
| return nullptr;
|
| }
|
|
|
| void ALyraGameMode::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
|
| {
|
| Super::InitGame(MapName, Options, ErrorMessage);
|
|
|
|
|
| GetWorld()->GetTimerManager().SetTimerForNextTick(this, &ThisClass::HandleMatchAssignmentIfNotExpectingOne);
|
| }
|
|
|
| void ALyraGameMode::HandleMatchAssignmentIfNotExpectingOne()
|
| {
|
| FPrimaryAssetId ExperienceId;
|
| FString ExperienceIdSource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| UWorld* World = GetWorld();
|
|
|
| if (!ExperienceId.IsValid() && UGameplayStatics::HasOption(OptionsString, TEXT("Experience")))
|
| {
|
| const FString ExperienceFromOptions = UGameplayStatics::ParseOption(OptionsString, TEXT("Experience"));
|
| ExperienceId = FPrimaryAssetId(FPrimaryAssetType(ULyraExperienceDefinition::StaticClass()->GetFName()), FName(*ExperienceFromOptions));
|
| ExperienceIdSource = TEXT("OptionsString");
|
| }
|
|
|
| if (!ExperienceId.IsValid() && World->IsPlayInEditor())
|
| {
|
| ExperienceId = GetDefault<ULyraDeveloperSettings>()->ExperienceOverride;
|
| ExperienceIdSource = TEXT("DeveloperSettings");
|
| }
|
|
|
|
|
| if (!ExperienceId.IsValid())
|
| {
|
| FString ExperienceFromCommandLine;
|
| if (FParse::Value(FCommandLine::Get(), TEXT("Experience="), ExperienceFromCommandLine))
|
| {
|
| ExperienceId = FPrimaryAssetId::ParseTypeAndName(ExperienceFromCommandLine);
|
| if (!ExperienceId.PrimaryAssetType.IsValid())
|
| {
|
| ExperienceId = FPrimaryAssetId(FPrimaryAssetType(ULyraExperienceDefinition::StaticClass()->GetFName()), FName(*ExperienceFromCommandLine));
|
| }
|
| ExperienceIdSource = TEXT("CommandLine");
|
| }
|
| }
|
|
|
|
|
| if (!ExperienceId.IsValid())
|
| {
|
| if (ALyraWorldSettings* TypedWorldSettings = Cast<ALyraWorldSettings>(GetWorldSettings()))
|
| {
|
| ExperienceId = TypedWorldSettings->GetDefaultGameplayExperience();
|
| ExperienceIdSource = TEXT("WorldSettings");
|
| }
|
| }
|
|
|
| ULyraAssetManager& AssetManager = ULyraAssetManager::Get();
|
| FAssetData Dummy;
|
| if (ExperienceId.IsValid() && !AssetManager.GetPrimaryAssetData(ExperienceId, Dummy))
|
| {
|
| UE_LOG(LogLyraExperience, Error, TEXT("EXPERIENCE: Wanted to use %s but couldn't find it, falling back to the default)"), *ExperienceId.ToString());
|
| ExperienceId = FPrimaryAssetId();
|
| }
|
|
|
|
|
| if (!ExperienceId.IsValid())
|
| {
|
| if (TryDedicatedServerLogin())
|
| {
|
|
|
| return;
|
| }
|
|
|
|
|
| ExperienceId = FPrimaryAssetId(FPrimaryAssetType("LyraExperienceDefinition"), FName("B_LyraDefaultExperience"));
|
| ExperienceIdSource = TEXT("Default");
|
| }
|
|
|
| OnMatchAssignmentGiven(ExperienceId, ExperienceIdSource);
|
| }
|
|
|
| bool ALyraGameMode::TryDedicatedServerLogin()
|
| {
|
|
|
| FString DefaultMap = UGameMapsSettings::GetGameDefaultMap();
|
| UWorld* World = GetWorld();
|
| UGameInstance* GameInstance = GetGameInstance();
|
| if (GameInstance && World && World->GetNetMode() == NM_DedicatedServer && World->URL.Map == DefaultMap)
|
| {
|
|
|
| UCommonUserSubsystem* UserSubsystem = GameInstance->GetSubsystem<UCommonUserSubsystem>();
|
|
|
|
|
| UserSubsystem->OnUserInitializeComplete.AddDynamic(this, &ALyraGameMode::OnUserInitializedForDedicatedServer);
|
|
|
|
|
| if (!UserSubsystem->TryToLoginForOnlinePlay(0))
|
| {
|
| OnUserInitializedForDedicatedServer(nullptr, false, FText(), ECommonUserPrivilege::CanPlayOnline, ECommonUserOnlineContext::Default);
|
| }
|
|
|
| return true;
|
| }
|
|
|
| return false;
|
| }
|
|
|
| void ALyraGameMode::HostDedicatedServerMatch(ECommonSessionOnlineMode OnlineMode)
|
| {
|
| FPrimaryAssetType UserExperienceType = ULyraUserFacingExperienceDefinition::StaticClass()->GetFName();
|
|
|
|
|
| FPrimaryAssetId UserExperienceId;
|
| FString UserExperienceFromCommandLine;
|
| if (FParse::Value(FCommandLine::Get(), TEXT("UserExperience="), UserExperienceFromCommandLine) ||
|
| FParse::Value(FCommandLine::Get(), TEXT("Playlist="), UserExperienceFromCommandLine))
|
| {
|
| UserExperienceId = FPrimaryAssetId::ParseTypeAndName(UserExperienceFromCommandLine);
|
| if (!UserExperienceId.PrimaryAssetType.IsValid())
|
| {
|
| UserExperienceId = FPrimaryAssetId(FPrimaryAssetType(UserExperienceType), FName(*UserExperienceFromCommandLine));
|
| }
|
| }
|
|
|
|
|
| ULyraAssetManager& AssetManager = ULyraAssetManager::Get();
|
| TSharedPtr<FStreamableHandle> Handle = AssetManager.LoadPrimaryAssetsWithType(UserExperienceType);
|
| if (ensure(Handle.IsValid()))
|
| {
|
| Handle->WaitUntilComplete();
|
| }
|
|
|
| TArray<UObject*> UserExperiences;
|
| AssetManager.GetPrimaryAssetObjectList(UserExperienceType, UserExperiences);
|
| ULyraUserFacingExperienceDefinition* FoundExperience = nullptr;
|
| ULyraUserFacingExperienceDefinition* DefaultExperience = nullptr;
|
|
|
| for (UObject* Object : UserExperiences)
|
| {
|
| ULyraUserFacingExperienceDefinition* UserExperience = Cast<ULyraUserFacingExperienceDefinition>(Object);
|
| if (ensure(UserExperience))
|
| {
|
| if (UserExperience->GetPrimaryAssetId() == UserExperienceId)
|
| {
|
| FoundExperience = UserExperience;
|
| break;
|
| }
|
|
|
| if (UserExperience->bIsDefaultExperience && DefaultExperience == nullptr)
|
| {
|
| DefaultExperience = UserExperience;
|
| }
|
| }
|
| }
|
|
|
| if (FoundExperience == nullptr)
|
| {
|
| FoundExperience = DefaultExperience;
|
| }
|
|
|
| UGameInstance* GameInstance = GetGameInstance();
|
| if (ensure(FoundExperience && GameInstance))
|
| {
|
|
|
| UCommonSession_HostSessionRequest* HostRequest = FoundExperience->CreateHostingRequest(this);
|
| if (ensure(HostRequest))
|
| {
|
| HostRequest->OnlineMode = OnlineMode;
|
|
|
|
|
|
|
| UCommonSessionSubsystem* SessionSubsystem = GameInstance->GetSubsystem<UCommonSessionSubsystem>();
|
| SessionSubsystem->HostSession(nullptr, HostRequest);
|
|
|
|
|
| }
|
| }
|
|
|
| }
|
|
|
| void ALyraGameMode::OnUserInitializedForDedicatedServer(const UCommonUserInfo* UserInfo, bool bSuccess, FText Error, ECommonUserPrivilege RequestedPrivilege, ECommonUserOnlineContext OnlineContext)
|
| {
|
| UGameInstance* GameInstance = GetGameInstance();
|
| if (GameInstance)
|
| {
|
|
|
| UCommonUserSubsystem* UserSubsystem = GameInstance->GetSubsystem<UCommonUserSubsystem>();
|
| UserSubsystem->OnUserInitializeComplete.RemoveDynamic(this, &ALyraGameMode::OnUserInitializedForDedicatedServer);
|
|
|
|
|
| if (bSuccess && ensure(UserInfo))
|
| {
|
| UE_LOG(LogLyraExperience, Log, TEXT("Dedicated server user login succeeded for id %s, starting online server"), *UserInfo->GetNetId().ToString());
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyraExperience, Log, TEXT("Dedicated server user login unsuccessful, starting online server as login is not required"));
|
| }
|
|
|
| HostDedicatedServerMatch(ECommonSessionOnlineMode::Online);
|
| }
|
| }
|
|
|
| void ALyraGameMode::OnMatchAssignmentGiven(FPrimaryAssetId ExperienceId, const FString& ExperienceIdSource)
|
| {
|
| if (ExperienceId.IsValid())
|
| {
|
| UE_LOG(LogLyraExperience, Log, TEXT("Identified experience %s (Source: %s)"), *ExperienceId.ToString(), *ExperienceIdSource);
|
|
|
| ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
|
| check(ExperienceComponent);
|
| ExperienceComponent->SetCurrentExperience(ExperienceId);
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyraExperience, Error, TEXT("Failed to identify experience, loading screen will stay up forever"));
|
| }
|
| }
|
|
|
| void ALyraGameMode::OnExperienceLoaded(const ULyraExperienceDefinition* CurrentExperience)
|
| {
|
|
|
|
|
|
|
| for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
|
| {
|
| APlayerController* PC = Cast<APlayerController>(*Iterator);
|
| if ((PC != nullptr) && (PC->GetPawn() == nullptr))
|
| {
|
| if (PlayerCanRestart(PC))
|
| {
|
| RestartPlayer(PC);
|
| }
|
| }
|
| }
|
| }
|
|
|
| bool ALyraGameMode::IsExperienceLoaded() const
|
| {
|
| check(GameState);
|
| ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
|
| check(ExperienceComponent);
|
|
|
| return ExperienceComponent->IsExperienceLoaded();
|
| }
|
|
|
| UClass* ALyraGameMode::GetDefaultPawnClassForController_Implementation(AController* InController)
|
| {
|
| if (const ULyraPawnData* PawnData = GetPawnDataForController(InController))
|
| {
|
| if (PawnData->PawnClass)
|
| {
|
| return PawnData->PawnClass;
|
| }
|
| }
|
|
|
| return Super::GetDefaultPawnClassForController_Implementation(InController);
|
| }
|
|
|
| APawn* ALyraGameMode::SpawnDefaultPawnAtTransform_Implementation(AController* NewPlayer, const FTransform& SpawnTransform)
|
| {
|
| FActorSpawnParameters SpawnInfo;
|
| SpawnInfo.Instigator = GetInstigator();
|
| SpawnInfo.ObjectFlags |= RF_Transient;
|
| SpawnInfo.bDeferConstruction = true;
|
|
|
| if (UClass* PawnClass = GetDefaultPawnClassForController(NewPlayer))
|
| {
|
| if (APawn* SpawnedPawn = GetWorld()->SpawnActor<APawn>(PawnClass, SpawnTransform, SpawnInfo))
|
| {
|
| if (ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(SpawnedPawn))
|
| {
|
| if (const ULyraPawnData* PawnData = GetPawnDataForController(NewPlayer))
|
| {
|
| PawnExtComp->SetPawnData(PawnData);
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyra, Error, TEXT("Game mode was unable to set PawnData on the spawned pawn [%s]."), *GetNameSafe(SpawnedPawn));
|
| }
|
| }
|
|
|
| SpawnedPawn->FinishSpawning(SpawnTransform);
|
|
|
| return SpawnedPawn;
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyra, Error, TEXT("Game mode was unable to spawn Pawn of class [%s] at [%s]."), *GetNameSafe(PawnClass), *SpawnTransform.ToHumanReadableString());
|
| }
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyra, Error, TEXT("Game mode was unable to spawn Pawn due to NULL pawn class."));
|
| }
|
|
|
| return nullptr;
|
| }
|
|
|
| bool ALyraGameMode::ShouldSpawnAtStartSpot(AController* Player)
|
| {
|
|
|
| return false;
|
| }
|
|
|
| void ALyraGameMode::HandleStartingNewPlayer_Implementation(APlayerController* NewPlayer)
|
| {
|
|
|
|
|
| if (IsExperienceLoaded())
|
| {
|
| Super::HandleStartingNewPlayer_Implementation(NewPlayer);
|
| }
|
| }
|
|
|
| AActor* ALyraGameMode::ChoosePlayerStart_Implementation(AController* Player)
|
| {
|
| if (ULyraPlayerSpawningManagerComponent* PlayerSpawningComponent = GameState->FindComponentByClass<ULyraPlayerSpawningManagerComponent>())
|
| {
|
| return PlayerSpawningComponent->ChoosePlayerStart(Player);
|
| }
|
|
|
| return Super::ChoosePlayerStart_Implementation(Player);
|
| }
|
|
|
| void ALyraGameMode::FinishRestartPlayer(AController* NewPlayer, const FRotator& StartRotation)
|
| {
|
| if (ULyraPlayerSpawningManagerComponent* PlayerSpawningComponent = GameState->FindComponentByClass<ULyraPlayerSpawningManagerComponent>())
|
| {
|
| PlayerSpawningComponent->FinishRestartPlayer(NewPlayer, StartRotation);
|
| }
|
|
|
| Super::FinishRestartPlayer(NewPlayer, StartRotation);
|
| }
|
|
|
| bool ALyraGameMode::PlayerCanRestart_Implementation(APlayerController* Player)
|
| {
|
| return ControllerCanRestart(Player);
|
| }
|
|
|
| bool ALyraGameMode::ControllerCanRestart(AController* Controller)
|
| {
|
| if (APlayerController* PC = Cast<APlayerController>(Controller))
|
| {
|
| if (!Super::PlayerCanRestart_Implementation(PC))
|
| {
|
| return false;
|
| }
|
| }
|
| else
|
| {
|
|
|
| if ((Controller == nullptr) || Controller->IsPendingKillPending())
|
| {
|
| return false;
|
| }
|
| }
|
|
|
| if (ULyraPlayerSpawningManagerComponent* PlayerSpawningComponent = GameState->FindComponentByClass<ULyraPlayerSpawningManagerComponent>())
|
| {
|
| return PlayerSpawningComponent->ControllerCanRestart(Controller);
|
| }
|
|
|
| return true;
|
| }
|
|
|
| void ALyraGameMode::InitGameState()
|
| {
|
| Super::InitGameState();
|
|
|
|
|
| ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
|
| check(ExperienceComponent);
|
| ExperienceComponent->CallOrRegister_OnExperienceLoaded(FOnLyraExperienceLoaded::FDelegate::CreateUObject(this, &ThisClass::OnExperienceLoaded));
|
| }
|
|
|
| void ALyraGameMode::GenericPlayerInitialization(AController* NewPlayer)
|
| {
|
| Super::GenericPlayerInitialization(NewPlayer);
|
|
|
| OnGameModePlayerInitialized.Broadcast(this, NewPlayer);
|
| }
|
|
|
| void ALyraGameMode::RequestPlayerRestartNextFrame(AController* Controller, bool bForceReset)
|
| {
|
| if (bForceReset && (Controller != nullptr))
|
| {
|
| Controller->Reset();
|
| }
|
|
|
| if (APlayerController* PC = Cast<APlayerController>(Controller))
|
| {
|
| GetWorldTimerManager().SetTimerForNextTick(PC, &APlayerController::ServerRestartPlayer_Implementation);
|
| }
|
| else if (ALyraPlayerBotController* BotController = Cast<ALyraPlayerBotController>(Controller))
|
| {
|
| GetWorldTimerManager().SetTimerForNextTick(BotController, &ALyraPlayerBotController::ServerRestartController);
|
| }
|
| }
|
|
|
| bool ALyraGameMode::UpdatePlayerStartSpot(AController* Player, const FString& Portal, FString& OutErrorMessage)
|
| {
|
|
|
|
|
| return true;
|
| }
|
|
|
| void ALyraGameMode::FailedToRestartPlayer(AController* NewPlayer)
|
| {
|
| Super::FailedToRestartPlayer(NewPlayer);
|
|
|
|
|
|
|
| if (UClass* PawnClass = GetDefaultPawnClassForController(NewPlayer))
|
| {
|
| if (APlayerController* NewPC = Cast<APlayerController>(NewPlayer))
|
| {
|
|
|
| if (PlayerCanRestart(NewPC))
|
| {
|
| RequestPlayerRestartNextFrame(NewPlayer, false);
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyra, Verbose, TEXT("FailedToRestartPlayer(%s) and PlayerCanRestart returned false, so we're not going to try again."), *GetPathNameSafe(NewPlayer));
|
| }
|
| }
|
| else
|
| {
|
| RequestPlayerRestartNextFrame(NewPlayer, false);
|
| }
|
| }
|
| else
|
| {
|
| UE_LOG(LogLyra, Verbose, TEXT("FailedToRestartPlayer(%s) but there's no pawn class so giving up."), *GetPathNameSafe(NewPlayer));
|
| }
|
| }
|
|
|