SDKLoader.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.IO;
  3. using System.Net.Http;
  4. using System.Threading.Tasks;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace com.JamesMobile.editor
  8. {
  9. /**
  10. *
  11. */
  12. public class SDKLoader : EditorWindow
  13. {
  14. public static string localUPVersion { private set; get; } = EditorConstants.VERSION;
  15. public static string lastestUPVersion { private set; get; } = "?.?.?";
  16. public static string localAARVersion { private set; get; } = "?.?.?";
  17. public static string localFrameworkVersion { private set; get; } = "?.?.?";
  18. public static SDKLoader Instance { private set; get; }
  19. private static HttpClient httpCli = new HttpClient();
  20. [MenuItem("UniMob/打开面板")]
  21. public static void Init()
  22. {
  23. if (Instance == null) Instance = GetWindow<SDKLoader>();
  24. Instance.Show();
  25. Instance.Focus();
  26. // 设置标题
  27. Instance.titleContent = new GUIContent("UniMob Manager");
  28. // 检查更新
  29. Instance.CheckVersion();
  30. }
  31. void OnGUI()
  32. {
  33. // UniMob 版本升级
  34. GUILayout.Space(20f);
  35. GUILayout.BeginHorizontal();
  36. if (GUILayout.Button("刷新")) CheckVersion();
  37. if(!CanUpdate()) GUI.enabled = false;
  38. if (GUILayout.Button("更新")) UpdateVersion();
  39. if (!CanUpdate()) GUI.enabled = true;
  40. //
  41. GUILayout.EndHorizontal();
  42. GUILayout.Space(5f);
  43. GUILayout.Label($"本地版本: {localUPVersion}");
  44. GUILayout.Label($"最新版本: {lastestUPVersion}");
  45. GUILayout.Space(10f);
  46. GUILayout.Label($"Android SDK 版本: {localAARVersion}");
  47. GUILayout.Label($"iOS SDK 版本: {localFrameworkVersion}");
  48. GUILayout.Space(30f);
  49. // proxy port
  50. GUILayout.BeginHorizontal();
  51. EditorUtils.ProxyPort = EditorGUILayout.IntField("Proxy Port", EditorUtils.ProxyPort, GUILayout.Width(500f));
  52. GUILayout.FlexibleSpace();
  53. GUILayout.EndHorizontal();
  54. // Build Options
  55. AndroidBuildOptionsRenderer.OnGUI();
  56. //
  57. // GUILayout.Label("iOS Options:");
  58. // GUILayout.Space(10f);
  59. // if (GUILayout.Button("Export Xcode Project")) Debug.LogError("To be continue");
  60. }
  61. void OnDestroy()
  62. {
  63. ClearCacheFiles();
  64. }
  65. //
  66. public static string GetUnityPackageByVersion(string shortName, string version) => $"unimob-v{version}-{shortName}.unitypackage";
  67. public void CheckVersion() {
  68. Debug.Log("CheckVersion");
  69. // Request lastest version
  70. var shortName = GetShortNameByAppId(Application.identifier);
  71. RefreshVersionAsync(shortName);
  72. // Get local version
  73. // aar
  74. var targetVer = EditorUtils.GetAARVersion();
  75. localAARVersion = string.IsNullOrEmpty(targetVer) ? "?.?.?" : targetVer;
  76. // framework
  77. targetVer = EditorUtils.GetFrameworkVersion();
  78. localFrameworkVersion = string.IsNullOrEmpty(targetVer) ? "?.?.?" : targetVer;
  79. }
  80. public bool CanUpdate() => EditorUtils.Version2Code(lastestUPVersion) > EditorUtils.Version2Code(localUPVersion);
  81. public void UpdateVersion() {
  82. if (! CanUpdate()) return;
  83. DownloadUnityPackage(lastestUPVersion, (destPath) => {
  84. // rm aar and framework
  85. EditorUtils.CleanAARs();
  86. EditorUtils.CleanFramework();
  87. AssetDatabase.Refresh();
  88. // update
  89. AssetDatabase.ImportPackage(destPath, true);
  90. FileUtil.DeleteFileOrDirectory(destPath);
  91. AssetDatabase.Refresh();
  92. CheckVersion();
  93. });
  94. }
  95. private static void ClearCacheFiles()
  96. {
  97. var files = Directory.GetFiles(Application.temporaryCachePath, "unimob-v*", SearchOption.TopDirectoryOnly);
  98. foreach (var f in files) FileUtil.DeleteFileOrDirectory(f);
  99. }
  100. public static async void DownloadUnityPackage(string version, Action<string> onSuccess)
  101. {
  102. var unitypackageFileName = GetUnityPackageByVersion(GetShortNameByAppId(Application.identifier), version);
  103. var cachefilePath = await DownloadSDK(unitypackageFileName);
  104. Debug.Log("Download Complete: " + cachefilePath);
  105. onSuccess.Invoke(cachefilePath);
  106. }
  107. private static string GetShortNameByAppId(string appId)
  108. {
  109. foreach (var appinfo in EditorConstants.APPS_INFOS)
  110. {
  111. #if UNITY_ANDROID
  112. if (appinfo.androidAppId == appId) return appinfo.shortName;
  113. #elif UNITY_IOS
  114. if (appinfo.iosAppId == appId) return appinfo.shortName;
  115. #endif
  116. }
  117. return null;
  118. }
  119. private static async void RefreshVersionAsync(string shortname) {
  120. var url = "http://bellosvn.com/unimob/version?shortname=" + shortname;
  121. var response = await httpCli.GetStringAsync(url);
  122. var res = JsonUtility.FromJson<SDKVersion>(response);
  123. lastestUPVersion = string.IsNullOrEmpty(res.data) ? "?.?.?" : res.data.Split('-')[1].Substring(1);
  124. }
  125. private static async Task<string> DownloadSDK(string sdkFileName) {
  126. var url = "http://bellosvn.com/unimobsdk/" + sdkFileName;
  127. var destPath = Path.Combine(Application.temporaryCachePath, sdkFileName);
  128. var res = await httpCli.GetAsync(url);
  129. res.EnsureSuccessStatusCode();
  130. using (var ms = await res.Content.ReadAsStreamAsync()) {
  131. using (var fs = File.Create(destPath)) {
  132. ms.Seek(0, SeekOrigin.Begin);
  133. ms.CopyTo(fs);
  134. }
  135. }
  136. return destPath;
  137. }
  138. }
  139. public struct SDKVersion {
  140. public string error;
  141. public string data;
  142. }
  143. }