Skip to content

Commit

Permalink
feat: added application configuration utilities (#151)
Browse files Browse the repository at this point in the history
* feat: added utilities to simplify managing immutable configurations

* chore: added missing commets
  • Loading branch information
YermekG authored and ImmutableJeffrey committed Nov 26, 2024
1 parent 1d589a8 commit 0812272
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/Immutable/Immutable.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public Immutable(ReadOnlyTargetRules Target) : base(Target)
"Json",
"UMG",
"Projects",
"DeveloperSettings",
}
);

Expand Down
31 changes: 31 additions & 0 deletions Source/Immutable/Private/Immutable/ImmutablePassport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Immutable/ImtblJSConnector.h"
#include "JsonObjectConverter.h"
#include "Immutable/ImmutableSaveGame.h"
#include "Immutable/ImmutableUtilities.h"
#include "Kismet/GameplayStatics.h"
#include "Policies/CondensedJsonPrintPolicy.h"

Expand Down Expand Up @@ -45,6 +46,36 @@ void UImmutablePassport::Initialize(const FImmutablePassportInitData& Data, cons
CallJS(ImmutablePassportAction::INIT, InitData.ToJsonString(), ResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnInitializeResponse), false);
}

void UImmutablePassport::Initialize(const FImtblPassportResponseDelegate& ResponseDelegate)
{
check(JSConnector.IsValid());

UApplicationConfig* ApplicationConfig = FImmutableUtilities::GetDefaultApplicationConfig();

if (!ApplicationConfig)
{
ResponseDelegate.ExecuteIfBound(FImmutablePassportResult{false, "Failed to retrieve default application configuration for Passport initialization"});

return;
}

InitData.clientId = ApplicationConfig->GetClientID();
InitData.environment = ApplicationConfig->GetEnvironment();
InitData.redirectUri = ApplicationConfig->GetRedirectURL();
InitData.logoutRedirectUri = ApplicationConfig->GetLogoutURL();

LoadPassportSettings();
// we check saved settings in case if player has not logged out properly
if (InitData.logoutRedirectUri.IsEmpty() && IsStateFlagsSet(IPS_PKCE))
{
IMTBL_ERR("Logout URI is empty. Previously logged in via PKCE.")
ResetStateFlags(IPS_PKCE);
SavePassportSettings();
}

CallJS(ImmutablePassportAction::INIT, InitData.ToJsonString(), ResponseDelegate, FImtblJSResponseDelegate::CreateUObject(this, &UImmutablePassport::OnInitializeResponse), false);
}

void UImmutablePassport::Connect(bool IsConnectImx, bool TryToRelogin, const FImtblPassportResponseDelegate& ResponseDelegate)
{
SetStateFlags(IPS_CONNECTING);
Expand Down
13 changes: 13 additions & 0 deletions Source/Immutable/Private/Immutable/ImmutableUtilities.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Immutable/ImmutableUtilities.h"

#include "Immutable/ImmutablePluginSettings.h"
#include "Immutable/Misc/ImtblLogging.h"
#include "Interfaces/IPluginManager.h"
#include "Misc/FileHelper.h"
Expand All @@ -19,3 +20,15 @@ bool FImmutableUtilities::LoadGameBridge(FString& GameBridge)

return false;
}

UApplicationConfig* FImmutableUtilities::GetDefaultApplicationConfig()
{
auto Settings = GetDefault<UImmutablePluginSettings>();

if (!Settings)
{
return nullptr;
}

return Settings->DefaultApplicationConfig.GetDefaultObject();
}
162 changes: 162 additions & 0 deletions Source/Immutable/Public/Immutable/ApplicationConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#pragma once

#include "ApplicationConfig.generated.h"

