docs: update resources API docs
This commit is contained in:
94
docs/api/resources/resource-dependency-graph/index.md
Normal file
94
docs/api/resources/resource-dependency-graph/index.md
Normal file
@@ -0,0 +1,94 @@
|
||||
# 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](./resource-types.md)
|
||||
- [ResourceManager](../resource-manager/resource-manager.md)
|
||||
- [ResourcePool](./resource-pool.md)
|
||||
Reference in New Issue
Block a user