DependencyStoreBuilder.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Threading.Tasks;
  5. using System.Collections.Generic;
  6. // All using statements now refer to the new data-only class
  7. using static IntelligentProjectAnalyzer.Editor.DependencyBuilderData;
  8. namespace IntelligentProjectAnalyzer.Editor
  9. {
  10. /// <summary>
  11. /// Acts as the central state machine and orchestrator of the dependency build process.
  12. /// This version performs the scan asynchronously and is driven by editor events.
  13. /// </summary>
  14. public static class DependencyStoreBuilder
  15. {
  16. private const int BatchSize = 20; // Number of assets to process per frame.
  17. #region State Management
  18. private enum BuilderState { Idle, PreFetching, Analyzing, Finalizing }
  19. private static BuilderState _currentState = BuilderState.Idle;
  20. // Queues for assets to be processed on the main thread
  21. private static Queue<string> _scriptGuidsToProcess;
  22. private static Queue<string> _prefabGuidsToProcess;
  23. private static Queue<string> _soGuidsToProcess;
  24. // Lists to store the pre-fetched metadata
  25. private static List<AssetMetadata> _allMetadata;
  26. private static List<ScriptMetadata> _scriptMetadata;
  27. // Task for background analysis
  28. private static Task<List<AssetMetadata>> _analysisTask;
  29. #endregion
  30. // Static constructor to hook into the editor update loop.
  31. static DependencyStoreBuilder()
  32. {
  33. EditorApplication.update += OnEditorUpdate;
  34. }
  35. /// <summary>
  36. /// Starts the build process. Called by DependencyBuilderTriggers.
  37. /// </summary>
  38. public static void BuildStore()
  39. {
  40. if (_currentState != BuilderState.Idle)
  41. {
  42. // A build is already running.
  43. return;
  44. }
  45. // --- Initialize State and Queues ---
  46. Debug.Log($"--- Starting Async Dependency Store Build ---");
  47. DependencyCacheManager.CleanAndPrepareCache();
  48. (_scriptGuidsToProcess, _prefabGuidsToProcess, _soGuidsToProcess) = AssetDataFetcher.FindAssetGuids();
  49. _allMetadata = new List<AssetMetadata>();
  50. _scriptMetadata = new List<ScriptMetadata>();
  51. _currentState = BuilderState.PreFetching;
  52. }
  53. private static void OnEditorUpdate()
  54. {
  55. // The state machine is processed here, once per editor frame.
  56. if (_currentState == BuilderState.Idle) return;
  57. try
  58. {
  59. switch (_currentState)
  60. {
  61. case BuilderState.PreFetching:
  62. ProcessPreFetchingBatch();
  63. break;
  64. case BuilderState.Analyzing:
  65. WaitForAnalysis();
  66. break;
  67. case BuilderState.Finalizing:
  68. FinalizeBuild();
  69. break;
  70. }
  71. }
  72. catch (Exception e)
  73. {
  74. Debug.LogError($"An error occurred during the build process. Aborting. Exception: {e}");
  75. ResetState();
  76. }
  77. }
  78. private static void ProcessPreFetchingBatch()
  79. {
  80. var totalAssets = _scriptGuidsToProcess.Count + _prefabGuidsToProcess.Count + _soGuidsToProcess.Count;
  81. var initialTotal = _allMetadata.Count + totalAssets;
  82. for (var i = 0; i < BatchSize; i++)
  83. {
  84. if (_scriptGuidsToProcess.Count > 0)
  85. {
  86. var meta = AssetDataFetcher.PreFetchScriptMetadata(_scriptGuidsToProcess.Dequeue());
  87. if (meta != null)
  88. {
  89. _allMetadata.Add(meta);
  90. _scriptMetadata.Add(meta);
  91. }
  92. }
  93. else if (_prefabGuidsToProcess.Count > 0)
  94. {
  95. var meta = AssetDataFetcher.PreFetchPrefabMetadata(_prefabGuidsToProcess.Dequeue());
  96. if (meta != null) _allMetadata.Add(meta);
  97. }
  98. else if (_soGuidsToProcess.Count > 0)
  99. {
  100. var meta = AssetDataFetcher.PreFetchScriptableObjectMetadata(_soGuidsToProcess.Dequeue());
  101. if (meta != null) _allMetadata.Add(meta);
  102. }
  103. else
  104. {
  105. // All pre-fetching is complete.
  106. Debug.Log("Pre-fetching complete. Starting background analysis...");
  107. StartBackgroundAnalysis();
  108. _currentState = BuilderState.Analyzing;
  109. return;
  110. }
  111. }
  112. // Update progress bar
  113. var progress = initialTotal > 0 ? (float)_allMetadata.Count / initialTotal : 1;
  114. EditorUtility.DisplayProgressBar("Building Dependency Store (1/3)", "Pre-fetching asset data...", progress);
  115. }
  116. private static void StartBackgroundAnalysis()
  117. {
  118. // Gather all the data needed for the background task.
  119. var roslynSetupData = AssetDataFetcher.PreFetchRoslynSetupData(_scriptMetadata);
  120. // Start the background task, passing it the data it needs.
  121. _analysisTask = Task.Run(() => BackgroundAnalyzer.RunAnalysis(_allMetadata, roslynSetupData));
  122. }
  123. private static void WaitForAnalysis()
  124. {
  125. if (_analysisTask == null)
  126. {
  127. Debug.LogError("Analysis task was not started correctly. Aborting.");
  128. ResetState();
  129. return;
  130. }
  131. EditorUtility.DisplayProgressBar("Building Dependency Store (2/3)", "Analyzing dependencies in background...", _analysisTask.IsCompleted ? 1f : 0.5f);
  132. if (_analysisTask.IsCompleted)
  133. {
  134. _currentState = BuilderState.Finalizing;
  135. }
  136. }
  137. private static void FinalizeBuild()
  138. {
  139. EditorUtility.DisplayProgressBar("Building Dependency Store (3/3)", "Writing results to disk...", 0.9f);
  140. if (_analysisTask.IsFaulted)
  141. {
  142. Debug.LogError($"Background analysis failed: {_analysisTask.Exception}");
  143. }
  144. else
  145. {
  146. var results = _analysisTask.Result;
  147. DependencyCacheManager.WriteResults(results);
  148. Debug.Log("--- Async Dependency Store Build Complete! ---");
  149. }
  150. ResetState();
  151. }
  152. private static void ResetState()
  153. {
  154. EditorUtility.ClearProgressBar();
  155. _currentState = BuilderState.Idle;
  156. _analysisTask = null;
  157. }
  158. }
  159. }