1
0
mirror of synced 2025-01-31 12:23:52 +01:00

Store the compressed and decompressed sizes

This commit is contained in:
KillzXGaming 2019-05-01 18:08:09 -04:00
parent f78ec57047
commit 84c643d667
11 changed files with 335 additions and 257 deletions

Binary file not shown.

View File

@ -53,10 +53,6 @@ namespace FirstPlugin
public void Load(System.IO.Stream stream)
{
TPFileSizeTable table = new TPFileSizeTable();
table.Read(new FileReader($"{Runtime.TpGamePath}/FileSizeList.txt"));
table.Write(new FileWriter($"{Runtime.TpGamePath}/FileSizeListTEST.txt"));
Text = FileName;
CanSave = true;
@ -81,6 +77,26 @@ namespace FirstPlugin
ContextMenuStrip.Items.Add(new ToolStripMenuItem("Save", null, Save, Keys.Control | Keys.E));
}
private void SatisfyFileSizeTable()
{
uint TotalDecompressedSize = IFileInfo.DecompressedSize;
uint TotalCompressedSize = IFileInfo.CompressedSize;
if (Runtime.TpGamePath != "" && Directory.Exists(Runtime.TpGamePath))
{
}
string TablePath = $"{Runtime.TpGamePath}/FileSizeList.txt";
TPFileSizeTable table = new TPFileSizeTable();
table.Read(new FileReader($"{Runtime.TpGamePath}/FileSizeList.txt"));
var tableSave = new MemoryStream();
table.Write(new FileWriter(tableSave));
File.WriteAllBytes($"{Runtime.TpGamePath}/FileSizeListTEST.txt", tableSave.ToArray());
}
private void Save(object sender, EventArgs args)
{
SaveFileDialog sfd = new SaveFileDialog();
@ -91,6 +107,8 @@ namespace FirstPlugin
if (sfd.ShowDialog() == DialogResult.OK)
{
STFileSaver.SaveFileFormat(this, sfd.FileName);
SatisfyFileSizeTable();
}
}
@ -121,6 +139,8 @@ namespace FirstPlugin
writer.WriteUint32Offset(files[i]._posHeader + 4);
writer.Write(files[i].Data);
}
writer.Close();
writer.Dispose();
}
private void SetAlignment(FileWriter writer, string FileName)

View File

