parent
b41e96d870
commit
4e6db88da5
|
|
@ -1,199 +0,0 @@
|
||||||
# Extra2D 资源服务设计计划
|
|
||||||
|
|
||||||
## 1. 概述
|
|
||||||
|
|
||||||
设计一个现代化的资源服务,支持高效的资源管理、存储和访问功能。采用设计模式确保代码的可扩展性、可维护性和性能。
|
|
||||||
|
|
||||||
## 2. 设计目标
|
|
||||||
|
|
||||||
- **高效缓存**: 避免重复加载相同资源
|
|
||||||
- **类型安全**: 使用模板和强类型ID
|
|
||||||
- **异步加载**: 支持后台加载大资源
|
|
||||||
- **内存管理**: 自动释放未使用资源
|
|
||||||
- **扩展性**: 易于添加新资源类型
|
|
||||||
|
|
||||||
## 3. 核心组件设计
|
|
||||||
|
|
||||||
### 3.1 资源类型枚举
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
enum class AssetType {
|
|
||||||
Texture, // 纹理/图片
|
|
||||||
Font, // 字体
|
|
||||||
Shader, // 着色器
|
|
||||||
Audio, // 音频
|
|
||||||
Data, // 通用数据
|
|
||||||
Custom // 自定义类型
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3.2 资源句柄 (Resource Handle)
|
|
||||||
|
|
||||||
使用强类型句柄替代裸指针,提供类型安全和生命周期管理:
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
template<typename T>
|
|
||||||
class AssetHandle {
|
|
||||||
AssetID id_;
|
|
||||||
Weak<T> cacheRef_;
|
|
||||||
public:
|
|
||||||
bool valid() const;
|
|
||||||
Ref<T> get() const;
|
|
||||||
void release();
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3.3 资源基类
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
class Asset {
|
|
||||||
public:
|
|
||||||
virtual ~Asset() = default;
|
|
||||||
virtual AssetType type() const = 0;
|
|
||||||
virtual bool loaded() const = 0;
|
|
||||||
virtual size_t memorySize() const = 0;
|
|
||||||
|
|
||||||
const AssetID& id() const { return id_; }
|
|
||||||
const std::string& path() const { return path_; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
AssetID id_;
|
|
||||||
std::string path_;
|
|
||||||
std::atomic<bool> loaded_{false};
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3.4 具体资源类型
|
|
||||||
|
|
||||||
#### 纹理资源
|
|
||||||
```cpp
|
|
||||||
class TextureAsset : public Asset {
|
|
||||||
public:
|
|
||||||
AssetType type() const override { return AssetType::Texture; }
|
|
||||||
|
|
||||||
int width() const { return width_; }
|
|
||||||
int height() const { return height_; }
|
|
||||||
int channels() const { return channels_; }
|
|
||||||
const u8* data() const { return data_.get(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
int width_ = 0;
|
|
||||||
int height_ = 0;
|
|
||||||
int channels_ = 0;
|
|
||||||
Unique<u8[]> data_;
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 字体资源
|
|
||||||
```cpp
|
|
||||||
class FontAsset : public Asset {
|
|
||||||
public:
|
|
||||||
AssetType type() const override { return AssetType::Font; }
|
|
||||||
|
|
||||||
const stbtt_fontinfo& info() const { return info_; }
|
|
||||||
float scaleForPixelHeight(float pixels) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<u8> data_;
|
|
||||||
stbtt_fontinfo info_;
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 着色器资源
|
|
||||||
```cpp
|
|
||||||
class ShaderAsset : public Asset {
|
|
||||||
public:
|
|
||||||
AssetType type() const override { return AssetType::Shader; }
|
|
||||||
|
|
||||||
const std::string& vertexSource() const { return vertexSrc_; }
|
|
||||||
const std::string& fragmentSource() const { return fragmentSrc_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string vertexSrc_;
|
|
||||||
std::string fragmentSrc_;
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
## 4. 服务接口设计
|
|
||||||
|
|
||||||
### 4.1 资源服务接口
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
class IAssetService : public IService {
|
|
||||||
public:
|
|
||||||
virtual ~IAssetService() = default;
|
|
||||||
|
|
||||||
// 同步加载
|
|
||||||
template<typename T>
|
|
||||||
AssetHandle<T> load(const std::string& path);
|
|
||||||
|
|
||||||
// 异步加载
|
|
||||||
template<typename T>
|
|
||||||
void loadAsync(const std::string& path, AssetLoadCallback<T> callback);
|
|
||||||
|
|
||||||
// 获取已缓存资源
|
|
||||||
template<typename T>
|
|
||||||
AssetHandle<T> get(const std::string& path);
|
|
||||||
|
|
||||||
// 卸载资源
|
|
||||||
virtual void unload(const AssetID& id) = 0;
|
|
||||||
virtual void unloadAll() = 0;
|
|
||||||
|
|
||||||
// 缓存管理
|
|
||||||
virtual void setCacheLimit(size_t maxBytes) = 0;
|
|
||||||
virtual size_t cacheSize() const = 0;
|
|
||||||
virtual void gc() = 0; // 垃圾回收
|
|
||||||
|
|
||||||
// 注册加载器
|
|
||||||
template<typename T>
|
|
||||||
void registerLoader(Unique<AssetLoader<T>> loader);
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
### 4.2 资源加载器接口
|
|
||||||
|
|
||||||
使用**策略模式**支持不同资源类型的加载:
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
template<typename T>
|
|
||||||
class AssetLoader {
|
|
||||||
public:
|
|
||||||
virtual ~AssetLoader() = default;
|
|
||||||
virtual Unique<T> load(const std::string& path) = 0;
|
|
||||||
virtual bool canLoad(const std::string& path) const = 0;
|
|
||||||
virtual AssetType type() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 纹理加载器
|
|
||||||
class TextureLoader : public AssetLoader<TextureAsset> {
|
|
||||||
public:
|
|
||||||
Unique<TextureAsset> load(const std::string& path) override;
|
|
||||||
bool canLoad(const std::string& path) const override;
|
|
||||||
AssetType type() const override { return AssetType::Texture; }
|
|
||||||
};
|
|
||||||
|
|
||||||
// 字体加载器
|
|
||||||
class FontLoader : public AssetLoader<FontAsset> {
|
|
||||||
public:
|
|
||||||
Unique<FontAsset> load(const std::string& path) override;
|
|
||||||
bool canLoad(const std::string& path) const override;
|
|
||||||
AssetType type() const override { return AssetType::Font; }
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
## 5. 实现架构
|
|
||||||
|
|
||||||
### 5.1 类图
|
|
||||||
|
|
||||||
```
|
|
||||||
IService
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
IAssetService (接口)
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
AssetService (实现)
|
|
||||||
│
|
|
||||||
┌──────────────┼──────────────┐
|
|
||||||
│ │ │
|
|
||||||
▼ ▼ ▼
|
|
||||||
Loading…
Reference in New Issue