Skip to content

Commit 2d58d3c

Browse files
authored
Cocos2d x 2.1.4 oh (#20893)
* 2.14 version OHOS adaptation Signed-off-by: 冰冰冰 <[email protected]> * Review and revise based on feedback Signed-off-by: 冰冰冰 <[email protected]> * Review and revise based on feedback Signed-off-by: 冰冰冰 <[email protected]> * Review and revise based on feedback Signed-off-by: 冰冰冰 <[email protected]> * ohos code optimization Signed-off-by: 冰冰冰 <[email protected]> * Change the ts file to ets; API15 upgrade; Napi call return value exception scenario handling; Napi Interactive Use Scope to Prevent Memory Leakage Signed-off-by: 冰冰冰 <[email protected]> * mainability optimization Signed-off-by: 冰冰冰 <[email protected]> --------- Signed-off-by: 冰冰冰 <[email protected]>
1 parent 7bc13d7 commit 2d58d3c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+620
-510
lines changed

cocos2dx/platform/ohos/napi/helper/NapiHelper.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ class JSFunction {
114114
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
115115
napi_value return_val;
116116
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);
117-
117+
if (status == napi_pending_exception) {
118+
LOGI("Caught invoke exception: napi_pending_exception");
119+
napi_value exception;
120+
napi_get_and_clear_last_exception(env, &exception);
121+
}
118122
ReturnType value;
119123
if (!NapiValueConverter::ToCppValue(env, return_val, value)) {
120124
// Handle error here
@@ -136,6 +140,11 @@ class JSFunction {
136140
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
137141
napi_value return_val;
138142
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);
143+
if (status == napi_pending_exception) {
144+
LOGI("Caught invoke exception: napi_pending_exception");
145+
napi_value exception;
146+
napi_get_and_clear_last_exception(env, &exception);
147+
}
139148
}
140149

141150
static void callFunctionWithParams(WorkParam *param) {
@@ -167,6 +176,11 @@ class JSFunction {
167176
}
168177
if (status != napi_ok) {
169178
LOGI("XXXXXX:napi_call_function getClassObject != napi_ok %{public}d", status);
179+
if (status == napi_pending_exception) {
180+
LOGI("Caught invoke exception: napi_pending_exception");
181+
napi_value exception;
182+
napi_get_and_clear_last_exception(env, &exception);
183+
}
170184
}
171185

172186
napi_value thenFunc = nullptr;
@@ -187,6 +201,11 @@ class JSFunction {
187201
status = napi_call_function(env, promise, thenFunc, 1, &successFunc, &ret);
188202
if (status != napi_ok) {
189203
LOGI("XXXXXX:napi_call_function thenFunc failed, ret: %{public}d", status);
204+
if (status == napi_pending_exception) {
205+
LOGI("Caught invoke exception: napi_pending_exception");
206+
napi_value exception;
207+
napi_get_and_clear_last_exception(env, &exception);
208+
}
190209
}
191210
}
192211
// Callback Function Type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Created on 2024/01/05.
3+
//
4+
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
5+
// please include "napi/native_api.h".
6+
7+
#include <js_native_api.h>
8+
#include <js_native_api_types.h>
9+
#include "MouseNapi.h"
10+
#include "platform/ohos/napi/plugin_manager.h"
11+
#include "../../CCLogOhos.h"
12+
#include "ui/UIEditBox/UIEditBoxImpl-ohos.h"
13+
#include "base/CCIMEDispatcher.h"
14+
#include "platform/ohos/napi/render/plugin_render.h"
15+
#include <string>
16+
17+
napi_value MouseNapi::mouseWheelCB(napi_env env, napi_callback_info info) {
18+
napi_status status;
19+
size_t argc = 2;
20+
napi_value args[2];
21+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
22+
if (argc != 2) {
23+
napi_throw_type_error(env, NULL, "Wrong number of arguments");
24+
return nullptr;
25+
}
26+
napi_valuetype valuetype;
27+
status = napi_typeof(env, args[0], &valuetype);
28+
if (status != napi_ok) {
29+
return nullptr;
30+
}
31+
if (valuetype != napi_string) {
32+
napi_throw_type_error(env, NULL, "Wrong arguments");
33+
return nullptr;
34+
}
35+
36+
status = napi_typeof(env, args[1], &valuetype);
37+
if (status != napi_ok) {
38+
return nullptr;
39+
}
40+
if (valuetype != napi_number) {
41+
napi_throw_type_error(env, NULL, "Wrong arguments");
42+
return nullptr;
43+
}
44+
size_t pInt;
45+
char eventType[256];
46+
NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], eventType, 256, &pInt));
47+
double scrollY;
48+
NAPI_CALL(env, napi_get_value_double(env, args[1], &scrollY));
49+
PluginRender::MouseWheelCB(eventType, scrollY);
50+
return nullptr;
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Created on 2024/01/05.
3+
//
4+
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
5+
// please include "napi/native_api.h".
6+
7+
#ifndef MyApplication_MouseNapi_H
8+
#define MyApplication_MouseNapi_H
9+
10+
#include <string>
11+
#include <napi/native_api.h>
12+
13+
class MouseNapi {
14+
public:
15+
static napi_value mouseWheelCB(napi_env env, napi_callback_info info);
16+
};
17+
18+
#endif //MyApplication_MouseNapi_H