@ -20,259 +20,6 @@ namespace Switch_Toolbox.Library.IO
uint64,
}
public class STFileSaver
{
/// <summary>
/// Saves the <see cref="IFileFormat"/> as a file from the given <param name="FileName">
/// </summary>
/// <param name="IFileFormat">The format instance of the file being saved</param>
/// <param name="FileName">The name of the file</param>
/// <param name="Alignment">The Alignment used for compression. Used for Yaz0 compression type. </param>
/// <param name="EnableDialog">Toggle for showing compression dialog</param>
/// <returns></returns>
public static void SaveFileFormat(IFileFormat FileFormat, string FileName, bool EnableDialog = true)
{
Cursor.Current = Cursors.WaitCursor;
FileFormat.FilePath = FileName;
SaveFileFormat(FileFormat.Save(), FileFormat.IFileInfo.FileIsCompressed, FileFormat.IFileInfo.Alignment,
FileFormat.IFileInfo.CompressionType, FileName, EnableDialog);
}
public static void SaveFileFormat(byte[] data, bool FileIsCompressed, int Alignment,
CompressionType CompressionType, string FileName, bool EnableDialog = true)
{
string extension = Path.GetExtension(FileName);
if (extension == ".szs" || extension == ".sbfres")
{
FileIsCompressed = true;
CompressionType = CompressionType.Yaz0;
}
if (extension == ".cmp")
{
FileIsCompressed = true;
CompressionType = CompressionType.Lz4f;
}
if (EnableDialog && FileIsCompressed)
{
DialogResult save = MessageBox.Show($"Compress file with {CompressionType}?", "File Save", MessageBoxButtons.YesNo);
if (save == DialogResult.Yes)
{
switch (CompressionType)
{
case CompressionType.Yaz0:
data = EveryFileExplorer.YAZ0.Compress(data, Runtime.Yaz0CompressionLevel, (uint)Alignment);
break;
case CompressionType.Zstb:
data = STLibraryCompression.ZSTD.Compress(data);
break;
case CompressionType.Lz4:
data = STLibraryCompression.Type_LZ4.Compress(data);
break;
case CompressionType.Lz4f:
data = STLibraryCompression.Type_LZ4F.Compress(data);
break;
case CompressionType.Gzip:
data = STLibraryCompression.GZIP.Compress(data);
break;
case CompressionType.Zlib:
data = STLibraryCompression.ZLIB.Compress(data, 2);
break;
default:
MessageBox.Show($"Compression Type {CompressionType} not supported!!");
break;
}
}
}
File.WriteAllBytes(FileName, data);
MessageBox.Show($"File has been saved to {FileName}");
Cursor.Current = Cursors.Default;
}
}
public class STFileLoader
{
/// <summary>
/// Gets the <see cref="TreeNodeFile"/> from a file or byte array.
/// </summary>
/// <param name="FileName">The name of the file</param>
/// <param name="data">The byte array of the data</param>
/// <param name="InArchive">If the file is in an archive so it can be saved back</param>
/// <param name="archiveNode">The node being replaced from an archive</param>
/// <param name="ArchiveHash">The unique hash from an archive for saving</param>
/// <param name="Compressed">If the file is being compressed or not</param>
/// <param name="CompType">The type of <see cref="CompressionType"/> being used</param>
/// <returns></returns>
public static TreeNode GetNodeFileFormat(string FileName, byte[] data = null, bool InArchive = false,
TreeNode archiveNode = null, bool LeaveStreamOpen = false, bool Compressed = false, CompressionType CompType = 0)
{
IFileFormat format = OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode);
if (format is TreeNode)
return (TreeNode)format;
else
return null;
}
/// <summary>
/// Gets the <see cref="IFileFormat"/> from a file or byte array.
/// </summary>
/// <param name="FileName">The name of the file</param>
/// <param name="data">The byte array of the data</param>
/// <param name="InArchive">If the file is in an archive so it can be saved back</param>
/// <param name="archiveNode">The node being replaced from an archive</param>
/// <param name="Compressed">If the file is being compressed or not</param>
/// <param name="CompType">The type of <see cref="CompressionType"/> being used</param>
/// <returns></returns>
public static IFileFormat OpenFileFormat(string FileName, byte[] data = null, bool LeaveStreamOpen = false, bool InArchive = false,
TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0)
{
FileReader fileReader;
if (data != null)
fileReader = new FileReader(data);
else
fileReader = new FileReader(FileName);
if (fileReader.BaseStream.Length <= 4)
{
fileReader.Close();
fileReader.Dispose();
return null;
}
Cursor.Current = Cursors.WaitCursor;
fileReader.ByteOrder = ByteOrder.BigEndian;
uint MagicHex = fileReader.ReadUInt32();
string Magic = fileReader.ReadMagic(0, 4);
fileReader.Position = 0;
if (Magic == "Yaz0")
{
if (data != null)
data = EveryFileExplorer.YAZ0.Decompress(data);
else
data = EveryFileExplorer.YAZ0.Decompress(FileName);
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true, CompressionType.Yaz0);
}
if (MagicHex == 0x28B52FFD || MagicHex == 0xFD2FB528)
{
data = STLibraryCompression.ZSTD.Decompress(fileReader.getSection(0, data.Length));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true, CompressionType.Zstb);
}
if (Path.GetExtension(FileName) == ".z" && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.ZLIB.Decompress(fileReader.getSection(2, data.Length - 2));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true, CompressionType.Zlib);
}
if (Magic == "ZLIB")
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.GZIP.Decompress(fileReader.getSection(64, data.Length - 64));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true, CompressionType.Zlib);
}
if (MagicHex == 0x1f8b0808 && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.GZIP.Decompress(data);
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true, CompressionType.Gzip);
}
if (Path.GetExtension(FileName) == ".lz" && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.LZ77.Decompress(fileReader.getSection(16, data.Length - 16));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true, CompressionType.Zlib);
}
if (Path.GetExtension(FileName) == ".cmp" && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
fileReader.Position = 0;
int OuSize = fileReader.ReadInt32();
int InSize = data.Length - 4;
data = STLibraryCompression.Type_LZ4F.Decompress(fileReader.getSection(4, InSize));
return OpenFileFormat(FileName, data, InArchive, LeaveStreamOpen, archiveNode, true, CompressionType.Lz4f);
}
fileReader.Dispose();
fileReader.Close();
Stream stream;
if (data != null)
stream = new MemoryStream(data);
else
stream = File.OpenRead(FileName);
foreach (IFileFormat fileFormat in FileManager.GetFileFormats())
{
//Set the file name so we can check it's extension in the identifier.
//Most is by magic but some can be extension or name.
fileFormat.FileName = Path.GetFileName(FileName);
fileFormat.IFileInfo = new IFileInfo();
if (fileFormat.Identify(stream))
{
return SetFileFormat(fileFormat, FileName, stream, LeaveStreamOpen, InArchive, archiveNode, Compressed, CompType);
}
}
return null;
}
private static IFileFormat SetFileFormat(IFileFormat fileFormat, string FileName, Stream stream, bool LeaveStreamOpen = false, bool InArchive = false,
TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0)
{
fileFormat.IFileInfo.CompressionType = CompType;
fileFormat.IFileInfo.FileIsCompressed = Compressed;
fileFormat.FileName = Path.GetFileName(FileName);
fileFormat.FilePath = FileName;
fileFormat.IFileInfo.InArchive = InArchive;
fileFormat.IFileInfo.FileIsCompressed = Compressed;
if (Compressed)
fileFormat.IFileInfo.CompressionType = CompType;
fileFormat.Load(stream);
if (fileFormat is TreeNode)
{
if (archiveNode != null)
{
((TreeNode)fileFormat).Text = archiveNode.Text;
((TreeNode)fileFormat).ImageKey = archiveNode.ImageKey;
((TreeNode)fileFormat).SelectedImageKey = archiveNode.SelectedImageKey;
}
}
//After file has been loaded and read, we'll dispose unless left open
if (!LeaveStreamOpen)
{
// stream.Close();
// stream.Dispose();
}
return fileFormat;
}
}
public class STLibraryCompression
{
public static byte[] CompressFile(byte[] data, IFileFormat format)

View File

@ -0,0 +1,205 @@
using Syroot.BinaryData;
using System.IO;
using System.IO.Compression;
using OpenTK;
using System.Windows.Forms;
namespace Switch_Toolbox.Library.IO
{
public class STFileLoader
{
/// <summary>
/// Gets the <see cref="TreeNodeFile"/> from a file or byte array.
/// </summary>
/// <param name="FileName">The name of the file</param>
/// <param name="data">The byte array of the data</param>
/// <param name="InArchive">If the file is in an archive so it can be saved back</param>
/// <param name="archiveNode">The node being replaced from an archive</param>
/// <param name="ArchiveHash">The unique hash from an archive for saving</param>
/// <param name="Compressed">If the file is being compressed or not</param>
/// <param name="CompType">The type of <see cref="CompressionType"/> being used</param>
/// <returns></returns>
public static TreeNode GetNodeFileFormat(string FileName, byte[] data = null, bool InArchive = false,
TreeNode archiveNode = null, bool LeaveStreamOpen = false, bool Compressed = false, CompressionType CompType = 0)
{
IFileFormat format = OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode);
if (format is TreeNode)
return (TreeNode)format;
else
return null;
}
/// <summary>
/// Gets the <see cref="IFileFormat"/> from a file or byte array.
/// </summary>
/// <param name="FileName">The name of the file</param>
/// <param name="data">The byte array of the data</param>
/// <param name="InArchive">If the file is in an archive so it can be saved back</param>
/// <param name="archiveNode">The node being replaced from an archive</param>
/// <param name="Compressed">If the file is being compressed or not</param>
/// <param name="CompType">The type of <see cref="CompressionType"/> being used</param>
/// <returns></returns>
public static IFileFormat OpenFileFormat(string FileName, byte[] data = null, bool LeaveStreamOpen = false, bool InArchive = false,
TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0, uint DecompressedSize = 0, uint CompressedSize = 0)
{
uint DecompressedFileSize = 0;
uint CompressedFileSize = 0;
FileReader fileReader;
if (data != null)
fileReader = new FileReader(data);
else
fileReader = new FileReader(FileName);
if (CompType == CompressionType.None)
DecompressedFileSize = (uint)fileReader.BaseStream.Length;
if (CompType != CompressionType.None)
CompressedFileSize = (uint)fileReader.BaseStream.Length;
if (fileReader.BaseStream.Length <= 4)
{
fileReader.Close();
fileReader.Dispose();
return null;
}
Cursor.Current = Cursors.WaitCursor;
fileReader.ByteOrder = ByteOrder.BigEndian;
uint MagicHex = fileReader.ReadUInt32();
string Magic = fileReader.ReadMagic(0, 4);
fileReader.Position = 0;
if (Magic == "Yaz0")
{
if (data != null)
data = EveryFileExplorer.YAZ0.Decompress(data);
else
data = EveryFileExplorer.YAZ0.Decompress(FileName);
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true,
CompressionType.Yaz0, DecompressedFileSize, CompressedFileSize);
}
if (MagicHex == 0x28B52FFD || MagicHex == 0xFD2FB528)
{
data = STLibraryCompression.ZSTD.Decompress(fileReader.getSection(0, data.Length));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true,
CompressionType.Zstb, DecompressedFileSize, CompressedFileSize);
}
if (Path.GetExtension(FileName) == ".z" && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.ZLIB.Decompress(fileReader.getSection(2, data.Length - 2));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true,
CompressionType.Zlib, DecompressedFileSize, CompressedFileSize);
}
if (Magic == "ZLIB")
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.GZIP.Decompress(fileReader.getSection(64, data.Length - 64));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true,
CompressionType.Zlib, DecompressedFileSize, CompressedFileSize);
}
if (MagicHex == 0x1f8b0808 && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.GZIP.Decompress(data);
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true,
CompressionType.Gzip, DecompressedFileSize, CompressedFileSize);
}
if (Path.GetExtension(FileName) == ".lz" && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
data = STLibraryCompression.LZ77.Decompress(fileReader.getSection(16, data.Length - 16));
return OpenFileFormat(FileName, data, LeaveStreamOpen, InArchive, archiveNode, true,
CompressionType.Zlib, DecompressedFileSize, CompressedFileSize);
}
if (Path.GetExtension(FileName) == ".cmp" && CompType == CompressionType.None)
{
if (data == null)
data = File.ReadAllBytes(FileName);
fileReader.Position = 0;
int OuSize = fileReader.ReadInt32();
int InSize = data.Length - 4;
data = STLibraryCompression.Type_LZ4F.Decompress(fileReader.getSection(4, InSize));
return OpenFileFormat(FileName, data, InArchive, LeaveStreamOpen, archiveNode, true,
CompressionType.Lz4f, DecompressedFileSize, CompressedFileSize);
}
fileReader.Dispose();
fileReader.Close();
Stream stream;
if (data != null)
stream = new MemoryStream(data);
else
stream = File.OpenRead(FileName);
foreach (IFileFormat fileFormat in FileManager.GetFileFormats())
{
//Set the file name so we can check it's extension in the identifier.
//Most is by magic but some can be extension or name.
fileFormat.FileName = Path.GetFileName(FileName);
fileFormat.IFileInfo = new IFileInfo();
if (fileFormat.Identify(stream))
{
fileFormat.IFileInfo.DecompressedSize = DecompressedFileSize;
fileFormat.IFileInfo.CompressedSize = CompressedFileSize;
return SetFileFormat(fileFormat, FileName, stream, LeaveStreamOpen, InArchive, archiveNode, Compressed, CompType);
}
}
return null;
}
private static IFileFormat SetFileFormat(IFileFormat fileFormat, string FileName, Stream stream, bool LeaveStreamOpen = false, bool InArchive = false,
TreeNode archiveNode = null, bool Compressed = false, CompressionType CompType = 0)
{
fileFormat.IFileInfo.CompressionType = CompType;
fileFormat.IFileInfo.FileIsCompressed = Compressed;
fileFormat.FileName = Path.GetFileName(FileName);
fileFormat.FilePath = FileName;
fileFormat.IFileInfo.InArchive = InArchive;
fileFormat.IFileInfo.FileIsCompressed = Compressed;
if (Compressed)
fileFormat.IFileInfo.CompressionType = CompType;
fileFormat.Load(stream);
if (fileFormat is TreeNode)
{
if (archiveNode != null)
{
((TreeNode)fileFormat).Text = archiveNode.Text;
((TreeNode)fileFormat).ImageKey = archiveNode.ImageKey;
((TreeNode)fileFormat).SelectedImageKey = archiveNode.SelectedImageKey;
}
}
//After file has been loaded and read, we'll dispose unless left open
if (!LeaveStreamOpen)
{
// stream.Close();
// stream.Dispose();
}
return fileFormat;
}
}
}

