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.

fix: PlayMode entity sync, hierarchy selection, and CSP for production builds

esengine38159aeed2a3
diff --git a/desktop/src-tauri/tauri.conf.json b/desktop/src-tauri/tauri.conf.json
index 916996258..7a7c1557c 100644
--- a/desktop/src-tauri/tauri.conf.json
+++ b/desktop/src-tauri/tauri.conf.json
@@ -23,7 +23,7 @@
}
],
"security": {
- "csp": "default-src 'self' asset: http://asset.localhost ipc: http://ipc.localhost; connect-src 'self' blob: asset: http://asset.localhost http://127.0.0.1:* http://localhost:*; script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval' 'unsafe-eval' blob:; img-src 'self' blob: asset: http://asset.localhost data:; media-src 'self' blob: asset: http://asset.localhost; style-src 'self' 'unsafe-inline'",
+ "csp": "default-src 'self' asset: http://asset.localhost ipc: http://ipc.localhost; connect-src 'self' blob: asset: http://asset.localhost ipc: http://ipc.localhost http://127.0.0.1:* http://localhost:*; script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval' 'unsafe-eval' blob:; img-src 'self' blob: asset: http://asset.localhost data:; media-src 'self' blob: asset: http://asset.localhost; style-src 'self' 'unsafe-inline'",
"dangerousDisableAssetCspModification": [
"style-src"
],
diff --git a/editor/src/panels/hierarchy/HierarchyPanel.ts b/editor/src/panels/hierarchy/HierarchyPanel.ts
index 9b0ae4d77..5ef92ec67 100644
--- a/editor/src/panels/hierarchy/HierarchyPanel.ts
+++ b/editor/src/panels/hierarchy/HierarchyPanel.ts
@@ -118,8 +118,11 @@ export class HierarchyPanel implements HierarchyState {
this.render();
}
}));
- this.disposables_.add(pms.onSelectionChange(() => {
- if (this.playMode) this.renderVisibleRows();
+ this.disposables_.add(pms.onSelectionChange((id) => {
+ if (this.playMode) {
+ this.playModeSelectedId = id;
+ this.renderVisibleRows();
+ }
}));
}
@@ -154,6 +157,17 @@ export class HierarchyPanel implements HierarchyState {
private onStoreNotify(dirtyFlags?: ReadonlySet<DirtyFlag>): void {
if (this.playMode) {
if (dirtyFlags?.has('selection')) {
+ const sel = this.store.selectedEntity;
+ if (sel !== null) {
+ const pms = getPlayModeService();
+ const runtimeId = pms.editorToRuntimeId(sel as number);
+ if (runtimeId !== null) {
+ this.playModeSelectedId = runtimeId;
+ pms.selectEntity(runtimeId);
+ }
+ } else {
+ this.playModeSelectedId = null;
+ }
this.renderVisibleRows();
}
return;
@@ -284,11 +298,16 @@ export class HierarchyPanel implements HierarchyState {
const row = target.closest('.es-hierarchy-row');
const item = row?.parentElement as HTMLElement;
if (!item?.classList.contains('es-hierarchy-item')) return;
- const entityId = parseInt(item.dataset.entityId ?? '', 10);
- if (!isNaN(entityId)) {
- this.store.selectEntity(entityId as Entity, 'replace');
+ const runtimeId = parseInt(item.dataset.entityId ?? '', 10);
+ if (!isNaN(runtimeId)) {
const pms = getPlayModeService();
- pms.selectEntity(entityId);
+ pms.selectEntity(runtimeId);
+ this.playModeSelectedId = runtimeId;
+ const editorId = pms.runtimeToEditorId(runtimeId);
+ if (editorId !== null) {
+ this.store.selectEntity(editorId as Entity, 'replace');
+ }
+ this.renderVisibleRows();
}
return;
}
diff --git a/editor/src/panels/hierarchy/HierarchyTree.ts b/editor/src/panels/hierarchy/HierarchyTree.ts
index e7a2f82c3..777cea73d 100644
--- a/editor/src/panels/hierarchy/HierarchyTree.ts
+++ b/editor/src/panels/hierarchy/HierarchyTree.ts
@@ -162,7 +162,10 @@ export function renderSingleRow(state: HierarchyState, row: FlattenedRow, select
const visibilityIcon = isVisible ? icons.eye(10) : icons.eyeOff(10);
let itemClass = 'es-hierarchy-item';
- if (state.store.selectedEntities.has(entity.id)) itemClass += ' es-selected';
+ const isSelected = inPlayMode
+ ? state.playModeSelectedId === entity.id
+ : state.store.selectedEntities.has(entity.id);
+ if (isSelected) itemClass += ' es-selected';
if (!isVisible) itemClass += ' es-entity-hidden';
if (entity.prefab?.isRoot) itemClass += ' es-prefab-root';
else if (entity.prefab) itemClass += ' es-prefab-child';
diff --git a/editor/src/panels/hierarchy/HierarchyTypes.ts b/editor/src/panels/hierarchy/HierarchyTypes.ts
index 8d8487598..fb8a3779b 100644
--- a/editor/src/panels/hierarchy/HierarchyTypes.ts
+++ b/editor/src/panels/hierarchy/HierarchyTypes.ts
@@ -36,6 +36,7 @@ export interface HierarchyState {
lastClickEntityId: number | null;
lastClickTime: number;
playMode: boolean;
+ playModeSelectedId?: number | null;
runtimeEntities?: EntityData[];
render(): void;
renderVisibleRows(): void;
diff --git a/editor/src/renderer/SharedRenderContext.ts b/editor/src/renderer/SharedRenderContext.ts
index b2a2020a0..b3a133e4e 100644
--- a/editor/src/renderer/SharedRenderContext.ts
+++ b/editor/src/renderer/SharedRenderContext.ts
@@ -70,6 +70,7 @@ export class SharedRenderContext {
private onInitCallbacks_: (() => void)[] = [];
private postTickCallback_: (() => void) | null = null;
private postRenderCallback_: (() => void) | null = null;
+ private postFrameCallbacks_: Array<() => void> = [];
constructor() {
this.pathResolver_ = new AssetPathResolver();
@@ -369,6 +370,10 @@ export class SharedRenderContext {
throw e;
}
}
+
+ for (const cb of this.postFrameCallbacks_) {
+ try { cb(); } catch (e) { console.warn('[SharedRenderContext] PostFrame callback error:', e); }
+ }
}
resetFrameTick(): void {
@@ -424,6 +429,14 @@ export class SharedRenderContext {
}
}
+ onPostFrame(callback: () => void): () => void {
+ this.postFrameCallbacks_.push(callback);
+ return () => {
+ const idx = this.postFrameCallbacks_.indexOf(callback);
+ if (idx !== -1) this.postFrameCallbacks_.splice(idx, 1);
+ };
+ }
+
tickApp(): void {
if (!this.app_) return;
if (this.playMode_ && !this.paused_) {
diff --git a/editor/src/services/PlayModeService.ts b/editor/src/services/PlayModeService.ts
index f2325aefa..9eae709cd 100644
--- a/editor/src/services/PlayModeService.ts
+++ b/editor/src/services/PlayModeService.ts
@@ -56,11 +56,22 @@ export class PlayModeService {
private sharedWorld_: World | null = null;
private entityNameMap_: Map<number, string> | null = null;
private entitySceneDataMap_: Map<number, { type: string; data: Record<string, unknown> }[]> | null = null;
+ private lastWorldVersion_ = -1;
+ private editorToRuntimeMap_: Map<number, number> | null = null;
+ private runtimeToEditorMap_: Map<number, number> | null = null;
get state(): PlayState { return this.state_; }
get runtimeEntities(): RuntimeEntityData[] { return this.cachedEntities_; }
get selectedEntityId(): number | null { return this.selectedEntityId_; }
+ editorToRuntimeId(editorId: number): number | null {
+ return this.editorToRuntimeMap_?.get(editorId) ?? null;
+ }
+
+ runtimeToEditorId(runtimeId: number): number | null {
+ return this.runtimeToEditorMap_?.get(runtimeId) ?? null;
+ }
+
getRuntimeEntityData(entityId: number): EntityData | null {
const re = this.cachedEntities_.find(e => e.entityId === entityId);
if (re) return runtimeToEntityData(re);
@@ -128,6 +139,9 @@ export class PlayModeService {
this.sharedWorld_ = null;
this.entityNameMap_ = null;
this.entitySceneDataMap_ = null;
+ this.editorToRuntimeMap_ = null;
+ this.runtimeToEditorMap_ = null;
+ this.lastWorldVersion_ = -1;
this.ejectUserScripts();
await getSharedRenderContext().exitPlayMode(snapshot?.scene);
@@ -188,6 +202,7 @@ export class PlayModeService {
if (parentId != null && world.valid(parentId as Entity)) {
world.insert(entity, Parent, { entity: parentId as Entity });
}
+ this.refreshEntityList_();
return entity as number;
}
@@ -198,6 +213,7 @@ export class PlayModeService {
this.selectedEntityId_ = null;
this.emitSelectionChange();
}
+ this.refreshEntityList_();
}
addComponent(entityId: number, componentType: string, data?: Record<string, unknown>): void {
@@ -205,6 +221,7 @@ export class PlayModeService {
const compDef = getComponent(componentType);
if (!compDef) return;
this.sharedWorld_.insert(entityId as Entity, compDef, data ?? {});
+ this.refreshEntityList_();
}
removeComponent(entityId: number, componentType: string): void {
@@ -212,11 +229,13 @@ export class PlayModeService {
const compDef = getComponent(componentType);
if (!compDef) return;
this.sharedWorld_.remove(entityId as Entity, compDef);
+ this.refreshEntityList_();
}
renameEntity(entityId: number, name: string): void {
if (!this.sharedWorld_ || !this.sharedWorld_.valid(entityId as Entity)) return;
this.sharedWorld_.insert(entityId as Entity, Name, { value: name });
+ this.refreshEntityList_();
}
reparentEntity(entityId: number, newParentId: number | null): void {
@@ -226,6 +245,7 @@ export class PlayModeService {
} else {
this.sharedWorld_.remove(entityId as Entity, Parent);
}
+ this.refreshEntityList_();
}
private registerProjectScenes(ctx: ReturnType<typeof getSharedRenderContext>): void {
@@ -292,6 +312,13 @@ export class PlayModeService {
}
}
+ private refreshEntityList_(): void {
+ if (!this.sharedWorld_) return;
+ this.lastWorldVersion_ = this.sharedWorld_.getWorldVersion();
+ this.cachedEntities_ = this.buildRuntimeEntityData(this.sharedWorld_);
+ this.emitEntityListUpdate();
+ }
+
private buildRuntimeEntityData(world: import('esengine').World): RuntimeEntityData[] {
const entities = world.getAllEntities();
const result: RuntimeEntityData[] = [];
@@ -350,11 +377,15 @@ export class PlayModeService {
this.entityNameMap_ = new Map();
this.entitySceneDataMap_ = new Map();
+ this.editorToRuntimeMap_ = new Map();
+ this.runtimeToEditorMap_ = new Map();
const sm = ctx.sceneManager_;
if (sm) {
const scene = getEditorStore().scene;
const entityMap = sm.getEntityMap();
for (const [editorId, ecsEntity] of entityMap) {
+ this.editorToRuntimeMap_.set(editorId, ecsEntity as number);
+ this.runtimeToEditorMap_.set(ecsEntity as number, editorId);
const ed = scene.entities.find(e => e.id === editorId);
if (ed) {
this.entityNameMap_.set(ecsEntity as number, ed.name);
@@ -363,24 +394,27 @@ export class PlayModeService {
}
}
+ this.lastWorldVersion_ = world.getWorldVersion();
this.cachedEntities_ = this.buildRuntimeEntityData(world);
this.emitEntityListUpdate();
- const unsubSpawn = world.onSpawn((entity: Entity) => {
- this.cachedEntities_ = this.buildRuntimeEntityData(world);
- this.emitEntityListUpdate();
- });
-
const unsubDespawn = world.onDespawn((entity: Entity) => {
if (this.selectedEntityId_ === (entity as number)) {
this.selectedEntityId_ = null;
this.emitSelectionChange();
}
- this.cachedEntities_ = this.buildRuntimeEntityData(world);
- this.emitEntityListUpdate();
});
- this.sharedCleanups_.push(unsubSpawn, unsubDespawn);
+ const unsubPostFrame = ctx.onPostFrame(() => {
+ const version = world.getWorldVersion();
+ if (version !== this.lastWorldVersion_) {
+ this.lastWorldVersion_ = version;
+ this.cachedEntities_ = this.buildRuntimeEntityData(world);
+ this.emitEntityListUpdate();
+ }
+ });
+
+ this.sharedCleanups_.push(unsubDespawn, unsubPostFrame);
}
private emitStateChange(): void {
diff --git a/sdk/src/world.ts b/sdk/src/world.ts
index 4506bcab5..1269b4f86 100644
--- a/sdk/src/world.ts
+++ b/sdk/src/world.ts
@@ -244,6 +244,7 @@ export class World {
handleWasmError(e, `setParent(child=${child}, parent=${parent})`);
}
}
+ this.queries_.markStructuralChange();
}
removeParent(entity: Entity): void {
@@ -255,6 +256,7 @@ export class World {
handleWasmError(e, `removeParent(entity=${entity})`);
}
}
+ this.queries_.markStructuralChange();
}
// =========================================================================