PackageUtility.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Release
  10. {
  11. using LitJson;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using UnityEditor;
  15. using UnityEditor.PackageManager;
  16. using UnityEditor.PackageManager.Requests;
  17. using UnityEngine;
  18. public enum TaskType
  19. {
  20. Add,
  21. Remove,
  22. Get
  23. }
  24. public struct TaskInfo
  25. {
  26. public TaskType taskType;
  27. public string param;
  28. public OnResponse onResponse;
  29. }
  30. public struct Result
  31. {
  32. public bool isSuccess;
  33. public List<UnityEditor.PackageManager.PackageInfo> packages;
  34. }
  35. public delegate void OnResponse(Result result);
  36. public static class PackageUtility
  37. {
  38. public static Queue<TaskInfo> taskQueue = new Queue<TaskInfo>();
  39. public static Dictionary<Request, TaskInfo> taskDict = new Dictionary<Request, TaskInfo>();
  40. private static Request currentRequest = null;
  41. static PackageUtility()
  42. {
  43. EditorApplication.update -= Update;
  44. EditorApplication.update += Update;
  45. }
  46. public static void Update()
  47. {
  48. if (currentRequest != null)
  49. {
  50. TaskInfo info;
  51. taskDict.TryGetValue(currentRequest, out info);
  52. if (currentRequest.IsCompleted)
  53. {
  54. Result result = new Result();
  55. result.isSuccess = currentRequest.Status == StatusCode.Success;
  56. if (info.taskType == TaskType.Get)
  57. {
  58. if (currentRequest.Status == StatusCode.Success)
  59. {
  60. result.packages = new List<UnityEditor.PackageManager.PackageInfo>();
  61. foreach (var package in ((ListRequest)currentRequest).Result)
  62. {
  63. result.packages.Add(package);
  64. }
  65. }
  66. }
  67. info.onResponse?.Invoke(result);
  68. taskDict.Remove(currentRequest);
  69. currentRequest = null;
  70. }
  71. else
  72. {
  73. //Debug.LogFormat("[{0}]:{1}", info.taskType, info.param);
  74. return;
  75. }
  76. }
  77. if (taskQueue.Count != 0)
  78. {
  79. TaskInfo task = taskQueue.Dequeue();
  80. switch (task.taskType)
  81. {
  82. case TaskType.Add:
  83. currentRequest = Client.Add(task.param);
  84. break;
  85. case TaskType.Remove:
  86. currentRequest = Client.Remove(task.param);
  87. break;
  88. case TaskType.Get:
  89. currentRequest = Client.List();
  90. break;
  91. default:
  92. break;
  93. }
  94. taskDict.Add(currentRequest, task);
  95. }
  96. }
  97. public static void Add(string package, OnResponse callback)
  98. {
  99. lock (taskQueue)
  100. {
  101. taskQueue.Enqueue(new TaskInfo() { taskType = TaskType.Add, onResponse = callback, param = package });
  102. }
  103. }
  104. public static void Remove(string package, OnResponse callback)
  105. {
  106. lock (taskQueue)
  107. {
  108. taskQueue.Enqueue(new TaskInfo() { taskType = TaskType.Remove, onResponse = callback, param = package });
  109. }
  110. }
  111. public static void GetAllPackages(OnResponse callback)
  112. {
  113. lock (taskQueue)
  114. {
  115. taskQueue.Enqueue(new TaskInfo() { taskType = TaskType.Get, onResponse = callback });
  116. }
  117. }
  118. public static Dictionary<string, string> GetAllPackagesByManifest()
  119. {
  120. string path = Path.Combine(Directory.GetParent(Application.dataPath).FullName, "Packages/manifest.json");
  121. var json = JsonMapper.ToObject(File.ReadAllText(path));
  122. var packages = json["dependencies"];
  123. Dictionary<string, string> dict = new Dictionary<string, string>();
  124. foreach (var item in packages.Keys)
  125. {
  126. dict.Add(item, packages[item].ToString());
  127. }
  128. return dict;
  129. }
  130. }
  131. }