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.

refactor(sdk): single-source component asset fields; delete AssetFieldRegistry

esengine7711568b8dc5
diff --git a/sdk/src/asset/AssetFieldRegistry.ts b/sdk/src/asset/AssetFieldRegistry.ts
deleted file mode 100644
index 0e275213c..000000000
--- a/sdk/src/asset/AssetFieldRegistry.ts
+++ /dev/null
@@ -1,111 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0
-// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-export type AssetFieldType = 'texture' | 'material' | 'font' | 'anim-clip' | 'audio' | 'video' | 'tilemap' | 'tileset' | 'timeline' | 'statemachine' | 'behaviortree' | 'animatorcontroller';
-
-export interface AssetFieldDescriptor {
- field: string;
- type: AssetFieldType;
-}
-
-export interface CompoundFieldDescriptor {
- type: string;
- fields: Record<string, string>;
-}
-
-interface RegistryEntry {
- fields: AssetFieldDescriptor[];
- compounds: CompoundFieldDescriptor[];
-}
-
-const registry_ = new Map<string, RegistryEntry>();
-
-function ensureEntry(componentType: string): RegistryEntry {
- let entry = registry_.get(componentType);
- if (!entry) {
- entry = { fields: [], compounds: [] };
- registry_.set(componentType, entry);
- }
- return entry;
-}
-
-export function registerAssetFields(componentType: string, fields: AssetFieldDescriptor[]): void {
- const entry = ensureEntry(componentType);
- entry.fields = fields;
-}
-
-export function registerCompoundAssetFields(componentType: string, compound: CompoundFieldDescriptor): void {
- const entry = ensureEntry(componentType);
- entry.compounds.push(compound);
-}
-
-export function getAssetFields(componentType: string): AssetFieldDescriptor[] {
- return registry_.get(componentType)?.fields ?? [];
-}
-
-export function getCompoundAssetFields(componentType: string): CompoundFieldDescriptor[] {
- return registry_.get(componentType)?.compounds ?? [];
-}
-
-export function getAssetFieldComponents(): string[] {
- return Array.from(registry_.keys());
-}
-
-export function clearAssetFieldRegistry(): void {
- registry_.clear();
-}
-
-export function initBuiltinAssetFields(): void {
- registerAssetFields('Sprite', [
- { field: 'texture', type: 'texture' },
- { field: 'material', type: 'material' },
- ]);
-
- registerCompoundAssetFields('SpineAnimation', {
- type: 'spine',
- fields: { skeleton: 'skeletonPath', atlas: 'atlasPath' },
- });
- registerAssetFields('SpineAnimation', [
- { field: 'material', type: 'material' },
- ]);
-
- registerAssetFields('BitmapText', [
- { field: 'font', type: 'font' },
- ]);
-
- registerAssetFields('UIVisual', [
- { field: 'texture', type: 'texture' },
- { field: 'material', type: 'material' },
- ]);
-
- registerAssetFields('SpriteAnimator', [
- { field: 'clip', type: 'anim-clip' },
- ]);
-
- registerAssetFields('AudioSource', [
- { field: 'clip', type: 'audio' },
- ]);
-
- registerAssetFields('Video', [
- { field: 'source', type: 'video' },
- ]);
-
- registerAssetFields('ParticleEmitter', [
- { field: 'texture', type: 'texture' },
- { field: 'material', type: 'material' },
- ]);
-
- registerAssetFields('Tilemap', [
- { field: 'source', type: 'tilemap' },
- ]);
-
- registerAssetFields('TilemapLayer', [
- { field: 'tileset', type: 'texture' },
- // The `.estileset` palette (out-of-band model field); preloaded so the sync can
- // resolve the render table + collision + animations live.
- { field: 'tilesetAsset', type: 'tileset' },
- ]);
-
- registerAssetFields('TimelinePlayer', [
- { field: 'timeline', type: 'timeline' },
- ]);
-}
diff --git a/sdk/src/asset/AssetPlugin.ts b/sdk/src/asset/AssetPlugin.ts
index 2042c948c..e59d0c8ce 100644
--- a/sdk/src/asset/AssetPlugin.ts
+++ b/sdk/src/asset/AssetPlugin.ts
@@ -5,7 +5,6 @@ import { defineResource } from '../resource';
import { Assets as AssetsClass } from './Assets';
import { HttpBackend } from './Backend';
import { transcoderFromModule, type BasisWasmModule } from './basisTranscoder';
-import { initBuiltinAssetFields } from './AssetFieldRegistry';
import { AssetRefCounter } from './AssetRefCounter';
import { Audio, type AudioAPI } from '../audio/Audio';
import { SpriteAnimation, type SpriteAnimationAPI } from '../animation/SpriteAnimator';
@@ -29,8 +28,6 @@ export class AssetPlugin implements Plugin {
return;
}
- initBuiltinAssetFields();
-
const assets = AssetsClass.create({
backend: new HttpBackend({ baseUrl: '' }),
module,
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.ts
index 2c1b63084..af028a708 100644
--- a/sdk/src/asset/Assets.ts
+++ b/sdk/src/asset/Assets.ts
@@ -30,8 +30,8 @@ import { AnimatorControllerAssetLoader } from './loaders/AnimatorControllerAsset
import { BtAssetLoader } from './loaders/BtAssetLoader';
import { LocaleAssetLoader } from './loaders/LocaleAssetLoader';
import type { SpineModuleController } from '../spine/SpineController';
-import { getAssetFields } from './AssetFieldRegistry';
import { getComponentDefaults } from '../component';
+import { getComponentAssetFieldDescriptors } from '../scene';
import { discoverSceneAssets } from './discoverAssets';
import { fetchDecodePixels } from './imageDecode';
import type { SceneData } from '../scene';
@@ -814,7 +814,11 @@ export class Assets {
for (const entity of sceneData.entities) {
for (const comp of entity.components) {
- const fields = getAssetFields(comp.type);
+ // Single source: the component def's declared asset fields.
+ // Handle write-back always keys off assetFields (the direct
+ // texture/material/font members), even for components whose
+ // discovery is driven by a discoverAssets callback instead.
+ const fields = getComponentAssetFieldDescriptors(comp.type);
for (const { field, type } of fields) {
const ref = comp.data[field];
if (typeof ref !== 'string' || !ref) continue;
diff --git a/sdk/src/asset/index.ts b/sdk/src/asset/index.ts
index 1b61de281..244582407 100644
--- a/sdk/src/asset/index.ts
+++ b/sdk/src/asset/index.ts
@@ -32,9 +32,3 @@ export {
export { type AssetLoader, type LoadContext, type TextureResult, type TextureResult as TextureInfo, type SpineResult, type SpineLoadResult, type MaterialResult, type FontResult, type AudioResult, type LocaleResult } from './AssetLoader';
export { TextureLoader, type TextureImportSettings, type TextureImportSettingsResolver } from './loaders/TextureLoader';
export { SceneHandle } from './SceneHandle';
-export {
- registerAssetFields, registerCompoundAssetFields,
- getAssetFields, getCompoundAssetFields,
- initBuiltinAssetFields,
- type AssetFieldType, type AssetFieldDescriptor, type CompoundFieldDescriptor,
-} from './AssetFieldRegistry';
diff --git a/sdk/src/runtimeLoader.ts b/sdk/src/runtimeLoader.ts
index 530ac5600..c7da5c104 100644
--- a/sdk/src/runtimeLoader.ts
+++ b/sdk/src/runtimeLoader.ts
@@ -19,7 +19,6 @@ import { SpinePlugin } from './spine/SpinePlugin';
import type { App } from './app';
import { Assets as AssetsClass } from './asset/Assets';
import { Assets as AssetsResource } from './asset/AssetPlugin';
-import { initBuiltinAssetFields } from './asset/AssetFieldRegistry';
import { transcoderFromModule, type BasisWasmModule } from './asset/basisTranscoder';
import type { TextureImportSettings } from './asset/loaders/TextureLoader';
import { SceneManager, type SceneConfig } from './sceneManager';
@@ -84,7 +83,6 @@ function ensureRuntimeAssets(
if (runtimeImportSettings.has(existing)) return existing;
}
- initBuiltinAssetFields();
const assets = AssetsClass.create({
backend: source.backend,
module,
diff --git a/sdk/tests/asset-field-registry.test.ts b/sdk/tests/asset-field-registry.test.ts
deleted file mode 100644
index d965868f4..000000000
--- a/sdk/tests/asset-field-registry.test.ts
+++ /dev/null
@@ -1,90 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0
-// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-import { describe, it, expect, beforeEach } from 'vitest';
-import {
- registerAssetFields,
- registerCompoundAssetFields,
- getAssetFields,
- getCompoundAssetFields,
- getAssetFieldComponents,
- clearAssetFieldRegistry,
- initBuiltinAssetFields,
-} from '../src/asset/AssetFieldRegistry';
-
-describe('AssetFieldRegistry', () => {
- beforeEach(() => {
- clearAssetFieldRegistry();
- });
-
- it('registers and retrieves single fields', () => {
- registerAssetFields('Sprite', [
- { field: 'texture', type: 'texture' },
- { field: 'material', type: 'material' },
- ]);
-
- const fields = getAssetFields('Sprite');
- expect(fields).toHaveLength(2);
- expect(fields[0]).toEqual({ field: 'texture', type: 'texture' });
- expect(fields[1]).toEqual({ field: 'material', type: 'material' });
- });
-
- it('registers and retrieves compound fields', () => {
- registerCompoundAssetFields('SpineAnimation', {
- type: 'spine',
- fields: { skeleton: 'skeletonPath', atlas: 'atlasPath' },
- });
-
- const compounds = getCompoundAssetFields('SpineAnimation');
- expect(compounds).toHaveLength(1);
- expect(compounds[0].type).toBe('spine');
- expect(compounds[0].fields).toEqual({ skeleton: 'skeletonPath', atlas: 'atlasPath' });
- });
-
- it('returns empty arrays for unregistered components', () => {
- expect(getAssetFields('Unknown')).toEqual([]);
- expect(getCompoundAssetFields('Unknown')).toEqual([]);
- });
-
- it('lists all registered components', () => {
- registerAssetFields('Sprite', [{ field: 'texture', type: 'texture' }]);
- registerAssetFields('Image', [{ field: 'texture', type: 'texture' }]);
-
- const components = getAssetFieldComponents();
- expect(components).toContain('Sprite');
- expect(components).toContain('Image');
- });
-
- it('initBuiltinAssetFields registers all known components', () => {
- initBuiltinAssetFields();
-
- const components = getAssetFieldComponents();
- expect(components).toContain('Sprite');
- expect(components).toContain('SpineAnimation');
- expect(components).toContain('BitmapText');
- expect(components).toContain('UIVisual');
- expect(components).toContain('SpriteAnimator');
- expect(components).toContain('AudioSource');
- expect(components).toContain('ParticleEmitter');
- expect(components).toContain('Tilemap');
- expect(components).toContain('TilemapLayer');
- expect(components).toContain('TimelinePlayer');
-
- const spineCompounds = getCompoundAssetFields('SpineAnimation');
- expect(spineCompounds).toHaveLength(1);
- expect(spineCompounds[0].type).toBe('spine');
-
- const spineSingleFields = getAssetFields('SpineAnimation');
- expect(spineSingleFields).toEqual([{ field: 'material', type: 'material' }]);
- });
-
- it('can register both fields and compounds for same component', () => {
- registerAssetFields('MyComponent', [{ field: 'texture', type: 'texture' }]);
- registerCompoundAssetFields('MyComponent', {
- type: 'custom',
- fields: { a: 'fieldA', b: 'fieldB' },
- });
-
- expect(getAssetFields('MyComponent')).toHaveLength(1);
- expect(getCompoundAssetFields('MyComponent')).toHaveLength(1);
- });
-});
diff --git a/sdk/tests/asset-refcounter-integration.test.ts b/sdk/tests/asset-refcounter-integration.test.ts
index 45df58bb0..54175085b 100644
--- a/sdk/tests/asset-refcounter-integration.test.ts
+++ b/sdk/tests/asset-refcounter-integration.test.ts
@@ -13,7 +13,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import { defineComponent, clearUserComponents } from '../src/component';
import { Assets } from '../src/asset/Assets';
import { AssetRefCounter } from '../src/asset/AssetRefCounter';
-import { registerAssetFields, clearAssetFieldRegistry } from '../src/asset/AssetFieldRegistry';
import type { Backend } from '../src/asset/Backend';
import type { SceneData } from '../src/scene';
import type { SceneAssetResult } from '../src/asset/Assets';
@@ -61,20 +60,21 @@ function emptyResult(): SceneAssetResult {
beforeEach(() => {
clearUserComponents();
- clearAssetFieldRegistry();
+ // resolveSceneAssetPaths reads the asset fields declared on the component
+ // def itself (the single source) — no separate registry.
defineComponent(SPRITE, {
texture: '',
material: '',
color: { r: 1, g: 1, b: 1, a: 1 },
+ }, {
+ assetFields: [
+ { field: 'texture', type: 'texture' },
+ { field: 'material', type: 'material' },
+ ],
+ });
+ defineComponent(LABEL, { font: '', text: '' }, {
+ assetFields: [{ field: 'font', type: 'font' }],
});
- defineComponent(LABEL, { font: '', text: '' });
- // resolveSceneAssetPaths pulls fields from this registry, not from
- // defineComponent metadata.
- registerAssetFields(SPRITE, [
- { field: 'texture', type: 'texture' },
- { field: 'material', type: 'material' },
- ]);
- registerAssetFields(LABEL, [{ field: 'font', type: 'font' }]);
});
describe('Assets.resolveSceneAssetPaths → AssetRefCounter', () => {
diff --git a/sdk/tests/component-asset-fields.test.ts b/sdk/tests/component-asset-fields.test.ts
new file mode 100644
index 000000000..6ec2d91e6
--- /dev/null
+++ b/sdk/tests/component-asset-fields.test.ts
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+//
+// Single-source guard for component asset fields. The former AssetFieldRegistry
+// hand-mirrored this data in a second table; it was deleted and every consumer
+// (scene asset discovery + handle write-back) now reads the ONE source — each
+// ComponentDef's `assetFields` / `discoverAssets`. These assertions lock the
+// builtins that write-back depends on, so a C++ ES_PROPERTY change (or a TS def
+// edit) that drops a texture/material/font field fails here instead of silently
+// producing a white sprite at runtime.
+import { describe, it, expect } from 'vitest';
+import {
+ Sprite, BitmapText, SpineAnimation, ParticleEmitter, TilemapLayer,
+ getComponent,
+} from '../src/component';
+import { UIVisual } from '../src/ui/core/ui-visual';
+import { SpriteAnimator } from '../src/animation/SpriteAnimator';
+import { AudioSource } from '../src/audio/AudioComponents';
+import { Video } from '../src/video/VideoComponents';
+import { Tilemap } from '../src/tilemap/components';
+import { TimelinePlayer } from '../src/timeline/TimelinePlugin';
+
+describe('component asset fields (single source)', () => {
+ it('builtins carry the asset fields handle write-back keys off', () => {
+ expect(Sprite.assetFields).toEqual([
+ { field: 'texture', type: 'texture' },
+ { field: 'material', type: 'material' },
+ ]);
+ expect(UIVisual.assetFields).toEqual([
+ { field: 'texture', type: 'texture' },
+ { field: 'material', type: 'material' },
+ ]);
+ expect(BitmapText.assetFields).toEqual([{ field: 'font', type: 'font' }]);
+ expect(SpineAnimation.assetFields).toEqual([{ field: 'material', type: 'material' }]);
+ expect(ParticleEmitter.assetFields).toEqual([
+ { field: 'texture', type: 'texture' },
+ { field: 'material', type: 'material' },
+ ]);
+ });
+
+ it('TS components declare their asset fields on the def', () => {
+ expect(SpriteAnimator.assetFields).toEqual([{ field: 'clip', type: 'anim-clip' }]);
+ expect(AudioSource.assetFields).toEqual([{ field: 'clip', type: 'audio' }]);
+ expect(Video.assetFields).toEqual([{ field: 'source', type: 'video' }]);
+ expect(Tilemap.assetFields).toEqual([{ field: 'source', type: 'tilemap' }]);
+ expect(TimelinePlayer.assetFields).toEqual([{ field: 'timeline', type: 'timeline' }]);
+ });
+
+ it('TilemapLayer is the single-source model: assetFields for write-back, discoverAssets for the .estileset out-of-band ref', () => {
+ // Write-back only sees the copied atlas texture (a real ABI field).
+ expect(TilemapLayer.assetFields).toEqual([{ field: 'tileset', type: 'texture' }]);
+ // Discovery is authoritative via the callback, which also surfaces the
+ // out-of-band tileset ref that has no ABI field.
+ expect(typeof TilemapLayer.discoverAssets).toBe('function');
+ const refs = TilemapLayer.discoverAssets!({
+ tileset: 'atlas.png',
+ tilesetAsset: '@uuid:abc',
+ } as never);
+ expect(refs).toContainEqual({ type: 'texture', path: 'atlas.png' });
+ expect(refs).toContainEqual({ type: 'tileset', path: '@uuid:abc' });
+ });
+
+ it('the write-back source resolves through the component registry', () => {
+ expect(getComponent('Sprite')?.assetFields).toBe(Sprite.assetFields);
+ });
+});
diff --git a/sdk/tests/scene.test.ts b/sdk/tests/scene.test.ts
index 2c65921d4..ca654e5ac 100644
--- a/sdk/tests/scene.test.ts
+++ b/sdk/tests/scene.test.ts
@@ -25,11 +25,8 @@ import {
} from '../src/scene';
import type { Entity } from '../src/types';
import { discoverSceneAssets } from '../src/asset/discoverAssets';
-import { getAssetFields, initBuiltinAssetFields } from '../src/asset/AssetFieldRegistry';
import { initResourceManager, shutdownResourceManager } from '../src/resourceManager';
-initBuiltinAssetFields();
-
const Transform = defineBuiltin('Transform', {
position: { x: 0, y: 0, z: 0 },
rotation: { w: 1, x: 0, y: 0, z: 0 },
@@ -657,7 +654,7 @@ describe('Scene', () => {
(sceneData: SceneData, result: { textureHandles: Map<string, number>; materialHandles: Map<string, number>; fontHandles: Map<string, number> }) => {
for (const entity of sceneData.entities) {
for (const comp of entity.components) {
- const fields = getAssetFields(comp.type) as { field: string; type: string }[];
+ const fields = getComponentAssetFieldDescriptors(comp.type);
for (const { field, type } of fields) {
const value = comp.data[field];
if (typeof value !== 'string' || !value) continue;