EditorPackageInstaller.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.PackageManager;
  6. using UnityEditor.PackageManager.Requests;
  7. using UnityEngine;
  8. using PackageInfo = UnityEditor.PackageManager.PackageInfo;
  9. // All AppRuntimes package requests share one queue: UPM operations must be sequential.
  10. internal sealed class EditorPackageInstaller
  11. {
  12. static readonly List<EditorPackageInstaller> Pending = new List<EditorPackageInstaller>();
  13. static EditorPackageInstaller active;
  14. static AddRequest request;
  15. static double nextCheck;
  16. readonly string name, url, label, key, retryHint;
  17. internal EditorPackageInstaller(string name, string url, string label, string key, string retryHint)
  18. {
  19. this.name = name;
  20. this.url = url;
  21. this.label = label;
  22. this.key = key;
  23. this.retryHint = retryHint;
  24. }
  25. internal bool IsInstalled => PackageInfo.GetAllRegisteredPackages().Any(package => package.name == name);
  26. internal bool IsInstalling => active == this || Pending.Contains(this) && !IsInstalled;
  27. internal string LastError => SessionState.GetString(key + ".PackageError", "");
  28. internal void Retry()
  29. {
  30. if (active == this || IsInstalled) return;
  31. SessionState.EraseBool(key + ".PackageAttempted");
  32. SessionState.EraseString(key + ".PackageError");
  33. Schedule();
  34. }
  35. internal void Schedule()
  36. {
  37. if (!Pending.Contains(this)) Pending.Add(this);
  38. nextCheck = EditorApplication.timeSinceStartup + 1;
  39. EditorApplication.update -= Update;
  40. EditorApplication.update += Update;
  41. }
  42. static void Update()
  43. {
  44. if (request != null)
  45. {
  46. if (!request.IsCompleted) return;
  47. var completed = request;
  48. var installer = active;
  49. request = null;
  50. active = null;
  51. if (completed.Status == StatusCode.Success)
  52. {
  53. SessionState.EraseString(installer.key + ".PackageError");
  54. Debug.Log("[AppRuntimes] " + installer.label + " package installed. Wait for compilation to finish.");
  55. }
  56. else installer.ReportFailure(completed.Error?.message ?? "Package Manager did not return an error message.");
  57. nextCheck = EditorApplication.timeSinceStartup + 1;
  58. return;
  59. }
  60. if (Pending.Count == 0)
  61. {
  62. EditorApplication.update -= Update;
  63. return;
  64. }
  65. if (EditorApplication.timeSinceStartup < nextCheck || EditorApplication.isCompiling || EditorApplication.isUpdating ||
  66. EditorApplication.isPlayingOrWillChangePlaymode || BuildPipeline.isBuildingPlayer) return;
  67. var next = Pending[0];
  68. Pending.RemoveAt(0);
  69. if (next.IsInstalled)
  70. {
  71. SessionState.EraseString(next.key + ".PackageError");
  72. return;
  73. }
  74. if (SessionState.GetBool(next.key + ".PackageAttempted", false)) return;
  75. SessionState.SetBool(next.key + ".PackageAttempted", true);
  76. try
  77. {
  78. Debug.Log("[AppRuntimes] Installing the missing " + next.label + " package: " + next.url);
  79. active = next;
  80. request = Client.Add(next.url);
  81. }
  82. catch (Exception exception)
  83. {
  84. active = null;
  85. next.ReportFailure(exception.Message);
  86. }
  87. }
  88. void ReportFailure(string message)
  89. {
  90. SessionState.SetString(key + ".PackageError", message);
  91. Debug.LogWarning("[AppRuntimes] " + label + " package installation failed: " + message +
  92. "\n" + retryHint + " Or add this Git URL in Package Manager: " + url);
  93. }
  94. }