예제 #1
0
        private void ReadLBL1(BinaryReaderX br)
        {
            // TODO: Continue reverse engineering the LBL1 section because the magic value below shouldn't be the end game
            long offset = br.BaseStream.Position;

            LBL1.Identifier  = br.ReadBytes(4);
            LBL1.SectionSize = br.ReadUInt32();
            LBL1.Unknown1    = br.ReadBytes(8);
            LBL1.Unknown2    = br.ReadBytes(8);
            uint startOfLabels = 0x35C;             // Magic LBL1 label start position

            LBL1.Unknown3 = br.ReadBytes((int)(startOfLabels - br.BaseStream.Position));

            while (br.BaseStream.Position < (offset + LBL1.Identifier.Length + sizeof(UInt32) + LBL1.Unknown1.Length + LBL1.SectionSize))
            {
                Entry lbl = new Entry();
                lbl.Length = Convert.ToUInt32(br.ReadByte());
                lbl.Value  = br.ReadBytes((int)lbl.Length);
                lbl.Index  = br.ReadInt32();
                LBL1.Labels.Add(lbl);
            }

            HasLabels = LBL1.Labels.Count > 0;

            PaddingSeek(br);
        }
예제 #2
0
        private void ReadLBL1(BinaryReaderX br)
        {
            bool eoi    = false;
            long offset = br.BaseStream.Position;

            LBL1.Identifier  = br.ReadBytes(4);
            LBL1.SectionSize = br.ReadUInt32();
            LBL1.Unknown1    = br.ReadBytes(8);
            LBL1.Unknown2    = br.ReadBytes(8);
            uint startOfLabels = 0x35C;             // Magic LBL1 label start position

            LBL1.Unknown3 = br.ReadBytes((int)(startOfLabels - br.BaseStream.Position));

            while (br.BaseStream.Position < (offset + LBL1.Identifier.Length + sizeof(UInt32) + LBL1.Unknown1.Length + LBL1.SectionSize))
            {
                Entry lbl = new Entry();
                lbl.Length = Convert.ToUInt32(br.ReadByte());
                lbl.Value  = br.ReadBytes((int)lbl.Length);
                lbl.ID     = br.ReadInt32();
                LBL1.Labels.Add(lbl);
            }

            HasLabels = LBL1.Labels.Count > 0;

            PaddingSeek(br);
        }
예제 #3
0
        private void PaddingSeek(BinaryReaderX br)
        {
            long remainder = br.BaseStream.Position % 16;

            if (remainder > 0)
            {
                paddingChar = br.ReadByte();
                br.BaseStream.Seek(-1, SeekOrigin.Current);
                br.BaseStream.Seek(16 - remainder, SeekOrigin.Current);
            }
        }
예제 #4
0
        // Reading
        private void ReadLBL1(BinaryReaderX br)
        {
            LBL1.Identifier  = br.ReadString(4);
            LBL1.SectionSize = br.ReadUInt32();
            LBL1.Padding1    = br.ReadBytes(8);
            long startOfLabels = br.BaseStream.Position;

            LBL1.NumberOfGroups = br.ReadUInt32();

            for (int i = 0; i < LBL1.NumberOfGroups; i++)
            {
                Group grp = new Group();
                grp.NumberOfLabels = br.ReadUInt32();
                grp.Offset         = br.ReadUInt32();
                LBL1.Groups.Add(grp);
            }

            foreach (Group grp in LBL1.Groups)
            {
                br.BaseStream.Seek(startOfLabels + grp.Offset, SeekOrigin.Begin);

                for (int i = 0; i < grp.NumberOfLabels; i++)
                {
                    Label lbl = new Label();

                    lbl.Length   = Convert.ToUInt32(br.ReadByte());
                    lbl.Name     = br.ReadString((int)lbl.Length);
                    lbl.Index    = br.ReadUInt32();
                    lbl.Checksum = (uint)LBL1.Groups.IndexOf(grp);
                    LBL1.Labels.Add(lbl);
                }
            }


            // Old rename correction
            foreach (Label lbl in LBL1.Labels)
            {
                uint previousChecksum = lbl.Checksum;
                lbl.Checksum = LabelChecksum(lbl.Name);

                if (previousChecksum != lbl.Checksum)
                {
                    LBL1.Groups[(int)previousChecksum].NumberOfLabels -= 1;
                    LBL1.Groups[(int)lbl.Checksum].NumberOfLabels     += 1;
                }
            }

            if (LBL1.Labels.Count > 0)
            {
                HasLabels = true;
            }

            PaddingSeek(br);
        }
