瀏覽代碼

修改bug

lzx 1 天之前
父節點
當前提交
125030a492

+ 23 - 7
Assets/Scripts/Core/Editor/BuildEtitor/BuildEditor.cs

@@ -10,6 +10,7 @@ using ICSharpCode.SharpZipLib.Checksum;
 using ICSharpCode.SharpZipLib.Zip;
 using ICSharpCode.SharpZipLib.Zip;
 using UnityEditor;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine;
+using UnityFS;
 
 
 namespace Fort23.Editor
 namespace Fort23.Editor
 {
 {
@@ -133,10 +134,9 @@ namespace Fort23.Editor
         private static void Build(PlatformType type, BuildAssetBundleOptions buildAssetBundleOptions,
         private static void Build(PlatformType type, BuildAssetBundleOptions buildAssetBundleOptions,
             BuildOptions buildOptions, bool isBuildExe, bool isContainAB, bool clearFolder)
             BuildOptions buildOptions, bool isBuildExe, bool isContainAB, bool clearFolder)
         {
         {
-           
             AssetDatabase.Refresh();
             AssetDatabase.Refresh();
             var buildTarget = BuildTarget.StandaloneWindows;
             var buildTarget = BuildTarget.StandaloneWindows;
-            var exeName = "release";
+            var exeName = Application.version;
             switch (type)
             switch (type)
             {
             {
                 case PlatformType.PC:
                 case PlatformType.PC:
@@ -158,7 +158,7 @@ namespace Fort23.Editor
                     break;
                     break;
             }
             }
 
 
-         
+
             var build = string.Format(buildFolder, type,
             var build = string.Format(buildFolder, type,
                 $"{1}.{1}.{1}");
                 $"{1}.{1}.{1}");
             string bundlePath = string.Format(bundleFolder, type);
             string bundlePath = string.Format(bundleFolder, type);
@@ -196,7 +196,7 @@ namespace Fort23.Editor
             AssetBundleEditor.BuildAllBundle(buildTarget);
             AssetBundleEditor.BuildAllBundle(buildTarget);
             LogTool.Log("资源标记完成");
             LogTool.Log("资源标记完成");
             AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
             AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
-            
+
             BuildPipeline.BuildAssetBundles(bundlePath, buildAssetBundleOptions, buildTarget);
             BuildPipeline.BuildAssetBundles(bundlePath, buildAssetBundleOptions, buildTarget);
             var assetMd5Info = new AssetMD5Info(new List<MD5FileInfo>());
             var assetMd5Info = new AssetMD5Info(new List<MD5FileInfo>());
             var allBundles = Directory.GetFiles(bundlePath);
             var allBundles = Directory.GetFiles(bundlePath);
@@ -208,7 +208,7 @@ namespace Fort23.Editor
                 string fileName = fileInfo.Name;
                 string fileName = fileInfo.Name;
                 if (fileInfo.Name.Equals("UnityBundle"))
                 if (fileInfo.Name.Equals("UnityBundle"))
                 {
                 {
-                    fileName="UnityBundle.unity3d";
+                    fileName = "UnityBundle.unity3d";
                 }
                 }
 
 
                 // data = DllTool.KeyEncryption(data);
                 // data = DllTool.KeyEncryption(data);
@@ -245,7 +245,6 @@ namespace Fort23.Editor
                 md5Info.size = fileInfo.Length;
                 md5Info.size = fileInfo.Length;
                 md5Info.fileName = Path.GetFileName(assetConfigPath);
                 md5Info.fileName = Path.GetFileName(assetConfigPath);
                 assetMd5Info.fileInfo.Add(md5Info);
                 assetMd5Info.fileInfo.Add(md5Info);
-                
             }
             }
 
 
             LogTool.Log("完成资源打包");
             LogTool.Log("完成资源打包");
@@ -277,11 +276,28 @@ namespace Fort23.Editor
 
 
             if (isBuildExe)
             if (isBuildExe)
             {
             {
+                string path = $"{relativeDirPrefix}/{Application.version}";
+                string apkPath = $"{path}/{exeName}";
                 AssetDatabase.Refresh();
                 AssetDatabase.Refresh();
                 LogTool.Log("开始EXE打包");
                 LogTool.Log("开始EXE打包");
                 SaveGroup();
                 SaveGroup();
-                BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, $"{relativeDirPrefix}/{exeName}", buildTarget, buildOptions);
+                BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, apkPath, buildTarget, buildOptions);
                 LogTool.Log("完成exe打包");
                 LogTool.Log("完成exe打包");
+                var assetMd5Info1 = new AssetMD5Info(new List<MD5FileInfo>());
+                var md5Info = new MD5FileInfo();
+                using (FileStream file = new FileStream(apkPath, FileMode.Open))
+                {
+                    md5Info.size = file.Length;
+                    var data = file.ReadAllBytes();
+                    md5Info.md5 = MD5Helper.FileMD5(data);
+                }
+
+                md5Info.fileName = Path.GetFileName(apkPath);
+                assetMd5Info1.fileInfo.Add(md5Info);
+
+                var md5Json1 = JsonHelper.ToJson(assetMd5Info1);
+                File.WriteAllText(path + "/MD5.txt", md5Json1);
+
                 string url = Application.dataPath.Split("Assets")[0] + relativeDirPrefix.Split("./")[1];
                 string url = Application.dataPath.Split("Assets")[0] + relativeDirPrefix.Split("./")[1];
                 Application.OpenURL(url);
                 Application.OpenURL(url);
             }
             }

+ 4 - 0
Assets/Scripts/GameUI/GameApplction.cs

@@ -56,6 +56,10 @@ public class GameApplction : IGameStart, ILogSend
     public async void StartGame(LoadType LoadType, string h5Url, GameStartUIPanel gameStartUIPanel,
     public async void StartGame(LoadType LoadType, string h5Url, GameStartUIPanel gameStartUIPanel,
         System.Action LoadFinish)
         System.Action LoadFinish)
     {
     {
+        VersionChecker versionChecker = new VersionChecker();
+        // await versionChecker.Start();
+        
+        
         Debug.Log("开始游戏逻辑");
         Debug.Log("开始游戏逻辑");
         HotSyncContent.DownloadUI = gameStartUIPanel;
         HotSyncContent.DownloadUI = gameStartUIPanel;
         this.LoadFinish = LoadFinish;
         this.LoadFinish = LoadFinish;

+ 202 - 0
Assets/Scripts/GameUI/VersionChecker.cs

@@ -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;
+}

+ 11 - 0
Assets/Scripts/GameUI/VersionChecker.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: CX0etX/5AigsqZI2j0FijXKKnnU33xgqolTNgYgbmuUBga78Dnfh6GOUuLQz
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

File diff suppressed because it is too large
+ 0 - 0
Assets/StreamingAssets/assetConfig.txt


Some files were not shown because too many files changed in this diff