Add Online system base

This commit is contained in:
Wojciech Figat
2022-01-26 17:01:30 +01:00
parent 0cfb7e8cee
commit 33eb3d8540
5 changed files with 334 additions and 0 deletions

View File

@@ -37,6 +37,7 @@ public class Engine : EngineModule
options.PublicDependencies.Add("Utilities");
options.PublicDependencies.Add("Visject");
options.PublicDependencies.Add("Localization");
options.PublicDependencies.Add("Online");
// Use source folder per platform group
switch (options.Platform.Target)

View File

@@ -0,0 +1,237 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#pragma once
#include "Online.h"
#include "Engine/Core/Types/Span.h"
#include "Engine/Core/Types/String.h"
#include "Engine/Core/Types/DateTime.h"
/// <summary>
/// Online platform user presence common states.
/// </summary>
API_ENUM(Namespace="FlaxEngine.Online") enum class OnlinePresenceStates
{
/// <summary>
/// User is offline.
/// </summary>
Offline = 0,
/// <summary>
/// User is online.
/// </summary>
Online,
/// <summary>
/// User is online but busy.
/// </summary>
Busy,
/// <summary>
/// User is online but away (no activity for some time).
/// </summary>
Away,
};
/// <summary>
/// Online platform user description.
/// </summary>
API_STRUCT(Namespace="FlaxEngine.Online") struct FLAXENGINE_API OnlineUser
{
DECLARE_SCRIPTING_TYPE_MINIMAL(OnlineUser);
/// <summary>
/// Unique player identifier. Specific for a certain online platform.
/// </summary>
API_FIELD() Guid Id;
/// <summary>
/// The player name.
/// </summary>
API_FIELD() String Name;
/// <summary>
/// The current player presence state.
/// </summary>
API_FIELD() OnlinePresenceStates PresenceState;
};
/// <summary>
/// Online platform achievement description.
/// </summary>
API_STRUCT(Namespace="FlaxEngine.Online") struct FLAXENGINE_API OnlineAchievement
{
DECLARE_SCRIPTING_TYPE_MINIMAL(OnlineAchievement);
/// <summary>
/// Unique achievement identifier. Specific for a certain online platform.
/// </summary>
API_FIELD() String Identifier;
/// <summary>
/// Achievement name. Specific for a game.
/// </summary>
API_FIELD() String Name;
/// <summary>
/// The achievement title text.
/// </summary>
API_FIELD() String Title;
/// <summary>
/// The achievement description text.
/// </summary>
API_FIELD() String Description;
/// <summary>
/// True if achievement is hidden from user (eg. can see it once it's unlocked).
/// </summary>
API_FIELD() bool IsHidden = false;
/// <summary>
/// Achievement unlock percentage progress (normalized to 0-100 range).
/// </summary>
API_FIELD() float Progress = 0.0f;
/// <summary>
/// Date and time at which player unlocked the achievement.
/// </summary>
API_FIELD() DateTime UnlockTime = DateTime::MinValue();
};
/// <summary>
/// Interface for online platform providers for communicating with various multiplayer services such as player info, achievements, game lobby or in-game store.
/// </summary>
API_INTERFACE(Namespace="FlaxEngine.Online") class FLAXENGINE_API IOnlinePlatform
{
DECLARE_SCRIPTING_TYPE_MINIMAL(IOnlinePlatform);
/// <summary>
/// Finalizes an instance of the <see cref="IOnlinePlatform"/> class.
/// </summary>
virtual ~IOnlinePlatform() = default;
/// <summary>
/// Initializes the online platform services.
/// </summary>
/// <remarks>Called only by Online system.</remarks>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool Initialize() = 0;
/// <summary>
/// Shutdowns the online platform services.
/// </summary>
/// <remarks>Called only by Online system. Can be used to destroy the object.</remarks>
API_FUNCTION() virtual void Deinitialize() = 0;
public:
/// <summary>
/// Logins the local user into the online platform.
/// </summary>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool UserLogin(User* localUser = nullptr) = 0;
/// <summary>
/// Logout the local user from the online platform.
/// </summary>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool UserLogout(User* localUser = nullptr) = 0;
/// <summary>
/// Checks if the local user is logged in.
/// </summary>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if user is logged, otherwise false.</returns>
API_FUNCTION() virtual bool GetUserLoggedIn(User* localUser = nullptr) = 0;
/// <summary>
/// Gets the player from the online platform.
/// </summary>
/// <param name="user">The local player user info.</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool GetUser(API_PARAM(Out) OnlineUser& user, User* localUser = nullptr) = 0;
/// <summary>
/// Gets the list of friends of the user from the online platform.
/// </summary>
/// <param name="friends">The result local player friends user infos.</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool GetFriends(API_PARAM(Out) Array<OnlineUser, HeapAllocation>& friends, User* localUser = nullptr) = 0;
public:
/// <summary>
/// Gets the list of all achievements for this game.
/// </summary>
/// <param name="achievements">The result achievements list</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool GetAchievements(API_PARAM(Out) Array<OnlineAchievement, HeapAllocation>& achievements, User* localUser = nullptr) = 0;
/// <summary>
/// Unlocks the achievement.
/// </summary>
/// <param name="name">The achievement name. Specific for a game.</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool UnlockAchievement(const StringView& name, User* localUser = nullptr) = 0;
/// <summary>
/// Updates the achievement unlocking progress (in range 0-100).
/// </summary>
/// <param name="name">The achievement name. Specific for a game.</param>
/// <param name="progress">The achievement unlock progress (in range 0-100).</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool UnlockAchievementProgress(const StringView& name, float progress, User* localUser = nullptr) = 0;
#if !BUILD_RELEASE
/// <summary>
/// Resets the all achievements progress for this game.
/// </summary>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool ResetAchievements(User* localUser = nullptr) = 0;
#endif
public:
/// <summary>
/// Gets the online statistical value.
/// </summary>
/// <param name="name">The stat name.</param>
/// <param name="value">The result value.</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool GetStat(const StringView& name, API_PARAM(Out) float& value, User* localUser = nullptr) = 0;
/// <summary>
/// Sets the online statistical value.
/// </summary>
/// <param name="name">The stat name.</param>
/// <param name="value">The value.</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool SetStat(const StringView& name, float value, User* localUser = nullptr) = 0;
public:
/// <summary>
/// Gets the online savegame data. Returns empty if savegame slot is unused.
/// </summary>
/// <param name="name">The savegame slot name.</param>
/// <param name="data">The result data. Empty or null for unused slot name.</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool GetSaveGame(const StringView& name, API_PARAM(Out) Array<byte, HeapAllocation>& data, User* localUser = nullptr) = 0;
/// <summary>
/// Sets the online savegame data.
/// </summary>
/// <param name="name">The savegame slot name.</param>
/// <param name="data">The data. Empty or null to delete slot (or mark as unused).</param>
/// <param name="localUser">The local user (null if use default one).</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() virtual bool SetSaveGame(const StringView& name, const Span<byte>& data, User* localUser = nullptr) = 0;
};

