From 06fbe69a973543ec12089b785f7d8ad7ee55da41 Mon Sep 17 00:00:00 2001 From: Perfare Date: Sat, 28 Mar 2020 04:24:32 +0800 Subject: [PATCH] performance improvement --- AssetStudioGUI/AssetStudioGUIForm.Designer.cs | 12 +- AssetStudioGUI/AssetStudioGUIForm.cs | 176 ++++++++---------- AssetStudioGUI/Studio.cs | 25 +-- 3 files changed, 98 insertions(+), 115 deletions(-) diff --git a/AssetStudioGUI/AssetStudioGUIForm.Designer.cs b/AssetStudioGUI/AssetStudioGUIForm.Designer.cs index 11bce8e..3d7f85a 100644 --- a/AssetStudioGUI/AssetStudioGUIForm.Designer.cs +++ b/AssetStudioGUI/AssetStudioGUIForm.Designer.cs @@ -83,8 +83,8 @@ this.listSearch = new System.Windows.Forms.TextBox(); this.tabPage3 = new System.Windows.Forms.TabPage(); this.classesListView = new System.Windows.Forms.ListView(); - this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.progressbarPanel = new System.Windows.Forms.Panel(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.previewPanel = new System.Windows.Forms.Panel(); @@ -622,16 +622,16 @@ this.classesListView.View = System.Windows.Forms.View.Details; this.classesListView.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.classesListView_ItemSelectionChanged); // - // columnHeader2 - // - this.columnHeader2.Text = "Name"; - this.columnHeader2.Width = 300; - // // columnHeader1 // this.columnHeader1.Text = "ID"; this.columnHeader1.Width = 70; // + // columnHeader2 + // + this.columnHeader2.Text = "Name"; + this.columnHeader2.Width = 300; + // // progressbarPanel // this.progressbarPanel.Controls.Add(this.progressBar1); diff --git a/AssetStudioGUI/AssetStudioGUIForm.cs b/AssetStudioGUI/AssetStudioGUIForm.cs index ce87384..46bcb88 100644 --- a/AssetStudioGUI/AssetStudioGUIForm.cs +++ b/AssetStudioGUI/AssetStudioGUIForm.cs @@ -13,6 +13,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; +using System.Threading.Tasks; using System.Timers; using System.Windows.Forms; using static AssetStudioGUI.Studio; @@ -82,47 +83,49 @@ namespace AssetStudioGUI [DllImport("gdi32.dll")] private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts); + public AssetStudioGUIForm() + { + Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); + InitializeComponent(); + Text = $"AssetStudioGUI v{Application.ProductVersion}"; + delayTimer = new System.Timers.Timer(800); + delayTimer.Elapsed += new ElapsedEventHandler(delayTimer_Elapsed); + displayAll.Checked = Properties.Settings.Default.displayAll; + displayInfo.Checked = Properties.Settings.Default.displayInfo; + enablePreview.Checked = Properties.Settings.Default.enablePreview; + FMODinit(); - private void loadFile_Click(object sender, EventArgs e) + Logger.Default = new GUILogger(StatusStripUpdate); + Progress.Default = new GUIProgress(SetProgressBarValue); + Studio.StatusStripUpdate = StatusStripUpdate; + } + + private async void loadFile_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { ResetForm(); - ThreadPool.QueueUserWorkItem(state => - { - assetsManager.LoadFiles(openFileDialog1.FileNames); - BuildAssetStructures(); - }); + await Task.Run(() => assetsManager.LoadFiles(openFileDialog1.FileNames)); + BuildAssetStructures(); } } - private void loadFolder_Click(object sender, EventArgs e) + private async void loadFolder_Click(object sender, EventArgs e) { var openFolderDialog = new OpenFolderDialog(); if (openFolderDialog.ShowDialog(this) == DialogResult.OK) { ResetForm(); - ThreadPool.QueueUserWorkItem(state => - { - assetsManager.LoadFolder(openFolderDialog.Folder); - BuildAssetStructures(); - }); + await Task.Run(() => assetsManager.LoadFolder(openFolderDialog.Folder)); + BuildAssetStructures(); } } private void extractFileToolStripMenuItem_Click(object sender, EventArgs e) { - var openBundleDialog = new OpenFileDialog + if (openFileDialog1.ShowDialog() == DialogResult.OK) { - Filter = "All types|*.*", - FilterIndex = 1, - RestoreDirectory = true, - Multiselect = true - }; - - if (openBundleDialog.ShowDialog() == DialogResult.OK) - { - ExtractFile(openBundleDialog.FileNames); + ExtractFile(openFileDialog1.FileNames); } } @@ -136,7 +139,7 @@ namespace AssetStudioGUI } } - private void BuildAssetStructures() + private async void BuildAssetStructures() { if (assetsManager.assetsFileList.Count == 0) { @@ -144,72 +147,66 @@ namespace AssetStudioGUI return; } - var data = BuildAssetData(); - var productName = data.Item1; - var treeNodeCollection = data.Item2; - var typeMap = BuildClassStructure(); + (var productName, var treeNodeCollection) = await Task.Run(() => BuildAssetData()); + var typeMap = await Task.Run(() => BuildClassStructure()); - BeginInvoke(new Action(() => + if (!string.IsNullOrEmpty(productName)) { - if (!string.IsNullOrEmpty(productName)) - { - Text = $"AssetStudioGUI - {productName} - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}"; - } - else - { - Text = $"AssetStudioGUI - no productName - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}"; - } + Text = $"AssetStudioGUI v{Application.ProductVersion} - {productName} - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}"; + } + else + { + Text = $"AssetStudioGUI v{Application.ProductVersion} - no productName - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}"; + } - assetListView.VirtualListSize = visibleAssets.Count; + assetListView.VirtualListSize = visibleAssets.Count; - sceneTreeView.BeginUpdate(); - sceneTreeView.Nodes.AddRange(treeNodeCollection.ToArray()); - foreach (TreeNode node in sceneTreeView.Nodes) - { - node.HideCheckBox(); - } - sceneTreeView.EndUpdate(); - treeNodeCollection.Clear(); + sceneTreeView.BeginUpdate(); + sceneTreeView.Nodes.AddRange(treeNodeCollection.ToArray()); + foreach (var node in treeNodeCollection) + { + node.HideCheckBox(); + } + sceneTreeView.EndUpdate(); + treeNodeCollection.Clear(); - classesListView.BeginUpdate(); - foreach (var version in typeMap) - { - var versionGroup = new ListViewGroup(version.Key); - classesListView.Groups.Add(versionGroup); + classesListView.BeginUpdate(); + foreach (var version in typeMap) + { + var versionGroup = new ListViewGroup(version.Key); + classesListView.Groups.Add(versionGroup); - foreach (var uclass in version.Value) - { - uclass.Value.Group = versionGroup; - classesListView.Items.Add(uclass.Value); - } + foreach (var uclass in version.Value) + { + uclass.Value.Group = versionGroup; + classesListView.Items.Add(uclass.Value); } - typeMap.Clear(); - classesListView.EndUpdate(); + } + typeMap.Clear(); + classesListView.EndUpdate(); - var types = exportableAssets.Select(x => x.Type).Distinct().OrderBy(x => x.ToString()).ToArray(); - foreach (var type in types) + var types = exportableAssets.Select(x => x.Type).Distinct().OrderBy(x => x.ToString()).ToArray(); + foreach (var type in types) + { + var typeItem = new ToolStripMenuItem { - var typeItem = new ToolStripMenuItem - { - CheckOnClick = true, - Name = type.ToString(), - Size = new Size(180, 22), - Text = type.ToString() - }; - typeItem.Click += typeToolStripMenuItem_Click; - filterTypeToolStripMenuItem.DropDownItems.Add(typeItem); - } - allToolStripMenuItem.Checked = true; - var log = $"Finished loading {assetsManager.assetsFileList.Count} files with {assetListView.Items.Count} exportable assets"; - var m_ObjectsCount = assetsManager.assetsFileList.Sum(x => x.m_Objects.Count); - var objectsCount = assetsManager.assetsFileList.Sum(x => x.Objects.Count); - if (m_ObjectsCount != objectsCount) - { - log += $" and {m_ObjectsCount - objectsCount} assets failed to read"; - } - StatusStripUpdate(log); - treeSearch.Select(); - })); + CheckOnClick = true, + Name = type.ToString(), + Size = new Size(180, 22), + Text = type.ToString() + }; + typeItem.Click += typeToolStripMenuItem_Click; + filterTypeToolStripMenuItem.DropDownItems.Add(typeItem); + } + allToolStripMenuItem.Checked = true; + var log = $"Finished loading {assetsManager.assetsFileList.Count} files with {assetListView.Items.Count} exportable assets"; + var m_ObjectsCount = assetsManager.assetsFileList.Sum(x => x.m_Objects.Count); + var objectsCount = assetsManager.assetsFileList.Sum(x => x.Objects.Count); + if (m_ObjectsCount != objectsCount) + { + log += $" and {m_ObjectsCount - objectsCount} assets failed to read"; + } + StatusStripUpdate(log); } private void typeToolStripMenuItem_Click(object sender, EventArgs e) @@ -1138,26 +1135,9 @@ namespace AssetStudioGUI } } - public AssetStudioGUIForm() - { - Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); - InitializeComponent(); - delayTimer = new System.Timers.Timer(800); - delayTimer.Elapsed += new ElapsedEventHandler(delayTimer_Elapsed); - displayAll.Checked = Properties.Settings.Default.displayAll; - displayInfo.Checked = Properties.Settings.Default.displayInfo; - enablePreview.Checked = Properties.Settings.Default.enablePreview; - FMODinit(); - - Logger.Default = new GUILogger(StatusStripUpdate); - Progress.Default = new GUIProgress(SetProgressBarValue); - Studio.StatusStripUpdate = StatusStripUpdate; - } - - private void ResetForm() { - Text = "AssetStudioGUI"; + Text = $"AssetStudioGUI v{Application.ProductVersion}"; assetsManager.Clear(); exportableAssets.Clear(); visibleAssets.Clear(); diff --git a/AssetStudioGUI/Studio.cs b/AssetStudioGUI/Studio.cs index 205c9fd..e1efc46 100644 --- a/AssetStudioGUI/Studio.cs +++ b/AssetStudioGUI/Studio.cs @@ -1,4 +1,5 @@ -using System; +using AssetStudio; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -6,7 +7,6 @@ using System.IO; using System.Linq; using System.Threading; using System.Windows.Forms; -using AssetStudio; using static AssetStudioGUI.Exporter; using Object = AssetStudio.Object; @@ -96,7 +96,7 @@ namespace AssetStudioGUI return extractedCount; } - public static Tuple> BuildAssetData() + public static (string, List) BuildAssetData() { StatusStripUpdate("Building asset list..."); @@ -109,7 +109,7 @@ namespace AssetStudioGUI foreach (var assetsFile in assetsManager.assetsFileList) { var tempExportableAssets = new List(); - AssetBundle ab = null; + Dictionary containers = null; foreach (var asset in assetsFile.Objects.Values) { var assetItem = new AssetItem(asset); @@ -174,8 +174,8 @@ namespace AssetStudioGUI productName = m_PlayerSettings.productName; break; case AssetBundle m_AssetBundle: - ab = m_AssetBundle; - assetItem.Text = ab.m_Name; + containers = m_AssetBundle.m_Container.ToDictionary(x => x.Value.asset.m_PathID, x => x.Key); + assetItem.Text = m_AssetBundle.m_Name; break; case NamedObject m_NamedObject: assetItem.Text = m_NamedObject.m_Name; @@ -200,18 +200,21 @@ namespace AssetStudioGUI } foreach (var item in tempExportableAssets) { - if (ab != null) + if (containers != null) { - var container = ab.m_Container.FirstOrDefault(y => y.Value.asset.m_PathID == item.Asset.m_PathID).Key; - if (!string.IsNullOrEmpty(container)) + if (containers.TryGetValue(item.Asset.m_PathID, out var container)) { - item.Container = container; + if (!string.IsNullOrEmpty(container)) + { + item.Container = container; + } } } item.SetSubItems(); } exportableAssets.AddRange(tempExportableAssets); tempExportableAssets.Clear(); + containers?.Clear(); } visibleAssets = exportableAssets; assetsNameHash.Clear(); @@ -291,7 +294,7 @@ namespace AssetStudioGUI objectAssetItemDic.Clear(); - return new Tuple>(productName, treeNodeCollection); + return (productName, treeNodeCollection); } public static Dictionary> BuildClassStructure()