1
0
mirror of synced 2024-12-03 19:47:29 +01:00
Switch-Toolbox/Switch_Toolbox_Library/Compression/CompressionMenus.cs
KillzXGaming 0c126e4155 More improvements.
Rewrote the compression handling from scatch. It's way easier and cleaner to add new formats code wise as it's handled like file formats.
Added wip TVOL support (Touhou Azure Reflections)
Added XCI support. Note I plan to improve NSP, XCI, NCA, etc later for exefs exporting.
The compression rework now compresses via streams, so files get decompressed properly within archives as streams.
Added hyrule warriors bin.gz compression along with archive rebuilding. Note i do not have texture rebuilding done just yet.
2019-09-15 19:13:01 -04:00

146 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Toolbox.Library.IO
{
public class CompressionMenus
{
public static List<ToolStripMenuItem> GetMenuItems()
{
CompressionMenus menus = new CompressionMenus();
return menus.MenuItems();
}
private List<ToolStripMenuItem> MenuItems()
{
var items = new List<ToolStripMenuItem>();
items.Add(new ToolStripMenuItem("Yaz0"));
items.Add(new ToolStripMenuItem("Gzip"));
items.Add(new ToolStripMenuItem("lZMA"));
items.Add(new ToolStripMenuItem("lZ4"));
items.Add(new ToolStripMenuItem("lZ4F"));
items.Add(new ToolStripMenuItem("ZSTD"));
items.Add(new ToolStripMenuItem("ZLIB"));
items.Add(new ToolStripMenuItem("ZLIB_GZ (Hyrule Warriors)"));
SetFunctions(items);
return items;
}
private void SetFunctions(List<ToolStripMenuItem> items)
{
foreach (var item in items)
item.DropDownItems.AddRange(FunctionItems());
}
private ToolStripItem[] FunctionItems()
{
var items = new List<ToolStripItem>();
items.Add(new ToolStripMenuItem("Decompress", null, Decompress));
items.Add(new ToolStripMenuItem("Compress", null, Compress));
return items.ToArray();
}
private void Decompress(object sender, EventArgs e)
{
ToolStripMenuItem curMenu = sender as ToolStripMenuItem;
ToolStrip toolStrip = curMenu.GetCurrentParent();
var itemCheck = (curMenu.GetCurrentParent() as ToolStripDropDown).OwnerItem;
SetToolStripFunctions(itemCheck.Text, false);
}
private void Compress(object sender, EventArgs e)
{
ToolStripMenuItem curMenu = sender as ToolStripMenuItem;
ToolStrip toolStrip = curMenu.GetCurrentParent();
var itemCheck = (curMenu.GetCurrentParent() as ToolStripDropDown).OwnerItem;
SetToolStripFunctions(itemCheck.Text, true);
}
private void SetToolStripFunctions(string Name, bool Compress)
{
if (Name == "Yaz0")
OpenFileForCompression(new Yaz0(), Compress);
else if (Name == "Gzip")
OpenFileForCompression(new Gzip(), Compress);
else if (Name == "lZMA")
OpenFileForCompression(new LZMA(), Compress);
else if (Name == "lZ4")
OpenFileForCompression(new lz4(), Compress);
else if (Name == "lZ4F")
OpenFileForCompression(new LZ4F(), Compress);
else if (Name == "ZSTD")
OpenFileForCompression(new Zstb(), Compress);
else if (Name == "ZLIB")
OpenFileForCompression(new Zlib(), Compress);
else if (Name.Contains("ZLIB_GZ"))
OpenFileForCompression(new ZlibGZ(), Compress);
else throw new Exception("Unimplimented Type! " + Name);
}
public void CompressData(ICompressionFormat CompressionFormat, Stream data)
{
try
{
SaveFileForCompression(true, data, CompressionFormat);
}
catch (Exception ex)
{
MessageBox.Show($"File failed to compress with {CompressionFormat} compression! {ex.ToString()}");
}
}
public void DecompressData(ICompressionFormat CompressionFormat, Stream data)
{
try
{
SaveFileForCompression(false, data, CompressionFormat);
}
catch (Exception ex)
{
MessageBox.Show($"File not compressed with {CompressionFormat} compression! {ex.ToString()}");
}
}
private void OpenFileForCompression(ICompressionFormat compressionFormat, bool Compress)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
Cursor.Current = Cursors.WaitCursor;
foreach (string file in ofd.FileNames)
{
if (Compress)
CompressData(compressionFormat, File.OpenRead(ofd.FileName));
else
DecompressData(compressionFormat, File.OpenRead(ofd.FileName));
}
}
}
private void SaveFileForCompression(bool Compress, Stream data, ICompressionFormat compressionFormat)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All files(*.*)|*.*";
Cursor.Current = Cursors.Default;
if (sfd.ShowDialog() == DialogResult.OK)
{
if (Compress)
compressionFormat.Compress(data).ExportToFile(sfd.FileName);
else
compressionFormat.Decompress(data).ExportToFile(sfd.FileName);
MessageBox.Show($"File has been saved to {sfd.FileName}", "Save Notification");
}
}
}
}