DependencyGraph.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using IntelligentProjectAnalyzer.Helper;
  6. using static IntelligentProjectAnalyzer.Editor.DependencyBuilderData;
  7. namespace IntelligentProjectAnalyzer.Editor.DependencyViewer
  8. {
  9. /// <summary>
  10. /// Loads, parses, and organizes dependency data from the cache into a queryable graph.
  11. /// This version reads the unified AssetMetadata structure.
  12. /// </summary>
  13. public class DependencyGraph
  14. {
  15. // Maps an asset's GUID to a list of GUIDs it depends.
  16. private readonly Dictionary<string, List<string>> _dependencies = new();
  17. // Maps an asset's GUID to a list of GUIDs that reference it.
  18. private readonly Dictionary<string, List<string>> _references = new();
  19. private readonly HashSet<string> _allKnownGuids = new();
  20. /// <summary>
  21. /// Initializes a new instance of the DependencyGraph class and builds it from the cache.
  22. /// </summary>
  23. public DependencyGraph()
  24. {
  25. BuildGraph();
  26. }
  27. /// <summary>
  28. /// Gets the list of assets that the specified asset depends on.
  29. /// </summary>
  30. public List<string> GetDependencies(string guid)
  31. {
  32. return _dependencies.TryGetValue(guid, out var dependencies) ? dependencies : new List<string>();
  33. }
  34. /// <summary>
  35. /// Gets the list of assets that reference the specified asset.
  36. /// </summary>
  37. public List<string> GetReferences(string guid)
  38. {
  39. return _references.TryGetValue(guid, out var refs) ? refs : new List<string>();
  40. }
  41. /// <summary>
  42. /// Checks if the graph contains any data related to the given asset GUID.
  43. /// </summary>
  44. public bool AssetExists(string guid)
  45. {
  46. return _allKnownGuids.Contains(guid);
  47. }
  48. /// <summary>
  49. /// Clears and rebuilds the entire dependency graph by reading from the cache.
  50. /// </summary>
  51. public void BuildGraph()
  52. {
  53. _dependencies.Clear();
  54. _references.Clear();
  55. _allKnownGuids.Clear();
  56. var cachePath = DependencyCacheManager.GetCachePath();
  57. if (!Directory.Exists(cachePath))
  58. {
  59. Debug.LogWarning("Cache directory does not exist. Skipping dependency graph build.");
  60. return;
  61. }
  62. var files = Directory.GetFiles(cachePath, "*.json");
  63. foreach (var file in files)
  64. {
  65. try
  66. {
  67. // First, deserialize the outer wrapper object.
  68. var wrapper = JsonFileSystem.Read<MetadataWrapper>(file);
  69. if (wrapper == null || string.IsNullOrEmpty(wrapper.JsonData))
  70. {
  71. continue;
  72. }
  73. // Dynamically get the concrete type from the stored index using our helper.
  74. var concreteType = MetadataTypeHelper.GetTypeFromIndex(wrapper.AssetTypeIndex);
  75. if (concreteType == null)
  76. {
  77. Debug.LogWarning($"Could not find a registered type for index '{wrapper.AssetTypeIndex}' during deserialization. Skipping file: {Path.GetFileName(file)}");
  78. continue;
  79. }
  80. // Now, deserialize the nested JSON data into the correct concrete type.
  81. var metadata = JsonFileSystem.Read(wrapper.JsonData, concreteType) as AssetMetadata;
  82. if (metadata?.DependencyGuids == null) continue;
  83. _dependencies[metadata.Guid] = metadata.DependencyGuids;
  84. _allKnownGuids.Add(metadata.Guid);
  85. foreach(var depGuid in metadata.DependencyGuids)
  86. {
  87. _allKnownGuids.Add(depGuid);
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. Debug.LogError($"Failed to parse dependency file {Path.GetFileName(file)}. Error: {e.Message}");
  93. }
  94. }
  95. // Now, build the inverse map (the references).
  96. foreach (var (referrerGuid, dependencies) in _dependencies)
  97. {
  98. foreach (var dependencyGuid in dependencies)
  99. {
  100. if (!_references.ContainsKey(dependencyGuid))
  101. {
  102. _references[dependencyGuid] = new List<string>();
  103. }
  104. _references[dependencyGuid].Add(referrerGuid);
  105. }
  106. }
  107. }
  108. }
  109. }