From d62b5ab3a346ca9f92be9e6298aba8edca421092 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Thu, 5 Sep 2019 21:29:43 -0400 Subject: [PATCH] More accurate WTB formatting and support PKZ archives --- .../FileFormats/Archives/PKZ.cs | 133 ++++++++++++++++++ .../FileFormats/Texture/WTB.cs | 22 ++- .../File_Format_Library.csproj | 1 + File_Format_Library/Main.cs | 1 + .../IO/Extensions/StreamExport.cs | 14 ++ 5 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 File_Format_Library/FileFormats/Archives/PKZ.cs diff --git a/File_Format_Library/FileFormats/Archives/PKZ.cs b/File_Format_Library/FileFormats/Archives/PKZ.cs new file mode 100644 index 00000000..59c17925 --- /dev/null +++ b/File_Format_Library/FileFormats/Archives/PKZ.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Threading.Tasks; +using Toolbox; +using System.Windows.Forms; +using Toolbox.Library; +using Toolbox.Library.IO; + +namespace FirstPlugin +{ + public class PKZ : IArchiveFile, IFileFormat, ILeaveOpenOnLoad + { + public FileType FileType { get; set; } = FileType.Archive; + + public bool CanSave { get; set; } + public string[] Description { get; set; } = new string[] { "Platinum Games Archive" }; + public string[] Extension { get; set; } = new string[] { "*.pkz" }; + public string FileName { get; set; } + public string FilePath { get; set; } + public IFileInfo IFileInfo { get; set; } + + public bool CanAddFiles { get; set; } + public bool CanRenameFiles { get; set; } + public bool CanReplaceFiles { get; set; } + public bool CanDeleteFiles { get; set; } + + public bool Identify(System.IO.Stream stream) + { + using (var reader = new Toolbox.Library.IO.FileReader(stream, true)) + { + return reader.CheckSignature(3, "pkz"); + } + } + + public Type[] Types + { + get + { + List types = new List(); + return types.ToArray(); + } + } + + public List files = new List(); + + public IEnumerable Files => files; + + public void ClearFiles() { files.Clear(); } + + private System.IO.Stream _stream; + public void Load(System.IO.Stream stream) + { + _stream = stream; + using (var reader = new FileReader(stream, true)) + { + reader.SetByteOrder(false); + uint magic = reader.ReadUInt32(); + uint unknown = reader.ReadUInt32(); + ulong totalFileSize = reader.ReadUInt64(); + uint fileCount = reader.ReadUInt32(); + uint offsetFileInfo = reader.ReadUInt32(); + ulong stringTableLength = reader.ReadUInt64(); + + uint InfoSize = fileCount * 0x20; + uint StringTablePos = InfoSize + offsetFileInfo; + + reader.SeekBegin(offsetFileInfo); + for (int i = 0; i < fileCount; i++) + { + var file = new FileEntry(); + ulong nameOffset = reader.ReadUInt64(); + file.fileSize = reader.ReadUInt64(); + file.fileOffset = reader.ReadUInt64(); + file.compressedSize = reader.ReadUInt64(); + + using (reader.TemporarySeek((long)nameOffset + StringTablePos, + System.IO.SeekOrigin.Begin)) + { + file.FileName = reader.ReadZeroTerminatedString(); + } + + file.FileDataStream = new SubStream(reader.BaseStream, (long)file.fileOffset, (long)file.compressedSize); + + files.Add(file); + } + } + } + + public void Unload() + { + + } + + public void Save(System.IO.Stream stream) + { + } + + public bool AddFile(ArchiveFileInfo archiveFileInfo) + { + return false; + } + + public bool DeleteFile(ArchiveFileInfo archiveFileInfo) + { + return false; + } + + public class FileEntry : ArchiveFileInfo + { + public ulong fileSize; + public ulong fileOffset; + public ulong compressedSize; + + protected Stream stream; + public override Stream FileDataStream + { + get + { + if (compressedSize != fileSize) + return new MemoryStream(STLibraryCompression.ZSTD.Decompress(stream.ToBytes())); + else + return stream; + } + set + { + stream = value; + } + } + } + } +} diff --git a/File_Format_Library/FileFormats/Texture/WTB.cs b/File_Format_Library/FileFormats/Texture/WTB.cs index faca466a..daeb0fca 100644 --- a/File_Format_Library/FileFormats/Texture/WTB.cs +++ b/File_Format_Library/FileFormats/Texture/WTB.cs @@ -129,10 +129,10 @@ namespace FirstPlugin public uint unknown; public uint ImageSize; public uint Padding; - public uint Format; - public uint MipCount; public uint unknown2; + public uint MipCount; public uint unknown3; + public uint Format; public uint Width; public uint Height; public uint Depth; @@ -175,13 +175,27 @@ namespace FirstPlugin Depth = tex.Info.Depth; ArrayCount = 1; MipCount = tex.Info.MipCount; + // Format = TextureData.ConvertFormat(tex.Info.Format); if (Formats.ContainsKey(tex.Info.Format)) Format = Formats[tex.Info.Format]; } private Dictionary Formats = new Dictionary() { - {0x01, TEX_FORMAT.R8G8_UNORM}, + {0x42 , TEX_FORMAT.BC1_UNORM}, + {0x43, TEX_FORMAT.BC2_UNORM}, + {0x44, TEX_FORMAT.BC3_UNORM}, + {0x45, TEX_FORMAT.BC4_UNORM}, + {0x46 , TEX_FORMAT.BC1_UNORM_SRGB}, + {0x47 , TEX_FORMAT.BC2_UNORM_SRGB}, + {0x48 , TEX_FORMAT.BC3_UNORM_SRGB}, + {0x49, TEX_FORMAT.BC4_SNORM}, + {0x79, TEX_FORMAT.ASTC_4x4_UNORM}, + {0x80, TEX_FORMAT.ASTC_8x8_UNORM}, + {0x87, TEX_FORMAT.ASTC_4x4_SRGB}, + {0x8E, TEX_FORMAT.ASTC_8x8_SRGB}, + + /* {0x01, TEX_FORMAT.R8G8_UNORM}, {0x0d, TEX_FORMAT.R9G9B9E5_SHAREDEXP}, {0x1a, TEX_FORMAT.BC1_UNORM}, {0x1b, TEX_FORMAT.BC2_UNORM}, @@ -202,7 +216,7 @@ namespace FirstPlugin {0x38, TEX_FORMAT.ASTC_8x8_UNORM}, {0x39, TEX_FORMAT.ASTC_12x10_UNORM}, {0x3a, TEX_FORMAT.ASTC_12x12_UNORM}, - {0x3b, TEX_FORMAT.B5G5R5A1_UNORM}, + {0x3b, TEX_FORMAT.B5G5R5A1_UNORM},*/ }; diff --git a/File_Format_Library/File_Format_Library.csproj b/File_Format_Library/File_Format_Library.csproj index 6d135417..d2903341 100644 --- a/File_Format_Library/File_Format_Library.csproj +++ b/File_Format_Library/File_Format_Library.csproj @@ -214,6 +214,7 @@ + diff --git a/File_Format_Library/Main.cs b/File_Format_Library/Main.cs index e3967a87..518e991e 100644 --- a/File_Format_Library/Main.cs +++ b/File_Format_Library/Main.cs @@ -381,6 +381,7 @@ namespace FirstPlugin Formats.Add(typeof(DKCTF.MSBT)); Formats.Add(typeof(DKCTF.PAK)); Formats.Add(typeof(WTB)); + Formats.Add(typeof(PKZ)); // Formats.Add(typeof(MSBP)); // Formats.Add(typeof(BFGRP)); diff --git a/Switch_Toolbox_Library/IO/Extensions/StreamExport.cs b/Switch_Toolbox_Library/IO/Extensions/StreamExport.cs index 178b0f3a..c3c6d8fb 100644 --- a/Switch_Toolbox_Library/IO/Extensions/StreamExport.cs +++ b/Switch_Toolbox_Library/IO/Extensions/StreamExport.cs @@ -9,6 +9,20 @@ namespace Toolbox.Library.IO { public static class StreamExport { + public static byte[] ToBytes(this Stream stream) + { + using (var reader = new FileReader(stream, true)) + { + return reader.ReadBytes((int)stream.Length); + } + + using (var memStream = new MemoryStream()) + { + stream.CopyTo(memStream); + return memStream.ToArray(); + } + } + public static void ExportToFile(this Stream stream, string fileName) { using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))