static void Main(string[] args) { if (args.Length == 1) { if (Directory.Exists(args[0]) && File.GetAttributes(args[0]).HasFlag(FileAttributes.Directory)) { var inputPath = args[0]; var outputPath = Path.Combine(Path.GetDirectoryName(inputPath), Path.GetFileNameWithoutExtension(args[0]) + ".txp"); Txp.Create(inputPath, outputPath); } else if (File.Exists(args[0])) { var inputPath = args[0]; var outputPath = Path.Combine(Path.GetDirectoryName(inputPath), Path.GetFileNameWithoutExtension(args[0])); Txp.Read(inputPath, outputPath); } } else if (args.Length == 3 && args[0] == "x") { var inputPath = args[1]; var outputPath = args[2]; Txp.Read(inputPath, outputPath); } else if (args.Length == 3 && args[0] == "c") { var inputPath = args[1]; var outputPath = args[2]; Txp.Create(inputPath, outputPath); } else if (args.Length == 2 && args[0] == "a") { string inputPath = args[1]; foreach (var file in Directory.EnumerateFiles(inputPath, "*.txp", SearchOption.AllDirectories)) { Console.WriteLine("Reading {0}...", inputPath); Txp.Read(file, file.Replace(".txp", "")); Console.WriteLine(); } } else { Console.WriteLine("usage:"); Console.WriteLine(""); Console.WriteLine("Extract:"); Console.WriteLine("\t{0} x filename.txp foldername", AppDomain.CurrentDomain.FriendlyName); Console.WriteLine(""); Console.WriteLine("Create:"); Console.WriteLine("\t{0} c foldername filename.txp", AppDomain.CurrentDomain.FriendlyName); Console.WriteLine(""); Console.WriteLine("Extract all TXPs in a folder:"); Console.WriteLine("\t{0} a foldername", AppDomain.CurrentDomain.FriendlyName); Console.WriteLine(""); } }
public static Txp Create(string inputPath, string outputPath) { if (!Directory.Exists(inputPath)) { return(null); } Txp file = new Txp(); // Look for all xml definition files in the folder var files = Directory.GetFiles(inputPath, "*.xml", SearchOption.AllDirectories); foreach (var xmlFilename in files) { TxpEntry entry = TxpEntry.FromFile(xmlFilename, inputPath); file._entries.Add(entry); } using (BinaryWriter writer = new BinaryWriter(File.Open(outputPath, FileMode.Create))) using (MemoryStream txpData = new MemoryStream()) using (BinaryWriter txpWriter = new BinaryWriter(txpData)) { file.Write(txpWriter); uint expectedFilesize = (uint)txpData.Length; if (expectedFilesize > 0xffffff) { writer.Write(0x00000019); writer.Write(expectedFilesize); } else { writer.Write((expectedFilesize << 8) | 0x19); } txpData.Seek(0, SeekOrigin.Begin); var d = Compress(txpData.ToArray()); writer.Write(d); } Console.WriteLine("Finished! Saved to {0}", outputPath); return(file); }
public static Txp Read(byte[] data, string inputPath, string outputPath, bool isRawExtract = false) { Txp file = new Txp(); using (MemoryStream dataStream = new MemoryStream(data)) using (BinaryReader reader = new BinaryReader(dataStream)) { if (reader.ReadUInt32() != Magic) { Console.WriteLine("ERROR: Not a valid TXP file."); } file.Version = reader.ReadInt32(); file.NumberOfTextures = reader.ReadInt32(); file.HashSectionOffset = reader.ReadUInt32(); // File info for (int i = 0; i < file.NumberOfTextures; i++) { reader.BaseStream.Seek(0x20 * (i + 1), SeekOrigin.Begin); TxpEntry entry = TxpEntry.Read(reader); entry.FileIndexA = file._entries.Count; file._entries.Add(entry); } // Filename hashes reader.BaseStream.Seek(file.HashSectionOffset, SeekOrigin.Begin); for (int i = 0; i < file.NumberOfTextures; i++) { file._entries[i].FilenameHash = reader.ReadUInt32(); int idx = reader.ReadInt32(); if (idx < file._entries.Count) { file._entries[idx].FileIndexB = i; } else { Console.WriteLine("ERROR(?): Found hash entry without a matching file entry"); } } // Image data for (int i = 0; i < file.NumberOfTextures; i++) { // Palette data if (file._entries[i].PaletteOffset != 0) { reader.BaseStream.Seek(file._entries[i].PaletteOffset, SeekOrigin.Begin); file._entries[i].PaletteData = new uint[0x100]; for (int x = 0; x < 0x100; x++) { file._entries[i].PaletteData[x] = reader.ReadUInt32(); } } reader.BaseStream.Seek(file._entries[i].TextureOffset, SeekOrigin.Begin); file._entries[i].TextureRawData = new byte[file._entries[i].TextureSize]; reader.Read(file._entries[i].TextureRawData, 0, (int)file._entries[i].TextureSize); } for (int i = 0; i < file.NumberOfTextures; i++) { Console.WriteLine("Converting {0}...", file._entries[i].InternalFilePath); file._entries[i].ToFile(outputPath, isRawExtract); } } return(file); }