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(editor): two people editing one scene stop naming the same entity
esengined210a68dfde6
diff --git a/desktop/src/engine/SceneModel.ts b/desktop/src/engine/SceneModel.tsindex b47a2c91d..54fda6066 100644--- a/desktop/src/engine/SceneModel.ts+++ b/desktop/src/engine/SceneModel.ts@@ -37,6 +37,18 @@ export type ModelEvent =type Listener = (ev: ModelEvent) => void;+/**+ * Where a session starts allocating: a random distance past the highest id in+ * the file, so two people editing the same scene do not both name their first+ * new entity the same thing. An empty scene starts at 1 — no other version of it+ * exists to merge with.+ */+const ID_SESSION_SPREAD = 0x10000;+function firstIdAfter(maxId: number): number {+ if (maxId === 0) return 1;+ return maxId + 1 + Math.floor(Math.random() * ID_SESSION_SPREAD);+}+/** A removed entity plus where it sat — everything an exact restore needs. */export interface RemovedEntity {entity: SceneEntity;@@ -125,7 +137,7 @@ export class SceneModelImpl {this.entityIndex_ = new Map(loaded.entities.map((e) => [e.id, e]));let maxId = 0;for (const e of loaded.entities) if (e.id > maxId) maxId = e.id;- this.nextSourceId = maxId + 1;+ this.nextSourceId = firstIdAfter(maxId);for (const [sourceId, runtime] of entityMap) {this.sourceToRuntime.set(sourceId, runtime);this.runtimeToSource.set(runtime, sourceId);diff --git a/desktop/tests/scene-model-id-allocation.test.ts b/desktop/tests/scene-model-id-allocation.test.tsnew file mode 100644index 000000000..c7f10556d--- /dev/null+++ b/desktop/tests/scene-model-id-allocation.test.ts@@ -0,0 +1,63 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file Where a session's new entity ids come from. Two people editing the same+ * scene must not both name their first new entity the same thing, or the+ * merged file has two entities answering to one id.+ */+import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';+import type { SceneData } from 'esengine';+import { SceneModel } from '@/engine/SceneModel';++function fixture(highestId: number): SceneData {+ return {+ version: 1,+ name: 'fixture',+ entities: Array.from({ length: highestId }, (_, i) => ({+ id: i + 1,+ name: `E${i + 1}`,+ parent: null,+ children: [],+ components: [],+ })),+ } as unknown as SceneData;+}++describe('source id allocation', () => {+ beforeEach(() => SceneModel.clear());+ afterEach(() => vi.restoreAllMocks());++ it('starts a session at a distance from the ids already in the file', () => {+ vi.spyOn(Math, 'random').mockReturnValue(0.5);+ SceneModel.adopt(fixture(3), new Map());+ // 3 is the highest in the file; the session starts 0x8000 past it.+ expect(SceneModel.addEntity('New', [])).toBe(3 + 1 + 0x8000);+ });++ it('gives two sessions on the same file different ids', () => {+ vi.spyOn(Math, 'random').mockReturnValue(0.25);+ SceneModel.adopt(fixture(3), new Map());+ const first = SceneModel.addEntity('New', []);++ SceneModel.clear();+ vi.spyOn(Math, 'random').mockReturnValue(0.75);+ SceneModel.adopt(fixture(3), new Map());+ const second = SceneModel.addEntity('New', []);++ expect(first).not.toBe(second);+ });++ it('numbers an empty scene from 1 — there is no other version of it to merge', () => {+ vi.spyOn(Math, 'random').mockReturnValue(0.5);+ SceneModel.adopt(fixture(0), new Map());+ expect(SceneModel.addEntity('First', [])).toBe(1);+ });++ it('keeps allocating upward within one session', () => {+ vi.spyOn(Math, 'random').mockReturnValue(0);+ SceneModel.adopt(fixture(2), new Map());+ const a = SceneModel.addEntity('A', []);+ const b = SceneModel.addEntity('B', []);+ expect(b).toBe(a + 1);+ });+});