mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-10-09 19:35:24 +00:00
[engine] [cocos2d-x] [jsb-adapter] 适配引擎 v2.4.12 版本
This commit is contained in:
389
cocos2d-x/cocos/platform/openharmony/napi/NapiHelper.cpp
Normal file
389
cocos2d-x/cocos/platform/openharmony/napi/NapiHelper.cpp
Normal file
@@ -0,0 +1,389 @@
|
||||
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
#include "platform/openharmony/napi/NapiHelper.h"
|
||||
#include "platform/openharmony/OpenHarmonyPlatform.h"
|
||||
#include "scripting/js-bindings/jswrapper/napi/HelperMacros.h"
|
||||
#include "cocos/platform/openharmony/FileUtils-openharmony.h"
|
||||
#include "scripting/js-bindings/jswrapper/SeApi.h"
|
||||
#include "ui/edit-box/EditBox-openharmony.h"
|
||||
#include "ui/webview/WebViewImpl-openharmony.h"
|
||||
#include "platform/openharmony/WorkerMessageQueue.h"
|
||||
|
||||
#define KEYCODE_BACK_OH 6
|
||||
|
||||
namespace cocos2d {
|
||||
const int32_t kMaxStringLen = 512;
|
||||
|
||||
// Must be the same as the value called by js
|
||||
enum ContextType {
|
||||
APP_LIFECYCLE = 0,
|
||||
JS_PAGE_LIFECYCLE,
|
||||
XCOMPONENT_CONTEXT,
|
||||
XCOMPONENT_REGISTER_LIFECYCLE_CALLBACK,
|
||||
NATIVE_RENDER_API,
|
||||
WORKER_INIT,
|
||||
ENGINE_UTILS,
|
||||
EDITBOX_UTILS,
|
||||
WEBVIEW_UTILS,
|
||||
DISPLAY_UTILS,
|
||||
UV_ASYNC_SEND,
|
||||
};
|
||||
NapiHelper::PostMessage2UIThreadCb NapiHelper::_postMsg2UIThreadCb;
|
||||
NapiHelper::PostSyncMessage2UIThreadCb NapiHelper::_postSyncMsg2UIThreadCb;
|
||||
|
||||
static bool js_set_PostMessage2UIThreadCallback(se::State& s) {
|
||||
const auto& args = s.args();
|
||||
size_t argc = args.size();
|
||||
CC_UNUSED bool ok = true;
|
||||
if (argc == 1) {
|
||||
do {
|
||||
if (args[0].isObject() && args[0].toObject()->isFunction()) {
|
||||
//se::Value jsThis(s.thisObject());
|
||||
se::Value jsFunc(args[0]);
|
||||
//jsThis.toObject()->attachObject(jsFunc.toObject());
|
||||
auto * thisObj = s.thisObject();
|
||||
NapiHelper::_postMsg2UIThreadCb = [=](const std::string larg0, const se::Value& larg1) -> void {
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
se::AutoHandleScope hs;
|
||||
CC_UNUSED bool ok = true;
|
||||
se::ValueArray args;
|
||||
args.resize(2);
|
||||
ok &= std_string_to_seval(larg0, &args[0]);
|
||||
CC_ASSERT(ok);
|
||||
args[1] = larg1;
|
||||
//ok &= nativevalue_to_se(larg1, args[1], nullptr /*ctx*/);
|
||||
se::Value rval;
|
||||
se::Object* funcObj = jsFunc.toObject();
|
||||
bool succeed = funcObj->call(args, thisObj, &rval);
|
||||
if (!succeed) {
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
}
|
||||
};
|
||||
}
|
||||
} while(false);
|
||||
return true;
|
||||
}
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
|
||||
return false;
|
||||
}
|
||||
SE_BIND_FUNC(js_set_PostMessage2UIThreadCallback);
|
||||
|
||||
static bool js_set_PostSyncMessage2UIThreadCallback(se::State& s) {
|
||||
const auto& args = s.args();
|
||||
size_t argc = args.size();
|
||||
CC_UNUSED bool ok = true;
|
||||
if (argc == 1) {
|
||||
do {
|
||||
if (args[0].isObject() && args[0].toObject()->isFunction()) {
|
||||
//se::Value jsThis(s.thisObject());
|
||||
se::Value jsFunc(args[0]);
|
||||
//jsThis.toObject()->attachObject(jsFunc.toObject());
|
||||
auto * thisObj = s.thisObject();
|
||||
NapiHelper::_postSyncMsg2UIThreadCb = [=](const std::string larg0, const se::Value& larg1, se::Value* res) -> void {
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
se::AutoHandleScope hs;
|
||||
CC_UNUSED bool ok = true;
|
||||
se::ValueArray args;
|
||||
args.resize(2);
|
||||
ok &= std_string_to_seval(larg0, &args[0]);
|
||||
CC_ASSERT(ok);
|
||||
args[1] = larg1;
|
||||
//ok &= nativevalue_to_se(larg1, args[1], nullptr /*ctx*/);
|
||||
//se::Value rval;
|
||||
se::Object* funcObj = jsFunc.toObject();
|
||||
bool succeed = funcObj->call(args, thisObj, res);
|
||||
if (!succeed) {
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
}
|
||||
};
|
||||
}
|
||||
} while(false);
|
||||
return true;
|
||||
}
|
||||
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
|
||||
return false;
|
||||
}
|
||||
SE_BIND_FUNC(js_set_PostSyncMessage2UIThreadCallback);
|
||||
|
||||
// NAPI Interface
|
||||
napi_value NapiHelper::getContext(napi_env env, napi_callback_info info) {
|
||||
napi_status status;
|
||||
napi_value exports;
|
||||
size_t argc = 1;
|
||||
napi_value args[1];
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
|
||||
|
||||
if (argc != 1) {
|
||||
napi_throw_type_error(env, NULL, "Wrong number of arguments");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_valuetype valuetype;
|
||||
status = napi_typeof(env, args[0], &valuetype);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
if (valuetype != napi_number) {
|
||||
napi_throw_type_error(env, NULL, "Wrong arguments");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int64_t value;
|
||||
NAPI_CALL(env, napi_get_value_int64(env, args[0], &value));
|
||||
NAPI_CALL(env, napi_create_object(env, &exports));
|
||||
switch (value) {
|
||||
case APP_LIFECYCLE: {
|
||||
// Register app lifecycle
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("onCreate", NapiHelper::napiOnCreate),
|
||||
DECLARE_NAPI_FUNCTION("onShow", NapiHelper::napiOnShow),
|
||||
DECLARE_NAPI_FUNCTION("onHide", NapiHelper::napiOnHide),
|
||||
DECLARE_NAPI_FUNCTION("onDestroy", NapiHelper::napiOnDestroy),
|
||||
DECLARE_NAPI_FUNCTION("onBackPress", NapiHelper::napiOnBackPress),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
}
|
||||
break;
|
||||
case JS_PAGE_LIFECYCLE: {
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("onPageShow", NapiHelper::napiOnPageShow),
|
||||
DECLARE_NAPI_FUNCTION("onPageHide", NapiHelper::napiOnPageHide),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
} break;
|
||||
case XCOMPONENT_REGISTER_LIFECYCLE_CALLBACK: {
|
||||
} break;
|
||||
case NATIVE_RENDER_API: {
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("nativeEngineInit", NapiHelper::napiNativeEngineInit),
|
||||
DECLARE_NAPI_FUNCTION("nativeEngineStart", NapiHelper::napiNativeEngineStart),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
} break;
|
||||
case WORKER_INIT: {
|
||||
se::ScriptEngine::setEnv(env);
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("workerInit", NapiHelper::napiWorkerInit),
|
||||
DECLARE_NAPI_FUNCTION("setPostMessageFunction", _SE(js_set_PostMessage2UIThreadCallback)),
|
||||
DECLARE_NAPI_FUNCTION("setPostSyncMessageFunction", _SE(js_set_PostSyncMessage2UIThreadCallback)),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
} break;
|
||||
case ENGINE_UTILS: {
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("resourceManagerInit", NapiHelper::napiResourceManagerInit),
|
||||
DECLARE_NAPI_FUNCTION("writablePathInit", NapiHelper::napiWritablePathInit),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
}break;
|
||||
case EDITBOX_UTILS: {
|
||||
std::vector<napi_property_descriptor> desc;
|
||||
OpenHarmonyEditBox::GetInterfaces(desc);
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, desc.size(), desc.data()));
|
||||
}break;
|
||||
case WEBVIEW_UTILS: {
|
||||
std::vector<napi_property_descriptor> desc;
|
||||
OpenHarmonyWebView::GetInterfaces(desc);
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, desc.size(), desc.data()));
|
||||
} break;
|
||||
case DISPLAY_UTILS: {
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("onDisplayChange", NapiHelper::napiOnDisplayChange),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
}
|
||||
case UV_ASYNC_SEND: {
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("send", NapiHelper::napiASend),
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
} break;
|
||||
default:
|
||||
LOGE("unknown type");
|
||||
}
|
||||
return exports;
|
||||
}
|
||||
|
||||
|
||||
// NAPI Interface
|
||||
bool NapiHelper::exportFunctions(napi_env env, napi_value exports) {
|
||||
napi_status status;
|
||||
// Application/SDK etc. Init
|
||||
// XComponent Init:
|
||||
napi_value exportInstance = nullptr;
|
||||
OH_NativeXComponent* nativeXComponent = nullptr;
|
||||
int32_t ret;
|
||||
|
||||
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
|
||||
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
|
||||
|
||||
status = napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance);
|
||||
if (status != napi_ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
status = napi_unwrap(env, exportInstance, reinterpret_cast<void**>(&nativeXComponent));
|
||||
if (status != napi_ok) {
|
||||
LOGE("napi_unwrap error %{public}d", status);
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize);
|
||||
if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
OpenHarmonyPlatform::getInstance()->setNativeXComponent(nativeXComponent);
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnCreate(napi_env env, napi_callback_info info) {
|
||||
// uv_loop_t* loop = nullptr;
|
||||
// NAPI_CALL(env, napi_get_uv_event_loop(env, &loop));
|
||||
// OpenHarmonyPlatform::getInstance()->onCreateNative(env, loop);
|
||||
LOGD("NapiHelper::napiOnCreate");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnShow(napi_env env, napi_callback_info info) {
|
||||
cocos2d::WorkerMessageData data{cocos2d::MessageType::WM_APP_SHOW, nullptr, nullptr};
|
||||
OpenHarmonyPlatform::getInstance()->enqueue(data);
|
||||
|
||||
Application* app = OpenHarmonyPlatform::getInstance()->g_app;
|
||||
if (app) {
|
||||
app->onResume();
|
||||
}
|
||||
LOGD("NapiHelper::napiOnShow");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnHide(napi_env env, napi_callback_info info) {
|
||||
cocos2d::WorkerMessageData data{cocos2d::MessageType::WM_APP_HIDE, nullptr, nullptr};
|
||||
OpenHarmonyPlatform::getInstance()->enqueue(data);
|
||||
|
||||
Application* app = OpenHarmonyPlatform::getInstance()->g_app;
|
||||
if (app) {
|
||||
app->onPause();
|
||||
}
|
||||
LOGD("NapiHelper::napiOnHide");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnBackPress(napi_env env, napi_callback_info info) {
|
||||
KeyboardEvent event;
|
||||
event.key = KEYCODE_BACK_OH;
|
||||
event.action = KeyboardEvent::Action::RELEASE;
|
||||
EventDispatcher::dispatchKeyboardEvent(event);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnDestroy(napi_env env, napi_callback_info info) {
|
||||
cocos2d::WorkerMessageData data{cocos2d::MessageType::WM_APP_DESTROY, nullptr, nullptr};
|
||||
OpenHarmonyPlatform::getInstance()->enqueue(data);
|
||||
LOGD("NapiHelper::napiOnDestroy");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnPageShow(napi_env env, napi_callback_info info) {
|
||||
LOGD("NapiHelper::napiOnPageShow");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnPageHide(napi_env env, napi_callback_info info) {
|
||||
LOGD("NapiHelper::napiOnPageHide");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiNativeEngineInit(napi_env env, napi_callback_info info) {
|
||||
LOGD("NapiHelper::napiNativeEngineInit");
|
||||
se::ScriptEngine::setEnv(env);
|
||||
OpenHarmonyPlatform::getInstance()->run(0, nullptr);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiNativeEngineStart(napi_env env, napi_callback_info info) {
|
||||
LOGD("NapiHelper::napiNativeEngineStart");
|
||||
OpenHarmonyPlatform::getInstance()->requestVSync();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiWorkerInit(napi_env env, napi_callback_info info) {
|
||||
uv_loop_t* loop = nullptr;
|
||||
NAPI_CALL(env, napi_get_uv_event_loop(env, &loop));
|
||||
OpenHarmonyPlatform::getInstance()->workerInit(env, loop);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiOnDisplayChange(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 1;
|
||||
napi_value args[1];
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
|
||||
CC_ASSERT(argc > 0);
|
||||
int32_t value;
|
||||
NAPI_CALL(env, napi_get_value_int32(env, args[0], &value));
|
||||
cocos2d::Device::Rotation rotation = cocos2d::Device::Rotation::_0;
|
||||
if(value == 0) {
|
||||
rotation = cocos2d::Device::Rotation::_0;
|
||||
} else if(value == 1) {
|
||||
// TODO(qgh): The openharmony platform is rotated clockwise.
|
||||
rotation = cocos2d::Device::Rotation::_270;
|
||||
} else if(value == 2) {
|
||||
rotation = cocos2d::Device::Rotation::_180;
|
||||
} else if(value == 3) {
|
||||
// TODO(qgh): The openharmony platform is rotated clockwise.
|
||||
rotation = cocos2d::Device::Rotation::_90;
|
||||
}
|
||||
EventDispatcher::dispatchOrientationChangeEvent((int)rotation);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
napi_value NapiHelper::napiResourceManagerInit(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 1;
|
||||
napi_value args[1];
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
|
||||
FileUtilsOpenHarmony::initResourceManager(env, args[0]);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiWritablePathInit(napi_env env, napi_callback_info info) {
|
||||
size_t argc = 1;
|
||||
napi_value args[1];
|
||||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
|
||||
|
||||
napi_status status;
|
||||
char buffer[kMaxStringLen];
|
||||
size_t result = 0;
|
||||
NODE_API_CALL(status, env, napi_get_value_string_utf8(env, args[0], buffer, kMaxStringLen, &result));
|
||||
FileUtilsOpenHarmony::_ohWritablePath = std::string(buffer);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value NapiHelper::napiASend(napi_env env, napi_callback_info info) {
|
||||
OpenHarmonyPlatform::getInstance()->triggerMessageSignal();
|
||||
return nullptr;
|
||||
}
|
||||
}
|
174
cocos2d-x/cocos/platform/openharmony/napi/NapiHelper.h
Normal file
174
cocos2d-x/cocos/platform/openharmony/napi/NapiHelper.h
Normal file
@@ -0,0 +1,174 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <napi/native_api.h>
|
||||
#include <uv.h>
|
||||
#include "scripting/js-bindings/jswrapper/SeApi.h"
|
||||
#include "scripting/js-bindings/manual/jsb_conversions.hpp"
|
||||
|
||||
namespace cocos2d {
|
||||
|
||||
class NapiHelper {
|
||||
public:
|
||||
|
||||
static napi_value getContext(napi_env env, napi_callback_info info);
|
||||
// APP Lifecycle
|
||||
static napi_value napiOnCreate(napi_env env, napi_callback_info info);
|
||||
static napi_value napiOnShow(napi_env env, napi_callback_info info);
|
||||
static napi_value napiOnHide(napi_env env, napi_callback_info info);
|
||||
static napi_value napiOnDestroy(napi_env env, napi_callback_info info);
|
||||
static napi_value napiOnBackPress(napi_env env, napi_callback_info info);
|
||||
|
||||
// JS Page : Lifecycle
|
||||
static napi_value napiOnPageShow(napi_env env, napi_callback_info info);
|
||||
static napi_value napiOnPageHide(napi_env env, napi_callback_info info);
|
||||
|
||||
// Worker Func
|
||||
static napi_value napiWorkerInit(napi_env env, napi_callback_info info);
|
||||
static napi_value napiASend(napi_env env, napi_callback_info info);
|
||||
static napi_value napiNativeEngineInit(napi_env env, napi_callback_info info);
|
||||
static napi_value napiNativeEngineStart(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value napiWritablePathInit(napi_env env, napi_callback_info info);
|
||||
static napi_value napiResourceManagerInit(napi_env env, napi_callback_info info);
|
||||
|
||||
static napi_value napiOnDisplayChange(napi_env env, napi_callback_info info);
|
||||
|
||||
template <class ReturnType>
|
||||
static napi_value napiCallFunction(const std::string& functionName, ReturnType* value) {
|
||||
if (!se::ScriptEngine::getInstance()->isValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
se::Value tickVal;
|
||||
se::AutoHandleScope scope;
|
||||
if (tickVal.isUndefined()) {
|
||||
se::ScriptEngine::getInstance()->getGlobalObject()->getProperty(functionName, &tickVal);
|
||||
}
|
||||
se::Value rval;
|
||||
se::ValueArray tickArgsValArr(1);
|
||||
if (!tickVal.isUndefined()) {
|
||||
tickVal.toObject()->call(tickArgsValArr, nullptr, &rval);
|
||||
}
|
||||
if(rval.isNullOrUndefined()) {
|
||||
return nullptr;
|
||||
}
|
||||
bool ok = true;
|
||||
ok &= seval_to_native_base_type(rval, value);
|
||||
SE_PRECONDITION2(ok, nullptr, "Error processing arguments");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value napiCallFunctionByStrArgs(const std::string& functionName, const std::string& args1, int32_t* value) {
|
||||
if (!se::ScriptEngine::getInstance()->isValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
se::Value tickVal;
|
||||
se::AutoHandleScope scope;
|
||||
if (tickVal.isUndefined()) {
|
||||
se::ScriptEngine::getInstance()->getGlobalObject()->getProperty(functionName, &tickVal);
|
||||
}
|
||||
se::Value rval;
|
||||
se::ValueArray tickArgsValArr;
|
||||
tickArgsValArr.push_back(se::Value(args1));
|
||||
if (!tickVal.isUndefined()) {
|
||||
tickVal.toObject()->call(tickArgsValArr, nullptr, &rval);
|
||||
}
|
||||
if(rval.isNullOrUndefined()) {
|
||||
return nullptr;
|
||||
}
|
||||
bool ok = true;
|
||||
ok &= seval_to_native_base_type(rval, value);
|
||||
SE_PRECONDITION2(ok, nullptr, "Error processing arguments");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value napiCallFunctionByFloatArgs(const std::string& functionName, float& sec, int32_t* value) {
|
||||
if (!se::ScriptEngine::getInstance()->isValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
se::Value tickVal;
|
||||
se::AutoHandleScope scope;
|
||||
if (tickVal.isUndefined()) {
|
||||
se::ScriptEngine::getInstance()->getGlobalObject()->getProperty(functionName, &tickVal);
|
||||
}
|
||||
se::Value rval;
|
||||
se::ValueArray tickArgsValArr;
|
||||
tickArgsValArr.push_back(se::Value(sec));
|
||||
if (!tickVal.isUndefined()) {
|
||||
tickVal.toObject()->call(tickArgsValArr, nullptr, &rval);
|
||||
}
|
||||
if(rval.isNullOrUndefined()) {
|
||||
return nullptr;
|
||||
}
|
||||
bool ok = true;
|
||||
ok &= seval_to_native_base_type(rval, value);
|
||||
SE_PRECONDITION2(ok, nullptr, "Error processing arguments");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static napi_value napiSetPostMessageFunction(napi_env env, napi_callback_info info);
|
||||
// Napi export
|
||||
static bool exportFunctions(napi_env env, napi_value exports);
|
||||
|
||||
static void postStringMessageToUIThread(const std::string& type, std::string param) {
|
||||
if (!_postMsg2UIThreadCb) {
|
||||
return;
|
||||
}
|
||||
CC_UNUSED bool ok = true;
|
||||
se::Value value;
|
||||
ok &= std_string_to_seval(param, &value);
|
||||
_postMsg2UIThreadCb(type, value);
|
||||
}
|
||||
|
||||
static void postIntMessageToUIThread(const std::string& type, int param) {
|
||||
if (!_postMsg2UIThreadCb) {
|
||||
return;
|
||||
}
|
||||
CC_UNUSED bool ok = true;
|
||||
se::Value value;
|
||||
ok &= native_int_to_se(param, value, nullptr /*ctx*/);
|
||||
_postMsg2UIThreadCb(type, value);
|
||||
}
|
||||
|
||||
static void postUnorderedMapMessageToUIThread(const std::string& type, std::unordered_map<std::string, cocos2d::Value> param) {
|
||||
if (!_postMsg2UIThreadCb) {
|
||||
return;
|
||||
}
|
||||
CC_UNUSED bool ok = true;
|
||||
se::Value value;
|
||||
ok &= native_unorderedmap_to_se(param, value, nullptr /*ctx*/);
|
||||
_postMsg2UIThreadCb(type, value);
|
||||
}
|
||||
|
||||
public:
|
||||
using PostMessage2UIThreadCb = std::function<void(const std::string&, const se::Value&)>;
|
||||
static PostMessage2UIThreadCb _postMsg2UIThreadCb;
|
||||
using PostSyncMessage2UIThreadCb = std::function<void(const std::string&, const se::Value&, se::Value*)>;
|
||||
static PostSyncMessage2UIThreadCb _postSyncMsg2UIThreadCb;
|
||||
};
|
||||
|
||||
}
|
63
cocos2d-x/cocos/platform/openharmony/napi/NapiInit.cpp
Normal file
63
cocos2d-x/cocos/platform/openharmony/napi/NapiInit.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
#include <ace/xcomponent/native_interface_xcomponent.h>
|
||||
#include "scripting/js-bindings/jswrapper/SeApi.h"
|
||||
#include "platform/openharmony/napi/NapiHelper.h"
|
||||
|
||||
const char kLibname[] = "cocos";
|
||||
/*
|
||||
* function for module exports
|
||||
*/
|
||||
static napi_value init(napi_env env, napi_value exports) {
|
||||
napi_property_descriptor desc[] = {
|
||||
DECLARE_NAPI_FUNCTION("getContext", cocos2d::NapiHelper::getContext),
|
||||
};
|
||||
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
bool ret = cocos2d::NapiHelper::exportFunctions(env, exports);
|
||||
if (!ret) {
|
||||
LOGE("Init failed");
|
||||
}
|
||||
return exports;
|
||||
}
|
||||
|
||||
/*
|
||||
* Napi Module define
|
||||
*/
|
||||
static napi_module cocos2dModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = init, // called by ACE XComponent
|
||||
.nm_modname = kLibname,
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = {0},
|
||||
};
|
||||
/*
|
||||
* Module register function
|
||||
*/
|
||||
extern "C" __attribute__((constructor)) void RegisterModule(void) {
|
||||
napi_module_register(&cocos2dModule);
|
||||
}
|
Reference in New Issue
Block a user