Skip to content

Commit 099c6b2

Browse files
authored
Modify the return parameter of scene.reload to an enum to clarify error types. (#415)
1 parent 5b29db2 commit 099c6b2

File tree

8 files changed

+86
-48
lines changed

8 files changed

+86
-48
lines changed

e2e/mcp/api/editor-prefab.e2e.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AssetsTestContext, generateTestId, setupAssetsTestEnvironment, teardownAssetsTestEnvironment } from '../../helpers/test-utils';
2+
import { ReloadResult } from '../../../src/core/scene/common/editor/type';
23

34
/**
45
* 测试打开预制体文件后,使用场景 API 对预制体内部进行操作
@@ -233,7 +234,7 @@ describe('MCP Editor Prefab API - Scene Operations on Prefab Assets', () => {
233234
// 2. 重新加载预制体
234235
const result = await context.mcpClient.callTool('scene-reload', {});
235236

236-
expect(result.data).toBe(true);
237+
expect(result.data).toBe(ReloadResult.SUCCESS); // ReloadResult.SUCCESS
237238
expect(result.code).toBe(200);
238239
});
239240

@@ -242,7 +243,7 @@ describe('MCP Editor Prefab API - Scene Operations on Prefab Assets', () => {
242243

243244
const result = await context.mcpClient.callTool('scene-reload', {});
244245

245-
expect(result.data).toBe(false);
246+
expect(result.data).toBe(ReloadResult.NO_EDITOR); // ReloadResult.NO_EDITOR
246247
expect(result.code).toBe(200);
247248
});
248249
});

e2e/mcp/api/scene.e2e.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AssetsTestContext, generateTestId, setupAssetsTestEnvironment, teardownAssetsTestEnvironment } from '../../helpers/test-utils';
2+
import { ReloadResult } from '../../../src/core/scene/common/editor/type';
23

34
describe('MCP Scene API', () => {
45
let context: AssetsTestContext;
@@ -328,7 +329,7 @@ describe('MCP Scene API', () => {
328329
// 重新加载场景
329330
const result = await context.mcpClient.callTool('scene-reload', {});
330331

331-
expect(result.data).toBe(true);
332+
expect(result.data).toBe(ReloadResult.SUCCESS);
332333
expect(result.code).toBe(200);
333334
});
334335

@@ -340,7 +341,7 @@ describe('MCP Scene API', () => {
340341
const result = await context.mcpClient.callTool('scene-reload', {});
341342

342343
// 应该失败或返回适当的错误
343-
expect(result.data).toBe(false);
344+
expect(result.data).toBe(ReloadResult.NO_EDITOR);
344345
expect(result.code).toBe(200);
345346
});
346347
});
@@ -386,7 +387,7 @@ describe('MCP Scene API', () => {
386387

387388
// 5. 重新加载场景
388389
const reloadResult = await context.mcpClient.callTool('scene-reload', {});
389-
expect(reloadResult.data).toBe(true);
390+
expect(reloadResult.data).toBe(ReloadResult.SUCCESS);
390391
expect(reloadResult.code).toBe(200);
391392

392393
// 6. 关闭场景

src/api/scene/schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export const SchemaCloseResult = z.boolean().describe('Close Scene/Prefab Result
2121

2222
export const SchemaSaveResult = SchemaSaveAssetResult.describe('Save Scene/Prefab Result'); // 保存场景/预制体结果
2323

24-
export const SchemaReload = z.boolean().describe('Reload Scene/Prefab Success'); // 重载场景/预制体是否成功
24+
import { ReloadResult } from '../../core/scene/common/editor/type';
25+
26+
export const SchemaReload = z.nativeEnum(ReloadResult).describe('Reload Scene/Prefab Result'); // 重载场景/预制体结果
2527

2628
export const SchemaCreateOptions = z.object({
2729
baseName: z.string().describe('Asset Name'), // 资源名称

src/core/scene/common/editor/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IScene } from './scene';
22
import type { Node, Scene } from 'cc';
33
import type { INode } from '../node';
44
import type { ICloseOptions, ICreateOptions, IOpenOptions, IReloadOptions, ISaveOptions } from './options';
5+
import { ReloadResult } from './type';
56
import { IAssetInfo } from '../../../assets/@types/public';
67
import { IBaseIdentifier } from './base';
78
import { IServiceEvents } from '../../scene-process/service/core';
@@ -69,7 +70,7 @@ export interface IEditorService extends IServiceEvents {
6970
* 重载资产
7071
* @param params
7172
*/
72-
reload(params: IReloadOptions): Promise<boolean>;
73+
reload(params: IReloadOptions): Promise<ReloadResult>;
7374

