Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/core/builder/platforms/ohos/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ module.exports = {
google_play_instant: 'Google Play Instant',
input_sdk: 'Input SDK',
remoteUrl: 'Remote URL',
JobSystem: 'Job System',
none: 'None',
},
encrypt: {
title: 'Encrypt JS',
encrypt_key: 'JS Encryption Key',
compress_zip: 'Zip Compress',
disable_tips: 'In debug mode, the Encrypt JS is invalid',
},
tips: {
not_empty: 'Can not be empty!',
Expand All @@ -32,6 +40,8 @@ module.exports = {
orientation_portrait: 'The screen is upright and the Home button is down',
orientation_landscape_left: 'The screen is horizontal, the Home button is on the left side of the screen',
orientation_landscape_right: 'The screen is horizontal, the Home button is on the right side of the screen',
JobSystemTaskFlow: 'TaskFlow needs C++17 support.',
JobSystemOther: 'C++17 will be enabled to support compilation.',
},
make: {
label: 'Make',
Expand Down
16 changes: 13 additions & 3 deletions src/core/builder/platforms/ohos/i18n/zh.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = {
// 专用名词,不要翻译
// 专用名词,不要翻译
title: 'HarmonyOS',
options: {
package_name: '应用 ID 名称',
Expand All @@ -15,14 +15,24 @@ module.exports = {
portrait: '竖屏',

render_back_end: '渲染后端',
JobSystem: '任务调度系统',
none: '不开启',
},
encrypt: {
title: '加密 JS',
encrypt_key: 'JS 加密密钥',
compress_zip: 'Zip 压缩',
disable_tips: '调试模式下,JS 加密无效',
},
tips: {
not_empty: '不能为空!',
at_least_one: '请至少选择一项',
package_name_error: '请输入正确的应用 ID:必须至少包含两段(一个或多个圆点),每段必须以字母开头, 包名中只能包含数字、字母和下划线。',
package_name_error: '请输入正确的应用 ID:必须至少包含两段(一个或多个圆点),每段必须以字母开头包名中只能包含数字、字母和下划线。',
ohos_sdk_error: '找不到 HarmonyOS NDK/SDK 路径,请到 偏好设置 -> 外部程序 中设置',
set_ohos_sdk: '设置 HarmonyOS SDK',
apilevel_empty: '获取 apiLevel 失败,请点击旁边的 \'设置 HarmonyOS SDK\' 按钮到 \'偏好设置 -> 外部程序\' 中检查 \'HarmonyOS NDK/SDK\' 路径配置',
apilevel_empty: '获取 apiLevel 失败,请点击旁边的“设置 HarmonyOS SDK”按钮到“偏好设置 -> 外部程序”中检查“HarmonyOS NDK/SDK”路径配置',
JobSystemTaskFlow: 'TaskFlow 需要启用 C++17',
JobSystemOther: '将会自动启用 C++17 以支持编译',
},
make: {
label: '生成',
Expand Down
31 changes: 31 additions & 0 deletions src/core/builder/platforms/ohos/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "ohos",
"version": "1.0.0",
"author": "Cocos",
"description": "OHOS platform plugin",
"license": "ISC",
"type": "commonjs",
"contributes": {
"builder": {
"register": true,
"platform": "ohos",
"config": "./src/config",
"hooks": "./src/hooks"
},
"pinkBuilder": {
"platform": "ohos",
"customView": "./dist/view/build-config.js",
"customExtensionHost": "./dist/view/build-config-host.js",
"customFields": [
"encrypted",
"xxteaKey",
"compressZip",
"JobSystem"
]
}
},
"scripts": {
"build-view": "node scripts/build-view.js",
"watch-view": "node scripts/build-view.js --watch"
}
}
57 changes: 57 additions & 0 deletions src/core/builder/platforms/ohos/scripts/build-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const ps = require('path');
const esbuild = require('esbuild');

const watch = process.argv.includes('--watch');

const viewOptions = {
entryPoints: [ps.join(__dirname, '../src/view/build-config.tsx')],
outfile: ps.join(__dirname, '../dist/view/build-config.js'),
bundle: true,
format: 'esm',
platform: 'browser',
target: 'es2022',
jsx: 'automatic',
external: [
'react',
'react-dom',
'react/jsx-runtime',
'react-dom/client',
'react/compiler-runtime',
'@pink/ui-kit',
],
logLevel: 'info',
};

const hostOptions = {
entryPoints: [ps.join(__dirname, '../src/view/build-config-host.ts')],
outfile: ps.join(__dirname, '../dist/view/build-config-host.js'),
bundle: true,
format: 'cjs',
platform: 'node',
target: 'node18',
external: ['vscode'],
logLevel: 'info',
};

const allOptions = [viewOptions, hostOptions];

(async function buildView() {
try {
if (watch) {
for (const options of allOptions) {
const ctx = await esbuild.context(options);
await ctx.rebuild();
await ctx.watch();
}
console.log('[ohos build-view] watching src/view for changes...');
} else {
console.time('Bundle OHOS View');
await Promise.all(allOptions.map((options) => esbuild.build(options)));
console.timeEnd('Bundle OHOS View');
process.exit(0);
}
} catch (error) {
console.error(error);
process.exit(1);
}
}());
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { IPlatformBuildPluginConfig } from '../../@types/protected';
import { commonOptions, baseNativeCommonOptions } from '../native-common';
import { IPlatformBuildPluginConfig } from '../../../@types/protected';
import { commonOptions, baseNativeCommonOptions } from '../../native-common';

const config: IPlatformBuildPluginConfig = {
...commonOptions,
Expand All @@ -28,7 +28,7 @@ const config: IPlatformBuildPluginConfig = {
message: 'Invalid package name specified',
},
},
hooks: './hooks',
hooks: './src/hooks',

options: {
...baseNativeCommonOptions,
Expand All @@ -54,11 +54,6 @@ const config: IPlatformBuildPluginConfig = {
type: 'boolean',
default: false,
},
upsideDown: {
label: 'i18n:ohos.options.upsideDown',
type: 'boolean',
default: false,
},
landscapeRight: {
label: 'i18n:ohos.options.landscape_right',
type: 'boolean',
Expand All @@ -72,7 +67,6 @@ const config: IPlatformBuildPluginConfig = {
},
default: {
portrait: false,
upsideDown: false,
landscapeRight: true,
landscapeLeft: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import path from 'path';
import os from 'os';
import { accessSync, existsSync, constants, outputJSON } from 'fs-extra';
import * as nativeCommonHook from '../native-common/hooks';
import { BuilderCache, IBuilder } from '../../@types/protected';
import * as nativeCommonHook from '../../native-common/hooks';
import { BuilderCache, IBuilder } from '../../../@types/protected';
export const throwError = true;
import { IBuildResult, IOhosInternalBuildOptions } from './type';
import { generateOptions } from './utils';
Expand Down Expand Up @@ -45,4 +45,4 @@ export function onAfterBundleInit(options: IOhosInternalBuildOptions) {
options.assetSerializeOptions!['cc.EffectAsset'].glsl1 = false;
options.assetSerializeOptions!['cc.EffectAsset'].glsl3 = true;
options.assetSerializeOptions!['cc.EffectAsset'].glsl4 = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { IInternalBuildOptions, InternalBuildResult } from '../../@types/protected';
import { CocosParams } from '../native-common/pack-tool/base/default';
import { ICustomBuildScriptParam, IOptions as INativeOption } from '../native-common/type';
import { IInternalBuildOptions, InternalBuildResult } from '../../../@types/protected';
import { CocosParams } from '../../native-common/pack-tool/base/default';
import { ICustomBuildScriptParam, IOptions as INativeOption } from '../../native-common/type';

export type IOrientation = 'landscape' | 'portrait';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { platform } from 'os';
*/
export async function generateOptions(options: IOhosInternalBuildOptions) {
const ohos = options.packages.ohos;
ohos.orientation = ohos.orientation || {};
if(!ohos.sdkPath) {
ohos.sdkPath = process.env.OHOS_HOME || process.env.OHOS_SDK_ROOT || '';

Expand Down Expand Up @@ -66,4 +65,4 @@ export async function generateOptions(options: IOhosInternalBuildOptions) {

console.log(`[OHOS] Using SDK at: ${ohos.sdkPath}, Using NDK at: ${ohos.ndkPath}`);
return ohos;
}
}
67 changes: 67 additions & 0 deletions src/core/builder/platforms/ohos/src/view/build-config-host.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as path from 'node:path';

type Bundle = Record<string, unknown>;

interface HostContext {
registerMethod(name: string, handler: (...args: any[]) => unknown | Promise<unknown>): void;
}

function currentLang(): 'zh' | 'en' {
let locale = 'en';
try {
const cfg = process.env.VSCODE_NLS_CONFIG;
if (cfg) {
locale = (JSON.parse(cfg) as { locale?: string }).locale || locale;
}
} catch {
// Fallback to English.
}
return locale.toLowerCase().startsWith('zh') ? 'zh' : 'en';
}

let cache: { lang: string; bundle: Bundle } | undefined;

function loadBundle(): Bundle {
const lang = currentLang();
if (cache?.lang === lang) {
return cache.bundle;
}

let bundle: Bundle = {};
try {
const file = path.join(__dirname, '..', '..', 'i18n', `${lang}.js`);
delete require.cache[require.resolve(file)];
bundle = (require(file) as Bundle) ?? {};
} catch {
bundle = {};
}
cache = { lang, bundle };
return bundle;
}

function lookup(bundle: Bundle, key: string): string | undefined {
let cur: unknown = bundle;
for (const seg of key.split('.')) {
if (cur && typeof cur === 'object' && seg in (cur as Bundle)) {
cur = (cur as Bundle)[seg];
} else {
return undefined;
}
}
return typeof cur === 'string' ? cur : undefined;
}

function substitute(text: string, sub?: Record<string, unknown>): string {
if (!sub) {
return text;
}
return text.replace(/%?\{(\w+)\}/g, (match, key: string) => (key in sub ? String(sub[key]) : match));
}

export function activate(context: HostContext): void {
context.registerMethod('getI18nBundle', () => loadBundle());
context.registerMethod('t', (key: string, sub?: Record<string, unknown>) => {
const text = lookup(loadBundle(), key);
return text === undefined ? key : substitute(text, sub);
});
}
Loading
Loading