增加一些新的方法

This commit is contained in:
cebgcontract 2022-07-13 12:31:26 +08:00
parent 2a35e49694
commit c691b25651
4 changed files with 66 additions and 30 deletions

View File

@ -4,24 +4,36 @@
#include "cocos/scripting/js-bindings/manual/jsb_global.h" #include "cocos/scripting/js-bindings/manual/jsb_global.h"
#include "scripting/js-bindings/event/EventDispatcher.h" #include "scripting/js-bindings/event/EventDispatcher.h"
#include "platform/CCApplication.h" #include "platform/CCApplication.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#import "AppDelegate.h"
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::Application *cocos_android_app_init(int width, int height); cocos2d::Application *cocos_android_app_init(int width, int height);
#endif
using namespace cocos2d; using namespace cocos2d;
NS_CC_BEGIN 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;
JcWallet::JcWallet() { JcWallet::JcWallet() {
JcWallet::_instance = this; JcWallet::_instance = this;
} }
void JcWallet::initEnv() { void JcWallet::initEnv() {
g_app = cocos_android_app_init(1, 1); if (!_isStarted) {
se::ScriptEngine *se = se::ScriptEngine::getInstance(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
EventDispatcher::init(); g_app = new AppDelegate(1, 1);
g_app->start(); #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
g_app = cocos_android_app_init(1, 1);
#endif
EventDispatcher::init();
g_app->start();
_isStarted = true;
}
} }
JcWallet::~JcWallet() { JcWallet::~JcWallet() {
@ -31,20 +43,36 @@ NS_CC_BEGIN
} }
char* JcWallet::initWallet() { char* JcWallet::initWallet() {
se::Value rval; se::Value value;
jsb_run_code("jc.wallet.currentAccount().address", &rval); jsb_run_code("jc.wallet.currentAccount().address", &value);
char* result = const_cast<char*>(rval.toString().c_str()); result = value.toString();
return result; return const_cast<char*>(result.c_str());
} }
char* JcWallet::signLogin(const char *nonceChar, const char *tipChar) { char* JcWallet::signLogin(const char *nonceChar, const char *tipChar) {
se::Value rval;
std::string nonce(nonceChar); std::string nonce(nonceChar);
std::string tips(tipChar); std::string tips(tipChar);
std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')"; std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')";
jsb_run_code(jsCode, &rval); se::Value value;
char* result = const_cast<char*>(rval.toString().c_str()); jsb_run_code(jsCode, &value);
return result; result = value.toString();
return const_cast<char*>(result.c_str());
}
char* JcWallet::createAccount() {
se::Value value;
jsb_run_code("jc.wallet.createAccount()", &value);
result = value.toString();
return const_cast<char*>(result.c_str());
}
char* JcWallet::importAccount(const char *privateKey){
std::string key(privateKey);
std::string jsCode = "jc.wallet.importAccount('" + key + "')";
se::Value value;
jsb_run_code(jsCode, &value);
result = value.toString();
return const_cast<char*>(result.c_str());
} }
@ -57,6 +85,12 @@ NS_CC_BEGIN
char* signLogin(const char *nonceChar, const char *tipChar) { char* signLogin(const char *nonceChar, const char *tipChar) {
return JcWallet::getInstance()->signLogin(nonceChar, tipChar); return JcWallet::getInstance()->signLogin(nonceChar, tipChar);
} }
char* createAccount() {
return JcWallet::getInstance()->createAccount();
}
char* importAccount(const char *privateKey) {
return JcWallet::getInstance()->importAccount(privateKey);
}
} }
NS_CC_END NS_CC_END

View File

@ -1,16 +1,18 @@
#include "cocos2d.h" #include "cocos2d.h"
#include "base/ccMacros.h" #include "base/ccMacros.h"
NS_CC_BEGIN NS_CC_BEGIN
class CC_DLL JcWallet { class CC_DLL JcWallet {
public: public:
void initEnv(); void initEnv();
JcWallet(); JcWallet();
virtual ~JcWallet(); virtual ~JcWallet();
static JcWallet* getInstance() { return _instance; } static JcWallet* getInstance() { return _instance; }
char* initWallet(); char* initWallet();
char* signLogin(const char *nonceChar, const char *tipChar); char* signLogin(const char *nonceChar, const char *tipChar);
private: char* createAccount();
static JcWallet* _instance; char* importAccount(const char *privateKey);
}; private:
static JcWallet* _instance;
};
NS_CC_END NS_CC_END

View File

@ -5,6 +5,6 @@ ext{
// js钱包项目路径 // js钱包项目路径
jsFilePath: '/Users/zhl/Documents/workspace/android/HeadlessCocos', jsFilePath: '/Users/zhl/Documents/workspace/android/HeadlessCocos',
// unity export的android项目的根路径 // unity export的android项目的根路径
unityAndroidProject: '/Users/zhl/Documents/workspace/unity/first/android/first' unityAndroidProject: '/Users/zhl/Documents/workspace/unity/first/android/tebg'
] ]
} }

File diff suppressed because one or more lines are too long