PackageCompareWindow.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.IO;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace YooAsset.Editor
  7. {
  8. public class PackageCompareWindow : EditorWindow
  9. {
  10. static PackageCompareWindow _thisInstance;
  11. [MenuItem("YooAsset/补丁包比对工具", false, 302)]
  12. static void ShowWindow()
  13. {
  14. if (_thisInstance == null)
  15. {
  16. _thisInstance = EditorWindow.GetWindow(typeof(PackageCompareWindow), false, "补丁包比对工具", true) as PackageCompareWindow;
  17. _thisInstance.minSize = new Vector2(800, 600);
  18. }
  19. _thisInstance.Show();
  20. }
  21. private string _manifestPath1 = string.Empty;
  22. private string _manifestPath2 = string.Empty;
  23. private readonly List<PackageBundle> _changeList = new List<PackageBundle>();
  24. private readonly List<PackageBundle> _newList = new List<PackageBundle>();
  25. private Vector2 _scrollPos1;
  26. private Vector2 _scrollPos2;
  27. private void OnGUI()
  28. {
  29. GUILayout.Space(10);
  30. EditorGUILayout.BeginHorizontal();
  31. if (GUILayout.Button("选择补丁包1", GUILayout.MaxWidth(150)))
  32. {
  33. string resultPath = EditorUtility.OpenFilePanel("Find", "Assets/", "bytes");
  34. if (string.IsNullOrEmpty(resultPath))
  35. return;
  36. _manifestPath1 = resultPath;
  37. }
  38. EditorGUILayout.LabelField(_manifestPath1);
  39. EditorGUILayout.EndHorizontal();
  40. GUILayout.Space(10);
  41. EditorGUILayout.BeginHorizontal();
  42. if (GUILayout.Button("选择补丁包2", GUILayout.MaxWidth(150)))
  43. {
  44. string resultPath = EditorUtility.OpenFilePanel("Find", "Assets/", "bytes");
  45. if (string.IsNullOrEmpty(resultPath))
  46. return;
  47. _manifestPath2 = resultPath;
  48. }
  49. EditorGUILayout.LabelField(_manifestPath2);
  50. EditorGUILayout.EndHorizontal();
  51. if (string.IsNullOrEmpty(_manifestPath1) == false && string.IsNullOrEmpty(_manifestPath2) == false)
  52. {
  53. if (GUILayout.Button("比对差异", GUILayout.MaxWidth(150)))
  54. {
  55. ComparePackage(_changeList, _newList);
  56. }
  57. }
  58. EditorGUILayout.Space();
  59. using (new EditorGUI.DisabledScope(false))
  60. {
  61. int totalCount = _changeList.Count;
  62. EditorGUILayout.Foldout(true, $"差异列表 ( {totalCount} )");
  63. EditorGUI.indentLevel = 1;
  64. _scrollPos1 = EditorGUILayout.BeginScrollView(_scrollPos1);
  65. {
  66. foreach (var bundle in _changeList)
  67. {
  68. EditorGUILayout.LabelField($"{bundle.BundleName} | {(bundle.FileSize / 1024)}K");
  69. }
  70. }
  71. EditorGUILayout.EndScrollView();
  72. EditorGUI.indentLevel = 0;
  73. }
  74. EditorGUILayout.Space();
  75. using (new EditorGUI.DisabledScope(false))
  76. {
  77. int totalCount = _newList.Count;
  78. EditorGUILayout.Foldout(true, $"新增列表 ( {totalCount} )");
  79. EditorGUI.indentLevel = 1;
  80. _scrollPos2 = EditorGUILayout.BeginScrollView(_scrollPos2);
  81. {
  82. foreach (var bundle in _newList)
  83. {
  84. EditorGUILayout.LabelField($"{bundle.BundleName}");
  85. }
  86. }
  87. EditorGUILayout.EndScrollView();
  88. EditorGUI.indentLevel = 0;
  89. }
  90. }
  91. private void ComparePackage(List<PackageBundle> changeList, List<PackageBundle> newList)
  92. {
  93. changeList.Clear();
  94. newList.Clear();
  95. // 加载补丁清单1
  96. byte[] bytesData1 = FileUtility.ReadAllBytes(_manifestPath1);
  97. PackageManifest manifest1 = ManifestTools.DeserializeFromBinary(bytesData1);
  98. // 加载补丁清单1
  99. byte[] bytesData2 = FileUtility.ReadAllBytes(_manifestPath2);
  100. PackageManifest manifest2 = ManifestTools.DeserializeFromBinary(bytesData2);
  101. // 拷贝文件列表
  102. foreach (var bundle2 in manifest2.BundleList)
  103. {
  104. if (manifest1.TryGetPackageBundleByBundleName(bundle2.BundleName, out PackageBundle bundle1))
  105. {
  106. if (bundle2.FileHash != bundle1.FileHash)
  107. {
  108. changeList.Add(bundle2);
  109. }
  110. }
  111. else
  112. {
  113. newList.Add(bundle2);
  114. }
  115. }
  116. // 按字母重新排序
  117. changeList.Sort((x, y) => string.Compare(x.BundleName, y.BundleName));
  118. newList.Sort((x, y) => string.Compare(x.BundleName, y.BundleName));
  119. Debug.Log("资源包差异比对完成!");
  120. }
  121. }
  122. }