Exemple #1
0
 /// <summary>
 /// Returns true if the file appears to be a BXF3 header file.
 /// </summary>
 public static bool IsBHD(string path)
 {
     using (FileStream fs = System.IO.File.OpenRead(path))
     {
         BinaryReaderEx br = new BinaryReaderEx(false, fs);
         return(IsBHD(SFUtil.GetDecompressedBR(br, out _)));
     }
 }
Exemple #2
0
        /// <summary>
        /// Loads a DRB from a byte array, automatically decompressing it if necessary.
        /// </summary>
        public static DRB Read(byte[] bytes, DRBVersion version)
        {
            BinaryReaderEx br  = new BinaryReaderEx(false, bytes);
            DRB            drb = new DRB();

            br = SFUtil.GetDecompressedBR(br, out drb.Compression);
            drb.Read(br, version);
            return(drb);
        }
Exemple #3
0
 internal BHF4()
 {
     Timestamp = SFUtil.DateToBinderTimestamp(DateTime.Now);
     Flag1     = false;
     Flag2     = false;
     Unicode   = true;
     Format    = 0x74;
     Extended  = 4;
 }
Exemple #4
0
 /// <summary>
 /// Creates an empty BND3 formatted for DS1.
 /// </summary>
 public BND3()
 {
     Files     = new List <BinderFile>();
     Timestamp = SFUtil.DateToBinderTimestamp(DateTime.Now);
     Format    = Binder.Format.x74;
     BigEndian = false;
     Unk1      = false;
     Unk2      = 0;
 }
Exemple #5
0
        /// <summary>
        /// Loads a file from a byte array, automatically decompressing it if necessary.
        /// </summary>
        public static TFormat Read(byte[] bytes)
        {
            BinaryReaderEx br   = new BinaryReaderEx(false, bytes);
            TFormat        file = new TFormat();

            br = SFUtil.GetDecompressedBR(br, out file.Compression);
            file.Read(br);
            return(file);
        }
Exemple #6
0
        public static FLVER2 Read(byte[] bytes, FlverCache cache)
        {
            BinaryReaderEx br   = new BinaryReaderEx(false, bytes);
            FLVER2         file = new FLVER2();

            file.Cache = cache;
            br         = SFUtil.GetDecompressedBR(br, out file.Compression);
            file.Read(br);
            return(file);
        }
Exemple #7
0
        private static byte[] DecompressDCXDFLT(BinaryReaderEx br, Type type)
        {
            br.AssertASCII("DCX\0");

            if (type == Type.DCX_DFLT_10000_24_9 || type == Type.DCX_DFLT_10000_44_9)
            {
                br.AssertInt32(0x10000);
            }
            else if (type == Type.DCX_DFLT_11000_44_8 || type == Type.DCX_DFLT_11000_44_9)
            {
                br.AssertInt32(0x11000);
            }

            br.AssertInt32(0x18);
            br.AssertInt32(0x24);

            if (type == Type.DCX_DFLT_10000_24_9)
            {
                br.AssertInt32(0x24);
                br.AssertInt32(0x2C);
            }
            else if (type == Type.DCX_DFLT_10000_44_9 || type == Type.DCX_DFLT_11000_44_8 || type == Type.DCX_DFLT_11000_44_9)
            {
                br.AssertInt32(0x44);
                br.AssertInt32(0x4C);
            }

            br.AssertASCII("DCS\0");
            int uncompressedSize = br.ReadInt32();
            int compressedSize   = br.ReadInt32();

            br.AssertASCII("DCP\0");
            br.AssertASCII("DFLT");
            br.AssertInt32(0x20);

            if (type == Type.DCX_DFLT_10000_24_9 || type == Type.DCX_DFLT_10000_44_9 || type == Type.DCX_DFLT_11000_44_9)
            {
                br.AssertInt32(0x9000000);
            }
            else if (type == Type.DCX_DFLT_11000_44_8)
            {
                br.AssertInt32(0x8000000);
            }

            br.AssertInt32(0x0);
            br.AssertInt32(0x0);
            br.AssertInt32(0x0);
            // These look suspiciously like flags
            br.AssertInt32(0x00010100);

            br.AssertASCII("DCA\0");
            int compressedHeaderLength = br.ReadInt32();

            return(SFUtil.ReadZlib(br, compressedSize));
        }
