diff --git a/Source/CronosPlayUnreal/Private/PlayCppSdkActor.cpp b/Source/CronosPlayUnreal/Private/PlayCppSdkActor.cpp index 740321a3..67dd2091 100644 --- a/Source/CronosPlayUnreal/Private/PlayCppSdkActor.cpp +++ b/Source/CronosPlayUnreal/Private/PlayCppSdkActor.cpp @@ -104,37 +104,15 @@ void APlayCppSdkActor::ConnectWalletConnect(FString description, FString url, TArray 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( @@ -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); @@ -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 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 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); } } @@ -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; @@ -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( @@ -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) { diff --git a/Source/CronosPlayUnreal/Public/PlayCppSdkActor.h b/Source/CronosPlayUnreal/Public/PlayCppSdkActor.h index 74fd125d..f38cfa10 100644 --- a/Source/CronosPlayUnreal/Public/PlayCppSdkActor.h +++ b/Source/CronosPlayUnreal/Public/PlayCppSdkActor.h @@ -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, @@ -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` @@ -211,6 +231,14 @@ class CRONOSPLAYUNREAL_API APlayCppSdkActor : public AActor { */ FEnsureSessionDelegate OnEnsureSessionDelegate; + /** + * Used for `ConnectWalletConnect` + */ + FString _description; + FString _url; + TArray _icon_urls; + FString _name; + int64 _chain_id; EConnectionType _connection_type = EConnectionType::URI_STRING; /** @@ -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 @@ -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