- audio: 更新 audio-system 方法文档 - components: 新增 audio-listener/audio-source 组件方法文档,新增 remove-component 方法 - core: 更新 filewriter, types 文档 - math: 更新 box 方法文档 - memory: 更新 proxy-allocator 文档 - resources: 更新 loader 和 texture 文档 - rhi: 更新 opengl 设备、shader、swap-chain 文档 - threading: 更新 mutex 和 task-system 文档
95 lines
3.5 KiB
Markdown
95 lines
3.5 KiB
Markdown
# ResourceDependencyGraph
|
|
|
|
## 命名空间
|
|
|
|
`XCEngine::Resources`
|
|
|
|
## 类型
|
|
|
|
类 (`class`)
|
|
|
|
## 描述
|
|
|
|
资源依赖图管理器,用于跟踪和管理资源之间的依赖关系。该类维护一个双向依赖图,支持添加/移除节点和依赖关系、查询依赖链、循环依赖检测、引用计数管理以及拓扑排序。
|
|
|
|
## 概述
|
|
|
|
`ResourceDependencyGraph` 提供了一套完整的资源依赖关系管理机制。每个资源被表示为一个节点,节点之间通过有向边建立依赖关系。类内部维护两个方向的关系:
|
|
- **依赖项** (dependencies): 当前资源直接依赖的资源
|
|
- **被依赖项** (dependents): 直接依赖当前资源的资源
|
|
|
|
该图结构支持:
|
|
- 双向依赖查询
|
|
- 递归获取完整依赖链
|
|
- 循环依赖检测
|
|
- 引用计数追踪
|
|
- 拓扑排序以确定资源加载/卸载顺序
|
|
|
|
## 公共方法表格
|
|
|
|
| 方法 | 描述 |
|
|
|------|------|
|
|
| `AddNode(ResourceGUID guid, ResourceType type)` | 添加资源节点到依赖图 |
|
|
| `RemoveNode(ResourceGUID guid)` | 从依赖图中移除资源节点 |
|
|
| `AddDependency(ResourceGUID owner, ResourceGUID dependency)` | 添加依赖关系 |
|
|
| `RemoveDependency(ResourceGUID owner, ResourceGUID dependency)` | 移除依赖关系 |
|
|
| `GetDependencies(ResourceGUID guid)` | 获取资源的直接依赖项 |
|
|
| `GetDependents(ResourceGUID guid)` | 获取资源的直接被依赖项 |
|
|
| `GetAllDependencies(ResourceGUID guid)` | 获取资源的完整依赖链(递归) |
|
|
| `IncrementRefCount(ResourceGUID guid)` | 增加资源引用计数 |
|
|
| `DecrementRefCount(ResourceGUID guid)` | 减少资源引用计数 |
|
|
| `GetRefCount(ResourceGUID guid)` | 获取资源引用计数 |
|
|
| `HasCircularDependency(ResourceGUID guid, Containers::Array<ResourceGUID>& outCycle)` | 检测是否存在循环依赖 |
|
|
| `TopologicalSort()` | 对所有资源节点进行拓扑排序 |
|
|
| `Unload(ResourceGUID guid)` | 检查资源是否可以卸载 |
|
|
| `Clear()` | 清空整个依赖图 |
|
|
| `HasNode(ResourceGUID guid)` | 检查节点是否存在 |
|
|
|
|
## 使用示例
|
|
|
|
```cpp
|
|
#include "Resources/ResourceDependencyGraph.h"
|
|
|
|
using namespace XCEngine;
|
|
using namespace Resources;
|
|
|
|
void Example() {
|
|
ResourceDependencyGraph graph;
|
|
|
|
// 添加资源节点
|
|
graph.AddNode("texture_albedo"_guid, ResourceType::Texture);
|
|
graph.AddNode("material_standard"_guid, ResourceType::Material);
|
|
graph.AddNode("mesh_cube"_guid, ResourceType::Mesh);
|
|
|
|
// 建立依赖关系
|
|
graph.AddDependency("material_standard"_guid, "texture_albedo"_guid);
|
|
graph.AddDependency("mesh_cube"_guid, "material_standard"_guid);
|
|
|
|
// 查询依赖
|
|
auto deps = graph.GetDependencies("material_standard"_guid); // 返回 ["texture_albedo"]
|
|
auto allDeps = graph.GetAllDependencies("mesh_cube"_guid); // 返回 ["material_standard", "texture_albedo"]
|
|
|
|
// 引用计数管理
|
|
graph.IncrementRefCount("texture_albedo"_guid);
|
|
graph.IncrementRefCount("texture_albedo"_guid);
|
|
uint32 count = graph.GetRefCount("texture_albedo"_guid); // 返回 2
|
|
|
|
// 检测循环依赖
|
|
Containers::Array<ResourceGUID> cycle;
|
|
if (graph.HasCircularDependency("mesh_cube"_guid, cycle)) {
|
|
// 处理循环依赖
|
|
}
|
|
|
|
// 检查是否可以卸载
|
|
if (graph.Unload("texture_albedo"_guid)) {
|
|
// 可以卸载
|
|
}
|
|
}
|
|
```
|
|
|
|
## 相关文档
|
|
|
|
- [ResourceTypes](../resourcetypes/resourcetypes.md) - 资源类型定义
|
|
- [ResourceManager](../resource-manager/resource-manager.md) - 资源管理器
|
|
- [Resources 总览](../resources.md) - 返回模块总览
|