Exemple #8
0
        private static void CompressDCXDFLT(byte[] data, BinaryWriterEx bw, Type type)
        {
            bw.WriteASCII("DCX\0");

            if (type == Type.DCX_DFLT_10000_24_9 || type == Type.DCX_DFLT_10000_44_9)
            {
                bw.WriteInt32(0x10000);
            }
            else if (type == Type.DCX_DFLT_11000_44_8 || type == Type.DCX_DFLT_11000_44_9)
            {
                bw.WriteInt32(0x11000);
            }

            bw.WriteInt32(0x18);
            bw.WriteInt32(0x24);

            if (type == Type.DCX_DFLT_10000_24_9)
            {
                bw.WriteInt32(0x24);
                bw.WriteInt32(0x2C);
            }
            else if (type == Type.DCX_DFLT_10000_44_9 || type == Type.DCX_DFLT_11000_44_8 || type == Type.DCX_DFLT_11000_44_9)
            {
                bw.WriteInt32(0x44);
                bw.WriteInt32(0x4C);
            }

            bw.WriteASCII("DCS\0");
            bw.WriteInt32(data.Length);
            bw.ReserveInt32("CompressedSize");
            bw.WriteASCII("DCP\0");
            bw.WriteASCII("DFLT");
            bw.WriteInt32(0x20);

            if (type == Type.DCX_DFLT_10000_24_9 || type == Type.DCX_DFLT_10000_44_9 || type == Type.DCX_DFLT_11000_44_9)
            {
                bw.WriteInt32(0x9000000);
            }
            else if (type == Type.DCX_DFLT_11000_44_8)
            {
                bw.WriteInt32(0x8000000);
            }

            bw.WriteInt32(0);
            bw.WriteInt32(0);
            bw.WriteInt32(0);
            bw.WriteInt32(0x00010100);
            bw.WriteASCII("DCA\0");
            bw.WriteInt32(8);

            long compressedStart = bw.Position;

            SFUtil.WriteZlib(bw, 0xDA, data);
            bw.FillInt32("CompressedSize", (int)(bw.Position - compressedStart));
        }
Exemple #9
0
 /// <summary>
 /// Loads a DRB from the specified path, automatically decompressing it if necessary.
 /// </summary>
 public static DRB Read(string path, DRBVersion version)
 {
     using (FileStream stream = File.OpenRead(path))
     {
         BinaryReaderEx br  = new BinaryReaderEx(false, stream);
         DRB            drb = new DRB();
         br = SFUtil.GetDecompressedBR(br, out drb.Compression);
         drb.Read(br, version);
         return(drb);
     }
 }
Exemple #10
0
        /// <summary>
        /// Returns true if the bytes appear to be a DRB.
        /// </summary>
        public static bool Is(byte[] bytes)
        {
            if (bytes.Length == 0)
            {
                return(false);
            }

            BinaryReaderEx br = new BinaryReaderEx(false, bytes);

            return(Is(SFUtil.GetDecompressedBR(br, out _)));
        }
Exemple #11
0
 /// <summary>
 /// Creates an empty BND4 formatted for DS3.
 /// </summary>
 public BND4()
 {
     Files     = new List <File>();
     Timestamp = SFUtil.DateToBinderTimestamp(DateTime.Now);
     Format    = 0x74;
     BigEndian = false;
     Flag1     = false;
     Flag2     = false;
     Unicode   = true;
     Extended  = 4;
 }
Exemple #12
0
 /// <summary>
 /// Loads a file from the specified path, automatically decompressing it if necessary.
 /// </summary>
 public static TFormat Read(string path)
 {
     using (FileStream stream = File.OpenRead(path))
     {
         BinaryReaderEx br   = new BinaryReaderEx(false, stream);
         TFormat        file = new TFormat();
         br = SFUtil.GetDecompressedBR(br, out file.Compression);
         file.Read(br);
         return(file);
     }
 }
