Skip to content

Commit

Permalink
Problem: Function RestoreClient is not aysnc (fix #244, #255) (#256)
Browse files Browse the repository at this point in the history
* Problem: Function RestoreClient is not aysnc (fix #255)

* Update docs
  • Loading branch information
damoncro authored Feb 2, 2023
1 parent 38c2bea commit 4aa53c2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 63 deletions.
136 changes: 78 additions & 58 deletions Source/CronosPlayUnreal/Private/PlayCppSdkActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,15 @@ void APlayCppSdkActor::ConnectWalletConnect(FString description, FString url,
TArray<FString> icon_urls,
FString name, int64 chain_id,
EConnectionType connection_type) {
FString Jsondata;
bool IsRestored;
FString RestoreClientOutputMessage;
RestoreClient(Jsondata, IsRestored, RestoreClientOutputMessage);
if (IsRestored) {
// Setup Callback
bool IsSetupCallback;
FString SetupCallbackOutputMessage;
OnSetupCallbackDelegate.BindDynamic(
this, &APlayCppSdkActor::OnWalletconnectSessionInfo);
SetupCallback(OnSetupCallbackDelegate, IsSetupCallback,
SetupCallbackOutputMessage);
if (IsSetupCallback) {
// Ensure session
OnEnsureSessionDelegate.BindDynamic(
this, &APlayCppSdkActor::OnRestoreSession);
EnsureSession(OnEnsureSessionDelegate);
} else {
UE_LOG(LogTemp, Error, TEXT("Setup Callbacked failed: %s"),
*(SetupCallbackOutputMessage));
}
} else {
OnInitializeWalletConnectDelegate.BindDynamic(
this, &APlayCppSdkActor::OnInitializeWalletConnectFinished);

// set the connection type
_connection_type = connection_type;

InitializeWalletConnect(description, url, icon_urls, name, chain_id,
OnInitializeWalletConnectDelegate);
}
OnRestoreClientDelegate.BindDynamic(
this, &APlayCppSdkActor::OnRestoreClientFinished);
RestoreClient(OnRestoreClientDelegate);
_description = description;
_url = url;
_icon_urls = icon_urls;
_name = name;
_chain_id = chain_id;
_connection_type = connection_type;
}

void APlayCppSdkActor::InitializeWalletConnect(
Expand Down Expand Up @@ -263,6 +241,8 @@ void APlayCppSdkActor::OnInitializeWalletConnectFinished(bool succeed,
break;
}

UE_LOG(LogTemp, Display, TEXT("Setup Callbacked succeeded"));

// Ensure session
OnEnsureSessionDelegate.BindDynamic(
this, &APlayCppSdkActor::OnNewSession);
Expand All @@ -278,37 +258,72 @@ void APlayCppSdkActor::OnInitializeWalletConnectFinished(bool succeed,
}
}

// TODO Turn into AsyncTask
void APlayCppSdkActor::RestoreClient(FString &Jsondata, bool &success,
FString &output_message) {
try {
if (NULL != _coreClient) {
success = false;
output_message = TEXT("Client Already Exists");
return;
void APlayCppSdkActor::RestoreClient(FRestoreClientDelegate Out) {
AsyncTask(
ENamedThreads::AnyHiPriThreadNormalTask,
[this, Out]() {
bool success;
FString message;
try {
FString jsondata;
success = FFileHelper::LoadFileToString(
jsondata,
*(FPaths::ProjectSavedDir() + "sessioninfo.json"));
// if load file success
// if session file is not empty
if (success && !jsondata.IsEmpty()) {
std::string sessioninfostring = TCHAR_TO_UTF8(*jsondata);
Box<WalletconnectClient> tmpClient =
walletconnect_restore_client(sessioninfostring);
_coreClient = tmpClient.into_raw();
assert(_coreClient != NULL);

success = true;
} else {
success = false;
}

} catch (const rust::cxxbridge1::Error &e) {
success = false;
message =
FString::Printf(TEXT("PlayCppSdk RestoreClient Error: %s"),
UTF8_TO_TCHAR(e.what()));
}

AsyncTask(ENamedThreads::GameThread, [Out, success, message]() {
Out.ExecuteIfBound(success, message);
});
}

success = FFileHelper::LoadFileToString(
Jsondata, *(FPaths::ProjectSavedDir() + "sessioninfo.json"));
// if load file failed, return
if (!success)
return;
// if nothing in session file, return
if (Jsondata.IsEmpty())
return;
);
}

std::string sessioninfostring = TCHAR_TO_UTF8(*Jsondata);
Box<WalletconnectClient> tmpClient =
walletconnect_restore_client(sessioninfostring);
_coreClient = tmpClient.into_raw();
assert(_coreClient != NULL);
void APlayCppSdkActor::OnRestoreClientFinished(bool succeed, FString message) {
if (succeed) {
UE_LOG(LogTemp, Log, TEXT("RestoreClient succeeded"));

success = true;
} catch (const rust::cxxbridge1::Error &e) {
success = false;
output_message =
FString::Printf(TEXT("PlayCppSdk RestoreClient Error: %s"),
UTF8_TO_TCHAR(e.what()));
// Setup Callback
bool IsSetupCallback;
FString SetupCallbackOutputMessage;
OnSetupCallbackDelegate.BindDynamic(
this, &APlayCppSdkActor::OnWalletconnectSessionInfo);
SetupCallback(OnSetupCallbackDelegate, IsSetupCallback,
SetupCallbackOutputMessage);
if (IsSetupCallback) {
// Ensure session
OnEnsureSessionDelegate.BindDynamic(
this, &APlayCppSdkActor::OnRestoreSession);
EnsureSession(OnEnsureSessionDelegate);
} else {
UE_LOG(LogTemp, Error, TEXT("Setup Callbacked failed: %s"),
*(SetupCallbackOutputMessage));
}

} else {
OnInitializeWalletConnectDelegate.BindDynamic(
this, &APlayCppSdkActor::OnInitializeWalletConnectFinished);
InitializeWalletConnect(_description, _url, _icon_urls, _name, _chain_id,
OnInitializeWalletConnectDelegate);
}
}

Expand All @@ -317,11 +332,14 @@ void APlayCppSdkActor::EnsureSession(FEnsureSessionDelegate Out) {
AsyncTask(ENamedThreads::AnyHiPriThreadNormalTask, [this, Out]() {
FWalletConnectEnsureSessionResult output;
FString result;
UE_LOG(LogTemp, Display, TEXT("EnsureSession..."));
try {

WalletConnectEnsureSessionResult sessionresult;
if (_coreClient != NULL) {
UE_LOG(LogTemp, Display, TEXT("EnsureSession blocking..."));
sessionresult = _coreClient->ensure_session_blocking();
UE_LOG(LogTemp, Display, TEXT("EnsureSession done..."));
output.addresses.Empty();
for (int i = 0; i < sessionresult.addresses.size(); i++) {
FWalletConnectAddress newaddress;
Expand Down Expand Up @@ -364,6 +382,7 @@ void APlayCppSdkActor::OnRestoreSession(
}
UE_LOG(LogTemp, Log, TEXT("OnRestoreSession Chain id: %d"),
SessionResult.chain_id);
this->OnRestoreSessionReady.ExecuteIfBound(SessionResult, Result);
}

void APlayCppSdkActor::OnNewSession(
Expand All @@ -381,6 +400,7 @@ void APlayCppSdkActor::OnNewSession(
}
UE_LOG(LogTemp, Log, TEXT("OnNewSession Chain id: %d"),
SessionResult.chain_id);
this->OnNewSessionReady.ExecuteIfBound(SessionResult, Result);
}

void APlayCppSdkActor::ClearSession(bool &success) {
Expand Down
49 changes: 44 additions & 5 deletions Source/CronosPlayUnreal/Public/PlayCppSdkActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ DECLARE_DYNAMIC_DELEGATE_OneParam(FWalletconnectSignPersonalDelegate,
DECLARE_DYNAMIC_DELEGATE_TwoParams(FInitializeWalletConnectDelegate, bool,
Succeed, FString, message);

/// called when new session is created
DECLARE_DYNAMIC_DELEGATE_TwoParams(FOnNewSessionReady,
FWalletConnectEnsureSessionResult,
SessionResult, FString, Result);

/// restore wallet connect delegate
DECLARE_DYNAMIC_DELEGATE_TwoParams(FRestoreClientDelegate, bool, Succeed,
FString, message);

/// called when session is restored
DECLARE_DYNAMIC_DELEGATE_TwoParams(FOnRestoreSessionReady,
FWalletConnectEnsureSessionResult,
SessionResult, FString, Result);

/// wallet connect ensure session delegate
DECLARE_DYNAMIC_DELEGATE_TwoParams(FEnsureSessionDelegate,
FWalletConnectEnsureSessionResult,
Expand Down Expand Up @@ -200,6 +214,12 @@ class CRONOSPLAYUNREAL_API APlayCppSdkActor : public AActor {
// `EnsureSession`
FWalletConnectEnsureSessionResult _session_result;

/**
* RestoreClient delegate, called after calling
* `RestoreClient`
*/
FRestoreClientDelegate OnRestoreClientDelegate;

/**
* InitializeWalletConnect delegate, called after calling
* `InitializeWalletConnect`
Expand All @@ -211,6 +231,14 @@ class CRONOSPLAYUNREAL_API APlayCppSdkActor : public AActor {
*/
FEnsureSessionDelegate OnEnsureSessionDelegate;

/**
* Used for `ConnectWalletConnect`
*/
FString _description;
FString _url;
TArray<FString> _icon_urls;
FString _name;
int64 _chain_id;
EConnectionType _connection_type = EConnectionType::URI_STRING;

/**
Expand Down Expand Up @@ -336,11 +364,22 @@ class CRONOSPLAYUNREAL_API APlayCppSdkActor : public AActor {
UFUNCTION()
void OnNewSession(FWalletConnectEnsureSessionResult SessionResult,
FString Result);
/**
* On NewSessionReady delegate, called if session is created
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PlayCppSdk")
FOnNewSessionReady OnNewSessionReady;

UFUNCTION()
void OnRestoreSession(FWalletConnectEnsureSessionResult SessionResult,
FString Result);

/**
* On RestoreSessionReady delegate, called if session is restored
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PlayCppSdk")
FOnRestoreSessionReady OnRestoreSessionReady;

/**
* Clear Session
* @param success whether clearing session succeed or not
Expand Down Expand Up @@ -416,15 +455,15 @@ class CRONOSPLAYUNREAL_API APlayCppSdkActor : public AActor {

/**
* restore session information from string(json)
* @param jsondata session information string(json)
* @param success succeed or fail
* @param output_message error message
* @param Out RestoreClient callback
*/
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "RestoreClient", Keywords = "PlayCppSdk"),
Category = "PlayCppSdk")
void RestoreClient(FString &jsondata, bool &success,
FString &output_message);
void RestoreClient(FRestoreClientDelegate Out);

UFUNCTION()
void OnRestoreClientFinished(bool succeed, FString message);

/**
* sign general message
Expand Down

0 comments on commit 4aa53c2

Please sign in to comment.