RequestJobManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System.Collections.Generic; //HashSet
  2. using UnityEditor.PackageManager.Requests; //ListRequest, AddRequest, etc
  3. using UnityEditor.PackageManager; //PackageCollection
  4. using System; //Action
  5. namespace Unity.RenderStreaming.Editor {
  6. /// <summary>
  7. /// An editor class to manage requests to UnityEditor.PackageManager.Client
  8. /// This class will perform its operations in background while Unity is running.
  9. /// </summary>
  10. internal class RequestJobManager
  11. {
  12. [UnityEditor.InitializeOnLoadMethod]
  13. static void OnLoad() {
  14. UnityEditor.EditorApplication.update+=UpdateRequestJobs;
  15. }
  16. //---------------------------------------------------------------------------------------------------------------------
  17. /// <summary>
  18. /// Queue a job to list the packages the project depends on.
  19. /// </summary>
  20. /// <param name="offlineMode">Specifies whether or not the Package Manager requests the latest information about
  21. /// the project's packages from the remote Unity package registry. When offlineMode is true,
  22. /// the PackageInfo objects in the PackageCollection returned by the Package Manager contain information
  23. /// obtained from the local package cache, which could be out of date.</param>
  24. /// <param name="includeIndirectIndependencies">Set to true to include indirect dependencies in the
  25. /// PackageCollection returned by the Package Manager. Indirect dependencies include packages referenced
  26. /// in the manifests of project packages or in the manifests of other indirect dependencies. Set to false
  27. /// to include only the packages listed directly in the project manifest.</param>
  28. /// <param name="onSuccess">Action which is executed if the request succeeded</param>
  29. /// <param name="onFail">Action which is executed if the request failed </param>
  30. ///
  31. public static void CreateListRequest(bool offlineMode, bool includeIndirectIndependencies,
  32. Action<Request<PackageCollection>> onSuccess, Action<Request<PackageCollection>> onFail)
  33. {
  34. m_pendingListRequests.Enqueue(new ListRequestInfo(offlineMode, includeIndirectIndependencies, onSuccess, onFail));
  35. }
  36. //---------------------------------------------------------------------------------------------------------------------
  37. /// <summary>
  38. /// Queue a job to add a package dependency to the project.
  39. /// </summary>
  40. /// <param name="packageName">The name or ID of the package to add. If only the name is specified,
  41. /// the latest version of the package is installed.</param>
  42. /// <param name="onSuccess">Action which is executed if the request succeeded</param>
  43. /// <param name="onFail">Action which is executed if the request failed </param>
  44. ///
  45. public static void CreateAddRequest(string packageName,
  46. Action<Request<PackageInfo>> onSuccess, Action<Request<PackageInfo>> onFail)
  47. {
  48. m_pendingAddRequests.Enqueue(new AddRequestInfo(packageName, onSuccess, onFail));
  49. }
  50. //---------------------------------------------------------------------------------------------------------------------
  51. /// <summary>
  52. /// Queue a job to removes a previously added package from the project.
  53. /// </summary>
  54. /// <param name="packageName">The name or ID of the package to add. </param>
  55. /// <param name="onSuccess">Action which is executed if the request succeeded</param>
  56. /// <param name="onFail">Action which is executed if the request failed </param>
  57. ///
  58. public static void CreateRemoveRequest(string packageName, Action onSuccess, Action onFail)
  59. {
  60. m_pendingRemoveRequests.Enqueue(new RemoveRequestInfo(packageName, onSuccess, onFail));
  61. }
  62. //---------------------------------------------------------------------------------------------------------------------
  63. /// <summary>
  64. /// Queue a job to searches the Unity package registry for the given package.
  65. /// </summary>
  66. /// <param name="packageName">The name or ID of the package to add.</param>
  67. /// <param name="offlineMode">Specifies whether or not the Package Manager requests the latest information about
  68. /// the project's packages from the remote Unity package registry. When offlineMode is true,
  69. /// the PackageInfo objects in the PackageCollection returned by the Package Manager contain information
  70. /// obtained from the local package cache, which could be out of date.</param>
  71. /// <param name="onSuccess">Action which is executed if the request succeeded</param>
  72. /// <param name="onFail">Action which is executed if the request failed </param>
  73. ///
  74. public static void CreateSearchRequest(string packageName, bool offlineMode,
  75. Action<Request<PackageInfo[]>> onSuccess, Action<Request<PackageInfo[]>> onFail)
  76. {
  77. m_pendingSearchRequests.Enqueue(new SearchRequestInfo(packageName, offlineMode, onSuccess, onFail));
  78. }
  79. //---------------------------------------------------------------------------------------------------------------------
  80. /// <summary>
  81. /// Queue a job to search the Unity package registry for all packages compatible with the current Unity version.
  82. /// </summary>
  83. /// <param name="offlineMode">Specifies whether or not the Package Manager requests the latest information about
  84. /// the project's packages from the remote Unity package registry. When offlineMode is true,
  85. /// the PackageInfo objects in the PackageCollection returned by the Package Manager contain information
  86. /// obtained from the local package cache, which could be out of date.</param>
  87. /// <param name="onSuccess">Action which is executed if the request succeeded</param>
  88. /// <param name="onFail">Action which is executed if the request failed </param>
  89. ///
  90. public static void CreateSearchAllRequest(bool offlineMode,
  91. Action<Request<PackageInfo[]>> onSuccess, Action<Request<PackageInfo[]>> onFail)
  92. {
  93. m_pendingSearchAllRequests.Enqueue(new SearchAllRequestInfo(offlineMode, onSuccess, onFail));
  94. }
  95. //---------------------------------------------------------------------------------------------------------------------
  96. static void UpdateRequestJobs()
  97. {
  98. { //Process pending list requests
  99. var enumerator = m_pendingListRequests.GetEnumerator();
  100. while (enumerator.MoveNext()) {
  101. ListRequestInfo info = enumerator.Current;
  102. ListRequest listReq = Client.List(info.OfflineMode, info.IncludeIndirectIndependencies);
  103. m_requestJobs.Add(new RequestJob<PackageCollection>(listReq,info.OnSuccessAction,info.OnFailAction));
  104. }
  105. m_pendingListRequests.Clear();
  106. }
  107. { //Process pending addrequests
  108. var enumerator = m_pendingAddRequests.GetEnumerator();
  109. while (enumerator.MoveNext()) {
  110. AddRequestInfo info = enumerator.Current;
  111. AddRequest addReq = Client.Add(info.PackageName);
  112. m_requestJobs.Add(new RequestJob<PackageInfo>(addReq,info.OnSuccessAction,info.OnFailAction));
  113. }
  114. m_pendingAddRequests.Clear();
  115. }
  116. { //Process pending RemoveRequests
  117. var enumerator = m_pendingRemoveRequests.GetEnumerator();
  118. while (enumerator.MoveNext()) {
  119. RemoveRequestInfo info = enumerator.Current;
  120. RemoveRequest removeReq = Client.Remove(info.PackageName);
  121. m_requestJobs.Add(new RequestJob(removeReq,info.OnSuccessAction,info.OnFailAction));
  122. }
  123. m_pendingRemoveRequests.Clear();
  124. }
  125. { //Process pending SearchRequests
  126. var enumerator = m_pendingSearchRequests.GetEnumerator();
  127. while (enumerator.MoveNext()) {
  128. SearchRequestInfo info = enumerator.Current;
  129. SearchRequest searchReq = Client.Search(info.PackageName, info.OfflineMode);
  130. m_requestJobs.Add(new RequestJob<PackageInfo[]>(searchReq,info.OnSuccessAction,info.OnFailAction));
  131. }
  132. m_pendingSearchRequests.Clear();
  133. }
  134. { //Process pending SearchAllRequests
  135. var enumerator = m_pendingSearchAllRequests.GetEnumerator();
  136. while (enumerator.MoveNext()) {
  137. SearchAllRequestInfo info = enumerator.Current;
  138. SearchRequest searchReq = Client.SearchAll(info.OfflineMode);
  139. m_requestJobs.Add(new RequestJob<PackageInfo[]>(searchReq,info.OnSuccessAction,info.OnFailAction));
  140. }
  141. m_pendingSearchAllRequests.Clear();
  142. }
  143. { //Update and register completed jobs
  144. var enumerator = m_requestJobs.GetEnumerator();
  145. while (enumerator.MoveNext()) {
  146. StatusCode code = enumerator.Current.Update();
  147. if (StatusCode.Failure == code || StatusCode.Success == code) {
  148. m_jobsToDelete.Add(enumerator.Current);
  149. }
  150. }
  151. }
  152. { //delete completed jobs
  153. var enumerator = m_jobsToDelete.GetEnumerator();
  154. while (enumerator.MoveNext()) {
  155. m_requestJobs.Remove(enumerator.Current);
  156. }
  157. m_jobsToDelete.Clear();
  158. }
  159. }
  160. //---------------------------------------------------------------------------------------------------------------------
  161. static Queue<ListRequestInfo> m_pendingListRequests = new Queue<ListRequestInfo>();
  162. static Queue<AddRequestInfo> m_pendingAddRequests = new Queue<AddRequestInfo>();
  163. static Queue<RemoveRequestInfo> m_pendingRemoveRequests = new Queue<RemoveRequestInfo>();
  164. static Queue<SearchRequestInfo> m_pendingSearchRequests = new Queue<SearchRequestInfo>();
  165. static Queue<SearchAllRequestInfo> m_pendingSearchAllRequests = new Queue<SearchAllRequestInfo>();
  166. static System.Collections.Generic.HashSet<IRequestJob> m_requestJobs = new HashSet<IRequestJob>();
  167. static System.Collections.Generic.List<IRequestJob> m_jobsToDelete = new List<IRequestJob>();
  168. }
  169. } //namespace Unity.RenderStreaming.Editor