cocos2dx/platform/ohos/napi/plugin_manager.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info)
7373

7474
int64_t value;
7575
NAPI_CALL(env, napi_get_value_int64(env, args[0], &value));
76-
76+
napi_handle_scope scope = nullptr;
77+
NAPI_CALL(env, napi_open_handle_scope(env, &scope));
78+
if(scope == nullptr){
79+
return nullptr;
80+
}
7781
NAPI_CALL(env, napi_create_object(env, &exports));
7882

7983
switch (value) {
@@ -180,6 +184,7 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info)
180184
default:
181185
OHOS_LOGE("unknown type");
182186
}
187+
NAPI_CALL(env, napi_close_handle_scope(env, scope));
183188
return exports;
184189
}
185190

samples/Cpp/TestCpp/proj.ohos/.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Language: Cpp
22
# BasedOnStyle: LLVM
33
ColumnLimit: 120
4-
SortIncludes: false
4+
SortIncludes: CaseSensitive
55
TabWidth: 4
66
IndentWidth: 4
77
UseTab: Never

samples/Cpp/TestCpp/proj.ohos/build-profile.json5

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "default",
66
"signingConfig": "default",
7-
"compatibleSdkVersion": "5.0.0(12)",
7+
"compatibleSdkVersion": "5.0.3(15)",
88
"runtimeOS": "HarmonyOS"
99
}
1010
],
@@ -13,13 +13,13 @@
1313
"name": "default",
1414
"type": "HarmonyOS",
1515
"material": {
16-
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_YsqqXgi_-vGu2MmS-XEvj0Qe99Edur1WB7XCSIQzA4g=.cer",
17-
"storePassword": "0000001BED65E7E69E75A6E8718A9A34837AD311857FB4848103433F9D2E37E097304FD5B3F6F343EB52BB",
16+
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cC8pZN1-zB3QuF67fWoCo0LyOU0uiMb3BLLKA5aZWeE=.cer",
1817
"keyAlias": "debugKey",
19-
"keyPassword": "0000001B41912C35007D0C5131CB9693F5EB89D9AFDF428A10E358BF40774BEC08CE2E14BE6BF19BEDB97B",
20-
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_YsqqXgi_-vGu2MmS-XEvj0Qe99Edur1WB7XCSIQzA4g=.p7b",
18+
"keyPassword": "0000001AE84C1F38E78F0D68D15FF84C5C34539A3902E3605BA1AA3E0716FC9C16BD2018DAD5296DF6FC",
19+
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cC8pZN1-zB3QuF67fWoCo0LyOU0uiMb3BLLKA5aZWeE=.p7b",
2120
"signAlg": "SHA256withECDSA",
22-
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_YsqqXgi_-vGu2MmS-XEvj0Qe99Edur1WB7XCSIQzA4g=.p12"
21+
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cC8pZN1-zB3QuF67fWoCo0LyOU0uiMb3BLLKA5aZWeE=.p12",
22+
"storePassword": "0000001AC7FD8DE631ADEDF00A9231852223654FC07DD13938A89DE7BA4B92BD16B0B1C863D114CD5A55"
2323
}
2424
}
2525
]