7475
/**
7576
* 创建新资产

src/core/scene/common/editor/type.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ export type TSceneTemplateType = typeof SCENE_TEMPLATE_TYPE[number];
99
*/
1010
export const CREATE_TYPES = ['scene', 'prefab'] as const;
1111
export type ICreateType = typeof CREATE_TYPES[number];
12+
13+
/**
14+
* 重载结果
15+
*/
16+
export enum ReloadResult {
17+
SUCCESS = 0,
18+
FAILED = 1,
19+
QUEUED = 2,
20+
NO_EDITOR = 3,
21+
ASSET_NOT_FOUND = 4,
22+
EDITOR_NOT_FOUND = 5,
23+
}

src/core/scene/scene-process/service/editor.ts

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
IReloadOptions,
1212
ISaveOptions,
1313
IScene,
14+
ReloadResult,
1415
} from '../../common';
1516
import { PrefabEditor, SceneEditor } from './editors';
1617
import { Rpc } from '../rpc';
@@ -234,48 +235,68 @@ export class EditorService extends BaseService<IEditorEvents> implements IEditor
234235
}
235236
}
236237

237-
async reload(params: IReloadOptions): Promise<boolean> {
238-
if (this.reloadPromise) {
238+
async reload(params: IReloadOptions): Promise<ReloadResult> {
239+
if (this._isReloading) {
239240
this.needReloadAgain = params;
240-
return false;
241-
}
242-
const urlOrUUID = params.urlOrUUID ?? this.currentEditorUuid;
243-
if (!urlOrUUID) {
244-
console.warn('当前没有打开任何编辑器');
245-
return false;
246-
}
247-
248-
const assetInfo = await Rpc.getInstance().request('assetManager', 'queryAssetInfo', [urlOrUUID]);
249-
if (!assetInfo) {
250-
console.warn(`通过 ${urlOrUUID} 请求资源失败`);
251-
return false;
252-
}
253-
254-
const editor = this.editorMap.get(assetInfo.uuid);
255-
if (!editor) {
256-
console.warn(`当前没有打开任何编辑器`);
257-
return false;
241+
return ReloadResult.QUEUED;
258242
}
243+
this._isReloading = true;
259244

260245
try {
261-
await this.waitLocks();
262-
this.reloadPromise = editor.reload() as Promise<IScene | INode>;
263-
await this.reloadPromise;
246+
const urlOrUUID = params.urlOrUUID ?? this.currentEditorUuid;
247+
if (!urlOrUUID) {
248+
console.warn('当前没有打开任何编辑器');
249+
this._isReloading = false;
250+
return ReloadResult.NO_EDITOR;
251+
}
264252

265-
if (this.needReloadAgain) {
266-
this.reload(this.needReloadAgain);
267-
this.needReloadAgain = null;
253+
const assetInfo = await Rpc.getInstance().request('assetManager', 'queryAssetInfo', [urlOrUUID]);
254+
if (!assetInfo) {
255+
console.warn(`通过 ${urlOrUUID} 请求资源失败`);
256+
this._isReloading = false;
257+
return ReloadResult.ASSET_NOT_FOUND;
258+
}
259+
260+
const editor = this.editorMap.get(assetInfo.uuid);
261+
if (!editor) {
262+
console.warn(`当前没有打开任何编辑器`);
263+
this._isReloading = false;
264+
return ReloadResult.EDITOR_NOT_FOUND;
268265
}
269266

270-
this.emit('editor:reload');
271-
this.broadcast('editor:reload');
272-
console.log(`重载 ${assetInfo.url}`);
273-
return true;
267+
this.reloadPromise = (async () => {
268+
try {
269+
let currentParams: IReloadOptions | null = params;
270+
while (currentParams) {
271+
await this.waitLocks();
272+
await editor.reload();
273+
274+
if (this.needReloadAgain) {
275+
currentParams = this.needReloadAgain;
276+
this.needReloadAgain = null;
277+
} else {
278+
currentParams = null;
279+
}
280+
281+
this.emit('editor:reload');
282+
this.broadcast('editor:reload');
283+
console.log(`重载 ${assetInfo.url}`);
284+
}
285+
return ReloadResult.SUCCESS;
286+
} catch (error) {
287+
console.error(error);
288+
return ReloadResult.FAILED;
289+
} finally {
290+
this.reloadPromise = null;
291+
this._isReloading = false;
292+
}
293+
})() as any;
294+
295+
return this.reloadPromise as unknown as Promise<ReloadResult>;
274296
} catch (error) {
275297
console.error(error);
276-
return false;
277-
} finally {
278-
this.reloadPromise = null;
298+
this._isReloading = false;
299+
return ReloadResult.FAILED;
279300
}
280301
}
281302

src/core/scene/test/editor-proxy-prefab.testcase.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IBaseIdentifier, INode, NodeType, TEditorEntity, } from '../common';
1+
import { IBaseIdentifier, INode, NodeType, ReloadResult, TEditorEntity, } from '../common';
22
import { EditorProxy } from '../main-process/proxy/editor-proxy';
33
import { SceneTestEnv } from './scene-test-env';
44
import { NodeProxy } from '../main-process/proxy/node-proxy';
@@ -65,7 +65,7 @@ describe('EditorProxy Prefab 测试', () => {
6565
urlOrUUID: identifier?.assetUuid,
6666
});
6767

68-
expect(result).toBe(true);
68+
expect(result).toBe(ReloadResult.SUCCESS);
6969
});
7070