예제 #5
0
        public static string Extract(string filename, string path, bool overwrite = true)
        {
            string result = "Files successfully extracted.";

            if (File.Exists(filename) && new FileInfo(filename).Length > 0)
            {
                FileStream    fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None);
                BinaryReaderX br = new BinaryReaderX(fs);

                try
                {
                    string magic = Encoding.ASCII.GetString(br.ReadBytes(4));

                    if (magic != "BG4\0")
                    {
                        throw new InvalidBG4Exception("The file provided is not a valid BG4 archive.");
                    }

                    ushort Constant1                 = br.ReadUInt16();
                    ushort NumberOfHeaders           = br.ReadUInt16();
                    uint   SizeOfHeaders             = br.ReadUInt32();
                    ushort NumberOfHeadersDerived    = br.ReadUInt16();
                    ushort NumberOfHeadersMultiplier = br.ReadUInt16();

                    // Read in file headers
                    List <FileEntry> entries = new List <FileEntry>();
                    FileEntry        entry   = new FileEntry();

                    for (int i = 0; i < NumberOfHeaders; i++)
                    {
                        uint offsetTemp = br.ReadUInt32();
                        if ((offsetTemp & 0x80000000) == 0x80000000)
                        {
                            entry.Compressed = true;
                        }
                        entry.Offset    = entry.Compressed ? (offsetTemp ^ 0x80000000) : offsetTemp;
                        entry.Size      = br.ReadUInt32();
                        entry.Unknown1  = br.ReadUInt32();
                        entry.NameIndex = br.ReadUInt16();

                        if (entry.Unknown1 != 0xFFFFFFFF)
                        {
                            entries.Add(entry);
                        }

                        entry = new FileEntry();
                    }

                    // Sort the file entries into NameIndex order
                    entries.Sort();

                    // Read in file names
                    bool          eofn      = false;
                    List <string> filenames = new List <string>();

                    while (!eofn)
                    {
                        if (br.PeekString(2) == Encoding.ASCII.GetString(new byte[] { 0xFF, 0xFF }))
                        {
                            eofn = true;
                        }
                        else
                        {
                            bool   eos  = false;
                            string name = string.Empty;

                            while (!eos)
                            {
                                byte chr = br.ReadByte();

                                if (chr == 0x00)
                                {
                                    eos = true;
                                }
                                else
                                {
                                    name += (char)chr;
                                }
                            }

                            if (name != "(invalid)")
                            {
                                filenames.Add(name);
                            }
                        }
                    }

                    // Extract!
                    for (int i = 0; i < entries.Count; i++)
                    {
                        FileEntry fe        = entries[i];
                        string    extension = (!Regex.IsMatch(filenames[i], @"\..*?$") ? ".bin" : string.Empty);

                        if (extension != string.Empty)
                        {
                            br.BaseStream.Seek(fe.Offset, SeekOrigin.Begin);
                            magic = Encoding.ASCII.GetString(br.ReadBytes(8));

                            if (magic.StartsWith("MsgStdBn"))
                            {
                                extension = ".msbt";
                            }
                            else if (magic.StartsWith("BCH"))
                            {
                                extension = ".bch";
                            }
                            else if (magic.StartsWith("PTX"))
                            {
                                extension = ".ptx";
                            }

                            // TODO: Add more known magic/extension pairs
                        }

                        Debug.Print("[" + fe.Offset.ToString("X8") + "] " + fe.NameIndex + " (" + fe.Unknown1 + ") " + filenames[i] + extension);

                        FileInfo      fi  = new FileInfo(filename);
                        FileStream    fsr = new FileStream(Path.Combine(path, filenames[i] + extension), FileMode.Create, FileAccess.Write, FileShare.None);
                        BinaryWriterX bw  = new BinaryWriterX(fsr);

                        br.BaseStream.Seek(fe.Offset, SeekOrigin.Begin);
                        bw.Write(br.ReadBytes((int)fe.Size));
                        bw.Close();
                    }

                    result = NumberOfHeaders + " header(s) were scanned and " + entries.Count + " files were successfully extracted!";
                }
                catch (InvalidBG4Exception ibex)
                {
                    result = ibex.Message;
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
                finally
                {
                    br.Close();
                }
            }

            return(result);
        }
예제 #6
0
        private void ReadTXT2(BinaryReaderX br)
        {
            TXT2.Identifier  = br.ReadString(4);
            TXT2.SectionSize = br.ReadUInt32();
            TXT2.Padding1    = br.ReadBytes(8);
            long startOfStrings = br.BaseStream.Position;

            TXT2.NumberOfStrings = br.ReadUInt32();

            List <UInt32> offsets = new List <UInt32>();

            for (int i = 0; i < TXT2.NumberOfStrings; i++)
            {
                offsets.Add(br.ReadUInt32());
            }
            for (int i = 0; i < TXT2.NumberOfStrings; i++)
            {
                String str        = new String();
                UInt32 nextOffset = (i + 1 < offsets.Count) ? ((UInt32)startOfStrings + offsets[i + 1]) : ((UInt32)startOfStrings + TXT2.SectionSize);

                br.BaseStream.Seek(startOfStrings + offsets[i], SeekOrigin.Begin);

                List <byte> result = new List <byte>();
                while (br.BaseStream.Position < nextOffset && br.BaseStream.Position < Header.FileSize)
                {
                    if (Header.EncodingByte == EncodingByte.UTF8)
                    {
                        result.Add(br.ReadByte());
                    }
                    else
                    {
                        byte[] unichar = br.ReadBytes(2);

                        if (br.ByteOrder == ByteOrder.BigEndian)
                        {
                            Array.Reverse(unichar);
                        }

                        result.AddRange(unichar);
                    }
                }
                str.Value = result.ToArray();
                str.Index = (uint)i;

                TXT2.OriginalStrings.Add(str);

                // Duplicate entries for editing
                String estr = new String();
                estr.Value = str.Value;
                estr.Index = str.Index;
                TXT2.Strings.Add(estr);
            }

            // Tie in LBL1 labels
            foreach (Label lbl in LBL1.Labels)
            {
                lbl.String = TXT2.Strings[(int)lbl.Index];
            }

            PaddingSeek(br);
        }
