123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.IO;
- using System.Net.Http;
- using System.Threading.Tasks;
- using UnityEditor;
- using UnityEngine;
- namespace com.JamesMobile.editor
- {
- /**
- *
- */
- public class SDKLoader : EditorWindow
- {
- public static string localUPVersion { private set; get; } = EditorConstants.VERSION;
- public static string lastestUPVersion { private set; get; } = "?.?.?";
- public static string localAARVersion { private set; get; } = "?.?.?";
- public static string localFrameworkVersion { private set; get; } = "?.?.?";
- public static SDKLoader Instance { private set; get; }
- private static HttpClient httpCli = new HttpClient();
- [MenuItem("UniMob/打开面板")]
- public static void Init()
- {
- if (Instance == null) Instance = GetWindow<SDKLoader>();
- Instance.Show();
- Instance.Focus();
- // 设置标题
- Instance.titleContent = new GUIContent("UniMob Manager");
- // 检查更新
- Instance.CheckVersion();
- }
- void OnGUI()
- {
- // UniMob 版本升级
- GUILayout.Space(20f);
- GUILayout.BeginHorizontal();
- if (GUILayout.Button("刷新")) CheckVersion();
- if(!CanUpdate()) GUI.enabled = false;
- if (GUILayout.Button("更新")) UpdateVersion();
- if (!CanUpdate()) GUI.enabled = true;
- //
- GUILayout.EndHorizontal();
- GUILayout.Space(5f);
- GUILayout.Label($"本地版本: {localUPVersion}");
- GUILayout.Label($"最新版本: {lastestUPVersion}");
- GUILayout.Space(10f);
- GUILayout.Label($"Android SDK 版本: {localAARVersion}");
- GUILayout.Label($"iOS SDK 版本: {localFrameworkVersion}");
- GUILayout.Space(30f);
- // proxy port
- GUILayout.BeginHorizontal();
- EditorUtils.ProxyPort = EditorGUILayout.IntField("Proxy Port", EditorUtils.ProxyPort, GUILayout.Width(500f));
- GUILayout.FlexibleSpace();
- GUILayout.EndHorizontal();
- // Build Options
- AndroidBuildOptionsRenderer.OnGUI();
- //
- // GUILayout.Label("iOS Options:");
- // GUILayout.Space(10f);
- // if (GUILayout.Button("Export Xcode Project")) Debug.LogError("To be continue");
- }
- void OnDestroy()
- {
- ClearCacheFiles();
- }
- //
- public static string GetUnityPackageByVersion(string shortName, string version) => $"unimob-v{version}-{shortName}.unitypackage";
- public void CheckVersion() {
- Debug.Log("CheckVersion");
- // Request lastest version
- var shortName = GetShortNameByAppId(Application.identifier);
- RefreshVersionAsync(shortName);
- // Get local version
- // aar
- var targetVer = EditorUtils.GetAARVersion();
- localAARVersion = string.IsNullOrEmpty(targetVer) ? "?.?.?" : targetVer;
- // framework
- targetVer = EditorUtils.GetFrameworkVersion();
- localFrameworkVersion = string.IsNullOrEmpty(targetVer) ? "?.?.?" : targetVer;
- }
- public bool CanUpdate() => EditorUtils.Version2Code(lastestUPVersion) > EditorUtils.Version2Code(localUPVersion);
- public void UpdateVersion() {
- if (! CanUpdate()) return;
- DownloadUnityPackage(lastestUPVersion, (destPath) => {
- // rm aar and framework
- EditorUtils.CleanAARs();
- EditorUtils.CleanFramework();
- AssetDatabase.Refresh();
- // update
- AssetDatabase.ImportPackage(destPath, true);
- FileUtil.DeleteFileOrDirectory(destPath);
- AssetDatabase.Refresh();
- CheckVersion();
- });
- }
- private static void ClearCacheFiles()
- {
- var files = Directory.GetFiles(Application.temporaryCachePath, "unimob-v*", SearchOption.TopDirectoryOnly);
- foreach (var f in files) FileUtil.DeleteFileOrDirectory(f);
- }
- public static async void DownloadUnityPackage(string version, Action<string> onSuccess)
- {
- var unitypackageFileName = GetUnityPackageByVersion(GetShortNameByAppId(Application.identifier), version);
- var cachefilePath = await DownloadSDK(unitypackageFileName);
- Debug.Log("Download Complete: " + cachefilePath);
- onSuccess.Invoke(cachefilePath);
- }
- private static string GetShortNameByAppId(string appId)
- {
- foreach (var appinfo in EditorConstants.APPS_INFOS)
- {
- #if UNITY_ANDROID
- if (appinfo.androidAppId == appId) return appinfo.shortName;
- #elif UNITY_IOS
- if (appinfo.iosAppId == appId) return appinfo.shortName;
- #endif
- }
- return null;
- }
- private static async void RefreshVersionAsync(string shortname) {
- var url = "http://bellosvn.com/unimob/version?shortname=" + shortname;
- var response = await httpCli.GetStringAsync(url);
- var res = JsonUtility.FromJson<SDKVersion>(response);
- lastestUPVersion = string.IsNullOrEmpty(res.data) ? "?.?.?" : res.data.Split('-')[1].Substring(1);
- }
- private static async Task<string> DownloadSDK(string sdkFileName) {
- var url = "http://bellosvn.com/unimobsdk/" + sdkFileName;
- var destPath = Path.Combine(Application.temporaryCachePath, sdkFileName);
- var res = await httpCli.GetAsync(url);
- res.EnsureSuccessStatusCode();
- using (var ms = await res.Content.ReadAsStreamAsync()) {
- using (var fs = File.Create(destPath)) {
- ms.Seek(0, SeekOrigin.Begin);
- ms.CopyTo(fs);
- }
- }
- return destPath;
- }
-
- }
- public struct SDKVersion {
- public string error;
- public string data;
- }
- }
|