Files
esengine/packages/platform-common/src/wasm/IWasmLibraryLoader.ts
YHH 63f006ab62 feat: 添加跨平台运行时、资产系统和UI适配功能 (#256)
* feat(platform-common): 添加WASM加载器和环境检测API

* feat(rapier2d): 新增Rapier2D WASM绑定包

* feat(physics-rapier2d): 添加跨平台WASM加载器

* feat(asset-system): 添加运行时资产目录和bundle格式

* feat(asset-system-editor): 新增编辑器资产管理包

* feat(editor-core): 添加构建系统和模块管理

* feat(editor-app): 重构浏览器预览使用import maps

* feat(platform-web): 添加BrowserRuntime和资产读取

* feat(engine): 添加材质系统和着色器管理

* feat(material): 新增材质系统和着色器编辑器

* feat(tilemap): 增强tilemap编辑器和动画系统

* feat(modules): 添加module.json配置

* feat(core): 添加module.json和类型定义更新

* chore: 更新依赖和构建配置

* refactor(plugins): 更新插件模板使用ModuleManifest

* chore: 添加第三方依赖库

* chore: 移除BehaviourTree-ai和ecs-astar子模块

* docs: 更新README和文档主题样式

* fix: 修复Rust文档测试和添加rapier2d WASM绑定

* fix(tilemap-editor): 修复画布高DPI屏幕分辨率适配问题

* feat(ui): 添加UI屏幕适配系统(CanvasScaler/SafeArea)

* fix(ecs-engine-bindgen): 添加缺失的ecs-framework-math依赖

* fix: 添加缺失的包依赖修复CI构建

* fix: 修复CodeQL检测到的代码问题

* fix: 修复构建错误和缺失依赖

* fix: 修复类型检查错误

* fix(material-system): 修复tsconfig配置支持TypeScript项目引用

* fix(editor-core): 修复Rollup构建配置添加tauri external

* fix: 修复CodeQL检测到的代码问题

* fix: 修复CodeQL检测到的代码问题
2025-12-03 22:15:22 +08:00

241 lines
5.1 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.
/**
* WASM 库平台适配层
*
* 提供统一的 WASM 库加载接口,屏蔽不同平台的差异
*
* 支持的平台:
* - Web 浏览器(标准 WebAssembly API
* - 微信小游戏WXWebAssembly
* - 字节跳动小游戏
* - 支付宝小游戏
* - 百度小游戏
*/
/**
* 平台类型枚举
*/
export enum PlatformType {
/** Web 浏览器 */
Web = 'web',
/** 微信小游戏 */
WeChatMiniGame = 'wechat-minigame',
/** 字节跳动小游戏 */
ByteDanceMiniGame = 'bytedance-minigame',
/** 支付宝小游戏 */
AlipayMiniGame = 'alipay-minigame',
/** 百度小游戏 */
BaiduMiniGame = 'baidu-minigame',
/** Node.js */
NodeJS = 'nodejs',
/** 未知平台 */
Unknown = 'unknown'
}
/**
* WASM 库加载配置
*
* @example
* ```typescript
* const config: WasmLibraryConfig = {
* name: 'Rapier2D',
* web: {
* useCompat: true, // Web 使用 compat 版本
* },
* minigame: {
* wasmPath: 'wasm/rapier2d_bg.wasm',
* needsTextDecoderPolyfill: true,
* }
* };
* ```
*/
export interface WasmLibraryConfig {
/**
* 库名称(用于日志和错误提示)
*/
name: string;
/**
* Web 平台配置
*/
web?: {
/**
* 使用 -compat 版本WASM 以 base64 嵌入 JS
*
* 优点:无需额外配置,开箱即用
* 缺点:包体积较大,首次加载慢
*/
useCompat?: boolean;
/**
* 模块路径(非 compat 版本时使用)
*/
modulePath?: string;
/**
* WASM 文件路径(非 compat 版本时使用)
*/
wasmPath?: string;
};
/**
* 小游戏平台配置
*/
minigame?: {
/**
* WASM 文件路径(相对于小游戏根目录)
*/
wasmPath: string;
/**
* JS glue 文件路径(可选)
*/
gluePath?: string;
/**
* 是否需要 TextDecoder polyfill
*
* iOS 微信小游戏通常需要此 polyfill
*/
needsTextDecoderPolyfill?: boolean;
/**
* 是否需要 TextEncoder polyfill
*
* iOS 微信小游戏通常需要此 polyfill
*/
needsTextEncoderPolyfill?: boolean;
};
/**
* 自定义初始化函数
*
* 用于库特定的初始化逻辑
*
* @param wasmInstance - WASM 实例
* @returns 初始化后的模块
*/
customInit?: (wasmInstance: any) => Promise<any>;
}
/**
* 平台信息
* Platform information
*/
export interface PlatformInfo {
/** 平台类型 | Platform type */
type: PlatformType;
/** 是否支持 WebAssembly | Supports WebAssembly */
supportsWasm: boolean;
/** 是否支持 SharedArrayBuffer | Supports SharedArrayBuffer */
supportsSharedArrayBuffer: boolean;
/** 需要安装的 polyfills 列表 | Required polyfills */
needsPolyfills: string[];
/**
* 是否在编辑器环境Tauri 桌面应用)
* Whether running in editor environment (Tauri desktop app)
*/
isEditor: boolean;
}
/**
* WASM 库加载器接口
*
* 每个 WASM 库需要实现此接口以支持跨平台加载
*
* @typeParam T - WASM 库模块类型
*
* @example
* ```typescript
* class Rapier2DLoader implements IWasmLibraryLoader<typeof RAPIER> {
* async load(): Promise<typeof RAPIER> {
* const RAPIER = await import('@dimforge/rapier2d-compat');
* await RAPIER.init();
* return RAPIER;
* }
*
* isSupported(): boolean {
* return typeof WebAssembly !== 'undefined';
* }
*
* getPlatformInfo(): PlatformInfo {
* return {
* type: PlatformType.Web,
* supportsWasm: true,
* supportsSharedArrayBuffer: true,
* needsPolyfills: []
* };
* }
* }
* ```
*/
export interface IWasmLibraryLoader<T> {
/**
* 加载 WASM 库
*
* @returns 加载完成的库模块
* @throws 如果加载失败或平台不支持
*/
load(): Promise<T>;
/**
* 检查当前平台是否支持此库
*
* @returns 是否支持
*/
isSupported(): boolean;
/**
* 获取当前平台信息
*
* @returns 平台信息
*/
getPlatformInfo(): PlatformInfo;
/**
* 获取库配置
*
* @returns 库配置
*/
getConfig(): WasmLibraryConfig;
}
/**
* 平台特定 WASM 加载器接口
*
* 提供平台级别的 WASM 加载能力
*/
export interface IPlatformWasmLoader {
/**
* 平台类型
*/
readonly platformType: PlatformType;
/**
* 加载 WASM 模块
*
* @param wasmPath - WASM 文件路径
* @param imports - WASM 导入对象
* @returns WASM 实例
*/
loadWasmModule(
wasmPath: string,
imports?: WebAssembly.Imports
): Promise<WebAssembly.Instance>;
/**
* 检查是否支持 WASM
*
* @returns 是否支持
*/
isSupported(): boolean;
/**
* 安装必要的 polyfills
*/
installPolyfills(): void;
}