Ejemplo n.º 1
0
        /* This class originally had something more clever going on, Zelda Four Swords threw a giant f*****g wrench in my code. */
        /* I'd not recommend using it, as it might be removed in the future if the codebase for it doesn't grow. */
        public static JAIInitType checkVersion(ref byte[] data)
        {
            var JStream = new MemoryStream(data);
            var JReader = new BeBinaryReader(JStream);
            var hdr     = JReader.ReadUInt32();

            if (hdr == 1094803260) // AA_< LITERAL , opening of BAA archive or BAA Format
            {
                JReader.Close();
                JStream.Close();
                return(JAIInitType.BAA); // return BAA type
            }
            else
            { /* PIKMIN BX ARCHIVE */
              /* CHECKING FOR BX
               * This is not 100% accurate, but the likelyhood of something like this actually getting confused with AAF is slim to none.
               * Considering there's only one game that uses BX.
               */

                JStream.Position = 0;               // reset pos;
                var BXWSOffs = JReader.ReadInt32(); // should point to location in file.
                if (BXWSOffs < JStream.Length)      // check if is within BX
                {
                    JStream.Position = BXWSOffs;
                    var WSO = JReader.ReadInt32();
                    if (WSO < JStream.Length) // fall out, not valid
                    {
                        var WSYS = JReader.ReadInt32();
                        if (WSYS == 0x57535953) // 0x57535953 is literal WSYS
                        {
                            JReader.Close();    // flush / close streams
                            JStream.Close();    // flush and close streams
                            return(JAIInitType.BX);
                        }
                    }
                }
            }
            // * The init type is otherwise AAF.
            {
                JReader.Close();
                JStream.Close();
                return(JAIInitType.AAF); // JAIInitSection v1 doesn't have an identifying header.
            }
        }
        public static byte[] DeSerializeLongString(byte[] buf)
        {
            BeBinaryReader br   = new BeBinaryReader(new MemoryStream(buf));
            uint           size = br.ReadUInt32();

            byte[] data = br.ReadBytes((int)size);
            br.Close();

            return(data);
        }
Ejemplo n.º 3
0
        private void rebuildAw(int num)
        {
            var awTemp     = File.OpenWrite("awBuild.temp");
            var grp        = root.currentWSYS.Groups[num];
            var grpFile    = grp.awFile;
            var awOriginal = File.OpenRead(grpFile);
            var wss        = root.currentWSYSStream;
            var wsysWrite  = new BeBinaryWriter(wss);
            var awWrite    = new BeBinaryWriter(awTemp);
            var awRead     = new BeBinaryReader(awOriginal);
            var awOffset   = 0;

            if (!rebuildData.ContainsKey(num))
            {
                return;
            }
            Console.WriteLine("Rebuild request found.");

            var rbdWaves = rebuildData[num];

            for (int i = 0; i < grp.Waves.Length; i++)
            {
                var    originalWave = grp.Waves[i];
                byte[] awBuff;
                if (rbdWaves.ContainsKey(i))
                {
                    awBuff = rbdWaves[i];
                    Console.WriteLine("Injecting custom wave...");
                }
                else
                {
                    awRead.BaseStream.Position = originalWave.wsys_start;
                    awBuff = awRead.ReadBytes(originalWave.wsys_size);
                }
                awTemp.Write(awBuff, 0, awBuff.Length);
                wsysWrite.BaseStream.Position = originalWave.mOffset;
                wsysWrite.Seek(0x04, SeekOrigin.Current);
                wsysWrite.Write((float)originalWave.sampleRate);
                wsysWrite.Write(awOffset);
                wsysWrite.Write(awBuff.Length);
                originalWave.wsys_start = awOffset;      // update in-memory copies
                originalWave.wsys_size  = awBuff.Length; // update in-memory copies
                awOffset += awBuff.Length;
                wsysWrite.Flush();
                awTemp.Flush();
            }
            Console.WriteLine($"Rebuilt {grpFile}");
            awTemp.Close();
            awRead.Close();
            awOriginal.Close();
            File.Delete(grpFile);
            File.Move("awBuild.temp", grpFile);
        }
Ejemplo n.º 4
0
        private void ReadThreadBody()
        {
            HandleHandshake();

            while (true)
            {
                int length = _reader.ReadInt32();
                if (length == 0)
                {
                    HandleKeepAlive();
                    continue;
                }

                byte[]         data   = _reader.ReadBytes(length);
                BeBinaryReader reader = new BeBinaryReader(new MemoryStream(data));
                int            mid    = reader.ReadByte();

                switch (mid)
                {
                case 0:
                    HandleChoke(reader, true);
                    break;

                case 1:
                    HandleChoke(reader, false);
                    break;

                case 2:
                    HandleInterested(reader, true);
                    break;

                case 3:
                    HandleInterested(reader, false);
                    break;

                case 4:
                    HandleHave(reader);
                    break;

                case 5:
                    HandleBitfield(reader);
                    break;

                case 6:
                    HandleRequest(reader);
                    break;

                case 7:
                    HandlePiece(reader, length);
                    break;

                case 8:
                    HandleCancel(reader);
                    break;

                case 9:
                    HandlePort(reader);
                    break;

                default:
                    Debug($"Unknown packet! {mid}");
                    break;
                }

                reader.Close();
            }
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        public static void Repack(string dir, string xgr)
        {
            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.Open(xgr, FileMode.Open, FileAccess.ReadWrite);
            FileStream     imgbStream = File.Open(imgb, FileMode.Open, FileAccess.ReadWrite);
            BeBinaryReader reader     = new BeBinaryReader(stream);
            BinaryWriter   xgrWriter  = new BinaryWriter(stream);
            BinaryWriter   imgbWriter = new BinaryWriter(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}");
                    if (!File.Exists($"{filePath}.dds") && !File.Exists(filePath))
                    {
                        continue;
                    }
                    long current = reader.BaseStream.Position;
                    reader.BaseStream.Position = infoOffset;
                    if (fileExt == "txbh")
                    {
                        byte[] bytes = File.ReadAllBytes($"{filePath}.dds").Skip(128).ToArray();
                        reader.BaseStream.Position += 88;
                        uint offsetImgb = reader.ReadUInt32();
                        uint size       = reader.ReadUInt32();
                        if (bytes.Length != size)
                        {
                            throw new Exception("The new file size should be the same as the original file size.");
                        }
                        imgbWriter.BaseStream.Position = offsetImgb;
                        imgbWriter.Write(bytes);
                    }
                    else
                    {
                        byte[] bytes = File.ReadAllBytes(filePath);
                        if (bytes.Length != infoSize)
                        {
                            throw new Exception("The new file size should be the same as the original file size.");
                        }
                        xgrWriter.BaseStream.Position = infoOffset;
                        xgrWriter.Write(bytes);
                    }
                    Console.WriteLine($"Repacked: {fileName}.{fileExt}");
                    reader.BaseStream.Position = current;
                }
            }
            else
            {
                throw new Exception("File is not a XGR file.");
            }
            reader.Close();
            xgrWriter.Close();
            stream.Close();
            imgbWriter.Close();
            imgbStream.Close();
        }