This commit is contained in:
YHH
2025-09-30 09:51:02 +08:00
parent 8c4e8d523e
commit 90153b98fe
4 changed files with 227 additions and 123 deletions

View File

@@ -10,7 +10,7 @@
-**SharedArrayBuffer**: 支持需要COOP/COEP头部
-**Transferable Objects**: 完全支持
-**高精度时间**: 使用 `performance.now()`
-**设备信息**: 完整的浏览器和设备信息
-**基础信息**: 浏览器版本和基本配置
## 完整实现
@@ -19,8 +19,7 @@ import type {
IPlatformAdapter,
PlatformWorker,
WorkerCreationOptions,
PlatformConfig,
BrowserDeviceInfo
PlatformConfig
} from '@esengine/ecs-framework';
/**
@@ -100,105 +99,33 @@ export class BrowserAdapter implements IPlatformAdapter {
public getPlatformConfig(): PlatformConfig {
return {
maxWorkerCount: this.getHardwareConcurrency(),
supportsModuleWorker: this.checkModuleWorkerSupport(),
supportsModuleWorker: false,
supportsTransferableObjects: true,
maxSharedArrayBufferSize: this.getMaxSharedArrayBufferSize(),
maxSharedArrayBufferSize: 1024 * 1024 * 1024, // 1GB
workerScriptPrefix: '',
limitations: {
noEval: false,
requiresWorkerInit: false
},
extensions: {
userAgent: navigator.userAgent,
vendor: navigator.vendor,
language: navigator.language,
cookieEnabled: navigator.cookieEnabled,
onLine: navigator.onLine
}
};
}
/**
* 获取浏览器设备信息
*/
public getDeviceInfo(): BrowserDeviceInfo {
const deviceInfo: BrowserDeviceInfo = {
// 浏览器信息
userAgent: navigator.userAgent,
vendor: navigator.vendor,
language: navigator.language,
languages: navigator.languages,
cookieEnabled: navigator.cookieEnabled,
onLine: navigator.onLine,
// 硬件信息
hardwareConcurrency: navigator.hardwareConcurrency,
deviceMemory: (navigator as any).deviceMemory,
maxTouchPoints: navigator.maxTouchPoints,
// 屏幕信息
screenWidth: screen.width,
screenHeight: screen.height,
availWidth: screen.availWidth,
availHeight: screen.availHeight,
colorDepth: screen.colorDepth,
pixelDepth: screen.pixelDepth,
// 窗口信息
innerWidth: window.innerWidth,
innerHeight: window.innerHeight,
outerWidth: window.outerWidth,
outerHeight: window.outerHeight,
devicePixelRatio: window.devicePixelRatio,
// 平台信息
platform: navigator.platform,
appVersion: navigator.appVersion,
appName: navigator.appName
};
// 连接信息(如果支持)
const connection = (navigator as any).connection;
if (connection) {
deviceInfo.connection = {
effectiveType: connection.effectiveType,
downlink: connection.downlink,
rtt: connection.rtt,
saveData: connection.saveData
};
}
return deviceInfo;
}
/**
* 获取浏览器信息
*/
private getBrowserInfo(): string {
const userAgent = navigator.userAgent;
let browserName = 'Unknown';
let version = 'Unknown';
// 简单的浏览器检测
if (userAgent.includes('Chrome')) {
browserName = 'Chrome';
const match = userAgent.match(/Chrome\/([0-9.]+)/);
if (match) version = match[1];
return match ? `Chrome ${match[1]}` : 'Chrome';
} else if (userAgent.includes('Firefox')) {
browserName = 'Firefox';
const match = userAgent.match(/Firefox\/([0-9.]+)/);
if (match) version = match[1];
if (match) return `Firefox ${match[1]}`;
} else if (userAgent.includes('Safari')) {
browserName = 'Safari';
const match = userAgent.match(/Version\/([0-9.]+)/);
if (match) version = match[1];
} else if (userAgent.includes('Edge')) {
browserName = 'Edge';
const match = userAgent.match(/Edge\/([0-9.]+)/);
if (match) version = match[1];
if (match) return `Safari ${match[1]}`;
}
return `${browserName} ${version}`;
return 'Unknown Browser';
}
/**
@@ -206,40 +133,12 @@ export class BrowserAdapter implements IPlatformAdapter {
*/
private checkSharedArrayBufferEnabled(): boolean {
try {
// 尝试创建一个小的SharedArrayBuffer来测试
new SharedArrayBuffer(8);
return true;
} catch {
return false;
}
}
/**
* 检查是否支持模块Worker
*/
private checkModuleWorkerSupport(): boolean {
try {
// 简单检测Chrome 80+、Firefox 114+等支持
return 'type' in Worker.prototype || false;
} catch {
return false;
}
}
/**
* 获取SharedArrayBuffer最大大小限制
*/
private getMaxSharedArrayBufferSize(): number {
// 浏览器通常限制为1GB或更少
const deviceMemory = (navigator as any).deviceMemory;
if (deviceMemory) {
// 限制为设备内存的25%
return Math.min(deviceMemory * 0.25 * 1024 * 1024 * 1024, 1024 * 1024 * 1024);
}
// 默认1GB
return 1024 * 1024 * 1024;
}
}
/**
@@ -411,19 +310,16 @@ interface PhysicsData {
}
```
### 4. 检查适配器状态
### 4. 验证适配器工作状态
```typescript
const manager = PlatformManager.getInstance();
// 检查是否已注册适配器
if (manager.hasAdapter()) {
console.log('适配器信息:', manager.getAdapterInfo());
// 检查功能支持
console.log('Worker支持:', manager.supportsFeature('worker'));
console.log('SharedArrayBuffer支持:', manager.supportsFeature('shared-array-buffer'));
}
// 验证适配器是否正常工作
const adapter = new BrowserAdapter();
console.log('适配器名称:', adapter.name);
console.log('浏览器版本:', adapter.version);
console.log('Worker支持:', adapter.isWorkerSupported());
console.log('SharedArrayBuffer支持:', adapter.isSharedArrayBufferSupported());
console.log('CPU核心数:', adapter.getHardwareConcurrency());
```
## 重要注意事项
@@ -471,5 +367,4 @@ console.log('Worker支持:', adapter.isWorkerSupported());
console.log('SharedArrayBuffer支持:', adapter.isSharedArrayBufferSupported());
console.log('硬件并发数:', adapter.getHardwareConcurrency());
console.log('平台配置:', adapter.getPlatformConfig());
console.log('设备信息:', adapter.getDeviceInfo());
```