Updater.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Threading.Tasks;
  5. using UnityEditor;
  6. using UnityEditor.Callbacks;
  7. using UnityEditor.PackageManager;
  8. using UnityEditor.PackageManager.Requests;
  9. using UnityEngine;
  10. using PackageInfo = UnityEditor.PackageManager.PackageInfo;
  11. public class Updater : Editor
  12. {
  13. public class Package
  14. {
  15. public string version;
  16. }
  17. private const string PackageName = "com.popcron.gizmos";
  18. private const string PackageURL = "https://raw.githubusercontent.com/popcron/gizmos/master/package.json";
  19. private const string CanUpdateKey = "Popcron.Gizmos.CanUpdate";
  20. private const string CheckForUpdateText = "Popcron/Gizmos/Check for updates";
  21. private const string UpdateText = "Popcron/Gizmos/Update";
  22. private static async Task<bool> IsUpdateAvailable()
  23. {
  24. WebClient wc = new WebClient();
  25. string json = await wc.DownloadStringTaskAsync(PackageURL);
  26. string versionText = JsonUtility.FromJson<Package>(json).version;
  27. Version version = Version.Parse(versionText);
  28. Version currentVersion = await GetLocalVersion();
  29. if (currentVersion != null)
  30. {
  31. bool updateAvailable = currentVersion.CompareTo(version) < 0;
  32. return updateAvailable;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. private static async Task<Version> GetLocalVersion()
  40. {
  41. ListRequest listRequest = Client.List(true);
  42. while (!listRequest.IsCompleted)
  43. {
  44. await Task.Delay(1);
  45. }
  46. foreach (PackageInfo pack in listRequest.Result)
  47. {
  48. if (pack.name == PackageName)
  49. {
  50. if (pack.source == PackageSource.Local) continue;
  51. Version localVersion = Version.Parse(pack.version);
  52. return localVersion;
  53. }
  54. }
  55. return null;
  56. }
  57. [MenuItem(CheckForUpdateText, false, 0)]
  58. [DidReloadScripts]
  59. private static async void CheckForUpdates()
  60. {
  61. //check for updates
  62. bool canUpdate = await IsUpdateAvailable();
  63. EditorPrefs.SetBool(CanUpdateKey, canUpdate);
  64. }
  65. [MenuItem(UpdateText, false, 0)]
  66. public static void Update()
  67. {
  68. //get the manifest.json file
  69. string path = Application.dataPath;
  70. path = Directory.GetParent(path).FullName;
  71. path = Path.Combine(path, "Packages", "manifest.json");
  72. if (File.Exists(path))
  73. {
  74. string text = File.ReadAllText(path);
  75. int index = text.IndexOf("\"lock\"");
  76. int start = index + text.Substring(index).IndexOf("\"" + PackageName + "\"");
  77. int end = start + text.Substring(start).IndexOf("}") + 2;
  78. string entry = text.Substring(start, end - start);
  79. //doesnt end with a comma, so remove the comma at the beginning of this entry if it exists because its the last entry
  80. if (!entry.EndsWith(","))
  81. {
  82. if (text.Substring(start - 2).Contains(","))
  83. {
  84. //4 spaces for tabs and 3 for quote, comma and }
  85. int comma = (start - 7) + text.Substring(start - 7).IndexOf(",");
  86. text = text.Remove(comma, 1);
  87. }
  88. }
  89. text = text.Replace(entry, "");
  90. File.WriteAllText(path, text);
  91. AssetDatabase.Refresh();
  92. }
  93. }
  94. [MenuItem(UpdateText, true)]
  95. private static bool CanUpdate()
  96. {
  97. return EditorPrefs.GetBool(CanUpdateKey);
  98. }
  99. }