ZibraLiquidUpdateChecker.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEngine.Networking;
  4. using UnityEngine.UIElements;
  5. using System;
  6. namespace com.zibra.common.Editor
  7. {
  8. internal class ZibraEffectsUpdateChecker : EditorWindow
  9. {
  10. public static GUIContent WindowTitle => new GUIContent("Zibra Effects Update Checker");
  11. #if !ZIBRA_EFFECTS_OTP_VERSION
  12. const string URL = "https://generation.zibra.ai/api/pluginVersion?effect=effects&engine=unity&sku=pro";
  13. #else
  14. const string URL = "https://generation.zibra.ai/api/pluginVersion?effect=effects&engine=unity&sku=otp";
  15. #endif
  16. private const string UPDATE_CHECK_PREFS_KEY = "ZibraEffectsAutomaticallyCheckForUpdates";
  17. private const string UPDATE_CHECKED_SESSION_STATE_KEY = "ZibraEffectsUpdateChecked";
  18. private static ZibraEffectsUpdateChecker InstanceInternal;
  19. private static ZibraEffectsUpdateChecker Instance
  20. {
  21. get {
  22. if (InstanceInternal == null)
  23. {
  24. InstanceInternal = CreateInstance<ZibraEffectsUpdateChecker>();
  25. }
  26. return InstanceInternal;
  27. }
  28. }
  29. private static UnityWebRequestAsyncOperation Request;
  30. private static Label LabelMessage;
  31. private static Label LabelPluginSKU;
  32. private static Button UpdateButton;
  33. private static Toggle Checkbox;
  34. private bool OnlyShowOutdated = false;
  35. private bool IsLatestVersion = false;
  36. [InitializeOnLoadMethod]
  37. internal static void InitializeOnLoad()
  38. {
  39. // Don't automatically open any windows in batch mode
  40. if (Application.isBatchMode)
  41. {
  42. return;
  43. }
  44. // Check whether user disabled auto update checking
  45. if (!EditorPrefs.GetBool(UPDATE_CHECK_PREFS_KEY, true))
  46. {
  47. return;
  48. }
  49. // Check whether auto update checking ran this editor session
  50. if (!SessionState.GetBool(UPDATE_CHECKED_SESSION_STATE_KEY, false))
  51. {
  52. SessionState.SetBool(UPDATE_CHECKED_SESSION_STATE_KEY, true);
  53. EditorApplication.update += ShowWindowDelayed;
  54. }
  55. }
  56. internal static void ShowWindowDelayed()
  57. {
  58. ShowWindow(true);
  59. EditorApplication.update -= ShowWindowDelayed;
  60. }
  61. [MenuItem(Effects.BaseMenuBarPath + "Check for Update", false, 9000)]
  62. public static void ShowWindowMenu()
  63. {
  64. ShowWindow(false);
  65. }
  66. public static void ShowWindow(bool onlyShowOutdated)
  67. {
  68. Instance.OnlyShowOutdated = onlyShowOutdated;
  69. if (!onlyShowOutdated)
  70. {
  71. Instance.Show();
  72. }
  73. }
  74. private void OnEnable()
  75. {
  76. titleContent = WindowTitle;
  77. var root = rootVisualElement;
  78. int width = 400;
  79. int height = 240;
  80. minSize = maxSize = new Vector2(width, height);
  81. var uxmlAssetPath = AssetDatabase.GUIDToAssetPath("f1c391edc80cd254a81b3eea9e36b979");
  82. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlAssetPath);
  83. visualTree.CloneTree(root);
  84. var commonUSSAssetPath = AssetDatabase.GUIDToAssetPath("20c4b12a1544dac44b6c04777afe69db");
  85. var commonStyleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(commonUSSAssetPath);
  86. root.styleSheets.Add(commonStyleSheet);
  87. UpdateButton = root.Q<Button>("UpdateButton");
  88. UpdateButton.visible = false;
  89. LabelMessage = root.Q<Label>("Version");
  90. Checkbox = root.Q<Toggle>("Check");
  91. Checkbox.value = EditorPrefs.GetBool(UPDATE_CHECK_PREFS_KEY, true);
  92. Checkbox.RegisterValueChangedCallback(evt => EditorPrefs.SetBool(UPDATE_CHECK_PREFS_KEY, evt.newValue));
  93. RequestLatestVersion();
  94. }
  95. private void UpdatePluginPageClick()
  96. {
  97. EditorApplication.ExecuteMenuItem("Window/Package Manager");
  98. }
  99. private void RequestLatestVersion()
  100. {
  101. Request = UnityWebRequest.Get(URL).SendWebRequest();
  102. LabelMessage.text = "Please wait.";
  103. Request.completed += UpdateCheckVersionRequest;
  104. }
  105. private void UpdateCheckVersionRequest(AsyncOperation obj)
  106. {
  107. if (Request == null || !Request.isDone)
  108. {
  109. return;
  110. }
  111. if (Request.webRequest.result != UnityWebRequest.Result.Success)
  112. {
  113. string errorMessage = $"Update check failed: {Request.webRequest.error}";
  114. Debug.LogError(errorMessage);
  115. LabelMessage.text = errorMessage;
  116. if (OnlyShowOutdated)
  117. {
  118. DestroyImmediate(Instance);
  119. }
  120. return;
  121. }
  122. if (Request.webRequest.responseCode != 200)
  123. {
  124. string errorMessage =
  125. $"Update check failed: {Request.webRequest.responseCode} - {Request.webRequest.downloadHandler.text}";
  126. Debug.LogError(errorMessage);
  127. LabelMessage.text = errorMessage;
  128. if (OnlyShowOutdated)
  129. {
  130. DestroyImmediate(Instance);
  131. }
  132. return;
  133. }
  134. CheckVersion();
  135. }
  136. public void CheckVersion()
  137. {
  138. var pluginVersion = JsonUtility.FromJson<ZibraEffectsVersion>(Request.webRequest.downloadHandler.text);
  139. IsLatestVersion = CheckIsLatestVersion(pluginVersion.version, Effects.Version);
  140. #pragma warning disable 0162
  141. if (Effects.IsPreReleaseVersion)
  142. {
  143. LabelMessage.text = $"You have the Pre-release version: {Effects.Version}";
  144. if (OnlyShowOutdated)
  145. {
  146. DestroyImmediate(Instance);
  147. }
  148. }
  149. else if (IsLatestVersion)
  150. {
  151. LabelMessage.text = $"You have the latest version: {Effects.Version}";
  152. if (OnlyShowOutdated)
  153. {
  154. DestroyImmediate(Instance);
  155. }
  156. }
  157. else
  158. {
  159. LabelMessage.text =
  160. $"New version available. Please consider updating. Latest version: \"{pluginVersion.version}\". Current version: \"{Effects.Version}\"";
  161. UpdateButton.visible = true;
  162. UpdateButton.clicked += UpdatePluginPageClick;
  163. if (OnlyShowOutdated)
  164. {
  165. Show();
  166. }
  167. }
  168. #pragma warning restore 0162
  169. LabelMessage.style.marginLeft = 50;
  170. LabelMessage.style.marginRight = 50;
  171. LabelMessage.style.whiteSpace = WhiteSpace.Normal;
  172. LabelMessage.style.unityTextAlign = TextAnchor.MiddleCenter;
  173. }
  174. private bool CheckIsLatestVersion(string pluginVersion, string localVersion)
  175. {
  176. char[] separator = { '.' };
  177. int[] currentVersion = {};
  178. int[] serverVersion = {};
  179. try
  180. {
  181. string[] currentVersionStrArr = localVersion.Split(separator);
  182. currentVersion = new int[currentVersionStrArr.Length];
  183. for (int i = 0; i < currentVersionStrArr.Length; i++)
  184. {
  185. currentVersion[i] = int.Parse(currentVersionStrArr[i]);
  186. }
  187. string[] serverVersionStrArr = pluginVersion.Split(separator);
  188. serverVersion = new int[serverVersionStrArr.Length];
  189. for (int i = 0; i < serverVersionStrArr.Length; i++)
  190. {
  191. serverVersion[i] = int.Parse(serverVersionStrArr[i]);
  192. }
  193. }
  194. catch (Exception)
  195. {
  196. return true;
  197. }
  198. if (currentVersion.Length < serverVersion.Length)
  199. {
  200. int[] tempArr = currentVersion;
  201. currentVersion = new int[serverVersion.Length];
  202. Array.Copy(tempArr, currentVersion, tempArr.Length);
  203. }
  204. else if (currentVersion.Length > serverVersion.Length)
  205. {
  206. int[] tempArr = serverVersion;
  207. serverVersion = new int[currentVersion.Length];
  208. Array.Copy(tempArr, serverVersion, tempArr.Length);
  209. }
  210. for (int i = 0; i < Math.Min(currentVersion.Length, serverVersion.Length); i++)
  211. {
  212. if (currentVersion[i] < serverVersion[i])
  213. {
  214. return false;
  215. }
  216. else if (currentVersion[i] > serverVersion[i])
  217. {
  218. return true;
  219. }
  220. }
  221. return true;
  222. }
  223. private struct ZibraEffectsVersion
  224. {
  225. public string version;
  226. }
  227. }
  228. }