DependencyStoreBuilder.cs 7.3 KB

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