ios-unity/Classes_cocos/JcWallet.mm

332 lines
11 KiB
Plaintext

#include "JcWallet.h"
#include "WalletEvent.h"
#include <atomic>
#include <iostream>
#include "stdarg.h"
#include <string>
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
#include "scripting/js-bindings/event/EventDispatcher.h"
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
#include "platform/CCApplication.h"
#include "base/CCScheduler.h"
#include "scrypt/native-crypto.h"
#include "rustwallet.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#include "AppDelegate.h"
#import "UIViewController+Wallet.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;
bool _isStarted = false;
uv_async_t gasync = {0};
struct AsyncTaskData {
std::mutex mtx;
std::list<std::function<void()> > tasks;
};
// run in server thread loop
void flush_tasks_in_server_loop_cb(uv_async_t *asyn) {
AsyncTaskData *data = (AsyncTaskData *) asyn->data;
std::lock_guard<std::mutex> guard(data->mtx);
while (!data->tasks.empty()) {
// fetch task, run task
data->tasks.front()();
// drop task
data->tasks.pop_front();
}
}
void init_libuv_async_handle(uv_async_t *async) {
memset(async, 0, sizeof(uv_async_t));
uv_loop_t *loop = uv_default_loop();
uv_async_init(loop, async, cocos2d::flush_tasks_in_server_loop_cb);
async->data = new AsyncTaskData();
uv_run(loop, UV_RUN_DEFAULT);
}
// run in game thread, dispatch runnable object into server loop
int schedule_task_into_server_thread_task_queue(uv_async_t *asyn, std::function<void()> func) {
AsyncTaskData *data = (AsyncTaskData *) asyn->data;
{
std::lock_guard<std::mutex> guard(data->mtx);
data->tasks.emplace_back(func);
}
//notify server thread to invoke `flush_tasks_in_server_loop_cb()`
return uv_async_send(asyn);
}
#define RUN_IN_SERVERTHREAD(task) do { \
schedule_task_into_server_thread_task_queue(&gasync, [=](){ \
task; \
}); \
} while(0)
bool runGlobalMethod(const char *name, se::ValueArray args, se::Value *value) {
se::AutoHandleScope scope;
bool ok = false;
auto global = se::ScriptEngine::getInstance()->getGlobalObject();
se::Value func;
if (global->getProperty(name, &func) && func.isObject()) {
ok = func.toObject()->call(args, global, value);
}
return ok;
}
bool addToArgArray(se::ValueArray *args, const char *valChar) {
std::string strVal(valChar);
se::Value tmpVal;
bool ok = true;
ok &= std_string_to_seval(strVal, &tmpVal);
args->push_back(tmpVal);
return ok;
}
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);
#endif
EventDispatcher::init();
g_app->start();
_isStarted = true;
WalletEvent::Emit("wallet_init_event", "{}");
cocos2d::init_libuv_async_handle(&gasync);
}
}
void JcWallet::initJSThread(std::shared_ptr<JcWallet> wallet) {
wallet->initEnv();
}
void JcWallet::tick(float dt) {
g_app->getScheduler()->update(dt);
EventDispatcher::dispatchTickEvent(0);
}
JcWallet::~JcWallet() {
EventDispatcher::destroy();
se::ScriptEngine::destroyInstance();
uv_loop_close(uv_default_loop());
JcWallet::_instance = nullptr;
}
char *JcWallet::runJsMethod(std::shared_ptr<JSMethodParam> data) {
cocos2d::log("thread: %ld call method %s", uv_thread_self(), data->methodName.c_str());
se::Value value;
bool ok = cocos2d::runGlobalMethod(data->methodName.c_str(), data->args, &value);
static std::string result;
if (value.isString()) {
if (ok && !value.isNullOrUndefined()) {
result = value.toString();
} else {
result = "";
}
cocos2d::log("method: %s ::result: %s", data->methodName.c_str(), result.c_str());
WalletEvent::Emit(data->funId.c_str(), result.c_str());
}
return const_cast<char *>(result.c_str());
}
extern "C" {
void initEnv() {
if (!_isStarted) {
std::shared_ptr<JcWallet> wallet(JcWallet::getInstance());
std::thread([=]() {
JcWallet::initJSThread(wallet);
}).detach();
}
}
void tick(float dt) {
if (_isStarted) {
schedule_task_into_server_thread_task_queue(&gasync, [=](){
JcWallet::getInstance()->tick(dt);
});
}
}
int runWalletMethod(const char *funId, const char *methodName, int paramCount, char **paramList) {
JSMethodParam *data = new JSMethodParam();
std::string methodNameStr(methodName);
data->methodName = methodNameStr;
data->funId = funId;
data->paramCount = paramCount;
addToArgArray(&data->args, funId);
for (int i = 0; i < paramCount; i++) {
char *arg = *(paramList + i);
addToArgArray(&data->args, arg);
}
std::shared_ptr<JSMethodParam> params(data);
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
JcWallet::getInstance()->runJsMethod(params);
});
// RUN_IN_SERVERTHREAD(JcWallet::getInstance()->runJsMethod(params));
return result == 0 ? 1 : 0;
}
}
int nativeCallBack(const char *funId, const char *methodName, const char *cparams) {
JSMethodParam *data = new JSMethodParam();
std::string methodNameStr(methodName);
data->methodName = methodNameStr;
data->funId = funId;
data->paramCount = 1;
addToArgArray(&data->args, funId);
addToArgArray(&data->args, cparams);
std::shared_ptr<JSMethodParam> params(data);
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
JcWallet::getInstance()->runJsMethod(params);
});
return result == 0 ? 1 : 0;
}
NS_CC_END
static bool getOrCreatePlainObject_r(const char* name, se::Object* parent, se::Object** outObj)
{
assert(parent != nullptr);
assert(outObj != nullptr);
se::Value tmp;
if (parent->getProperty(name, &tmp) && tmp.isObject())
{
*outObj = tmp.toObject();
(*outObj)->incRef();
}
else
{
*outObj = se::Object::createPlainObject();
parent->setProperty(name, se::Value(*outObj));
}
return true;
}
bool jsb_wallet_callback(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
if (argc >= 2) {
bool ok;
std::string funId;
ok = seval_to_std_string(args[0], &funId);
SE_PRECONDITION2(ok, false, "Error processing arguments");
std::string msg;
ok = seval_to_std_string(args[1], &msg);
SE_PRECONDITION2(ok, false, "Error processing arguments");
WalletEvent::Emit(funId.c_str(), msg.c_str());
return true;
}
return false;
}
SE_BIND_FUNC(jsb_wallet_callback)
bool jsb_scanQRCode(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
if (argc >= 2) {
bool ok;
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "Error processing arguments");
std::string title;
ok = seval_to_std_string(args[1], &title);
SE_PRECONDITION2(ok, false, "Error processing arguments");
NSString *ntitle = [NSString stringWithCString:title.c_str() encoding:NSUTF8StringEncoding];
NSString *nfunid = [NSString stringWithCString:funid.c_str() encoding:NSUTF8StringEncoding];
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
[window.rootViewController scanQRCode:nfunid title:ntitle];
return true;
}
return false;
}
SE_BIND_FUNC(jsb_scanQRCode)
bool JSB_restoreKey(se::State& s)
{
const auto& args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 1) {
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
std::string oid;
ok = seval_to_std_string(args[1], &oid);
SE_PRECONDITION2(ok, false, "oid is invalid!");
NSString *nfunid = [NSString stringWithCString:funid.c_str() encoding:NSUTF8StringEncoding];
NSString *noid = [NSString stringWithCString:oid.c_str() encoding:NSUTF8StringEncoding];
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
[window.rootViewController loadRestoreKey:nfunid oid: noid];
return true;
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 2);
return false;
}
SE_BIND_FUNC(JSB_restoreKey)
bool jsb_signWithGoogle(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
if (argc >= 1) {
bool ok;
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "Error processing arguments");
NSString *nfunid = [NSString stringWithCString:funid.c_str() encoding:NSUTF8StringEncoding];
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
[window.rootViewController signWithGoogle:nfunid];
return true;
}
return false;
}
SE_BIND_FUNC(jsb_signWithGoogle)
bool jsb_signOutGoogle(se::State& s) {
const auto& args = s.args();
size_t argc = args.size();
if (argc >= 1) {
bool ok;
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "Error processing arguments");
NSString *nfunid = [NSString stringWithCString:funid.c_str() encoding:NSUTF8StringEncoding];
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
[window.rootViewController signOutGoogle:nfunid];
return true;
}
return false;
}
SE_BIND_FUNC(jsb_signOutGoogle)
bool jsb_register_walletevent_modules(se::Object* global) {
getOrCreatePlainObject_r("jsb", global, &__jsbObj);
__jsbObj->defineFunction("jcCallback", _SE(jsb_wallet_callback));
__jsbObj->defineFunction("scanQRCode", _SE(jsb_scanQRCode));
__jsbObj->defineFunction("restoreKey", _SE(JSB_restoreKey));
__jsbObj->defineFunction("signWithGoogle", _SE(jsb_signWithGoogle));
__jsbObj->defineFunction("signOutGoogle", _SE(jsb_signOutGoogle));
return true;
}