增加一些新的方法

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 "scripting/js-bindings/event/EventDispatcher.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);
#endif
using namespace cocos2d;
NS_CC_BEGIN
cocos2d::Application *g_app = nullptr;
JcWallet *JcWallet::_instance = nullptr;
static std::string result;
bool _isStarted = false;
JcWallet::JcWallet() {
JcWallet::_instance = this;
}
void JcWallet::initEnv() {
if (!_isStarted) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
g_app = new AppDelegate(1, 1);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
g_app = cocos_android_app_init(1, 1);
se::ScriptEngine *se = se::ScriptEngine::getInstance();
#endif
EventDispatcher::init();
g_app->start();
_isStarted = true;
}
}
JcWallet::~JcWallet() {
@ -31,20 +43,36 @@ NS_CC_BEGIN
}
char* JcWallet::initWallet() {
se::Value rval;
jsb_run_code("jc.wallet.currentAccount().address", &rval);
char* result = const_cast<char*>(rval.toString().c_str());
return result;
se::Value value;
jsb_run_code("jc.wallet.currentAccount().address", &value);
result = value.toString();
return const_cast<char*>(result.c_str());
}
char* JcWallet::signLogin(const char *nonceChar, const char *tipChar) {
se::Value rval;
std::string nonce(nonceChar);
std::string tips(tipChar);
std::string jsCode = "jc.wallet.loginSign('" + nonce + "','" + tips + "')";
jsb_run_code(jsCode, &rval);
char* result = const_cast<char*>(rval.toString().c_str());
return result;
se::Value value;
jsb_run_code(jsCode, &value);
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) {
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

View File

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

View File

@ -5,6 +5,6 @@ ext{
// js钱包项目路径
jsFilePath: '/Users/zhl/Documents/workspace/android/HeadlessCocos',
// 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