refactor: reorganize package structure and decouple framework packages (#338)

* refactor: reorganize package structure and decouple framework packages

## Package Structure Reorganization
- Reorganized 55 packages into categorized subdirectories:
  - packages/framework/ - Generic framework (Laya/Cocos compatible)
  - packages/engine/ - ESEngine core modules
  - packages/rendering/ - Rendering modules (WASM dependent)
  - packages/physics/ - Physics modules
  - packages/streaming/ - World streaming
  - packages/network-ext/ - Network extensions
  - packages/editor/ - Editor framework and plugins
  - packages/rust/ - Rust WASM engine
  - packages/tools/ - Build tools and SDK

## Framework Package Decoupling
- Decoupled behavior-tree and blueprint packages from ESEngine dependencies
- Created abstracted interfaces (IBTAssetManager, IBehaviorTreeAssetContent)
- ESEngine-specific code moved to esengine/ subpath exports
- Framework packages now usable with Cocos/Laya without ESEngine

## CI Configuration
- Updated CI to only type-check and lint framework packages
- Added type-check:framework and lint:framework scripts

## Breaking Changes
- Package import paths changed due to directory reorganization
- ESEngine integrations now use subpath imports (e.g., '@esengine/behavior-tree/esengine')

* fix: update es-engine file path after directory reorganization

* docs: update README to focus on framework over engine

* ci: only build framework packages, remove Rust/WASM dependencies

* fix: remove esengine subpath from behavior-tree and blueprint builds

ESEngine integration code will only be available in full engine builds.
Framework packages are now purely engine-agnostic.

* fix: move network-protocols to framework, build both in CI

* fix: update workflow paths from packages/core to packages/framework/core

* fix: exclude esengine folder from type-check in behavior-tree and blueprint

* fix: update network tsconfig references to new paths

* fix: add test:ci:framework to only test framework packages in CI

* fix: only build core and math npm packages in CI

* fix: exclude test files from CodeQL and fix string escaping security issue
This commit is contained in:
YHH
2025-12-26 14:50:35 +08:00
committed by GitHub
parent a84ff902e4
commit 155411e743
1936 changed files with 4147 additions and 11578 deletions

View File

@@ -0,0 +1,388 @@
import { ERelationType } from '../core/FieldTypes';
import type { GObject } from '../core/GObject';
import { FGUIEvents } from '../events/Events';
/**
* Relation definition
* 关联定义
*/
interface RelationDef {
relationType: ERelationType;
usePercent: boolean;
percent: number;
}
/**
* RelationItem
*
* Represents a single relation constraint between two objects.
*
* 表示两个对象之间的单个关联约束
*/
export class RelationItem {
/** Owner object | 所有者对象 */
public readonly owner: GObject;
private _target: GObject | null = null;
private _relations: RelationDef[] = [];
private _targetX: number = 0;
private _targetY: number = 0;
private _targetWidth: number = 0;
private _targetHeight: number = 0;
constructor(owner: GObject) {
this.owner = owner;
}
/**
* Get target object
* 获取目标对象
*/
public get target(): GObject | null {
return this._target;
}
/**
* Set target object
* 设置目标对象
*/
public set target(value: GObject | null) {
if (this._target !== value) {
if (this._target) {
this.releaseRefTarget(this._target);
}
this._target = value;
if (this._target) {
this.addRefTarget(this._target);
}
}
}
/**
* Add a relation
* 添加关联
*/
public add(relationType: ERelationType, bUsePercent: boolean): void {
if (relationType === ERelationType.Size) {
this.add(ERelationType.Width, bUsePercent);
this.add(ERelationType.Height, bUsePercent);
return;
}
const existing = this._relations.find(r => r.relationType === relationType);
if (existing) {
existing.usePercent = bUsePercent;
} else {
this._relations.push({
relationType,
usePercent: bUsePercent,
percent: 0
});
}
this.internalAdd(relationType, bUsePercent);
}
/**
* Internal add relation (used by Relations.setup)
* 内部添加关联(由 Relations.setup 使用)
*/
public internalAdd(relationType: ERelationType, bUsePercent: boolean): void {
// Add the relation definition if it doesn't exist
let def = this._relations.find(r => r.relationType === relationType);
if (!def) {
def = {
relationType,
usePercent: bUsePercent,
percent: 0
};
this._relations.push(def);
} else {
def.usePercent = bUsePercent;
}
if (!this._target) return;
// Calculate initial percent if needed
if (bUsePercent) {
switch (relationType) {
case ERelationType.LeftLeft:
case ERelationType.LeftCenter:
case ERelationType.LeftRight:
case ERelationType.CenterCenter:
case ERelationType.RightLeft:
case ERelationType.RightCenter:
case ERelationType.RightRight:
if (this._targetWidth > 0) {
def.percent = this.owner.x / this._targetWidth;
}
break;
case ERelationType.TopTop:
case ERelationType.TopMiddle:
case ERelationType.TopBottom:
case ERelationType.MiddleMiddle:
case ERelationType.BottomTop:
case ERelationType.BottomMiddle:
case ERelationType.BottomBottom:
if (this._targetHeight > 0) {
def.percent = this.owner.y / this._targetHeight;
}
break;
case ERelationType.Width:
if (this._targetWidth > 0) {
def.percent = this.owner.width / this._targetWidth;
}
break;
case ERelationType.Height:
if (this._targetHeight > 0) {
def.percent = this.owner.height / this._targetHeight;
}
break;
}
}
}
/**
* Remove a relation
* 移除关联
*/
public remove(relationType: ERelationType): void {
if (relationType === ERelationType.Size) {
this.remove(ERelationType.Width);
this.remove(ERelationType.Height);
return;
}
const index = this._relations.findIndex(r => r.relationType === relationType);
if (index !== -1) {
this._relations.splice(index, 1);
}
}
/**
* Check if empty
* 检查是否为空
*/
public isEmpty(): boolean {
return this._relations.length === 0;
}
/**
* Copy from another item
* 从另一个项复制
*/
public copyFrom(source: RelationItem): void {
this.target = source.target;
this._relations = source._relations.map(r => ({ ...r }));
}
private addRefTarget(target: GObject): void {
if (!target) return;
target.on(FGUIEvents.XY_CHANGED, this.onTargetXYChanged, this);
target.on(FGUIEvents.SIZE_CHANGED, this.onTargetSizeChanged, this);
this._targetX = target.x;
this._targetY = target.y;
this._targetWidth = target.width;
this._targetHeight = target.height;
}
private releaseRefTarget(target: GObject): void {
if (!target) return;
target.off(FGUIEvents.XY_CHANGED, this.onTargetXYChanged);
target.off(FGUIEvents.SIZE_CHANGED, this.onTargetSizeChanged);
}
private onTargetXYChanged(): void {
if (!this._target || this.owner._gearLocked) return;
const ox = this._targetX;
const oy = this._targetY;
this._targetX = this._target.x;
this._targetY = this._target.y;
this.applyOnXYChanged(this._targetX - ox, this._targetY - oy);
}
private onTargetSizeChanged(): void {
if (!this._target || this.owner._gearLocked) return;
const ow = this._targetWidth;
const oh = this._targetHeight;
this._targetWidth = this._target.width;
this._targetHeight = this._target.height;
this.applyOnSizeChanged(this._targetWidth - ow, this._targetHeight - oh);
}
/**
* Apply relations when target position changed
* 当目标位置改变时应用关联
*/
public applyOnXYChanged(dx: number, dy: number): void {
for (const def of this._relations) {
switch (def.relationType) {
case ERelationType.LeftLeft:
case ERelationType.LeftCenter:
case ERelationType.LeftRight:
case ERelationType.CenterCenter:
case ERelationType.RightLeft:
case ERelationType.RightCenter:
case ERelationType.RightRight:
this.owner.x += dx;
break;
case ERelationType.TopTop:
case ERelationType.TopMiddle:
case ERelationType.TopBottom:
case ERelationType.MiddleMiddle:
case ERelationType.BottomTop:
case ERelationType.BottomMiddle:
case ERelationType.BottomBottom:
this.owner.y += dy;
break;
}
}
}
/**
* Apply relations when target size changed
* 当目标尺寸改变时应用关联
*/
public applyOnSizeChanged(dWidth: number, dHeight: number): void {
if (!this._target) return;
let ox = this.owner.x;
let oy = this.owner.y;
for (const def of this._relations) {
switch (def.relationType) {
case ERelationType.LeftLeft:
// No change needed
break;
case ERelationType.LeftCenter:
ox = this._target.width / 2 + (ox - this._targetWidth / 2);
break;
case ERelationType.LeftRight:
ox = this._target.width + (ox - this._targetWidth);
break;
case ERelationType.CenterCenter:
ox = this._target.width / 2 + (ox + this.owner.width / 2 - this._targetWidth / 2) - this.owner.width / 2;
break;
case ERelationType.RightLeft:
ox = ox + this.owner.width - this._target.width / 2 + (this._targetWidth / 2 - this.owner.width);
break;
case ERelationType.RightCenter:
ox = this._target.width / 2 + (ox + this.owner.width - this._targetWidth / 2) - this.owner.width;
break;
case ERelationType.RightRight:
ox = this._target.width + (ox + this.owner.width - this._targetWidth) - this.owner.width;
break;
case ERelationType.TopTop:
// No change needed
break;
case ERelationType.TopMiddle:
oy = this._target.height / 2 + (oy - this._targetHeight / 2);
break;
case ERelationType.TopBottom:
oy = this._target.height + (oy - this._targetHeight);
break;
case ERelationType.MiddleMiddle:
oy = this._target.height / 2 + (oy + this.owner.height / 2 - this._targetHeight / 2) - this.owner.height / 2;
break;
case ERelationType.BottomTop:
oy = oy + this.owner.height - this._target.height / 2 + (this._targetHeight / 2 - this.owner.height);
break;
case ERelationType.BottomMiddle:
oy = this._target.height / 2 + (oy + this.owner.height - this._targetHeight / 2) - this.owner.height;
break;
case ERelationType.BottomBottom:
oy = this._target.height + (oy + this.owner.height - this._targetHeight) - this.owner.height;
break;
case ERelationType.Width:
if (def.usePercent) {
this.owner.width = this._target.width * def.percent;
} else {
this.owner.width += dWidth;
}
break;
case ERelationType.Height:
if (def.usePercent) {
this.owner.height = this._target.height * def.percent;
} else {
this.owner.height += dHeight;
}
break;
}
}
if (ox !== this.owner.x || oy !== this.owner.y) {
this.owner.setXY(ox, oy);
}
}
/**
* Apply relations when owner resized
* 当所有者尺寸改变时应用关联
*/
public applyOnSelfResized(dWidth: number, dHeight: number, bApplyPivot: boolean): void {
if (!this._target) return;
for (const def of this._relations) {
switch (def.relationType) {
case ERelationType.CenterCenter:
this.owner.x -= dWidth / 2;
break;
case ERelationType.RightCenter:
case ERelationType.RightLeft:
case ERelationType.RightRight:
this.owner.x -= dWidth;
break;
case ERelationType.MiddleMiddle:
this.owner.y -= dHeight / 2;
break;
case ERelationType.BottomMiddle:
case ERelationType.BottomTop:
case ERelationType.BottomBottom:
this.owner.y -= dHeight;
break;
}
}
}
/**
* Dispose
* 销毁
*/
public dispose(): void {
if (this._target) {
this.releaseRefTarget(this._target);
this._target = null;
}
this._relations.length = 0;
}
}

View File

@@ -0,0 +1,184 @@
import { ERelationType } from '../core/FieldTypes';
import type { GObject } from '../core/GObject';
import type { GComponent } from '../core/GComponent';
import type { ByteBuffer } from '../utils/ByteBuffer';
import { RelationItem } from './RelationItem';
/**
* Relations
*
* Manages constraint-based layout relationships between UI objects.
*
* 管理 UI 对象之间的约束布局关系
*/
export class Relations {
/** Owner object | 所有者对象 */
public readonly owner: GObject;
/** Size dirty flag | 尺寸脏标记 */
public sizeDirty: boolean = false;
private _items: RelationItem[] = [];
constructor(owner: GObject) {
this.owner = owner;
}
/**
* Add a relation
* 添加关联
*/
public add(target: GObject, relationType: ERelationType, bUsePercent: boolean = false): void {
let item: RelationItem | null = null;
for (const existing of this._items) {
if (existing.target === target) {
item = existing;
break;
}
}
if (!item) {
item = new RelationItem(this.owner);
item.target = target;
this._items.push(item);
}
item.add(relationType, bUsePercent);
}
/**
* Remove a relation
* 移除关联
*/
public remove(target: GObject, relationType: ERelationType = ERelationType.Size): void {
for (let i = this._items.length - 1; i >= 0; i--) {
const item = this._items[i];
if (item.target === target) {
item.remove(relationType);
if (item.isEmpty()) {
this._items.splice(i, 1);
}
break;
}
}
}
/**
* Check if target has any relations
* 检查目标是否有任何关联
*/
public contains(target: GObject): boolean {
return this._items.some(item => item.target === target);
}
/**
* Clear relations with a target
* 清除与目标的所有关联
*/
public clearFor(target: GObject): void {
for (let i = this._items.length - 1; i >= 0; i--) {
if (this._items[i].target === target) {
this._items.splice(i, 1);
}
}
}
/**
* Clear all relations
* 清除所有关联
*/
public clearAll(): void {
for (const item of this._items) {
item.dispose();
}
this._items.length = 0;
}
/**
* Copy relations from another object
* 从另一个对象复制关联
*/
public copyFrom(source: Relations): void {
this.clearAll();
for (const item of source._items) {
const newItem = new RelationItem(this.owner);
newItem.copyFrom(item);
this._items.push(newItem);
}
}
/**
* Called when owner size changed
* 当所有者尺寸改变时调用
*/
public onOwnerSizeChanged(dWidth: number, dHeight: number, bApplyPivot: boolean): void {
for (const item of this._items) {
item.applyOnSelfResized(dWidth, dHeight, bApplyPivot);
}
}
/**
* Ensure relations size is correct
* 确保关联尺寸正确
*/
public ensureRelationsSizeCorrect(): void {
if (!this.sizeDirty) return;
this.sizeDirty = false;
for (const item of this._items) {
item.target?.ensureSizeCorrect();
}
}
/**
* Get items count
* 获取项目数量
*/
public get count(): number {
return this._items.length;
}
/**
* Setup relations from buffer
* 从缓冲区设置关联
*/
public setup(buffer: ByteBuffer, bParentToChild: boolean): void {
const cnt = buffer.readByte();
for (let i = 0; i < cnt; i++) {
const targetIndex = buffer.getInt16();
let target: GObject | null = null;
if (targetIndex === -1) {
target = this.owner.parent;
} else if (bParentToChild) {
target = (this.owner as GComponent).getChildAt(targetIndex);
} else if (this.owner.parent) {
target = this.owner.parent.getChildAt(targetIndex);
}
if (!target) continue;
const newItem = new RelationItem(this.owner);
newItem.target = target;
this._items.push(newItem);
const cnt2 = buffer.readByte();
for (let j = 0; j < cnt2; j++) {
const rt = buffer.readByte() as ERelationType;
const bUsePercent = buffer.readBool();
newItem.internalAdd(rt, bUsePercent);
}
}
}
/**
* Dispose
* 销毁
*/
public dispose(): void {
this.clearAll();
}
}