Exemple #13
0
 /// <summary>
 /// Loads a file from the specified path, automatically decompressing it if necessary.
 /// </summary>
 public static FLVER2 Read(string path, FlverCache cache)
 {
     using (FileStream stream = File.OpenRead(path))
     {
         BinaryReaderEx br   = new BinaryReaderEx(false, stream);
         FLVER2         file = new FLVER2();
         file.Cache = cache;
         br         = SFUtil.GetDecompressedBR(br, out file.Compression);
         file.Read(br);
         return(file);
     }
 }
Exemple #14
0
        /// <summary>
        /// Returns true if the file appears to be a DRB.
        /// </summary>
        public static bool Is(string path)
        {
            using (FileStream stream = File.OpenRead(path))
            {
                if (stream.Length == 0)
                {
                    return(false);
                }

                BinaryReaderEx br = new BinaryReaderEx(false, stream);
                return(Is(SFUtil.GetDecompressedBR(br, out _)));
            }
        }
Exemple #15
0
            internal File(BinaryReaderEx br, bool unicode, byte format)
            {
                Flags = br.AssertByte(0x00, 0x02, 0x03, 0x0A, 0x40, 0xC0);
                br.AssertByte(0);
                br.AssertByte(0);
                br.AssertByte(0);

                br.AssertInt32(-1);
                long compressedSize   = br.ReadInt64();
                long uncompressedSize = 0;

                if (format == 0x26 || format == 0x2A || format == 0x2E || format == 0x54 || format == 0x74)
                {
                    uncompressedSize = br.ReadInt64();
                }
                int fileOffset = br.ReadInt32();

                if (format == 0x0C)
                {
                    ID = -1;
                }
                else
                {
                    ID = br.ReadInt32();
                }
                int nameOffset = br.ReadInt32();

                if (unicode)
                {
                    Name = br.GetUTF16(nameOffset);
                }
                else
                {
                    Name = br.GetShiftJIS(nameOffset);
                }

                if (Flags == 0x03 || Flags == 0xC0)
                {
                    br.StepIn(fileOffset);
                    Bytes = SFUtil.ReadZlib(br, (int)compressedSize);
                    br.StepOut();
                }
                else
                {
                    Bytes = br.GetBytes(fileOffset, (int)compressedSize);
                }
            }
Exemple #16
0
        private static bool IsRead(BinaryReaderEx br, out TFormat file)
        {
            var test = new TFormat();

            br = SFUtil.GetDecompressedBR(br, out test.Compression);
            if (test.Is(br))
            {
                br.Position = 0;
                test.Read(br);
                file = test;
                return(true);
            }
            else
            {
                file = null;
                return(false);
            }
        }
Exemple #17
0
        private static BinderFile ReadFile(BinaryReaderEx br, Binder.Format format)
        {
            Binder.FileFlags flags = br.ReadEnum8 <Binder.FileFlags>();
            br.AssertByte(0);
            br.AssertByte(0);
            br.AssertByte(0);

            int  compressedSize = br.ReadInt32();
            uint fileOffset     = br.ReadUInt32();

            int id = -1;

            if (Binder.HasID(format))
            {
                id = br.ReadInt32();
            }

            string name = null;

            if (Binder.HasName(format))
            {
                uint fileNameOffset = br.ReadUInt32();
                name = br.GetShiftJIS(fileNameOffset);
            }

            if (Binder.HasUncompressedSize(format))
            {
                int uncompressedSize = br.ReadInt32();
            }

            byte[] bytes;
            if (Binder.IsCompressed(flags))
            {
                br.StepIn(fileOffset);
                bytes = SFUtil.ReadZlib(br, compressedSize);
                br.StepOut();
            }
            else
            {
                bytes = br.GetBytes(fileOffset, compressedSize);
            }

            return(new BinderFile(flags, id, name, bytes));
        }
