From 898550c148ce1a4c54ffaacfb189fc3f040a8622 Mon Sep 17 00:00:00 2001 From: atenfyr Date: Wed, 7 Aug 2024 13:18:08 -0500 Subject: [PATCH] use MountPoint suffix in file container display for clarity, stage ubulk, better stage/extract/delete io error handling --- UAssetGUI/FileContainerForm.cs | 42 ++++++++++++++++++++++++++-------- UAssetGUI/UAGConfig.cs | 18 +++++++++++---- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/UAssetGUI/FileContainerForm.cs b/UAssetGUI/FileContainerForm.cs index 58b41c2..9b12528 100644 --- a/UAssetGUI/FileContainerForm.cs +++ b/UAssetGUI/FileContainerForm.cs @@ -204,7 +204,21 @@ public void LoadContainer(string path) MountPoint = pakReader.GetMountPoint(); } - DirectoryTreeMap[loadTreeView] = new DirectoryTree(this, allFiles); + // if the MountPoint starts with "../../../", then let's adjust all files so we can just change the MountPoint to that + string mpPrefix = "../../../"; + string Prefix = ""; + if (MountPoint.StartsWith(mpPrefix)) + { + Prefix = MountPoint.Substring(mpPrefix.Length); + MountPoint = mpPrefix; + + for (int i = 0; i < allFiles.Length; i++) + { + allFiles[i] = Path.Combine(Prefix, allFiles[i]); + } + } + + DirectoryTreeMap[loadTreeView] = new DirectoryTree(this, allFiles, null, Prefix); RefreshTreeView(loadTreeView); this.Text = BaseForm.DisplayVersion + " - " + CurrentContainerPath; @@ -626,6 +640,7 @@ public PointingFileTreeNode(string text, DirectoryTreeItem item) : base(text) tsmItem.Click += (sender, args) => { string outPath = UAGConfig.ExtractFile(Pointer); + if (outPath == null) return; if ((Path.GetExtension(outPath)?.Length ?? 0) > 0) outPath = Path.GetDirectoryName(outPath); UAGUtils.OpenDirectory(outPath); }; @@ -658,17 +673,17 @@ public DirectoryTree(FileContainerForm parentForm) ParentForm = parentForm; } - public DirectoryTree(FileContainerForm parentForm, string[] paths, string[] fixedAssetsOnDisk = null) + public DirectoryTree(FileContainerForm parentForm, string[] paths, string[] fixedAssetsOnDisk = null, string prefix = null) { RootNodes = new Dictionary(); ParentForm = parentForm; if (fixedAssetsOnDisk != null && fixedAssetsOnDisk.Length == paths.Length) { - for (int i = 0; i < paths.Length; i++) this.CreateNode(paths[i], fixedAssetsOnDisk[i]); + for (int i = 0; i < paths.Length; i++) this.CreateNode(paths[i], fixedAssetsOnDisk[i], prefix); } else { - for (int i = 0; i < paths.Length; i++) this.CreateNode(paths[i]); + for (int i = 0; i < paths.Length; i++) this.CreateNode(paths[i], null, prefix); } } @@ -708,7 +723,7 @@ public DirectoryTreeItem GetNode(string path) return currentItem; } - public DirectoryTreeItem CreateNode(string path, string fixedAssetOnDisk = null) + public DirectoryTreeItem CreateNode(string path, string fixedAssetOnDisk = null, string prefix = null) { string[] pathComponents = path.Split('/'); if (pathComponents.Length == 0) return null; @@ -727,7 +742,7 @@ public DirectoryTreeItem CreateNode(string path, string fixedAssetOnDisk = null) string ext = Path.GetExtension(pathComponents[0]); if (ext.Length > 1 && (ext == ".uexp" || ext == ".ubulk" || ext == ".bak")) return null; - RootNodes[pathComponents[0]] = new DirectoryTreeItem(ParentForm, pathComponents[0], pathComponents[0], ext.Length > 1); + RootNodes[pathComponents[0]] = new DirectoryTreeItem(ParentForm, pathComponents[0], pathComponents[0], ext.Length > 1, prefix); } DirectoryTreeItem currentItem = RootNodes[pathComponents[0]]; @@ -744,7 +759,7 @@ public DirectoryTreeItem CreateNode(string path, string fixedAssetOnDisk = null) if (!currentItem.Children.ContainsKey(pathComponents[i])) { - currentItem.Children[pathComponents[i]] = new DirectoryTreeItem(ParentForm, pathComponents[i], currentItem.FullPath + "/" + pathComponents[i], ext.Length > 1); + currentItem.Children[pathComponents[i]] = new DirectoryTreeItem(ParentForm, pathComponents[i], currentItem.FullPath + "/" + pathComponents[i], ext.Length > 1, prefix); currentItem.Children[pathComponents[i]].Parent = currentItem; } currentItem = currentItem.Children[pathComponents[i]]; @@ -766,6 +781,7 @@ public class DirectoryTreeItem public string Name; public string FullPath; public string FixedPathOnDisk; + public string Prefix; public bool IsFile = false; public DirectoryTreeItem Parent; public IDictionary Children; @@ -776,12 +792,14 @@ public string SaveFileToTemp() string outputPath1 = Path.Combine(outputPathDirectory, FullPath.Replace('/', Path.DirectorySeparatorChar)); string outputPath2 = Path.Combine(outputPathDirectory, Path.ChangeExtension(FullPath, ".uexp").Replace('/', Path.DirectorySeparatorChar)); + string outputPath3 = Path.Combine(outputPathDirectory, Path.ChangeExtension(FullPath, ".ubulk").Replace('/', Path.DirectorySeparatorChar)); Directory.CreateDirectory(Path.GetDirectoryName(outputPath1)); // same directory as outputPath2, no need to create that one too if (FixedPathOnDisk != null) { File.Copy(FixedPathOnDisk, outputPath1, true); try { File.Copy(Path.ChangeExtension(FixedPathOnDisk, ".uexp"), outputPath2, true); } catch { } + try { File.Copy(Path.ChangeExtension(FixedPathOnDisk, ".ubulk"), outputPath3, true); } catch { } return outputPath1; } @@ -789,7 +807,7 @@ public string SaveFileToTemp() { var reader = new PakBuilder().Reader(stream); - byte[] res = reader.Get(stream, FullPath); + byte[] res = reader.Get(stream, FullPath.Substring(Prefix?.Length ?? 0)); if (res != null) { File.WriteAllBytes(outputPath1, res); @@ -799,7 +817,9 @@ public string SaveFileToTemp() return null; } - res = reader.Get(stream, Path.ChangeExtension(FullPath, ".uexp")); + res = reader.Get(stream, 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")); if (res != null) File.WriteAllBytes(outputPath2, res); } @@ -878,6 +898,7 @@ public void DeleteFile() { File.Delete(FixedPathOnDisk); try { File.Delete(Path.ChangeExtension(FixedPathOnDisk, ".uexp")); } catch { } + try { File.Delete(Path.ChangeExtension(FixedPathOnDisk, ".ubulk")); } catch { } } catch (UnauthorizedAccessException) { @@ -888,12 +909,13 @@ public void DeleteFile() this.ParentForm.RefreshTreeView(this.ParentForm.saveTreeView); } - public DirectoryTreeItem(FileContainerForm parentForm, string name, string fullPath, bool isFile) + public DirectoryTreeItem(FileContainerForm parentForm, string name, string fullPath, bool isFile, string prefix) { ParentForm = parentForm; FullPath = fullPath; Name = Path.GetFileName(name); IsFile = isFile; + Prefix = prefix; Children = new Dictionary(); } diff --git a/UAssetGUI/UAGConfig.cs b/UAssetGUI/UAGConfig.cs index fa66d9a..33c0e49 100644 --- a/UAssetGUI/UAGConfig.cs +++ b/UAssetGUI/UAGConfig.cs @@ -90,10 +90,13 @@ public static void StageFile(string rawPathOnDisk, string CurrentContainerPath, newPath = newPath.Replace('/', Path.DirectorySeparatorChar); var finalPath = DifferentStagingPerPak ? Path.Combine(StagingFolder, Path.GetFileNameWithoutExtension(CurrentContainerPath), newPath) : Path.Combine(StagingFolder, newPath); - Directory.CreateDirectory(Path.GetDirectoryName(finalPath)); + try { Directory.CreateDirectory(Path.GetDirectoryName(finalPath)); } catch { return; } // 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(rawPathOnDisk, finalPath, true); try { File.Copy(Path.ChangeExtension(rawPathOnDisk, ".uexp"), Path.ChangeExtension(finalPath, ".uexp"), true); } catch { } + try { File.Copy(Path.ChangeExtension(rawPathOnDisk, ".ubulk"), Path.ChangeExtension(finalPath, ".ubulk"), true); } catch { } } public static void StageFile(DirectoryTreeItem item, string newPath = null) @@ -111,13 +114,16 @@ public static void StageFile(DirectoryTreeItem item, string newPath = null) string outputPath = item.SaveFileToTemp(); var finalPath = DifferentStagingPerPak ? Path.Combine(StagingFolder, Path.GetFileNameWithoutExtension(item.ParentForm.CurrentContainerPath), newPath) : Path.Combine(StagingFolder, newPath); if (outputPath == null || finalPath == null) return; - Directory.CreateDirectory(Path.GetDirectoryName(finalPath)); + try { Directory.CreateDirectory(Path.GetDirectoryName(finalPath)); } catch { return; } // 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 - try { File.Delete(finalPath); } catch { } 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 { } } public static string ExtractFile(DirectoryTreeItem item) @@ -132,12 +138,16 @@ public static string ExtractFile(DirectoryTreeItem item) } string outputPath = item.SaveFileToTemp(); - Directory.CreateDirectory(Path.GetDirectoryName(finalPath)); + 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; }