Skip to content

Commit

Permalink
added GameInstanceSubsystem for checking DiscordCore duplicated objec…
Browse files Browse the repository at this point in the history
…ts and some insignificant delegates are removed and changed with multicast delegates
  • Loading branch information
Eletmetrix committed Dec 11, 2022
1 parent a7e6973 commit d897653
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 30 deletions.
99 changes: 84 additions & 15 deletions Source/DiscordGSDK/Private/DiscordCore.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,91 @@
// Copyright Epic Games, Inc. All Rights Reserved.

#include "DiscordCore.h"

#include "DiscordActivityManager.h"
#include "DiscordGameInstanceSubsystem.h"
#include "DiscordPluginSettings.h"
#include "DiscordRelationshipManager.h"
#include "DiscordUserManager.h"
#include "ThirdParty/DiscordGSDKLibrary/Include/core.h"

bool UDiscordCore::Create(int64 ClientID, bool bIsDiscordRequired)
UDiscordCore* UDiscordCore::Get(UWorld* World)
{
UE_LOG(LogDiscord, Log, TEXT("Trying to create Discord Core..."));

const discord::Result Result = discord::Core::Create(ClientID, bIsDiscordRequired ? DiscordCreateFlags_Default : DiscordCreateFlags_NoRequireDiscord, &Core);
if (World
&& World->GetGameInstance()
&& World->GetGameInstance()->GetSubsystem<UDiscordGameInstanceSubsystem>()
&& World->GetGameInstance()->GetSubsystem<UDiscordGameInstanceSubsystem>()->GetDiscordCore())
{
return World->GetGameInstance()->GetSubsystem<UDiscordGameInstanceSubsystem>()->GetDiscordCore();
}

return nullptr;
}

if (Result == discord::Result::Ok && Core != nullptr)
void UDiscordCore::BeginPlay()
{
if (GetOuter()
&& Cast<UGameInstance>(GetOuter())
&& Cast<UGameInstance>(GetOuter())->GetSubsystem<UDiscordGameInstanceSubsystem>()
&& Cast<UGameInstance>(GetOuter())->GetSubsystem<UDiscordGameInstanceSubsystem>()->GetDiscordCore()
&& Cast<UGameInstance>(GetOuter())->GetSubsystem<UDiscordGameInstanceSubsystem>()->GetDiscordCore() == this)
{
UE_LOG(LogDiscord, Log, TEXT("Discord Core was successfully created."));
auto LogHook = [=](discord::LogLevel inMinLevel, const char* inLogText)
if (ReconnectCount <= 0)
{
OnDiscordLogHook.Broadcast(static_cast<FDiscordLogLevel::Type>(inMinLevel), UTF8_TO_TCHAR(inLogText));
};
UE_LOG(LogDiscord, Log, TEXT("Trying to connect with Discord..."));
}

const UDiscordPluginSettings* DiscordPluginSettings = GetDefault<UDiscordPluginSettings>();
#if PLATFORM_DESKTOP
const discord::Result Result = discord::Core::Create(DiscordPluginSettings->ClientID, DiscordCreateFlags_Default, &Core);
#elif
const discord::Result Result = discord::Core::Create(DiscordPluginSettings->ClientID, DiscordCreateFlags_NoRequireDiscord, &Core);
#endif

if (Result == discord::Result::Ok && Core != nullptr)
{
UE_LOG(LogDiscord, Log, TEXT("Successfully connected with Discord."));
auto LogHook = [=](discord::LogLevel inMinLevel, const char* inLogText)
{
OnDiscordLogHook.Broadcast(static_cast<FDiscordLogLevel::Type>(inMinLevel), UTF8_TO_TCHAR(inLogText));
};

Core->SetLogHook(discord::LogLevel::Debug, LogHook);
Core->SetLogHook(static_cast<discord::LogLevel>(DiscordPluginSettings->LogVerbosity.GetValue()), LogHook);

InitializeInterfaces();
InitializeInterfaces();

Core->ActivityManager().RegisterSteam(DiscordPluginSettings->SteamAppID);
}
else
{
// This should be bad. Try to reconnect with Discord if we haven't exceeded out limit.
if (ReconnectCount < DiscordPluginSettings->ReconnectLimit)
{
ReconnectCount++;

FTimerHandle UnusedHandle;
GetWorld()->GetTimerManager().SetTimer(UnusedHandle, this, &UDiscordCore::BeginPlay, 1, false);
}
else
{
UE_LOG(LogDiscord, Error, TEXT("Failed to connect with Discord."));
}
}
}
else
{
UE_LOG(LogDiscord, Error, TEXT("Failed to create Discord Core."));
// We don't want to another DiscordCore object
MarkAsGarbage();
}
}

void UDiscordCore::PostInitProperties()
{
UObject::PostInitProperties();

if (GetWorld())
{
BeginPlay();
}

return Result == discord::Result::Ok;
}

void UDiscordCore::BeginDestroy()
Expand All @@ -44,6 +99,20 @@ void UDiscordCore::BeginDestroy()
}
}

UWorld* UDiscordCore::GetWorld() const
{
// Return pointer to World from object owner, if we don’t work in editor
if (GIsEditor && !GIsPlayInEditorWorld)
{
return nullptr;
}
if (GetOuter())
{
return GetOuter()->GetWorld();
}
return nullptr;
}

