DependencyGraph.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 on.
  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. /// <summary>
  20. /// Initializes a new instance of the DependencyGraph class and builds it from the cache.
  21. /// </summary>
  22. public DependencyGraph()
  23. {
  24. BuildGraph();
  25. }
  26. /// <summary>
  27. /// Gets the list of assets that the specified asset depends on.
  28. /// </summary>
  29. public List<string> GetDependencies(string guid)
  30. {
  31. return _dependencies.TryGetValue(guid, out var deps) ? deps : new List<string>();
  32. }
  33. /// <summary>
  34. /// Gets the list of assets that reference the specified asset.
  35. /// </summary>
  36. public List<string> GetReferences(string guid)
  37. {
  38. return _references.TryGetValue(guid, out var refs) ? refs : new List<string>();
  39. }
  40. /// <summary>
  41. /// Clears and rebuilds the entire dependency graph by reading from the cache.
  42. /// </summary>
  43. public void BuildGraph()
  44. {
  45. _dependencies.Clear();
  46. _references.Clear();
  47. var cachePath = DependencyCacheManager.GetCachePath();
  48. if (!Directory.Exists(cachePath))
  49. {
  50. // This is an expected state if the analysis hasn't run yet, so no warning is needed.
  51. return;
  52. }
  53. var files = Directory.GetFiles(cachePath, "*.json");
  54. foreach (var file in files)
  55. {
  56. try
  57. {
  58. // First, deserialize the outer wrapper object.
  59. var wrapper = JsonFileSystem.Read<MetadataWrapper>(file);
  60. if (wrapper == null || string.IsNullOrEmpty(wrapper.JsonData))
  61. {
  62. continue;
  63. }
  64. // Dynamically get the concrete type from the stored index using our helper.
  65. var concreteType = MetadataTypeHelper.GetTypeFromIndex(wrapper.AssetTypeIndex);
  66. if (concreteType == null)
  67. {
  68. Debug.LogWarning($"Could not find a registered type for index '{wrapper.AssetTypeIndex}' during deserialization. Skipping file: {Path.GetFileName(file)}");
  69. continue;
  70. }
  71. // Now, deserialize the nested JSON data into the correct concrete type.
  72. var metadata = JsonFileSystem.Read(wrapper.JsonData, concreteType) as AssetMetadata;
  73. if (metadata?.DependencyGuids != null)
  74. {
  75. _dependencies[metadata.Guid] = metadata.DependencyGuids;
  76. }
  77. }
  78. catch (Exception e)
  79. {
  80. Debug.LogError($"Failed to parse dependency file {Path.GetFileName(file)}. Error: {e.Message}");
  81. }
  82. }
  83. // Now, build the inverse map (the references).
  84. foreach (var entry in _dependencies)
  85. {
  86. var referrerGuid = entry.Key;
  87. foreach (var dependencyGuid in entry.Value)
  88. {
  89. if (!_references.ContainsKey(dependencyGuid))
  90. {
  91. _references[dependencyGuid] = new List<string>();
  92. }
  93. _references[dependencyGuid].Add(referrerGuid);
  94. }
  95. }
  96. }
  97. }
  98. }