-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBG4.cs
160 lines (132 loc) · 4.08 KB
/
BG4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace MsbtEditor.BG4
{
public class FileEntry : IComparable<FileEntry>
{
public UInt32 Offset;
public UInt32 Size;
public UInt32 Unknown1;
public UInt16 NameIndex;
public bool Compressed;
public int CompareTo(FileEntry rhs)
{
return NameIndex.CompareTo(rhs.NameIndex);
}
}
public class InvalidBG4Exception : Exception
{
public InvalidBG4Exception(string message) : base(message) { }
}
public class BG4
{
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;
}
public static void Pack(string filename, string path)
{
throw new NotImplementedException();
}
}
}