Skip to content

Commit

Permalink
v3.8 Support openharmony4.0 (#15708)
Browse files Browse the repository at this point in the history
* Fix openharmony platform issues:
1. Fix xmlhttprequest request problems.
2. Repair the problem of not being able to display full screen.

* fix: crash on OH 4.0 (#15656)

* Openharmony adapted to the new Deveco(3.1 beta2) version

* Runtime environment using harmonyos

* Modifying configuration content

* Modify the usage syntax
Restore default values

---------

Co-authored-by: PP <[email protected]>
  • Loading branch information
qiuguohua and PPpro authored Jul 13, 2023
1 parent 5271644 commit 39c57f0
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 1,270 deletions.
9 changes: 6 additions & 3 deletions cocos/rendering/index.jsb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
declare const nr: any;
declare const jsb: any;

import { OPEN_HARMONY } from 'internal:constants'
import { ccenum, CCString, js } from '../core';
import * as pipeline from './define';
import { ccclass, serializable, editable, type } from '../core/data/class-decorator';
Expand Down Expand Up @@ -447,9 +448,11 @@ function proxyArrayAttributeImpl(proto: any, attr: string) {

let proxyArrayAttribute = proxyArrayAttributeImpl;

proxyArrayAttribute(RenderFlow.prototype, '_stages');

proxyArrayAttribute(RenderPipeline.prototype, '_flows');
if (!OPEN_HARMONY) {
// WORKAROUND: the proxy array getLength crashed on OH platform
proxyArrayAttribute(RenderFlow.prototype, '_stages');
proxyArrayAttribute(RenderPipeline.prototype, '_flows');
}

//-------------------- register types --------------------

Expand Down
6 changes: 4 additions & 2 deletions native/cocos/network/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ static bool configureCURL(HttpClient *client, HttpRequest *request, CURL *handle
if (code != CURLE_OK) {
return false;
}
code = curl_easy_setopt(handle, CURLOPT_TIMEOUT, request->getTimeout());
// In the openharmony platform, the long type must be used, otherwise there will be an exception.
long timeout = static_cast<long>(request->getTimeout());
code = curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);
if (code != CURLE_OK) {
return false;
}
code = curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, request->getTimeout());
code = curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, timeout);
if (code != CURLE_OK) {
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions native/cocos/platform/openharmony/OpenHarmonyPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ void OpenHarmonyPlatform::onSurfaceCreated(OH_NativeXComponent* component, void*
}

void OpenHarmonyPlatform::onSurfaceChanged(OH_NativeXComponent* component, void* window) {
uint64_t width = 0;
uint64_t height = 0;
int32_t ret = OH_NativeXComponent_GetXComponentSize(_component, window, &width, &height);
CC_ASSERT(ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS);
WindowEvent ev;
ev.windowId = cc::ISystemWindow::mainWindowId;
ev.type = WindowEvent::Type::SIZE_CHANGED;
ev.width = width;
ev.height = height;
events::WindowEvent::broadcast(ev);
}

void OpenHarmonyPlatform::onSurfaceDestroyed(OH_NativeXComponent* component, void* window) {
Expand Down
6 changes: 6 additions & 0 deletions platforms/native/builtin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ 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;
}
Expand Down
10 changes: 4 additions & 6 deletions scripts/native-pack-tool/source/platforms/openharmony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { cchelper, Paths } from "../utils";
import { randomBytes } from "crypto";
import { outputJSONSync } from 'fs-extra';
import * as JSON5 from "json5"
import { writeFileSync } from "fs";

export interface IOrientation {
landscapeLeft: boolean;
Expand Down Expand Up @@ -96,13 +97,10 @@ export class OpenHarmonyPackTool extends NativePackTool {
stringJSON.string.find((item: any) => item.name === 'MainAbility_label').value = this.params.projectName;
outputJSONSync(stringJSONPath, stringJSON, { spaces: 2 });

const packageJsonPath = ps.join(ohosProjDir, 'package.json');
const packageJson = fs.readJSONSync(packageJsonPath);
const packageJsonPath = ps.join(ohosProjDir, 'oh-package.json5');
const packageJson = this.readJSON5Sync(packageJsonPath);
packageJson.name = this.params.projectName;
fs.writeJSONSync(packageJsonPath, packageJson, {
spaces: 4,
});

writeFileSync(packageJsonPath, JSON5.stringify(packageJson, null, 4));
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion templates/openharmony/entry/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"targets": [
{
"name": "default"
"name": "default",
"runtimeOS": "HarmonyOS"
},
]
}
10 changes: 10 additions & 0 deletions templates/openharmony/entry/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"license": "",
"devDependencies": {},
"author": "",
"name": "entry",
"description": "example description",
"main": "",
"version": "1.0.0",
"dependencies": {}
}
5 changes: 0 additions & 5 deletions templates/openharmony/entry/package-lock.json

This file was deleted.

14 changes: 0 additions & 14 deletions templates/openharmony/entry/package.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import nativerender from "libcocos.so";
import { ContextType } from "../common/Constants"
import window from '@ohos.window';
import resourceManager from '@ohos.resourceManager';
import avsession from '@ohos.multimedia.avsession';
//import avsession from '@ohos.multimedia.avsession';

const nativeContext = nativerender.getContext(ContextType.ENGINE_UTILS);
const nativeAppLifecycle = nativerender.getContext(ContextType.APP_LIFECYCLE);
Expand All @@ -41,11 +41,11 @@ export default class MainAbility extends UIAbility {
// TODO(qgh): This is a temporary fix for audio not continuing to play when switching from background to foreground.
// The principle of the fix is to allow the app to continue playing audio after switching background, similar to music apps.
// After a while it will be killed by the system.
// @ts-ignore
avsession.createAVSession(this.context, tag, 'audio').then(async (session) =>{
globalThis.avsessionManager = session;
await globalThis.avsessionManager.activate();
})
// This will cause a crash on harmonyos.
// avsession.createAVSession(this.context, tag, 'audio').then(async (session) =>{
// globalThis.avsessionManager = session;
// await globalThis.avsessionManager.activate();
// })
}

onDestroy() {
Expand Down
2 changes: 2 additions & 0 deletions templates/openharmony/hvigor/hvigor-wrapper.js

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions templates/openharmony/hvigorw
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# ----------------------------------------------------------------------------
# Hvigor startup script, version 1.0.0
#
# Required ENV vars:
# ------------------
# NODE_HOME - location of a Node home dir
# or
# Add /usr/local/nodejs/bin to the PATH environment variable
# ----------------------------------------------------------------------------

HVIGOR_APP_HOME=$(dirname $(readlink -f $0))
HVIGOR_WRAPPER_SCRIPT=${HVIGOR_APP_HOME}/hvigor/hvigor-wrapper.js
warn() {
echo ""
echo -e "\033[1;33m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

error() {
echo ""
echo -e "\033[1;31m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

fail() {
error "$@"
exit 1
}

# Determine node to start hvigor wrapper script
if [ -n "${NODE_HOME}" ];then
EXECUTABLE_NODE="${NODE_HOME}/bin/node"
if [ ! -x "$EXECUTABLE_NODE" ];then
fail "ERROR: NODE_HOME is set to an invalid directory,check $NODE_HOME\n\nPlease set NODE_HOME in your environment to the location where your nodejs installed"
fi
else
EXECUTABLE_NODE="node"
which ${EXECUTABLE_NODE} > /dev/null 2>&1 || fail "ERROR: NODE_HOME is not set and not 'node' command found in your path"
fi

# Check hvigor wrapper script
if [ ! -r "$HVIGOR_WRAPPER_SCRIPT" ];then
fail "ERROR: Couldn't find hvigor/hvigor-wrapper.js in ${HVIGOR_APP_HOME}"
fi

# start hvigor-wrapper script
exec "${EXECUTABLE_NODE}" \
"${HVIGOR_WRAPPER_SCRIPT}" "$@"
64 changes: 64 additions & 0 deletions templates/openharmony/hvigorw.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Hvigor startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

set WRAPPER_MODULE_PATH=%APP_HOME%\hvigor\hvigor-wrapper.js
set NODE_EXE=node.exe

goto start

:start
@rem Find node.exe
if defined NODE_HOME goto findNodeFromNodeHome

%NODE_EXE% --version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute

echo.
echo ERROR: NODE_HOME is not set and no 'node' command could be found in your PATH.
echo.
echo Please set the NODE_HOME variable in your environment to match the
echo location of your NodeJs installation.

goto fail

:findNodeFromNodeHome
set NODE_HOME=%NODE_HOME:"=%
set NODE_EXE_PATH=%NODE_HOME%/%NODE_EXE%

if exist "%NODE_EXE_PATH%" goto execute
echo.
echo ERROR: NODE_HOME is not set and no 'node' command could be found in your PATH.
echo.
echo Please set the NODE_HOME variable in your environment to match the
echo location of your NodeJs installation.

goto fail

:execute
@rem Execute hvigor
"%NODE_EXE%" %WRAPPER_MODULE_PATH% %*

if "%ERRORLEVEL%" == "0" goto hvigorwEnd

:fail
exit /b 1

:hvigorwEnd
if "%OS%" == "Windows_NT" endlocal

:end
14 changes: 14 additions & 0 deletions templates/openharmony/oh-package-lock.json5

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions templates/openharmony/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"license": "",
"devDependencies": {
"@ohos/hypium": "1.0.6"
},
"author": "",
"name": "test-cases",
"description": "example description",
"main": "",
"version": "1.0.0",
"dependencies": {}
}
Loading

0 comments on commit 39c57f0

Please sign in to comment.