Пример #1
0
        /// <summary>
        /// Converts a RTPack File to a Bitmap by file name. Returns false if invalid file.
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static Bitmap ConvertRTPACKFile(string filename)
        {
            if (!File.Exists(filename))
            {
                return(new Bitmap(32, 32));
            }
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                BinaryReader br = new BinaryReader(fs);

                /*
                 * Main File Header.
                 * - RTTXTR -> Texture File.
                 * - RTFONT -> Font File.
                 * - RTPACK -> One of them, recompressed after output. (RTPack.exe ".rtfont/.rttex")
                 *
                 * */
                string rtpack_magic = new string(br.ReadChars(6));
                if (String.Equals(rtpack_magic, "RTTXTR"))
                {
                    return(new RTTEX(br).texture);
                }
                else if (String.Equals(rtpack_magic, "RTFONT"))
                {
                    throw new FileFormatException("RTFONT file selected.");
                }
                else if (String.Equals(rtpack_magic, "RTPACK"))
                {
                    byte version  = br.ReadByte();
                    byte reserved = br.ReadByte();

                    //RTPACK Header (24 bytes)
                    uint compressedSize   = br.ReadUInt32();
                    uint decompressedSize = br.ReadUInt32();

                    eCompressionType compressionType = (eCompressionType)br.ReadByte();

                    fs.Seek(15, SeekOrigin.Current);

                    //Zlib Magic header (78 9C), IO.Compression doesn't want it for deflate so just skip it
                    fs.ReadByte();
                    fs.ReadByte();

                    //RTFONT Header
                    using (MemoryStream ms = new MemoryStream())
                    {
                        if (compressionType == eCompressionType.C_COMPRESSION_ZLIB)
                        {
                            using (DeflateStream zs = new DeflateStream(fs, CompressionMode.Decompress))
                            {
                                zs.CopyTo(ms);
                            }
                            ms.Position = 0;
                        }
                        else
                        {
                            fs.CopyTo(ms);
                            ms.Position = 0;
                        }

                        BinaryReader bdr = new BinaryReader(ms);

                        //RTFile Header again (8 bytes)
                        string decomp_magic = new string(bdr.ReadChars(6));
                        if (String.Equals(decomp_magic, "RTTXTR"))
                        {
                            return(new RTTEX(bdr).texture);
                        }
                        else if (String.Equals(decomp_magic, "RTFONT"))
                        {
                            throw new FileFormatException("RTFONT file selected.");
                        }
                    }
                }
                else
                {
                    throw new FileFormatException("Not a RTPACK file.");
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Converts a RTPack File to .png by file name. Returns false if invalid file.
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        static void ConvertRTPACKFile(string filename)
        {
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                BinaryReader br = new BinaryReader(fs);

                /*
                 * Main File Header.
                 * - RTTXTR -> Texture File.
                 * - RTFONT -> Font File.
                 * - RTPACK -> One of them, recompressed after output. (RTPack.exe ".rtfont/.rttex")
                 *
                 * */
                string rtpack_magic = new string(br.ReadChars(6));
                if (String.Equals(rtpack_magic, "RTTXTR"))
                {
                    Log("Uncompressed texture file detected.", Color.LimeGreen);
                    new RTTEX(br).texture.Save($@"{Path.GetDirectoryName(filename)}\{Path.GetFileNameWithoutExtension(filename)}.png");
                }
                else if (String.Equals(rtpack_magic, "RTFONT"))
                {
                    Log("Uncompressed font file detected.", Color.LimeGreen);
                    RTFONT font = new RTFONT(br);
                    font.fontBitmap.texture.Save($@"{Path.GetDirectoryName(filename)}\{Path.GetFileNameWithoutExtension(filename)}.png");
                    Log("Extracting all characters.", Color.LimeGreen);
                    font.ExtractCharacters(filename);
                }
                else if (String.Equals(rtpack_magic, "RTPACK"))
                {
                    byte version  = br.ReadByte();
                    byte reserved = br.ReadByte();

                    //RTPACK Header (24 bytes)
                    uint compressedSize = br.ReadUInt32();
                    Log($"-> Compressed Size : {BytesToString(compressedSize)}", Color.Orange);
                    uint decompressedSize = br.ReadUInt32();
                    Log($"-> Decompressed Size : {BytesToString(decompressedSize)}", Color.Orange);

                    eCompressionType compressionType = (eCompressionType)br.ReadByte();
                    Log($"-> Compression Type : {compressionType}", Color.Orange);

                    fs.Seek(15, SeekOrigin.Current);

                    //Zlib Magic header (78 9C), IO.Compression doesn't want it for deflate so just skip it
                    fs.ReadByte();
                    fs.ReadByte();

                    //RTFONT Header
                    using (MemoryStream ms = new MemoryStream())
                    {
                        if (compressionType == eCompressionType.C_COMPRESSION_ZLIB)
                        {
                            Log("Decompressing..", Color.Yellow);
                            using (DeflateStream zs = new DeflateStream(fs, CompressionMode.Decompress))
                            {
                                zs.CopyTo(ms);
                            }
                            ms.Position = 0;

                            //Decompress and save file
#if DEBUG
                            using (FileStream file = new FileStream(Path.GetFileNameWithoutExtension(filename) + ".rtpack", FileMode.Create))
                            {
                                ms.CopyTo(file);
                                ms.Position = 0;
                            }
#endif
                        }
                        else
                        {
                            fs.CopyTo(ms);
                            ms.Position = 0;
                        }

                        Log("Loaded onto memory.");
                        BinaryReader bdr = new BinaryReader(ms);

                        //RTFile Header again (8 bytes)
                        string decomp_magic = new string(bdr.ReadChars(6));
                        if (String.Equals(decomp_magic, "RTTXTR"))
                        {
                            Log("Texture file detected. (RTTXTR/rttex)", Color.LimeGreen);
                            new RTTEX(bdr).texture.Save($@"{Path.GetDirectoryName(filename)}\{Path.GetFileNameWithoutExtension(filename)}.png");
                        }
                        else if (String.Equals(decomp_magic, "RTFONT"))
                        {
                            Log("Font detected. (RTFONT/rtfont)", Color.LimeGreen);
                            RTFONT font = new RTFONT(bdr);
                            font.fontBitmap.texture.Save($@"{Path.GetDirectoryName(filename)}\{Path.GetFileNameWithoutExtension(filename)}.png");
                            Log("Extracting all characters.", Color.LimeGreen);
                            font.ExtractCharacters(filename);
                        }
                    }
                }
                else
                {
                    throw new FileFormatException("Not a RTPACK file.");
                }
            }
        }