More accurate WTB formatting and support PKZ archives

This commit is contained in:
KillzXGaming 2019-09-05 21:29:43 -04:00
parent 4bb6e50601
commit d62b5ab3a3
5 changed files with 167 additions and 4 deletions

View file

@ -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<Type> types = new List<Type>();
return types.ToArray();
}
}
public List<FileEntry> files = new List<FileEntry>();
public IEnumerable<ArchiveFileInfo> 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;
}
}
}
}
}

View file

@ -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<uint, TEX_FORMAT> Formats = new Dictionary<uint, TEX_FORMAT>()
{
{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},*/
};

View file

@ -214,6 +214,7 @@
<Compile Include="FileFormats\Archives\ARC.cs" />
<Compile Include="FileFormats\Archives\GFA.cs" />
<Compile Include="FileFormats\Archives\LM2\LM2_Material.cs" />
<Compile Include="FileFormats\Archives\PKZ.cs" />
<Compile Include="FileFormats\Audio\Archives\AudioCommon.cs" />
<Compile Include="FileFormats\DKCTF\Common.cs" />
<Compile Include="FileFormats\DKCTF\MSBT.cs" />

View file

@ -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));

View file

@ -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))