diff --git a/native/cocos/audio/openharmony/PcmAudioService.cpp b/native/cocos/audio/openharmony/PcmAudioService.cpp index 3844eb717f1..817b7b7610f 100644 --- a/native/cocos/audio/openharmony/PcmAudioService.cpp +++ b/native/cocos/audio/openharmony/PcmAudioService.cpp @@ -149,4 +149,4 @@ void PcmAudioService::resume() { OH_AudioRenderer_Start(_audioRenderer); } } -} // namespace cocos2d \ No newline at end of file +} // namespace cc \ No newline at end of file diff --git a/native/cocos/bindings/jswrapper/jsvm/Object.cpp b/native/cocos/bindings/jswrapper/jsvm/Object.cpp index 02560a20aa1..b12ffb5ce9a 100644 --- a/native/cocos/bindings/jswrapper/jsvm/Object.cpp +++ b/native/cocos/bindings/jswrapper/jsvm/Object.cpp @@ -104,7 +104,7 @@ void Object::setPrivateObject(PrivateObjectBase* data) { auto tmpThis = _objRef.getValue(_env); JSVM_Ref result = nullptr; NODE_API_CALL(status, _env, - OH_JSVM_Wrap(_env, tmpThis, this, sendWeakCallback, + OH_JSVM_Wrap(_env, tmpThis, this, weakCallback, (void*)this /* finalize_hint */, &result)); //_objRef.setWeakref(_env, result); setProperty("__native_ptr__", se::Value(static_cast(reinterpret_cast(data)))); @@ -423,19 +423,8 @@ Object* Object::createTypedArrayWithBuffer(TypedArrayType type, const Object *ob Object* Object::createExternalArrayBufferObject(void* contents, size_t byteLength, BufferContentsFreeFunc freeFunc, void* freeUserData) { JSVM_Status status; JSVM_Value result; - if (freeFunc) { - struct ExternalArrayBufferCallbackParams* param = new (struct ExternalArrayBufferCallbackParams); - param->func = freeFunc; - param->contents = contents; - param->byteLength = byteLength; - param->userData = freeUserData; - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &contents, &result)); - param->func(param->contents, param->byteLength, param->userData); - delete param; - } else { - NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &contents, &result)); - } - + // JSVM does not support the napi_create_external_arraybuffer interface like in NAPI. + NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArrayBufferFromBackingStoreData(ScriptEngine::getEnv(), contents, byteLength, 0, byteLength, &result)); Object* obj = Object::_createJSObject(ScriptEngine::getEnv(), result, nullptr); return obj; } @@ -703,6 +692,7 @@ void Object::sendWeakCallback(JSVM_Env env, void* nativeObject, void* finalizeHi } void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint /*finalize_hint*/) { + if (finalizeHint) { if (nativeObject == nullptr) { return; @@ -710,9 +700,6 @@ void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint / void *rawPtr = reinterpret_cast(finalizeHint)->_privateData; Object* seObj = reinterpret_cast(finalizeHint); Object* rawPtrObj = reinterpret_cast(rawPtr); - if(rawPtrObj->getRefCount() == 0) { - return; - } if (seObj->_onCleaingPrivateData) { //called by cleanPrivateData, not release seObj; return; } @@ -726,11 +713,11 @@ void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint / } if (seObj->_finalizeCb != nullptr) { - seObj->_finalizeCb(env, rawPtr, rawPtr); + seObj->_finalizeCb(env, finalizeHint, finalizeHint); } else { assert(seObj->_getClass() != nullptr); if (seObj->_getClass()->_getFinalizeFunction() != nullptr) { - seObj->_getClass()->_getFinalizeFunction()(env, rawPtr, rawPtr); + seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint); } } seObj->decRef(); diff --git a/native/cocos/bindings/jswrapper/jsvm/Object.h b/native/cocos/bindings/jswrapper/jsvm/Object.h index c6238130d3b..4f83928025b 100644 --- a/native/cocos/bindings/jswrapper/jsvm/Object.h +++ b/native/cocos/bindings/jswrapper/jsvm/Object.h @@ -189,10 +189,10 @@ class Object : public RefCounter { /** - * @brief Delete a property of an object. - * @param[in] name A utf-8 string containing the property's name. - * @return true if the property is deleted successfully, otherwise false. - */ + * @brief Delete a property of an object. + * @param[in] name A utf-8 string containing the property's name. + * @return true if the property is deleted successfully, otherwise false. + */ bool deleteProperty(const char *name); /** diff --git a/native/cocos/bindings/jswrapper/napi/Object.cpp b/native/cocos/bindings/jswrapper/napi/Object.cpp index 732d1403570..2d50ccac80b 100644 --- a/native/cocos/bindings/jswrapper/napi/Object.cpp +++ b/native/cocos/bindings/jswrapper/napi/Object.cpp @@ -662,11 +662,11 @@ void Object::weakCallback(napi_env env, void* nativeObject, void* finalizeHint / } if (seObj->_finalizeCb != nullptr) { - seObj->_finalizeCb(env, rawPtr, rawPtr); + seObj->_finalizeCb(env, finalizeHint, finalizeHint); } else { assert(seObj->_getClass() != nullptr); if (seObj->_getClass()->_getFinalizeFunction() != nullptr) { - seObj->_getClass()->_getFinalizeFunction()(env, rawPtr, rawPtr); + seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint); } } seObj->decRef(); diff --git a/native/cocos/bindings/manual/jsb_global.cpp b/native/cocos/bindings/manual/jsb_global.cpp index 41685fb68eb..338ad1a72bd 100644 --- a/native/cocos/bindings/manual/jsb_global.cpp +++ b/native/cocos/bindings/manual/jsb_global.cpp @@ -979,7 +979,7 @@ static bool jsb_createExternalArrayBuffer(se::State &s) { // NOLINT SE_PRECONDITION2(ok, false, "Error processing arguments"); if (byteLength > 0) { // NOTE: Currently V8 use shared_ptr which has different abi on win64-debug and win64-release -#if CC_PLATFORM == CC_PLATFORM_WINDOWS && SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8 +#if (CC_PLATFORM == CC_PLATFORM_WINDOWS && SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8) || (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSVM) se::HandleObject arrayBuffer{se::Object::createArrayBufferObject(nullptr, byteLength)}; #else void *buffer = malloc(byteLength); diff --git a/native/cocos/platform/openharmony/modules/Screen.cpp b/native/cocos/platform/openharmony/modules/Screen.cpp index fb414522095..82f3846ce2d 100644 --- a/native/cocos/platform/openharmony/modules/Screen.cpp +++ b/native/cocos/platform/openharmony/modules/Screen.cpp @@ -56,11 +56,11 @@ Screen::Orientation Screen::getDeviceOrientation() const { if(value == 0) { return Orientation::PORTRAIT; } else if(value == 1) { - return Orientation::LANDSCAPE_RIGHT; + return Orientation::LANDSCAPE_LEFT; } else if(value == 2) { return Orientation::PORTRAIT_UPSIDE_DOWN; } else if(value == 3) { - return Orientation::LANDSCAPE_LEFT; + return Orientation::LANDSCAPE_RIGHT; } CC_ASSERT(false); return Orientation::PORTRAIT; diff --git a/native/cocos/platform/openharmony/napi/NapiHelper.cpp b/native/cocos/platform/openharmony/napi/NapiHelper.cpp index 8099b517b16..6304ab9f385 100644 --- a/native/cocos/platform/openharmony/napi/NapiHelper.cpp +++ b/native/cocos/platform/openharmony/napi/NapiHelper.cpp @@ -52,7 +52,6 @@ enum ContextType { ENGINE_UTILS, EDITBOX_UTILS, WEBVIEW_UTILS, - SYSTEM_UTILS, DISPLAY_UTILS, UV_ASYNC_SEND, VIDEO_UTILS @@ -226,13 +225,13 @@ static void napiOnDisplayChange(const Napi::CallbackInfo &info) { orientation = static_cast(cc::IScreen::Orientation::PORTRAIT); break; case 1: // ROTATION_90 - orientation = static_cast(cc::IScreen::Orientation::LANDSCAPE_RIGHT); + orientation = static_cast(cc::IScreen::Orientation::LANDSCAPE_LEFT); break; case 2: // ROTATION_180 orientation = static_cast(cc::IScreen::Orientation::PORTRAIT_UPSIDE_DOWN); break; case 3: // ROTATION_270 - orientation = static_cast(cc::IScreen::Orientation::LANDSCAPE_LEFT); + orientation = static_cast(cc::IScreen::Orientation::LANDSCAPE_RIGHT); break; } cc::events::Orientation::broadcast(orientation); diff --git a/platforms/native/builtin/index.js b/platforms/native/builtin/index.js index 81f991398af..46130a11b30 100644 --- a/platforms/native/builtin/index.js +++ b/platforms/native/builtin/index.js @@ -217,12 +217,6 @@ for (const key in jsbWindow) { } } -// In the openharmony platform, XMLHttpRequest is not undefined, but there are problems to using it. -// So the native implementation is forced to be used. -if (window.oh && typeof globalThis.XMLHttpRequest !== 'undefined') { - globalThis.XMLHttpRequest = jsbWindow.XMLHttpRequest; -} - if (typeof globalThis.window === 'undefined') { globalThis.window = globalThis; } diff --git a/scripts/native-pack-tool/source/platforms/harmonyos-next.ts b/scripts/native-pack-tool/source/platforms/harmonyos-next.ts index c9edd1e34be..7ce1005923d 100644 --- a/scripts/native-pack-tool/source/platforms/harmonyos-next.ts +++ b/scripts/native-pack-tool/source/platforms/harmonyos-next.ts @@ -71,10 +71,10 @@ export class HarmonyOSNextPackTool extends NativePackTool { moduleJSON.module.abilities[0].orientation = 'auto_rotation'; } else if (cfg.landscapeRight && !cfg.landscapeLeft) { - moduleJSON.module.abilities[0].orientation = 'landscape'; + moduleJSON.module.abilities[0].orientation = 'landscape_inverted'; } else if (!cfg.landscapeRight && cfg.landscapeLeft) { - moduleJSON.module.abilities[0].orientation = 'landscape_inverted'; + moduleJSON.module.abilities[0].orientation = 'landscape'; } else if (cfg.landscapeRight && cfg.landscapeLeft) { moduleJSON.module.abilities[0].orientation = 'auto_rotation_landscape'; diff --git a/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/oh-package.json5 b/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/oh-package.json5 index 20aaf4fb86d..7460650ce74 100644 --- a/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/oh-package.json5 +++ b/templates/harmonyos-next/entry/src/main/cpp/types/libcocos/oh-package.json5 @@ -1,6 +1,6 @@ { "name": "libcocos.so", "types": "./index.d.ts", - "version": "", + "version": "1.0.0", "description": "Please describe the basic information." } \ No newline at end of file diff --git a/templates/harmonyos-next/entry/src/main/ets/cocos/jsb-adapter/sys-ability-polyfill.js b/templates/harmonyos-next/entry/src/main/ets/cocos/jsb-adapter/sys-ability-polyfill.js deleted file mode 100644 index 641d0cd9a95..00000000000 --- a/templates/harmonyos-next/entry/src/main/ets/cocos/jsb-adapter/sys-ability-polyfill.js +++ /dev/null @@ -1,228 +0,0 @@ -/**************************************************************************** - 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. -****************************************************************************/ -import display from '@ohos.display'; -import I18n from '@ohos.i18n'; -import deviceInfo from '@ohos.deviceInfo'; -import batteryInfo from '@ohos.batteryInfo'; -import sensor from '@ohos.sensor'; -import connection from '@ohos.net.connection' -import vibrator from '@ohos.vibrator'; -import { ContextType } from "../common/Constants" -import cocos from "libcocos.so"; - -const systemUtils = cocos.getContext(ContextType.SYSTEM_UTILS); - -let cutout = { - left: 0, - top: 0, - width: 0, - height: 0 -}; - -export function systemReady () { - return new Promise(resolve => { - if (typeof XMLHttpRequest === 'undefined') { - window.XMLHttpRequest = function () {} - } - display.getDefaultDisplay((err, data) => { - window.oh.display = data; - // TODO: impl device in js. - //https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-display-0000001281001106 - }); - resolve(); - }) - -} - -window.getSystemLanguage = function () { - return I18n.System.getSystemLanguage(); -} - -window.getOSFullName = function () { - return deviceInfo.osFullName; -} - -window.getDeviceModel = function () { - return deviceInfo.productModel; -} - -window.getBatteryLevel = function () { - return batteryInfo.batterySOC; -} - -window.getDPI = function () { - var displayClass = display.getDefaultDisplaySync(); - return displayClass.densityDPI; -} - -window.getPixelRation = function () { - var displayClass = display.getDefaultDisplaySync(); - return displayClass.densityPixels; -} - -let onDisplayChange = (data) => { - // Monitor changes in screen orientation. - systemUtils.onDisplayChange(window.getDeviceOrientation()); - - // update screen cutout info - window.initScreenInfo(); -} - -try { - display.on("change", onDisplayChange); -} catch (exception) { - console.log('Failed to register callback. Code: ' + JSON.stringify(exception)); -} -window.getDeviceOrientation = function () { - var displayClass = display.getDefaultDisplaySync(); - return displayClass.rotation; -} - -function radiansToDegrees(radians) { - var pi = Math.PI; - return radians * (180/pi); -} - -let sDeviceMotionValues = []; -try { - sensor.on(sensor.SensorId.ACCELEROMETER, function (data) { - sDeviceMotionValues[0] = data.x; - sDeviceMotionValues[1] = data.y; - sDeviceMotionValues[2] = -data.z; - }, - { interval: 10000000000 } - ); -} catch (err) { - sDeviceMotionValues[0] = 0; - sDeviceMotionValues[1] = 0; - sDeviceMotionValues[2] = 0; -} - -try { - sensor.on(sensor.SensorId.LINEAR_ACCELEROMETER, function(data){ - sDeviceMotionValues[3] = data.x; - sDeviceMotionValues[4] = data.y; - sDeviceMotionValues[5] = data.z; - }, - {interval: 10000000000} - ); -} catch (err) { - sDeviceMotionValues[3] = 0; - sDeviceMotionValues[4] = 0; - sDeviceMotionValues[5] = 0; -} -try { - sensor.on(sensor.SensorId.GYROSCOPE, function(data){ - sDeviceMotionValues[6] = radiansToDegrees(data.x); - sDeviceMotionValues[7] = radiansToDegrees(data.y); - sDeviceMotionValues[8] = radiansToDegrees(data.z); - }, - {interval: 10000000000} - ); -} catch (err) { - sDeviceMotionValues[6] = 0; - sDeviceMotionValues[7] = 0; - sDeviceMotionValues[8] = 0; -} - -window.getDeviceMotionValue = function () { - return sDeviceMotionValues; -} - -window.getNetworkType = function () { - let netHandle = connection.getDefaultNetSync(); - if(netHandle && netHandle.netId != 0) { - let result = connection.getNetCapabilitiesSync(netHandle); - if (result && result.bearerTypes) { - return result.bearerTypes[0]; - } - } - return -1; -} - -window.vibrate = function (duration) { - console.log('begin to vibrate, duration is.' + duration); - try { - vibrator.startVibration({ - type: 'time', - duration: duration * 1000 - }, { - id: 0, - usage: 'alarm' - }, (error) => { - if (error) { - console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message); - return error.code; - } - console.log('Vibration start sucessful.'); - return 0; - }); - } catch (err) { - console.error('errCode: ' + err.code + ' ,msg: ' + err.message); - } -} - -window.initScreenInfo = function () { - display.getDefaultDisplaySync().getCutoutInfo().then((data) => { - if (data.boundingRects.length == 0) { - return; - } - - cutout.left = data.boundingRects[0].left; - cutout.top = data.boundingRects[0].top; - cutout.width = data.boundingRects[0].width; - cutout.height = data.boundingRects[0].height; - }).catch((err) => { - console.log("get cutout info error!"); - }); -}; -window.initScreenInfo(); - -window.getCutoutWidth = function () { - if(!cutout.width) { - return 0; - } - - let disPlayWidth = display.getDefaultDisplaySync().width; - if(cutout.left + cutout.width > disPlayWidth - cutout.left) { - return disPlayWidth - cutout.left; - } - return cutout.left + cutout.width; -} - -window.getCutoutHeight = function () { - if(!cutout.height) { - return 0; - } - - let orientation = globalThis.getDeviceOrientation(); - if (orientation == display.Orientation.PORTRAIT) { - return cutout.top + cutout.height; - } else if(orientation == display.Orientation.PORTRAIT_INVERTED) { - let displayHeight = display.getDefaultDisplaySync().height; - return displayHeight - cutout.top; - } - return 0; -} \ No newline at end of file diff --git a/templates/harmonyos-next/entry/src/main/ets/cocos/oh-adapter/sys-ability-polyfill.js b/templates/harmonyos-next/entry/src/main/ets/cocos/oh-adapter/sys-ability-polyfill.js index 4d252561406..74b33024e4b 100644 --- a/templates/harmonyos-next/entry/src/main/ets/cocos/oh-adapter/sys-ability-polyfill.js +++ b/templates/harmonyos-next/entry/src/main/ets/cocos/oh-adapter/sys-ability-polyfill.js @@ -182,11 +182,11 @@ globalThis.initScreenInfo = function () { if (data.boundingRects.length == 0) { return; } - const rc = data.boundingRects[0]; - cutout.left = rc.left; - cutout.top = rc.top; - cutout.width = rc.width; - cutout.height = rc.height; + + cutout.left = data.boundingRects[0].left; + cutout.top = data.boundingRects[0].top; + cutout.width = data.boundingRects[0].width; + cutout.height = data.boundingRects[0].height; }).catch((err) => { console.log("get cutout info error!"); }); diff --git a/templates/harmonyos-next/entry/src/main/ets/common/Constants.ts b/templates/harmonyos-next/entry/src/main/ets/common/Constants.ts index 6247ef4030a..11889767ab8 100644 --- a/templates/harmonyos-next/entry/src/main/ets/common/Constants.ts +++ b/templates/harmonyos-next/entry/src/main/ets/common/Constants.ts @@ -32,7 +32,6 @@ export enum ContextType { ENGINE_UTILS, EDITBOX_UTILS, WEBVIEW_UTILS, - SYSTEM_UTILS, DISPLAY_UTILS, UV_ASYNC_SEND, VIDEO_UTILS