Exemple #18
0
            internal File(BinaryReaderEx br, byte format)
            {
                Flags = br.AssertByte(0x00, 0x02, 0x40, 0xC0);
                br.AssertByte(0);
                br.AssertByte(0);
                br.AssertByte(0);

                int compressedSize = br.ReadInt32();
                int fileOffset     = br.ReadInt32();

                ID = br.ReadInt32();

                if (format == 0x40)
                {
                    Name = null;
                }
                else
                {
                    int fileNameOffset = br.ReadInt32();
                    Name = br.GetShiftJIS(fileNameOffset);
                }

                int uncompressedSize = compressedSize;

                if (format == 0x2E || format == 0x54 || format == 0x64 || format == 0x74)
                {
                    uncompressedSize = br.ReadInt32();
                }

                // Compressed
                if (Flags == 0xC0)
                {
                    br.StepIn(fileOffset);
                    Bytes = SFUtil.ReadZlib(br, compressedSize);
                    br.StepOut();
                }
                else
                {
                    Bytes = br.GetBytes(fileOffset, compressedSize);
                }
            }
Exemple #19
0
        /// <summary>
        /// Deserializes file data from a stream.
        /// </summary>
        protected override void Read(BinaryReaderEx br)
        {
            br.BigEndian = false;

            br.AssertASCII("ENFL");
            // Probably 4 bytes
            br.AssertInt32(0x10415);
            int compressedSize   = br.ReadInt32();
            int uncompressedSize = br.ReadInt32();

            byte[] data = SFUtil.ReadZlib(br, compressedSize);

            br = new BinaryReaderEx(false, data);
            br.AssertInt32(0);
            int unkCount1 = br.ReadInt32();
            int unkCount2 = br.ReadInt32();

            br.AssertInt32(0);

            Struct1s = new List <Struct1>(unkCount1);
            for (int i = 0; i < unkCount1; i++)
            {
                Struct1s.Add(new Struct1(br));
            }
            br.Pad(0x10);

            Struct2s = new List <Struct2>(unkCount2);
            for (int i = 0; i < unkCount2; i++)
            {
                Struct2s.Add(new Struct2(br));
            }
            br.Pad(0x10);

            br.AssertInt16(0);
            Strings = new List <string>(unkCount2);
            for (int i = 0; i < unkCount2; i++)
            {
                Strings.Add(br.ReadUTF16());
            }
        }
Exemple #20
0
        private static void WriteFileData(BinderFile file, BinaryWriterEx bw, int index)
        {
            if (file.Bytes.Length > 0)
            {
                bw.Pad(0x10);
            }

            bw.FillUInt32($"FileData{index}", (uint)bw.Position);

            int compressedSize = file.Bytes.Length;

            if (Binder.IsCompressed(file.Flags))
            {
                compressedSize = SFUtil.WriteZlib(bw, 0x9C, file.Bytes);
            }
            else
            {
                bw.WriteBytes(file.Bytes);
            }

            bw.FillInt32($"CompressedSize{index}", compressedSize);
        }
Exemple #21
0
        private static void CompressDCPDFLT(byte[] data, BinaryWriterEx bw)
        {
            bw.WriteASCII("DCP\0");
            bw.WriteASCII("DFLT");
            bw.WriteInt32(0x20);
            bw.WriteInt32(0x9000000);
            bw.WriteInt32(0);
            bw.WriteInt32(0);
            bw.WriteInt32(0);
            bw.WriteInt32(0x00010100);

            bw.WriteASCII("DCS\0");
            bw.WriteInt32(data.Length);
            bw.ReserveInt32("CompressedSize");

            int compressedSize = SFUtil.WriteZlib(bw, 0xDA, data);

            bw.FillInt32("CompressedSize", compressedSize);

            bw.WriteASCII("DCA\0");
            bw.WriteInt32(8);
        }
