botw-toolset/Control/TabYaz0.xaml.cs
2021-06-04 20:38:35 -07:00

129 lines
4.6 KiB
C#

using BOTWToolset.Debugging;
using BOTWToolset.Exceptions;
using BOTWToolset.IO.Yaz0;
using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Windows.Controls;
namespace BOTWToolset.Control
{
/// <summary>
/// Interaction logic for the Yaz0 tab.
/// </summary>
public partial class TabYaz0 : UserControl
{
public TabYaz0()
{
InitializeComponent();
}
private void Menu_FileDecode(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog
{
InitialDirectory = Dashboard.UserDesktop,
RestoreDirectory = true,
Title = "Select Yaz0 file",
DefaultExt = "yaz0",
Filter = "All Files (*.*)|*.*",
CheckFileExists = true
};
if ((bool)openFileDialog.ShowDialog())
{
try
{
byte[] decoded = Yaz0.Decompress(File.ReadAllBytes(openFileDialog.FileName));
// If decoding at source is checked, don't open a file save picker
if ((bool)Yaz0DecodeAtSource.IsChecked)
{
string decoded_filename = openFileDialog.FileName;
string ext = Path.GetExtension(openFileDialog.FileName);
// Replace extension when decoding
if (ext.StartsWith(".s"))
{
ext = ext.Replace(".s", ".");
decoded_filename = Path.ChangeExtension(decoded_filename, ext);
}
File.WriteAllBytes(decoded_filename, decoded);
}
else
{
var saveFileDialog = new SaveFileDialog
{
InitialDirectory = Dashboard.UserDesktop,
RestoreDirectory = true,
Title = "Save decoded Yaz0 file",
Filter = "All Files (*.*)|*.*"
};
if ((bool)saveFileDialog.ShowDialog())
{
File.WriteAllBytes(saveFileDialog.FileName, decoded);
}
}
BOTWConsole.LogStatus("Yaz0 file successfully decoded.");
}
catch (InvalidMagicException err)
{
BOTWConsole.LogStatus(err.Message);
}
}
}
private void Menu_FileEncode(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog
{
InitialDirectory = Dashboard.UserDesktop,
RestoreDirectory = true,
Title = "Select file to Yaz0-encode",
Filter = "All Files (*.*)|*.*",
CheckFileExists = true
};
if ((bool)openFileDialog.ShowDialog())
{
try
{
byte[] encoded = Yaz0.Compress(File.ReadAllBytes(openFileDialog.FileName));
// If decoding at source is checked, don't open a file save picker
if ((bool)Yaz0DecodeAtSource.IsChecked)
{
// Replace extension when encoding
string ext = Path.GetExtension(openFileDialog.FileName).Replace(".", ".s");
string encoded_filename = Path.ChangeExtension(openFileDialog.FileName, ext);
File.WriteAllBytes(encoded_filename, encoded);
}
else
{
var saveFileDialog = new SaveFileDialog
{
InitialDirectory = Dashboard.UserDesktop,
RestoreDirectory = true,
Title = "Save encoded Yaz0 file",
Filter = "All Files (*.*)|*.*"
};
if ((bool)saveFileDialog.ShowDialog())
{
File.WriteAllBytes(saveFileDialog.FileName, encoded);
}
}
BOTWConsole.LogStatus("Yaz0 file successfully encoded.");
}
catch (InvalidMagicException err)
{
BOTWConsole.LogStatus(err.Message);
}
}
}
}
}