From d30d1a4cd04e65b91de159f03385ba5fb27fa9f6 Mon Sep 17 00:00:00 2001 From: atenfyr Date: Sat, 18 Jan 2025 19:28:18 -0600 Subject: [PATCH] extract all perf improvements --- UAssetGUI/FileContainerForm.cs | 56 ++++++++++++++++++++------- UAssetGUI/ProgressBarForm.Designer.cs | 2 +- UAssetGUI/UAGConfig.cs | 19 ++------- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/UAssetGUI/FileContainerForm.cs b/UAssetGUI/FileContainerForm.cs index 44aba2c..4f3ed17 100644 --- a/UAssetGUI/FileContainerForm.cs +++ b/UAssetGUI/FileContainerForm.cs @@ -529,11 +529,11 @@ private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) if (e.Node is PointingFileTreeNode ptn) InitializeChildren(ptn); } - private void ExtractVisit(DirectoryTreeItem processingNode, ProgressBarForm progressBarForm) + private void ExtractVisit(DirectoryTreeItem processingNode, ProgressBarForm progressBarForm, FileStream stream2 = null, PakReader reader2 = null) { if (processingNode.IsFile) { - UAGConfig.ExtractFile(processingNode); + UAGConfig.ExtractFile(processingNode, stream2, reader2); extractAllBackgroundWorker.ReportProgress(0); // the percentage we pass in is unused return; } @@ -541,7 +541,7 @@ private void ExtractVisit(DirectoryTreeItem processingNode, ProgressBarForm prog foreach (var entry in processingNode.Children) { if (extractAllBackgroundWorker.CancellationPending) break; - ExtractVisit(entry.Value, progressBarForm); + ExtractVisit(entry.Value, progressBarForm, stream2, reader2); } } @@ -574,10 +574,15 @@ private void extractAllToolStripMenuItem_Click(object sender, EventArgs e) private void extractAllBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { if (!DirectoryTreeMap.TryGetValue(loadTreeView, out DirectoryTree loadedTree) || loadedTree == null) throw new InvalidOperationException("No container loaded"); - foreach (var entry in loadedTree.RootNodes) + + using (FileStream stream = new FileStream(this.CurrentContainerPath, FileMode.Open)) { - if (extractAllBackgroundWorker.CancellationPending) break; - ExtractVisit(entry.Value, progressBarForm); + var reader = new PakBuilder().Reader(stream); + foreach (var entry in loadedTree.RootNodes) + { + if (extractAllBackgroundWorker.CancellationPending) break; + ExtractVisit(entry.Value, progressBarForm, stream, reader); + } } if (extractAllBackgroundWorker.CancellationPending) @@ -786,9 +791,9 @@ public class DirectoryTreeItem public DirectoryTreeItem Parent; public IDictionary Children; - public string SaveFileToTemp() + public string SaveFileToTemp(string outputPathDirectory = null, FileStream stream2 = null, PakReader reader2 = null) { - string outputPathDirectory = Path.Combine(Path.GetTempPath(), "UAG_read_only", Path.GetFileNameWithoutExtension(ParentForm.CurrentContainerPath)); + outputPathDirectory = outputPathDirectory ?? Path.Combine(Path.GetTempPath(), "UAG_read_only", Path.GetFileNameWithoutExtension(ParentForm.CurrentContainerPath)); string outputPath1 = Path.Combine(outputPathDirectory, FullPath.Replace('/', Path.DirectorySeparatorChar)); string outputPath2 = Path.Combine(outputPathDirectory, Path.ChangeExtension(FullPath, ".uexp").Replace('/', Path.DirectorySeparatorChar)); @@ -803,11 +808,35 @@ public string SaveFileToTemp() return outputPath1; } - using (FileStream stream = new FileStream(ParentForm.CurrentContainerPath, FileMode.Open)) + if (reader2 == null || stream2 == null) { - var reader = new PakBuilder().Reader(stream); + using (FileStream stream = new FileStream(ParentForm.CurrentContainerPath, FileMode.Open)) + { + var reader = new PakBuilder().Reader(stream); + + byte[] res = reader.Get(stream, FullPath.Substring(Prefix?.Length ?? 0)); + if (res != null) + { + if (File.Exists(outputPath1)) { try { File.Delete(outputPath1); } catch { } } + File.WriteAllBytes(outputPath1, res); + } + else + { + return null; + } - byte[] res = reader.Get(stream, FullPath.Substring(Prefix?.Length ?? 0)); + res = reader.Get(stream, Path.ChangeExtension(FullPath.Substring(Prefix?.Length ?? 0), ".uexp")); + if (File.Exists(outputPath2)) { try { File.Delete(outputPath2); } catch { } } + if (res != null) File.WriteAllBytes(outputPath2, res); + + res = reader.Get(stream, Path.ChangeExtension(FullPath.Substring(Prefix?.Length ?? 0), ".ubulk")); + if (File.Exists(outputPath3)) { try { File.Delete(outputPath3); } catch { } } + if (res != null) File.WriteAllBytes(outputPath3, res); + } + } + else + { + byte[] res = reader2.Get(stream2, FullPath.Substring(Prefix?.Length ?? 0)); if (res != null) { File.WriteAllBytes(outputPath1, res); @@ -817,12 +846,13 @@ public string SaveFileToTemp() return null; } - res = reader.Get(stream, Path.ChangeExtension(FullPath.Substring(Prefix?.Length ?? 0), ".uexp")); + res = reader2.Get(stream2, Path.ChangeExtension(FullPath.Substring(Prefix?.Length ?? 0), ".uexp")); if (res != null) File.WriteAllBytes(outputPath2, res); - res = reader.Get(stream, Path.ChangeExtension(FullPath.Substring(Prefix?.Length ?? 0), ".ubulk")); + res = reader2.Get(stream2, Path.ChangeExtension(FullPath.Substring(Prefix?.Length ?? 0), ".ubulk")); if (res != null) File.WriteAllBytes(outputPath2, res); } + return outputPath1; } diff --git a/UAssetGUI/ProgressBarForm.Designer.cs b/UAssetGUI/ProgressBarForm.Designer.cs index f37dbf4..f68d78c 100644 --- a/UAssetGUI/ProgressBarForm.Designer.cs +++ b/UAssetGUI/ProgressBarForm.Designer.cs @@ -60,7 +60,7 @@ private void InitializeComponent() // label1.Location = new System.Drawing.Point(16, 79); label1.Name = "label1"; - label1.Size = new System.Drawing.Size(71, 30); + label1.Size = new System.Drawing.Size(95, 30); label1.TabIndex = 16; label1.Text = "label1"; label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; diff --git a/UAssetGUI/UAGConfig.cs b/UAssetGUI/UAGConfig.cs index 8fee801..3b86d46 100644 --- a/UAssetGUI/UAGConfig.cs +++ b/UAssetGUI/UAGConfig.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Windows.Forms; +using UAssetAPI; using UAssetAPI.Unversioned; namespace UAssetGUI @@ -127,30 +128,18 @@ public static void StageFile(DirectoryTreeItem item, string newPath = null) try { File.Delete(Path.ChangeExtension(outputPath, ".ubulk")); } catch { } } - public static string ExtractFile(DirectoryTreeItem item) + public static string ExtractFile(DirectoryTreeItem item, FileStream stream2 = null, PakReader reader2 = null) { var finalPath = Path.Combine(ExtractedFolder, item.FullPath.Replace('/', Path.DirectorySeparatorChar)); // recursive if we were given a directory if (!item.IsFile) { - foreach (var child in item.Children) ExtractFile(child.Value); + foreach (var child in item.Children) ExtractFile(child.Value, stream2, reader2); return finalPath; } - string outputPath = item.SaveFileToTemp(); - try { Directory.CreateDirectory(Path.GetDirectoryName(finalPath)); } catch { return null; } // fail silently if cant make the directory we need - - try { Directory.Delete(finalPath, true); } catch { } // if we turn a directory into a file, try and get rid of the directory - - File.Copy(outputPath, finalPath, true); - try { File.Copy(Path.ChangeExtension(outputPath, ".uexp"), Path.ChangeExtension(finalPath, ".uexp"), true); } catch { } - try { File.Copy(Path.ChangeExtension(outputPath, ".ubulk"), Path.ChangeExtension(finalPath, ".ubulk"), true); } catch { } - try { File.Delete(outputPath); } catch { } - try { File.Delete(Path.ChangeExtension(outputPath, ".uexp")); } catch { } - try { File.Delete(Path.ChangeExtension(outputPath, ".ubulk")); } catch { } - - return finalPath; + return item.SaveFileToTemp(ExtractedFolder, stream2, reader2); } public static bool TryGetMappings(string name, out Usmap mappings)