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.

feat(sdk): the DragonBones plugin, acquired only if something asks for it

esengine1fcc8613c6a5
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index c91f59c3d..ad2421afb 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -6051,7 +6051,7 @@ acquire: (id: SideModuleId) => Promise<SideModule | null>
## SideModuleId — type
```
-'physics' | 'basis' | 'videodec' | `spine:${SpineVersion}`
+'physics' | 'basis' | 'videodec' | 'dragonbones' | `spine:${SpineVersion}`
```
## Signal — interface
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index f42cc8a20..90b39ea83 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -6215,7 +6215,7 @@ acquire: (id: SideModuleId) => Promise<SideModule | null>
## SideModuleId — type
```
-'physics' | 'basis' | 'videodec' | `spine:${SpineVersion}`
+'physics' | 'basis' | 'videodec' | 'dragonbones' | `spine:${SpineVersion}`
```
## Signal — interface
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 1a9370f97..e6aca2573 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -6046,7 +6046,7 @@ acquire: (id: SideModuleId) => Promise<SideModule | null>
## SideModuleId — type
```
-'physics' | 'basis' | 'videodec' | `spine:${SpineVersion}`
+'physics' | 'basis' | 'videodec' | 'dragonbones' | `spine:${SpineVersion}`
```
## Signal — interface
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 22d252efe..fa10a60cf 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -6261,7 +6261,7 @@ acquire: (id: SideModuleId) => Promise<SideModule | null>
## SideModuleId — type
```
-'physics' | 'basis' | 'videodec' | `spine:${SpineVersion}`
+'physics' | 'basis' | 'videodec' | 'dragonbones' | `spine:${SpineVersion}`
```
## Signal — interface
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index ffdc4d52a..e60f06812 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -6053,7 +6053,7 @@ acquire: (id: SideModuleId) => Promise<SideModule | null>
## SideModuleId — type
```
-'physics' | 'basis' | 'videodec' | `spine:${SpineVersion}`
+'physics' | 'basis' | 'videodec' | 'dragonbones' | `spine:${SpineVersion}`
```
## Signal — interface
diff --git a/sdk/src/dragonbones/DragonBonesPlugin.ts b/sdk/src/dragonbones/DragonBonesPlugin.ts
new file mode 100644
index 000000000..88b71f545
--- /dev/null
+++ b/sdk/src/dragonbones/DragonBonesPlugin.ts
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file dragonbones/DragonBonesPlugin.ts
+ * @brief Wires the DragonBones manager into an App: a resource to reach it by,
+ * a tick that advances it, a submit that draws it, and a teardown.
+ *
+ * @details The module is acquired lazily. Loading it costs a wasm instantiation,
+ * and a project with no DragonBones in it should not pay that — so
+ * nothing is fetched until something asks for the manager, and an app
+ * that never does never touches the side module at all.
+ */
+import type { App, Plugin } from '../app';
+import { Schedule } from '../system';
+import type { SystemDef } from '../system';
+import type { Entity } from '../types';
+import { defineResource, Time } from '../resource';
+import { engineApi } from '../ecs/engineApi';
+import { DragonBonesManager } from './DragonBonesManager';
+import { DragonBonesModuleController } from './DragonBonesController';
+import { wrapDragonBonesModule, type DragonBonesWasmModule } from './DragonBonesModuleLoader';
+import { log } from '../logger';
+
+/** Reach the manager from a system or from gameplay code. Null until acquired. */
+export const DragonBones = defineResource<DragonBonesManager | null>(null, 'DragonBones');
+
+export class DragonBonesPlugin implements Plugin {
+ name = 'dragonbones';
+ private manager_: DragonBonesManager | null;
+ private app_: App | null = null;
+ private despawnUnsub_: (() => void) | null = null;
+ private acquiring_: Promise<DragonBonesManager | null> | null = null;
+ private submitWired_ = false;
+
+ /** Pass a manager for headless use or tests; otherwise it is built on demand. */
+ constructor(manager?: DragonBonesManager) {
+ this.manager_ = manager ?? null;
+ }
+
+ get manager(): DragonBonesManager | null {
+ return this.manager_;
+ }
+
+ /**
+ * Load the side module and build the manager, once.
+ *
+ * Concurrent callers share the one in-flight promise: two entities spawning in
+ * the same frame must not instantiate two wasm modules and then disagree about
+ * which one owns their skeletons.
+ */
+ acquire(): Promise<DragonBonesManager | null> {
+ if (this.manager_) return Promise.resolve(this.manager_);
+ if (this.acquiring_) return this.acquiring_;
+
+ const app = this.app_;
+ const host = app?.sideModules;
+ if (!app || !host) return Promise.resolve(null);
+
+ this.acquiring_ = host.acquire('dragonbones')
+ .then(module => {
+ if (!module) {
+ log.warn('dragonbones', 'module unavailable in this realm');
+ return null;
+ }
+ const raw = module as unknown as DragonBonesWasmModule;
+ const manager = new DragonBonesManager(
+ new DragonBonesModuleController(raw, wrapDragonBonesModule(raw)));
+ this.manager_ = manager;
+ app.insertResource(DragonBones, manager);
+ return manager;
+ })
+ .catch((err: unknown) => {
+ log.warn('dragonbones', `module failed to load: ${String(err)}`);
+ return null;
+ })
+ .finally(() => {
+ this.acquiring_ = null;
+ });
+ return this.acquiring_;
+ }
+
+ build(app: App): void {
+ this.app_ = app;
+ app.insertResource(DragonBones, this.manager_);
+
+ this.despawnUnsub_ = app.world.onDespawn((entity: Entity) => {
+ this.manager_?.removeEntity(entity);
+ });
+
+ // Reads this.manager_ live rather than capturing it, so a manager that
+ // arrives later — which is the normal case, since acquisition is lazy —
+ // is picked up without registering a second callback that double-submits.
+ if (!this.submitWired_) {
+ this.submitWired_ = true;
+ const core = engineApi(app);
+ app.pipeline?.addPreFlushCallback(registry => {
+ if (core) this.manager_?.submitMeshes(core, registry._cpp);
+ });
+ }
+
+ const updateSystem: SystemDef = {
+ _id: Symbol('DragonBonesUpdateSystem'),
+ _name: 'DragonBonesUpdateSystem',
+ _params: [],
+ _fn: () => {
+ const time = app.getResource(Time);
+ this.manager_?.updateAnimations(time?.delta ?? 0);
+ },
+ };
+ // PreUpdate, like Spine: poses have to be current before anything that
+ // reads a bone position or a bounding box runs this frame.
+ app.addSystemToSchedule(Schedule.PreUpdate, updateSystem);
+ }
+
+ /**
+ * Drop the despawn subscription and free every skeleton the module holds.
+ * Without it a re-init leaves a listener pointing at a dead manager and the
+ * wasm side keeps armatures nothing can reach.
+ */
+ cleanup(): void {
+ this.despawnUnsub_?.();
+ this.despawnUnsub_ = null;
+ this.manager_?.dispose();
+ this.manager_ = null;
+ this.acquiring_ = null;
+ }
+}
+
+export const dragonBonesPlugin = (): DragonBonesPlugin => new DragonBonesPlugin();
diff --git a/sdk/src/dragonbones/index.ts b/sdk/src/dragonbones/index.ts
index bc307c1c2..b30f0232b 100644
--- a/sdk/src/dragonbones/index.ts
+++ b/sdk/src/dragonbones/index.ts
@@ -9,3 +9,4 @@ export type {
} from './DragonBonesModuleLoader';
export { DragonBonesManager } from './DragonBonesManager';
export type { DragonBonesEntityOptions } from './DragonBonesManager';
+export { DragonBonesPlugin, dragonBonesPlugin, DragonBones } from './DragonBonesPlugin';
diff --git a/sdk/src/sideModules/registry.ts b/sdk/src/sideModules/registry.ts
index 7de903cab..9b3209ca9 100644
--- a/sdk/src/sideModules/registry.ts
+++ b/sdk/src/sideModules/registry.ts
@@ -18,7 +18,7 @@
export type SpineVersion = '2.1' | '3.8' | '4.1' | '4.2' | '4.3';
/** Identifies an optional native module across every realm and transport. */
-export type SideModuleId = 'physics' | 'basis' | 'videodec' | `spine:${SpineVersion}`;
+export type SideModuleId = 'physics' | 'basis' | 'videodec' | 'dragonbones' | `spine:${SpineVersion}`;
export interface SideModuleDescriptor {
/** Artifact base name: the glue is `<file>.js`, the binary `<file>.wasm`. */
@@ -40,6 +40,9 @@ export const SIDE_MODULES: Record<SideModuleId, SideModuleDescriptor> = {
// MPEG-1 software video decoder (pl_mpeg) — the engine-owned decode path
// behind the wasm video backend on runtimes without a reliable native decoder.
videodec: { file: 'videodec', globalName: 'ESVideoModule' },
+ // One entry, not one per version: the DragonBones format is frozen, so there
+ // is no second runtime for a version suffix to tell apart.
+ dragonbones: { file: 'dragonbones', globalName: 'ESDragonBonesModule' },
'spine:2.1': { file: 'spine21', globalName: 'ESSpineModule' },
'spine:3.8': { file: 'spine38', globalName: 'ESSpineModule' },
'spine:4.1': { file: 'spine41', globalName: 'ESSpineModule' },