DependencyStoreBuilder.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. _allMetadata = new List<AssetMetadata>();
  54. _scriptMetadata = new List<ScriptMetadata>();
  55. _sceneMetadata = new List<SceneMetadata>();
  56. _miscMetadata = new List<MiscAssetMetadata>();
  57. _currentState = BuilderState.PreFetching;
  58. }
  59. private static void OnEditorUpdate()
  60. {
  61. // The state machine is processed here, once per editor frame.
  62. if (_currentState == BuilderState.Idle) return;
  63. try
  64. {
  65. switch (_currentState)
  66. {
  67. case BuilderState.PreFetching:
  68. ProcessPreFetchingBatch();
  69. break;
  70. case BuilderState.Analyzing:
  71. WaitForAnalysis();
  72. break;
  73. case BuilderState.Finalizing:
  74. FinalizeBuild();
  75. break;
  76. }
  77. }
  78. catch (Exception e)
  79. {
  80. Debug.LogError($"An error occurred during the build process. Aborting. Exception: {e}");
  81. ResetState();
  82. }
  83. }
  84. private static void ProcessPreFetchingBatch()
  85. {
  86. var totalAssets = _scriptGuidsToProcess.Count + _prefabGuidsToProcess.Count + _soGuidsToProcess.Count + _sceneGuidsToProcess.Count + _miscGuidsToProcess.Count;
  87. var initialTotal = _allMetadata.Count + totalAssets;
  88. for (var i = 0; i < BatchSize; i++)
  89. {
  90. if (_sceneGuidsToProcess.Count > 0)
  91. {
  92. var meta = AssetDataFetcher.PreFetchSceneMetadata(_sceneGuidsToProcess.Dequeue());
  93. if (meta != null)
  94. {
  95. _allMetadata.Add(meta);
  96. _sceneMetadata.Add(meta);
  97. }
  98. }
  99. else if (_scriptGuidsToProcess.Count > 0)
  100. {
  101. var meta = AssetDataFetcher.PreFetchScriptMetadata(_scriptGuidsToProcess.Dequeue());
  102. if (meta != null)
  103. {
  104. _allMetadata.Add(meta);
  105. _scriptMetadata.Add(meta);
  106. }
  107. }
  108. else if (_prefabGuidsToProcess.Count > 0)
  109. {
  110. var meta = AssetDataFetcher.PreFetchPrefabMetadata(_prefabGuidsToProcess.Dequeue());
  111. if (meta != null) _allMetadata.Add(meta);
  112. }
  113. else if (_soGuidsToProcess.Count > 0)
  114. {
  115. var meta = AssetDataFetcher.PreFetchScriptableObjectMetadata(_soGuidsToProcess.Dequeue());
  116. if (meta != null) _allMetadata.Add(meta);
  117. }
  118. else if (_miscGuidsToProcess.Count > 0)
  119. {
  120. var meta = AssetDataFetcher.PreFetchMiscAssetMetadata(_miscGuidsToProcess.Dequeue());
  121. if (meta != null)
  122. {
  123. _allMetadata.Add(meta);
  124. _miscMetadata.Add(meta);
  125. }
  126. }
  127. else
  128. {
  129. // All pre-fetching is complete.
  130. Debug.Log("Pre-fetching complete. Starting background analysis...");
  131. StartBackgroundAnalysis();
  132. _currentState = BuilderState.Analyzing;
  133. return;
  134. }
  135. }
  136. // Update progress bar
  137. var progress = initialTotal > 0 ? (float)_allMetadata.Count / initialTotal : 1;
  138. EditorUtility.DisplayProgressBar("Building Dependency Store (1/3)", "Pre-fetching asset data...", progress);
  139. }
  140. private static void StartBackgroundAnalysis()
  141. {
  142. // Gather all the data needed for the background task.
  143. var roslynSetupData = AssetDataFetcher.PreFetchRoslynSetupData(_scriptMetadata);
  144. // Start the background task, passing it the data it needs.
  145. _analysisTask = Task.Run(() => BackgroundAnalyzer.RunAnalysis(_allMetadata, roslynSetupData));
  146. }
  147. private static void WaitForAnalysis()
  148. {
  149. if (_analysisTask == null)
  150. {
  151. Debug.LogError("Analysis task was not started correctly. Aborting.");
  152. ResetState();
  153. return;
  154. }
  155. EditorUtility.DisplayProgressBar("Building Dependency Store (2/3)", "Analyzing dependencies in background...", _analysisTask.IsCompleted ? 1f : 0.5f);
  156. if (_analysisTask.IsCompleted)
  157. {
  158. _currentState = BuilderState.Finalizing;
  159. }
  160. }
  161. private static void FinalizeBuild()
  162. {
  163. EditorUtility.DisplayProgressBar("Building Dependency Store (3/3)", "Writing results to disk...", 0.9f);
  164. if (_analysisTask.IsFaulted)
  165. {
  166. Debug.LogError($"Background analysis failed: {_analysisTask.Exception}");
  167. }
  168. else
  169. {
  170. var results = _analysisTask.Result;
  171. DependencyCacheManager.WriteResults(results);
  172. Debug.Log("--- Async Dependency Store Build Complete! ---");
  173. }
  174. ResetState();
  175. }
  176. private static void ResetState()
  177. {
  178. EditorUtility.ClearProgressBar();
  179. _currentState = BuilderState.Idle;
  180. _analysisTask = null;
  181. }
  182. }
  183. }