Skip to content

Commit

Permalink
use MountPoint suffix in file container display for clarity, stage ub…
Browse files Browse the repository at this point in the history
…ulk, better stage/extract/delete io error handling
  • Loading branch information
atenfyr committed Aug 7, 2024
1 parent aad2fe6 commit 898550c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
42 changes: 32 additions & 10 deletions UAssetGUI/FileContainerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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<string, DirectoryTreeItem>();
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);
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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]];
Expand All @@ -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]];
Expand All @@ -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<string, DirectoryTreeItem> Children;
Expand All @@ -776,20 +792,22 @@ 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;
}

using (FileStream stream = new FileStream(ParentForm.CurrentContainerPath, FileMode.Open))
{
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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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<string, DirectoryTreeItem>();
}

Expand Down
18 changes: 14 additions & 4 deletions UAssetGUI/UAGConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}
Expand Down

0 comments on commit 898550c

Please sign in to comment.