From 033ca9d1bde5ef6cb7c38fd419a910629cc00eef Mon Sep 17 00:00:00 2001 From: cebgcontract <99630598+cebgcontract@users.noreply.github.com> Date: Wed, 13 Jul 2022 16:42:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E7=BB=99c#=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Classes/JcWallet.cpp | 25 ++++++++++++++++++++----- Classes/JcWallet.h | 1 + Classes/WalletEvent.cpp | 16 ++++++++++++++++ Classes/WalletEvent.h | 22 ++++++++++++++++++++++ app/jni/Android.mk | 1 + 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 Classes/WalletEvent.cpp create mode 100644 Classes/WalletEvent.h diff --git a/Classes/JcWallet.cpp b/Classes/JcWallet.cpp index 7854f2a..e131421 100644 --- a/Classes/JcWallet.cpp +++ b/Classes/JcWallet.cpp @@ -1,4 +1,5 @@ #include "JcWallet.h" +#include "WalletEvent.h" #include #include "cocos/scripting/js-bindings/jswrapper/SeApi.h" #include "cocos/scripting/js-bindings/manual/jsb_global.h" @@ -16,7 +17,6 @@ NS_CC_BEGIN cocos2d::Application *g_app = nullptr; JcWallet *JcWallet::_instance = nullptr; - static std::string result; bool _isStarted = false; JcWallet::JcWallet() { @@ -45,7 +45,10 @@ NS_CC_BEGIN char* JcWallet::initWallet() { se::Value value; jsb_run_code("jc.wallet.currentAccount().address", &value); - result = value.toString(); + static std::string result = value.toString(); + WalletEvent::Emit("wallet_inited", "{1}"); + WalletEvent::Emit("wallet_inited", "{2}"); + WalletEvent::Emit("wallet_inited", "{3}"); return const_cast(result.c_str()); } @@ -55,14 +58,14 @@ NS_CC_BEGIN std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')"; se::Value value; jsb_run_code(jsCode, &value); - result = value.toString(); + static std::string result = value.toString(); return const_cast(result.c_str()); } char* JcWallet::createAccount() { se::Value value; jsb_run_code("jc.wallet.createAccount()", &value); - result = value.toString(); + static std::string result = value.toString(); return const_cast(result.c_str()); } @@ -71,10 +74,19 @@ NS_CC_BEGIN std::string jsCode = "jc.wallet.importAccount('" + key + "')"; se::Value value; jsb_run_code(jsCode, &value); - result = value.toString(); + static std::string result = value.toString(); return const_cast(result.c_str()); } + char* JcWallet::runJsMethod(const char *methodName, const char *paramObj) { + std::string methodStr(methodName); + std::string paramsStr(paramObj); + std::string jsCode = "jc.wallet."+methodStr+"('" + paramsStr + "')"; + se::Value value; + jsb_run_code(jsCode, &value); + static std::string result = value.toString(); + return const_cast(result.c_str()); + } extern "C" { @@ -91,6 +103,9 @@ NS_CC_BEGIN char* importAccount(const char *privateKey) { return JcWallet::getInstance()->importAccount(privateKey); } + char* runWalletMethod(const char *methodName, const char *paramObj) { + return JcWallet::getInstance()->runJsMethod(methodName, paramObj); + } } NS_CC_END diff --git a/Classes/JcWallet.h b/Classes/JcWallet.h index 0fb03ff..3169503 100644 --- a/Classes/JcWallet.h +++ b/Classes/JcWallet.h @@ -11,6 +11,7 @@ NS_CC_BEGIN char* signLogin(const char *nonceChar, const char *tipChar); char* createAccount(); char* importAccount(const char *privateKey); + char* runJsMethod(const char *methodName, const char *paramObj); private: static JcWallet* _instance; }; diff --git a/Classes/WalletEvent.cpp b/Classes/WalletEvent.cpp new file mode 100644 index 0000000..659c591 --- /dev/null +++ b/Classes/WalletEvent.cpp @@ -0,0 +1,16 @@ +// +// WalletEvent.cpp +// Unity-iPhone +// +// Created by zhl on 2022/7/13. +// + +#include "WalletEvent.h" +// 定义函数指针,用来接受C#的委托 +void(*WalletEvent::Emit)(char* name, char* message); + +// 注册C#的委托 +void registWalletEventDelegate(void(*Emit)(char* name, char* message)) +{ + WalletEvent::Emit = Emit; +} \ No newline at end of file diff --git a/Classes/WalletEvent.h b/Classes/WalletEvent.h new file mode 100644 index 0000000..f9087e6 --- /dev/null +++ b/Classes/WalletEvent.h @@ -0,0 +1,22 @@ +// +// WalletEvent.h +// Unity-iPhone +// +// Created by zhl on 2022/7/13. +// + + +#pragma once +extern "C" +{ +class WalletEvent +{ +public: + static void (*Emit)(char* name, char* message); +}; + +// 注册C#的委托 +void registWalletEventDelegate(void (*Emit)(char* name, char* message)); + +} + diff --git a/app/jni/Android.mk b/app/jni/Android.mk index 189d1f7..c5ac969 100755 --- a/app/jni/Android.mk +++ b/app/jni/Android.mk @@ -13,6 +13,7 @@ endif LOCAL_SRC_FILES := hellojavascript/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/jsb_module_register.cpp \ + ../../Classes/WalletEvent.cpp \ ../../Classes/JcWallet.cpp \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes