Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/cocos-cli-types/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ describe('cocos-cli-types: cli', () => {

it('IServiceManager.Node should have CRUD methods', () => {
type NodeKeys = keyof IServiceManager['Node'];
const nodeMethods: NodeKeys[] = ['createByType', 'createByAsset', 'delete', 'update', 'query', 'queryNodeTree'];
const nodeMethods: NodeKeys[] = ['createNodeByType', 'createNodeByAsset', 'deleteNode', 'updateNode', 'queryNode', 'queryNodeTree'];
expect(nodeMethods.length).toBeGreaterThan(0);
});

it('IServiceManager.Component should have component methods', () => {
type CompKeys = keyof IServiceManager['Component'];
const compMethods: CompKeys[] = ['add', 'remove', 'setProperty', 'query', 'queryAll'];
const compMethods: CompKeys[] = ['addComponent', 'removeComponent', 'setProperty', 'queryComponent', 'queryAllComponent'];
expect(compMethods.length).toBeGreaterThan(0);
});

Expand Down
10 changes: 5 additions & 5 deletions src/api/scene/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ComponentApi {
@result(SchemaComponentResult)
async addComponent(@param(SchemaAddComponentInfo) addComponentInfo: TAddComponentInfo): Promise<CommonResultType<TComponentResult>> {
try {
const component = await Scene.Component.add({ nodePath: addComponentInfo.nodePath, component: addComponentInfo.component });
const component = await Scene.addComponent({ nodePathOrUuid: addComponentInfo.nodePath, component: addComponentInfo.component });
return {
code: COMMON_STATUS.SUCCESS,
data: component
Expand All @@ -52,7 +52,7 @@ export class ComponentApi {
@result(SchemaBooleanResult)
async removeComponent(@param(SchemaRemoveComponent) component: TRemoveComponentOptions): Promise<CommonResultType<boolean>> {
try {
const result = await Scene.Component.remove(component);
const result = await Scene.removeComponent(component);
return {
code: COMMON_STATUS.SUCCESS,
data: result
Expand All @@ -74,7 +74,7 @@ export class ComponentApi {
@result(SchemaComponentResult)
async queryComponent(@param(SchemaQueryComponent) component: TQueryComponentOptions): Promise<CommonResultType<TComponentResult | null>> {
try {
const componentInfo = await Scene.Component.query(component);
const componentInfo = await Scene.queryComponent(component);
if (!componentInfo) {
throw new Error(`component not found: ${component.path}`);
}
Expand All @@ -99,7 +99,7 @@ export class ComponentApi {
@result(SchemaBooleanResult)
async setProperty(@param(SchemaSetPropertyOptions) setPropertyOptions?: TSetPropertyOptions): Promise<CommonResultType<boolean>> {
try {
const result = await Scene.Component.setProperty(setPropertyOptions as ISetPropertyOptions);
const result = await Scene.setProperty(setPropertyOptions as ISetPropertyOptions);
return {
code: COMMON_STATUS.SUCCESS,
data: result
Expand All @@ -121,7 +121,7 @@ export class ComponentApi {
@result(SchemaQueryAllComponentResult)
async queryAllComponent(): Promise<CommonResultType<TQueryAllComponentResult>> {
try {
const components = await Scene.Component.queryAll();
const components = await Scene.queryAllComponent();
return {
code: COMMON_STATUS.SUCCESS,
data: components,
Expand Down
12 changes: 6 additions & 6 deletions src/api/scene/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from './node-schema';
import { description, param, result, title, tool } from '../decorator/decorator.js';
import { COMMON_STATUS, CommonResultType } from '../base/schema-base';
import { ICreateByNodeTypeParams, INode, Scene } from '../../core/scene';
import { ICreateByNodeTypeParams, Scene } from '../../core/scene';

export class NodeApi {

Expand All @@ -35,7 +35,7 @@ export class NodeApi {
data: undefined,
};
try {
const resultNode = await Scene.Node.createByType(options as ICreateByNodeTypeParams);
const resultNode = await Scene.createNodeByType(options as ICreateByNodeTypeParams);
if (resultNode) {
ret.data = resultNode;
}
Expand All @@ -62,7 +62,7 @@ export class NodeApi {
data: undefined,
};
try {
const resultNode = await Scene.Node.createByAsset(options);
const resultNode = await Scene.createNodeByAsset(options);
if (resultNode) {
ret.data = resultNode;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ export class NodeApi {
};

try {
const result = await Scene.Node.delete(options);
const result = await Scene.deleteNode(options);
if (!result) throw new Error(`node not found at path: ${options.path}`);
ret.data = {
path: result.path,
Expand All @@ -114,7 +114,7 @@ export class NodeApi {
@result(SchemaNodeUpdateResult)
async updateNode(@param(SchemaNodeUpdate) options: TUpdateNodeOptions): Promise<CommonResultType<TNodeUpdateResult>> {
try {
const data = await Scene.Node.update(options);
const data = await Scene.updateNode(options);
return {
data: data,
code: COMMON_STATUS.SUCCESS,
Expand Down Expand Up @@ -142,7 +142,7 @@ export class NodeApi {
};

try {
const result = await Scene.Node.query(options) as INode | null;
const result = await Scene.queryNode(options);
if (!result) throw new Error(`node not found at path: ${options.path}`);
ret.data = result;
} catch (e) {
Expand Down
70 changes: 30 additions & 40 deletions src/core/scene/common/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface IComponentForEditor extends IProperty {
* 添加/创建组件的选项
*/
export interface IAddComponentOptions {
nodePath: string;
nodePathOrUuid: string;
component: string;
}

Expand All @@ -51,10 +51,6 @@ export interface IAddComponentOptions {
export interface IRemoveComponentOptions {
path: string;
}
export interface IRemovedComponentInfo {
name: string;
fileID: string;
}

/**
* 查询组件的选项
Expand All @@ -67,7 +63,7 @@ export interface IQueryComponentOptions {
* CLI 设置组件属性的选项
*/
export interface ISetPropertyOptions {
componentPath: string; // 修改属性的节点路径
componentPath: string; // 修改属性的对象的 uuid
// key: string; // 属性的 key
properties: {
[key: string]: null | undefined | number | boolean | string | object | Array<unknown>;
Expand All @@ -79,7 +75,7 @@ export interface ISetPropertyOptions {
* 编辑器设置组件属性的选项
*/
export interface ISetPropertyOptionsForEditor {
nodePath: string; // 修改属性的节点路径
uuid: string; // 修改属性的对象的 uuid
path: string; // 属性挂载对象的搜索路径
// key: string; // 属性的 key
dump: IProperty; // 属性 dump 出来的数据
Expand All @@ -90,7 +86,7 @@ export interface ISetPropertyOptionsForEditor {
* 执行组件方法的选项
*/
export interface IExecuteComponentMethodOptions {
path: string; // 组件路径,如 'Canvas/cc.Label_1'
uuid: string;
name: string;
args: any[];
}
Expand All @@ -108,26 +104,20 @@ export interface IQueryClassesOptions {
*/
export interface IComponentEvents extends INodeEvents {
'component:add': [Component];
'component:before-remove': [Component];
'component:remove': [Component];
'component:set-property': [Component, IChangeNodeOptions];
'component:added': [Component];
'component:removed': [Component];
'component:before-add-component': [string, Node];
'component:before-remove-component': [Component];
}

/**
* 组件服务的公开接口,排除了内部方法和事件相关接口
*/
export interface IPublicComponentService extends Omit<IComponentService, keyof IServiceEvents |
'init' |
'unregisterCompMgrEvents' |
'create' |
'reset' |
'queryClasses' |
'queryFunctionOfNode' |
'executeMethod' |
'hasScript'
'unregisterCompMgrEvents'
> { }

/**
Expand All @@ -137,33 +127,33 @@ export interface IComponentService extends IServiceEvents {
/**
* 添加组件到指定节点,返回添加后的组件信息
* @param params - 添加组件选项
* @param params.nodePath - 目标节点路径
* @param params.nodePathOrUuid - 目标节点路径或 UUID
* @param params.component - 组件类名,支持精确匹配('cc.Label')和模糊匹配('label')
* @returns 添加成功后的组件信息
*
* @example
* ```ts
* // 通过节点路径 + 精确组件名
* const comp = await add({ nodePath: 'Canvas/MyNode', component: 'cc.Label' });
* const comp = await addComponent({ nodePathOrUuid: 'Canvas/MyNode', component: 'cc.Label' });
*
* // 通过节点路径 + 模糊组件名
* const comp = await add({ nodePath: 'Canvas/MyNode', component: 'label' });
* // 通过节点 UUID + 模糊组件名
* const comp = await addComponent({ nodePathOrUuid: 'abc-123-uuid', component: 'label' });
* ```
*/
add(params: IAddComponentOptions): Promise<IComponent>;
addComponent(params: IAddComponentOptions): Promise<IComponent>;

/**
* 删除指定组件
* @param params - 删除组件选项
* @param params.path - 组件路径
* @param params.path - 组件路径,支持路径、UUID 或资源 URL
* @returns 删除成功返回 true,失败返回 false
*/
remove(params: IRemoveComponentOptions): Promise<boolean>;
removeComponent(params: IRemoveComponentOptions): Promise<boolean>;

/**
* 设置组件属性
* - CLI 调用时传入 ISetPropertyOptions,通过 componentPath 定位,属性为扁平键值对
* - 编辑器调用时传入 ISetPropertyOptionsForEditor,通过节点路径 + dump 路径定位,属性为 IProperty 格式
* - 编辑器调用时传入 ISetPropertyOptionsForEditor,通过节点 UUID + dump 路径定位,属性为 IProperty 格式
*
* @param params - 设置属性选项,根据调用方不同传入不同类型
* @returns 设置成功返回 true,失败返回 false
Expand All @@ -176,9 +166,9 @@ export interface IComponentService extends IServiceEvents {
* properties: { string: 'Hello', fontSize: 32 },
* });
*
* // 编辑器方式:通过节点路径 + dump 路径定位,传 IProperty 格式
* // 编辑器方式:通过节点 UUID + dump 路径定位,传 IProperty 格式
* await setProperty({
* nodePath: 'Canvas/MyNode',
* uuid: 'node-uuid',
* path: '__comps__.0.string',
* dump: { value: 'Hello', type: 'String' },
* });
Expand All @@ -197,38 +187,38 @@ export interface IComponentService extends IServiceEvents {
* @example
* ```ts
* CLI 模式:返回 IComponent(扁平属性)
* const comp = await query({ path: 'Canvas/cc.Label_1' }) as IComponent;
* const comp = await queryComponent({ path: 'Canvas/cc.Label_1' }) as IComponent;
*
* 编辑器模式:直接传 string,这里是uuid,因为与cli重复了,也支持 path 和 url
* const comp = await query('uuid') as IComponentForEditor;
* const comp = await queryComponent('uuid') as IComponentForEditor;
* ```
*/
query(params: IQueryComponentOptions | string): Promise<IComponent | IComponentForEditor | null>;
queryComponent(params: IQueryComponentOptions | string): Promise<IComponent | IComponentForEditor | null>;

/**
* 获取所有已注册的组件类名,包含内置与自定义组件
* @returns 组件类名数组,如 ['cc.Label', 'cc.Sprite', 'MyCustomComponent']
*/
queryAll(): Promise<string[]>;
queryAllComponent(): Promise<string[]>;

// ---- 编辑器相关接口 ----

/**
* 创建组件(编辑器使用),与 add 不同的是仅返回是否成功
* 创建组件(编辑器使用),与 addComponent 不同的是仅返回是否成功
* @param params - 添加组件选项
* @param params.nodePath - 目标节点路径
* @param params.nodePathOrUuid - 目标节点路径或 UUID
* @param params.component - 组件类名
* @returns 创建成功返回 true,失败返回 false
*/
create(params: IAddComponentOptions): Promise<boolean>;
createComponent(params: IAddComponentOptions): Promise<boolean>;

/**
* 复位组件,将组件所有属性恢复为默认值
* @param params - 查询组件选项,用于定位要复位的组件
* @param params.path - 组件路径
* @param params.path - 组件路径,支持路径、UUID 或资源 URL
* @returns 复位成功返回 true,失败返回 false
*/
reset(params: IQueryComponentOptions): Promise<boolean>;
resetComponent(params: IQueryComponentOptions): Promise<boolean>;

/**
* 获取所有注册类名,支持按继承关系过滤
Expand All @@ -253,27 +243,27 @@ export interface IComponentService extends IServiceEvents {

/**
* 查询指定节点上所有组件暴露的可调用函数
* @param path - 节点路径
* @param uuid - 节点 UUID
* @returns 节点上组件的函数信息,节点不存在时返回空对象
*/
queryFunctionOfNode(path: string): Promise<any>;
queryComponentFunctionOfNode(uuid: string): Promise<any>;

/**
* 执行组件上的指定方法
* @param options - 执行选项
* @param options.path - 组件路径,如 'Canvas/cc.Label_1'
* @param options.uuid - 组件实例的 UUID
* @param options.name - 要执行的方法名,如 'onLoad'、'start'
* @param options.args - 方法参数列表
* @returns 执行成功返回 true,失败返回 false
*/
executeMethod(options: IExecuteComponentMethodOptions): Promise<any>;
executeComponentMethod(options: IExecuteComponentMethodOptions): Promise<boolean>;

/**
* 查询指定名称的组件是否已注册(是否存在对应脚本)
* @param name - 组件类名,如 'cc.Label'
* @returns 存在返回 true,不存在返回 false
*/
hasScript(name: string): Promise<boolean>;
queryComponentHasScript(name: string): Promise<boolean>;

// ---- 内部接口,不对外暴露 ----

Expand Down
18 changes: 1 addition & 17 deletions src/core/scene/common/editor/scene.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { INode } from '../node';
import type { IComponentIdentifier } from '../component';
import type { IBaseIdentifier } from './base';
import { IPrefabInfo, ITargetOverrideInfoForEditor } from '../prefab';
import { IProperty } from '../../@types/public';
import { IPrefabInfo } from '../prefab';

/**
* 场景信息
Expand All @@ -13,18 +12,3 @@ export interface IScene extends IBaseIdentifier {
children: INode[];
components: IComponentIdentifier[];
}

export interface ISceneForEditor {
name: IProperty;
active: IProperty;
locked: IProperty;
_globals: Record<string, IProperty>;
isScene: boolean;
autoReleaseAssets: IProperty;

uuid: IProperty;
children: IProperty[];
parent: string;
__type__: string;
targetOverrides?: ITargetOverrideInfoForEditor[];
}
Loading
Loading