Files
AdvancedSessionsPlugin/AdvancedSessions/Source/AdvancedSessions/Private/LoginUserCallbackProxy.cpp
Lothar May 31c4be3629 Update UniqueId in login callback. See also ShowLoginUICallbackProxy.cpp in UE sources.
(cherry picked from commit 826bef7b861e8c20013033b57dd45cd8fec304e5)
2022-03-05 13:47:47 +01:00

98 lines
2.4 KiB
C++

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "LoginUserCallbackProxy.h"
//////////////////////////////////////////////////////////////////////////
// ULoginUserCallbackProxy
ULoginUserCallbackProxy::ULoginUserCallbackProxy(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
, Delegate(FOnLoginCompleteDelegate::CreateUObject(this, &ThisClass::OnCompleted))
{
}
ULoginUserCallbackProxy* ULoginUserCallbackProxy::LoginUser(UObject* WorldContextObject, class APlayerController* PlayerController, FString UserID, FString UserToken, FString Type)
{
ULoginUserCallbackProxy* Proxy = NewObject<ULoginUserCallbackProxy>();
Proxy->PlayerControllerWeakPtr = PlayerController;
Proxy->UserID = UserID;
Proxy->UserToken = UserToken;
Proxy->Type = Type;
Proxy->WorldContextObject = WorldContextObject;
return Proxy;
}
void ULoginUserCallbackProxy::Activate()
{
if (!PlayerControllerWeakPtr.IsValid())
{
OnFailure.Broadcast();
return;
}
ULocalPlayer* Player = Cast<ULocalPlayer>(PlayerControllerWeakPtr->Player);
if (!Player)
{
OnFailure.Broadcast();
return;
}
auto Identity = Online::GetIdentityInterface();
if (Identity.IsValid())
{
// Fallback to default AuthType if nothing is specified
if (Type.IsEmpty())
{
Type = Identity->GetAuthType();
}
DelegateHandle = Identity->AddOnLoginCompleteDelegate_Handle(Player->GetControllerId(), Delegate);
FOnlineAccountCredentials AccountCreds(Type, UserID, UserToken);
Identity->Login(Player->GetControllerId(), AccountCreds);
return;
}
// Fail immediately
OnFailure.Broadcast();
}
void ULoginUserCallbackProxy::OnCompleted(int32 LocalUserNum, bool bWasSuccessful, const FUniqueNetId& UserId, const FString& ErrorVal)
{
if (PlayerControllerWeakPtr.IsValid())
{
ULocalPlayer* Player = Cast<ULocalPlayer>(PlayerControllerWeakPtr->Player);
auto uniqueId = UserId.AsShared();
if (Player)
{
auto Identity = Online::GetIdentityInterface();
if (Identity.IsValid())
{
Identity->ClearOnLoginCompleteDelegate_Handle(Player->GetControllerId(), DelegateHandle);
}
Player->SetCachedUniqueNetId(uniqueId);
}
APlayerState* State = PlayerControllerWeakPtr->PlayerState;
if (State)
{
// Update UniqueId. See also ShowLoginUICallbackProxy.cpp
State->SetUniqueId(uniqueId);
}
}
if (bWasSuccessful)
{
OnSuccess.Broadcast();
}
else
{
OnFailure.Broadcast();
}
}