Feature/runtime cdn and plugin loader (#240)
* feat(ui): 完善 UI 布局系统和编辑器可视化工具 * refactor: 移除 ModuleRegistry,统一使用 PluginManager 插件系统 * fix: 修复 CodeQL 警告并提升测试覆盖率 * refactor: 分离运行时入口点,解决 runtime bundle 包含 React 的问题 * fix(ci): 添加 editor-core 和 editor-runtime 到 CI 依赖构建步骤 * docs: 完善 ServiceContainer 文档,新增 Symbol.for 模式和 @InjectProperty 说明 * fix(ci): 修复 type-check 失败问题 * fix(ci): 修复类型检查失败问题 * fix(ci): 修复类型检查失败问题 * fix(ci): behavior-tree 构建添加 @tauri-apps 外部依赖 * fix(ci): behavior-tree 添加 @tauri-apps/plugin-fs 类型依赖 * fix(ci): platform-web 添加缺失的 behavior-tree 依赖 * fix(lint): 移除正则表达式中不必要的转义字符
This commit is contained in:
@@ -146,7 +146,9 @@ export type PropertyOptions =
|
||||
| AssetPropertyOptions
|
||||
| AnimationClipsPropertyOptions;
|
||||
|
||||
export const PROPERTY_METADATA = Symbol('property:metadata');
|
||||
// 使用 Symbol.for 创建全局 Symbol,确保跨包共享元数据
|
||||
// Use Symbol.for to create a global Symbol to ensure metadata sharing across packages
|
||||
export const PROPERTY_METADATA = Symbol.for('@esengine/property:metadata');
|
||||
|
||||
/**
|
||||
* 属性装饰器 - 声明组件属性的编辑器元数据
|
||||
|
||||
@@ -284,6 +284,11 @@ export abstract class EntitySystem implements ISystemBase, IService {
|
||||
const querySystem = this.scene.querySystem;
|
||||
let currentEntities: readonly Entity[] = [];
|
||||
|
||||
// matchNothing 条件返回空数组(用于只需要生命周期方法的系统)
|
||||
if (this._matcher.isNothing()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 空条件返回所有实体
|
||||
if (this._matcher.isEmpty()) {
|
||||
currentEntities = querySystem.getAllEntities();
|
||||
|
||||
@@ -11,6 +11,7 @@ export interface QueryCondition {
|
||||
tag?: number; // 按标签查询
|
||||
name?: string; // 按名称查询
|
||||
component?: ComponentType; // 单组件查询
|
||||
matchNothing?: boolean; // 不匹配任何实体
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,6 +108,34 @@ export class Matcher {
|
||||
return new Matcher();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建不匹配任何实体的匹配器
|
||||
* 用于只需要 onBegin/onEnd 生命周期方法但不需要处理实体的系统
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 创建一个只在帧开始时执行的系统
|
||||
* class FrameBeginSystem extends EntitySystem {
|
||||
* constructor() {
|
||||
* super(Matcher.nothing());
|
||||
* }
|
||||
*
|
||||
* protected onBegin(): void {
|
||||
* // 每帧开始时执行
|
||||
* }
|
||||
*
|
||||
* protected process(entities: readonly Entity[]): void {
|
||||
* // 永远不会被调用,因为没有实体匹配
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
public static nothing(): Matcher {
|
||||
const matcher = new Matcher();
|
||||
matcher.condition.matchNothing = true;
|
||||
return matcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 必须包含所有指定组件
|
||||
* @param types 组件类型
|
||||
@@ -211,12 +240,14 @@ export class Matcher {
|
||||
none: [...this.condition.none],
|
||||
...(this.condition.tag !== undefined && { tag: this.condition.tag }),
|
||||
...(this.condition.name !== undefined && { name: this.condition.name }),
|
||||
...(this.condition.component !== undefined && { component: this.condition.component })
|
||||
...(this.condition.component !== undefined && { component: this.condition.component }),
|
||||
...(this.condition.matchNothing && { matchNothing: true })
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为空条件
|
||||
* 注意:matchNothing 不算空条件,因为它是明确的"不匹配任何实体"语义
|
||||
*/
|
||||
public isEmpty(): boolean {
|
||||
return this.condition.all.length === 0 &&
|
||||
@@ -224,7 +255,15 @@ export class Matcher {
|
||||
this.condition.none.length === 0 &&
|
||||
this.condition.tag === undefined &&
|
||||
this.condition.name === undefined &&
|
||||
this.condition.component === undefined;
|
||||
this.condition.component === undefined &&
|
||||
!this.condition.matchNothing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为"不匹配任何实体"的匹配器
|
||||
*/
|
||||
public isNothing(): boolean {
|
||||
return this.condition.matchNothing === true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,6 +276,7 @@ export class Matcher {
|
||||
delete this.condition.tag;
|
||||
delete this.condition.name;
|
||||
delete this.condition.component;
|
||||
delete this.condition.matchNothing;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -257,6 +297,9 @@ export class Matcher {
|
||||
if (this.condition.component !== undefined) {
|
||||
cloned.condition.component = this.condition.component;
|
||||
}
|
||||
if (this.condition.matchNothing) {
|
||||
cloned.condition.matchNothing = true;
|
||||
}
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@@ -264,6 +307,10 @@ export class Matcher {
|
||||
* 字符串表示
|
||||
*/
|
||||
public toString(): string {
|
||||
if (this.condition.matchNothing) {
|
||||
return 'Matcher[nothing]';
|
||||
}
|
||||
|
||||
const parts: string[] = [];
|
||||
|
||||
if (this.condition.all.length > 0) {
|
||||
|
||||
@@ -266,4 +266,358 @@ describe('EntitySystem', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('nothing 匹配器与系统', () => {
|
||||
class NothingSystem extends EntitySystem {
|
||||
public onBeginCallCount = 0;
|
||||
public processCallCount = 0;
|
||||
public onEndCallCount = 0;
|
||||
|
||||
constructor() {
|
||||
super(Matcher.nothing());
|
||||
}
|
||||
|
||||
protected override onBegin(): void {
|
||||
this.onBeginCallCount++;
|
||||
}
|
||||
|
||||
protected override process(entities: readonly Entity[]): void {
|
||||
this.processCallCount++;
|
||||
// nothing 匹配器不应该有任何实体
|
||||
expect(entities).toHaveLength(0);
|
||||
}
|
||||
|
||||
protected override onEnd(): void {
|
||||
this.onEndCallCount++;
|
||||
}
|
||||
}
|
||||
|
||||
it('使用 nothing 匹配器的系统应该正常运行生命周期方法', () => {
|
||||
const nothingSystem = new NothingSystem();
|
||||
scene.addSystem(nothingSystem);
|
||||
|
||||
// 手动触发更新
|
||||
nothingSystem.update();
|
||||
nothingSystem.lateUpdate();
|
||||
|
||||
// 生命周期方法应该被调用
|
||||
expect(nothingSystem.onBeginCallCount).toBe(1);
|
||||
expect(nothingSystem.processCallCount).toBe(1);
|
||||
expect(nothingSystem.onEndCallCount).toBe(1);
|
||||
|
||||
// 清理
|
||||
scene.removeSystem(nothingSystem);
|
||||
});
|
||||
|
||||
it('nothing 匹配器系统的 entities 应该为空', () => {
|
||||
const nothingSystem = new NothingSystem();
|
||||
scene.addSystem(nothingSystem);
|
||||
|
||||
// 即使场景中有实体,entities 也应该为空
|
||||
expect(nothingSystem.entities).toHaveLength(0);
|
||||
|
||||
scene.removeSystem(nothingSystem);
|
||||
});
|
||||
});
|
||||
|
||||
describe('系统更新顺序', () => {
|
||||
it('setUpdateOrder 应该能够设置更新顺序', () => {
|
||||
const plainSystem = new PlainEntitySystem();
|
||||
scene.addSystem(plainSystem);
|
||||
|
||||
expect(plainSystem.updateOrder).toBe(0);
|
||||
|
||||
plainSystem.setUpdateOrder(10);
|
||||
expect(plainSystem.updateOrder).toBe(10);
|
||||
|
||||
plainSystem.updateOrder = 20;
|
||||
expect(plainSystem.updateOrder).toBe(20);
|
||||
|
||||
scene.removeSystem(plainSystem);
|
||||
});
|
||||
|
||||
it('设置相同的 updateOrder 不应该触发重排序', () => {
|
||||
const plainSystem = new PlainEntitySystem();
|
||||
scene.addSystem(plainSystem);
|
||||
|
||||
plainSystem.setUpdateOrder(10);
|
||||
// 设置相同的值不应该有问题
|
||||
plainSystem.setUpdateOrder(10);
|
||||
|
||||
expect(plainSystem.updateOrder).toBe(10);
|
||||
|
||||
scene.removeSystem(plainSystem);
|
||||
});
|
||||
});
|
||||
|
||||
describe('系统启用/禁用', () => {
|
||||
it('禁用系统时不应该调用 process', () => {
|
||||
const plainSystem = new PlainEntitySystem();
|
||||
scene.addSystem(plainSystem);
|
||||
|
||||
plainSystem.enabled = false;
|
||||
plainSystem.update();
|
||||
|
||||
expect(plainSystem.processCallCount).toBe(0);
|
||||
|
||||
scene.removeSystem(plainSystem);
|
||||
});
|
||||
|
||||
it('重新启用系统后应该正常调用 process', () => {
|
||||
const plainSystem = new PlainEntitySystem();
|
||||
scene.addSystem(plainSystem);
|
||||
|
||||
plainSystem.enabled = false;
|
||||
plainSystem.update();
|
||||
expect(plainSystem.processCallCount).toBe(0);
|
||||
|
||||
plainSystem.enabled = true;
|
||||
plainSystem.update();
|
||||
expect(plainSystem.processCallCount).toBe(1);
|
||||
|
||||
scene.removeSystem(plainSystem);
|
||||
});
|
||||
});
|
||||
|
||||
describe('实体跟踪回调', () => {
|
||||
class TrackingSystem extends EntitySystem {
|
||||
public addedEntities: Entity[] = [];
|
||||
public removedEntities: Entity[] = [];
|
||||
|
||||
constructor() {
|
||||
super(Matcher.all(TestComponent));
|
||||
}
|
||||
|
||||
protected override process(entities: readonly Entity[]): void {
|
||||
// 处理实体
|
||||
}
|
||||
|
||||
protected override onAdded(entity: Entity): void {
|
||||
this.addedEntities.push(entity);
|
||||
}
|
||||
|
||||
protected override onRemoved(entity: Entity): void {
|
||||
this.removedEntities.push(entity);
|
||||
}
|
||||
}
|
||||
|
||||
it('当实体添加组件后应该触发 onAdded', () => {
|
||||
const trackingSystem = new TrackingSystem();
|
||||
scene.addSystem(trackingSystem);
|
||||
|
||||
// 创建新实体并添加组件
|
||||
const newEntity = scene.createEntity('new_entity');
|
||||
newEntity.addComponent(new TestComponent(5));
|
||||
scene.addEntity(newEntity);
|
||||
|
||||
// 触发更新以检测变化
|
||||
trackingSystem.update();
|
||||
|
||||
// 应该检测到新实体
|
||||
expect(trackingSystem.addedEntities.length).toBeGreaterThan(0);
|
||||
|
||||
scene.removeSystem(trackingSystem);
|
||||
});
|
||||
|
||||
it('当实体移除组件后应该触发 onRemoved', () => {
|
||||
const trackingSystem = new TrackingSystem();
|
||||
scene.addSystem(trackingSystem);
|
||||
|
||||
// 先触发更新来跟踪现有实体
|
||||
trackingSystem.update();
|
||||
|
||||
// 移除组件
|
||||
const comp = entity.getComponent(TestComponent);
|
||||
if (comp) {
|
||||
entity.removeComponent(comp);
|
||||
}
|
||||
|
||||
// 再次更新
|
||||
trackingSystem.update();
|
||||
|
||||
// 应该检测到实体被移除
|
||||
expect(trackingSystem.removedEntities.length).toBeGreaterThan(0);
|
||||
|
||||
scene.removeSystem(trackingSystem);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reset 方法', () => {
|
||||
it('reset 后系统应该可以重新初始化', () => {
|
||||
const plainSystem = new PlainEntitySystem();
|
||||
scene.addSystem(plainSystem);
|
||||
|
||||
// 调用 reset
|
||||
plainSystem.reset();
|
||||
|
||||
// 验证系统被重置
|
||||
expect(plainSystem.scene).toBeNull();
|
||||
});
|
||||
|
||||
it('已销毁的系统调用 reset 不应该执行任何操作', () => {
|
||||
const plainSystem = new PlainEntitySystem();
|
||||
scene.addSystem(plainSystem);
|
||||
|
||||
// 先销毁系统
|
||||
plainSystem.destroy();
|
||||
|
||||
// reset 不应该有任何效果
|
||||
plainSystem.reset();
|
||||
|
||||
// onDestroy 只应被调用一次
|
||||
expect(plainSystem.onDestroyCallCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('辅助方法', () => {
|
||||
class HelperSystem extends EntitySystem {
|
||||
constructor() {
|
||||
super(Matcher.all(TestComponent));
|
||||
}
|
||||
|
||||
protected override process(entities: readonly Entity[]): void {}
|
||||
|
||||
// 暴露 protected 方法供测试
|
||||
public testRequireComponent<T extends new (...args: any[]) => Component>(
|
||||
entity: Entity,
|
||||
componentType: T
|
||||
): InstanceType<T> {
|
||||
return this.requireComponent(entity, componentType) as InstanceType<T>;
|
||||
}
|
||||
|
||||
public testForEach(
|
||||
entities: readonly Entity[],
|
||||
processor: (entity: Entity, index: number) => void
|
||||
): void {
|
||||
this.forEach(entities, processor);
|
||||
}
|
||||
|
||||
public testFilterEntities(
|
||||
entities: readonly Entity[],
|
||||
predicate: (entity: Entity, index: number) => boolean
|
||||
): Entity[] {
|
||||
return this.filterEntities(entities, predicate);
|
||||
}
|
||||
|
||||
public testFindEntity(
|
||||
entities: readonly Entity[],
|
||||
predicate: (entity: Entity, index: number) => boolean
|
||||
): Entity | undefined {
|
||||
return this.findEntity(entities, predicate);
|
||||
}
|
||||
|
||||
public testSomeEntity(
|
||||
entities: readonly Entity[],
|
||||
predicate: (entity: Entity, index: number) => boolean
|
||||
): boolean {
|
||||
return this.someEntity(entities, predicate);
|
||||
}
|
||||
|
||||
public testEveryEntity(
|
||||
entities: readonly Entity[],
|
||||
predicate: (entity: Entity, index: number) => boolean
|
||||
): boolean {
|
||||
return this.everyEntity(entities, predicate);
|
||||
}
|
||||
}
|
||||
|
||||
let helperSystem: HelperSystem;
|
||||
|
||||
beforeEach(() => {
|
||||
helperSystem = new HelperSystem();
|
||||
scene.addSystem(helperSystem);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
scene.removeSystem(helperSystem);
|
||||
});
|
||||
|
||||
it('requireComponent 应该返回存在的组件', () => {
|
||||
const component = helperSystem.testRequireComponent(entity, TestComponent);
|
||||
expect(component).toBeDefined();
|
||||
expect(component.value).toBe(10);
|
||||
});
|
||||
|
||||
it('requireComponent 应该在组件不存在时抛出错误', () => {
|
||||
class NonExistentComponent extends Component {}
|
||||
|
||||
expect(() => {
|
||||
helperSystem.testRequireComponent(entity, NonExistentComponent);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
it('forEach 应该遍历所有实体', () => {
|
||||
const entities = [entity];
|
||||
const visited: Entity[] = [];
|
||||
|
||||
helperSystem.testForEach(entities, (e) => {
|
||||
visited.push(e);
|
||||
});
|
||||
|
||||
expect(visited).toHaveLength(1);
|
||||
expect(visited[0]).toBe(entity);
|
||||
});
|
||||
|
||||
it('filterEntities 应该正确过滤实体', () => {
|
||||
const entity2 = scene.createEntity('entity2');
|
||||
entity2.addComponent(new TestComponent(20));
|
||||
scene.addEntity(entity2);
|
||||
|
||||
const entities = [entity, entity2];
|
||||
|
||||
const filtered = helperSystem.testFilterEntities(entities, (e) => {
|
||||
const comp = e.getComponent(TestComponent);
|
||||
return comp !== null && comp.value > 15;
|
||||
});
|
||||
|
||||
expect(filtered).toHaveLength(1);
|
||||
expect(filtered[0]).toBe(entity2);
|
||||
});
|
||||
|
||||
it('findEntity 应该返回第一个匹配的实体', () => {
|
||||
const entities = [entity];
|
||||
|
||||
const found = helperSystem.testFindEntity(entities, (e) => {
|
||||
const comp = e.getComponent(TestComponent);
|
||||
return comp !== null && comp.value === 10;
|
||||
});
|
||||
|
||||
expect(found).toBe(entity);
|
||||
});
|
||||
|
||||
it('findEntity 应该在未找到时返回 undefined', () => {
|
||||
const entities = [entity];
|
||||
|
||||
const found = helperSystem.testFindEntity(entities, () => false);
|
||||
|
||||
expect(found).toBeUndefined();
|
||||
});
|
||||
|
||||
it('someEntity 应该正确判断是否存在匹配实体', () => {
|
||||
const entities = [entity];
|
||||
|
||||
const hasMatch = helperSystem.testSomeEntity(entities, (e) => {
|
||||
const comp = e.getComponent(TestComponent);
|
||||
return comp !== null && comp.value === 10;
|
||||
});
|
||||
|
||||
expect(hasMatch).toBe(true);
|
||||
|
||||
const noMatch = helperSystem.testSomeEntity(entities, () => false);
|
||||
expect(noMatch).toBe(false);
|
||||
});
|
||||
|
||||
it('everyEntity 应该正确判断是否所有实体都匹配', () => {
|
||||
const entities = [entity];
|
||||
|
||||
const allMatch = helperSystem.testEveryEntity(entities, (e) => {
|
||||
return e.getComponent(TestComponent) !== null;
|
||||
});
|
||||
|
||||
expect(allMatch).toBe(true);
|
||||
|
||||
const notAllMatch = helperSystem.testEveryEntity(entities, () => false);
|
||||
expect(notAllMatch).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -485,6 +485,59 @@ describe('Matcher', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('nothing() 匹配器', () => {
|
||||
test('nothing() 应该创建不匹配任何实体的匹配器', () => {
|
||||
const matcher = Matcher.nothing();
|
||||
const condition = matcher.getCondition();
|
||||
|
||||
expect(condition.matchNothing).toBe(true);
|
||||
expect(condition.all).toHaveLength(0);
|
||||
expect(condition.any).toHaveLength(0);
|
||||
expect(condition.none).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('isNothing() 应该正确判断 nothing 匹配器', () => {
|
||||
const nothingMatcher = Matcher.nothing();
|
||||
expect(nothingMatcher.isNothing()).toBe(true);
|
||||
|
||||
const normalMatcher = Matcher.all(Position);
|
||||
expect(normalMatcher.isNothing()).toBe(false);
|
||||
|
||||
const emptyMatcher = Matcher.empty();
|
||||
expect(emptyMatcher.isNothing()).toBe(false);
|
||||
});
|
||||
|
||||
test('isEmpty() 不应该将 nothing 匹配器视为空', () => {
|
||||
const nothingMatcher = Matcher.nothing();
|
||||
// nothing 匹配器有明确的语义,不应该算作空
|
||||
expect(nothingMatcher.isEmpty()).toBe(false);
|
||||
});
|
||||
|
||||
test('toString() 应该正确处理 nothing 匹配器', () => {
|
||||
const matcher = Matcher.nothing();
|
||||
const str = matcher.toString();
|
||||
|
||||
expect(str).toBe('Matcher[nothing]');
|
||||
});
|
||||
|
||||
test('clone() 应该正确复制 nothing 匹配器', () => {
|
||||
const original = Matcher.nothing();
|
||||
const cloned = original.clone();
|
||||
|
||||
expect(cloned.isNothing()).toBe(true);
|
||||
expect(cloned.getCondition().matchNothing).toBe(true);
|
||||
});
|
||||
|
||||
test('reset() 应该清除 matchNothing 标志', () => {
|
||||
const matcher = Matcher.nothing();
|
||||
expect(matcher.isNothing()).toBe(true);
|
||||
|
||||
matcher.reset();
|
||||
expect(matcher.isNothing()).toBe(false);
|
||||
expect(matcher.isEmpty()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('类型安全性', () => {
|
||||
test('ComponentType 应该正确工作', () => {
|
||||
// 这个测试主要是确保类型编译正确
|
||||
|
||||
Reference in New Issue
Block a user