Files
XCEngine/docs/api/resources/dependencygraph/dependencygraph.md
ssdfasd 94beec946b Fix documentation links and add missing destructor docs
- Add missing ~ProxyAllocator destructor entry to proxy-allocator.md
- Fix relative link paths in resources documentation
2026-03-19 01:14:36 +08:00

116 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ResourceDependencyGraph
**命名空间**: `XCEngine::Resources`
**类型**: `class`
**描述**: 资源依赖图管理器,负责跟踪资源之间的依赖关系、引用计数和拓扑排序。
## 概述
`ResourceDependencyGraph` 维护了所有资源之间的依赖关系图。它支持添加/移除依赖节点、查询依赖关系、循环依赖检测、拓扑排序(用于正确的加载/卸载顺序)等功能。
## DependencyNode 结构体
| 成员 | 类型 | 描述 |
|------|------|------|
| `guid` | `ResourceGUID` | 资源全局唯一标识符 |
| `type` | `ResourceType` | 资源类型 |
| `dependencies` | `Containers::Array<ResourceGUID>` | 此资源依赖的其他资源 |
| `dependents` | `Containers::Array<ResourceGUID>` | 依赖此资源的其他资源 |
| `refCount` | `Core::uint32` | 引用计数 |
## 公共方法
### 节点管理
| 方法 | 描述 |
|------|------|
| `void AddNode(ResourceGUID guid, ResourceType type)` | 添加依赖节点 |
| `void RemoveNode(ResourceGUID guid)` | 移除依赖节点 |
| `bool HasNode(ResourceGUID guid) const` | 检查节点是否存在 |
### 依赖关系
| 方法 | 描述 |
|------|------|
| `void AddDependency(ResourceGUID owner, ResourceGUID dependency)` | 添加依赖关系A 依赖 B |
| `void RemoveDependency(ResourceGUID owner, ResourceGUID dependency)` | 移除依赖关系 |
| `Containers::Array<ResourceGUID> GetDependencies(ResourceGUID guid) const` | 获取指定资源的直接依赖列表 |
| `Containers::Array<ResourceGUID> GetDependents(ResourceGUID guid) const` | 获取依赖指定资源的所有资源列表 |
| `Containers::Array<ResourceGUID> GetAllDependencies(ResourceGUID guid) const` | 获取所有递归依赖(包括传递依赖) |
### 引用计数
| 方法 | 描述 |
|------|------|
| `void IncrementRefCount(ResourceGUID guid)` | 增加引用计数 |
| `void DecrementRefCount(ResourceGUID guid)` | 减少引用计数 |
| `Core::uint32 GetRefCount(ResourceGUID guid) const` | 获取引用计数 |
### 循环检测
| 方法 | 描述 |
|------|------|
| `bool HasCircularDependency(ResourceGUID guid, Containers::Array<ResourceGUID>& outCycle) const` | 检测是否存在循环依赖 |
### 排序与卸载
| 方法 | 描述 |
|------|------|
| `Containers::Array<ResourceGUID> TopologicalSort() const` | 拓扑排序(当前返回空数组 - stub |
| `bool Unload(ResourceGUID guid)` | 安全卸载(考虑依赖关系) |
## 实现说明
**注意**: `TopologicalSort()` 当前为 stub返回空数组。
### 清理
| 方法 | 描述 |
|------|------|
| `void Clear()` | 清空所有节点和依赖关系 |
## 使用示例
```cpp
ResourceDependencyGraph graph;
// 添加节点
graph.AddNode(textureGuid, ResourceType::Texture);
graph.AddNode(materialGuid, ResourceType::Material);
graph.AddNode(shaderGuid, ResourceType::Shader);
// 设置依赖关系Material 依赖 Texture 和 Shader
graph.AddDependency(materialGuid, textureGuid);
graph.AddDependency(materialGuid, shaderGuid);
// 查询依赖
auto deps = graph.GetDependencies(materialGuid);
// deps 包含 textureGuid 和 shaderGuid
// 查询被依赖者
auto dependents = graph.GetDependents(textureGuid);
// dependents 包含 materialGuid
// 拓扑排序(正确的加载顺序)
auto loadOrder = graph.TopologicalSort();
// loadOrder 保证依赖在目标之前加载
// 循环依赖检测
Containers::Array<ResourceGUID> cycle;
if (graph.HasCircularDependency(guid, cycle)) {
printf("Circular dependency detected!");
}
// 引用计数
graph.IncrementRefCount(guid);
graph.DecrementRefCount(guid);
```
## 相关文档
- [ResourceManager](../resourcemanager/resourcemanager.md) - 资源管理器
- [ResourceHandle](../resourcehandle/resourcehandle.md) - 资源句柄
- [Resources 总览](../resources.md) - 返回模块总览