#pragma once #include "CoreMinimal.h" #include "Online/OnlineServices.h" #include "Online/OnlineAsyncOpHandle.h" #include "Online/TitleFile.h" #include "Subsystems/GameInstanceSubsystem.h" #include "OnlineSampleOnlineSubsystem.generated.h" DECLARE_LOG_CATEGORY_EXTERN(LogOnlineSampleOnlineSubsystem, Log, All); /** * */ UCLASS() class ONLINESAMPLE_API UOnlineSampleOnlineSubsystem : public UGameInstanceSubsystem { GENERATED_BODY() public: //////////////////////////////////////////////////////// /// OnlineSampleOnlineSubsystem Init/Deinit /** Called to determine whether the Subsystem should be created */ virtual bool ShouldCreateSubsystem(UObject* Outer) const override; /** Called to initialize Game Instance Subsystem */ virtual void Initialize(FSubsystemCollectionBase& Collection) override; /** Called to deinitialize Game Instance Subsystem */ virtual void Deinitialize() override; /** Called to register local online user with the Game Instance Subsystem */ void RegisterLocalOnlineUser(FPlatformUserId PlatformUserId); /** Called to retrieve online user info for this platform user id */ TObjectPtr<UOnlineUserInfo> GetOnlineUserInfo(FPlatformUserId PlatformUserId); /** Called to read the Game's Title File from the backend services and return the contents */ FString ReadTitleFile(FString Filename, FPlatformUserId PlatformUserId); protected: struct FOnlineServicesInfo { /** Online Services Pointer - Access Interfaces through this pointer */ UE::Online::IOnlineServicesPtr OnlineServices = nullptr; /** Interface pointers */ UE::Online::IAuthPtr AuthInterface = nullptr; UE::Online::ITitleFilePtr TitleFileInterface = nullptr; /** Online Services Implementation */ UE::Online::EOnlineServices OnlineServicesType = UE::Online::EOnlineServices::None; /** Title File content */ UE::Online::FTitleFileContents TitleFileContent; /** Reset struct to initial settings */ void Reset() { OnlineServices.Reset(); AuthInterface.Reset(); TitleFileInterface.Reset(); OnlineServicesType = UE::Online::EOnlineServices::None; } }; //////////////////////////////////////////////////////// /// Online Services Init /** Pointer to an internal struct containing relevant online services pointers */ FOnlineServicesInfo* OnlineServicesInfoInternal = nullptr; /** Called to initialize online services and interface pointers */ void InitializeOnlineServices(); //////////////////////////////////////////////////////// /// Title File /** Called to retrieve title file from online services */ void RetrieveTitleFile(FString Filename, FPlatformUserId PlatformUserId); //////////////////////////////////////////////////////// /// Events /** Called to handle the EnumerateFiles async event */ void HandleEnumerateFiles(const UE::Online::TOnlineResult<UE::Online::FTitleFileEnumerateFiles>& EnumerateFilesResult, TObjectPtr<UOnlineUserInfo> OnlineUser, FString Filename); /** Called to handle the ReadFile async event */ void HandleReadFile(const UE::Online::TOnlineResult<UE::Online::FTitleFileReadFile>& ReadFileResult, FString Filename); //////////////////////////////////////////////////////// /// Online User Information /** Called to create a UOnlineUserInfo object for this user */ TObjectPtr<UOnlineUserInfo> CreateOnlineUserInfo(int32 LocalUserIndex, FPlatformUserId PlatformUserId, UE::Online::FAccountId AccountId, UE::Online::EOnlineServices Services); /** Called to register the user with the OnlineUserInfos map and add user after creation with CreateOnlineUserInfo */ TObjectPtr<UOnlineUserInfo> CreateAndRegisterUserInfo(int32 LocalUserIndex, FPlatformUserId PlatformUserId, UE::Online::FAccountId AccountId, UE::Online::EOnlineServices Services); /** Information about each local user */ TMap<FPlatformUserId, TObjectPtr<UOnlineUserInfo>> OnlineUserInfos; /** Friend the UOnlineUserInfo class to access it */ friend UOnlineUserInfo; }; UCLASS() class ONLINESAMPLE_API UOnlineUserInfo : public UObject { GENERATED_BODY() public: UOnlineUserInfo(); //////////////////////////////////////////////////////// /// Online User Fields int32 LocalUserIndex = -1; FPlatformUserId PlatformUserId; UE::Online::FAccountId AccountId; UE::Online::EOnlineServices Services = UE::Online::EOnlineServices::None; //////////////////////////////////////////////////////// /// Online User Logging/Debugging Functions /** Called to obtain OnlineUserInfo as a string */ const FString DebugInfoToString(); friend UOnlineSampleOnlineSubsystem; };