docs: update resources API docs
This commit is contained in:
31
docs/api/resources/resource-loader/canload.md
Normal file
31
docs/api/resources/resource-loader/canload.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# IResourceLoader::CanLoad
|
||||
|
||||
```cpp
|
||||
virtual bool CanLoad(const Containers::String& path) const = 0
|
||||
```
|
||||
|
||||
纯虚方法,检查此加载器是否能加载指定路径的资源。通过比对路径扩展名与支持列表判断。
|
||||
|
||||
**参数:**
|
||||
- `path` - 资源路径
|
||||
|
||||
**返回:** 如果扩展名在支持列表中则返回 true
|
||||
|
||||
**复杂度:** O(k),k 为扩展名数量
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
bool TextureLoader::CanLoad(const Containers::String& path) const {
|
||||
Containers::String ext = GetExtension(path);
|
||||
auto supported = GetSupportedExtensions();
|
||||
for (const auto& s : supported) {
|
||||
if (ext == s) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [IResourceLoader 总览](resource-loader.md) - 返回类总览
|
||||
25
docs/api/resources/resource-loader/getdefaultsettings.md
Normal file
25
docs/api/resources/resource-loader/getdefaultsettings.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# IResourceLoader::GetDefaultSettings
|
||||
|
||||
```cpp
|
||||
virtual ImportSettings* GetDefaultSettings() const = 0
|
||||
```
|
||||
|
||||
纯虚方法,返回此加载器使用的默认导入设置。每个加载器可提供针对其资源类型的默认导入参数。
|
||||
|
||||
**返回:** `ImportSettings*` 默认导入设置指针
|
||||
|
||||
**实现注意:** 当前各加载器实现返回 `nullptr`,未返回实际的默认设置对象。
|
||||
|
||||
**线程安全:** ✅
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
ImportSettings* TextureLoader::GetDefaultSettings() const {
|
||||
return new TextureImportSettings();
|
||||
}
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [IResourceLoader 总览](resource-loader.md) - 返回类总览
|
||||
27
docs/api/resources/resource-loader/getresourcetype.md
Normal file
27
docs/api/resources/resource-loader/getresourcetype.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# IResourceLoader::GetResourceType
|
||||
|
||||
```cpp
|
||||
virtual ResourceType GetResourceType() const = 0
|
||||
```
|
||||
|
||||
纯虚方法,返回此加载器所处理的资源类型。每个加载器必须声明自己处理的资源类型(如 Texture、Mesh、Audio 等)。
|
||||
|
||||
**返回:** `ResourceType` 枚举值,表示支持的资源类型
|
||||
|
||||
**线程安全:** ✅
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
class TextureLoader : public IResourceLoader {
|
||||
public:
|
||||
ResourceType GetResourceType() const override {
|
||||
return ResourceType::Texture;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [IResourceLoader 总览](resource-loader.md) - 返回类总览
|
||||
23
docs/api/resources/resource-loader/getsupportedextensions.md
Normal file
23
docs/api/resources/resource-loader/getsupportedextensions.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# IResourceLoader::GetSupportedExtensions
|
||||
|
||||
```cpp
|
||||
virtual Containers::Array<Containers::String> GetSupportedExtensions() const = 0
|
||||
```
|
||||
|
||||
纯虚方法,返回此加载器支持的文件扩展名列表。例如纹理加载器可能返回 `{".png", ".jpg", ".jpeg", ".bmp", ".tga"}`。
|
||||
|
||||
**返回:** `Containers::Array<Containers::String>` 支持的扩展名列表(包含点号)
|
||||
|
||||
**线程安全:** ✅
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
Containers::Array<Containers::String> TextureLoader::GetSupportedExtensions() const {
|
||||
return {".png", ".jpg", ".jpeg", ".bmp", ".tga"};
|
||||
}
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [IResourceLoader 总览](resource-loader.md) - 返回类总览
|
||||
42
docs/api/resources/resource-loader/load.md
Normal file
42
docs/api/resources/resource-loader/load.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# IResourceLoader::Load
|
||||
|
||||
```cpp
|
||||
virtual LoadResult Load(const Containers::String& path, const ImportSettings* settings = nullptr) = 0
|
||||
```
|
||||
|
||||
同步加载资源。纯虚方法,由具体加载器实现。从指定路径读取文件数据,解析为对应类型的资源对象。
|
||||
|
||||
**参数:**
|
||||
- `path` - 资源文件路径
|
||||
- `settings` - 可选的导入设置,用于自定义加载行为
|
||||
|
||||
**返回:** `LoadResult`,包含加载结果(资源指针、是否成功、错误信息等)
|
||||
|
||||
**复杂度:** O(n),取决于文件大小
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
LoadResult TextureLoader::Load(const Containers::String& path,
|
||||
const ImportSettings* settings) {
|
||||
LoadResult result;
|
||||
auto data = ReadFileData(path);
|
||||
if (data.Empty()) {
|
||||
result.errorMessage = "Failed to read file: " + path;
|
||||
return result;
|
||||
}
|
||||
Texture* tex = new Texture();
|
||||
if (!tex->LoadFromData(data.Data(), data.Size(), settings)) {
|
||||
delete tex;
|
||||
result.errorMessage = "Failed to parse texture data";
|
||||
return result;
|
||||
}
|
||||
result.resource = tex;
|
||||
result.success = true;
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [IResourceLoader 总览](resource-loader.md) - 返回类总览
|
||||
45
docs/api/resources/resource-loader/loadasync.md
Normal file
45
docs/api/resources/resource-loader/loadasync.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# IResourceLoader::LoadAsync
|
||||
|
||||
```cpp
|
||||
virtual void LoadAsync(const Containers::String& path,
|
||||
const ImportSettings* settings,
|
||||
std::function<void(LoadResult)> callback)
|
||||
```
|
||||
|
||||
异步加载资源。虚方法,提供默认实现(直接在调用线程执行 Load 然后调用回调)。子类可重写以提供真正的异步实现。
|
||||
|
||||
**参数:**
|
||||
- `path` - 资源文件路径
|
||||
- `settings` - 导入设置
|
||||
- `callback` - 加载完成后的回调函数,接收 `LoadResult` 参数
|
||||
|
||||
**线程安全:** ❌(默认实现同步执行,子类重写时需确保线程安全)
|
||||
|
||||
**示例:**
|
||||
|
||||
```cpp
|
||||
// 使用默认实现的异步加载
|
||||
ResourceManager::Get().LoadAsync<Texture>("textures/player.png",
|
||||
nullptr, [](LoadResult result) {
|
||||
if (result.success) {
|
||||
Texture* tex = static_cast<Texture*>(result.resource);
|
||||
// 使用纹理
|
||||
}
|
||||
});
|
||||
|
||||
// 子类重写提供真正的异步实现
|
||||
class AsyncTextureLoader : public IResourceLoader {
|
||||
void LoadAsync(const Containers::String& path,
|
||||
const ImportSettings* settings,
|
||||
std::function<void(LoadResult)> callback) override {
|
||||
std::thread([this, path, settings, callback]() {
|
||||
LoadResult result = Load(path, settings);
|
||||
callback(result);
|
||||
}).detach();
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [IResourceLoader 总览](resource-loader.md) - 返回类总览
|
||||
119
docs/api/resources/resource-loader/resource-loader.md
Normal file
119
docs/api/resources/resource-loader/resource-loader.md
Normal file
@@ -0,0 +1,119 @@
|
||||
# IResourceLoader
|
||||
|
||||
**命名空间**: `XCEngine::Resources`
|
||||
|
||||
**类型**: `class` (抽象基类)
|
||||
|
||||
**描述**: 资源加载器抽象接口,定义了资源加载的标准协议。每个资源类型需要提供对应的加载器实现。
|
||||
|
||||
## 概述
|
||||
|
||||
`IResourceLoader` 是资源加载系统的核心抽象接口。它定义了同步和异步加载资源的方法,以及资源类型的查询。`ResourceManager` 通过注册加载器来支持不同类型资源的加载。
|
||||
|
||||
## LoadResult 结构体
|
||||
|
||||
加载操作的返回值结构体。
|
||||
|
||||
```cpp
|
||||
struct LoadResult {
|
||||
IResource* resource = nullptr;
|
||||
bool success = false;
|
||||
Containers::String errorMessage;
|
||||
LoadResult() = default;
|
||||
explicit LoadResult(IResource* res) : resource(res), success(res != nullptr) {}
|
||||
explicit LoadResult(const Containers::String& error) : success(false), errorMessage(error) {}
|
||||
explicit LoadResult(bool inSuccess, const Containers::String& error = "")
|
||||
: success(inSuccess), errorMessage(error) {}
|
||||
operator bool() const { return success && resource != nullptr; }
|
||||
};
|
||||
```
|
||||
|
||||
## 公共方法
|
||||
|
||||
### 资源信息
|
||||
|
||||
| 方法 | 描述 |
|
||||
|------|------|
|
||||
| [`GetResourceType`](getresourcetype.md) | 获取此加载器支持的资源类型 |
|
||||
| [`GetSupportedExtensions`](getsupportedextensions.md) | 获取支持的文件扩展名列表 |
|
||||
| [`CanLoad`](canload.md) | 检查此加载器是否能加载指定路径 |
|
||||
| [`GetDefaultSettings`](getdefaultsettings.md) | 获取默认导入设置 |
|
||||
|
||||
### 同步加载
|
||||
|
||||
| 方法 | 描述 |
|
||||
|------|------|
|
||||
| [`Load`](load.md) | 同步加载资源 |
|
||||
|
||||
### 异步加载
|
||||
|
||||
| 方法 | 描述 |
|
||||
|------|------|
|
||||
| [`LoadAsync`](loadasync.md) | 异步加载资源(带默认实现,子类可重写) |
|
||||
|
||||
### 辅助方法(受保护)
|
||||
|
||||
| 方法 | 描述 |
|
||||
|------|------|
|
||||
| `static Containers::Array<Core::uint8> ReadFileData(const Containers::String& path)` | 读取文件数据 |
|
||||
| `static Containers::String GetExtension(const Containers::String& path)` | 获取文件扩展名 |
|
||||
|
||||
## 实现说明
|
||||
|
||||
**注意**: 各资源加载器的 `GetDefaultSettings()` 当前返回 `nullptr`,未返回实际的默认设置对象。
|
||||
|
||||
## 宏
|
||||
|
||||
### REGISTER_RESOURCE_LOADER
|
||||
|
||||
自动注册加载器到 ResourceManager 的宏。
|
||||
|
||||
```cpp
|
||||
REGISTER_RESOURCE_LOADER(TextureLoader)
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
```cpp
|
||||
class TextureLoader : public IResourceLoader {
|
||||
public:
|
||||
ResourceType GetResourceType() const override {
|
||||
return ResourceType::Texture;
|
||||
}
|
||||
|
||||
Containers::Array<Containers::String> GetSupportedExtensions() const override {
|
||||
return {".png", ".jpg", ".jpeg", ".bmp", ".tga"};
|
||||
}
|
||||
|
||||
bool CanLoad(const Containers::String& path) const override {
|
||||
Containers::String ext = GetExtension(path);
|
||||
return ext == ".png" || ext == ".jpg" || ext == ".bmp";
|
||||
}
|
||||
|
||||
ImportSettings* GetDefaultSettings() const override {
|
||||
return new TextureImportSettings();
|
||||
}
|
||||
|
||||
LoadResult Load(const Containers::String& path,
|
||||
const ImportSettings* settings = nullptr) override {
|
||||
LoadResult result;
|
||||
auto data = ReadFileData(path);
|
||||
if (data.Empty()) {
|
||||
return LoadResult("Failed to read file: " + path);
|
||||
}
|
||||
result.resource = new Texture();
|
||||
result.success = true;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
// 注册加载器
|
||||
ResourceManager::Get().RegisterLoader(new TextureLoader());
|
||||
```
|
||||
|
||||
## 相关文档
|
||||
|
||||
- [ResourceManager](../resourcemanager/resourcemanager.md) - 资源管理器
|
||||
- [AsyncLoader](../asyncloader/asyncloader.md) - 异步加载器
|
||||
- [ImportSettings](../importsettings/importsettings.md) - 导入设置
|
||||
- [Resources 总览](../resources.md) - 返回模块总览
|
||||
Reference in New Issue
Block a user