DependencyCacheManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using Newtonsoft.Json;
  5. using System.Collections.Generic;
  6. using IntelligentProjectAnalyzer.Helper;
  7. // All using statements now refer to the new data-only class
  8. using static IntelligentProjectAnalyzer.Editor.DependencyBuilderData;
  9. namespace IntelligentProjectAnalyzer.Editor
  10. {
  11. /// <summary>
  12. /// Manages all file system operations related to the dependency cache.
  13. /// </summary>
  14. public static class DependencyCacheManager
  15. {
  16. private const string CacheFolderName = "DependencyCache";
  17. /// <summary>
  18. /// Gets the full path to the dependency cache directory.
  19. /// </summary>
  20. /// <returns>The absolute path to the cache folder within the project's Library folder.</returns>
  21. public static string GetCachePath()
  22. {
  23. return Path.Combine("Library", CacheFolderName);
  24. }
  25. /// <summary>
  26. /// Deletes the existing cache directory and recreates it to ensure a clean state.
  27. /// </summary>
  28. public static void CleanAndPrepareCache()
  29. {
  30. var cachePath = GetCachePath();
  31. if (Directory.Exists(cachePath))
  32. {
  33. try
  34. {
  35. Directory.Delete(cachePath, true);
  36. }
  37. catch (Exception e)
  38. {
  39. Debug.LogError($"Could not clean cache folder at {cachePath}. Please remove it manually. Error: {e.Message}");
  40. return;
  41. }
  42. }
  43. Directory.CreateDirectory(cachePath);
  44. }
  45. /// <summary>
  46. /// Writes the analysis results to individual JSON files using the wrapper object.
  47. /// </summary>
  48. /// <param name="results">The list of unified AssetMetadata to write to disk.</param>
  49. public static void WriteResults(List<AssetMetadata> results)
  50. {
  51. var cachePath = GetCachePath();
  52. foreach (var result in results)
  53. {
  54. var typeIndex = MetadataTypeHelper.GetIndexFromType(result.GetType());
  55. if (typeIndex == -1)
  56. {
  57. Debug.LogWarning($"Could not find a registered type for {result.GetType()}. Skipping asset with GUID {result.Guid}.");
  58. continue;
  59. }
  60. // Create the wrapper object.
  61. var wrapper = new MetadataWrapper
  62. {
  63. // Store the fully qualified type name so we can deserialize correctly later.
  64. AssetTypeIndex = typeIndex,
  65. // Serialize the actual metadata object into a JSON string.
  66. JsonData = JsonFileSystem.GetJson(result)
  67. };
  68. var outputPath = Path.Combine(cachePath, $"{result.Guid}.json");
  69. // Now, serialize the WRAPPER object to the file. This is safe because
  70. // MetadataWrapper is a concrete, non-abstract class.
  71. JsonFileSystem.Write(wrapper, outputPath);
  72. }
  73. }
  74. }
  75. }