Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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>();
            }
        }
Ejemplo n.º 3
0
        public MSBT(string filename)
        {
            File = new FileInfo(filename);

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

                // Initialize Members
                LBL1.Labels          = new List <Entry>();
                ATR1.Attributes      = new List <UInt32>();
                TXT2.OriginalEntries = new List <Entry>();
                TXT2.Entries         = new List <Entry>();

                // Header
                Header.Identifier = br.ReadBytes(8);
                if (Encoding.ASCII.GetString(Header.Identifier) != "MsgStdBn")
                {
                    throw new Exception("File is not a valid MSBT.");
                }
                Header.ByteOrderMark = br.ReadBytes(2);

                // Byte Order
                br.ByteOrder = Header.ByteOrderMark[0] == 0xFF ? ByteOrder.LittleEndian : ByteOrder.BigEndian;

                Header.Unknown1         = br.ReadUInt16();
                Header.Unknown2         = br.ReadUInt16();
                Header.NumberOfSections = br.ReadUInt16();
                Header.Unknown3         = br.ReadUInt16();
                Header.FileSizeOffset   = (UInt32)br.BaseStream.Position;
                Header.FileSize         = br.ReadUInt32();
                Header.Unknown4         = br.ReadBytes(10);

                SectionOrder.Clear();
                for (int i = 0; i < Header.NumberOfSections; i++)
                {
                    // Section Detection
                    if (br.PeekString() == "LBL1")
                    {
                        ReadLBL1(br);
                        SectionOrder.Add("LBL1");
                    }
                    else if (br.PeekString() == "NLI1")
                    {
                        ReadNLI1(br);
                        SectionOrder.Add("NLI1");
                    }
                    else if (br.PeekString() == "ATR1")
                    {
                        ReadATR1(br);
                        SectionOrder.Add("ATR1");
                    }
                    else if (br.PeekString() == "TSY1")
                    {
                        ReadTSY1(br);
                        SectionOrder.Add("TSY1");
                    }
                    else if (br.PeekString() == "TXT2")
                    {
                        ReadTXT2(br);
                        SectionOrder.Add("TXT2");
                    }
                }

                br.Close();
            }
        }
Ejemplo n.º 4
0
		public MSBT(string filename)
		{
			File = new FileInfo(filename);

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

				// Initialize Members
				LBL1.Labels = new List<Entry>();
				ATR1.Attributes = new List<UInt32>();
				TXT2.OriginalEntries = new List<Entry>();
				TXT2.Entries = new List<Entry>();

				// Header
				Header.Identifier = br.ReadBytes(8);
				if (Encoding.ASCII.GetString(Header.Identifier) != "MsgStdBn")
					throw new InvalidMSBTException("The file provided is not a valid MSBT file.");
				Header.ByteOrderMark = br.ReadBytes(2);

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

				Header.Unknown1 = br.ReadUInt16();
				Header.Unknown2 = br.ReadUInt16();
				Header.NumberOfSections = br.ReadUInt16();
				Header.Unknown3 = br.ReadUInt16();
				Header.FileSizeOffset = (UInt32)br.BaseStream.Position;
				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++)
				{
					// Section Detection
					if (br.PeekString() == "LBL1")
					{
						ReadLBL1(br);
						SectionOrder.Add("LBL1");
					}
					else if (br.PeekString() == "NLI1")
					{
						ReadNLI1(br);
						SectionOrder.Add("NLI1");
					}
					else if (br.PeekString() == "ATR1")
					{
						ReadATR1(br);
						SectionOrder.Add("ATR1");
					}
					else if (br.PeekString() == "TSY1")
					{
						ReadTSY1(br);
						SectionOrder.Add("TSY1");
					}
					else if (br.PeekString() == "TXT2")
					{
						ReadTXT2(br);
						SectionOrder.Add("TXT2");
					}
				}

				br.Close();
			}
		}