/**
* @class UApplicationConfig
* @brief Configuration settings for Passport and various APIs.
* @details This class stores configuration settings such as URLs, chain names, contract addresses,
* client IDs, and environment settings for the zkEVM API, Orderbook API, and Passport.
*/
UCLASS(Abstract, Blueprintable, ClassGroup = Immutable)
class UApplicationConfig : public UObject
{
GENERATED_BODY()

public:
/**
* Retrieves URL for the zkEVM API.
*
* @return A constant reference to an FString containing the name of the chain.
*/
const FString& GetzkEVMAPIURL()
{
return zkEVMAPIURL;
}

/**
* Retrieves the name of the chain used to pass to the zkEVM API.
*
* @return A constant reference to an FString containing the name of the chain.
*/
const FString& GetzkEVMAPIChainName()
{
return zkEVMAPIChainName;
}

/**
* Retrieves URL for the Orderbook API.
*
* @return A constant reference to an FString containing the name of the chain.
*/
const FString& GetOrderbookAPIURL()
{
return OrederbookAPIURL;
}

/**
* Retrieves the name of the chain used to pass to the Orderbook API.
*
* @return A constant reference to an FString containing the name of the chain.
*/
const FString& GetOrderbookAPIChainName()
{
return OrderbookAPIChainName;
}

/**
* @brief Retrieves the cryptocurrency contract address associated with the user's wallet balance.
*
* @return A string representing the contract address.
*/
const FString& GetTokenBalanceContractAddress()
{
return TokenBalanceContractAddress;
}

/**
* Retrieves the list of NFT contracts used in the APIs' queries.
*
* @return A constant reference to an array of strings representing the contracts.
*/
const TArray<FString>& GetNFTContractAddresses()
{
return NFTContractAddress;
}

/**
* Retrieves the Client ID used for Passport initialization.
*
* @return A constant reference to an FString containing the Client ID.
*/
const FString& GetClientID()
{
return ClientID;
}

/**
* Retrieves the environment configuration used for Passport initialization.
*
* @return A constant reference to an FString representing the environment.
*/
const FString& GetEnvironment()
{
return Environment;
}

/**
* Retrieves the URL where the browser will redirect after successful authentication.
* @note This is only used for Android, iOS, and macOS.
*
* @return A constant reference to an FString containing the redirect URL.
*/
const FString& GetRedirectURL()
{
return RedirectURL;
}

/**
* Retrieves the URL used for logging out.
*
* @return A constant reference to an FString containing the logout URL.
*/
const FString& GetLogoutURL()
{
return LogoutURL;
}

protected:
/** The URL for the zkEVM API. */
UPROPERTY(EditDefaultsOnly, Category = "zkEVM API")
FString zkEVMAPIURL;

/** The name of the API chain used by the zkEVM API. */
UPROPERTY(EditDefaultsOnly, Category = "zkEVM API")
FString zkEVMAPIChainName;

/** The URL for the Orderbook API. */
UPROPERTY(EditDefaultsOnly, Category = "Orderbook API")
FString OrederbookAPIURL;

/** The name of the API chain used by Orderbook API. */
UPROPERTY(EditDefaultsOnly, Category = "Orderbook API")
FString OrderbookAPIChainName;

/** The address of the cryptocurrency contract in the blockchain. */
UPROPERTY(EditDefaultsOnly, Category = "Contracts")
FString TokenBalanceContractAddress;

/** An array of NFT contract addresses used for searching NFTs in the marketplace or displaying them in the player's inventory. */
UPROPERTY(EditDefaultsOnly, Category = "Contracts")
TArray<FString> NFTContractAddress;

/** Passport Client ID. */
UPROPERTY(EditDefaultsOnly, Category = "Passport")
FString ClientID;

/** Environment used to initialize passport. Ex. sandbox or production */
UPROPERTY(EditDefaultsOnly, Category = "Passport")
FString Environment;

/**
* (Android, iOS, and macOS only)
* The URL where the browser will redirect after successful authentication.
*/
UPROPERTY(EditDefaultsOnly, Category = "Passport")
FString RedirectURL;

/** The URL where the browser will redirect after logout is complete. */
UPROPERTY(EditDefaultsOnly, Category = "Passport")
FString LogoutURL;

};
9 changes: 9 additions & 0 deletions Source/Immutable/Public/Immutable/ImmutablePassport.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ class IMMUTABLE_API UImmutablePassport : public UObject
*/
void Initialize(const FImmutablePassportInitData& InitData, const FImtblPassportResponseDelegate& ResponseDelegate);

/**
* Initialises passport. This sets up the Passport instance, configures the web browser, and waits for the ready signal.
* @details The functionality is the same with Initilize above except it obtains Passport initilization data from ApplicationConfig asset
* @see UApplicationConfig
*
* @param ResponseDelegate Callback delegate.
*/
void Initialize(const FImtblPassportResponseDelegate& ResponseDelegate);

/**
* Logs the user into Passport via device code auth and sets up the Immutable X provider.
*
Expand Down
25 changes: 25 additions & 0 deletions Source/Immutable/Public/Immutable/ImmutablePluginSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "Engine/DeveloperSettings.h"
#include "ApplicationConfig.h"

#include "ImmutablePluginSettings.generated.h"


/**
* ImmutablePluginSettings is a configuration class for the Immutable plugin.
* This class contains settings that can be adjusted to control the behavior
* of the Immutable plugin within the Unreal Engine environment.
*/
UCLASS(config = Game, defaultconfig, meta = (DisplayName = "Immutable Plugin Settings"))
class IMMUTABLE_API UImmutablePluginSettings : public UDeveloperSettings
{
GENERATED_BODY()

public:
/// The default application configuration class.
/// This property holds a reference to a subclass of UApplicationConfig,
/// which will be used as the default configuration for the application.
UPROPERTY(Config, EditAnywhere, BlueprintReadOnly, Category = "General")
TSubclassOf<UApplicationConfig> DefaultApplicationConfig;
};
3 changes: 3 additions & 0 deletions Source/Immutable/Public/Immutable/ImmutableUtilities.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "ApplicationConfig.h"


/** A wrapper struct around various Immutable namespace utility and support methods. */
Expand All @@ -12,4 +13,6 @@ struct IMMUTABLE_API FImmutableUtilities
* @return True if the game bridge content was sucessfully retrieved. Otherwise, false.
*/
static bool LoadGameBridge(FString& GameBridge);

static UApplicationConfig* GetDefaultApplicationConfig();
};

0 comments on commit 0812272

Please sign in to comment.