Skip to content

Commit

Permalink
extract all perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
atenfyr committed Jan 19, 2025
1 parent 1918ad0 commit d30d1a4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
56 changes: 43 additions & 13 deletions UAssetGUI/FileContainerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,19 @@ 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;
}

foreach (var entry in processingNode.Children)
{
if (extractAllBackgroundWorker.CancellationPending) break;
ExtractVisit(entry.Value, progressBarForm);
ExtractVisit(entry.Value, progressBarForm, stream2, reader2);
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -786,9 +791,9 @@ public class DirectoryTreeItem
public DirectoryTreeItem Parent;
public IDictionary<string, DirectoryTreeItem> 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));
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion UAssetGUI/ProgressBarForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 4 additions & 15 deletions UAssetGUI/UAGConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using UAssetAPI;
using UAssetAPI.Unversioned;

namespace UAssetGUI
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d30d1a4

Please sign in to comment.