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

This commit is contained in:
yhh
2025-12-03 16:19:03 +08:00
parent e2cca5e490
commit 25e70a1d7b
15 changed files with 1068 additions and 614 deletions

View File

@@ -7,40 +7,64 @@ import {
AssetType,
AssetGUID,
IAssetLoadOptions,
IAssetMetadata,
IAssetLoadResult
IAssetMetadata
} from '../types/AssetTypes';
import type { IAssetContent, AssetContentType } from './IAssetReader';
/**
* Base asset loader interface
* 基础资产加载器接口
* Parse context provided to loaders.
* 提供给加载器的解析上下文。
*/
export interface IAssetParseContext {
/** Asset metadata. | 资产元数据。 */
metadata: IAssetMetadata;
/** Load options. | 加载选项。 */
options?: IAssetLoadOptions;
/**
* Load a dependency asset by relative path.
* 通过相对路径加载依赖资产。
*/
loadDependency<D = unknown>(relativePath: string): Promise<D>;
}
/**
* Asset loader interface.
* 资产加载器接口。
*
* Loaders only parse content, file reading is handled by AssetManager.
* 加载器只负责解析内容,文件读取由 AssetManager 处理。
*/
export interface IAssetLoader<T = unknown> {
/** 支持的资产类型 / Supported asset type */
/** Supported asset type. | 支持的资产类型。 */
readonly supportedType: AssetType;
/** 支持的文件扩展名 / Supported file extensions */
/** Supported file extensions. | 支持的文件扩展名。 */
readonly supportedExtensions: string[];
/**
* Load an asset from the given path
* 从指定路径加载资产
* Required content type for this loader.
* 此加载器需要的内容类型。
*
* - 'text': For JSON, shader, material files
* - 'binary': For binary formats
* - 'image': For textures
* - 'audio': For audio files
*/
load(
path: string,
metadata: IAssetMetadata,
options?: IAssetLoadOptions
): Promise<IAssetLoadResult<T>>;
readonly contentType: AssetContentType;
/**
* Validate if the loader can handle this asset
* 验证加载器是否可以处理此资产
* Parse asset from content.
* 从内容解析资产
*
* @param content - File content. | 文件内容。
* @param context - Parse context. | 解析上下文。
* @returns Parsed asset. | 解析后的资产。
*/
canLoad(path: string, metadata: IAssetMetadata): boolean;
parse(content: IAssetContent, context: IAssetParseContext): Promise<T>;
/**
* Dispose loaded asset and free resources
* 释放已加载的资产并释放资源
* Dispose loaded asset and free resources.
* 释放已加载的资产
*/
dispose(asset: T): void;
}

View File

@@ -69,6 +69,12 @@ export interface IAssetManager {
*/
getAssetByHandle<T = unknown>(handle: AssetHandle): T | null;
/**
* Get loaded asset by path (synchronous)
* 通过路径获取已加载的资产(同步)
*/
getAssetByPath<T = unknown>(path: string): T | null;
/**
* Check if asset is loaded
* 检查资产是否已加载

View File

@@ -0,0 +1,90 @@
/**
* Asset Reader Interface
* 资产读取器接口
*
* Provides unified file reading abstraction across different platforms.
* 提供跨平台的统一文件读取抽象。
*/
/**
* Asset content types.
* 资产内容类型。
*/
export type AssetContentType = 'text' | 'binary' | 'image' | 'audio';
/**
* Asset content result.
* 资产内容结果。
*/
export interface IAssetContent {
/** Content type. | 内容类型。 */
type: AssetContentType;
/** Text content (for text/json files). | 文本内容。 */
text?: string;
/** Binary content. | 二进制内容。 */
binary?: ArrayBuffer;
/** Image element (for textures). | 图片元素。 */
image?: HTMLImageElement;
/** Audio buffer (for audio files). | 音频缓冲区。 */
audioBuffer?: AudioBuffer;
}
/**
* Asset reader interface.
* 资产读取器接口。
*
* Abstracts platform-specific file reading operations.
* 抽象平台特定的文件读取操作。
*/
export interface IAssetReader {
/**
* Read file as text.
* 读取文件为文本。
*
* @param absolutePath - Absolute file path. | 绝对文件路径。
* @returns Text content. | 文本内容。
*/
readText(absolutePath: string): Promise<string>;
/**
* Read file as binary.
* 读取文件为二进制。
*
* @param absolutePath - Absolute file path. | 绝对文件路径。
* @returns Binary content. | 二进制内容。
*/
readBinary(absolutePath: string): Promise<ArrayBuffer>;
/**
* Load image from file.
* 从文件加载图片。
*
* @param absolutePath - Absolute file path. | 绝对文件路径。
* @returns Image element. | 图片元素。
*/
loadImage(absolutePath: string): Promise<HTMLImageElement>;
/**
* Load audio from file.
* 从文件加载音频。
*
* @param absolutePath - Absolute file path. | 绝对文件路径。
* @returns Audio buffer. | 音频缓冲区。
*/
loadAudio(absolutePath: string): Promise<AudioBuffer>;
/**
* Check if file exists.
* 检查文件是否存在。
*
* @param absolutePath - Absolute file path. | 绝对文件路径。
* @returns True if exists. | 是否存在。
*/
exists(absolutePath: string): Promise<boolean>;
}
/**
* Service identifier for IAssetReader.
* IAssetReader 的服务标识符。
*/
export const IAssetReaderService = Symbol.for('IAssetReaderService');