예제 #7
0
        public MSBT(string filename = "null")
        {
            if (filename != "null")
            {
                File = new FileInfo(filename);

                if (File.Exists && filename.Length > 0)
                {
                    FileStream    fs = System.IO.File.Open(File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    BinaryReaderX br = new BinaryReaderX(fs);

                    // Initialize Members
                    LBL1.Groups          = new List <Group>();
                    LBL1.Labels          = new List <IEntry>();
                    TXT2.Strings         = new List <IEntry>();
                    TXT2.OriginalStrings = new List <IEntry>();

                    // Header
                    Header.Identifier = br.ReadString(8);
                    if (Header.Identifier != "MsgStdBn")
                    {
                        throw new InvalidMSBTException("The file provided is not a valid MSBT file.");
                    }

                    // Byte Order
                    Header.ByteOrderMark = br.ReadBytes(2);
                    br.ByteOrder         = Header.ByteOrderMark[0] > Header.ByteOrderMark[1] ? ByteOrder.LittleEndian : ByteOrder.BigEndian;

                    Header.Unknown1 = br.ReadUInt16();

                    // Encoding
                    Header.EncodingByte = (EncodingByte)br.ReadByte();
                    FileEncoding        = (Header.EncodingByte == EncodingByte.UTF8 ? Encoding.UTF8 : Encoding.Unicode);

                    Header.Unknown2         = br.ReadByte();
                    Header.NumberOfSections = br.ReadUInt16();
                    Header.Unknown3         = br.ReadUInt16();
                    Header.FileSizeOffset   = (UInt32)br.BaseStream.Position; // Record offset for future use
                    Header.FileSize         = br.ReadUInt32();
                    Header.Unknown4         = br.ReadBytes(10);

                    if (Header.FileSize != br.BaseStream.Length)
                    {
                        throw new InvalidMSBTException("The file provided is not a valid MSBT file.");
                    }

                    SectionOrder.Clear();
                    for (int i = 0; i < Header.NumberOfSections; i++)
                    {
                        switch (br.PeekString())
                        {
                        case "LBL1":
                            ReadLBL1(br);
                            SectionOrder.Add("LBL1");
                            break;

                        case "NLI1":
                            ReadNLI1(br);
                            SectionOrder.Add("NLI1");
                            break;

                        case "ATO1":
                            ReadATO1(br);
                            SectionOrder.Add("ATO1");
                            break;

                        case "ATR1":
                            ReadATR1(br);
                            SectionOrder.Add("ATR1");
                            break;

                        case "TSY1":
                            ReadTSY1(br);
                            SectionOrder.Add("TSY1");
                            break;

                        case "TXT2":
                            ReadTXT2(br);
                            SectionOrder.Add("TXT2");
                            break;
                        }
                    }

                    br.Close();
                }
            }
            else
            {
                // Initialize Members
                LBL1.Groups          = new List <Group>();
                LBL1.Labels          = new List <IEntry>();
                TXT2.Strings         = new List <IEntry>();
                TXT2.OriginalStrings = new List <IEntry>();
            }
        }
예제 #8
0
		private void PaddingSeek(BinaryReaderX br)
		{
			long remainder = br.BaseStream.Position % 16;
			if (remainder > 0)
			{
				paddingChar = br.ReadByte();
				br.BaseStream.Seek(-1, SeekOrigin.Current);
				br.BaseStream.Seek(16 - remainder, SeekOrigin.Current);
			}
		}
예제 #9
0
		private void ReadLBL1(BinaryReaderX br)
		{
			// TODO: Continue reverse engineering the LBL1 section because the magic value below shouldn't be the end game
			long offset = br.BaseStream.Position;
			LBL1.Identifier = br.ReadBytes(4);
			LBL1.SectionSize = br.ReadUInt32();
			LBL1.Unknown1 = br.ReadBytes(8);
			LBL1.Unknown2 = br.ReadBytes(8);
			uint startOfLabels = 0x35C; // Magic LBL1 label start position
			LBL1.Unknown3 = br.ReadBytes((int)(startOfLabels - br.BaseStream.Position));

			while (br.BaseStream.Position < (offset + LBL1.Identifier.Length + sizeof(UInt32) + LBL1.Unknown1.Length + LBL1.SectionSize))
			{
				Entry lbl = new Entry();
				lbl.Length = Convert.ToUInt32(br.ReadByte());
				lbl.Value = br.ReadBytes((int)lbl.Length);
				lbl.Index = br.ReadInt32();
				LBL1.Labels.Add(lbl);
			}

			HasLabels = LBL1.Labels.Count > 0;

			PaddingSeek(br);
		}