Feature/editor optimization (#251)
* refactor: 编辑器/运行时架构拆分与构建系统升级 * feat(core): 层级系统重构与UI变换矩阵修复 * refactor: 移除 ecs-components 聚合包并修复跨包组件查找问题 * fix(physics): 修复跨包组件类引用问题 * feat: 统一运行时架构与浏览器运行支持 * feat(asset): 实现浏览器运行时资产加载系统 * fix: 修复文档、CodeQL安全问题和CI类型检查错误 * fix: 修复文档、CodeQL安全问题和CI类型检查错误 * fix: 修复文档、CodeQL安全问题、CI类型检查和测试错误 * test: 补齐核心模块测试用例,修复CI构建配置 * fix: 修复测试用例中的类型错误和断言问题 * fix: 修复 turbo build:npm 任务的依赖顺序问题 * fix: 修复 CI 构建错误并优化构建性能
This commit is contained in:
@@ -8,6 +8,8 @@ import { Entity } from '../Entity';
|
||||
import { ComponentType } from '../Core/ComponentStorage';
|
||||
import { ComponentSerializer, SerializedComponent } from './ComponentSerializer';
|
||||
import { IScene } from '../IScene';
|
||||
import { HierarchyComponent } from '../Components/HierarchyComponent';
|
||||
import { HierarchySystem } from '../Systems/HierarchySystem';
|
||||
|
||||
/**
|
||||
* 序列化后的实体数据
|
||||
@@ -68,9 +70,14 @@ export class EntitySerializer {
|
||||
*
|
||||
* @param entity 要序列化的实体
|
||||
* @param includeChildren 是否包含子实体(默认true)
|
||||
* @param hierarchySystem 层级系统(可选,用于获取层级信息)
|
||||
* @returns 序列化后的实体数据
|
||||
*/
|
||||
public static serialize(entity: Entity, includeChildren: boolean = true): SerializedEntity {
|
||||
public static serialize(
|
||||
entity: Entity,
|
||||
includeChildren: boolean = true,
|
||||
hierarchySystem?: HierarchySystem
|
||||
): SerializedEntity {
|
||||
const serializedComponents = ComponentSerializer.serializeComponents(
|
||||
Array.from(entity.components)
|
||||
);
|
||||
@@ -86,15 +93,24 @@ export class EntitySerializer {
|
||||
children: []
|
||||
};
|
||||
|
||||
// 序列化父实体引用
|
||||
if (entity.parent) {
|
||||
serializedEntity.parentId = entity.parent.id;
|
||||
// 通过 HierarchyComponent 获取层级信息
|
||||
const hierarchy = entity.getComponent(HierarchyComponent);
|
||||
if (hierarchy?.parentId !== null && hierarchy?.parentId !== undefined) {
|
||||
serializedEntity.parentId = hierarchy.parentId;
|
||||
}
|
||||
|
||||
// 序列化子实体
|
||||
if (includeChildren) {
|
||||
for (const child of entity.children) {
|
||||
serializedEntity.children.push(this.serialize(child, true));
|
||||
// 直接使用 HierarchyComponent.childIds 获取子实体
|
||||
if (includeChildren && hierarchy && hierarchy.childIds.length > 0) {
|
||||
// 获取场景引用:优先从 hierarchySystem,否则从 entity.scene
|
||||
const scene = hierarchySystem?.scene ?? entity.scene;
|
||||
if (scene) {
|
||||
for (const childId of hierarchy.childIds) {
|
||||
const child = scene.findEntityById(childId);
|
||||
if (child) {
|
||||
serializedEntity.children.push(this.serialize(child, true, hierarchySystem));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +125,7 @@ export class EntitySerializer {
|
||||
* @param idGenerator 实体ID生成器(用于生成新ID或保持原ID)
|
||||
* @param preserveIds 是否保持原始ID(默认false)
|
||||
* @param scene 目标场景(可选,用于设置entity.scene以支持添加组件)
|
||||
* @param hierarchySystem 层级系统(可选,用于建立层级关系)
|
||||
* @returns 反序列化后的实体
|
||||
*/
|
||||
public static deserialize(
|
||||
@@ -116,12 +133,17 @@ export class EntitySerializer {
|
||||
componentRegistry: Map<string, ComponentType>,
|
||||
idGenerator: () => number,
|
||||
preserveIds: boolean = false,
|
||||
scene?: IScene
|
||||
scene?: IScene,
|
||||
hierarchySystem?: HierarchySystem | null,
|
||||
allEntities?: Map<number, Entity>
|
||||
): Entity {
|
||||
// 创建实体(使用原始ID或新生成的ID)
|
||||
const entityId = preserveIds ? serializedEntity.id : idGenerator();
|
||||
const entity = new Entity(serializedEntity.name, entityId);
|
||||
|
||||
// 将实体添加到收集 Map 中(用于后续添加到场景)
|
||||
allEntities?.set(entity.id, entity);
|
||||
|
||||
// 如果提供了scene,先设置entity.scene以支持添加组件
|
||||
if (scene) {
|
||||
entity.scene = scene;
|
||||
@@ -143,16 +165,28 @@ export class EntitySerializer {
|
||||
entity.addComponent(component);
|
||||
}
|
||||
|
||||
// 反序列化子实体
|
||||
// 重要:清除 HierarchyComponent 中的旧 ID
|
||||
// 当 preserveIds=false 时,序列化的 parentId 和 childIds 是旧 ID,需要重新建立
|
||||
// 通过 hierarchySystem.setParent 会正确设置新的 ID
|
||||
const hierarchy = entity.getComponent(HierarchyComponent);
|
||||
if (hierarchy) {
|
||||
hierarchy.parentId = null;
|
||||
hierarchy.childIds = [];
|
||||
}
|
||||
|
||||
// 反序列化子实体并建立层级关系
|
||||
for (const childData of serializedEntity.children) {
|
||||
const childEntity = this.deserialize(
|
||||
childData,
|
||||
componentRegistry,
|
||||
idGenerator,
|
||||
preserveIds,
|
||||
scene
|
||||
scene,
|
||||
hierarchySystem,
|
||||
allEntities
|
||||
);
|
||||
entity.addChild(childEntity);
|
||||
// 使用 HierarchySystem 建立层级关系
|
||||
hierarchySystem?.setParent(childEntity, entity);
|
||||
}
|
||||
|
||||
return entity;
|
||||
@@ -163,19 +197,23 @@ export class EntitySerializer {
|
||||
*
|
||||
* @param entities 实体数组
|
||||
* @param includeChildren 是否包含子实体
|
||||
* @param hierarchySystem 层级系统(可选,用于获取层级信息)
|
||||
* @returns 序列化后的实体数据数组
|
||||
*/
|
||||
public static serializeEntities(
|
||||
entities: Entity[],
|
||||
includeChildren: boolean = true
|
||||
includeChildren: boolean = true,
|
||||
hierarchySystem?: HierarchySystem
|
||||
): SerializedEntity[] {
|
||||
const result: SerializedEntity[] = [];
|
||||
|
||||
for (const entity of entities) {
|
||||
// 只序列化顶层实体(没有父实体的实体)
|
||||
// 子实体会在父实体序列化时一并处理
|
||||
if (!entity.parent || !includeChildren) {
|
||||
result.push(this.serialize(entity, includeChildren));
|
||||
const hierarchy = entity.getComponent(HierarchyComponent);
|
||||
const bHasParent = hierarchy?.parentId !== null && hierarchy?.parentId !== undefined;
|
||||
if (!bHasParent || !includeChildren) {
|
||||
result.push(this.serialize(entity, includeChildren, hierarchySystem));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,6 +228,7 @@ export class EntitySerializer {
|
||||
* @param idGenerator 实体ID生成器
|
||||
* @param preserveIds 是否保持原始ID
|
||||
* @param scene 目标场景(可选,用于设置entity.scene以支持添加组件)
|
||||
* @param hierarchySystem 层级系统(可选,用于建立层级关系)
|
||||
* @returns 反序列化后的实体数组
|
||||
*/
|
||||
public static deserializeEntities(
|
||||
@@ -197,9 +236,11 @@ export class EntitySerializer {
|
||||
componentRegistry: Map<string, ComponentType>,
|
||||
idGenerator: () => number,
|
||||
preserveIds: boolean = false,
|
||||
scene?: IScene
|
||||
): Entity[] {
|
||||
const result: Entity[] = [];
|
||||
scene?: IScene,
|
||||
hierarchySystem?: HierarchySystem | null
|
||||
): { rootEntities: Entity[]; allEntities: Map<number, Entity> } {
|
||||
const rootEntities: Entity[] = [];
|
||||
const allEntities = new Map<number, Entity>();
|
||||
|
||||
for (const serialized of serializedEntities) {
|
||||
const entity = this.deserialize(
|
||||
@@ -207,12 +248,14 @@ export class EntitySerializer {
|
||||
componentRegistry,
|
||||
idGenerator,
|
||||
preserveIds,
|
||||
scene
|
||||
scene,
|
||||
hierarchySystem,
|
||||
allEntities
|
||||
);
|
||||
result.push(entity);
|
||||
rootEntities.push(entity);
|
||||
}
|
||||
|
||||
return result;
|
||||
return { rootEntities, allEntities };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,8 @@ import { ComponentSerializer, SerializedComponent } from './ComponentSerializer'
|
||||
import { SerializedEntity } from './EntitySerializer';
|
||||
import { ComponentType } from '../Core/ComponentStorage';
|
||||
import { BinarySerializer } from '../../Utils/BinarySerializer';
|
||||
import { HierarchyComponent } from '../Components/HierarchyComponent';
|
||||
import { HierarchySystem } from '../Systems/HierarchySystem';
|
||||
|
||||
/**
|
||||
* 变更操作类型
|
||||
@@ -196,6 +198,10 @@ export class IncrementalSerializer {
|
||||
for (const entity of scene.entities.buffer) {
|
||||
snapshot.entityIds.add(entity.id);
|
||||
|
||||
// 获取层级信息
|
||||
const hierarchy = entity.getComponent(HierarchyComponent);
|
||||
const parentId = hierarchy?.parentId;
|
||||
|
||||
// 存储实体基本信息
|
||||
snapshot.entities.set(entity.id, {
|
||||
name: entity.name,
|
||||
@@ -203,7 +209,7 @@ export class IncrementalSerializer {
|
||||
active: entity.active,
|
||||
enabled: entity.enabled,
|
||||
updateOrder: entity.updateOrder,
|
||||
...(entity.parent && { parentId: entity.parent.id })
|
||||
...(parentId !== null && parentId !== undefined && { parentId })
|
||||
});
|
||||
|
||||
// 快照组件
|
||||
@@ -272,6 +278,10 @@ export class IncrementalSerializer {
|
||||
for (const entity of scene.entities.buffer) {
|
||||
currentEntityIds.add(entity.id);
|
||||
|
||||
// 获取层级信息
|
||||
const hierarchy = entity.getComponent(HierarchyComponent);
|
||||
const parentId = hierarchy?.parentId;
|
||||
|
||||
if (!baseSnapshot.entityIds.has(entity.id)) {
|
||||
// 新增实体
|
||||
incremental.entityChanges.push({
|
||||
@@ -285,7 +295,7 @@ export class IncrementalSerializer {
|
||||
active: entity.active,
|
||||
enabled: entity.enabled,
|
||||
updateOrder: entity.updateOrder,
|
||||
...(entity.parent && { parentId: entity.parent.id }),
|
||||
...(parentId !== null && parentId !== undefined && { parentId }),
|
||||
components: [],
|
||||
children: []
|
||||
}
|
||||
@@ -312,7 +322,7 @@ export class IncrementalSerializer {
|
||||
oldData.active !== entity.active ||
|
||||
oldData.enabled !== entity.enabled ||
|
||||
oldData.updateOrder !== entity.updateOrder ||
|
||||
oldData.parentId !== entity.parent?.id;
|
||||
oldData.parentId !== parentId;
|
||||
|
||||
if (entityChanged) {
|
||||
incremental.entityChanges.push({
|
||||
@@ -324,7 +334,7 @@ export class IncrementalSerializer {
|
||||
active: entity.active,
|
||||
enabled: entity.enabled,
|
||||
updateOrder: entity.updateOrder,
|
||||
...(entity.parent && { parentId: entity.parent.id })
|
||||
...(parentId !== null && parentId !== undefined && { parentId })
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -539,16 +549,20 @@ export class IncrementalSerializer {
|
||||
if (change.entityData.enabled !== undefined) entity.enabled = change.entityData.enabled;
|
||||
if (change.entityData.updateOrder !== undefined) entity.updateOrder = change.entityData.updateOrder;
|
||||
|
||||
if (change.entityData.parentId !== undefined) {
|
||||
const newParent = scene.entities.findEntityById(change.entityData.parentId);
|
||||
if (newParent && entity.parent !== newParent) {
|
||||
if (entity.parent) {
|
||||
entity.parent.removeChild(entity);
|
||||
// 使用 HierarchySystem 更新层级关系
|
||||
const hierarchySystem = scene.getSystem(HierarchySystem);
|
||||
if (hierarchySystem) {
|
||||
const hierarchy = entity.getComponent(HierarchyComponent);
|
||||
const currentParentId = hierarchy?.parentId;
|
||||
|
||||
if (change.entityData.parentId !== undefined) {
|
||||
const newParent = scene.entities.findEntityById(change.entityData.parentId);
|
||||
if (newParent && currentParentId !== change.entityData.parentId) {
|
||||
hierarchySystem.setParent(entity, newParent);
|
||||
}
|
||||
newParent.addChild(entity);
|
||||
} else if (currentParentId !== null && currentParentId !== undefined) {
|
||||
hierarchySystem.setParent(entity, null);
|
||||
}
|
||||
} else if (entity.parent) {
|
||||
entity.parent.removeChild(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import { EntitySerializer, SerializedEntity } from './EntitySerializer';
|
||||
import { getComponentTypeName } from '../Decorators';
|
||||
import { getSerializationMetadata } from './SerializationDecorators';
|
||||
import { BinarySerializer } from '../../Utils/BinarySerializer';
|
||||
import { HierarchySystem } from '../Systems/HierarchySystem';
|
||||
import { HierarchyComponent } from '../Components/HierarchyComponent';
|
||||
|
||||
/**
|
||||
* 场景序列化格式
|
||||
@@ -167,8 +169,11 @@ export class SceneSerializer {
|
||||
// 过滤实体和组件
|
||||
const entities = this.filterEntities(scene, opts);
|
||||
|
||||
// 序列化实体
|
||||
const serializedEntities = EntitySerializer.serializeEntities(entities, true);
|
||||
// 获取层级系统用于序列化子实体
|
||||
const hierarchySystem = scene.getSystem(HierarchySystem);
|
||||
|
||||
// 序列化实体(传入 hierarchySystem 以正确序列化子实体)
|
||||
const serializedEntities = EntitySerializer.serializeEntities(entities, true, hierarchySystem ?? undefined);
|
||||
|
||||
// 收集组件类型信息
|
||||
const componentTypeRegistry = this.buildComponentTypeRegistry(entities);
|
||||
@@ -258,19 +263,24 @@ export class SceneSerializer {
|
||||
// ID生成器
|
||||
const idGenerator = () => scene.identifierPool.checkOut();
|
||||
|
||||
// 获取层级系统
|
||||
const hierarchySystem = scene.getSystem(HierarchySystem);
|
||||
|
||||
// 反序列化实体
|
||||
const entities = EntitySerializer.deserializeEntities(
|
||||
const { rootEntities, allEntities } = EntitySerializer.deserializeEntities(
|
||||
serializedScene.entities,
|
||||
componentRegistry,
|
||||
idGenerator,
|
||||
opts.preserveIds || false,
|
||||
scene
|
||||
scene,
|
||||
hierarchySystem
|
||||
);
|
||||
|
||||
// 将实体添加到场景
|
||||
for (const entity of entities) {
|
||||
// 将所有实体添加到场景(包括子实体)
|
||||
// 先添加根实体,再递归添加子实体
|
||||
for (const entity of rootEntities) {
|
||||
scene.addEntity(entity, true);
|
||||
this.addChildrenRecursively(entity, scene);
|
||||
this.addChildrenRecursively(entity, scene, hierarchySystem, allEntities);
|
||||
}
|
||||
|
||||
// 统一清理缓存(批量操作完成后)
|
||||
@@ -285,8 +295,8 @@ export class SceneSerializer {
|
||||
// 调用所有组件的 onDeserialized 生命周期方法
|
||||
// Call onDeserialized lifecycle method on all components
|
||||
const deserializedPromises: Promise<void>[] = [];
|
||||
for (const entity of entities) {
|
||||
this.callOnDeserializedRecursively(entity, deserializedPromises);
|
||||
for (const entity of allEntities.values()) {
|
||||
this.callOnDeserializedForEntity(entity, deserializedPromises);
|
||||
}
|
||||
|
||||
// 如果有异步的 onDeserialized,在后台执行
|
||||
@@ -298,9 +308,12 @@ export class SceneSerializer {
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归调用实体及其子实体所有组件的 onDeserialized 方法
|
||||
* 调用实体所有组件的 onDeserialized 方法(不递归)
|
||||
*/
|
||||
private static callOnDeserializedRecursively(entity: Entity, promises: Promise<void>[]): void {
|
||||
private static callOnDeserializedForEntity(
|
||||
entity: Entity,
|
||||
promises: Promise<void>[]
|
||||
): void {
|
||||
for (const component of entity.components) {
|
||||
try {
|
||||
const result = component.onDeserialized();
|
||||
@@ -311,10 +324,6 @@ export class SceneSerializer {
|
||||
console.error(`Error calling onDeserialized on component ${component.constructor.name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
for (const child of entity.children) {
|
||||
this.callOnDeserializedRecursively(child, promises);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -327,11 +336,27 @@ export class SceneSerializer {
|
||||
*
|
||||
* @param entity 父实体
|
||||
* @param scene 目标场景
|
||||
* @param hierarchySystem 层级系统
|
||||
*/
|
||||
private static addChildrenRecursively(entity: Entity, scene: IScene): void {
|
||||
for (const child of entity.children) {
|
||||
scene.addEntity(child, true); // 延迟缓存清理
|
||||
this.addChildrenRecursively(child, scene); // 递归处理子实体的子实体
|
||||
private static addChildrenRecursively(
|
||||
entity: Entity,
|
||||
scene: IScene,
|
||||
hierarchySystem?: HierarchySystem | null,
|
||||
childEntitiesMap?: Map<number, Entity>
|
||||
): void {
|
||||
const hierarchy = entity.getComponent(HierarchyComponent);
|
||||
if (!hierarchy || hierarchy.childIds.length === 0) return;
|
||||
|
||||
// 获取子实体
|
||||
// 注意:此时子实体还没有被添加到场景,所以不能用 scene.findEntityById
|
||||
// 需要从 childEntitiesMap 中查找(如果提供了的话)
|
||||
for (const childId of hierarchy.childIds) {
|
||||
// 尝试从 map 中获取,否则从场景获取(用于已添加的情况)
|
||||
const child = childEntitiesMap?.get(childId) ?? scene.findEntityById(childId);
|
||||
if (child) {
|
||||
scene.addEntity(child, true); // 延迟缓存清理
|
||||
this.addChildrenRecursively(child, scene, hierarchySystem, childEntitiesMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user