SRIntegrityCheckWindow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace SRDebugger.Editor
  6. {
  7. [InitializeOnLoad]
  8. class SRIntegrityCheckWindow : EditorWindow
  9. {
  10. private List<IntegrityIssue> _results;
  11. private Vector2 _scrollPosition;
  12. private bool _applyingFix;
  13. private static bool _isOpen;
  14. static SRIntegrityCheckWindow()
  15. {
  16. // Delay call to prevent any UI stalls after compile complete.
  17. EditorApplication.delayCall += () =>
  18. {
  19. if (!_isOpen && SRDebugEditor.QuickIntegrityCheck().Any())
  20. {
  21. Debug.Log("[SRDebugger] Some issues have been detected with SRDebugger, opening integrity check window.");
  22. Open();
  23. }
  24. };
  25. }
  26. public static void Open()
  27. {
  28. var window = GetWindow<SRIntegrityCheckWindow>(true, "SRDebugger Integrity Check", true);
  29. window.minSize = new Vector2(640, 400);
  30. window.Show();
  31. }
  32. private void OnEnable()
  33. {
  34. _isOpen = true;
  35. RefreshIntegrityCheck();
  36. }
  37. private void OnDisable()
  38. {
  39. _isOpen = false;
  40. }
  41. public void RefreshIntegrityCheck()
  42. {
  43. _results = SRDebugEditor.QuickIntegrityCheck().ToList();
  44. }
  45. private void OnGUI()
  46. {
  47. // Draw header area
  48. SRInternalEditorUtil.BeginDrawBackground();
  49. SRInternalEditorUtil.DrawLogo(SRInternalEditorUtil.GetLogo());
  50. SRInternalEditorUtil.EndDrawBackground();
  51. // Draw header/content divider
  52. EditorGUILayout.BeginVertical(SRInternalEditorUtil.Styles.SettingsHeaderBoxStyle);
  53. EditorGUILayout.EndVertical();
  54. GUILayout.Label(
  55. "SRDebugger automatically scans your project to find common issues with the SRDebugger installation.");
  56. EditorGUILayout.Space();
  57. // TODO: Enable button when there are some more 'expensive' integrity checks. For now no point as alt the checks are really quick
  58. if (GUILayout.Button("Refresh"))
  59. {
  60. RefreshIntegrityCheck();
  61. }
  62. if (_applyingFix)
  63. {
  64. if (!EditorApplication.isCompiling && !EditorApplication.isUpdating)
  65. {
  66. _applyingFix = false;
  67. RefreshIntegrityCheck();
  68. }
  69. EditorGUI.BeginDisabledGroup(_applyingFix);
  70. }
  71. EditorGUILayout.Space();
  72. if (_results == null)
  73. {
  74. _results = new List<IntegrityIssue>();
  75. }
  76. EditorGUILayout.TextArea("Issues Detected: " + _results.Count, EditorStyles.boldLabel);
  77. EditorGUILayout.Separator();
  78. EditorGUILayout.Space();
  79. if (_results.Count == 0)
  80. {
  81. EditorGUILayout.HelpBox("No issues have been found!", MessageType.None);
  82. }
  83. else
  84. {
  85. EditorGUILayout.HelpBox("It is highly recommended to backup your project before using this tool.", MessageType.Warning);
  86. _scrollPosition = GUILayout.BeginScrollView(_scrollPosition, false, false,
  87. GUILayout.Width(position.width));
  88. DrawIssuesList();
  89. EditorGUILayout.EndScrollView();
  90. }
  91. if (_applyingFix)
  92. {
  93. EditorGUI.EndDisabledGroup();
  94. }
  95. }
  96. private void DrawIssuesList()
  97. {
  98. EditorGUILayout.BeginVertical();
  99. for (var i = 0; i < _results.Count; i++)
  100. {
  101. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  102. GUILayout.Label(_results[i].Title, EditorStyles.boldLabel);
  103. GUILayout.Label(_results[i].Description, SRInternalEditorUtil.Styles.ParagraphLabel);
  104. var fixes = _results[i].GetFixes();
  105. if (fixes.Count > 0)
  106. {
  107. EditorGUILayout.Space();
  108. EditorGUILayout.BeginHorizontal();
  109. EditorGUILayout.BeginVertical();
  110. GUILayout.Label("Possible Fixes:", EditorStyles.miniBoldLabel);
  111. foreach (Fix fix in fixes)
  112. {
  113. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  114. GUILayout.Space(10);
  115. EditorGUILayout.BeginVertical();
  116. GUILayout.Label(fix.Name, EditorStyles.boldLabel);
  117. GUILayout.Label(fix.Description, SRInternalEditorUtil.Styles.ParagraphLabelItalic);
  118. if (fix.IsAutoFix && GUILayout.Button("Apply Fix", GUILayout.Width(90)))
  119. {
  120. fix.Execute();
  121. _applyingFix = true;
  122. }
  123. GUILayout.Space(2);
  124. EditorGUILayout.EndVertical();
  125. EditorGUILayout.EndHorizontal();
  126. EditorGUILayout.Space();
  127. }
  128. EditorGUILayout.EndVertical();
  129. EditorGUILayout.EndHorizontal();
  130. EditorGUILayout.EndVertical();
  131. }
  132. }
  133. GUILayout.FlexibleSpace();
  134. EditorGUILayout.EndVertical();
  135. }
  136. }
  137. }