Files
esengine/packages/asset-system-editor/src/meta/AssetMetaFile.ts
YHH a716d8006c fix(build): 修复 Web 构建组件注册和用户脚本打包问题 (#302)
* refactor(build): 重构 Web 构建管线,支持配置驱动的 Import Maps

- 重构 WebBuildPipeline 支持 split-bundles 和 single-bundle 两种构建模式
- 使用 module.json 的 isCore 字段识别核心模块,消除硬编码列表
- 动态生成 Import Map,从模块清单的 name 字段获取包名映射
- 动态扫描 module.json 文件,不再依赖固定模块列表
- 添加 HTTP 服务器启动脚本 (start-server.bat/sh) 支持 ESM 模块
- 更新 BuildSettingsPanel UI 支持新的构建模式选项
- 添加多语言支持 (zh/en/es)

* fix(build): 修复 Web 构建组件注册和用户脚本打包问题

主要修复:
- 修复组件反序列化时找不到类型的问题
- @ECSComponent 装饰器现在自动注册到 ComponentRegistry
- 添加未使用装饰器的组件警告
- 构建管线自动扫描用户脚本(无需入口文件)

架构改进:
- 解决 Decorators ↔ ComponentRegistry 循环依赖
- 新建 ComponentTypeUtils.ts 作为底层无依赖模块
- 移除冗余的防御性 register 调用
- 统一 ComponentType 定义位置

* refactor(build): 统一 WASM 配置架构,移除硬编码

- 新增 wasmConfig 统一配置替代 wasmPaths/wasmBindings
- wasmConfig.files 支持多候选源路径和明确目标路径
- wasmConfig.runtimePath 指定运行时加载路径
- 重构 _copyWasmFiles 使用统一配置
- HTML 生成使用配置中的 runtimePath
- 移除 physics-rapier2d 的冗余 WASM 配置(由 rapier2d 负责)
- IBuildFileSystem 新增 deleteFile 方法

* feat(build): 单文件构建模式完善和场景配置驱动

## 主要改动

### 单文件构建(single-file mode)
- 修复 WASM 初始化问题,支持 initSync 同步初始化
- 配置驱动的 WASM 识别,通过 wasmConfig.isEngineCore 标识核心引擎模块
- 从 wasmConfig.files 动态获取 JS 绑定路径,消除硬编码

### 场景配置
- 构建验证:必须选择至少一个场景才能构建
- 自动扫描:项目加载时扫描 scenes 目录
- 抽取 _filterScenesByWhitelist 公共方法统一过滤逻辑

### 构建面板优化
- availableScenes prop 传递场景列表
- 场景复选框可点击切换启用状态
- 移除动态 import,使用 prop 传入数据

* chore(build): 补充构建相关的辅助改动

- 添加 BuildFileSystemService 的 listFilesByExtension 优化
- 更新 module.json 添加 externalDependencies 配置
- BrowserRuntime 支持 wasmModule 参数传递
- GameRuntime 添加 loadSceneFromData 方法
- Rust 构建命令更新
- 国际化文案更新

* feat(build): 持久化构建设置到项目配置

## 设计架构

### ProjectService 扩展
- 新增 BuildSettingsConfig 接口定义构建配置字段
- ProjectConfig 添加 buildSettings 字段
- 新增 getBuildSettings / updateBuildSettings 方法

### BuildSettingsPanel
- 组件挂载时从 projectService 加载已保存配置
- 设置变化时自动保存(500ms 防抖)
- 场景选择状态与项目配置同步

### 配置保存位置
保存在项目的 ecs-editor.config.json 中:
- scenes: 选中的场景列表
- buildMode: 构建模式
- companyName/productName/version: 产品信息
- developmentBuild/sourceMap: 构建选项

* fix(editor): Ctrl+S 仅在主编辑区域触发保存场景

- 模态窗口打开时跳过(构建设置、设置、关于等)
- 焦点在 input/textarea/contenteditable 时跳过

* fix(tests): 修复 ECS 测试中 Component 注册问题

- 为所有测试 Component 类添加 @ECSComponent 装饰器
- 移除 beforeEach 中的 ComponentRegistry.reset() 调用
- 将内联 Component 类移到文件顶层以支持装饰器
- 更新测试预期值匹配新的组件类型名称
- 添加缺失的 HierarchyComponent 导入

所有 1388 个测试现已通过。
2025-12-10 18:23:29 +08:00

416 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Asset Meta File (.meta) Management
* 资产元数据文件 (.meta) 管理
*
* Each asset file has a companion .meta file that stores:
* - GUID: Persistent unique identifier
* - Import settings: How to process the asset
* - Labels: User-defined tags
*
* 每个资产文件都有一个配套的 .meta 文件,存储:
* - GUID持久化唯一标识符
* - 导入设置:如何处理资产
* - 标签:用户定义的标签
*/
import {
AssetGUID,
AssetType,
generateGUID
} from '@esengine/asset-system';
/**
* Meta file content structure
* 元数据文件内容结构
*/
export interface IAssetMeta {
/** Persistent unique identifier | 持久化唯一标识符 */
guid: AssetGUID;
/** Asset type | 资产类型 */
type: AssetType;
/**
* Explicit loader type override
* 显式指定的加载器类型覆盖
*
* When set, this type will be used instead of extension-based detection.
* Useful when file extension doesn't match the actual content type.
*
* 设置后,将使用此类型而非基于扩展名的检测。
* 适用于文件扩展名与实际内容类型不匹配的情况。
*/
loaderType?: string;
/** Import settings | 导入设置 */
importSettings?: IImportSettings;
/** User-defined labels | 用户定义的标签 */
labels?: string[];
/** Meta file version | 元数据文件版本 */
version: number;
/** Last modified timestamp | 最后修改时间戳 */
lastModified?: number;
}
/**
* Import settings for different asset types
* 不同资产类型的导入设置
*/
export interface IImportSettings {
// Texture settings | 纹理设置
maxSize?: number;
compression?: 'none' | 'dxt' | 'etc2' | 'astc' | 'webp';
generateMipmaps?: boolean;
filterMode?: 'point' | 'bilinear' | 'trilinear';
wrapMode?: 'clamp' | 'repeat' | 'mirror';
premultiplyAlpha?: boolean;
// Audio settings | 音频设置
audioFormat?: 'mp3' | 'ogg' | 'wav';
sampleRate?: number;
channels?: 1 | 2;
normalize?: boolean;
// General settings | 通用设置
[key: string]: unknown;
}
/**
* Get meta file path for an asset
* 获取资产的元数据文件路径
*/
export function getMetaFilePath(assetPath: string): string {
return `${assetPath}.meta`;
}
/**
* Infer asset type from file extension
* 根据文件扩展名推断资产类型
*/
export function inferAssetType(path: string): AssetType {
const ext = path.split('.').pop()?.toLowerCase() || '';
const typeMap: Record<string, AssetType> = {
// Textures
png: 'texture',
jpg: 'texture',
jpeg: 'texture',
gif: 'texture',
webp: 'texture',
bmp: 'texture',
svg: 'texture',
// Audio
mp3: 'audio',
wav: 'audio',
ogg: 'audio',
m4a: 'audio',
flac: 'audio',
// Data
json: 'json',
txt: 'text',
xml: 'text',
csv: 'text',
// Scenes and prefabs
ecs: 'scene',
prefab: 'prefab',
// Fonts
ttf: 'font',
otf: 'font',
woff: 'font',
woff2: 'font',
// Shaders
glsl: 'shader',
vert: 'shader',
frag: 'shader',
// Custom types (plugins)
tilemap: 'tilemap',
tileset: 'tileset',
btree: 'behavior-tree',
bp: 'blueprint',
mat: 'material',
particle: 'particle'
};
return typeMap[ext] || 'binary';
}
/**
* Get default import settings for asset type
* 获取资产类型的默认导入设置
*/
export function getDefaultImportSettings(type: AssetType): IImportSettings {
switch (type) {
case 'texture':
return {
maxSize: 2048,
compression: 'none',
generateMipmaps: false,
filterMode: 'bilinear',
wrapMode: 'clamp',
premultiplyAlpha: false
};
case 'audio':
return {
audioFormat: 'mp3',
sampleRate: 44100,
channels: 2,
normalize: false
};
default:
return {};
}
}
/**
* Create a new meta file content
* 创建新的元数据文件内容
*/
export function createAssetMeta(assetPath: string, overrides?: Partial<IAssetMeta>): IAssetMeta {
const type = overrides?.type || inferAssetType(assetPath);
return {
guid: overrides?.guid || generateGUID(),
type,
importSettings: overrides?.importSettings || getDefaultImportSettings(type),
labels: overrides?.labels || [],
version: 1,
lastModified: Date.now()
};
}
/**
* Serialize meta to JSON string
* 将元数据序列化为 JSON 字符串
*/
export function serializeAssetMeta(meta: IAssetMeta): string {
return JSON.stringify(meta, null, 2);
}
/**
* Parse meta from JSON string
* 从 JSON 字符串解析元数据
*/
export function parseAssetMeta(json: string): IAssetMeta {
const meta = JSON.parse(json) as IAssetMeta;
// Validate required fields
if (!meta.guid || typeof meta.guid !== 'string') {
throw new Error('Invalid meta file: missing or invalid guid');
}
if (!meta.type || typeof meta.type !== 'string') {
throw new Error('Invalid meta file: missing or invalid type');
}
// Set defaults for optional fields
meta.version = meta.version || 1;
meta.labels = meta.labels || [];
meta.importSettings = meta.importSettings || {};
return meta;
}
/**
* Asset Meta File Manager
* 资产元数据文件管理器
*
* Handles reading/writing .meta files through a file system interface.
*/
export class AssetMetaManager {
private _cache = new Map<string, IAssetMeta>();
private _guidToPath = new Map<AssetGUID, string>();
/**
* File system interface for reading/writing files
* 用于读写文件的文件系统接口
*/
private _fs: IMetaFileSystem | null = null;
/**
* Set file system interface
* 设置文件系统接口
*/
setFileSystem(fs: IMetaFileSystem): void {
this._fs = fs;
}
/**
* Get or create meta for an asset
* 获取或创建资产的元数据
*/
async getOrCreateMeta(assetPath: string): Promise<IAssetMeta> {
// Check cache first
const cached = this._cache.get(assetPath);
if (cached) {
return cached;
}
const metaPath = getMetaFilePath(assetPath);
// Try to read existing meta file
if (this._fs) {
try {
if (await this._fs.exists(metaPath)) {
const content = await this._fs.readText(metaPath);
const meta = parseAssetMeta(content);
this._cache.set(assetPath, meta);
this._guidToPath.set(meta.guid, assetPath);
return meta;
}
} catch (e) {
console.warn(`Failed to read meta file: ${metaPath}`, e);
}
}
// Create new meta
const meta = createAssetMeta(assetPath);
this._cache.set(assetPath, meta);
this._guidToPath.set(meta.guid, assetPath);
// Save to file system
if (this._fs) {
try {
await this._fs.writeText(metaPath, serializeAssetMeta(meta));
} catch (e) {
console.warn(`Failed to write meta file: ${metaPath}`, e);
}
}
return meta;
}
/**
* Get meta by GUID
* 根据 GUID 获取元数据
*/
getMetaByGUID(guid: AssetGUID): IAssetMeta | undefined {
const path = this._guidToPath.get(guid);
return path ? this._cache.get(path) : undefined;
}
/**
* Get asset path by GUID
* 根据 GUID 获取资产路径
*/
getPathByGUID(guid: AssetGUID): string | undefined {
return this._guidToPath.get(guid);
}
/**
* Get GUID by asset path
* 根据资产路径获取 GUID
*/
async getGUIDByPath(assetPath: string): Promise<AssetGUID> {
const meta = await this.getOrCreateMeta(assetPath);
return meta.guid;
}
/**
* Update meta and save
* 更新元数据并保存
*/
async updateMeta(assetPath: string, updates: Partial<IAssetMeta>): Promise<void> {
const meta = await this.getOrCreateMeta(assetPath);
// Apply updates
Object.assign(meta, updates);
meta.lastModified = Date.now();
meta.version++;
// Update cache
this._cache.set(assetPath, meta);
// Handle GUID change (rare, but possible)
if (updates.guid && updates.guid !== meta.guid) {
this._guidToPath.delete(meta.guid);
this._guidToPath.set(updates.guid, assetPath);
}
// Save to file system
if (this._fs) {
const metaPath = getMetaFilePath(assetPath);
await this._fs.writeText(metaPath, serializeAssetMeta(meta));
}
}
/**
* Handle asset rename
* 处理资产重命名
*/
async handleAssetRename(oldPath: string, newPath: string): Promise<void> {
const meta = this._cache.get(oldPath);
if (meta) {
// Update cache with new path
this._cache.delete(oldPath);
this._cache.set(newPath, meta);
this._guidToPath.set(meta.guid, newPath);
// Move meta file
if (this._fs) {
const oldMetaPath = getMetaFilePath(oldPath);
const newMetaPath = getMetaFilePath(newPath);
if (await this._fs.exists(oldMetaPath)) {
const content = await this._fs.readText(oldMetaPath);
await this._fs.writeText(newMetaPath, content);
await this._fs.delete(oldMetaPath);
}
}
}
}
/**
* Handle asset delete
* 处理资产删除
*/
async handleAssetDelete(assetPath: string): Promise<void> {
const meta = this._cache.get(assetPath);
if (meta) {
this._cache.delete(assetPath);
this._guidToPath.delete(meta.guid);
// Delete meta file
if (this._fs) {
const metaPath = getMetaFilePath(assetPath);
if (await this._fs.exists(metaPath)) {
await this._fs.delete(metaPath);
}
}
}
}
/**
* Clear cache
* 清除缓存
*/
clear(): void {
this._cache.clear();
this._guidToPath.clear();
}
/**
* Get all cached metas
* 获取所有缓存的元数据
*/
getAllMetas(): Map<string, IAssetMeta> {
return new Map(this._cache);
}
}
/**
* File system interface for meta file operations
* 元数据文件操作的文件系统接口
*/
export interface IMetaFileSystem {
exists(path: string): Promise<boolean>;
readText(path: string): Promise<string>;
writeText(path: string, content: string): Promise<void>;
delete(path: string): Promise<void>;
}