samples/Cpp/TestCpp/proj.ohos/entry/build-profile.json5

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"sourceOption": {
1414
"workers": [
15-
'./src/main/ets/workers/CocosWorker.ts'
15+
'./src/main/ets/workers/CocosWorker.ets'
1616
]
1717
}
1818
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import window from '@ohos.window';
2+
import UIAbility from '@ohos.app.ability.UIAbility';
3+
import nativeRender from "libnativerender.so";
4+
import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities"
5+
import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities"
6+
import { BusinessError } from '@kit.BasicServicesKit';
7+
import { WorkerManager } from '../workers/WorkerManager';
8+
import Want from '@ohos.app.ability.Want';
9+
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
10+
11+
const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE);
12+
const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS);
13+
let cocosWorker = WorkerManager.getInstance().getWorker();
14+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
15+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
16+
export default class MainAbility extends UIAbility {
17+
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
18+
nativeAppLifecycle.onCreate();
19+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context);
20+
console.info('[LIFECYCLE-App] onCreate')
21+
}
22+
23+
onDestroy() {
24+
nativeAppLifecycle.onDestroy();
25+
console.info('[LIFECYCLE-App] onDestroy')
26+
}
27+
28+
onWindowStageCreate(windowStage: window.WindowStage): void {
29+
// Main window is created, set main page for this ability
30+
windowStage.loadContent('pages/Index', (err:BusinessError, data) => {
31+
if (err.code) {
32+
return;
33+
}
34+
rawFileUtils.nativeResourceManagerInit(this.context.resourceManager);
35+
rawFileUtils.writablePathInit(this.context.filesDir);
36+
});
37+
38+
windowStage.getMainWindow().then((windowIns: window.Window) => {
39+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW, windowIns);
40+
// Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed.
41+
let systemBarPromise = windowIns.setWindowSystemBarEnable([]);
42+
// Whether the window layout is displayed in full screen mode
43+
let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true);
44+
// Sets whether the screen is always on.
45+
let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true);
46+
Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => {
47+
console.info('Succeeded in setting the window');
48+
}).catch((err: BusinessError) => {
49+
console.error('Failed to set the window, cause ', err.code, err.message);
50+
});
51+
52+
try {
53+
DeviceUtils.calculateSafeArea(cocosWorker, windowIns.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT), windowIns.getWindowProperties().windowRect);
54+
windowIns.on('avoidAreaChange', (data) => {
55+
console.info('getSafeAreaRect Succeeded in enabling the listener for system avoid area changes. type:' +
56+
JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area));
57+
58+
if(data.type == window.AvoidAreaType.TYPE_SYSTEM_GESTURE || data.type == window.AvoidAreaType.TYPE_KEYBOARD) {
59+
return;
60+
}
61+
62+
let mainWindow: window.Window = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW);
63+
DeviceUtils.calculateSafeArea(cocosWorker, data.area, mainWindow.getWindowProperties().windowRect);
64+
});
65+
} catch (exception) {
66+
console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`);
67+
}
68+
})
69+
70+
windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => {
71+
let stageEventType: window.WindowStageEventType = data;
72+
switch (stageEventType) {
73+
case window.WindowStageEventType.RESUMED:
74+
console.info('[LIFECYCLE-App] onShow_RESUMED')
75+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
76+
nativeAppLifecycle.onShow();
77+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
78+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
79+
}
80+
break;
81+
case window.WindowStageEventType.PAUSED:
82+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
83+
console.info('[LIFECYCLE-App] onHide_PAUSED')
84+
nativeAppLifecycle.onHide();
85+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
86+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
87+
}
88+
break;
89+
default:
90+
break;
91+
}
92+
});
93+
}
94+
95+
onWindowStageDestroy() {
96+
// Main window is destroyed, release UI related resources
97+
}
98+
99+
onForeground() {
100+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
101+
// Ability has brought to foreground
102+
console.info('[LIFECYCLE-App] onShow')
103+
nativeAppLifecycle.onShow();
104+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
105+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
106+
}
107+
}
108+
109+
onBackground() {
110+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
111+
// Ability has back to background
112+
console.info('[LIFECYCLE-App] onHide')
113+
nativeAppLifecycle.onHide();
114+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
115+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
116+
}
117+
}
118+
};

samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/MainAbility/MainAbility.ts

-80
This file was deleted.

samples/Cpp/TestCpp/proj.ohos/entry/src/main/ets/components/CocosVideoPlayer.ets

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export struct CocosVideoPlayer {
1212
Video({
1313
src: this.videoPlayerInfo.isUrl ? this.videoPlayerInfo.url : this.videoPlayerInfo.rawfile,
1414
controller: this.videoPlayerInfo.controller
15-
}).position({ x: this.videoPlayerInfo.x, y: this.videoPlayerInfo.y })
15+
})
1616
.width(this.videoPlayerInfo.w)
1717
.height(this.videoPlayerInfo.h)
1818
.visibility(this.videoPlayerInfo.visible ? Visibility.Visible : Visibility.None)

0 commit comments

Comments
 (0)