HybridCLRPackageSetup.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEditor.PackageManager;
  5. using UnityEditor.PackageManager.Requests;
  6. using UnityEngine;
  7. [InitializeOnLoad]
  8. public static class HybridCLRPackageSetup
  9. {
  10. public const string PackageName = "com.code-philosophy.hybridclr";
  11. public const string PackageUrl = "https://gitee.com/focus-creative-games/hybridclr_unity.git#v9.0.3";
  12. const string AttemptedKey = "AppRuntimes.HybridCLR.PackageAttempted";
  13. const string ErrorKey = "AppRuntimes.HybridCLR.PackageError";
  14. static AddRequest request;
  15. static bool scheduled;
  16. static double nextCheck;
  17. public static bool IsInstalled => UnityEditor.PackageManager.PackageInfo.GetAllRegisteredPackages().Any(package => package.name == PackageName);
  18. public static bool IsInstalling => request != null || scheduled && !IsInstalled;
  19. public static string LastError => SessionState.GetString(ErrorKey, "");
  20. static HybridCLRPackageSetup() => Schedule();
  21. public static void RetryInstallation()
  22. {
  23. if (request != null || IsInstalled) return;
  24. SessionState.EraseBool(AttemptedKey);
  25. SessionState.EraseString(ErrorKey);
  26. Schedule();
  27. }
  28. static void Schedule()
  29. {
  30. scheduled = true;
  31. nextCheck = EditorApplication.timeSinceStartup + 1;
  32. EditorApplication.update -= Update;
  33. EditorApplication.update += Update;
  34. }
  35. static void StopChecking()
  36. {
  37. scheduled = false;
  38. EditorApplication.update -= Update;
  39. }
  40. static void Update()
  41. {
  42. if (request != null)
  43. {
  44. if (!request.IsCompleted) return;
  45. var completed = request;
  46. request = null;
  47. StopChecking();
  48. if (completed.Status == StatusCode.Success)
  49. {
  50. SessionState.EraseString(ErrorKey);
  51. Debug.Log("[AppRuntimes] HybridCLR package installed. Wait for compilation, then configure AppSettings and build resources.");
  52. }
  53. else ReportFailure(completed.Error?.message ?? "Package Manager did not return an error message.");
  54. return;
  55. }
  56. if (EditorApplication.timeSinceStartup < nextCheck || EditorApplication.isCompiling || EditorApplication.isUpdating ||
  57. EditorApplication.isPlayingOrWillChangePlaymode || BuildPipeline.isBuildingPlayer) return;
  58. if (IsInstalled)
  59. {
  60. SessionState.EraseString(ErrorKey);
  61. StopChecking();
  62. return;
  63. }
  64. if (SessionState.GetBool(AttemptedKey, false))
  65. {
  66. StopChecking();
  67. return;
  68. }
  69. SessionState.SetBool(AttemptedKey, true);
  70. try
  71. {
  72. Debug.Log("[AppRuntimes] Installing the missing HybridCLR package: " + PackageUrl);
  73. request = Client.Add(PackageUrl);
  74. }
  75. catch (Exception exception)
  76. {
  77. StopChecking();
  78. ReportFailure(exception.Message);
  79. }
  80. }
  81. static void ReportFailure(string message)
  82. {
  83. SessionState.SetString(ErrorKey, message);
  84. Debug.LogWarning("[AppRuntimes] HybridCLR package installation failed: " + message +
  85. "\nOpen AppSettings and retry, or add this Git URL in Package Manager: " + PackageUrl);
  86. }
  87. }