void UDiscordCore::Tick(float DeltaTime)
{
Core->RunCallbacks();
Expand Down
6 changes: 3 additions & 3 deletions Source/DiscordGSDK/Private/DiscordUserManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool UDiscordUserManager::GetCurrentUser(FDiscordUser& User)
return false;
}

void UDiscordUserManager::GetUser(int64 UserID, FDiscordGetUserResult Callback)
void UDiscordUserManager::GetUser(int64 UserID)
{
if (GetCore())
{
Expand All @@ -58,12 +58,12 @@ void UDiscordUserManager::GetUser(int64 UserID, FDiscordGetUserResult Callback)
inUser.Discriminator = User.GetDiscriminator();
inUser.Avatar = User.GetAvatar();

Callback.ExecuteIfBound(true, inUser);
OnGetUserResult.Broadcast(true, inUser);
}
else
{
LOG_ERROR(Result);
Callback.ExecuteIfBound(false, FDiscordUser());
OnGetUserResult.Broadcast(false, FDiscordUser());
}
};

Expand Down
22 changes: 17 additions & 5 deletions Source/DiscordGSDK/Public/DiscordCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ class DISCORDGSDK_API UDiscordCore : public UObject, public FTickableGameObject
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Discord|Core")
bool Create(int64 ClientID, bool bIsDiscordRequired);
static UDiscordCore* Get(UWorld* World);

// Begin overrides
virtual void BeginPlay();
virtual void PostInitProperties() override;
virtual void BeginDestroy() override;
virtual UWorld* GetWorld() const override;
// End overrides

// Begin Tick overrides
virtual void Tick(float DeltaTime) override;
virtual bool IsTickable() const override;
virtual TStatId GetStatId() const override;
// End Tick overrides

// Begin mutators
discord::Core* GetCore() const { return Core; }

UFUNCTION(BlueprintCallable, Category = "Discord|Core|Interfaces")
Expand All @@ -47,21 +55,25 @@ class DISCORDGSDK_API UDiscordCore : public UObject, public FTickableGameObject
UDiscordActivityManager* GetDiscordActivityManager() const { return DiscordActivityManager; }
UFUNCTION(BlueprintCallable, Category = "Discord|Core|Interfaces")
UDiscordRelationshipManager* GetDiscordRelationshipManager() const { return DiscordRelationshipManager; }
// End mutators

UPROPERTY(BlueprintAssignable, Category = "Discord|Core")
FDiscordLogHook OnDiscordLogHook;

private:
private: // Variables
// @TODO: Discord Interfaces
UPROPERTY()
UDiscordUserManager* DiscordUserManager = nullptr;
UPROPERTY()
UDiscordActivityManager* DiscordActivityManager = nullptr;
UDiscordActivityManager* DiscordActivityManager = nullptr;
UPROPERTY()
UDiscordRelationshipManager* DiscordRelationshipManager = nullptr;

void InitializeInterfaces();

discord::Core* Core = nullptr;

int ReconnectCount = 0;

private: // Private functions
void InitializeInterfaces();
};

17 changes: 12 additions & 5 deletions Source/DiscordGSDK/Public/DiscordTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ namespace discord
class Core;
}

UENUM(BlueprintType)
enum class DISCORD_OUT_EXEC : uint8
{
OnSuccess,
OnFail
};

UENUM(BlueprintType)
namespace FDiscordLogLevel
{
enum Type
{
DDL_None = 0,
DDL_Error = 1,
DDL_Warn,
DDL_Info,
DDL_Debug
DDL_None = 0 UMETA(DisplayName = "None"),
DDL_Error = 1 UMETA(DisplayName = "Error"),
DDL_Warn UMETA(DisplayName = "Warning"),
DDL_Info UMETA(DisplayName = "Info"),
DDL_Debug UMETA(DisplayName = "Debug")
};
}

Expand Down
6 changes: 4 additions & 2 deletions Source/DiscordGSDK/Public/DiscordUserManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace discord
}

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDiscordCurrentUserUpdate);
DECLARE_DYNAMIC_DELEGATE_TwoParams(FDiscordGetUserResult, bool, bResult, const FDiscordUser&, User);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FDiscordGetUserResult, bool, bResult, const FDiscordUser&, User);

/**
*
Expand All @@ -30,12 +30,14 @@ class DISCORDGSDK_API UDiscordUserManager : public UObject, public IDiscordInter
UFUNCTION(BlueprintCallable, Category = "Discord|User")
bool GetCurrentUser(FDiscordUser& User);
UFUNCTION(BlueprintCallable, Category = "Discord|User")
void GetUser(int64 UserID, FDiscordGetUserResult Callback);
void GetUser(int64 UserID);
UFUNCTION(BlueprintCallable, Category = "Discord|User")
bool GetCurrentUserPremiumType(TEnumAsByte<FDiscordPremiumType::Type>& PremiumType);
UFUNCTION(BlueprintCallable, Category = "Discord|User")
bool CurrentUserHasFlag(const TEnumAsByte<FDiscordUserFlag::Type>& Flag);

UPROPERTY(BlueprintAssignable, Category = "Discord|User")
FDiscordCurrentUserUpdate OnCurrentUserUpdate;
UPROPERTY(BlueprintAssignable, Category = "Discord|User")
FDiscordGetUserResult OnGetUserResult;
};

0 comments on commit d897653

Please sign in to comment.