Exemple #22
0
        private static byte[] DecompressDCPDFLT(BinaryReaderEx br)
        {
            br.AssertASCII("DCP\0");
            br.AssertASCII("DFLT");
            br.AssertInt32(0x20);
            br.AssertInt32(0x9000000);
            br.AssertInt32(0);
            br.AssertInt32(0);
            br.AssertInt32(0);
            br.AssertInt32(0x00010100);

            br.AssertASCII("DCS\0");
            int uncompressedSize = br.ReadInt32();
            int compressedSize   = br.ReadInt32();

            byte[] decompressed = SFUtil.ReadZlib(br, compressedSize);

            br.AssertASCII("DCA\0");
            br.AssertInt32(8);

            return(decompressed);
        }
Exemple #23
0
        /// <summary>
        /// Serializes file data to a stream.
        /// </summary>
        protected override void Write(BinaryWriterEx bw)
        {
            BinaryWriterEx bwData = new BinaryWriterEx(false);

            bwData.WriteInt32(0);
            bwData.WriteInt32(Struct1s.Count);
            bwData.WriteInt32(Struct2s.Count);
            bwData.WriteInt32(0);

            foreach (Struct1 struct1 in Struct1s)
            {
                struct1.Write(bwData);
            }
            bwData.Pad(0x10);

            foreach (Struct2 struct2 in Struct2s)
            {
                struct2.Write(bwData);
            }
            bwData.Pad(0x10);

            bwData.WriteInt16(0);
            foreach (string str in Strings)
            {
                bwData.WriteUTF16(str, true);
            }
            bwData.Pad(0x10);

            byte[] data = bwData.FinishBytes();

            bw.WriteASCII("ENFL");
            bw.WriteInt32(0x10415);
            bw.ReserveInt32("CompressedSize");
            bw.WriteInt32(data.Length);
            int compressedSize = SFUtil.WriteZlib(bw, 0xDA, data);

            bw.FillInt32("CompressedSize", compressedSize);
        }
Exemple #24
0
        internal static void Compress(byte[] data, BinaryWriterEx bw, Type type)
        {
            // Some day I hope to get Oodle compression working, but not today
            if (type == Type.DCX_KRAK)
            {
                type = Type.DCX_DFLT_11000_44_9;
            }

            bw.BigEndian = true;
            if (type == Type.Zlib)
            {
                SFUtil.WriteZlib(bw, 0xDA, data);
            }
            else if (type == Type.DCP_DFLT)
            {
                CompressDCPDFLT(data, bw);
            }
            else if (type == Type.DCX_EDGE)
            {
                CompressDCXEDGE(data, bw);
            }
            else if (type == Type.DCX_DFLT_10000_24_9 || type == Type.DCX_DFLT_10000_44_9 || type == Type.DCX_DFLT_11000_44_8 || type == Type.DCX_DFLT_11000_44_9)
            {
                CompressDCXDFLT(data, bw, type);
            }
            else if (type == Type.DCX_KRAK)
            {
                CompressDCXKRAK(data, bw);
            }
            else if (type == Type.Unknown)
            {
                throw new ArgumentException("You cannot compress a DCX with an unknown type.");
            }
            else
            {
                throw new NotImplementedException("Compression for the given type is not implemented.");
            }
        }
Exemple #25
0
            internal void WriteData(BinaryWriterEx bw, int index, byte format)
            {
                if (Bytes.Length > 0)
                {
                    bw.Pad(0x10);
                }

                bw.FillInt32($"FileData{index}", (int)bw.Position);

                byte[] bytes          = Bytes;
                int    compressedSize = bytes.Length;

                if (Flags == 0xC0)
                {
                    compressedSize = SFUtil.WriteZlib(bw, 0x9C, bytes);
                }
                else
                {
                    bw.WriteBytes(bytes);
                }

                bw.FillInt32($"CompressedSize{index}", bytes.Length);
            }
 /// <summary>
 /// Returns every Part in the order they'll be written.
 /// </summary>
 public override List <Part> GetEntries()
 {
     return(SFUtil.ConcatAll <Part>(
                MapPieces, Objects, Enemies, Players, Collisions,
                Protobosses, Navmeshes, DummyObjects, DummyEnemies, ConnectCollisions));
 }
