-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmg.go
305 lines (256 loc) · 7.32 KB
/
bmg.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"bytes"
"encoding/binary"
"encoding/xml"
"errors"
"io"
"log"
"os"
"strings"
"unicode/utf16"
)
var (
// FileMagic is the byte representation of "MESGbmg1".
FileMagic = [8]byte{'M', 'E', 'S', 'G', 'b', 'm', 'g', '1'}
ErrInvalidMagic = errors.New("provided BMG has an invalid magic")
ErrUnsupportedEncoding = errors.New("provided BMG uses a text encoding not supported")
ErrInvalidSection = errors.New("provided BMG has an invalid section size")
)
// CharsetTypes represents the enum for possible charsets within a BMG.
type CharsetTypes byte
const (
CharsetUndefined CharsetTypes = iota
CharsetCP1252
CharsetUTF16
CharsetShiftJIS
CharsetUTF8
)
const (
NullStringPlaceholder = "==== THIS STRING INTENTIONALLY LEFT NULL ===="
LessThanPlaceholder = "##LESS_THAN_SYMBOL##"
GreaterThanPlaceholder = "##GREATER_THAN_SYMBOL##"
)
// BMG represents the internal structure of our BMG.
type BMG struct {
INF *INF
DAT DAT
MID []MID
}
// SectionTypes are known parts of a BMG.
type SectionTypes [4]byte
var (
SectionTypeINF1 SectionTypes = [4]byte{'I', 'N', 'F', '1'}
SectionTypeDAT1 SectionTypes = [4]byte{'D', 'A', 'T', '1'}
SectionTypeMID1 SectionTypes = [4]byte{'M', 'I', 'D', '1'}
)
// BMGHeader is taken from http://wiki.tockdom.com/w/index.php?title=BMG_%28File_Format%29.
type BMGHeader struct {
// Magic is only "MESG" followed by "bmg1",
// but bmg1 following sequentially is the only value.
Magic [8]byte
FileSize uint32
SectionCount uint32
Charset CharsetTypes
// Appears to be padding.
_ [15]byte
}
// SectionHeader allows us to read section header info.
type SectionHeader struct {
Type SectionTypes
Size uint32
}
// XMLFormat specifies XML necessities for marshalling and unmarshalling.
type XMLFormat struct {
MessageID MID `xml:"key,attr"`
Attributes uint32 `xml:"attributes,attr"`
String string `xml:",innerxml"`
}
type Translations struct {
XMLName xml.Name `xml:"root"`
Translation []XMLFormat `xml:"str"`
}
func (b BMG) ReadString(entry INFEntry) []rune {
return b.DAT.ReadOffset(entry.Offset)
}
func parseBMG(data []byte) ([]byte, error) {
// Create a new reader for serialization
readable := bytes.NewReader(data)
var header BMGHeader
err := binary.Read(readable, binary.BigEndian, &header)
if err != nil {
return nil, err
}
// Validate header
if !bytes.Equal(FileMagic[:], header.Magic[:]) {
return nil, ErrInvalidMagic
}
if readable.Size() != int64(header.FileSize) {
return nil, io.ErrUnexpectedEOF
}
if header.Charset != CharsetUTF16 {
return nil, ErrUnsupportedEncoding
}
var currentBMG BMG
// Read sections
for count := header.SectionCount; count != 0; count-- {
var sectionHeader SectionHeader
err = binary.Read(readable, binary.BigEndian, §ionHeader)
if err != nil {
return nil, err
}
// Subtract the header size
sectionSize := int(sectionHeader.Size) - 8
temp := make([]byte, sectionSize)
_, err = readable.Read(temp)
if err != nil {
return nil, err
}
// Add to sections
switch sectionHeader.Type {
case SectionTypeINF1:
currentBMG.INF, err = NewINF(temp)
case SectionTypeDAT1:
currentBMG.DAT = NewDAT(temp)
case SectionTypeMID1:
currentBMG.MID, err = NewMID(temp)
default:
log.Println("unhandled type", string(sectionHeader.Type[:]))
}
if err != nil {
return nil, err
}
}
if len(currentBMG.INF.Entries) != len(currentBMG.MID) {
return nil, ErrInvalidSection
}
var output []XMLFormat
for index, entry := range currentBMG.INF.Entries {
currentString := string(currentBMG.ReadString(entry))
currentString = strings.ReplaceAll(currentString, "<", LessThanPlaceholder)
currentString = strings.ReplaceAll(currentString, ">", GreaterThanPlaceholder)
if currentString == "" {
currentString = NullStringPlaceholder
}
xmlNode := XMLFormat{
MessageID: currentBMG.MID[index],
Attributes: binary.BigEndian.Uint32(entry.Attributes[:]),
String: currentString,
}
output = append(output, xmlNode)
}
return xml.MarshalIndent(Translations{Translation: output}, "", "\t")
}
func createBMG(input []byte, output string) error {
var bmg Translations
err := xml.Unmarshal(input, &bmg)
if err != nil {
return err
}
var utf16Strings []uint16
var mid []uint32
var offsets []uint32
var attributes []uint32
// Initial padding
utf16Strings = append(utf16Strings, uint16(0))
// The first offset will always be 2
offsets = append(offsets, uint32(2))
for i, format := range bmg.Translation {
// Append the message ID
mid = append(mid, uint32(format.MessageID))
// Now the strings
currentString := format.String
currentString = strings.ReplaceAll(currentString, LessThanPlaceholder, "<")
currentString = strings.ReplaceAll(currentString, GreaterThanPlaceholder, ">")
if currentString == NullStringPlaceholder {
} else {
utf16Strings = append(utf16Strings, utf16.Encode([]rune(currentString))...)
utf16Strings = append(utf16Strings, uint16(0))
}
offsets = append(offsets, uint32(len(utf16Strings)*2))
// Finally, append the attributes
attributes = append(attributes, format.Attributes)
// On the last index add 28 bytes of padding for the text.
if i == len(bmg.Translation)-1 {
for i2 := 0; i2 < 14; i2++ {
utf16Strings = append(utf16Strings, uint16(0))
}
}
}
// Apply 4 bytes of padding to MID
mid = append(mid, uint32(0))
// Now that we have all our data, construct the INF block
var inf []uint32
for i, _ := range bmg.Translation {
inf = append(inf, offsets[i])
inf = append(inf, attributes[i])
// On the last index add 24 bytes of padding for the INF block
if i == len(bmg.Translation)-1 {
for i2 := 0; i2 < 6; i2++ {
inf = append(inf, uint32(0))
}
}
}
// Add up the size of the headers and data to get filesize
filesize := uint32(72 + (len(utf16Strings) * 2) + (len(mid) * 4) + (len(inf) * 4))
// Create the BMG header
bmgHeader := BMGHeader{
Magic: FileMagic,
FileSize: filesize,
SectionCount: 3,
Charset: CharsetUTF16,
}
// Then INF header
infHeader := struct {
SectionType SectionHeader
INFHeader INFHeader
}{
SectionType: SectionHeader{
Type: SectionTypeINF1,
Size: uint32(len(inf)*4 + 16),
},
INFHeader: INFHeader{
EntryCount: uint16(len(mid) - 1),
EntryLength: 8,
GroupID: 0,
DefaultColor: 0,
},
}
// DAT header
datHeader := SectionHeader{
Type: SectionTypeDAT1,
Size: uint32(len(utf16Strings)*2 + 8),
}
// Finally, MID header
midHeader := struct {
SectionType SectionHeader
MIDHeader MIDHeader
}{
SectionType: SectionHeader{
Type: SectionTypeMID1,
Size: uint32(len(mid)*4 + 16),
},
MIDHeader: MIDHeader{
SectionCount: uint16(len(mid) - 1),
Format: uint8(10),
Info: uint8(1),
},
}
create, err := os.Create(output)
if err != nil {
return err
}
// Now write all the parts of the BMG
err = binary.Write(create, binary.BigEndian, bmgHeader)
if err != nil {
return err
}
// If it didn't error upon initial creation of file, we should be fine not handling errors for the rest
binary.Write(create, binary.BigEndian, infHeader)
binary.Write(create, binary.BigEndian, inf)
binary.Write(create, binary.BigEndian, datHeader)
binary.Write(create, binary.BigEndian, utf16Strings)
binary.Write(create, binary.BigEndian, midHeader)
binary.Write(create, binary.BigEndian, mid)
return nil
}