Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(cocos): a directory load leaves out what the layer cannot load instead of failing, and Component.EventHandler exists

yhh6c1cda8111c6
diff --git a/sdk/src/cocos/assetManager.ts b/sdk/src/cocos/assetManager.ts
index 25ded5b9e..9b0b0d5f1 100644
--- a/sdk/src/cocos/assetManager.ts
+++ b/sdk/src/cocos/assetManager.ts
@@ -92,6 +92,9 @@ function parseLoadArgs<T>(args: unknown[]): { type: AssetCtor | null; onProgress
const loaded = new Cache<Asset>();
+/** An asset of a kind the layer does not load: not a fault of the asset, so a directory load goes on without it. */
+export class UnsupportedAssetKind extends Error {}
+
async function loadEntry(entry: ResourceEntry, type: AssetCtor | null): Promise<Asset> {
const assets = assetsFor(getCurrentWorld());
switch (entry.kind) {
@@ -121,7 +124,7 @@ async function loadEntry(entry: ResourceEntry, type: AssetCtor | null): Promise<
}
default:
}
- throw new Error(`cocos compat: loading "${entry.path}" (${entry.kind}${type ? `, as ${type.name}` : ''}) is not supported by the compatibility layer yet; prefabs, JSON and text are`);
+ throw new UnsupportedAssetKind(`cocos compat: loading "${entry.path}" (${entry.kind}${type ? `, as ${type.name}` : ''}) is not supported by the compatibility layer yet; prefabs, JSON and text are`);
}
export class Bundle {
@@ -163,6 +166,7 @@ export class Bundle {
const entries = await this.entries_();
const keys = Object.keys(entries).filter((k) => dir === '' || k === dir || k.startsWith(`${dir}/`));
const out: Asset[] = [];
+ const skipped = new Set<string>();
for (let i = 0; i < keys.length; i++) {
const entry = entries[keys[i]!]!;
try {
@@ -171,12 +175,14 @@ export class Bundle {
if (!asset) loaded.add(key, (asset = await loadEntry(entry, type)));
out.push(asset);
} catch (err) {
- // an entry of a kind that does not load, or of another type than asked for, is skipped like a type mismatch
+ // an entry of a kind that does not load, or of another type than asked for, is left out: a directory of configs holds a readme too
if (type) continue;
+ if (err instanceof UnsupportedAssetKind) { skipped.add(entry.kind); continue; }
throw err;
}
onProgress?.(i + 1, keys.length, entry);
}
+ if (skipped.size > 0) log.info('cocos', `loadDir("${dir}") left out the assets of kind ${[...skipped].join(', ')}, which the layer does not load`);
return out;
})().then(
(r) => onComplete?.(null, r),
diff --git a/sdk/src/cocos/ui/button.ts b/sdk/src/cocos/ui/button.ts
index d92f4a1f0..50a173ed7 100644
--- a/sdk/src/cocos/ui/button.ts
+++ b/sdk/src/cocos/ui/button.ts
@@ -177,3 +177,6 @@ export function declareButton(): void {
}
export const isButtonDeclared = (): boolean => classInfoNamed('Button') !== undefined;
+
+/** `Component.EventHandler`, as scripts name the class. */
+(Component as unknown as { EventHandler: typeof EventHandler }).EventHandler = EventHandler;
diff --git a/sdk/tests/cocos/globals.test.ts b/sdk/tests/cocos/globals.test.ts
index 4f382276e..e5b6b9e5e 100644
--- a/sdk/tests/cocos/globals.test.ts
+++ b/sdk/tests/cocos/globals.test.ts
@@ -6,7 +6,7 @@
*/
import { beforeEach, describe, expect, it } from 'vitest';
import {
- Game, KeyCode, Scheduler, Asset, director, reportFrame, Layers, Prefab, Size, View, assetManager, game, macro, resources, screen, setResourceManifest, setVisibleSize, sys, view,
+ Component, EventHandler, Game, KeyCode, Scheduler, Asset, director, reportFrame, Layers, Prefab, Size, View, assetManager, game, macro, resources, screen, setResourceManifest, setVisibleSize, sys, view,
providePrefab,
} from '../../src/cocos';
import { keyCodeOf } from '../../src/cocos/inputApi';
@@ -262,3 +262,28 @@ describe('Asset', () => {
expect(missing?.message).toMatch(/uuid nope/);
});
});
+
+describe('a directory with a file the layer cannot load', () => {
+ it('loads the rest: a directory of configs holds a readme too', async () => {
+ setResourceManifest({
+ resources: {
+ 'cfg/rules': { path: 'rules.esprefab', kind: 'prefab' },
+ 'cfg/README': { path: '', kind: 'md' },
+ },
+ });
+ providePrefab('rules.esprefab', { ref: 'rules.esprefab', scene: { version: 4, name: 'rules', entities: [] } as never, rootId: 0, missing: [], release() {} });
+ const result = await new Promise<{ err: Error | null; assets: unknown[] }>((resolve) =>
+ resources.loadDir('cfg', (err: Error | null, assets: unknown[]) => resolve({ err, assets })));
+ expect(result.err).toBeNull();
+ expect(result.assets).toHaveLength(1);
+ });
+});
+
+describe('Component.EventHandler', () => {
+ it('is the class scripts construct handlers with', () => {
+ const handler = new Component.EventHandler();
+ handler.handler = 'onTap';
+ expect(handler).toBeInstanceOf(EventHandler);
+ expect(handler.handler).toBe('onTap');
+ });
+});