|
|
|
|
| #include "LyraFrontendStateComponent.h"
|
|
|
| #include "CommonGameInstance.h"
|
| #include "CommonSessionSubsystem.h"
|
| #include "CommonUserSubsystem.h"
|
| #include "ControlFlowManager.h"
|
| #include "GameModes/LyraExperienceManagerComponent.h"
|
| #include "Kismet/GameplayStatics.h"
|
| #include "NativeGameplayTags.h"
|
| #include "PrimaryGameLayout.h"
|
| #include "Widgets/CommonActivatableWidgetContainer.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraFrontendStateComponent)
|
|
|
| namespace FrontendTags
|
| {
|
| UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_PLATFORM_TRAIT_SINGLEONLINEUSER, "Platform.Trait.SingleOnlineUser");
|
| UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_UI_LAYER_MENU, "UI.Layer.Menu");
|
| }
|
|
|
| ULyraFrontendStateComponent::ULyraFrontendStateComponent(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
| }
|
|
|
| void ULyraFrontendStateComponent::BeginPlay()
|
| {
|
| Super::BeginPlay();
|
|
|
|
|
| AGameStateBase* GameState = GetGameStateChecked<AGameStateBase>();
|
| ULyraExperienceManagerComponent* ExperienceComponent = GameState->FindComponentByClass<ULyraExperienceManagerComponent>();
|
| check(ExperienceComponent);
|
|
|
|
|
| ExperienceComponent->CallOrRegister_OnExperienceLoaded_HighPriority(FOnLyraExperienceLoaded::FDelegate::CreateUObject(this, &ThisClass::OnExperienceLoaded));
|
| }
|
|
|
| void ULyraFrontendStateComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
|
| {
|
| Super::EndPlay(EndPlayReason);
|
| }
|
|
|
| bool ULyraFrontendStateComponent::ShouldShowLoadingScreen(FString& OutReason) const
|
| {
|
| if (bShouldShowLoadingScreen)
|
| {
|
| OutReason = TEXT("Frontend Flow Pending...");
|
|
|
| if (FrontEndFlow.IsValid())
|
| {
|
| const TOptional<FString> StepDebugName = FrontEndFlow->GetCurrentStepDebugName();
|
| if (StepDebugName.IsSet())
|
| {
|
| OutReason = StepDebugName.GetValue();
|
| }
|
| }
|
|
|
| return true;
|
| }
|
|
|
| return false;
|
| }
|
|
|
| void ULyraFrontendStateComponent::OnExperienceLoaded(const ULyraExperienceDefinition* Experience)
|
| {
|
| FControlFlow& Flow = FControlFlowStatics::Create(this, TEXT("FrontendFlow"))
|
| .QueueStep(TEXT("Wait For User Initialization"), this, &ThisClass::FlowStep_WaitForUserInitialization)
|
| .QueueStep(TEXT("Try Show Press Start Screen"), this, &ThisClass::FlowStep_TryShowPressStartScreen)
|
| .QueueStep(TEXT("Try Join Requested Session"), this, &ThisClass::FlowStep_TryJoinRequestedSession)
|
| .QueueStep(TEXT("Try Show Main Screen"), this, &ThisClass::FlowStep_TryShowMainScreen);
|
|
|
| Flow.ExecuteFlow();
|
|
|
| FrontEndFlow = Flow.AsShared();
|
| }
|
|
|
| void ULyraFrontendStateComponent::FlowStep_WaitForUserInitialization(FControlFlowNodeRef SubFlow)
|
| {
|
|
|
|
|
| bool bWasHardDisconnect = false;
|
| AGameModeBase* GameMode = GetWorld()->GetAuthGameMode<AGameModeBase>();
|
| UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
|
|
|
| if (ensure(GameMode) && UGameplayStatics::HasOption(GameMode->OptionsString, TEXT("closed")))
|
| {
|
| bWasHardDisconnect = true;
|
| }
|
|
|
|
|
| UCommonUserSubsystem* UserSubsystem = GameInstance->GetSubsystem<UCommonUserSubsystem>();
|
| if (ensure(UserSubsystem) && bWasHardDisconnect)
|
| {
|
| UserSubsystem->ResetUserState();
|
| }
|
|
|
|
|
| UCommonSessionSubsystem* SessionSubsystem = GameInstance->GetSubsystem<UCommonSessionSubsystem>();
|
| if (ensure(SessionSubsystem))
|
| {
|
| SessionSubsystem->CleanUpSessions();
|
| }
|
|
|
| SubFlow->ContinueFlow();
|
| }
|
|
|
| void ULyraFrontendStateComponent::FlowStep_TryShowPressStartScreen(FControlFlowNodeRef SubFlow)
|
| {
|
| const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
|
| UCommonUserSubsystem* UserSubsystem = GameInstance->GetSubsystem<UCommonUserSubsystem>();
|
|
|
|
|
| if (const UCommonUserInfo* FirstUser = UserSubsystem->GetUserInfoForLocalPlayerIndex(0))
|
| {
|
| if (FirstUser->InitializationState == ECommonUserInitializationState::LoggedInLocalOnly ||
|
| FirstUser->InitializationState == ECommonUserInitializationState::LoggedInOnline)
|
| {
|
| SubFlow->ContinueFlow();
|
| return;
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| if (!UserSubsystem->ShouldWaitForStartInput())
|
| {
|
|
|
| InProgressPressStartScreen = SubFlow;
|
| UserSubsystem->OnUserInitializeComplete.AddDynamic(this, &ULyraFrontendStateComponent::OnUserInitialized);
|
| UserSubsystem->TryToInitializeForLocalPlay(0, FInputDeviceId(), false);
|
|
|
| return;
|
| }
|
|
|
|
|
| if (UPrimaryGameLayout* RootLayout = UPrimaryGameLayout::GetPrimaryGameLayoutForPrimaryPlayer(this))
|
| {
|
| constexpr bool bSuspendInputUntilComplete = true;
|
| RootLayout->PushWidgetToLayerStackAsync<UCommonActivatableWidget>(FrontendTags::TAG_UI_LAYER_MENU, bSuspendInputUntilComplete, PressStartScreenClass,
|
| [this, SubFlow](EAsyncWidgetLayerState State, UCommonActivatableWidget* Screen) {
|
| switch (State)
|
| {
|
| case EAsyncWidgetLayerState::AfterPush:
|
| bShouldShowLoadingScreen = false;
|
| Screen->OnDeactivated().AddWeakLambda(this, [this, SubFlow]() {
|
| SubFlow->ContinueFlow();
|
| });
|
| break;
|
| case EAsyncWidgetLayerState::Canceled:
|
| bShouldShowLoadingScreen = false;
|
| SubFlow->ContinueFlow();
|
| return;
|
| }
|
| });
|
| }
|
| }
|
|
|
| void ULyraFrontendStateComponent::OnUserInitialized(const UCommonUserInfo* UserInfo, bool bSuccess, FText Error, ECommonUserPrivilege RequestedPrivilege, ECommonUserOnlineContext OnlineContext)
|
| {
|
| FControlFlowNodePtr FlowToContinue = InProgressPressStartScreen;
|
| UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
|
| UCommonUserSubsystem* UserSubsystem = GameInstance->GetSubsystem<UCommonUserSubsystem>();
|
|
|
| if (ensure(FlowToContinue.IsValid() && UserSubsystem))
|
| {
|
| UserSubsystem->OnUserInitializeComplete.RemoveDynamic(this, &ULyraFrontendStateComponent::OnUserInitialized);
|
| InProgressPressStartScreen.Reset();
|
|
|
| if (bSuccess)
|
| {
|
|
|
| FlowToContinue->ContinueFlow();
|
| }
|
| else
|
| {
|
|
|
| FlowToContinue->ContinueFlow();
|
| }
|
| }
|
| }
|
|
|
| void ULyraFrontendStateComponent::FlowStep_TryJoinRequestedSession(FControlFlowNodeRef SubFlow)
|
| {
|
| UCommonGameInstance* GameInstance = Cast<UCommonGameInstance>(UGameplayStatics::GetGameInstance(this));
|
| if (GameInstance->GetRequestedSession() != nullptr && GameInstance->CanJoinRequestedSession())
|
| {
|
| UCommonSessionSubsystem* SessionSubsystem = GameInstance->GetSubsystem<UCommonSessionSubsystem>();
|
| if (ensure(SessionSubsystem))
|
| {
|
|
|
|
|
| OnJoinSessionCompleteEventHandle = SessionSubsystem->OnJoinSessionCompleteEvent.AddWeakLambda(this, [this, SubFlow, SessionSubsystem](const FOnlineResultInformation& Result)
|
| {
|
|
|
| SessionSubsystem->OnJoinSessionCompleteEvent.Remove(OnJoinSessionCompleteEventHandle);
|
| OnJoinSessionCompleteEventHandle.Reset();
|
|
|
| if (Result.bWasSuccessful)
|
| {
|
|
|
| SubFlow->CancelFlow();
|
| }
|
| else
|
| {
|
|
|
| SubFlow->ContinueFlow();
|
| return;
|
| }
|
| });
|
| GameInstance->JoinRequestedSession();
|
| return;
|
| }
|
| }
|
|
|
| SubFlow->ContinueFlow();
|
| }
|
|
|
| void ULyraFrontendStateComponent::FlowStep_TryShowMainScreen(FControlFlowNodeRef SubFlow)
|
| {
|
| if (UPrimaryGameLayout* RootLayout = UPrimaryGameLayout::GetPrimaryGameLayoutForPrimaryPlayer(this))
|
| {
|
| constexpr bool bSuspendInputUntilComplete = true;
|
| RootLayout->PushWidgetToLayerStackAsync<UCommonActivatableWidget>(FrontendTags::TAG_UI_LAYER_MENU, bSuspendInputUntilComplete, MainScreenClass,
|
| [this, SubFlow](EAsyncWidgetLayerState State, UCommonActivatableWidget* Screen) {
|
| switch (State)
|
| {
|
| case EAsyncWidgetLayerState::AfterPush:
|
| bShouldShowLoadingScreen = false;
|
| SubFlow->ContinueFlow();
|
| return;
|
| case EAsyncWidgetLayerState::Canceled:
|
| bShouldShowLoadingScreen = false;
|
| SubFlow->ContinueFlow();
|
| return;
|
| }
|
| });
|
| }
|
| }
|
|
|
|
|