DependencyStoreBuilder.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. private static Queue<string> _sceneGuidsToProcess;
  25. private static Queue<string> _miscGuidsToProcess;
  26. // Lists to store the pre-fetched metadata
  27. private static List<AssetMetadata> _allMetadata;
  28. private static List<ScriptMetadata> _scriptMetadata;
  29. private static List<SceneMetadata> _sceneMetadata;
  30. private static List<MiscAssetMetadata> _miscMetadata;
  31. // Task for background analysis
  32. private static Task<List<AssetMetadata>> _analysisTask;
  33. #endregion
  34. // Static constructor to hook into the editor update loop.
  35. static DependencyStoreBuilder()
  36. {
  37. EditorApplication.update += OnEditorUpdate;
  38. }
  39. /// <summary>
  40. /// Starts the build process. Called by DependencyBuilderTriggers.
  41. /// </summary>
  42. public static void BuildStore()
  43. {
  44. if (_currentState != BuilderState.Idle)
  45. {
  46. // A build is already running.
  47. return;
  48. }
  49. // --- Initialize State and Queues ---
  50. Debug.Log($"--- Starting Async Dependency Store Build ---");
  51. DependencyCacheManager.CleanAndPrepareCache();
  52. (_scriptGuidsToProcess, _prefabGuidsToProcess, _soGuidsToProcess, _sceneGuidsToProcess, _miscGuidsToProcess) = AssetDataFetcher.FindAssetGuids();
  53. var analyzerSettings = AnalyzerSettingsCrud.GetOrCreateSettings();
  54. if (!analyzerSettings.AnalyzeScripts)
  55. {
  56. _scriptGuidsToProcess?.Clear();
  57. }
  58. if (!analyzerSettings.AnalyzePrefabs)
  59. {
  60. _prefabGuidsToProcess?.Clear();
  61. }
  62. if (!analyzerSettings.AnalyzeScriptableObjects)
  63. {
  64. _soGuidsToProcess?.Clear();
  65. }
  66. if (!analyzerSettings.AnalyzeMiscellaneous)
  67. {
  68. _miscGuidsToProcess?.Clear();
  69. }
  70. _allMetadata = new List<AssetMetadata>();
  71. _scriptMetadata = new List<ScriptMetadata>();
  72. _sceneMetadata = new List<SceneMetadata>();
  73. _miscMetadata = new List<MiscAssetMetadata>();
  74. _currentState = BuilderState.PreFetching;
  75. }
  76. private static void OnEditorUpdate()
  77. {
  78. // The state machine is processed here, once per editor frame.
  79. if (_currentState == BuilderState.Idle) return;
  80. try
  81. {
  82. switch (_currentState)
  83. {
  84. case BuilderState.PreFetching:
  85. ProcessPreFetchingBatch();
  86. break;
  87. case BuilderState.Analyzing:
  88. WaitForAnalysis();
  89. break;
  90. case BuilderState.Finalizing:
  91. FinalizeBuild();
  92. break;
  93. }
  94. }
  95. catch (Exception e)
  96. {
  97. Debug.LogError($"An error occurred during the build process. Aborting. Exception: {e}");
  98. ResetState();
  99. }
  100. }
  101. private static void ProcessPreFetchingBatch()
  102. {
  103. var totalAssets = _scriptGuidsToProcess.Count + _prefabGuidsToProcess.Count + _soGuidsToProcess.Count + _sceneGuidsToProcess.Count + _miscGuidsToProcess.Count;
  104. var initialTotal = _allMetadata.Count + totalAssets;
  105. for (var i = 0; i < BatchSize; i++)
  106. {
  107. if (_sceneGuidsToProcess.Count > 0)
  108. {
  109. var meta = AssetDataFetcher.PreFetchSceneMetadata(_sceneGuidsToProcess.Dequeue());
  110. if (meta != null)
  111. {
  112. _allMetadata.Add(meta);
  113. _sceneMetadata.Add(meta);
  114. }
  115. }
  116. else if (_scriptGuidsToProcess.Count > 0)
  117. {
  118. var meta = AssetDataFetcher.PreFetchScriptMetadata(_scriptGuidsToProcess.Dequeue());
  119. if (meta != null)
  120. {
  121. _allMetadata.Add(meta);
  122. _scriptMetadata.Add(meta);
  123. }
  124. }
  125. else if (_prefabGuidsToProcess.Count > 0)
  126. {
  127. var meta = AssetDataFetcher.PreFetchPrefabMetadata(_prefabGuidsToProcess.Dequeue());
  128. if (meta != null) _allMetadata.Add(meta);
  129. }
  130. else if (_soGuidsToProcess.Count > 0)
  131. {
  132. var meta = AssetDataFetcher.PreFetchScriptableObjectMetadata(_soGuidsToProcess.Dequeue());
  133. if (meta != null) _allMetadata.Add(meta);
  134. }
  135. else if (_miscGuidsToProcess.Count > 0)
  136. {
  137. var meta = AssetDataFetcher.PreFetchMiscAssetMetadata(_miscGuidsToProcess.Dequeue());
  138. if (meta != null)
  139. {
  140. _allMetadata.Add(meta);
  141. _miscMetadata.Add(meta);
  142. }
  143. }
  144. else
  145. {
  146. // All pre-fetching is complete.
  147. Debug.Log("Pre-fetching complete. Starting background analysis...");
  148. StartBackgroundAnalysis();
  149. _currentState = BuilderState.Analyzing;
  150. return;
  151. }
  152. }
  153. // Update progress bar
  154. var progress = initialTotal > 0 ? (float)_allMetadata.Count / initialTotal : 1;
  155. EditorUtility.DisplayProgressBar("Building Dependency Store (1/3)", "Pre-fetching asset data...", progress);
  156. }
  157. private static void StartBackgroundAnalysis()
  158. {
  159. // Gather all the data needed for the background task.
  160. var roslynSetupData = AssetDataFetcher.PreFetchRoslynSetupData(_scriptMetadata);
  161. // Start the background task, passing it the data it needs.
  162. _analysisTask = Task.Run(() => BackgroundAnalyzer.RunAnalysis(_allMetadata, roslynSetupData));
  163. }
  164. private static void WaitForAnalysis()
  165. {
  166. if (_analysisTask == null)
  167. {
  168. Debug.LogError("Analysis task was not started correctly. Aborting.");
  169. ResetState();
  170. return;
  171. }
  172. EditorUtility.DisplayProgressBar("Building Dependency Store (2/3)", "Analyzing dependencies in background...", _analysisTask.IsCompleted ? 1f : 0.5f);
  173. if (_analysisTask.IsCompleted)
  174. {
  175. _currentState = BuilderState.Finalizing;
  176. }
  177. }
  178. private static void FinalizeBuild()
  179. {
  180. EditorUtility.DisplayProgressBar("Building Dependency Store (3/3)", "Writing results to disk...", 0.9f);
  181. if (_analysisTask.IsFaulted)
  182. {
  183. Debug.LogError($"Background analysis failed: {_analysisTask.Exception}");
  184. }
  185. else
  186. {
  187. var results = _analysisTask.Result;
  188. DependencyCacheManager.WriteResults(results);
  189. Debug.Log("--- Async Dependency Store Build Complete! ---");
  190. }
  191. ResetState();
  192. }
  193. private static void ResetState()
  194. {
  195. EditorUtility.ClearProgressBar();
  196. _currentState = BuilderState.Idle;
  197. _analysisTask = null;
  198. }
  199. }
  200. }