View File

@@ -0,0 +1,10 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
using Flax.Build;
/// <summary>
/// Online services base module.
/// </summary>
public class Online : EngineModule
{
}

View File

@@ -0,0 +1,51 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#include "Online.h"
#include "IOnlinePlatform.h"
#include "Engine/Core/Log.h"
#include "Engine/Engine/EngineService.h"
#include "Engine/Scripting/ScriptingObject.h"
class OnlineService : public EngineService
{
public:
OnlineService()
: EngineService(TEXT("Online"), 100)
{
}
void Dispose() override
{
// Cleanup current online platform
Online::Initialize(nullptr);
}
};
IOnlinePlatform* Online::Platform = nullptr;
Action Online::PlatformChanged;
OnlineService OnlineServiceInstance;
bool Online::Initialize(IOnlinePlatform* platform)
{
if (Platform == platform)
return false;
const auto object = ScriptingObject::FromInterface(platform, IOnlinePlatform::TypeInitializer);
LOG(Info, "Changing online platform to {0}", object ? object->ToString() : (platform ? TEXT("?") : TEXT("none")));
if (Platform)
{
Platform->Deinitialize();
}
Platform = platform;
if (Platform)
{
if (Platform->Initialize())
{
Platform = nullptr;
LOG(Error, "Failed to initialize online platform.");
return true;
}
}
return false;
}

View File

@@ -0,0 +1,35 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Delegate.h"
#include "Engine/Scripting/ScriptingType.h"
class IOnlinePlatform;
/// <summary>
/// The online system for communicating with various multiplayer services such as player info, achievements, game lobby or in-game store.
/// </summary>
API_CLASS(Static, Namespace="FlaxEngine.Online") class FLAXENGINE_API Online
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(Online);
public:
/// <summary>
/// The current online platform.
/// </summary>
API_FIELD(ReadOnly) static IOnlinePlatform* Platform;
/// <summary>
/// Event called when current online platform gets changed.
/// </summary>
API_EVENT() static Action PlatformChanged;
public:
/// <summary>
/// Initializes the online system with a given online platform implementation.
/// </summary>
/// <remarks>Destroys the current platform (in any already in-use).</remarks>
/// <param name="platform">The online platform object.</param>
/// <returns>True if failed, otherwise false.</returns>
API_FUNCTION() static bool Initialize(IOnlinePlatform* platform);
};