Пример #1
0
            internal Texture(BinaryReaderEx br, TPFPlatform platform, byte encoding)
            {
                int fileOffset = br.ReadInt32();
                int fileSize   = br.ReadInt32();

                Format  = br.ReadByte();
                Cubemap = br.ReadBoolean();
                Mipmaps = br.ReadByte();
                Flags1  = br.AssertByte(0, 1, 2, 3);

                int nameOffset = 0;

                if (platform == TPFPlatform.PC)
                {
                    Header     = null;
                    nameOffset = br.ReadInt32();
                    Flags2     = br.AssertInt32(0, 1);
                }
                else if (platform == TPFPlatform.PS3)
                {
                    Header        = new TexHeader();
                    Header.Width  = br.ReadInt16();
                    Header.Height = br.ReadInt16();
                    Header.Unk1   = br.ReadInt32();
                    Header.Unk2   = br.AssertInt32(0, 0xAAE4);
                    nameOffset    = br.ReadInt32();
                    Flags2        = br.AssertInt32(0, 1);
                }
                else if (platform == TPFPlatform.PS4 || platform == TPFPlatform.Xbone)
                {
                    Header        = new TexHeader();
                    Header.Width  = br.ReadInt16();
                    Header.Height = br.ReadInt16();

                    Header.TextureCount = br.AssertByte(1, 6);
                    br.AssertByte(0);
                    br.AssertByte(0);
                    br.AssertByte(0);

                    Header.Unk2       = br.AssertInt32(0xD);
                    nameOffset        = br.ReadInt32();
                    Flags2            = br.AssertInt32(0, 1);
                    Header.DXGIFormat = br.ReadInt32();
                }

                Bytes = br.GetBytes(fileOffset, fileSize);
                if (Flags1 == 2 || Flags1 == 3)
                {
                    Bytes = DCX.Decompress(Bytes);
                }

                if (encoding == 1)
                {
                    Name = br.GetUTF16(nameOffset);
                }
                else if (encoding == 0 || encoding == 2)
                {
                    Name = br.GetShiftJIS(nameOffset);
                }
            }
Пример #2
0
 internal File(BinaryReaderEx br, BHF4.FileHeader fileHeader)
 {
     Name  = fileHeader.Name;
     Flags = fileHeader.Flags;
     ID    = fileHeader.ID;
     Bytes = br.GetBytes((long)fileHeader.Offset, (int)fileHeader.CompressedSize);
     if (Flags == 0x03 || Flags == 0xC0)
     {
         Bytes = DCX.Decompress(Bytes);
     }
 }
Пример #3
0
 /// <summary>
 /// Decompresses data and returns a new BinaryReaderEx if necessary.
 /// </summary>
 public static BinaryReaderEx GetDecompressedBR(BinaryReaderEx br, out DCX.Type compression)
 {
     if (DCX.Is(br))
     {
         byte[] bytes = DCX.Decompress(br, out compression);
         return(new BinaryReaderEx(false, bytes));
     }
     else
     {
         compression = DCX.Type.None;
         return(br);
     }
 }
Пример #4
0
        internal BinderFile ReadFileData(BinaryReaderEx br)
        {
            byte[]   bytes;
            DCX.Type compressionType = DCX.Type.Zlib;
            if (IsCompressed(Flags))
            {
                bytes = br.GetBytes(DataOffset, (int)CompressedSize);
                bytes = DCX.Decompress(bytes, out compressionType);
            }
            else
            {
                bytes = br.GetBytes(DataOffset, (int)CompressedSize);
            }

            return(new BinderFile(Flags, ID, Name, bytes)
            {
                CompressionType = compressionType,
            });
        }
