|
|
@@ -0,0 +1,202 @@
|
|
|
+using System;
|
|
|
+using System.IO;
|
|
|
+using System.Text;
|
|
|
+using Fort23.Core;
|
|
|
+using Fort23.GameData;
|
|
|
+using Fort23.UTool;
|
|
|
+using hirdParty.DownloadSystem;
|
|
|
+using ThirdParty;
|
|
|
+using ThirdParty.DownloadSystem;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.Networking;
|
|
|
+
|
|
|
+public class VersionChecker
|
|
|
+{
|
|
|
+ private string versionUrl;
|
|
|
+ string savePath;
|
|
|
+ private AssetMD5Info _assetMD5Info;
|
|
|
+ private VersionInfo versionInfo = null;
|
|
|
+
|
|
|
+
|
|
|
+ public async CTask Start()
|
|
|
+ {
|
|
|
+ versionUrl = $"{HotSyncContent.AssetURL}/{versionInfo.apkVersion}/version.json";
|
|
|
+ savePath = Path.Combine(Application.persistentDataPath, versionInfo.apkVersion + "update.apk");
|
|
|
+
|
|
|
+ versionInfo = await GetRemoteVersion();
|
|
|
+ string local = Application.version;
|
|
|
+
|
|
|
+ if (versionInfo != null && versionInfo.apkVersion != local)
|
|
|
+ {
|
|
|
+ await GetMd5();
|
|
|
+ if (!await Cheek())
|
|
|
+ {
|
|
|
+ Debug.Log("检测到新版本,开始下载...");
|
|
|
+ await DownloadAndInstall();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private async CTask GetMd5()
|
|
|
+ {
|
|
|
+ CTask cTask = CTask.Create();
|
|
|
+ DownloadFileData downloadFileData2 = new DownloadFileData();
|
|
|
+ downloadFileData2.remoteUrl = $"{HotSyncContent.AssetURL}/{versionInfo.apkVersion}/MD5.txt";
|
|
|
+ downloadFileData2.maxCount = int.MaxValue;
|
|
|
+ downloadFileData2.timeOut = 10;
|
|
|
+ DownloadHander downloadHander2 = FileDownloadSystem.Instance.DownloadFile(downloadFileData2);
|
|
|
+ DownLoadHanderGroup downLoadHanderGroup = new DownLoadHanderGroup();
|
|
|
+ downLoadHanderGroup.AddHander(downloadHander2);
|
|
|
+ downLoadHanderGroup.OnCallBack = async delegate
|
|
|
+ {
|
|
|
+ if (downloadHander2.HttpDownloadBasic.DownloadHander.HttpDownloadBasic.Result() !=
|
|
|
+ UnityWebRequest.Result.Success)
|
|
|
+ {
|
|
|
+ LogTool.Warning("获取MD5失败,尝试重新获取");
|
|
|
+ await GetMd5();
|
|
|
+ cTask.SetResult();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ string json = UTF8Encoding.UTF8.GetString(downloadHander2.Data);
|
|
|
+ _assetMD5Info = JsonHelper.FromJson<AssetMD5Info>(json);
|
|
|
+ cTask.SetResult();
|
|
|
+ };
|
|
|
+
|
|
|
+ downLoadHanderGroup.StartUpdate();
|
|
|
+ await cTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ private CTask<bool> Cheek()
|
|
|
+ {
|
|
|
+ CTask<bool> cheekTask = CTask<bool>.Create();
|
|
|
+ CheckFilePool _checkFileThrans = new CheckFilePool();
|
|
|
+ _checkFileThrans.isStreamingAssetsPath = false;
|
|
|
+ _checkFileThrans.streamingAssetsPath = Application.persistentDataPath;
|
|
|
+ _checkFileThrans.Start(Application.persistentDataPath,
|
|
|
+ _assetMD5Info.fileInfo);
|
|
|
+ _checkFileThrans.OnFinish = async delegate
|
|
|
+ {
|
|
|
+ //失败
|
|
|
+ if (_checkFileThrans.shiBaiFile.Count > 0)
|
|
|
+ {
|
|
|
+ cheekTask.SetResult(false);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ cheekTask.SetResult(true);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return cheekTask;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private async CTask<VersionInfo> GetRemoteVersion()
|
|
|
+ {
|
|
|
+ UnityWebRequest req = UnityWebRequest.Get(versionUrl);
|
|
|
+ var s = req.SendWebRequest();
|
|
|
+ CTask cTask = CTask.Create();
|
|
|
+ s.completed += operation => cTask.SetResult();
|
|
|
+ await cTask;
|
|
|
+
|
|
|
+ if (req.result != UnityWebRequest.Result.Success)
|
|
|
+ {
|
|
|
+ LogTool.Error("版本号获取失败:" + req.error);
|
|
|
+ return await GetRemoteVersion();
|
|
|
+ }
|
|
|
+
|
|
|
+ var json = req.downloadHandler.text;
|
|
|
+ var data = JsonUtility.FromJson<VersionInfo>(json);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async CTask DownloadAndInstall()
|
|
|
+ {
|
|
|
+ CTask cTask = CTask.Create();
|
|
|
+
|
|
|
+ Debug.Log("APK 下载路径:" + savePath);
|
|
|
+
|
|
|
+
|
|
|
+ DownLoadHanderGroup downLoadHanderGroup = new DownLoadHanderGroup();
|
|
|
+ DownloadFileData downloadFileData = new DownloadFileData();
|
|
|
+ downloadFileData.remoteUrl = versionInfo.apkUrl;
|
|
|
+ Debug.Log("下载文件" + downloadFileData.remoteUrl);
|
|
|
+ downloadFileData.localPath = savePath;
|
|
|
+ DownloadHander fileDow = FileDownloadSystem.Instance.DownloadFile(downloadFileData);
|
|
|
+
|
|
|
+ downLoadHanderGroup.AddHander(fileDow);
|
|
|
+
|
|
|
+ float m = downLoadHanderGroup.size / 1024f / 1024f;
|
|
|
+ float speed = fileDow.UnityWebRequestAsyncOperation.webRequest.downloadedBytes / 1024f;
|
|
|
+ LogTool.Log(
|
|
|
+ $"Downloading: {m.ToString("0.00")}M / {_assetMD5Info.fileInfo[0].size.ToString("0.00")}M speed {HotSyncContent.FormatSpeed(speed)}");
|
|
|
+
|
|
|
+ downLoadHanderGroup.OnCallBack = async () =>
|
|
|
+ {
|
|
|
+ if (fileDow.HttpDownloadBasic.DownloadHander.HttpDownloadBasic.Result() != UnityWebRequest.Result.Success)
|
|
|
+ {
|
|
|
+ await DownloadAndInstall();
|
|
|
+ cTask.SetResult();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Debug.Log("APK 下载成功");
|
|
|
+ if (!await Cheek())
|
|
|
+ {
|
|
|
+ LogTool.Warning("md5对比失败,重新下载");
|
|
|
+ await DownloadAndInstall();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ InstallAPK(savePath);
|
|
|
+ cTask.SetResult();
|
|
|
+ };
|
|
|
+ downLoadHanderGroup.StartUpdate();
|
|
|
+ await cTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ static void InstallAPK(string apkPath)
|
|
|
+ {
|
|
|
+#if UNITY_ANDROID
|
|
|
+ try
|
|
|
+ {
|
|
|
+ AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
|
|
+ AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
|
|
+
|
|
|
+ AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
|
|
|
+ AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
|
|
|
+
|
|
|
+ string ACTION_VIEW = intentClass.GetStatic<string>("ACTION_VIEW");
|
|
|
+ intent.Call<AndroidJavaObject>("setAction", ACTION_VIEW);
|
|
|
+
|
|
|
+ AndroidJavaClass fileClass = new AndroidJavaClass("java.io.File");
|
|
|
+ AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath);
|
|
|
+
|
|
|
+ AndroidJavaClass uriClass = new AndroidJavaClass("androidx.core.content.FileProvider");
|
|
|
+ AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
|
|
|
+ AndroidJavaObject uri = uriClass.CallStatic<AndroidJavaObject>("getUriForFile",
|
|
|
+ context,
|
|
|
+ "com.yourgame.fileprovider",
|
|
|
+ fileObj);
|
|
|
+
|
|
|
+ intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive");
|
|
|
+ intent.Call<AndroidJavaObject>("addFlags", 1 << 25); // FLAG_GRANT_READ_URI_PERMISSION
|
|
|
+ intent.Call<AndroidJavaObject>("addFlags", 1 << 31); // FLAG_ACTIVITY_NEW_TASK
|
|
|
+
|
|
|
+ currentActivity.Call("startActivity", intent);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Debug.LogError("安装 APK 出错:" + ex.Message);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+[Serializable]
|
|
|
+public class VersionInfo
|
|
|
+{
|
|
|
+ public string apkVersion;
|
|
|
+ public string apkUrl;
|
|
|
+}
|