private static string ExtractTopMip(string fileName, TopMip topMip) { /* * Diffuse maps = DXT1 * Normal maps = DX10 w/ BC7 */ using (Stream stream = new FileStream($"{fileName}-combined", FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BinaryReader reader = new BinaryReader(stream)) { // this is omnipresent DatafileHeader header = new DatafileHeader { ResourceType = reader.ReadInt32(), FileSize = reader.ReadInt32(), FileNameSize = reader.ReadInt32() }; header.FileName = reader.ReadChars(header.FileNameSize); reader.BaseStream.Seek(18, SeekOrigin.Current); byte[] data = reader.ReadBytes(header.FileSize - 18); // write DDS file Helpers.WriteTempDDS(fileName, data, topMip.Width, topMip.Height, topMip.Mipmaps, topMip.DXTType); } } return(string.Format("{0}.dds", Helpers.GetTempPath(fileName))); // Helpers.ConvertDDSToPNG(); }
private static void ExtractTopMip(byte[] topMipData, string name, TopMip topMip, Action <string> completionAction) { using (Stream stream = new MemoryStream(topMipData)) { using (BinaryReader reader = new BinaryReader(stream)) { DatafileHeader header = new DatafileHeader { ResourceType = reader.ReadInt32(), FileSize = reader.ReadInt32(), FileNameSize = reader.ReadInt32() }; header.FileName = reader.ReadChars(header.FileNameSize); reader.BaseStream.Seek(18, SeekOrigin.Current); byte[] data = reader.ReadBytes(header.FileSize - 18); // write DDS file Helpers.WriteTempDDS(name, data, topMip.Width, topMip.Height, topMip.Mipmaps, topMip.DXTType, () => { Helpers.ConvertDDS($"{Helpers.GetTempPath(name)}.dds", (bool error) => { if (error) { completionAction("FAILED"); } else { completionAction($"{Helpers.GetTempPath(name)}.png"); } }); }); } } }
public static bool ExtractTextureMap(string fileName, Forge forge) { using (Stream stream = new FileStream($"{fileName}-combined", FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BinaryReader reader = new BinaryReader(stream)) { // this is omnipresent DatafileHeader header = new DatafileHeader { ResourceType = reader.ReadInt32(), FileSize = reader.ReadInt32(), FileNameSize = reader.ReadInt32() }; header.FileName = reader.ReadChars(header.FileNameSize); // ignore the 2 bytes, file ID, and resource type identifier reader.BaseStream.Seek(14, SeekOrigin.Current); // toppmip 0 TopMip mip0 = new TopMip { Width = reader.ReadInt32(), Height = reader.ReadInt32() }; reader.BaseStream.Seek(8, SeekOrigin.Current); mip0.DXTType = DXTExtensions.GetDXT(reader.ReadInt32()); reader.BaseStream.Seek(4, SeekOrigin.Current); mip0.Mipmaps = reader.ReadInt32(); reader.BaseStream.Seek(81, SeekOrigin.Current); // topmip 1 TopMip mip1 = new TopMip { Width = reader.ReadInt32(), Height = reader.ReadInt32() }; reader.BaseStream.Seek(8, SeekOrigin.Current); mip1.DXTType = DXTExtensions.GetDXT(reader.ReadInt32()); reader.BaseStream.Seek(4, SeekOrigin.Current); mip1.Mipmaps = reader.ReadInt32(); // locate the two topmips, if they exist if (forge.FileEntries.Where(x => x.NameTable.Name.Contains(Path.GetFileName(fileName) + "_TopMip")).Count() == 2) { Forge.FileEntry topMipEntry = forge.FileEntries.Where(x => x.NameTable.Name == Path.GetFileName(fileName) + "_TopMip_0").First(); // extract, read, and create DDS images with the first topmips byte[] rawData = forge.GetRawData(topMipEntry); Helpers.WriteToTempFile(topMipEntry.NameTable.Name, rawData); ReadFile(Helpers.GetTempPath(topMipEntry.NameTable.Name)); ExtractTopMip(Helpers.GetTempPath(topMipEntry.NameTable.Name), mip0); } else // topmips do not exist. fear not! there is still image data found here. let us use that. { reader.BaseStream.Seek(25, SeekOrigin.Current); TextureMap map = new TextureMap { DataSize = reader.ReadInt32() }; byte[] mipmapData = reader.ReadBytes(map.DataSize); Helpers.WriteTempDDS(fileName, mipmapData, mip0.Width, mip0.Height, mip0.Mipmaps, mip0.DXTType); } } } return(true); }
public static void ExtractTextureMap(string fileName, EntryTreeNode node, Action <string> completionAction) { string name = Path.GetFileNameWithoutExtension(fileName); using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BinaryReader reader = new BinaryReader(stream)) { DatafileHeader header = new DatafileHeader { ResourceType = reader.ReadInt32(), FileSize = reader.ReadInt32(), FileNameSize = reader.ReadInt32() }; header.FileName = reader.ReadChars(header.FileNameSize); // ignore the 2 bytes, file ID, and resource type reader.BaseStream.Seek(14, SeekOrigin.Current); // toppmip 0 TopMip mip0 = new TopMip { Width = reader.ReadInt32(), Height = reader.ReadInt32() }; reader.BaseStream.Seek(8, SeekOrigin.Current); mip0.DXTType = DXTExtensions.GetDXT(reader.ReadInt32()); reader.BaseStream.Seek(4, SeekOrigin.Current); mip0.Mipmaps = reader.ReadInt32(); reader.BaseStream.Seek(81, SeekOrigin.Current); // topmip 1, ignored TopMip mip1 = new TopMip { Width = reader.ReadInt32(), Height = reader.ReadInt32() }; reader.BaseStream.Seek(8, SeekOrigin.Current); mip1.DXTType = DXTExtensions.GetDXT(reader.ReadInt32()); reader.BaseStream.Seek(4, SeekOrigin.Current); mip1.Mipmaps = reader.ReadInt32(); // locate the two topmips, if they exist if (node.GetForge().FileEntries.Where(x => x.NameTable.Name.Contains(name + "_TopMip")).Count() == 2) { Forge.FileEntry topMipEntry = node.GetForge().FileEntries.Where(x => x.NameTable.Name == name + "_TopMip_0").First(); // extract, read, and create a DDS image with the first topmip byte[] rawTopMipData = node.GetForge().GetRawData(topMipEntry); //Helpers.WriteToFile(topMipEntry.NameTable.Name, rawTopMipData, true); // read //ReadFile(Helpers.GetTempPath(topMipEntry.NameTable.Name)); byte[] topMipData = ReadFile(rawTopMipData); // extract //ExtractTopMip(Helpers.GetTempPath(topMipEntry.NameTable.Name), mip0, completionAction); ExtractTopMip(topMipData, topMipEntry.NameTable.Name, mip0, completionAction); } else // topmips do not exist. fear not! there is still image data found here. let us use that. { reader.BaseStream.Seek(25, SeekOrigin.Current); TextureMap map = new TextureMap { DataSize = reader.ReadInt32() }; byte[] mipmapData = reader.ReadBytes(map.DataSize); // write DDS file Helpers.WriteTempDDS(name, mipmapData, mip0.Width, mip0.Height, mip0.Mipmaps, mip0.DXTType, () => { Helpers.ConvertDDS($"{Helpers.GetTempPath(name)}.dds", (bool error) => { if (error) { completionAction("FAILED"); } else { completionAction($"{Helpers.GetTempPath(name)}.png"); } }); }); } } } }