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)) { if (stream.Length == 0) { return; } 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 1 byte, file ID, resource type, and 1 extra byte reader.BaseStream.Seek(14, SeekOrigin.Current); // mip 0 Mip mip0 = new Mip { 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(39, SeekOrigin.Current); // go to next mip // mip 1 Mip mip1 = new Mip { 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 mips, if they exist if (node.GetForge().FileEntries.Where(x => x.NameTable.Name.Contains(Path.GetFileNameWithoutExtension(fileName) + "_Mip")).Count() == 2) { Forge.FileEntry[] mipEntries = node.GetForge().FileEntries.Where(x => x.NameTable.Name == Path.GetFileNameWithoutExtension(fileName) + "_Mip0").ToArray(); if (mipEntries.Length > 0) { Forge.FileEntry mipEntry = mipEntries[0]; // extract, read, and create a DDS image with the first mip byte[] rawTopMipData = node.GetForge().GetRawData(mipEntry); //Helpers.WriteToFile(mipEntry.NameTable.Name, rawData, true); // read //ReadFile(Helpers.GetTempPath(mipEntry.NameTable.Name)); byte[] topMipData = ReadFile(rawTopMipData); // extract //ExtractTopMip(Helpers.GetTempPath(mipEntry.NameTable.Name), mip0, completionAction); ExtractTopMip(topMipData, mipEntry.NameTable.Name, mip0, completionAction); } } else // mips do not exist. fear not! there is still image data found here. let us use that. { reader.BaseStream.Seek(12, SeekOrigin.Current); TextureMap map = new TextureMap { DataSize = reader.ReadInt32() }; byte[] mipmapData = reader.ReadBytes(map.DataSize); // write DDS file Helpers.WriteTempDDS(name, mipmapData, mip1.Width, mip1.Height, mip1.Mipmaps, mip1.DXTType, () => { Helpers.ConvertDDS($"{Helpers.GetTempPath(name)}.dds", (bool error) => { if (error) { completionAction("FAILED"); } else { completionAction($"{Helpers.GetTempPath(name)}.png"); } }); }); } } } }