7171
it('queryCurrent - 通过 UUID 关闭后获取当前预制体应该为空', async () => {
@@ -133,7 +133,7 @@ describe('EditorProxy Prefab 测试', () => {
133133
urlOrUUID: instanceAssetURL
134134
});
135135

136-
expect(result).toBe(true);
136+
expect(result).toBe(ReloadResult.SUCCESS);
137137
});
138138

139139
it('queryCurrent - 通过 URL 关闭后获取当前预制体应该为空', async () => {
@@ -196,7 +196,7 @@ describe('EditorProxy Prefab 测试', () => {
196196
it('reload - 重载当前预制体', async () => {
197197
const result = await EditorProxy.reload({});
198198

199-
expect(result).toBe(true);
199+
expect(result).toBe(ReloadResult.SUCCESS);
200200
});
201201

202202
it('queryCurrent - 获取当前预制体', async () => {

src/core/scene/test/editor-proxy-scene.testcase.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IBaseIdentifier, IScene, NodeType, TEditorEntity, } from '../common';
1+
import { IBaseIdentifier, IScene, NodeType, TEditorEntity, ReloadResult } from '../common';
22
import { EditorProxy } from '../main-process/proxy/editor-proxy';
33
import { SceneTestEnv } from './scene-test-env';
44
import { NodeProxy } from '../main-process/proxy/node-proxy';
@@ -55,7 +55,7 @@ describe('EditorProxy Scene 测试', () => {
5555
const result = await EditorProxy.reload({
5656
urlOrUUID: identifier.assetUuid,
5757
});
58-
expect(result).toBe(true);
58+
expect(result).toBe(ReloadResult.SUCCESS);
5959
});
6060

6161
it('queryCurrent - 通过 UUID 关闭后获取当前场景应该为空', async () => {
@@ -114,7 +114,7 @@ describe('EditorProxy Scene 测试', () => {
114114
const result = await EditorProxy.reload({
115115
urlOrUUID: identifier.assetUrl,
116116
});
117-
expect(result).toBe(true);
117+
expect(result).toBe(ReloadResult.SUCCESS);
118118
});
119119

120120
it('queryCurrent - 通过 URL 关闭后获取当前场景应该为空', async () => {
@@ -152,7 +152,7 @@ describe('EditorProxy Scene 测试', () => {
152152

153153
it('reload - 重载当前场景', async () => {
154154
const result = await EditorProxy.reload({});
155-
expect(result).toBe(true);
155+
expect(result).toBe(ReloadResult.SUCCESS);
156156
});
157157

158158
it('queryCurrent - 获取当前场景', async () => {

0 commit comments

Comments
 (0)