From 67dfdf94c61cc6d7838f299356f663b35b7e94d2 Mon Sep 17 00:00:00 2001 From: chenxi Date: Wed, 6 Sep 2023 15:28:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8C=85=E5=B7=A5=E5=85=B7=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AssetBundleWindow.meta | 8 + .../CompareAssetBundleWindow.cs | 195 ++++++++++++++++++ .../CompareAssetBundleWindow.cs.meta | 11 + .../Editor/BFBuildProjectTools/BuildMenu.cs | 18 ++ 4 files changed, 232 insertions(+) create mode 100644 Assets/Editor/BFBuildProjectTools/AssetBundleWindow.meta create mode 100644 Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs create mode 100644 Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs.meta diff --git a/Assets/Editor/BFBuildProjectTools/AssetBundleWindow.meta b/Assets/Editor/BFBuildProjectTools/AssetBundleWindow.meta new file mode 100644 index 000000000..3c7f56297 --- /dev/null +++ b/Assets/Editor/BFBuildProjectTools/AssetBundleWindow.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0c41a7273e266a0468c52d3a52652dbd +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs b/Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs new file mode 100644 index 000000000..ee356c7a6 --- /dev/null +++ b/Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs @@ -0,0 +1,195 @@ +using System.Collections.Generic; +using System.IO; +using UnityEditor; +using UnityEngine; + +namespace BFEditor.Build +{ + public class CompareAssetBundleWindow : EditorWindow + { + private string newestABPath = ""; + private string lastABPath = ""; + public bool IsAndroid = true; + private List ABList = new List(); + Vector2 resultScrollPos; + public CompareAssetBundleWindow() + { + titleContent = new GUIContent("AssetBundle对比"); + } + + private void OnEnable() + { + GetNewestABPath(); + } + + void OnGUI() + { + GUILayout.Space(18); + GUILayout.BeginHorizontal(); + GUILayout.Label("当前版本AB路径"); + GUILayout.TextField(newestABPath, GUILayout.Width(600)); + if (GUILayout.Button("选择", GUILayout.Width(80))) + { + string openPath = EditorUtility.OpenFolderPanel("select ab path", newestABPath, ""); + if (openPath.CompareTo("") != 0){ + newestABPath = openPath; + } + } + GUILayout.EndHorizontal(); + GUILayout.Space(10); + + GUILayout.BeginHorizontal(); + GUILayout.Label("上个版本AB路径"); + GUILayout.TextField(lastABPath, GUILayout.Width(600)); + if (GUILayout.Button("选择", GUILayout.Width(80))) + { + string openPath = EditorUtility.OpenFolderPanel("select ab path", lastABPath, ""); + if (openPath.CompareTo("") != 0){ + lastABPath = openPath; + } + } + GUILayout.EndHorizontal(); + GUILayout.Space(10); + + GUILayout.BeginHorizontal(); + GUILayout.Space(150); + if (GUILayout.Button("对比", GUILayout.Width(200), GUILayout.Height(40))) + { + CheckMD5AndAssetHash(); + } + + GUILayout.Space(100); + if (GUILayout.Button("使用上个版本的AB修复", GUILayout.Width(200), GUILayout.Height(40))) + { + UseLastABFix(); + } + GUILayout.EndHorizontal(); + GUILayout.Space(30); + + DrawResult(); + } + + private void DrawResult() + { + resultScrollPos = GUILayout.BeginScrollView(resultScrollPos, GUILayout.Width(800), GUILayout.Height(620)); + for (int i = ABList.Count - 1; i >= 0; i--) + { + var path = ABList[i]; + + GUILayout.BeginHorizontal(); + GUILayout.Space(20); + EditorGUILayout.TextField(path); + + GUILayout.Space(10); + + GUILayout.EndHorizontal(); + } + GUILayout.EndScrollView(); + } + + private void CheckMD5AndAssetHash() + { + if (string.IsNullOrEmpty(newestABPath)) + { + return; + } + if (string.IsNullOrEmpty(lastABPath)) + { + return; + } + ABList.Clear(); + var manifestPath = System.IO.Path.Combine(newestABPath, "asset_bundle_manifest.ab"); + var manifestAB = AssetBundle.LoadFromFile(manifestPath); + var Manifest = manifestAB.LoadAsset("AssetBundleManifest"); + manifestAB.Unload(false); + + var lastManifestPath = System.IO.Path.Combine(lastABPath, "asset_bundle_manifest.ab"); + var lastManifestAB = AssetBundle.LoadFromFile(lastManifestPath); + var lastManifest = lastManifestAB.LoadAsset("AssetBundleManifest"); + lastManifestAB.Unload(false); + + var allAB = Manifest.GetAllAssetBundles(); + foreach(var path in allAB) + { + if (path.Contains("ab_config.bytes") || path.Contains("asset_bundle_manifest.ab")) + { + continue; + } + var fullPathA = Path.Combine(newestABPath, path); + var fullPathB = Path.Combine(lastABPath, path); + if (File.Exists(fullPathB) && BF.GameLaunchUtils.GetFileMD5(fullPathA).CompareTo(BF.GameLaunchUtils.GetFileMD5(fullPathB)) != 0) + { + if (Manifest.GetAssetBundleHash(path) == lastManifest.GetAssetBundleHash(path)) + { + // md5 不一致但是AssetHash一致的情况 + ABList.Add(path); + } + } + } + } + + private void UseLastABFix() + { + if (string.IsNullOrEmpty(newestABPath)) + { + return; + } + if (string.IsNullOrEmpty(lastABPath)) + { + return; + } + if (ABList.Count == 0) + { + return; + } + foreach(var path in ABList) + { + var fullPathA = Path.Combine(newestABPath, path); + var fullPathB = Path.Combine(lastABPath, path); + File.Copy(fullPathB, fullPathA, true); + } + var version = newestABPath.Substring(newestABPath.Replace("\\", "/").LastIndexOf("/") + 1); + AssetBundleUtils.RegenerateABConfigMd5(newestABPath, version); + + ABList.Clear(); + } + + public static void ShowWindow() + { + var window = GetWindow(); + window.Show(); + } + + private void GetNewestABPath() + { + var bundleCachePath = IsAndroid ? Application.dataPath + "/../HistoryAssetBundles" : Application.dataPath + "/../HistoryAssetBundles/IOS"; + var dirInfo = new DirectoryInfo(bundleCachePath); + var dirs = dirInfo.GetDirectories(); + if (dirs.Length == 0) + { + newestABPath = ""; + lastABPath = ""; + } + if (dirs.Length == 1) + { + newestABPath = dirs[0].FullName; + lastABPath = ""; + } + var hightestIndex = 0; + var secondIndex = 0; + for (var i = 1; i < dirs.Length; i++) + { + if (dirs[i].FullName.CompareTo(dirs[hightestIndex].FullName) > 0) + { + secondIndex = hightestIndex; + hightestIndex = i; + } + } + newestABPath = dirs[hightestIndex].FullName; + if (hightestIndex > 0) + { + lastABPath = dirs[secondIndex].FullName; + } + } + } +} diff --git a/Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs.meta b/Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs.meta new file mode 100644 index 000000000..e62022d57 --- /dev/null +++ b/Assets/Editor/BFBuildProjectTools/AssetBundleWindow/CompareAssetBundleWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ea05efeb3467b0947b735c8fe281cb8c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/BFBuildProjectTools/BuildMenu.cs b/Assets/Editor/BFBuildProjectTools/BuildMenu.cs index c2bc79e78..fc0c8ecad 100644 --- a/Assets/Editor/BFBuildProjectTools/BuildMenu.cs +++ b/Assets/Editor/BFBuildProjectTools/BuildMenu.cs @@ -192,6 +192,24 @@ namespace BFEditor.Build } } + [MenuItem("打包工具/AssetBundles/对比上一个版本AB Android", priority = 207)] + static void CompareLastVersionABAndroid() + { + var window = (CompareAssetBundleWindow)EditorWindow.GetWindowWithRect(typeof(CompareAssetBundleWindow), + new Rect(Screen.width / 2, Screen.height / 2, 800, 800), true); + window.IsAndroid = true; + window.Show(); + } + + [MenuItem("打包工具/AssetBundles/对比上一个版本AB IOS", priority = 208)] + static void CompareLastVersionABIOS() + { + var window = (CompareAssetBundleWindow)EditorWindow.GetWindowWithRect(typeof(CompareAssetBundleWindow), + new Rect(Screen.width / 2, Screen.height / 2, 800, 800), true); + window.IsAndroid = false; + window.Show(); + } + [MenuItem("打包工具/打包窗口", priority = 301)] static void ShowBuildWindow() {