From 857eb1d0bb32bdaee7e7d22a88b1cb5a9d707cbf Mon Sep 17 00:00:00 2001
From: hevinci <hevinci@hotmail.com>
Date: Tue, 15 Mar 2022 22:30:57 +0800
Subject: [PATCH] Update AssetBundleDebugger

---
 ...Profiler.meta => AssetBundleDebugger.meta} |   0
 .../AssetBundleDebugger.uxml                  |   7 +
 .../AssetBundleDebugger.uxml.meta             |  10 +
 .../AssetBundleDebuggerWindow.cs              |  63 +++++
 .../AssetBundleDebuggerWindow.cs.meta}        |   0
 .../AssetBundleDebugger/VisualViewers.meta    |   8 +
 .../VisualViewers/AssetListDebuggerViewer.cs  | 237 ++++++++++++++++++
 .../AssetListDebuggerViewer.cs.meta           |  11 +
 .../AssetListDebuggerViewer.uxml              |  19 ++
 .../AssetListDebuggerViewer.uxml.meta         |  10 +
 .../AssetBundleProfilerWindow.cs              |  11 -
 11 files changed, 365 insertions(+), 11 deletions(-)
 rename Assets/YooAsset/Editor/{AssetBundleProfiler.meta => AssetBundleDebugger.meta} (100%)
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml.meta
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebuggerWindow.cs
 rename Assets/YooAsset/Editor/{AssetBundleProfiler/AssetBundleProfilerWindow.cs.meta => AssetBundleDebugger/AssetBundleDebuggerWindow.cs.meta} (100%)
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers.meta
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs.meta
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml
 create mode 100644 Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml.meta
 delete mode 100644 Assets/YooAsset/Editor/AssetBundleProfiler/AssetBundleProfilerWindow.cs

