diff --git a/FtexTool/Ftex/Enum/FtexTextureType.cs b/FtexTool/Ftex/Enum/FtexTextureType.cs index 94d764f..52ac9a6 100644 --- a/FtexTool/Ftex/Enum/FtexTextureType.cs +++ b/FtexTool/Ftex/Enum/FtexTextureType.cs @@ -2,12 +2,13 @@ { public enum FtexTextureType { - // SRM and MTM files - // Each channel acts as a material parameter - // R = Ambient Occlusion - // G = Specular Albedo - // B = Roughness /// + /// SRM and MTM files + /// SRM files: + /// Each channel acts as a material parameter + /// R = Ambient Occlusion + /// G = Specular Albedo + /// B = Roughness /// MaterialMap = 0x01000001, diff --git a/FtexTool/FtexDdsConverter.cs b/FtexTool/FtexDdsConverter.cs index 5c0cac0..9197aca 100644 --- a/FtexTool/FtexDdsConverter.cs +++ b/FtexTool/FtexDdsConverter.cs @@ -127,9 +127,7 @@ private static List GetFtexsChunks(FtexFileMipMapInfo mipMapInfo int chunkSize = Math.Min(mipMapData.Length - mipMapDataOffset, maxChunkSize); byte[] chunkData = new byte[chunkSize]; Array.Copy(mipMapData, mipMapDataOffset, chunkData, 0, chunkSize); - chunk.ChunkData = chunkData; - chunk.CompressedChunkSize = Convert.ToInt16(chunkSize); - chunk.DecompressedChunkSize = Convert.ToInt16(chunkSize); + chunk.SetData(chunkData, false); ftexsFileChunks.Add(chunk); mipMapDataOffset += chunkSize; } diff --git a/FtexTool/Ftexs/FtexsFileChunk.cs b/FtexTool/Ftexs/FtexsFileChunk.cs index 8c0b000..ea8e61e 100644 --- a/FtexTool/Ftexs/FtexsFileChunk.cs +++ b/FtexTool/Ftexs/FtexsFileChunk.cs @@ -8,10 +8,20 @@ public class FtexsFileChunk { public const int IndexSize = 8; private const int OffsetBitMask = 0xFFFF; - public short CompressedChunkSize { get; set; } - public short DecompressedChunkSize { get; set; } + + public short CompressedChunkSize + { + get { return Convert.ToInt16(CompressedChunkData.Length); } + } + + public short ChunkSize + { + get { return Convert.ToInt16(ChunkData.Length); } + } + public uint Offset { get; set; } - public byte[] ChunkData { get; set; } + public byte[] ChunkData { get; private set; } + public byte[] CompressedChunkData { get; private set; } public static FtexsFileChunk ReadFtexsFileChunk(Stream inputStream, bool absoluteOffset) { @@ -23,8 +33,8 @@ public static FtexsFileChunk ReadFtexsFileChunk(Stream inputStream, bool absolut public void Read(Stream inputStream, bool absoluteOffset) { BinaryReader reader = new BinaryReader(inputStream, Encoding.Default, true); - CompressedChunkSize = reader.ReadInt16(); - DecompressedChunkSize = reader.ReadInt16(); + short compressedChunkSize = reader.ReadInt16(); + short decompressedChunkSize = reader.ReadInt16(); Offset = reader.ReadUInt32(); long indexEndPosition = reader.BaseStream.Position; @@ -39,11 +49,9 @@ public void Read(Stream inputStream, bool absoluteOffset) reader.BaseStream.Position = indexEndPosition + (Offset & OffsetBitMask) - IndexSize; } - byte[] data = reader.ReadBytes(CompressedChunkSize); - ChunkData = CompressedChunkSize == DecompressedChunkSize - ? data - : ZipUtility.Inflate(data); - + byte[] data = reader.ReadBytes(compressedChunkSize); + bool dataCompressed = compressedChunkSize != decompressedChunkSize; + SetData(data, dataCompressed); reader.BaseStream.Position = indexEndPosition; } @@ -51,16 +59,35 @@ public void Write(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); writer.Write(CompressedChunkSize); - writer.Write(DecompressedChunkSize); + writer.Write(ChunkSize); writer.Write(Offset); } - public void WriteData(Stream outputStream) + public void WriteData(Stream outputStream, bool writeCompressedData) { BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); - byte[] data = ZipUtility.Deflate(ChunkData); - CompressedChunkSize = Convert.ToInt16(data.Length); - writer.Write(data); + if (writeCompressedData) + { + writer.Write(CompressedChunkData); + } + else + { + writer.Write(ChunkData); + } + } + + public void SetData(byte[] chunkData, bool compressed) + { + if (compressed) + { + CompressedChunkData = chunkData; + ChunkData = ZipUtility.Inflate(chunkData); + } + else + { + CompressedChunkData = ZipUtility.Deflate(chunkData); + ChunkData = chunkData; + } } } } diff --git a/FtexTool/Ftexs/FtexsFileMipMap.cs b/FtexTool/Ftexs/FtexsFileMipMap.cs index b4f98ff..0b6b0df 100644 --- a/FtexTool/Ftexs/FtexsFileMipMap.cs +++ b/FtexTool/Ftexs/FtexsFileMipMap.cs @@ -86,6 +86,7 @@ public void Write(Stream outputStream) foreach (var chunk in Chunks) { + bool writeCompressedChunkData = true; if (absoluteOffset) { chunk.Offset = Convert.ToUInt32(writer.BaseStream.Position); @@ -93,10 +94,16 @@ public void Write(Stream outputStream) else { chunk.Offset = DefaultRelativeOffset; - if (chunk.CompressedChunkSize == chunk.DecompressedChunkSize) + if (chunk.ChunkSize == chunk.CompressedChunkSize) + { chunk.Offset = chunk.Offset | UncompressedFlag; + writeCompressedChunkData = false; + } } - chunk.WriteData(outputStream); + chunk.WriteData(outputStream, writeCompressedChunkData); + // TODO: Write 8 zeroes and the next chunk info + ////writer.WriteZeros(8); + ////writer.WriteZeros(8); } long endPosition = writer.BaseStream.Position; writer.BaseStream.Position = Offset; diff --git a/FtexTool/Properties/AssemblyInfo.cs b/FtexTool/Properties/AssemblyInfo.cs index 87e48d5..ad9b2aa 100644 --- a/FtexTool/Properties/AssemblyInfo.cs +++ b/FtexTool/Properties/AssemblyInfo.cs @@ -35,5 +35,5 @@ // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.2.1.0")] -[assembly: AssemblyFileVersion("0.2.1.0")] +[assembly: AssemblyVersion("0.2.2.0")] +[assembly: AssemblyFileVersion("0.2.2.0")] diff --git a/PftxsTool/Properties/AssemblyInfo.cs b/PftxsTool/Properties/AssemblyInfo.cs index cfa627c..5b608a0 100644 --- a/PftxsTool/Properties/AssemblyInfo.cs +++ b/PftxsTool/Properties/AssemblyInfo.cs @@ -35,5 +35,5 @@ // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.2.1.0")] -[assembly: AssemblyFileVersion("0.2.1.0")] +[assembly: AssemblyVersion("0.2.2.0")] +[assembly: AssemblyFileVersion("0.2.2.0")]