View File

@ -0,0 +1,102 @@
using Syroot.BinaryData;
using System.IO;
using System.IO.Compression;
using OpenTK;
using System.Windows.Forms;
namespace Switch_Toolbox.Library.IO
{
public class STFileSaver
{
/// <summary>
/// Saves the <see cref="IFileFormat"/> as a file from the given <param name="FileName">
/// </summary>
/// <param name="IFileFormat">The format instance of the file being saved</param>
/// <param name="FileName">The name of the file</param>
/// <param name="Alignment">The Alignment used for compression. Used for Yaz0 compression type. </param>
/// <param name="EnableDialog">Toggle for showing compression dialog</param>
/// <returns></returns>
public static void SaveFileFormat(IFileFormat FileFormat, string FileName, bool EnableDialog = true)
{
Cursor.Current = Cursors.WaitCursor;
FileFormat.FilePath = FileName;
byte[] data = FileFormat.Save();
FileFormat.IFileInfo.DecompressedSize = (uint)data.Length;
CompressFileFormat(data,
FileFormat.IFileInfo.FileIsCompressed,
FileFormat.IFileInfo.Alignment,
FileFormat.IFileInfo.CompressionType,
FileName,
EnableDialog);
FileFormat.IFileInfo.CompressedSize = (uint)data.Length;
File.WriteAllBytes(FileName, data);
MessageBox.Show($"File has been saved to {FileName}");
Cursor.Current = Cursors.Default;
}
public static void SaveFileFormat(byte[] data, bool FileIsCompressed, int Alignment,
CompressionType CompressionType, string FileName, bool EnableDialog = true)
{
Cursor.Current = Cursors.WaitCursor;
CompressFileFormat(data, FileIsCompressed, Alignment, CompressionType, FileName, EnableDialog);
File.WriteAllBytes(FileName, data);
MessageBox.Show($"File has been saved to {FileName}");
Cursor.Current = Cursors.Default;
}
private static void CompressFileFormat(byte[] data, bool FileIsCompressed, int Alignment,
CompressionType CompressionType, string FileName, bool EnableDialog = true)
{
string extension = Path.GetExtension(FileName);
if (extension == ".szs" || extension == ".sbfres")
{
FileIsCompressed = true;
CompressionType = CompressionType.Yaz0;
}
if (extension == ".cmp")
{
FileIsCompressed = true;
CompressionType = CompressionType.Lz4f;
}
if (EnableDialog && FileIsCompressed)
{
DialogResult save = MessageBox.Show($"Compress file with {CompressionType}?", "File Save", MessageBoxButtons.YesNo);
if (save == DialogResult.Yes)
{
switch (CompressionType)
{
case CompressionType.Yaz0:
data = EveryFileExplorer.YAZ0.Compress(data, Runtime.Yaz0CompressionLevel, (uint)Alignment);
break;
case CompressionType.Zstb:
data = STLibraryCompression.ZSTD.Compress(data);
break;
case CompressionType.Lz4:
data = STLibraryCompression.Type_LZ4.Compress(data);
break;
case CompressionType.Lz4f:
data = STLibraryCompression.Type_LZ4F.Compress(data);
break;
case CompressionType.Gzip:
data = STLibraryCompression.GZIP.Compress(data);
break;
case CompressionType.Zlib:
data = STLibraryCompression.ZLIB.Compress(data, 2);
break;
default:
MessageBox.Show($"Compression Type {CompressionType} not supported!!");
break;
}
}
}
}
}
}

View File

@ -37,5 +37,7 @@ namespace Switch_Toolbox.Library
public virtual bool IsActive { get; set; }
public virtual bool InArchive { get; set; }
public virtual int Alignment { get; set; } //Alignment to save the file back. Also used for Yaz0 comp sometimes
public virtual uint DecompressedSize { get; set; }
public virtual uint CompressedSize { get; set; }
}
}

View File

@ -524,6 +524,8 @@
<Compile Include="IO\FileReader.cs" />
<Compile Include="IO\FileWriter.cs" />
<Compile Include="IO\Matrix4x4.cs" />
<Compile Include="IO\STFileLoader.cs" />
<Compile Include="IO\STFileSaver.cs" />
<Compile Include="IO\StringExtension.cs" />
<Compile Include="IO\STStream.cs" />
<Compile Include="Plugin\GenericPluginLoader.cs" />