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(animation): a clip or controller comes from the realm that loaded it

esengine8741961709d2
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 564e9db7d..fbf8c53ae 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -404,6 +404,7 @@ ResourceDef<AnimatorControllerAPI>
## AnimatorControllerAPI — class @experimental
```
+@internal useAssetControllers: (source: (ref: string) => AnimatorControllerDef | undefined) => void
clearControllers: () => void
getBool: (entity: Entity, name: string) => boolean
getController: (name: string) => AnimatorControllerDef | undefined
@@ -416,7 +417,7 @@ setFloat: (entity: Entity, name: string, value: number) => void
setSpineDriver: (driver: SpineAnimationDriver | null) => void
setTrigger: (entity: Entity, name: string) => void
unregisterController: (name: string) => void
-update: (world: World, resolveKey?: (ref: string) => string) => void
+update: (world: World) => void
static new (): AnimatorControllerAPI
static prototype: AnimatorControllerAPI
```
@@ -7471,6 +7472,7 @@ ResourceDef<SpriteAnimationAPI>
## SpriteAnimationAPI — class @experimental
```
+@internal useAssetClips: (source: (ref: string) => SpriteAnimClip | undefined) => void
aliasClip: (name: string, clip: SpriteAnimClip) => void
clearClips: () => void
getClip: (name: string) => SpriteAnimClip | undefined
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 32b57e436..19ca74b91 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -404,6 +404,7 @@ ResourceDef<AnimatorControllerAPI>
## AnimatorControllerAPI — class @experimental
```
+@internal useAssetControllers: (source: (ref: string) => AnimatorControllerDef | undefined) => void
clearControllers: () => void
getBool: (entity: Entity, name: string) => boolean
getController: (name: string) => AnimatorControllerDef | undefined
@@ -416,7 +417,7 @@ setFloat: (entity: Entity, name: string, value: number) => void
setSpineDriver: (driver: SpineAnimationDriver | null) => void
setTrigger: (entity: Entity, name: string) => void
unregisterController: (name: string) => void
-update: (world: World, resolveKey?: (ref: string) => string) => void
+update: (world: World) => void
static new (): AnimatorControllerAPI
static prototype: AnimatorControllerAPI
```
@@ -7664,6 +7665,7 @@ ResourceDef<SpriteAnimationAPI>
## SpriteAnimationAPI — class @experimental
```
+@internal useAssetClips: (source: (ref: string) => SpriteAnimClip | undefined) => void
aliasClip: (name: string, clip: SpriteAnimClip) => void
clearClips: () => void
getClip: (name: string) => SpriteAnimClip | undefined
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 7468448aa..1658dcb39 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -399,6 +399,7 @@ ResourceDef<AnimatorControllerAPI>
## AnimatorControllerAPI — class @experimental
```
+@internal useAssetControllers: (source: (ref: string) => AnimatorControllerDef | undefined) => void
clearControllers: () => void
getBool: (entity: Entity, name: string) => boolean
getController: (name: string) => AnimatorControllerDef | undefined
@@ -411,7 +412,7 @@ setFloat: (entity: Entity, name: string, value: number) => void
setSpineDriver: (driver: SpineAnimationDriver | null) => void
setTrigger: (entity: Entity, name: string) => void
unregisterController: (name: string) => void
-update: (world: World, resolveKey?: (ref: string) => string) => void
+update: (world: World) => void
static new (): AnimatorControllerAPI
static prototype: AnimatorControllerAPI
```
@@ -7466,6 +7467,7 @@ ResourceDef<SpriteAnimationAPI>
## SpriteAnimationAPI — class @experimental
```
+@internal useAssetClips: (source: (ref: string) => SpriteAnimClip | undefined) => void
aliasClip: (name: string, clip: SpriteAnimClip) => void
clearClips: () => void
getClip: (name: string) => SpriteAnimClip | undefined
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index c17c4f49e..9693c104a 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -399,6 +399,7 @@ ResourceDef<AnimatorControllerAPI>
## AnimatorControllerAPI — class @experimental
```
+@internal useAssetControllers: (source: (ref: string) => AnimatorControllerDef | undefined) => void
clearControllers: () => void
getBool: (entity: Entity, name: string) => boolean
getController: (name: string) => AnimatorControllerDef | undefined
@@ -411,7 +412,7 @@ setFloat: (entity: Entity, name: string, value: number) => void
setSpineDriver: (driver: SpineAnimationDriver | null) => void
setTrigger: (entity: Entity, name: string) => void
unregisterController: (name: string) => void
-update: (world: World, resolveKey?: (ref: string) => string) => void
+update: (world: World) => void
static new (): AnimatorControllerAPI
static prototype: AnimatorControllerAPI
```
@@ -7712,6 +7713,7 @@ ResourceDef<SpriteAnimationAPI>
## SpriteAnimationAPI — class @experimental
```
+@internal useAssetClips: (source: (ref: string) => SpriteAnimClip | undefined) => void
aliasClip: (name: string, clip: SpriteAnimClip) => void
clearClips: () => void
getClip: (name: string) => SpriteAnimClip | undefined
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index e017abaa9..b2a745461 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -399,6 +399,7 @@ ResourceDef<AnimatorControllerAPI>
## AnimatorControllerAPI — class @experimental
```
+@internal useAssetControllers: (source: (ref: string) => AnimatorControllerDef | undefined) => void
clearControllers: () => void
getBool: (entity: Entity, name: string) => boolean
getController: (name: string) => AnimatorControllerDef | undefined
@@ -411,7 +412,7 @@ setFloat: (entity: Entity, name: string, value: number) => void
setSpineDriver: (driver: SpineAnimationDriver | null) => void
setTrigger: (entity: Entity, name: string) => void
unregisterController: (name: string) => void
-update: (world: World, resolveKey?: (ref: string) => string) => void
+update: (world: World) => void
static new (): AnimatorControllerAPI
static prototype: AnimatorControllerAPI
```
@@ -7473,6 +7474,7 @@ ResourceDef<SpriteAnimationAPI>
## SpriteAnimationAPI — class @experimental
```
+@internal useAssetClips: (source: (ref: string) => SpriteAnimClip | undefined) => void
aliasClip: (name: string, clip: SpriteAnimClip) => void
clearClips: () => void
getClip: (name: string) => SpriteAnimClip | undefined
diff --git a/sdk/src/animation/AnimationPlugin.ts b/sdk/src/animation/AnimationPlugin.ts
index ae2d7205b..417825e16 100644
--- a/sdk/src/animation/AnimationPlugin.ts
+++ b/sdk/src/animation/AnimationPlugin.ts
@@ -15,10 +15,9 @@ import { engineApi } from '../ecs/bridge/engineApi';
import { log } from '../util/logger';
import type { AnimCore } from './Tween';
import { Tween, TweenAPI } from './Tween';
-import { SpriteAnimation, SpriteAnimationAPI } from './SpriteAnimator';
-import { AnimatorController, AnimatorControllerAPI } from './Animator';
-import { Assets } from '../asset/AssetPlugin';
-import { resolveAssetKey } from '../asset/resolveAssetKey';
+import { SpriteAnimation, SpriteAnimationAPI, type SpriteAnimClip } from './SpriteAnimator';
+import { AnimatorController, AnimatorControllerAPI, type AnimatorControllerDef } from './Animator';
+import { appRegistryAsset } from '../asset/registryLookup';
import { playModeOnly } from '../ecs/env';
import { SystemLabel } from '../ecs/systemLabels';
@@ -57,19 +56,20 @@ export class AnimationPlugin implements Plugin {
{ name: 'TweenSystem' }
), { runIf: playModeOnly });
- // Resolve a `.esanimator` controller ref to the load path its loader keyed
- // the store with (an `estella://…` URL in the play realm), matching the FSM
- // plugin — without it, an Animator referencing a controller BY PATH resolves
- // to nothing at runtime and silently never seeds a state.
- const resolveKey = (ref: string): string =>
- resolveAssetKey(app.hasResource(Assets) ? app.getResource(Assets) : null, ref);
+ // A `.esanim` / `.esanimator` comes from THIS app's realm, whose slots
+ // answer to the authored ref as well as the resolved path. A code
+ // registration still wins over one.
+ anim.useAssetClips((ref) => appRegistryAsset<SpriteAnimClip>(app, 'anim-clip', ref));
+ animator.useAssetControllers(
+ (ref) => appRegistryAsset<AnimatorControllerDef>(app, 'animatorcontroller', ref),
+ );
// The state machine runs before the sprite animator so a transition's
// clip switch applies the same frame it fires.
app.addSystemToSchedule(Schedule.Update, defineSystem(
[Res(AnimatorController)],
(ctrl: AnimatorControllerAPI) => {
- ctrl.update(world, resolveKey);
+ ctrl.update(world);
},
{ name: 'AnimatorSystem' }
), { runAfter: [SystemLabel.Tween], runIf: playModeOnly });
diff --git a/sdk/src/animation/Animator.ts b/sdk/src/animation/Animator.ts
index 5ab841611..35052023a 100644
--- a/sdk/src/animation/Animator.ts
+++ b/sdk/src/animation/Animator.ts
@@ -416,6 +416,7 @@ export function clearAnimatorControllerStore(): void {
* resource; read it as `app.getResource(AnimatorController)`.
*/
export class AnimatorControllerAPI {
+ private assetControllers_: ((ref: string) => AnimatorControllerDef | undefined) | null = null;
private readonly controllers = new Map<string, AnimatorControllerDef>();
private readonly params = new Map<Entity, Map<string, number | boolean>>();
private readonly triggers = new Map<Entity, Set<string>>();
@@ -437,10 +438,20 @@ export class AnimatorControllerAPI {
this.controllers.delete(name);
}
+ /**
+ * @internal Where a `.esanimator` comes from: this app's realm. Set by the
+ * animation plugin; without one only code registrations answer.
+ */
+ useAssetControllers(source: (ref: string) => AnimatorControllerDef | undefined): void {
+ this.assetControllers_ = source;
+ }
+
getController(name: string): AnimatorControllerDef | undefined {
- // A code-registered name wins; otherwise fall back to a controller the
- // asset loader registered under its `.esanimator` path.
- return this.controllers.get(name) ?? getRegisteredAnimatorController(name);
+ // A code-registered name wins, then this realm's asset, then a
+ // controller registered by path through the module door.
+ return this.controllers.get(name)
+ ?? this.assetControllers_?.(name)
+ ?? getRegisteredAnimatorController(name);
}
clearControllers(): void {
@@ -493,19 +504,13 @@ export class AnimatorControllerAPI {
// -- per-frame system -----------------------------------------------------
- update(world: World, resolveKey?: (ref: string) => string): void {
+ update(world: World): void {
const entities = world.getEntitiesWithComponents([Animator]);
for (const entity of entities) {
const a = world.get(entity, Animator) as AnimatorData;
if (!a.enabled) continue;
- // A code-registered NAME wins; otherwise the controller was loaded from
- // a `.esanimator` asset and lives in the path store under its RESOLVED
- // load path (e.g. `estella://…` in the play realm). Resolve the ref the
- // same way the loader keyed it before falling back to the raw ref.
- const def = this.controllers.get(a.controller)
- ?? getRegisteredAnimatorController(resolveKey ? resolveKey(a.controller) : a.controller)
- ?? getRegisteredAnimatorController(a.controller);
+ const def = this.getController(a.controller);
if (!def || def.states.length === 0) continue;
// Seed / repair the active state path. The path descends into a
diff --git a/sdk/src/animation/SpriteAnimator.ts b/sdk/src/animation/SpriteAnimator.ts
index c09c78df5..e570c2f4d 100644
--- a/sdk/src/animation/SpriteAnimator.ts
+++ b/sdk/src/animation/SpriteAnimator.ts
@@ -72,6 +72,7 @@ export function shouldFireEvent(eventFrame: number, prevFrame: number, newFrame:
*/
export class SpriteAnimationAPI {
private readonly clips = new Map<string, SpriteAnimClip>();
+ private assetClips_: ((ref: string) => SpriteAnimClip | undefined) | null = null;
private readonly entityListeners = new Map<Entity, SpriteAnimEventHandler[]>();
private readonly globalListeners: SpriteAnimEventHandler[] = [];
@@ -96,8 +97,17 @@ export class SpriteAnimationAPI {
this.clips.delete(name);
}
+ /**
+ * @internal Where a clip loaded from a `.esanim` comes from: this app's
+ * realm. A code registration wins, as it always did — what changes is that
+ * the asset half is no longer a second copy kept in the map above.
+ */
+ useAssetClips(source: (ref: string) => SpriteAnimClip | undefined): void {
+ this.assetClips_ = source;
+ }
+
getClip(name: string): SpriteAnimClip | undefined {
- return this.clips.get(name);
+ return this.clips.get(name) ?? this.assetClips_?.(name);
}
clearClips(): void {
@@ -162,7 +172,7 @@ export class SpriteAnimationAPI {
const animator = world.get(entity, SpriteAnimator) as SpriteAnimatorData;
if (!animator.enabled || !animator.playing || !animator.clip) continue;
- const clip = this.clips.get(animator.clip);
+ const clip = this.getClip(animator.clip);
if (!clip || clip.frames.length === 0) continue;
// playing raised on a finished one-shot = replay from the top
@@ -244,7 +254,7 @@ export class SpriteAnimationAPI {
// -- goto frame / label ---------------------------------------------------
gotoFrame(animator: SpriteAnimatorData, frameIndex: number, andPlay: boolean = true): void {
- const clip = this.clips.get(animator.clip);
+ const clip = this.getClip(animator.clip);
if (!clip || clip.frames.length === 0) return;
animator.currentFrame = Math.max(0, Math.min(frameIndex, clip.frames.length - 1));
@@ -254,7 +264,7 @@ export class SpriteAnimationAPI {
}
gotoLabel(animator: SpriteAnimatorData, label: string, andPlay: boolean = true): void {
- const clip = this.clips.get(animator.clip);
+ const clip = this.getClip(animator.clip);
if (!clip || !clip.labels) return;
const frameIndex = clip.labels[label];
diff --git a/sdk/src/asset/loaders/AnimClipAssetLoader.ts b/sdk/src/asset/loaders/AnimClipAssetLoader.ts
index a3313dae4..5679e82b7 100644
--- a/sdk/src/asset/loaders/AnimClipAssetLoader.ts
+++ b/sdk/src/asset/loaders/AnimClipAssetLoader.ts
@@ -13,7 +13,6 @@ import type { AssetLoader, LoadContext, AnimClipResult, RegistryAssetLoader } fr
import type { RegistryEra } from '../registryAssets';
import { AssetScope } from '../AssetLease';
import { extractAnimClipTexturePaths, parseAnimClipAsset, parseAnimClipData } from '../../animation/AnimClipLoader';
-import type { SpriteAnimClip } from '../../animation/SpriteAnimator';
import { log } from '../../util/logger';
export class AnimClipAssetLoader implements AssetLoader<AnimClipResult> {
@@ -43,16 +42,10 @@ export class AnimClipAssetLoader implements AssetLoader<AnimClipResult> {
dependencies,
};
},
- publish: (names, published, ctx) => {
- const anim = ctx.getSpriteAnimation();
- for (const name of names) anim?.aliasClip(name, published as SpriteAnimClip);
- },
- unpublish: (names, published, ctx) => {
- const anim = ctx.getSpriteAnimation();
- if (!anim) return;
- for (const name of names) {
- if (anim.getClip(name) === published) anim.unregisterClip(name);
- }
- },
+ // Nothing: the slot holds the era, and this app's sprite animation
+ // reads it there. A clip registered into the API as well would be a
+ // second copy of "which clip is walk.esanim now".
+ publish: () => {},
+ unpublish: () => {},
};
}
diff --git a/sdk/tests/realm-registry-isolation.test.ts b/sdk/tests/realm-registry-isolation.test.ts
index ff4bf61b6..bb15222fb 100644
--- a/sdk/tests/realm-registry-isolation.test.ts
+++ b/sdk/tests/realm-registry-isolation.test.ts
@@ -130,6 +130,23 @@ describe('two realms in one process do not share an asset', () => {
expect(b.assets.resolveRegistryAsset('timeline', 'cut/intro.estimeline')).toBeUndefined();
});
+ it('a code-registered controller still wins over the realm\'s asset', async () => {
+ // This round changes which realm answers, not who wins: a name the game
+ // registered in code is still the one an Animator gets.
+ const { AnimatorControllerAPI } = await import('../src/animation/Animator');
+ const CODE = { initialState: 'run', states: [{ name: 'run' }] };
+ const DISK = JSON.stringify({ initialState: 'idle', states: [{ name: 'idle' }] });
+ const a = realm(() => DISK);
+ await a.assets.acquireTyped('animatorcontroller', 'enemy.esanimator');
+
+ const ctrl = new AnimatorControllerAPI();
+ ctrl.useAssetControllers((ref) => a.assets.resolveRegistryAsset('animatorcontroller', ref));
+ expect(ctrl.getController('enemy.esanimator')?.initialState).toBe('idle');
+
+ ctrl.registerController('enemy.esanimator', CODE as never);
+ expect(ctrl.getController('enemy.esanimator')?.initialState).toBe('run');
+ });
+
it('a code registration has no realm, and every app still sees it', async () => {
// The other half of the rule: `registerFsm('patrol', …)` is not an asset.
const { registerFsm } = await import('../src/ai/fsm/StateMachineAgent');
diff --git a/sdk/tests/registry-asset-lifetime.test.ts b/sdk/tests/registry-asset-lifetime.test.ts
index 6bddbe4fb..23a458b6b 100644
--- a/sdk/tests/registry-asset-lifetime.test.ts
+++ b/sdk/tests/registry-asset-lifetime.test.ts
@@ -17,7 +17,7 @@ import { App } from '../src/app/app';
import { SceneManager, SceneManagerState } from '../src/scene/sceneManager';
import { Assets as AssetsResource } from '../src/asset/AssetPlugin';
import { AssetScope } from '../src/asset/AssetLease';
-import { SpriteAnimationAPI, SpriteAnimator } from '../src/animation/SpriteAnimator';
+import { SpriteAnimationAPI, SpriteAnimator, type SpriteAnimClip } from '../src/animation/SpriteAnimator';
import { connectFakeCpp } from './helpers/fakeEngine';
import { markEngineComponentBaseline, seedEngineComponents } from '../src/ecs/component';
import type { Entity } from '../src/types';
@@ -90,6 +90,9 @@ function buildAssets(sprites: SpriteAnimationAPI, doc: () => string) {
module: { _malloc: vi.fn(() => 0), _free: vi.fn(), HEAPU8: new Uint8Array(1 << 16), GL: null, FS: null } as never,
getSpriteAnimation: () => sprites,
} as never);
+ // What AnimationPlugin wires: a clip loaded from a `.esanim` comes from this
+ // app's realm, and `getClip` is where the two halves meet.
+ sprites.useAssetClips((ref) => assets.resolveRegistryAsset<SpriteAnimClip>('anim-clip', ref));
return assets;
}