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(ecs): invalidate projections when the core changes
yhhaa2b6a68eff7
diff --git a/sdk/src/ecs/ComponentMutationReader.ts b/sdk/src/ecs/ComponentMutationReader.tsindex 4e0649115..8e4710b50 100644--- a/sdk/src/ecs/ComponentMutationReader.ts+++ b/sdk/src/ecs/ComponentMutationReader.ts@@ -28,6 +28,8 @@ export class ComponentMutationReader {private lease: number | null = null;private cursor = 0;private first = true;+ private bindingReset = false;+ private disposed = false;private resetEpoch: number | null = null;private lastRegistry: CppRegistry | null = null;private readonly nativeTypes = new Map<number, AnyComponentDef>();@@ -42,11 +44,14 @@ export class ComponentMutationReader {this.values = new Set([...components, ...subtree]);this.members = new Set([...this.values, ...topologyOnly]);this.offDisconnect = world.onCoreDisconnect(() => {- try { this.pause(); } finally { this.lastRegistry = null; this.resetEpoch = null; }+ try { this.pause(); } finally {+ this.bindingReset = true; this.lastRegistry = null; this.resetEpoch = null;+ }});}private open(): boolean {+ if (this.disposed) return false;if (this.lease !== null) return true;const api = worldEngineApi(this.world), registry = this.world.getCppRegistry();if (!registry?.componentTypeId || !api?.registry_watchEntityChanges || !api.registry_unwatchEntityChanges@@ -90,7 +95,7 @@ export class ComponentMutationReader {for (const [component, reader] of this.topology) {for (const entity of world.mutationsForCursor(component, 'topology', reader)) note(component, entity, true);}- let reset = resetEpoch < 0 || resetEpoch !== this.resetEpoch;+ let reset = this.bindingReset || resetEpoch < 0 || resetEpoch !== this.resetEpoch;let full = this.first || reset;if (nativeSequence !== this.cursor) {withMalloc({ _malloc: api._malloc!.bind(api), _free: api._free!.bind(api) },@@ -137,6 +142,7 @@ export class ComponentMutationReader {commit(batch: ComponentMutationBatch): void {this.cursor = batch.nativeSequence; this.first = false;+ this.bindingReset = false;this.resetEpoch = batch.resetEpoch;for (const [component, reader] of this.writes) this.world.advanceMutationCursor(component, 'write', reader, batch.writeSequence);for (const [component, reader] of this.topology) this.world.advanceMutationCursor(component, 'topology', reader, batch.topologySequence);@@ -154,6 +160,7 @@ export class ComponentMutationReader {}dispose(): void {+ this.disposed = true;try { this.pause(); } finally { this.lastRegistry = null; this.offDisconnect(); }}}diff --git a/sdk/tests/physics-mutation-reconcile-wasm.test.ts b/sdk/tests/physics-mutation-reconcile-wasm.test.tsindex bd1dbeda3..f129d5846 100644--- a/sdk/tests/physics-mutation-reconcile-wasm.test.ts+++ b/sdk/tests/physics-mutation-reconcile-wasm.test.ts@@ -2,6 +2,8 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';import { App } from '../src/app/app';+import { World } from '../src/ecs/world';+import { ComponentMutationReader } from '../src/ecs/ComponentMutationReader';import { Transform, Disabled } from '../src/ecs/component';import { Time } from '../src/ecs/resource';import { Schedule, defineSystem } from '../src/ecs/system';@@ -165,4 +167,26 @@ describe.skipIf(!HAS_WASM || !hasSideModule('physics'))('component-driven physicexpect(world.writeReaderCount(Transform)).toBe(0);expect(unwatch).toHaveBeenCalledOnce();});++ it('invalidates projections after a core switch and cannot reopen a disposed reader', () => {+ const world = new World(), first = new core.Registry(), second = new core.Registry();+ world.connectCpp(first, core);+ const entity = world.spawn(); world.insert(entity, Transform);+ expect(second.create()).toBe(entity);+ second.addTransform(entity, first.getTransform(entity));+ const reader = new ComponentMutationReader(world, [Transform], [], []);+ try {+ reader.commit(reader.read()!);+ world.connectCpp(second, core);+ expect(core.registry_readEntityChanges!(first, 0, 0, 0)).toBe(-1);+ const current = reader.read()!;+ expect(current.full).toBe(true); expect(current.reset).toBe(true);+ reader.commit(current); reader.dispose();+ expect(reader.read()).toBeNull();+ expect(world.writeReaderCount(Transform)).toBe(0);+ expect(core.registry_readEntityChanges!(second, 0, 0, 0)).toBe(-1);+ } finally {+ reader.dispose(); world.disconnectCpp(); first.delete(); second.delete();+ }+ });});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex afddf67e3..c4c661ab6 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(ecs): invalidate projections when the core changes':+ { note: 'Physics reconciliation follows component mutation cursors.' },'perf(physics): reconcile only changed body definitions':{ note: 'Physics reconciliation follows component mutation cursors.' },'refactor(ecs): unify SDK and native mutation cursors':