Exemplo n.º 1
0
        public static byte[] Create(ushort width, ushort height, GTEXFormat.GtexPixelFormat format, byte mimapCount, short depth, int linerSize, byte[] raw)
        {
            MemoryStream result = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(result);

            writer.Write(0x20534444); //DDS magic

            writer.Write(0x0000007c); //header size
            writer.Write(0x00801007); //flags
            writer.Write((uint)height);
            writer.Write((uint)width);
            writer.Write(linerSize);                         //liner
            writer.Write((int)depth);                        //depth
            writer.Write((int)mimapCount);                   //mimap count
            writer.Write(new byte[11 * 4]);                  //reserved1
                                                             //DDS Pixel Format
            writer.Write(0x000020);                          //size
            writer.Write(GTEXFormat.GetFlags(format));       //flags
            writer.Write(GTEXFormat.GetFormat(format));      //fourCC
            writer.Write(GTEXFormat.GetRGBBitCount(format)); //RGB bit count
            writer.Write(GTEXFormat.GetRBitMask(format));    //RBit mark
            writer.Write(GTEXFormat.GetGBitMask(format));    //GBit mark
            writer.Write(GTEXFormat.GetBBitMask(format));    //BBit mark
            writer.Write(GTEXFormat.GetABitMask(format));    //ABit mark
            writer.Write(0x00001000);                        //caps
            writer.Write(new byte[4]);                       //caps2
            writer.Write(new byte[4]);                       //caps3
            writer.Write(new byte[4]);                       //caps4
            writer.Write(new byte[4]);                       //reserved2
                                                             //Raw Texture
            writer.Write(raw);
            writer.Close();
            return(result.ToArray());
        }
Exemplo n.º 2
0
        public static void Unpack(string xgr, string dir)
        {
            string imgb = Path.Combine(Path.GetDirectoryName(xgr), $"{Path.GetFileNameWithoutExtension(xgr)}.imgb");

            if (!File.Exists(imgb))
            {
                throw new Exception("IMGB file not found.");
            }
            FileStream     stream     = File.OpenRead(xgr);
            FileStream     imgbStream = File.OpenRead(imgb);
            BeBinaryReader reader     = new BeBinaryReader(stream);
            BinaryReader   imgbReader = new BinaryReader(imgbStream);

            if (reader.ReadInt32() == 0x57504400)
            {
                uint fileCount = reader.ReadUInt32();
                reader.BaseStream.Position += 8; //zeroes
                for (int i = 0; i < fileCount; i++)
                {
                    string fileName   = Encoding.ASCII.GetString(reader.ReadBytes(16)).Replace($"{(char)0}", string.Empty);
                    uint   infoOffset = reader.ReadUInt32();
                    uint   infoSize   = reader.ReadUInt32();
                    string fileExt    = Encoding.ASCII.GetString(reader.ReadBytes(8)).Replace($"{(char)0}", string.Empty);
                    string filePath   = Path.Combine(dir, $"{fileName}.{fileExt}");
                    long   current    = reader.BaseStream.Position;
                    reader.BaseStream.Position = infoOffset;
                    if (fileExt == "txbh")
                    {
                        string magicWord = Encoding.ASCII.GetString(reader.ReadBytes(8)).Replace($"{(char)0}", string.Empty);
                        reader.BaseStream.Position += 56; //unknow
                        string fileType = Encoding.ASCII.GetString(reader.ReadBytes(4)).Replace($"{(char)0}", string.Empty);
                        reader.BaseStream.Position += 2;  //unknow
                        GTEXFormat.GtexPixelFormat format = (GTEXFormat.GtexPixelFormat)reader.ReadByte();
                        byte mimapCount = reader.ReadByte();
                        reader.ReadByte(); //unknow
                        bool   isCubeMap = reader.ReadBoolean();
                        ushort width     = reader.ReadUInt16();
                        ushort height    = reader.ReadUInt16();
                        short  depth     = reader.ReadInt16();
                        int    linerSize = reader.ReadInt32();
                        reader.BaseStream.Position += 4;
                        uint offsetImgb = reader.ReadUInt32();
                        uint size       = reader.ReadUInt32();
                        imgbReader.BaseStream.Position = offsetImgb;
                        byte[] raw    = imgbReader.ReadBytes((int)size);
                        byte[] result = DDS.Create(width, height, format, mimapCount, depth, linerSize, raw);
                        File.WriteAllBytes($"{filePath}.dds", result);
                    }
                    else
                    {
                        byte[] bytes = reader.ReadBytes((int)infoSize);
                        File.WriteAllBytes($"{filePath}.{fileExt}", bytes);
                    }
                    Console.WriteLine($"Unpacked: {fileName}.{fileExt}");
                    reader.BaseStream.Position = current;
                }
            }
            else
            {
                throw new Exception("File is not a XGR file.");
            }
            reader.Close();
            stream.Close();
            imgbReader.Close();
            imgbStream.Close();
        }