Skip to content

Commit

Permalink
Improved file extraction code
Browse files Browse the repository at this point in the history
  • Loading branch information
MattN-L committed Mar 5, 2024
1 parent 89c2905 commit f40b813
Showing 1 changed file with 70 additions and 51 deletions.
121 changes: 70 additions & 51 deletions PCK-Studio/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,74 +555,93 @@ private void selectNode(object sender, TreeViewEventArgs e)
}
}

private void extractFile(string outFilePath, PckFileData file)
{
File.WriteAllBytes(outFilePath, file.Data);
if (file.Properties.Count > 0)
{
using var fs = File.CreateText($"{outFilePath}.txt");
file.Properties.ForEach(property => fs.WriteLine($"{property.Key}: {property.Value}"));
}
}

private void extractFolderFile(string outPath, PckFileData file)
{
TreeNode node = treeViewMain.SelectedNode;

string abbPath = Path.GetDirectoryName(file.Filename);
Console.WriteLine(abbPath.IndexOf(node.Text));
Console.WriteLine(abbPath.Length);
abbPath = abbPath.Substring(abbPath.IndexOf(node.Text), abbPath.Length - abbPath.IndexOf(node.Text));
string finalPath = ($"{outPath}/{abbPath}/").Replace('\\', '/');
Console.WriteLine(finalPath);

if (!Directory.Exists(finalPath)) Directory.CreateDirectory(finalPath);

extractFile(finalPath + "/" + Path.GetFileName(file.Filename), file);
}

private void extractFolder(string outPath)
{
TreeNode node = treeViewMain.SelectedNode;

string selectedFolder = node.FullPath;

if (IsSubPCKNode(node.FullPath) && node.Tag == null)
{
GetAllChildNodes(node.Nodes).ForEach(fileNode =>
{
if (fileNode.TryGetTagData(out PckFileData file))
{
extractFolderFile(outPath, file);
}
}
);
}
else
{
foreach (var _file in currentPCK.GetFiles())
{
if (_file.Filename.StartsWith(selectedFolder))
{
extractFolderFile(outPath, _file);
}
};
}
MessageBox.Show("Folder Extracted");
}

private void extractToolStripMenuItem_Click(object sender, EventArgs e)
{
var node = treeViewMain.SelectedNode;

if (node == null)
return;
if (node.TryGetTagData(out PckFileData file))

if (node.Tag == null)
{
Console.WriteLine("FOLDER");

OpenFolderDialog dialog = new OpenFolderDialog();
dialog.Title = @"Select destination folder";

if (dialog.ShowDialog(this.Handle) == true) extractFolder(dialog.ResultPath);
}
else if (node.TryGetTagData(out PckFileData file))
{
using SaveFileDialog exFile = new SaveFileDialog();
exFile.FileName = Path.GetFileName(file.Filename);
exFile.Filter = Path.GetExtension(file.Filename).Replace(".", string.Empty) + " File|*" + Path.GetExtension(file.Filename);
if (exFile.ShowDialog() != DialogResult.OK ||
// Makes sure chosen directory isn't null or whitespace AKA makes sure its usable
string.IsNullOrWhiteSpace(Path.GetDirectoryName(exFile.FileName))) return;
string extractFilePath = exFile.FileName;

File.WriteAllBytes(extractFilePath, file.Data);
if (file.Properties.Count > 0)
{
using var fs = File.CreateText($"{extractFilePath}.txt");
file.Properties.ForEach(property => fs.WriteLine($"{property.Key}: {property.Value}"));
}
extractFile(exFile.FileName, file);

// Verification that file extraction path was successful
MessageBox.Show("File Extracted");
return;
}

string selectedFolder = node.FullPath;
OpenFolderDialog dialog = new OpenFolderDialog();
dialog.Title = @"Select destination folder";

if (dialog.ShowDialog() == true)
{
string extractPath = dialog.ResultPath;
if (IsSubPCKNode(node.FullPath) && node.Tag == null)
{
GetAllChildNodes(node.Nodes).ForEach(fileNode =>
{
if (fileNode.TryGetTagData(out PckFileData file))
{
Directory.CreateDirectory($"{extractPath}/{Path.GetDirectoryName(file.Filename)}");
File.WriteAllBytes($"{extractPath}/{file.Filename}", file.Data);
if (file.Properties.Count > 0)
{
using var fs = File.CreateText($"{extractPath}/{file.Filename}.txt");
file.Properties.ForEach(property => fs.WriteLine($"{property.Key}: {property.Value}"));
}
}
}
);
}
else
{
foreach (var _file in currentPCK.GetFiles())
{
if (_file.Filename.StartsWith(selectedFolder))
{
Directory.CreateDirectory($"{extractPath}/{Path.GetDirectoryName(_file.Filename)}");
File.WriteAllBytes($"{extractPath}/{_file.Filename}", _file.Data);
if (_file.Properties.Count > 0)
{
using var fs = File.CreateText($"{extractPath}/{_file.Filename}.txt");
_file.Properties.ForEach(property => fs.WriteLine($"{property.Key}: {property.Value}"));
}
}
};
}
MessageBox.Show("Folder Extracted");
}
}

private void SaveTemplate()
Expand Down

0 comments on commit f40b813

Please sign in to comment.