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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(sdk): a subsystem installs one plugin, however many callers registered it
esengine659cdfde0c5b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6db8b2c0c..8c4bd0e38 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -23,6 +23,12 @@ published separately; it ships inside the editor.### Fixed+- **A build that ships Spine or DragonBones installs one of each again, not two.** Making+ registration a call rather than a module's side effect gave it two callers — the+ `esengine/spine` subpath and an entry's own install — and the plugin list appended both,+ so the editor built every app with two SpinePlugins and said so once in a log nobody+ reads. The list is keyed by subsystem now, so a second registration replaces.+- **An optional subsystem is installed again in builds where a bundler had dropped it.**Making the core runtime name no optionalsubsystem left each one registering itself when its module was imported — and a barediff --git a/sdk/src/dragonbones/support.ts b/sdk/src/dragonbones/support.tsindex 59aaabca6..3372f7924 100644--- a/sdk/src/dragonbones/support.ts+++ b/sdk/src/dragonbones/support.ts@@ -8,7 +8,7 @@ import { DragonBonesPlugin as DragonBonesPluginCtor } from './DragonBonesPlugin'import { loadDragonBonesAssets, applyDragonBonesEntities } from './loadDragonBonesScene';export function registerDragonBonesSupport(): void {- addEntryPlugin(() => new DragonBonesPluginCtor());+ addEntryPlugin('dragonbones', () => new DragonBonesPluginCtor());setSceneOptionals({dragonBones: {acquire: async (app) => (await app.getPlugin(DragonBonesPluginCtor)?.acquire()) ?? null,diff --git a/sdk/src/runtime/entryPlugins.ts b/sdk/src/runtime/entryPlugins.tsindex 9c51c1b83..9011e29a3 100644--- a/sdk/src/runtime/entryPlugins.ts+++ b/sdk/src/runtime/entryPlugins.ts@@ -7,20 +7,27 @@* Its own module because `webAppFactory` is re-exported wholesale by every SDK* entry: a setter living there would be public API, and this is a seam between* an entry and the factory, not something a game calls.+ *+ * Keyed, not appended. The list was an array, safe because the only caller was a+ * module's top-level side effect and ESM runs a module once. Registration is a+ * CALL now — `esengine/spine` makes it and so does an entry's+ * `installOptionalPlugins()` — and the editor does both, so every app it built+ * carried two SpinePlugins. One subsystem, one maker, held here rather than in+ * each caller's memory of whether it has run.*/import type { Plugin } from '../app/app';-// Additive, not a single setter: each optional subsystem installs itself when-// its subpath is imported, and a setter would mean the last one imported was the-// only one installed. ESM runs a module once, so a double import adds nothing.-const makers: Array<() => Plugin> = [];+const makers = new Map<string, () => Plugin>();-/** Called by an optional subsystem's entry, for its side effect. */-export function addEntryPlugin(make: () => Plugin): void {- makers.push(make);+/**+ * Called by an optional subsystem's support module. `id` is the subsystem, so a+ * second registration of the same one replaces rather than adds.+ */+export function addEntryPlugin(id: string, make: () => Plugin): void {+ makers.set(id, make);}/** Fresh instances per app: a plugin carries per-app state once built. */export function entryPlugins(): Plugin[] {- return makers.map((make) => make());+ return [...makers.values()].map((make) => make());}diff --git a/sdk/src/spine/support.ts b/sdk/src/spine/support.tsindex f099ff606..ed09608d5 100644--- a/sdk/src/spine/support.ts+++ b/sdk/src/spine/support.ts@@ -19,10 +19,10 @@ import { setSceneOptionals } from '../runtime/sceneOptionals';import { SpinePlugin as SpinePluginCtor } from './SpinePlugin';import { loadSpineAssets, applySpineEntities } from './loadSpineScene';-/** Idempotent: both the `esengine/spine` subpath and an entry that ships- * everything call it, and a host may evaluate the bundle more than once. */+/** Called by both the `esengine/spine` subpath and an entry that ships+ * everything; `addEntryPlugin` keys by subsystem so the second call replaces. */export function registerSpineSupport(): void {- addEntryPlugin(() => new SpinePluginCtor());+ addEntryPlugin('spine', () => new SpinePluginCtor());setSceneOptionals({spine: {manager: (app) => app.getPlugin(SpinePluginCtor)?.spineManager ?? null,diff --git a/sdk/tests/entry-plugins-once.test.ts b/sdk/tests/entry-plugins-once.test.tsnew file mode 100644index 000000000..1249aae81--- /dev/null+++ b/sdk/tests/entry-plugins-once.test.ts@@ -0,0 +1,25 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A subsystem installs one plugin however many times it is registered.+ *+ * Both `esengine/spine` and an entry's `installOptionalPlugins()` register it,+ * and a host may evaluate the bundle again — so the count must not follow the+ * number of callers.+ */+import { describe, it, expect } from 'vitest';+import { installOptionalPlugins } from '../src/runtime/optionalPlugins';+import '../src/spine';+import '../src/dragonbones';+import { entryPlugins } from '../src/runtime/entryPlugins';++describe('the plugins an entry installs', () => {+ it('names each subsystem once, however many callers registered it', () => {+ installOptionalPlugins();+ // Twice more: a host may evaluate the bundle again, and nothing should+ // accumulate when it does.+ installOptionalPlugins();+ const names = entryPlugins().map((p) => p.name);+ expect([...new Set(names)]).toEqual(names);+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 412231d51..157f339e3 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,9 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(sdk): a subsystem installs one plugin, however many callers registered it':+ { note: 'A build that ships Spine or DragonBones installs one of each again, not two.' },+'fix(scale): the frame ceilings are set from both machines, not one':{ internal: 'A repository perf budget; no creator runs it.' },