1
0
mirror of synced 2024-11-12 02:00:50 +01:00

Start to add folder operations for improved archive support

This commit is contained in:
KillzXGaming 2019-05-18 11:13:14 -04:00
parent ffdb042c3f
commit 7e8cfcd16c
7 changed files with 139 additions and 10 deletions

Binary file not shown.

View File

@ -82,7 +82,7 @@ namespace Switch_Toolbox.Library.Forms
{
InitializeComponent();
if (UseListView && FileFormat is IArchiveFile)
if (FileFormat is IArchiveFile)
{
/* ObjectList = new ObjectEditorList();
ObjectList.Dock = DockStyle.Fill;
@ -94,13 +94,7 @@ namespace Switch_Toolbox.Library.Forms
stPanel1.Controls.Add(ObjectTree);
TreeNode FileRoot = new TreeNode(FileFormat.FileName);
foreach (var archive in ((IArchiveFile)FileFormat).Files)
{
ArchiveNodeWrapper node = new ArchiveNodeWrapper(archive.FileName);
node.ArchiveFileInfo = archive;
FileRoot.Nodes.Add(node);
}
FillTreeNodes(FileRoot, ((IArchiveFile)FileFormat).Files);
AddNode(FileRoot);
}
else
@ -112,7 +106,61 @@ namespace Switch_Toolbox.Library.Forms
}
}
void FillTreeNodes(TreeNode root, IEnumerable<ArchiveFileInfo> files)
{
var rootText = root.Text;
var rootTextLength = rootText.Length;
var nodeStrings = files;
foreach (var node in nodeStrings)
{
string nodeString = node.FileName;
var roots = nodeString.Split(new char[] { '/' },
StringSplitOptions.RemoveEmptyEntries);
// The initial parent is the root node
var parentNode = root;
var sb = new StringBuilder(rootText, nodeString.Length + rootTextLength);
for (int rootIndex = 0; rootIndex < roots.Length; rootIndex++)
{
// Build the node name
var parentName = roots[rootIndex];
sb.Append("/");
sb.Append(parentName);
var nodeName = sb.ToString();
// Search for the node
var index = parentNode.Nodes.IndexOfKey(nodeName);
if (index == -1)
{
// Node was not found, add it
var folder = new ArchiveFolderNodeWrapper(parentName);
if (rootIndex == roots.Length - 1)
{
ArchiveNodeWrapper wrapperFile = new ArchiveNodeWrapper(parentName);
wrapperFile.ArchiveFileInfo = node;
wrapperFile.Name = nodeName;
parentNode.Nodes.Add(wrapperFile);
parentNode = wrapperFile;
}
else
{
folder.Name = nodeName;
parentNode.Nodes.Add(folder);
parentNode = folder;
}
}
else
{
// Node was found, set that as parent and continue
parentNode = parentNode.Nodes[index];
}
}
}
}
public Viewport GetViewport() => viewport;
//Attatch a viewport instance here if created.

View File

@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Switch_Toolbox.Library.Forms;
using Switch_Toolbox.Library.IO;
using System.Text.RegularExpressions;
namespace Switch_Toolbox.Library
{
@ -63,6 +64,86 @@ namespace Switch_Toolbox.Library
public ArchiveFileState State { get; set; } = ArchiveFileState.Empty;
}
public class ArchiveFolderNodeWrapper : TreeNodeCustom
{
public ArchiveFolderNodeWrapper(string text)
{
Text = text;
ContextMenuStrip = new STContextMenuStrip();
ContextMenuStrip.Items.Add(new STToolStripItem("Extract Folder", ExtractAction));
ContextMenuStrip.Items.Add(new STToolStripItem("Replace Folder", ReplaceAction));
}
private void ExtractAction(object sender, EventArgs args)
{
TreeNode node = this;
var ParentPath = string.Empty;
if (node.Parent != null)
{
ParentPath = node.Parent.FullPath;
}
FolderSelectDialog folderDialog = new FolderSelectDialog();
if (folderDialog.ShowDialog() == DialogResult.OK)
{
STProgressBar progressBar = new STProgressBar();
progressBar.Task = "Extracing Files...";
progressBar.Refresh();
progressBar.Value = 0;
progressBar.StartPosition = FormStartPosition.CenterScreen;
progressBar.Show();
var Collection = TreeViewExtensions.Collect(Nodes);
int Curfile = 0;
foreach (TreeNode file in Collection)
{
string FilePath = ((ArchiveNodeWrapper)file).ArchiveFileInfo.FileName;
FilePath = FilePath.Replace(ParentPath, string.Empty);
Console.WriteLine($"FilePath " + FilePath);
var path = Path.Combine(folderDialog.SelectedPath, FilePath);
progressBar.Value = (Curfile++ * 100) / Collection.Count();
progressBar.Refresh();
CreateDirectoryIfExists($"{path}");
if (file is ArchiveNodeWrapper)
{
File.WriteAllBytes($"{path}",
((ArchiveNodeWrapper)file).ArchiveFileInfo.FileData);
}
}
progressBar.Value = 100;
progressBar.Refresh();
progressBar.Close();
}
}
private void ReplaceAction(object sender, EventArgs args)
{
}
private void CreateDirectoryIfExists(string Dir)
{
if (!String.IsNullOrWhiteSpace(Path.GetDirectoryName(Dir)))
{
//Make sure no file names use the same name to prevent errors
if (!File.Exists(Dir))
{
if (!Directory.Exists(Dir))
{
Directory.CreateDirectory(Path.GetDirectoryName(Dir));
}
}
}
}
}
public class ArchiveNodeWrapper : TreeNodeCustom
{
public ArchiveNodeWrapper(string text)