Files
esengine/packages/platform-common/src/IPlatformSubsystems.ts

633 lines
14 KiB
TypeScript
Raw Normal View History

/**
*
*
*/
// ============================================================================
// Canvas/渲染子系统
// ============================================================================
/**
* Canvas
*/
/**
* Canvas Web
*/
export interface CanvasContextAttributes {
alpha?: boolean | number;
antialias?: boolean;
depth?: boolean;
stencil?: boolean;
premultipliedAlpha?: boolean;
preserveDrawingBuffer?: boolean;
failIfMajorPerformanceCaveat?: boolean;
powerPreference?: 'default' | 'high-performance' | 'low-power';
antialiasSamples?: number;
}
export interface IPlatformCanvas {
width: number;
height: number;
getContext(contextType: '2d' | 'webgl' | 'webgl2', contextAttributes?: CanvasContextAttributes): RenderingContext | null;
toDataURL(): string;
toTempFilePath?(options: TempFilePathOptions): void;
}
/**
* Image
*/
export interface IPlatformImage {
src: string;
width: number;
height: number;
onload: (() => void) | null;
onerror: ((error: any) => void) | null;
}
/**
*
*/
export interface TempFilePathOptions {
x?: number;
y?: number;
width?: number;
height?: number;
destWidth?: number;
destHeight?: number;
fileType?: 'png' | 'jpg';
quality?: number;
success?: (res: { tempFilePath: string }) => void;
fail?: (error: any) => void;
complete?: () => void;
}
/**
* Canvas
*/
export interface IPlatformCanvasSubsystem {
/**
* Canvas Canvas
*/
createCanvas(width?: number, height?: number): IPlatformCanvas;
/**
*
*/
createImage(): IPlatformImage;
/**
* ImageData
*/
createImageData?(width: number, height: number): ImageData;
/**
*
*/
getScreenWidth(): number;
/**
*
*/
getScreenHeight(): number;
/**
*
*/
getDevicePixelRatio(): number;
}
// ============================================================================
// 音频子系统
// ============================================================================
/**
*
*/
export interface IPlatformAudioContext {
src: string;
autoplay: boolean;
loop: boolean;
volume: number;
duration: number;
currentTime: number;
paused: boolean;
buffered: number;
play(): void;
pause(): void;
stop(): void;
seek(position: number): void;
destroy(): void;
onPlay(callback: () => void): void;
onPause(callback: () => void): void;
onStop(callback: () => void): void;
onEnded(callback: () => void): void;
onError(callback: (error: { errCode: number; errMsg: string }) => void): void;
onTimeUpdate(callback: () => void): void;
onCanplay(callback: () => void): void;
onSeeking(callback: () => void): void;
onSeeked(callback: () => void): void;
offPlay(callback: () => void): void;
offPause(callback: () => void): void;
offStop(callback: () => void): void;
offEnded(callback: () => void): void;
offError(callback: (error: { errCode: number; errMsg: string }) => void): void;
offTimeUpdate(callback: () => void): void;
}
/**
*
*/
export interface IPlatformAudioSubsystem {
/**
*
*/
createAudioContext(options?: { useWebAudioImplement?: boolean }): IPlatformAudioContext;
/**
*
*/
getSupportedFormats(): string[];
/**
*
*/
setInnerAudioOption?(options: {
mixWithOther?: boolean;
obeyMuteSwitch?: boolean;
speakerOn?: boolean;
}): Promise<void>;
}
// ============================================================================
// 存储子系统
// ============================================================================
/**
*
*/
export interface StorageInfo {
keys: string[];
currentSize: number;
limitSize: number;
}
/**
*
*/
export interface IPlatformStorageSubsystem {
/**
*
*/
getStorageSync<T = any>(key: string): T | undefined;
/**
*
*/
setStorageSync<T = any>(key: string, value: T): void;
/**
*
*/
removeStorageSync(key: string): void;
/**
*
*/
clearStorageSync(): void;
/**
*
*/
getStorageInfoSync(): StorageInfo;
/**
*
*/
getStorage<T = any>(key: string): Promise<T | undefined>;
/**
*
*/
setStorage<T = any>(key: string, value: T): Promise<void>;
/**
*
*/
removeStorage(key: string): Promise<void>;
/**
*
*/
clearStorage(): Promise<void>;
}
// ============================================================================
// 网络子系统
// ============================================================================
/**
*
*/
export interface RequestConfig {
url: string;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
data?: any;
header?: Record<string, string>;
timeout?: number;
dataType?: 'json' | 'text' | 'arraybuffer';
responseType?: 'text' | 'arraybuffer';
}
/**
*
*/
export interface RequestResponse<T = any> {
data: T;
statusCode: number;
header: Record<string, string>;
}
/**
*
*/
export interface IDownloadTask {
abort(): void;
onProgressUpdate(callback: (res: {
progress: number;
totalBytesWritten: number;
totalBytesExpectedToWrite: number;
}) => void): void;
offProgressUpdate(callback: Function): void;
}
/**
*
*/
export interface IUploadTask {
abort(): void;
onProgressUpdate(callback: (res: {
progress: number;
totalBytesSent: number;
totalBytesExpectedToSend: number;
}) => void): void;
offProgressUpdate(callback: Function): void;
}
/**
* WebSocket
*/
export interface IPlatformWebSocket {
send(data: string | ArrayBuffer): void;
close(code?: number, reason?: string): void;
onOpen(callback: (res: { header: Record<string, string> }) => void): void;
onClose(callback: (res: { code: number; reason: string }) => void): void;
onError(callback: (error: any) => void): void;
onMessage(callback: (res: { data: string | ArrayBuffer }) => void): void;
}
/**
*
*/
export interface IPlatformNetworkSubsystem {
/**
*
*/
request<T = any>(config: RequestConfig): Promise<RequestResponse<T>>;
/**
*
*/
downloadFile(options: {
url: string;
filePath?: string;
header?: Record<string, string>;
timeout?: number;
}): Promise<{ tempFilePath: string; filePath?: string; statusCode: number }> & IDownloadTask;
/**
*
*/
uploadFile(options: {
url: string;
filePath: string;
name: string;
header?: Record<string, string>;
formData?: Record<string, any>;
timeout?: number;
}): Promise<{ data: string; statusCode: number }> & IUploadTask;
/**
* WebSocket
*/
connectSocket(options: {
url: string;
header?: Record<string, string>;
protocols?: string[];
timeout?: number;
}): IPlatformWebSocket;
/**
*
*/
getNetworkType(): Promise<'wifi' | '2g' | '3g' | '4g' | '5g' | 'unknown' | 'none'>;
/**
*
*/
onNetworkStatusChange(callback: (res: {
isConnected: boolean;
networkType: string;
}) => void): void;
/**
*
*/
offNetworkStatusChange(callback: Function): void;
}
// ============================================================================
// 输入子系统
// ============================================================================
/**
*
*/
export interface TouchInfo {
identifier: number;
x: number;
y: number;
force?: number;
}
/**
*
*/
export interface TouchEvent {
touches: TouchInfo[];
changedTouches: TouchInfo[];
timeStamp: number;
}
/**
*
*/
export type TouchHandler = (event: TouchEvent) => void;
/**
*
*/
export interface IPlatformInputSubsystem {
/**
*
*/
onTouchStart(handler: TouchHandler): void;
/**
*
*/
onTouchMove(handler: TouchHandler): void;
/**
*
*/
onTouchEnd(handler: TouchHandler): void;
/**
*
*/
onTouchCancel(handler: TouchHandler): void;
/**
*
*/
offTouchStart(handler: TouchHandler): void;
/**
*
*/
offTouchMove(handler: TouchHandler): void;
/**
*
*/
offTouchEnd(handler: TouchHandler): void;
/**
*
*/
offTouchCancel(handler: TouchHandler): void;
/**
*
*/
supportsPressure?(): boolean;
}
// ============================================================================
// 文件系统子系统
// ============================================================================
/**
*
*/
export interface FileInfo {
size: number;
createTime: number;
modifyTime?: number;
isDirectory: boolean;
isFile: boolean;
}
/**
*
*/
export interface IPlatformFileSubsystem {
/**
*
*/
readFile(options: {
filePath: string;
encoding?: 'ascii' | 'base64' | 'binary' | 'hex' | 'ucs2' | 'utf-8' | 'utf8';
position?: number;
length?: number;
}): Promise<string | ArrayBuffer>;
/**
*
*/
readFileSync(
filePath: string,
encoding?: 'ascii' | 'base64' | 'binary' | 'hex' | 'ucs2' | 'utf-8' | 'utf8',
position?: number,
length?: number
): string | ArrayBuffer;
/**
*
*/
writeFile(options: {
filePath: string;
data: string | ArrayBuffer;
encoding?: 'ascii' | 'base64' | 'binary' | 'hex' | 'ucs2' | 'utf-8' | 'utf8';
}): Promise<void>;
/**
*
*/
writeFileSync(
filePath: string,
data: string | ArrayBuffer,
encoding?: 'ascii' | 'base64' | 'binary' | 'hex' | 'ucs2' | 'utf-8' | 'utf8'
): void;
/**
*
*/
appendFile(options: {
filePath: string;
data: string | ArrayBuffer;
encoding?: 'ascii' | 'base64' | 'binary' | 'hex' | 'ucs2' | 'utf-8' | 'utf8';
}): Promise<void>;
/**
*
*/
unlink(filePath: string): Promise<void>;
/**
*
*/
mkdir(options: {
dirPath: string;
recursive?: boolean;
}): Promise<void>;
/**
*
*/
rmdir(options: {
dirPath: string;
recursive?: boolean;
}): Promise<void>;
/**
*
*/
readdir(dirPath: string): Promise<string[]>;
/**
*
*/
stat(path: string): Promise<FileInfo>;
/**
* /
*/
access(path: string): Promise<void>;
/**
*
*/
rename(oldPath: string, newPath: string): Promise<void>;
/**
*
*/
copyFile(srcPath: string, destPath: string): Promise<void>;
/**
*
*/
getUserDataPath(): string;
/**
*
*/
unzip?(options: {
zipFilePath: string;
targetPath: string;
}): Promise<void>;
}
// ============================================================================
// WASM 子系统
// ============================================================================
/**
* WASM
*/
export type WASMExports = Record<string, WebAssembly.ExportValue>;
/**
* WASM Web
*/
export type WASMImportValue = WebAssembly.ExportValue | number;
/**
* WASM
*/
export type WASMImports = Record<string, Record<string, WASMImportValue>>;
/**
* WASM
*/
export interface IWASMInstance {
exports: WASMExports;
}
/**
* WASM
*/
export interface IPlatformWASMSubsystem {
/**
* WASM
* @param path WASM
* @param imports
*/
instantiate(path: string, imports?: WASMImports): Promise<IWASMInstance>;
/**
* WASM
*/
isSupported(): boolean;
}
// ============================================================================
// 系统信息
// ============================================================================
/**
*
*/
export interface SystemInfo {
/** 设备品牌 */
brand: string;
/** 设备型号 */
model: string;
/** 设备像素比 */
pixelRatio: number;
/** 屏幕宽度 */
screenWidth: number;
/** 屏幕高度 */
screenHeight: number;
/** 可使用窗口宽度 */
windowWidth: number;
/** 可使用窗口高度 */
windowHeight: number;
/** 状态栏高度 */
statusBarHeight: number;
/** 操作系统及版本 */
system: string;
/** 客户端平台 */
platform: 'ios' | 'android' | 'windows' | 'mac' | 'devtools';
/** 客户端基础库版本 */
SDKVersion: string;
/** 设备性能等级 */
benchmarkLevel: number;
/** 设备内存大小 (MB) */
memorySize?: number;
}