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
refactor: AssetRefCounter generics, shared ensureUIRenderer, PluginName constants, input cleanup
esenginead0efba3c6f2
diff --git a/sdk/src/asset/AssetRefCounter.ts b/sdk/src/asset/AssetRefCounter.tsindex 5c981a4ef..73be5ca22 100644--- a/sdk/src/asset/AssetRefCounter.ts+++ b/sdk/src/asset/AssetRefCounter.ts@@ -9,167 +9,94 @@ export interface AssetRefInfo {entities: number[];}-export class AssetRefCounter {- private textureRefs_ = new Map<string, Set<number>>();- private fontRefs_ = new Map<string, Set<number>>();- private materialRefs_ = new Map<string, Set<number>>();+class RefMap {+ private refs_ = new Map<string, Set<number>>();- addTextureRef(path: string, entity: number): void {- let refs = this.textureRefs_.get(path);+ add(path: string, entity: number): void {+ let refs = this.refs_.get(path);if (!refs) {refs = new Set();- this.textureRefs_.set(path, refs);+ this.refs_.set(path, refs);}refs.add(entity);}- removeTextureRef(path: string, entity: number): void {- const refs = this.textureRefs_.get(path);+ remove(path: string, entity: number): void {+ const refs = this.refs_.get(path);if (refs) {refs.delete(entity);- if (refs.size === 0) {- this.textureRefs_.delete(path);- }+ if (refs.size === 0) this.refs_.delete(path);}}- getTextureRefCount(path: string): number {- return this.textureRefs_.get(path)?.size ?? 0;- }-- getTextureRefs(path: string): number[] {- return Array.from(this.textureRefs_.get(path) ?? []);+ getCount(path: string): number {+ return this.refs_.get(path)?.size ?? 0;}- addFontRef(path: string, entity: number): void {- let refs = this.fontRefs_.get(path);- if (!refs) {- refs = new Set();- this.fontRefs_.set(path, refs);- }- refs.add(entity);+ getRefs(path: string): number[] {+ return Array.from(this.refs_.get(path) ?? []);}- removeFontRef(path: string, entity: number): void {- const refs = this.fontRefs_.get(path);- if (refs) {- refs.delete(entity);- if (refs.size === 0) {- this.fontRefs_.delete(path);- }- }- }-- getFontRefCount(path: string): number {- return this.fontRefs_.get(path)?.size ?? 0;- }-- getFontRefs(path: string): number[] {- return Array.from(this.fontRefs_.get(path) ?? []);- }-- addMaterialRef(path: string, entity: number): void {- let refs = this.materialRefs_.get(path);- if (!refs) {- refs = new Set();- this.materialRefs_.set(path, refs);+ getAll(): AssetRefInfo[] {+ const result: AssetRefInfo[] = [];+ for (const [path, refs] of this.refs_) {+ result.push({ assetPath: path, refCount: refs.size, entities: Array.from(refs) });}- refs.add(entity);+ return result;}- removeMaterialRef(path: string, entity: number): void {- const refs = this.materialRefs_.get(path);- if (refs) {+ removeEntity(entity: number): void {+ for (const [path, refs] of this.refs_) {refs.delete(entity);- if (refs.size === 0) {- this.materialRefs_.delete(path);- }+ if (refs.size === 0) this.refs_.delete(path);}}- getMaterialRefCount(path: string): number {- return this.materialRefs_.get(path)?.size ?? 0;- }-- getMaterialRefs(path: string): number[] {- return Array.from(this.materialRefs_.get(path) ?? []);- }-- getAllTextureRefs(): AssetRefInfo[] {- const result: AssetRefInfo[] = [];- for (const [path, refs] of this.textureRefs_.entries()) {- result.push({- assetPath: path,- refCount: refs.size,- entities: Array.from(refs),- });- }- return result;- }+ get size(): number { return this.refs_.size; }- getAllFontRefs(): AssetRefInfo[] {- const result: AssetRefInfo[] = [];- for (const [path, refs] of this.fontRefs_.entries()) {- result.push({- assetPath: path,- refCount: refs.size,- entities: Array.from(refs),- });- }- return result;- }+ clear(): void { this.refs_.clear(); }+}- getAllMaterialRefs(): AssetRefInfo[] {- const result: AssetRefInfo[] = [];- for (const [path, refs] of this.materialRefs_.entries()) {- result.push({- assetPath: path,- refCount: refs.size,- entities: Array.from(refs),- });- }- return result;- }+export class AssetRefCounter {+ private textures_ = new RefMap();+ private fonts_ = new RefMap();+ private materials_ = new RefMap();++ addTextureRef(path: string, entity: number): void { this.textures_.add(path, entity); }+ removeTextureRef(path: string, entity: number): void { this.textures_.remove(path, entity); }+ getTextureRefCount(path: string): number { return this.textures_.getCount(path); }+ getTextureRefs(path: string): number[] { return this.textures_.getRefs(path); }+ getAllTextureRefs(): AssetRefInfo[] { return this.textures_.getAll(); }++ addFontRef(path: string, entity: number): void { this.fonts_.add(path, entity); }+ removeFontRef(path: string, entity: number): void { this.fonts_.remove(path, entity); }+ getFontRefCount(path: string): number { return this.fonts_.getCount(path); }+ getFontRefs(path: string): number[] { return this.fonts_.getRefs(path); }+ getAllFontRefs(): AssetRefInfo[] { return this.fonts_.getAll(); }++ addMaterialRef(path: string, entity: number): void { this.materials_.add(path, entity); }+ removeMaterialRef(path: string, entity: number): void { this.materials_.remove(path, entity); }+ getMaterialRefCount(path: string): number { return this.materials_.getCount(path); }+ getMaterialRefs(path: string): number[] { return this.materials_.getRefs(path); }+ getAllMaterialRefs(): AssetRefInfo[] { return this.materials_.getAll(); }removeAllRefsForEntity(entity: number): void {- for (const refs of this.textureRefs_.values()) {- refs.delete(entity);- }- for (const refs of this.fontRefs_.values()) {- refs.delete(entity);- }- for (const refs of this.materialRefs_.values()) {- refs.delete(entity);- }-- for (const [path, refs] of this.textureRefs_.entries()) {- if (refs.size === 0) {- this.textureRefs_.delete(path);- }- }- for (const [path, refs] of this.fontRefs_.entries()) {- if (refs.size === 0) {- this.fontRefs_.delete(path);- }- }- for (const [path, refs] of this.materialRefs_.entries()) {- if (refs.size === 0) {- this.materialRefs_.delete(path);- }- }+ this.textures_.removeEntity(entity);+ this.fonts_.removeEntity(entity);+ this.materials_.removeEntity(entity);}clear(): void {- this.textureRefs_.clear();- this.fontRefs_.clear();- this.materialRefs_.clear();+ this.textures_.clear();+ this.fonts_.clear();+ this.materials_.clear();}getTotalRefCount(): { textures: number; fonts: number; materials: number } {return {- textures: this.textureRefs_.size,- fonts: this.fontRefs_.size,- materials: this.materialRefs_.size,+ textures: this.textures_.size,+ fonts: this.fonts_.size,+ materials: this.materials_.size,};}}diff --git a/sdk/src/input.ts b/sdk/src/input.tsindex 5e94d4512..25ab9f665 100644--- a/sdk/src/input.ts+++ b/sdk/src/input.ts@@ -46,6 +46,15 @@ export class InputState {getScrollDelta(): { x: number; y: number } {return { x: this.scrollDeltaX, y: this.scrollDeltaY };}++ clearFrameState(): void {+ this.keysPressed.clear();+ this.keysReleased.clear();+ this.mouseButtonsPressed.clear();+ this.mouseButtonsReleased.clear();+ this.scrollDeltaX = 0;+ this.scrollDeltaY = 0;+ }}export const Input = defineResource<InputState>(new InputState(), 'Input');@@ -53,6 +62,7 @@ export const Input = defineResource<InputState>(new InputState(), 'Input');export class InputPlugin implements Plugin {name = 'input';private target_: unknown;+ private unbind_: (() => void) | null = null;constructor(target?: unknown) {this.target_ = target ?? null;@@ -94,14 +104,16 @@ export class InputPlugin implements Plugin {}, this.target_ ?? undefined);app.addSystemToSchedule(Schedule.Last, defineSystem([], () => {- state.keysPressed.clear();- state.keysReleased.clear();- state.mouseButtonsPressed.clear();- state.mouseButtonsReleased.clear();- state.scrollDeltaX = 0;- state.scrollDeltaY = 0;+ state.clearFrameState();}, { name: 'InputClearSystem' }));}++ cleanup(): void {+ const platform = getPlatform() as any;+ if (typeof platform.unbindInputEvents === 'function') {+ platform.unbindInputEvents();+ }+ }}export const inputPlugin = new InputPlugin();diff --git a/sdk/src/prefab/entityRef.ts b/sdk/src/prefab/entityRef.tsindex 92de0b95b..ead1f1863 100644--- a/sdk/src/prefab/entityRef.ts+++ b/sdk/src/prefab/entityRef.ts@@ -1,5 +1,6 @@import type { ComponentData } from './types';import { getComponent } from '../component';+import { INVALID_ENTITY } from '../types';export function remapComponentEntityRefs(components: ComponentData[],@@ -10,7 +11,7 @@ export function remapComponentEntityRefs(if (!def || def.entityFields.length === 0) continue;for (const field of def.entityFields) {const value = comp.data[field];- if (typeof value === 'number' && value !== 0) {+ if (typeof value === 'number' && value !== INVALID_ENTITY) {const mapped = idMapping.get(value);if (mapped !== undefined) {comp.data[field] = mapped;diff --git a/sdk/src/systemLabels.ts b/sdk/src/systemLabels.tsindex 9ed5b23bc..7826dc6db 100644--- a/sdk/src/systemLabels.ts+++ b/sdk/src/systemLabels.ts@@ -1,8 +1,14 @@/*** @file systemLabels.ts- * @brief System name constants for ordering dependencies+ * @brief System and plugin name constants for ordering dependencies*/+export const PluginName = {+ UILayout: 'uiLayout',+ UIInteraction: 'uiInteraction',+ CollectionView: 'collectionView',+} as const;+export const SystemLabel = {UILayout: 'UILayoutSystem',UILayoutLate: 'UILayoutLateSystem',diff --git a/sdk/src/ui/CollectionViewPlugin.ts b/sdk/src/ui/CollectionViewPlugin.tsindex 3b149c3cc..665b7de7c 100644--- a/sdk/src/ui/CollectionViewPlugin.ts+++ b/sdk/src/ui/CollectionViewPlugin.ts@@ -15,7 +15,7 @@ import { ItemPool } from './ItemPool';import { UIRect, type UIRectData } from './UIRect';import { getEffectiveWidth, getEffectiveHeight } from './uiHelpers';import { Image as UIImage } from './Image';-import { SystemLabel } from '../systemLabels';+import { SystemLabel, PluginName } from '../systemLabels';export interface CollectionState {pool: ItemPool;@@ -32,7 +32,7 @@ export function getCollectionState(entity: Entity): CollectionState | null {export class CollectionViewPlugin implements Plugin {name = 'collectionView';- dependencies = ['uiLayout'];+ dependencies = [PluginName.UILayout];private cleanup_: (() => void) | null = null;diff --git a/sdk/src/ui/DragPlugin.ts b/sdk/src/ui/DragPlugin.tsindex d8f6a76c6..bda629f92 100644--- a/sdk/src/ui/DragPlugin.ts+++ b/sdk/src/ui/DragPlugin.ts@@ -18,7 +18,7 @@ import { UICameraInfo } from './UICameraInfo';import type { UICameraData } from './UICameraInfo';import { playModeOnly } from '../env';import { getEntityDepth } from './uiHelpers';-import { SystemLabel } from '../systemLabels';+import { SystemLabel, PluginName } from '../systemLabels';import { quaternionToAngle2D } from './uiMath';function worldToLocalDelta(@@ -72,7 +72,7 @@ function applyConstraints(export class DragPlugin implements Plugin {name = 'drag';- dependencies = ['uiInteraction'];+ dependencies = [PluginName.UIInteraction];build(app: App): void {registerComponent('Draggable', Draggable);diff --git a/sdk/src/ui/ImagePlugin.ts b/sdk/src/ui/ImagePlugin.tsindex 91effa244..24ac88ace 100644--- a/sdk/src/ui/ImagePlugin.ts+++ b/sdk/src/ui/ImagePlugin.ts@@ -8,24 +8,9 @@ import { UIRect } from './UIRect';import type { UIRectData } from './UIRect';import { UIRenderer, UIVisualType } from './UIRenderer';import type { UIRendererData } from './UIRenderer';-import { getEffectiveWidth, getEffectiveHeight } from './uiHelpers';+import { getEffectiveWidth, getEffectiveHeight, ensureUIRenderer } from './uiHelpers';import { SystemLabel } from '../systemLabels';-function ensureUIRenderer(world: import('../world').World, entity: Entity): void {- if (!world.has(entity, UIRenderer)) {- world.insert(entity, UIRenderer, {- visualType: UIVisualType.None,- texture: 0,- color: { r: 1, g: 1, b: 1, a: 1 },- uvOffset: { x: 0, y: 0 },- uvScale: { x: 1, y: 1 },- sliceBorder: { x: 0, y: 0, z: 0, w: 0 },- material: 0,- enabled: true,- });- }-}-export class ImagePlugin implements Plugin {name = 'image';diff --git a/sdk/src/ui/LayoutGroupPlugin.ts b/sdk/src/ui/LayoutGroupPlugin.tsindex a747a897b..a0f4f0b16 100644--- a/sdk/src/ui/LayoutGroupPlugin.ts+++ b/sdk/src/ui/LayoutGroupPlugin.ts@@ -1,10 +1,11 @@import type { App, Plugin } from '../app';import { registerComponent } from '../component';import { LayoutGroup } from './LayoutGroup';+import { PluginName } from '../systemLabels';export class LayoutGroupPlugin implements Plugin {name = 'layoutGroup';- dependencies = ['uiLayout'];+ dependencies = [PluginName.UILayout];build(_app: App): void {registerComponent('LayoutGroup', LayoutGroup);diff --git a/sdk/src/ui/ScrollViewPlugin.ts b/sdk/src/ui/ScrollViewPlugin.tsindex 6a0c4fecb..7397df805 100644--- a/sdk/src/ui/ScrollViewPlugin.ts+++ b/sdk/src/ui/ScrollViewPlugin.ts@@ -26,7 +26,7 @@ import {SCROLL_MAX_OVERSCROLL_RATIO, SCROLL_MAX_VELOCITY_RATIO,SCROLL_POSITION_EPSILON,} from './uiConstants';-import { SystemLabel } from '../systemLabels';+import { SystemLabel, PluginName } from '../systemLabels';function rubberBand(pos: number, min: number, max: number, viewSize: number): number {if (viewSize <= 0) return 1;@@ -70,7 +70,7 @@ interface ScrollState {export class ScrollViewPlugin implements Plugin {name = 'scrollView';- dependencies = ['uiLayout'];+ dependencies = [PluginName.UILayout];private cleanup_: (() => void) | null = null;diff --git a/sdk/src/ui/TextPlugin.ts b/sdk/src/ui/TextPlugin.tsindex a2d526786..5074a1444 100644--- a/sdk/src/ui/TextPlugin.ts+++ b/sdk/src/ui/TextPlugin.ts@@ -7,29 +7,14 @@ import { TextRenderer } from './TextRenderer';import { UIRect, type UIRectData } from './UIRect';import { UIRenderer, UIVisualType } from './UIRenderer';import type { UIRendererData } from './UIRenderer';-import { getEffectiveWidth, getEffectiveHeight, setUIRectSizeNative } from './uiHelpers';+import { getEffectiveWidth, getEffectiveHeight, setUIRectSizeNative, ensureUIRenderer } from './uiHelpers';import { getImageResolver, type DefaultImageResolver } from './ImageResolver';import { parseRichText } from './RichTextParser';-import { SystemLabel } from '../systemLabels';--function ensureUIRenderer(world: import('../world').World, entity: Entity): void {- if (!world.has(entity, UIRenderer)) {- world.insert(entity, UIRenderer, {- visualType: UIVisualType.None,- texture: 0,- color: { r: 1, g: 1, b: 1, a: 1 },- uvOffset: { x: 0, y: 0 },- uvScale: { x: 1, y: 1 },- sliceBorder: { x: 0, y: 0, z: 0, w: 0 },- material: 0,- enabled: true,- });- }-}+import { SystemLabel, PluginName } from '../systemLabels';export class TextPlugin implements Plugin {name = 'text';- dependencies = ['uiLayout'];+ dependencies = [PluginName.UILayout];build(app: App): void {registerComponent('Text', Text);diff --git a/sdk/src/ui/UIInteractionPlugin.ts b/sdk/src/ui/UIInteractionPlugin.tsindex a732dded8..09e3e0424 100644--- a/sdk/src/ui/UIInteractionPlugin.ts+++ b/sdk/src/ui/UIInteractionPlugin.ts@@ -24,7 +24,7 @@ import { Image } from './Image';import type { ImageData } from './Image';import type { ESEngineModule, CppRegistry } from '../wasm';import { UILayoutGeneration } from './UILayoutGeneration';-import { SystemLabel } from '../systemLabels';+import { SystemLabel, PluginName } from '../systemLabels';import type { UILayoutGenerationData } from './UILayoutGeneration';const vpCache = createInvVPCache();@@ -51,7 +51,7 @@ function emitWithBubbling(export class UIInteractionPlugin implements Plugin {name = 'uiInteraction';- dependencies = ['uiLayout'];+ dependencies = [PluginName.UILayout];build(app: App): void {registerComponent('Interactable', Interactable);diff --git a/sdk/src/ui/UIRenderOrderPlugin.ts b/sdk/src/ui/UIRenderOrderPlugin.tsindex 291c9292c..c4be617e2 100644--- a/sdk/src/ui/UIRenderOrderPlugin.ts+++ b/sdk/src/ui/UIRenderOrderPlugin.ts@@ -1,10 +1,11 @@import type { App, Plugin } from '../app';import { defineSystem, Schedule } from '../system';import type { ESEngineModule, CppRegistry } from '../wasm';+import { PluginName } from '../systemLabels';export class UIRenderOrderPlugin implements Plugin {name = 'uiRenderOrder';- dependencies = ['uiLayout', 'collectionView'];+ dependencies = [PluginName.UILayout, PluginName.CollectionView];build(app: App): void {const world = app.world;diff --git a/sdk/src/ui/uiHelpers.ts b/sdk/src/ui/uiHelpers.tsindex 3dff69087..79995f893 100644--- a/sdk/src/ui/uiHelpers.ts+++ b/sdk/src/ui/uiHelpers.ts@@ -6,7 +6,7 @@ import type { Entity, Color } from '../types';import type { World } from '../world';import { UIRect } from './UIRect';import type { UIRectData } from './UIRect';-import { UIRenderer } from './UIRenderer';+import { UIRenderer, UIVisualType } from './UIRenderer';import type { UIRendererData } from './UIRenderer';import { FillDirection } from './uiTypes';import type { ColorTransition } from './uiTypes';@@ -364,6 +364,21 @@ export function ensureComponent(}}+export function ensureUIRenderer(world: World, entity: Entity): void {+ if (!world.has(entity, UIRenderer)) {+ world.insert(entity, UIRenderer, {+ visualType: UIVisualType.None,+ texture: 0,+ color: { r: 1, g: 1, b: 1, a: 1 },+ uvOffset: { x: 0, y: 0 },+ uvScale: { x: 1, y: 1 },+ sliceBorder: { x: 0, y: 0, z: 0, w: 0 },+ material: 0,+ enabled: true,+ });+ }+}+export function makeInteractable(world: World, entity: Entity): void {ensureComponent(world, entity, Interactable, {enabled: true,