|
|
|
|
| #include "CommonGameInstance.h"
|
|
|
| #include "CommonLocalPlayer.h"
|
| #include "CommonSessionSubsystem.h"
|
| #include "CommonUISettings.h"
|
| #include "CommonUserSubsystem.h"
|
| #include "GameUIManagerSubsystem.h"
|
| #include "ICommonUIModule.h"
|
| #include "LogCommonGame.h"
|
| #include "Messaging/CommonGameDialog.h"
|
| #include "Messaging/CommonMessagingSubsystem.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(CommonGameInstance)
|
|
|
| UCommonGameInstance::UCommonGameInstance(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| {
|
|
|
| }
|
|
|
| void UCommonGameInstance::HandleSystemMessage(FGameplayTag MessageType, FText Title, FText Message)
|
| {
|
| ULocalPlayer* FirstPlayer = GetFirstGamePlayer();
|
|
|
| if (FirstPlayer && MessageType.MatchesTag(FCommonUserTags::SystemMessage_Error))
|
| {
|
| if (UCommonMessagingSubsystem* Messaging = FirstPlayer->GetSubsystem<UCommonMessagingSubsystem>())
|
| {
|
| Messaging->ShowError(UCommonGameDialogDescriptor::CreateConfirmationOk(Title, Message));
|
| }
|
| }
|
| }
|
|
|
| void UCommonGameInstance::HandlePrivilegeChanged(const UCommonUserInfo* UserInfo, ECommonUserPrivilege Privilege, ECommonUserAvailability OldAvailability, ECommonUserAvailability NewAvailability)
|
| {
|
|
|
| if (Privilege == ECommonUserPrivilege::CanPlay && OldAvailability == ECommonUserAvailability::NowAvailable && NewAvailability != ECommonUserAvailability::NowAvailable)
|
| {
|
| UE_LOG(LogCommonGame, Error, TEXT("HandlePrivilegeChanged: Player %d no longer has permission to play the game!"), UserInfo->LocalPlayerIndex);
|
|
|
|
|
| }
|
| }
|
|
|
| void UCommonGameInstance::HandlerUserInitialized(const UCommonUserInfo* UserInfo, bool bSuccess, FText Error, ECommonUserPrivilege RequestedPrivilege, ECommonUserOnlineContext OnlineContext)
|
| {
|
|
|
| }
|
|
|
| int32 UCommonGameInstance::AddLocalPlayer(ULocalPlayer* NewPlayer, FPlatformUserId UserId)
|
| {
|
| int32 ReturnVal = Super::AddLocalPlayer(NewPlayer, UserId);
|
| if (ReturnVal != INDEX_NONE)
|
| {
|
| if (!PrimaryPlayer.IsValid())
|
| {
|
| UE_LOG(LogCommonGame, Log, TEXT("AddLocalPlayer: Set %s to Primary Player"), *NewPlayer->GetName());
|
| PrimaryPlayer = NewPlayer;
|
| }
|
|
|
| GetSubsystem<UGameUIManagerSubsystem>()->NotifyPlayerAdded(Cast<UCommonLocalPlayer>(NewPlayer));
|
| }
|
|
|
| return ReturnVal;
|
| }
|
|
|
| bool UCommonGameInstance::RemoveLocalPlayer(ULocalPlayer* ExistingPlayer)
|
| {
|
| if (PrimaryPlayer == ExistingPlayer)
|
| {
|
|
|
| PrimaryPlayer.Reset();
|
| UE_LOG(LogCommonGame, Log, TEXT("RemoveLocalPlayer: Unsetting Primary Player from %s"), *ExistingPlayer->GetName());
|
| }
|
| GetSubsystem<UGameUIManagerSubsystem>()->NotifyPlayerDestroyed(Cast<UCommonLocalPlayer>(ExistingPlayer));
|
|
|
| return Super::RemoveLocalPlayer(ExistingPlayer);
|
| }
|
|
|
| void UCommonGameInstance::Init()
|
| {
|
| Super::Init();
|
|
|
|
|
| FGameplayTagContainer PlatformTraits = ICommonUIModule::GetSettings().GetPlatformTraits();
|
|
|
| UCommonUserSubsystem* UserSubsystem = GetSubsystem<UCommonUserSubsystem>();
|
| if (ensure(UserSubsystem))
|
| {
|
| UserSubsystem->SetTraitTags(PlatformTraits);
|
| UserSubsystem->OnHandleSystemMessage.AddDynamic(this, &UCommonGameInstance::HandleSystemMessage);
|
| UserSubsystem->OnUserPrivilegeChanged.AddDynamic(this, &UCommonGameInstance::HandlePrivilegeChanged);
|
| UserSubsystem->OnUserInitializeComplete.AddDynamic(this, &UCommonGameInstance::HandlerUserInitialized);
|
| }
|
|
|
| UCommonSessionSubsystem* SessionSubsystem = GetSubsystem<UCommonSessionSubsystem>();
|
| if (ensure(SessionSubsystem))
|
| {
|
| SessionSubsystem->OnUserRequestedSessionEvent.AddUObject(this, &UCommonGameInstance::OnUserRequestedSession);
|
| SessionSubsystem->OnDestroySessionRequestedEvent.AddUObject(this, &UCommonGameInstance::OnDestroySessionRequested);
|
| }
|
| }
|
|
|
| void UCommonGameInstance::ResetUserAndSessionState()
|
| {
|
| UCommonUserSubsystem* UserSubsystem = GetSubsystem<UCommonUserSubsystem>();
|
| if (ensure(UserSubsystem))
|
| {
|
| UserSubsystem->ResetUserState();
|
| }
|
|
|
| UCommonSessionSubsystem* SessionSubsystem = GetSubsystem<UCommonSessionSubsystem>();
|
| if (ensure(SessionSubsystem))
|
| {
|
| SessionSubsystem->CleanUpSessions();
|
| }
|
| }
|
|
|
| void UCommonGameInstance::ReturnToMainMenu()
|
| {
|
|
|
| ResetUserAndSessionState();
|
|
|
| Super::ReturnToMainMenu();
|
| }
|
|
|
| void UCommonGameInstance::OnUserRequestedSession(const FPlatformUserId& PlatformUserId, UCommonSession_SearchResult* InRequestedSession, const FOnlineResultInformation& RequestedSessionResult)
|
| {
|
| if (InRequestedSession)
|
| {
|
| SetRequestedSession(InRequestedSession);
|
| }
|
| else
|
| {
|
| HandleSystemMessage(FCommonUserTags::SystemMessage_Error, NSLOCTEXT("CommonGame", "Warning_RequestedSessionFailed", "Requested Session Failed"), RequestedSessionResult.ErrorText);
|
| }
|
| }
|
|
|
| void UCommonGameInstance::OnDestroySessionRequested(const FPlatformUserId& PlatformUserId, const FName& SessionName)
|
| {
|
|
|
|
|
| UE_LOG(LogCommonGame, Verbose, TEXT("[%hs] PlatformUserId:%d, SessionName: %s)"), __FUNCTION__, PlatformUserId.GetInternalId(), *SessionName.ToString());
|
|
|
| ReturnToMainMenu();
|
| }
|
|
|
| void UCommonGameInstance::SetRequestedSession(UCommonSession_SearchResult* InRequestedSession)
|
| {
|
| RequestedSession = InRequestedSession;
|
| if (RequestedSession)
|
| {
|
| if (CanJoinRequestedSession())
|
| {
|
| JoinRequestedSession();
|
| }
|
| else
|
| {
|
| ResetGameAndJoinRequestedSession();
|
| }
|
| }
|
| }
|
|
|
| bool UCommonGameInstance::CanJoinRequestedSession() const
|
| {
|
|
|
| return true;
|
| }
|
|
|
| void UCommonGameInstance::JoinRequestedSession()
|
| {
|
| if (RequestedSession)
|
| {
|
| if (ULocalPlayer* const FirstPlayer = GetFirstGamePlayer())
|
| {
|
| UCommonSessionSubsystem* SessionSubsystem = GetSubsystem<UCommonSessionSubsystem>();
|
| if (ensure(SessionSubsystem))
|
| {
|
|
|
| UCommonSession_SearchResult* LocalRequestedSession = RequestedSession;
|
| RequestedSession = nullptr;
|
| SessionSubsystem->JoinSession(FirstPlayer->PlayerController, LocalRequestedSession);
|
| }
|
| }
|
| }
|
| }
|
|
|
| void UCommonGameInstance::ResetGameAndJoinRequestedSession()
|
| {
|
|
|
| ReturnToMainMenu();
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|