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检测到的代码问题
This commit is contained in:
235
packages/platform-common/src/polyfills/TextDecoderPolyfill.ts
Normal file
235
packages/platform-common/src/polyfills/TextDecoderPolyfill.ts
Normal file
@@ -0,0 +1,235 @@
|
||||
/**
|
||||
* TextDecoder polyfill
|
||||
*
|
||||
* 用于不原生支持 TextDecoder 的平台(如微信小游戏 iOS 环境)
|
||||
*
|
||||
* 支持以下编码格式:
|
||||
* - UTF-8(包含1-4字节字符、代理对)
|
||||
* - ASCII
|
||||
* - UTF-16LE
|
||||
*/
|
||||
class TextDecoderPolyfill {
|
||||
/**
|
||||
* 编码格式
|
||||
*/
|
||||
readonly encoding: string;
|
||||
|
||||
/**
|
||||
* 是否在遇到无效序列时抛出错误
|
||||
*/
|
||||
readonly fatal: boolean = false;
|
||||
|
||||
/**
|
||||
* 是否忽略 BOM(字节顺序标记)
|
||||
*/
|
||||
readonly ignoreBOM: boolean = false;
|
||||
|
||||
/**
|
||||
* 创建 TextDecoder 实例
|
||||
*
|
||||
* @param encoding - 编码格式,默认 'utf-8'
|
||||
* @param options - 解码选项
|
||||
*/
|
||||
constructor(encoding: string = 'utf-8', options?: TextDecoderOptions) {
|
||||
this.encoding = encoding.toLowerCase().replace('-', '');
|
||||
if (options?.fatal) {
|
||||
this.fatal = options.fatal;
|
||||
}
|
||||
if (options?.ignoreBOM) {
|
||||
this.ignoreBOM = options.ignoreBOM;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将二进制数据解码为字符串
|
||||
*
|
||||
* @param input - 要解码的二进制数据
|
||||
* @param options - 解码选项
|
||||
* @returns 解码后的字符串
|
||||
*/
|
||||
decode(input?: BufferSource | null, options?: TextDecodeOptions): string {
|
||||
if (!input) return '';
|
||||
|
||||
const bytes = input instanceof Uint8Array
|
||||
? input
|
||||
: input instanceof ArrayBuffer
|
||||
? new Uint8Array(input)
|
||||
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
||||
|
||||
if (this.encoding === 'utf8' || this.encoding === 'utf-8') {
|
||||
return this.decodeUTF8(bytes);
|
||||
}
|
||||
|
||||
if (this.encoding === 'ascii' || this.encoding === 'usascii') {
|
||||
return this.decodeASCII(bytes);
|
||||
}
|
||||
|
||||
if (this.encoding === 'utf16le' || this.encoding === 'utf-16le') {
|
||||
return this.decodeUTF16LE(bytes);
|
||||
}
|
||||
|
||||
// 降级:作为 ASCII 处理
|
||||
return this.decodeASCII(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码 UTF-8 数据
|
||||
*
|
||||
* @param bytes - 字节数组
|
||||
* @returns 解码后的字符串
|
||||
*/
|
||||
private decodeUTF8(bytes: Uint8Array): string {
|
||||
const result: string[] = [];
|
||||
let i = 0;
|
||||
|
||||
// 跳过 BOM(如果存在且不忽略)
|
||||
if (!this.ignoreBOM && bytes.length >= 3 &&
|
||||
bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF) {
|
||||
i = 3;
|
||||
}
|
||||
|
||||
while (i < bytes.length) {
|
||||
const byte1 = bytes[i++];
|
||||
|
||||
if (byte1 < 0x80) {
|
||||
// 1字节字符(ASCII: 0xxxxxxx)
|
||||
result.push(String.fromCharCode(byte1));
|
||||
} else if ((byte1 & 0xE0) === 0xC0) {
|
||||
// 2字节字符(110xxxxx 10xxxxxx)
|
||||
if (i >= bytes.length) {
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
break;
|
||||
}
|
||||
const byte2 = bytes[i++];
|
||||
if ((byte2 & 0xC0) !== 0x80) {
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
result.push(String.fromCharCode(
|
||||
((byte1 & 0x1F) << 6) | (byte2 & 0x3F)
|
||||
));
|
||||
} else if ((byte1 & 0xF0) === 0xE0) {
|
||||
// 3字节字符(1110xxxx 10xxxxxx 10xxxxxx)
|
||||
if (i + 1 >= bytes.length) {
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
break;
|
||||
}
|
||||
const byte2 = bytes[i++];
|
||||
const byte3 = bytes[i++];
|
||||
if ((byte2 & 0xC0) !== 0x80 || (byte3 & 0xC0) !== 0x80) {
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
i -= 2;
|
||||
continue;
|
||||
}
|
||||
result.push(String.fromCharCode(
|
||||
((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F)
|
||||
));
|
||||
} else if ((byte1 & 0xF8) === 0xF0) {
|
||||
// 4字节字符(11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
|
||||
if (i + 2 >= bytes.length) {
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
break;
|
||||
}
|
||||
const byte2 = bytes[i++];
|
||||
const byte3 = bytes[i++];
|
||||
const byte4 = bytes[i++];
|
||||
if ((byte2 & 0xC0) !== 0x80 || (byte3 & 0xC0) !== 0x80 || (byte4 & 0xC0) !== 0x80) {
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
i -= 3;
|
||||
continue;
|
||||
}
|
||||
// 计算码点并转换为代理对
|
||||
const codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) |
|
||||
((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
|
||||
if (codePoint > 0x10FFFF) {
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
continue;
|
||||
}
|
||||
const surrogate = codePoint - 0x10000;
|
||||
result.push(
|
||||
String.fromCharCode(0xD800 + (surrogate >> 10)),
|
||||
String.fromCharCode(0xDC00 + (surrogate & 0x3FF))
|
||||
);
|
||||
} else {
|
||||
// 无效字节
|
||||
if (this.fatal) throw new TypeError('无效的 UTF-8 序列');
|
||||
result.push('\uFFFD');
|
||||
}
|
||||
}
|
||||
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码 ASCII 数据
|
||||
*
|
||||
* @param bytes - 字节数组
|
||||
* @returns 解码后的字符串
|
||||
*/
|
||||
private decodeASCII(bytes: Uint8Array): string {
|
||||
const result: string[] = [];
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
result.push(String.fromCharCode(bytes[i] & 0x7F));
|
||||
}
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码 UTF-16LE 数据
|
||||
*
|
||||
* @param bytes - 字节数组
|
||||
* @returns 解码后的字符串
|
||||
*/
|
||||
private decodeUTF16LE(bytes: Uint8Array): string {
|
||||
const result: string[] = [];
|
||||
|
||||
// 跳过 BOM(如果存在)
|
||||
let i = 0;
|
||||
if (!this.ignoreBOM && bytes.length >= 2 &&
|
||||
bytes[0] === 0xFF && bytes[1] === 0xFE) {
|
||||
i = 2;
|
||||
}
|
||||
|
||||
for (; i + 1 < bytes.length; i += 2) {
|
||||
const codeUnit = bytes[i] | (bytes[i + 1] << 8);
|
||||
result.push(String.fromCharCode(codeUnit));
|
||||
}
|
||||
|
||||
return result.join('');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装 TextDecoder polyfill
|
||||
*
|
||||
* 如果当前环境不支持 TextDecoder,则安装 polyfill
|
||||
*
|
||||
* @returns 是否安装了 polyfill
|
||||
*/
|
||||
export function installTextDecoderPolyfill(): boolean {
|
||||
if (typeof globalThis.TextDecoder === 'undefined') {
|
||||
(globalThis as any).TextDecoder = TextDecoderPolyfill;
|
||||
console.log('[Polyfill] TextDecoder 已安装');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 TextDecoder 是否可用(原生或 polyfill)
|
||||
*
|
||||
* @returns 是否可用
|
||||
*/
|
||||
export function isTextDecoderAvailable(): boolean {
|
||||
return typeof globalThis.TextDecoder !== 'undefined';
|
||||
}
|
||||
|
||||
export { TextDecoderPolyfill };
|
||||
167
packages/platform-common/src/polyfills/TextEncoderPolyfill.ts
Normal file
167
packages/platform-common/src/polyfills/TextEncoderPolyfill.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* TextEncoder polyfill
|
||||
*
|
||||
* 用于不原生支持 TextEncoder 的平台(如微信小游戏 iOS 环境)
|
||||
*
|
||||
* 支持 UTF-8 编码,包含:
|
||||
* - ASCII 字符(1字节输出)
|
||||
* - 扩展 Unicode(2-4字节输出)
|
||||
* - BMP 外字符的代理对处理
|
||||
*/
|
||||
class TextEncoderPolyfill {
|
||||
/**
|
||||
* 编码格式(始终为 'utf-8')
|
||||
*/
|
||||
readonly encoding: string = 'utf-8';
|
||||
|
||||
/**
|
||||
* 将字符串编码为 UTF-8 字节数组
|
||||
*
|
||||
* @param input - 要编码的字符串
|
||||
* @returns UTF-8 编码的字节数组
|
||||
*/
|
||||
encode(input: string = ''): Uint8Array {
|
||||
const bytes: number[] = [];
|
||||
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
let codePoint = input.charCodeAt(i);
|
||||
|
||||
// 处理代理对(BMP 外的字符)
|
||||
if (codePoint >= 0xD800 && codePoint <= 0xDBFF) {
|
||||
// 高代理项
|
||||
if (i + 1 < input.length) {
|
||||
const next = input.charCodeAt(i + 1);
|
||||
if (next >= 0xDC00 && next <= 0xDFFF) {
|
||||
// 低代理项 - 组合成完整码点
|
||||
codePoint = 0x10000 + ((codePoint - 0xD800) << 10) + (next - 0xDC00);
|
||||
i++; // 跳过低代理项
|
||||
}
|
||||
}
|
||||
} else if (codePoint >= 0xDC00 && codePoint <= 0xDFFF) {
|
||||
// 孤立的低代理项 - 替换为替换字符
|
||||
codePoint = 0xFFFD;
|
||||
}
|
||||
|
||||
if (codePoint < 0x80) {
|
||||
// 1字节字符(ASCII)
|
||||
bytes.push(codePoint);
|
||||
} else if (codePoint < 0x800) {
|
||||
// 2字节字符
|
||||
bytes.push(0xC0 | (codePoint >> 6));
|
||||
bytes.push(0x80 | (codePoint & 0x3F));
|
||||
} else if (codePoint < 0x10000) {
|
||||
// 3字节字符
|
||||
bytes.push(0xE0 | (codePoint >> 12));
|
||||
bytes.push(0x80 | ((codePoint >> 6) & 0x3F));
|
||||
bytes.push(0x80 | (codePoint & 0x3F));
|
||||
} else if (codePoint <= 0x10FFFF) {
|
||||
// 4字节字符
|
||||
bytes.push(0xF0 | (codePoint >> 18));
|
||||
bytes.push(0x80 | ((codePoint >> 12) & 0x3F));
|
||||
bytes.push(0x80 | ((codePoint >> 6) & 0x3F));
|
||||
bytes.push(0x80 | (codePoint & 0x3F));
|
||||
} else {
|
||||
// 无效码点 - 使用替换字符
|
||||
bytes.push(0xEF, 0xBF, 0xBD); // U+FFFD 的 UTF-8 编码
|
||||
}
|
||||
}
|
||||
|
||||
return new Uint8Array(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串编码到目标缓冲区
|
||||
*
|
||||
* 尽可能多地将源字符串编码到目标缓冲区中
|
||||
*
|
||||
* @param source - 要编码的字符串
|
||||
* @param destination - 目标缓冲区
|
||||
* @returns 包含已读取字符数和已写入字节数的对象
|
||||
*/
|
||||
encodeInto(source: string, destination: Uint8Array): TextEncoderEncodeIntoResult {
|
||||
let read = 0;
|
||||
let written = 0;
|
||||
|
||||
for (let i = 0; i < source.length; i++) {
|
||||
let codePoint = source.charCodeAt(i);
|
||||
let bytesNeeded: number;
|
||||
|
||||
// 处理代理对
|
||||
if (codePoint >= 0xD800 && codePoint <= 0xDBFF && i + 1 < source.length) {
|
||||
const next = source.charCodeAt(i + 1);
|
||||
if (next >= 0xDC00 && next <= 0xDFFF) {
|
||||
codePoint = 0x10000 + ((codePoint - 0xD800) << 10) + (next - 0xDC00);
|
||||
}
|
||||
}
|
||||
|
||||
// 计算所需字节数
|
||||
if (codePoint < 0x80) {
|
||||
bytesNeeded = 1;
|
||||
} else if (codePoint < 0x800) {
|
||||
bytesNeeded = 2;
|
||||
} else if (codePoint < 0x10000) {
|
||||
bytesNeeded = 3;
|
||||
} else {
|
||||
bytesNeeded = 4;
|
||||
}
|
||||
|
||||
// 检查是否有足够空间
|
||||
if (written + bytesNeeded > destination.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
// 写入字节
|
||||
if (codePoint < 0x80) {
|
||||
destination[written++] = codePoint;
|
||||
} else if (codePoint < 0x800) {
|
||||
destination[written++] = 0xC0 | (codePoint >> 6);
|
||||
destination[written++] = 0x80 | (codePoint & 0x3F);
|
||||
} else if (codePoint < 0x10000) {
|
||||
destination[written++] = 0xE0 | (codePoint >> 12);
|
||||
destination[written++] = 0x80 | ((codePoint >> 6) & 0x3F);
|
||||
destination[written++] = 0x80 | (codePoint & 0x3F);
|
||||
} else {
|
||||
destination[written++] = 0xF0 | (codePoint >> 18);
|
||||
destination[written++] = 0x80 | ((codePoint >> 12) & 0x3F);
|
||||
destination[written++] = 0x80 | ((codePoint >> 6) & 0x3F);
|
||||
destination[written++] = 0x80 | (codePoint & 0x3F);
|
||||
}
|
||||
|
||||
read++;
|
||||
// 如果处理了代理对,跳过低代理项
|
||||
if (codePoint >= 0x10000) {
|
||||
read++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return { read, written };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装 TextEncoder polyfill
|
||||
*
|
||||
* 如果当前环境不支持 TextEncoder,则安装 polyfill
|
||||
*
|
||||
* @returns 是否安装了 polyfill
|
||||
*/
|
||||
export function installTextEncoderPolyfill(): boolean {
|
||||
if (typeof globalThis.TextEncoder === 'undefined') {
|
||||
(globalThis as any).TextEncoder = TextEncoderPolyfill;
|
||||
console.log('[Polyfill] TextEncoder 已安装');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 TextEncoder 是否可用(原生或 polyfill)
|
||||
*
|
||||
* @returns 是否可用
|
||||
*/
|
||||
export function isTextEncoderAvailable(): boolean {
|
||||
return typeof globalThis.TextEncoder !== 'undefined';
|
||||
}
|
||||
|
||||
export { TextEncoderPolyfill };
|
||||
58
packages/platform-common/src/polyfills/index.ts
Normal file
58
packages/platform-common/src/polyfills/index.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 平台 polyfills
|
||||
*
|
||||
* 提供跨平台兼容性支持,用于填补不同平台的 API 差异
|
||||
*/
|
||||
|
||||
export {
|
||||
TextDecoderPolyfill,
|
||||
installTextDecoderPolyfill,
|
||||
isTextDecoderAvailable
|
||||
} from './TextDecoderPolyfill';
|
||||
|
||||
export {
|
||||
TextEncoderPolyfill,
|
||||
installTextEncoderPolyfill,
|
||||
isTextEncoderAvailable
|
||||
} from './TextEncoderPolyfill';
|
||||
|
||||
import { installTextDecoderPolyfill } from './TextDecoderPolyfill';
|
||||
import { installTextEncoderPolyfill } from './TextEncoderPolyfill';
|
||||
|
||||
/**
|
||||
* 安装当前平台所需的所有 polyfills
|
||||
*
|
||||
* @returns 已安装的 polyfill 列表
|
||||
*/
|
||||
export function installAllPolyfills(): { installed: string[] } {
|
||||
const installed: string[] = [];
|
||||
|
||||
if (installTextDecoderPolyfill()) {
|
||||
installed.push('TextDecoder');
|
||||
}
|
||||
|
||||
if (installTextEncoderPolyfill()) {
|
||||
installed.push('TextEncoder');
|
||||
}
|
||||
|
||||
return { installed };
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前平台需要哪些 polyfills
|
||||
*
|
||||
* @returns 需要的 polyfill 名称列表
|
||||
*/
|
||||
export function getRequiredPolyfills(): string[] {
|
||||
const required: string[] = [];
|
||||
|
||||
if (typeof globalThis.TextDecoder === 'undefined') {
|
||||
required.push('TextDecoder');
|
||||
}
|
||||
|
||||
if (typeof globalThis.TextEncoder === 'undefined') {
|
||||
required.push('TextEncoder');
|
||||
}
|
||||
|
||||
return required;
|
||||
}
|
||||
Reference in New Issue
Block a user