diff --git a/Assets/YooAsset/Editor/AssetBundleProfiler.meta b/Assets/YooAsset/Editor/AssetBundleDebugger.meta
similarity index 100%
rename from Assets/YooAsset/Editor/AssetBundleProfiler.meta
rename to Assets/YooAsset/Editor/AssetBundleDebugger.meta
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml b/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml
new file mode 100644
index 0000000..b0f3d16
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml
@@ -0,0 +1,7 @@
+<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
+    <uie:Toolbar name="Toolbar" style="display: flex;">
+        <uie:ToolbarButton text="Sample" display-tooltip-when-elided="true" name="SampleButton" style="width: 70px; background-color: rgb(20, 134, 171); -unity-text-align: middle-center;" />
+        <uie:ToolbarMenu display-tooltip-when-elided="true" name="ShowModeMenu" text="ShowMode" style="width: 100px; flex-grow: 0;" />
+        <uie:ToolbarSearchField focusable="true" name="rSearchField" style="flex-grow: 1;" />
+    </uie:Toolbar>
+</ui:UXML>
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml.meta b/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml.meta
new file mode 100644
index 0000000..599022d
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml.meta
@@ -0,0 +1,10 @@
+fileFormatVersion: 2
+guid: 790db12999afd334e8fb6ba70ef0a947
+ScriptedImporter:
+  internalIDToNameTable: []
+  externalObjects: {}
+  serializedVersion: 2
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
+  script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebuggerWindow.cs b/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebuggerWindow.cs
new file mode 100644
index 0000000..b273163
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebuggerWindow.cs
@@ -0,0 +1,63 @@
+using UnityEditor;
+using UnityEngine;
+using UnityEngine.UIElements;
+using UnityEditor.UIElements;
+
+namespace YooAsset.Editor
+{
+    public class AssetBundleDebuggerWindow : EditorWindow
+    {
+        [MenuItem("YooAsset/AssetBundle Debugger", false, 104)]
+        public static void ShowExample()
+        {
+            AssetBundleDebuggerWindow wnd = GetWindow<AssetBundleDebuggerWindow>();
+            wnd.titleContent = new GUIContent("资源包调试工具");
+            wnd.minSize = new Vector2(800, 600);
+        }
+
+        private AssetListDebuggerViewer _assetListViewer;
+        private readonly DebugSummy _summy = new DebugSummy();
+        private string _searchKeyWord;
+
+
+        public void CreateGUI()
+        {
+            VisualElement root = rootVisualElement;
+
+            // 加载布局文件
+            string uxml = "Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebugger.uxml";
+            var visualAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxml);
+            if (visualAsset == null)
+            {
+                Debug.LogError($"Not found AssetBundleDebugger.uxml : {uxml}");
+                return;
+            }
+            visualAsset.CloneTree(root);
+
+            // 采样按钮
+			var sampleBtn = root.Q<Button>("SampleButton");
+            sampleBtn.clicked += SampleBtn_onClick;
+
+            // 搜索栏
+            var searchField = root.Q<ToolbarPopupSearchField>("SearchField");
+            searchField.RegisterValueChangedCallback(OnSearchKeyWordChange);
+
+            // 加载页面
+            _assetListViewer = new AssetListDebuggerViewer();
+            _assetListViewer.InitViewer();
+
+            // 初始页面
+            _assetListViewer.AttachParent(root);
+        }
+        private void SampleBtn_onClick()
+        {
+            YooAssets.GetDebugSummy(_summy);
+            _assetListViewer.FillViewData(_summy, _searchKeyWord);
+        }
+        private void OnSearchKeyWordChange(ChangeEvent<string> e)
+        {
+            _searchKeyWord = e.newValue;
+            _assetListViewer.FillViewData(_summy, _searchKeyWord);
+        }
+    }
+}
\ No newline at end of file
diff --git a/Assets/YooAsset/Editor/AssetBundleProfiler/AssetBundleProfilerWindow.cs.meta b/Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebuggerWindow.cs.meta
similarity index 100%
rename from Assets/YooAsset/Editor/AssetBundleProfiler/AssetBundleProfilerWindow.cs.meta
rename to Assets/YooAsset/Editor/AssetBundleDebugger/AssetBundleDebuggerWindow.cs.meta
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers.meta b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers.meta
new file mode 100644
index 0000000..ba52124
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: a034acd0035503742bc8d24b3c4068c2
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs
new file mode 100644
index 0000000..836a6b2
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs
@@ -0,0 +1,237 @@
+#if UNITY_2019_4_OR_NEWER
+using System.IO;
+using System.Linq;
+using System.Collections.Generic;
+using UnityEditor;
+using UnityEngine;
+using UnityEditor.UIElements;
+using UnityEngine.UIElements;
+
+namespace YooAsset.Editor
+{
+	internal class AssetListDebuggerViewer
+	{
+		private VisualTreeAsset _visualAsset;
+		private TemplateContainer _root;
+
+		private ListView _assetListView;
+		private ListView _dependListView;
+		private DebugSummy _summy;
+
+		/// <summary>
+		/// 初始化页面
+		/// </summary>
+		public void InitViewer()
+		{
+			// 加载布局文件
+			string uxml = "Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml";
+			_visualAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxml);
+			if (_visualAsset == null)
+			{
+				Debug.LogError($"Not found {nameof(AssetListDebuggerViewer)}.uxml : {uxml}");
+				return;
+			}
+			_root = _visualAsset.CloneTree();
+			_root.style.flexGrow = 1f;
+
+			// 资源列表
+			_assetListView = _root.Q<ListView>("TopListView");
+			_assetListView.makeItem = MakeAssetListViewItem;
+			_assetListView.bindItem = BindAssetListViewItem;
+
+#if UNITY_2020_1_OR_NEWER
+			_assetListView.onSelectionChange += AssetListView_onSelectionChange;
+#else
+			_assetListView.onSelectionChanged += AssetListView_onSelectionChange;
+#endif
+			// 依赖列表
+			_dependListView = _root.Q<ListView>("BottomListView");
+			_dependListView.makeItem = MakeDependListViewItem;
+			_dependListView.bindItem = BindDependListViewItem;
+		}
+
+		/// <summary>
+		/// 填充页面数据
+		/// </summary>
+		public void FillViewData(DebugSummy summy, string searchKeyWord)
+		{
+			_summy = summy;
+			_assetListView.Clear();
+			_assetListView.itemsSource = FilterViewItems(summy, searchKeyWord);
+		}
+		private List<DebugProviderInfo> FilterViewItems(DebugSummy summy, string searchKeyWord)
+		{
+			var result = new List<DebugProviderInfo>(summy.ProviderInfos.Count);
+			foreach (var providerInfo in summy.ProviderInfos)
+			{
+				if(string.IsNullOrEmpty(searchKeyWord) == false)
+				{
+					if (providerInfo.AssetPath.Contains(searchKeyWord) == false)
+						continue;					
+				}
+				result.Add(providerInfo);
+			}
+			return result;
+		}
+
+		/// <summary>
+		/// 挂接到父类页面上
+		/// </summary>
+		public void AttachParent(VisualElement parent)
+		{
+			parent.Add(_root);
+		}
+
+		/// <summary>
+		/// 从父类页面脱离开
+		/// </summary>
+		public void DetachParent()
+		{
+			_root.RemoveFromHierarchy();
+		}
+
+
+		// 资源列表相关
+		private VisualElement MakeAssetListViewItem()
+		{
+			VisualElement element = new VisualElement();
+			element.style.flexDirection = FlexDirection.Row;
+
+			{
+				var label = new Label();
+				label.name = "Label1";
+				label.style.unityTextAlign = TextAnchor.MiddleLeft;
+				label.style.marginLeft = 3f;
+				label.style.flexGrow = 1f;
+				label.style.width = 280;
+				element.Add(label);
+			}
+
+			{
+				var label = new Label();
+				label.name = "Label2";
+				label.style.unityTextAlign = TextAnchor.MiddleLeft;
+				label.style.marginLeft = 3f;
+				//label.style.flexGrow = 1f;
+				label.style.width = 100;
+				element.Add(label);
+			}
+
+			{
+				var label = new Label();
+				label.name = "Label3";
+				label.style.unityTextAlign = TextAnchor.MiddleLeft;
+				label.style.marginLeft = 3f;
+				//label.style.flexGrow = 1f;
+				label.style.width = 120;
+				element.Add(label);
+			}
+
+			return element;
+		}
+		private void BindAssetListViewItem(VisualElement element, int index)
+		{
+			var sourceData = _assetListView.itemsSource as List<DebugProviderInfo>;
+			var providerInfo = sourceData[index];
+			
+			// Asset Path
+			var label1 = element.Q<Label>("Label1");
+			label1.text = providerInfo.AssetPath;
+
+			// Ref Count
+			var label2 = element.Q<Label>("Label2");
+			label2.text = providerInfo.RefCount.ToString();
+
+			// Status
+			var label3 = element.Q<Label>("Label3");
+			label3.text = providerInfo.Status.ToString();
+		}
+		private void AssetListView_onSelectionChange(IEnumerable<object> objs)
+		{
+			foreach (var item in objs)
+			{
+				DebugProviderInfo providerInfo = item as DebugProviderInfo;
+				FillDependListView(providerInfo);
+			}
+		}
+
+		// 依赖列表相关
+		private VisualElement MakeDependListViewItem()
+		{
+			VisualElement element = new VisualElement();
+			element.style.flexDirection = FlexDirection.Row;
+
+			{
+				var label = new Label();
+				label.name = "Label1";
+				label.style.unityTextAlign = TextAnchor.MiddleLeft;
+				label.style.marginLeft = 3f;
+				label.style.flexGrow = 1f;
+				label.style.width = 280;
+				element.Add(label);
+			}
+
+			{
+				var label = new Label();
+				label.name = "Label2";
+				label.style.unityTextAlign = TextAnchor.MiddleLeft;
+				label.style.marginLeft = 3f;
+				//label.style.flexGrow = 1f;
+				label.style.width = 100;
+				element.Add(label);
+			}
+
+			{
+				var label = new Label();
+				label.name = "Label3";
+				label.style.unityTextAlign = TextAnchor.MiddleLeft;
+				label.style.marginLeft = 3f;
+				//label.style.flexGrow = 1f;
+				label.style.width = 250;
+				element.Add(label);
+			}
+
+			{
+				var label = new Label();
+				label.name = "Label4";
+				label.style.unityTextAlign = TextAnchor.MiddleLeft;
+				label.style.marginLeft = 3f;
+				//label.style.flexGrow = 1f;
+				label.style.width = 150;
+				element.Add(label);
+			}
+
+			return element;
+		}
+		private void BindDependListViewItem(VisualElement element, int index)
+		{
+			List<DebugBundleInfo> bundles = _dependListView.itemsSource as List<DebugBundleInfo>;
+			DebugBundleInfo bundleInfo = bundles[index];
+
+			// Bundle Name
+			var label1 = element.Q<Label>("Label1");
+			label1.text = bundleInfo.BundleName;
+
+			// Version
+			var label2 = element.Q<Label>("Label2");
+			label2.text = bundleInfo.Version.ToString();
+
+			// Ref Count
+			var label3 = element.Q<Label>("Label3");
+			label3.text = bundleInfo.RefCount.ToString();
+
+			// Status
+			var label4 = element.Q<Label>("Label4");
+			label4.text = bundleInfo.Status.ToString();
+		}
+		private void FillDependListView(DebugProviderInfo providerInfo)
+		{
+			_dependListView.Clear();
+#if UNITY_2020_1_OR_NEWER
+			_dependListView.ClearSelection();
+#endif
+			_dependListView.itemsSource = providerInfo.BundleInfos;
+		}
+	}
+}
+#endif
\ No newline at end of file
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs.meta b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs.meta
new file mode 100644
index 0000000..13629bf
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 48da07f89a738f345a11ed22c2bcf5f7
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml
new file mode 100644
index 0000000..9dd0ed7
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml
@@ -0,0 +1,19 @@
+<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
+    <ui:VisualElement name="TopGroup" style="flex-grow: 1; border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-left-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-top-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); margin-left: 0; margin-right: 0; margin-top: 2px; margin-bottom: 1px; display: flex;">
+        <uie:Toolbar name="TopBar" style="height: 25px; margin-left: 1px; margin-right: 1px;">
+            <uie:ToolbarButton text="Asset Path" display-tooltip-when-elided="true" name="TopBar1" style="width: 280px; -unity-text-align: middle-left; flex-grow: 1;" />
+            <uie:ToolbarButton text="Ref Count" display-tooltip-when-elided="true" name="TopBar2" style="width: 100px; -unity-text-align: middle-left; flex-grow: 0;" />
+            <uie:ToolbarButton text="Status" display-tooltip-when-elided="true" name="TopBar3" style="width: 120px; -unity-text-align: middle-left;" />
+        </uie:Toolbar>
+        <ui:ListView focusable="true" name="TopListView" item-height="18" style="flex-grow: 1;" />
+    </ui:VisualElement>
+    <ui:VisualElement name="BottomGroup" style="height: 200px; border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-left-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-top-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); margin-left: 0; margin-right: 0; margin-top: 1px; margin-bottom: 1px; display: flex;">
+        <uie:Toolbar name="BottomBar" style="height: 25px; margin-left: 1px; margin-right: 1px;">
+            <uie:ToolbarButton text="Depend Bundles" display-tooltip-when-elided="true" name="BottomBar1" style="width: 280px; -unity-text-align: middle-left; flex-grow: 1;" />
+            <uie:ToolbarButton text="Version" display-tooltip-when-elided="true" name="BottomBar2" style="width: 100px; -unity-text-align: middle-left; flex-grow: 0;" />
+            <uie:ToolbarButton text="Ref Count" display-tooltip-when-elided="true" name="BottomBar3" style="width: 100px; -unity-text-align: middle-left;" />
+            <uie:ToolbarButton text="Status" display-tooltip-when-elided="true" name="BottomBar4" style="width: 120px; -unity-text-align: middle-left;" />
+        </uie:Toolbar>
+        <ui:ListView focusable="true" name="BottomListView" item-height="18" style="flex-grow: 1;" />
+    </ui:VisualElement>
+</ui:UXML>
diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml.meta b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml.meta
new file mode 100644
index 0000000..45b7281
--- /dev/null
+++ b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/AssetListDebuggerViewer.uxml.meta
@@ -0,0 +1,10 @@
+fileFormatVersion: 2
+guid: 31c6096c1cb29b4469096b7b4942a322
+ScriptedImporter:
+  internalIDToNameTable: []
+  externalObjects: {}
+  serializedVersion: 2
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
+  script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}
diff --git a/Assets/YooAsset/Editor/AssetBundleProfiler/AssetBundleProfilerWindow.cs b/Assets/YooAsset/Editor/AssetBundleProfiler/AssetBundleProfilerWindow.cs
deleted file mode 100644
index 248adc1..0000000
--- a/Assets/YooAsset/Editor/AssetBundleProfiler/AssetBundleProfilerWindow.cs
+++ /dev/null
@@ -1,11 +0,0 @@
-using System.Collections;
-using System.Collections.Generic;
-using UnityEngine;
-
-namespace YooAsset.Editor
-{
-	public class AssetBundleProfilerWindow
-	{
-
-	}
-}
\ No newline at end of file