Пример #5
0
        /// <summary>
        /// Guesses the extension of a file based on its contents.
        /// </summary>
        public static string GuessExtension(byte[] bytes, bool bigEndian = false)
        {
            bool dcx = false;

            if (DCX.Is(bytes))
            {
                dcx   = true;
                bytes = DCX.Decompress(bytes);
            }

            bool checkMsb(BinaryReaderEx br)
            {
                if (br.Length < 8)
                {
                    return(false);
                }

                int offset = br.GetInt32(4);

                if (offset < 0 || offset >= br.Length - 1)
                {
                    return(false);
                }

                try
                {
                    return(br.GetASCII(offset) == "MODEL_PARAM_ST");
                }
                catch
                {
                    return(false);
                }
            }

            bool checkParam(BinaryReaderEx br)
            {
                if (br.Length < 0x2C)
                {
                    return(false);
                }

                string param = br.GetASCII(0xC, 0x20);

                return(Regex.IsMatch(param, "^[^\0]+\0 *$"));
            }

            bool checkTdf(BinaryReaderEx br)
            {
                if (br.Length < 4)
                {
                    return(false);
                }

                if (br.GetASCII(0, 1) != "\"")
                {
                    return(false);
                }

                for (int i = 1; i < br.Length; i++)
                {
                    if (br.GetASCII(i, 1) == "\"")
                    {
                        return(i < br.Length - 2 && br.GetASCII(i + 1, 2) == "\r\n");
                    }
                }
                return(false);
            }

            string ext = "";

            using (var ms = new MemoryStream(bytes))
            {
                var    br    = new BinaryReaderEx(bigEndian, ms);
                string magic = null;
                if (br.Length >= 4)
                {
                    magic = br.ReadASCII(4);
                }

                if (magic == "AISD")
                {
                    ext = ".aisd";
                }
                else if (magic == "BDF3" || magic == "BDF4")
                {
                    ext = ".bdt";
                }
                else if (magic == "BHF3" || magic == "BHF4")
                {
                    ext = ".bhd";
                }
                else if (magic == "BND3" || magic == "BND4")
                {
                    ext = ".bnd";
                }
                else if (magic == "DDS ")
                {
                    ext = ".dds";
                }
                // ESD or FFX
                else if (magic != null && magic.ToUpper() == "DLSE")
                {
                    ext = ".dlse";
                }
                else if (bigEndian && magic == "\0BRD" || !bigEndian && magic == "DRB\0")
                {
                    ext = ".drb";
                }
                else if (magic == "EDF\0")
                {
                    ext = ".edf";
                }
                else if (magic == "ELD\0")
                {
                    ext = ".eld";
                }
                else if (magic == "ENFL")
                {
                    ext = ".entryfilelist";
                }
                else if (magic != null && magic.ToUpper() == "FSSL")
                {
                    ext = ".esd";
                }
                else if (magic == "EVD\0")
                {
                    ext = ".evd";
                }
                else if (br.Length >= 3 && br.GetASCII(0, 3) == "FEV" || br.Length >= 0x10 && br.GetASCII(8, 8) == "FEV FMT ")
                {
                    ext = ".fev";
                }
                else if (br.Length >= 6 && br.GetASCII(0, 6) == "FLVER\0")
                {
                    ext = ".flver";
                }
                else if (br.Length >= 3 && br.GetASCII(0, 3) == "FSB")
                {
                    ext = ".fsb";
                }
                else if (br.Length >= 3 && br.GetASCII(0, 3) == "GFX")
                {
                    ext = ".gfx";
                }
                else if (br.Length >= 0x19 && br.GetASCII(0xC, 0xE) == "ITLIMITER_INFO")
                {
                    ext = ".itl";
                }
                else if (br.Length >= 4 && br.GetASCII(1, 3) == "Lua")
                {
                    ext = ".lua";
                }
                else if (checkMsb(br))
                {
                    ext = ".msb";
                }
                else if (br.Length >= 0x30 && br.GetASCII(0x2C, 4) == "MTD ")
                {
                    ext = ".mtd";
                }
                else if (magic == "DFPN")
                {
                    ext = ".nfd";
                }
                else if (checkParam(br))
                {
                    ext = ".param";
                }
                else if (br.Length >= 4 && br.GetASCII(1, 3) == "PNG")
                {
                    ext = ".png";
                }
                else if (br.Length >= 0x2C && br.GetASCII(0x28, 4) == "SIB ")
                {
                    ext = ".sib";
                }
                else if (magic == "TAE ")
                {
                    ext = ".tae";
                }
                else if (checkTdf(br))
                {
                    ext = ".tdf";
                }
                else if (magic == "TPF\0")
                {
                    ext = ".tpf";
                }
                else if (magic == "#BOM")
                {
                    ext = ".txt";
                }
                else if (br.Length >= 5 && br.GetASCII(0, 5) == "<?xml")
                {
                    ext = ".xml";
                }
                // This is pretty sketchy
                else if (br.Length >= 0xC && br.GetByte(0) == 0 && br.GetByte(3) == 0 && br.GetInt32(4) == br.Length && br.GetInt16(0xA) == 0)
                {
                    ext = ".fmg";
                }
            }

            if (dcx)
            {
                return(ext + ".dcx");
            }
            else
            {
                return(ext);
            }
        }
Пример #6
0
        private static BinderFile ReadFile(BinaryReaderEx br, bool unicode, Binder.Format format)
        {
            Binder.FileFlags flags = br.ReadEnum8 <Binder.FileFlags>();
            br.AssertByte(0);
            br.AssertByte(0);
            br.AssertByte(0);

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

            if (Binder.HasUncompressedSize(format))
            {
                long uncompressedSize = br.ReadInt64();
            }

            uint fileOffset = br.ReadUInt32();

            int id = -1;

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

            string name = null;

            if (Binder.HasName(format))
            {
                uint nameOffset = br.ReadUInt32();
                if (unicode)
                {
                    name = br.GetUTF16(nameOffset);
                }
                else
                {
                    name = br.GetShiftJIS(nameOffset);
                }
            }

            if (format == Binder.Format.x20)
            {
                br.AssertInt64(0);
            }

            byte[] bytes;
            if (Binder.IsCompressed(flags))
            {
                if (format == Binder.Format.x2E)
                {
                    bytes = br.GetBytes(fileOffset, (int)compressedSize);
                    bytes = DCX.Decompress(bytes, out DCX.Type type);
                    if (type != DCX.Type.DemonsSoulsEDGE)
                    {
                        throw null;
                    }
                }
                else
                {
                    br.StepIn(fileOffset);
                    bytes = SFUtil.ReadZlib(br, (int)compressedSize);
                    br.StepOut();
                }
            }
            else
            {
                bytes = br.GetBytes(fileOffset, (int)compressedSize);
            }

            return(new BinderFile(flags, id, name, bytes));
        }
