增加一个回调给c#的方法

This commit is contained in:
cebgcontract 2022-07-13 16:42:20 +08:00
parent c691b25651
commit 033ca9d1bd
5 changed files with 60 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#include "JcWallet.h" #include "JcWallet.h"
#include "WalletEvent.h"
#include <string> #include <string>
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h" #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#include "cocos/scripting/js-bindings/manual/jsb_global.h" #include "cocos/scripting/js-bindings/manual/jsb_global.h"
@ -16,7 +17,6 @@ NS_CC_BEGIN
cocos2d::Application *g_app = nullptr; cocos2d::Application *g_app = nullptr;
JcWallet *JcWallet::_instance = nullptr; JcWallet *JcWallet::_instance = nullptr;
static std::string result;
bool _isStarted = false; bool _isStarted = false;
JcWallet::JcWallet() { JcWallet::JcWallet() {
@ -45,7 +45,10 @@ NS_CC_BEGIN
char* JcWallet::initWallet() { char* JcWallet::initWallet() {
se::Value value; se::Value value;
jsb_run_code("jc.wallet.currentAccount().address", &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<char*>(result.c_str()); return const_cast<char*>(result.c_str());
} }
@ -55,14 +58,14 @@ NS_CC_BEGIN
std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')"; std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')";
se::Value value; se::Value value;
jsb_run_code(jsCode, &value); jsb_run_code(jsCode, &value);
result = value.toString(); static std::string result = value.toString();
return const_cast<char*>(result.c_str()); return const_cast<char*>(result.c_str());
} }
char* JcWallet::createAccount() { char* JcWallet::createAccount() {
se::Value value; se::Value value;
jsb_run_code("jc.wallet.createAccount()", &value); jsb_run_code("jc.wallet.createAccount()", &value);
result = value.toString(); static std::string result = value.toString();
return const_cast<char*>(result.c_str()); return const_cast<char*>(result.c_str());
} }
@ -71,10 +74,19 @@ NS_CC_BEGIN
std::string jsCode = "jc.wallet.importAccount('" + key + "')"; std::string jsCode = "jc.wallet.importAccount('" + key + "')";
se::Value value; se::Value value;
jsb_run_code(jsCode, &value); jsb_run_code(jsCode, &value);
result = value.toString(); static std::string result = value.toString();
return const_cast<char*>(result.c_str()); return const_cast<char*>(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<char*>(result.c_str());
}
extern "C" extern "C"
{ {
@ -91,6 +103,9 @@ NS_CC_BEGIN
char* importAccount(const char *privateKey) { char* importAccount(const char *privateKey) {
return JcWallet::getInstance()->importAccount(privateKey); return JcWallet::getInstance()->importAccount(privateKey);
} }
char* runWalletMethod(const char *methodName, const char *paramObj) {
return JcWallet::getInstance()->runJsMethod(methodName, paramObj);
}
} }
NS_CC_END NS_CC_END

View File

@ -11,6 +11,7 @@ NS_CC_BEGIN
char* signLogin(const char *nonceChar, const char *tipChar); char* signLogin(const char *nonceChar, const char *tipChar);
char* createAccount(); char* createAccount();
char* importAccount(const char *privateKey); char* importAccount(const char *privateKey);
char* runJsMethod(const char *methodName, const char *paramObj);
private: private:
static JcWallet* _instance; static JcWallet* _instance;
}; };

16
Classes/WalletEvent.cpp Normal file
View File

@ -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;
}

22
Classes/WalletEvent.h Normal file
View File

@ -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));
}

View File

@ -13,6 +13,7 @@ endif
LOCAL_SRC_FILES := hellojavascript/main.cpp \ LOCAL_SRC_FILES := hellojavascript/main.cpp \
../../Classes/AppDelegate.cpp \ ../../Classes/AppDelegate.cpp \
../../Classes/jsb_module_register.cpp \ ../../Classes/jsb_module_register.cpp \
../../Classes/WalletEvent.cpp \
../../Classes/JcWallet.cpp \ ../../Classes/JcWallet.cpp \
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes