EditorWindowHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using UnityEngine;
  4. using System.Threading.Tasks;
  5. using UnityEditor;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace SingularityGroup.HotReload.Editor {
  9. internal static class EditorWindowHelper {
  10. #if UNITY_2020_1_OR_NEWER
  11. public static bool supportsNotifications = true;
  12. #else
  13. public static bool supportsNotifications = false;
  14. #endif
  15. private static readonly Regex ValidEmailRegex = new Regex(@"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
  16. + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
  17. + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$", RegexOptions.IgnoreCase);
  18. public static bool IsValidEmailAddress(string email) {
  19. return ValidEmailRegex.IsMatch(email);
  20. }
  21. public static bool IsHumanControllingUs() {
  22. if (Application.isBatchMode) {
  23. return false;
  24. }
  25. var isCI = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI"));
  26. return !isCI;
  27. }
  28. internal enum NotificationStatus {
  29. None,
  30. Patching,
  31. NeedsRecompile
  32. }
  33. private static readonly Dictionary<NotificationStatus, GUIContent> notificationContent = new Dictionary<NotificationStatus, GUIContent> {
  34. { NotificationStatus.Patching, new GUIContent("[Hot Reload] Applying patches...")},
  35. { NotificationStatus.NeedsRecompile, new GUIContent("[Hot Reload] Unsupported Changes detected! Recompiling...")},
  36. };
  37. static Type gameViewT;
  38. private static EditorWindow[] gameViewWindows {
  39. get {
  40. gameViewT = gameViewT ?? typeof(EditorWindow).Assembly.GetType("UnityEditor.GameView");
  41. return Resources.FindObjectsOfTypeAll(gameViewT).Cast<EditorWindow>().ToArray();
  42. }
  43. }
  44. private static EditorWindow[] sceneWindows {
  45. get {
  46. return Resources.FindObjectsOfTypeAll(typeof(SceneView)).Cast<EditorWindow>().ToArray();
  47. }
  48. }
  49. private static EditorWindow[] notificationWindows {
  50. get {
  51. return gameViewWindows.Concat(sceneWindows).ToArray();
  52. }
  53. }
  54. static NotificationStatus lastNotificationStatus;
  55. private static DateTime? latestNotificationStartedAt;
  56. private static bool notificationShownRecently => latestNotificationStartedAt != null && DateTime.UtcNow - latestNotificationStartedAt < TimeSpan.FromSeconds(1);
  57. internal static void ShowNotification(NotificationStatus notificationType, float maxDuration = 3) {
  58. // Patch status goes from Unsupported changes to patching rapidly when making unsupported change
  59. // patching also shows right before unsupported changes sometimes
  60. // so we don't override NeedsRecompile notification ever
  61. bool willOverrideNeedsCompileNotification = notificationType != NotificationStatus.NeedsRecompile && notificationShownRecently || lastNotificationStatus == NotificationStatus.NeedsRecompile && notificationShownRecently;
  62. if (!supportsNotifications || willOverrideNeedsCompileNotification) {
  63. return;
  64. }
  65. foreach (EditorWindow notificationWindow in notificationWindows) {
  66. notificationWindow.ShowNotification(notificationContent[notificationType], maxDuration);
  67. notificationWindow.Repaint();
  68. }
  69. latestNotificationStartedAt = DateTime.UtcNow;
  70. lastNotificationStatus = notificationType;
  71. }
  72. internal static void RemoveNotification() {
  73. if (!supportsNotifications) {
  74. return;
  75. }
  76. // only patching notifications should be removed after showing less than 1 second
  77. if (notificationShownRecently && lastNotificationStatus != NotificationStatus.Patching) {
  78. return;
  79. }
  80. foreach (EditorWindow notificationWindow in notificationWindows) {
  81. notificationWindow.RemoveNotification();
  82. notificationWindow.Repaint();
  83. }
  84. latestNotificationStartedAt = null;
  85. lastNotificationStatus = NotificationStatus.None;
  86. }
  87. }
  88. }