Пример #7
0
            internal Texture(BinaryReaderEx br, TPFPlatform platform, byte flag2, byte encoding)
            {
                uint fileOffset = br.ReadUInt32();
                int  fileSize   = br.ReadInt32();

                Format  = br.ReadByte();
                Type    = br.ReadEnum8 <TexType>();
                Mipmaps = br.ReadByte();
                Flags1  = br.AssertByte(0, 1, 2, 3);

                if (platform != TPFPlatform.PC)
                {
                    Header        = new TexHeader();
                    Header.Width  = br.ReadInt16();
                    Header.Height = br.ReadInt16();

                    if (platform == TPFPlatform.Xbox360)
                    {
                        br.AssertInt32(0);
                    }
                    else if (platform == TPFPlatform.PS3)
                    {
                        Header.Unk1 = br.ReadInt32();
                        if (flag2 != 0)
                        {
                            Header.Unk2 = br.AssertInt32(0, 0x69E0, 0xAAE4);
                        }
                    }
                    else if (platform == TPFPlatform.PS4 || platform == TPFPlatform.Xbone)
                    {
                        Header.TextureCount = br.AssertInt32(1, 6);
                        Header.Unk2         = br.AssertInt32(0xD);
                    }
                }

                uint nameOffset     = br.ReadUInt32();
                bool hasFloatStruct = br.AssertInt32(0, 1) == 1;

                if (platform == TPFPlatform.PS4 || platform == TPFPlatform.Xbone)
                {
                    Header.DXGIFormat = br.ReadInt32();
                }

                if (hasFloatStruct)
                {
                    FloatStruct = new FloatStruct(br);
                }

                Bytes = br.GetBytes(fileOffset, fileSize);
                if (Flags1 == 2 || Flags1 == 3)
                {
                    Bytes = DCX.Decompress(Bytes, out DCX.Type type);
                    if (type != DCX.Type.DCP_EDGE)
                    {
                        throw new NotImplementedException($"TPF compression is expected to be DCP_EDGE, but it was {type}");
                    }
                }

                if (encoding == 1)
                {
                    Name = br.GetUTF16(nameOffset);
                }
                else if (encoding == 0 || encoding == 2)
                {
                    Name = br.GetShiftJIS(nameOffset);
                }
            }
Пример #8
0
            internal Texture(BinaryReaderEx br, TPFPlatform platform, byte flag2, byte encoding)
            {
                uint fileOffset = br.ReadUInt32();
                int  fileSize   = br.ReadInt32();

                Format  = br.ReadByte();
                Type    = br.ReadEnum8 <TexType>();
                Mipmaps = br.ReadByte();
                Flags1  = br.AssertByte(0, 1, 2, 3);

                uint nameOffset = uint.MaxValue;

                if (platform == TPFPlatform.PC)
                {
                    Header     = null;
                    nameOffset = br.ReadUInt32();
                    Flags2     = br.AssertInt32(0, 1);
                }
                else
                {
                    Header        = new TexHeader();
                    Header.Width  = br.ReadInt16();
                    Header.Height = br.ReadInt16();

                    if (platform == TPFPlatform.Xbox360)
                    {
                        br.AssertInt32(0);
                        nameOffset = br.ReadUInt32();
                        br.AssertInt32(0);
                    }
                    else if (platform == TPFPlatform.PS3)
                    {
                        Header.Unk1 = br.ReadInt32();
                        if (flag2 != 0)
                        {
                            Header.Unk2 = br.AssertInt32(0, 0xAAE4);
                        }
                        nameOffset = br.ReadUInt32();
                        Flags2     = br.AssertInt32(0, 1);
                        if (Flags2 == 1)
                        {
                            Unk20 = br.ReadInt32();
                            Unk24 = br.ReadInt32();
                            Unk28 = br.ReadSingle();
                        }
                    }
                    else if (platform == TPFPlatform.PS4 || platform == TPFPlatform.Xbone)
                    {
                        Header.TextureCount = br.AssertInt32(1, 6);
                        Header.Unk2         = br.AssertInt32(0xD);
                        nameOffset          = br.ReadUInt32();
                        Flags2            = br.AssertInt32(0, 1);
                        Header.DXGIFormat = br.ReadInt32();
                    }
                }

                Bytes = br.GetBytes(fileOffset, fileSize);
                if (Flags1 == 2 || Flags1 == 3)
                {
                    Bytes = DCX.Decompress(Bytes);
                }

                if (encoding == 1)
                {
                    Name = br.GetUTF16(nameOffset);
                }
                else if (encoding == 0 || encoding == 2)
                {
                    Name = br.GetShiftJIS(nameOffset);
                }
            }