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 loaded asset is named for its file, and what the layer kept of a play session is dropped when the next begins

yhha8c4a5034704
diff --git a/sdk/src/cocos/assetManager.ts b/sdk/src/cocos/assetManager.ts
index 9b0b0d5f1..5ea8f29da 100644
--- a/sdk/src/cocos/assetManager.ts
+++ b/sdk/src/cocos/assetManager.ts
@@ -19,7 +19,7 @@ import { platformFetch } from '../platform';
import { log } from '../util/logger';
import { Asset, JsonAsset, Material, Prefab, TextAsset } from './assets';
import { getCurrentWorld } from './node';
-import { assetsFor, loadPrefab } from './prefabs';
+import { assetsFor, loadPrefab, onNewSession } from './prefabs';
export interface ResourceEntry {
/** The project-relative path of the file. */
@@ -91,6 +91,14 @@ function parseLoadArgs<T>(args: unknown[]): { type: AssetCtor | null; onProgress
}
const loaded = new Cache<Asset>();
+// What the layer loaded belongs to the session that loaded it.
+onNewSession(() => { loaded.clear(); manifest = null; manifestLoad = null; });
+
+/** An asset is named for its file, without the extension: the name a script keys what it loads by (`asset.name`). */
+function named<T extends Asset>(asset: T, entry: ResourceEntry): T {
+ if (!asset.name) asset.name = (entry.path.split('/').pop() ?? '').replace(/\.[^.]+$/, '');
+ return 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 {}
@@ -148,7 +156,7 @@ export class Bundle {
const entry = await this.entry_(list[i]!);
const key = `${this.name}/${entry.path}`;
let asset = loaded.get(key);
- if (!asset) loaded.add(key, (asset = await loadEntry(entry, type)));
+ if (!asset) loaded.add(key, (asset = named(await loadEntry(entry, type), entry)));
out.push(asset);
onProgress?.(i + 1, list.length, entry);
}
@@ -172,7 +180,7 @@ export class Bundle {
try {
const key = `${this.name}/${entry.path}`;
let asset = loaded.get(key);
- if (!asset) loaded.add(key, (asset = await loadEntry(entry, type)));
+ if (!asset) loaded.add(key, (asset = named(await loadEntry(entry, type), entry)));
out.push(asset);
} catch (err) {
// 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
diff --git a/sdk/src/cocos/prefabs.ts b/sdk/src/cocos/prefabs.ts
index 823b1c1d1..739911fe5 100644
--- a/sdk/src/cocos/prefabs.ts
+++ b/sdk/src/cocos/prefabs.ts
@@ -28,6 +28,14 @@ import { Node, getCurrentWorld, markDetached } from './node';
import { Prefab, preparedPrefabs } from './assets';
const assetsOf = new WeakMap<World, AssetsClass>();
+let currentAssets: AssetsClass | null = null;
+const sessionListeners: Array<() => void> = [];
+
+/**
+ * Run @p fn when a play session ends and another begins: the engine releases the session's resources, so what the layer kept of
+ * them (a prepared prefab, a loaded asset) names things that are gone. Told as the new session's asset loader is first seen.
+ */
+export function onNewSession(fn: () => void): void { sessionListeners.push(fn); }
const wanted = new Set<string>();
const inFlight = new Set<string>();
@@ -69,6 +77,13 @@ export function declarePrefabSystem(): void {
[Res(Assets), GetWorld()],
(assets, world) => {
if (!assets) return;
+ if (currentAssets && currentAssets !== assets) {
+ preparedPrefabs.clear();
+ wanted.clear();
+ inFlight.clear();
+ for (const fn of sessionListeners) fn();
+ }
+ currentAssets = assets as AssetsClass;
assetsOf.set(world, assets as AssetsClass);
if (wanted.size > 0) drain(assets as AssetsClass);
},
diff --git a/sdk/tests/cocos/globals.test.ts b/sdk/tests/cocos/globals.test.ts
index e5b6b9e5e..82e268534 100644
--- a/sdk/tests/cocos/globals.test.ts
+++ b/sdk/tests/cocos/globals.test.ts
@@ -276,6 +276,8 @@ describe('a directory with a file the layer cannot load', () => {
resources.loadDir('cfg', (err: Error | null, assets: unknown[]) => resolve({ err, assets })));
expect(result.err).toBeNull();
expect(result.assets).toHaveLength(1);
+ // named for its file, as a script keys what it loads
+ expect((result.assets[0] as { name: string }).name).toBe('rules');
});
});