diff --git a/.vs/Switch_Toolbox/v15/.suo b/.vs/Switch_Toolbox/v15/.suo index 50bfa977..ce4b3c0a 100644 Binary files a/.vs/Switch_Toolbox/v15/.suo and b/.vs/Switch_Toolbox/v15/.suo differ diff --git a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide index 80a911bb..62b00e08 100644 Binary files a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide and b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide differ diff --git a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal index 894314ee..2dfc0d4b 100644 Binary files a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal and b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal differ diff --git a/Switch_FileFormatsMain/FileFormats/Texture/GTXFile.cs b/Switch_FileFormatsMain/FileFormats/Texture/GTXFile.cs index 0f56f0f6..1b326233 100644 --- a/Switch_FileFormatsMain/FileFormats/Texture/GTXFile.cs +++ b/Switch_FileFormatsMain/FileFormats/Texture/GTXFile.cs @@ -10,6 +10,7 @@ using OpenTK.Graphics.OpenGL; using Switch_Toolbox.Library.Forms; using Bfres.Structs; using System.IO; +using System.Linq; namespace FirstPlugin { @@ -149,6 +150,11 @@ namespace FirstPlugin } public byte[] Save() { + //Get each block type for saving except alignment + var TextureInfoBlocks = blocks.Where(i => i.BlockType == BlockType.ImageInfo); + var TextureDataBlocks = blocks.Where(i => i.BlockType == BlockType.ImageData); + var TextureMipDataBlocks = blocks.Where(i => i.BlockType == BlockType.MipData); + System.IO.MemoryStream mem = new System.IO.MemoryStream(); using (FileWriter writer = new FileWriter(mem)) { @@ -188,15 +194,34 @@ namespace FirstPlugin } else if ((uint)block.BlockType == dataBlockType) { - block.data = textures[imageBlockIndex++].surface.data; + var tex = textures[imageBlockIndex++]; + + var pos = writer.Position; + uint Alignment = tex.surface.alignment; + //Create alignment block first + uint dataAlignment = GetAlignBlockSize((uint)pos + 32, Alignment); + GTXDataBlock dataAlignBlock = new GTXDataBlock(BlockType.AlignData, dataAlignment, 0, 0); + dataAlignBlock.Write(writer); + + block.data = tex.surface.data; block.Write(writer); } else if ((uint)block.BlockType == mipBlockType) { - block.data = textures[imageMipBlockIndex++].surface.mipData; + var tex = textures[imageMipBlockIndex++]; + + var pos = writer.Position; + uint Alignment = tex.surface.alignment; + //Create alignment block first + uint dataAlignment = GetAlignBlockSize((uint)pos + 32, Alignment); + GTXDataBlock dataAlignBlock = new GTXDataBlock(BlockType.AlignData, dataAlignment, 0, 0); + dataAlignBlock.Write(writer); + + + block.data = tex.surface.mipData; block.Write(writer); } - else + else if (block.BlockType != BlockType.AlignData) { block.Write(writer); } @@ -204,6 +229,21 @@ namespace FirstPlugin } return mem.ToArray(); } + + private static uint GetAlignBlockSize(uint DataOffset, uint Alignment) + { + uint alignSize = RoundUp(DataOffset, Alignment) - DataOffset - 32; + + uint z = 1; + while (alignSize < 0) + alignSize = RoundUp(DataOffset + (Alignment * z), Alignment) - DataOffset - 32; + z += 1; + + return alignSize; + } + + private static uint RoundUp(uint X, uint Y) { return((X - 1) | (Y - 1)) + 1; } + private void ReadGx2(FileReader reader) { reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian; @@ -352,10 +392,24 @@ namespace FirstPlugin public uint MinorVersion; public BlockType BlockType; public uint Identifier; - public uint index; + public uint Index; public uint DataSize; public byte[] data; + public GTXDataBlock() { } + + public GTXDataBlock(BlockType blockType, uint dataSize, uint identifier, uint index) + { + HeaderSize = 32; + MajorVersion = 1; + MinorVersion = 0; + BlockType = blockType; + DataSize = dataSize; + Identifier = identifier; + Index = index; + data = new byte[dataSize]; + } + public void Read(FileReader reader) { long blockStart = reader.Position; @@ -370,7 +424,7 @@ namespace FirstPlugin BlockType = reader.ReadEnum(false); DataSize = reader.ReadUInt32(); Identifier = reader.ReadUInt32(); - index = reader.ReadUInt32(); + Index = reader.ReadUInt32(); reader.Seek(blockStart + HeaderSize, System.IO.SeekOrigin.Begin); data = reader.ReadBytes((int)DataSize); @@ -386,7 +440,7 @@ namespace FirstPlugin writer.Write(BlockType, false); writer.Write(data.Length); writer.Write(Identifier); - writer.Write(index); + writer.Write(Index); writer.Seek(blockStart + HeaderSize, System.IO.SeekOrigin.Begin); writer.Write(data); @@ -460,13 +514,14 @@ namespace FirstPlugin surface.imageCount = NewSurface.imageCount; surface.imageSize = NewSurface.imageSize; surface.mipData = NewSurface.mipData; + surface.mipSize = NewSurface.mipSize; surface.mipOffset = NewSurface.mipOffset; surface.numArray = NewSurface.numArray; surface.numMips = NewSurface.numMips; surface.pitch = NewSurface.pitch; surface.resourceFlags = NewSurface.resourceFlags; surface.swizzle = NewSurface.swizzle; - surface.texRegs = NewSurface.texRegs; + // surface.texRegs = NewSurface.texRegs; surface.tileMode = NewSurface.tileMode; surface.use = NewSurface.use; surface.width = NewSurface.width; diff --git a/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache b/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache index 8c3c11eb..c6938e57 100644 Binary files a/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache and b/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache differ diff --git a/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache b/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache index 02a4df43..5f514a53 100644 Binary files a/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache and b/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache differ diff --git a/Switch_Toolbox_Library/Swizzling/GX2.cs b/Switch_Toolbox_Library/Swizzling/GX2.cs index 8677b054..ce6f1187 100644 --- a/Switch_Toolbox_Library/Swizzling/GX2.cs +++ b/Switch_Toolbox_Library/Swizzling/GX2.cs @@ -597,11 +597,12 @@ namespace Switch_Toolbox.Library surf.bpp = bpp; surf.format = (uint)Format; surf.numMips = MipCount; + surf.imageCount = MipCount; + surf.firstSlice = 0; + surf.firstMip = 0; surf.aa = AAMode; surf.mipOffset = mipOffsets.ToArray(); - surf.numMips = (uint)Swizzled.Count; surf.alignment = alignment; - surf.mipSize = mipSize; surf.imageSize = imageSize; surf.data = Swizzled[0]; @@ -614,6 +615,8 @@ namespace Switch_Toolbox.Library surf.mipData = Utils.CombineByteArray(mips.ToArray()); mips.Clear(); + surf.mipSize = surf.mipData != null ? (uint)surf.mipData.Length : 0; + Console.WriteLine(""); Console.WriteLine("// ----- GX2Surface Swizzled Info ----- ");