Exemple #27
0
        /// <summary>
        /// Returns true if the file appears to be a BXF3 data file.
        /// </summary>
        public static bool IsBDT(byte[] bytes)
        {
            BinaryReaderEx br = new BinaryReaderEx(false, bytes);

            return(IsBDT(SFUtil.GetDecompressedBR(br, out _)));
        }
Exemple #28
0
 /// <summary>
 /// Returns every Event in the order they'll be written.
 /// </summary>
 public override List <Event> GetEntries()
 {
     return(SFUtil.ConcatAll <Event>(
                Treasures, Generators, ObjActs, MapOffsets, PseudoMultiplayers, WalkRoutes, GroupTours, Others));
 }
 /// <summary>
 /// Returns every model in the order they will be written.
 /// </summary>
 public override List <Model> GetEntries()
 {
     return(SFUtil.ConcatAll <Model>(
                MapPieces, Objects, Enemies, Items, Players, Collisions, Navmeshes, DummyObjects, DummyEnemies, Others));
 }
Exemple #30
0
        internal static byte[] Decompress(BinaryReaderEx br, out Type type)
        {
            br.BigEndian = true;
            type         = Type.Unknown;

            string magic = br.ReadASCII(4);

            if (magic == "DCP\0")
            {
                string format = br.GetASCII(4, 4);
                if (format == "DFLT")
                {
                    type = Type.DCP_DFLT;
                }
                else if (format == "EDGE")
                {
                    type = Type.DCP_EDGE;
                }
            }
            else if (magic == "DCX\0")
            {
                string format = br.GetASCII(0x28, 4);
                if (format == "EDGE")
                {
                    type = Type.DCX_EDGE;
                }
                else if (format == "DFLT")
                {
                    int unk04 = br.GetInt32(0x4);
                    int unk10 = br.GetInt32(0x10);
                    int unk30 = br.GetInt32(0x30);
                    if (unk10 == 0x24)
                    {
                        type = Type.DCX_DFLT_10000_24_9;
                    }
                    else if (unk10 == 0x44)
                    {
                        if (unk04 == 0x10000)
                        {
                            type = Type.DCX_DFLT_10000_44_9;
                        }
                        else if (unk04 == 0x11000)
                        {
                            if (unk30 == 0x8000000)
                            {
                                type = Type.DCX_DFLT_11000_44_8;
                            }
                            else if (unk30 == 0x9000000)
                            {
                                type = Type.DCX_DFLT_11000_44_9;
                            }
                        }
                    }
                }
                else if (format == "KRAK")
                {
                    type = Type.DCX_KRAK;
                }
            }
            else
            {
                byte b0 = br.GetByte(0);
                byte b1 = br.GetByte(1);
                if (b0 == 0x78 && (b1 == 0x01 || b1 == 0x5E || b1 == 0x9C || b1 == 0xDA))
                {
                    type = Type.Zlib;
                }
            }

            br.Position = 0;
            if (type == Type.Zlib)
            {
                return(SFUtil.ReadZlib(br, (int)br.Length));
            }
            else if (type == Type.DCP_EDGE)
            {
                return(DecompressDCPEDGE(br));
            }
            else if (type == Type.DCP_DFLT)
            {
                return(DecompressDCPDFLT(br));
            }
            else if (type == Type.DCX_EDGE)
            {
                return(DecompressDCXEDGE(br));
            }
            else if (type == Type.DCX_DFLT_10000_24_9 || type == Type.DCX_DFLT_10000_44_9 || type == Type.DCX_DFLT_11000_44_8 || type == Type.DCX_DFLT_11000_44_9)
            {
                return(DecompressDCXDFLT(br, type));
            }
            else if (type == Type.DCX_KRAK)
            {
                return(DecompressDCXKRAK(br));
            }
            else
            {
                throw new FormatException("Unknown DCX format.");
            }
        }