-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.ts
More file actions
249 lines (214 loc) · 7.14 KB
/
Copy pathdecode.ts
File metadata and controls
249 lines (214 loc) · 7.14 KB
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
import type { Buffer } from "node:buffer"
import { Decoder } from "binary-util"
import { LanguageCodes } from "./constants.ts"
import { decrypt } from "./crypto.ts"
import type { REMsg } from "./types.ts"
import { bufferToUUID, extractStringMap, hashString } from "./utils.ts"
// Internal types
type MsgHeaderParsed = {
magic: "GMSG"
version: number
headerOffset: bigint
entryCount: number
attributeCount: number
langCount: number
dataOffset: bigint
langOffset: bigint
attributesOffset: bigint
attributeNamesOffset: bigint
}
type MsgAttributeHeaderParsed = {
type: number
nameOffset: bigint
name: string
}
type MsgEntryHeaderParsed = {
id: string
crc: number
hash: number
nameOffset: bigint
attributesOffset: bigint
contentOffsetsByLang: bigint[]
}
type MsgEntryParsed = {
header: MsgEntryHeaderParsed
name: string
attributes: Array<string | number>
strings: Record<string, string>
}
const parseHeader = (parser: Decoder): MsgHeaderParsed => ({
version: parser.readUint32(),
magic: parser.readString({ length: 4 }) as "GMSG",
headerOffset: parser.readUint64(),
entryCount: parser.readUint32(),
attributeCount: parser.readUint32(),
langCount: parser.readUint32(),
["_" as never]: parser.alignTo(8),
dataOffset: parser.readUint64(),
["_2" as never]: parser.seek(8),
langOffset: parser.readUint64(),
attributesOffset: parser.readUint64(),
attributeNamesOffset: parser.readUint64(),
})
export const decodeMsg = (data: Buffer): REMsg => {
const parser = new Decoder(data)
const header = parseHeader(parser)
const entries = [] as MsgEntryParsed[]
if (header.magic !== "GMSG") {
throw new Error(`Invalid magic: ${header.magic as string}`)
}
if (header.version !== 539100710 && header.version !== 23) {
throw new Error(`Unknown version: ${header.version}`)
}
const entryHeaderOffsets = [] as bigint[]
for (let i = 0; i < header.entryCount; i++) {
entryHeaderOffsets.push(parser.readUint64())
}
// unknown data (probably header separator?)
parser.seek(8)
const languages = [] as (typeof LanguageCodes)[number][]
for (let i = 0; i < header.langCount; i++) {
const language = LanguageCodes[parser.readInt32()]!
if (languages == null) throw new Error(`Unknown language: ${language}`)
languages.push(language)
}
parser.alignTo(8)
if (Number(header.attributesOffset) !== parser.currentOffset) {
throw new Error(
`Attributes offset mismatch: ${header.attributesOffset} != ${parser.currentOffset}`,
)
}
const attributesHeaders = [] as MsgAttributeHeaderParsed[]
for (let i = 0; i < header.attributeCount; i++) {
attributesHeaders.push({
type: parser.readInt32(),
nameOffset: null!,
name: null!,
})
}
parser.alignTo(8)
if (Number(header.attributeNamesOffset) !== parser.currentOffset) {
throw new Error(
`Attribute names offset mismatch: ${header.attributeNamesOffset} != ${parser.currentOffset}`,
)
}
for (let i = 0; i < header.attributeCount; i++) {
attributesHeaders[i]!.nameOffset = parser.readUint64()
}
if (header.entryCount !== 0) {
for (let i = 0; i < header.entryCount; i++) {
if (Number(entryHeaderOffsets[i]) !== parser.currentOffset) {
throw new Error(
`Entry header offset mismatch: $${parser.currentOffset} != entry:${i}:${entryHeaderOffsets[i]}`,
)
}
const entryHeader = {
id: bufferToUUID(parser.readBuffer({ length: 16 })),
crc: parser.readUint32(),
hash: parser.readUint32(),
nameOffset: parser.readUint64(),
attributesOffset: parser.readUint64(),
contentOffsetsByLang: [] as bigint[],
} satisfies MsgEntryHeaderParsed
for (let j = 0; j < header.langCount; j++) {
entryHeader.contentOffsetsByLang.push(parser.readUint64())
}
entries.push({ header: entryHeader } as MsgEntryParsed)
}
for (let i = 0; i < header.entryCount; i++) {
const entry = entries[i]!
if (Number(entry.header.attributesOffset) !== parser.currentOffset) {
throw new Error(
`Entry header attributes offset mismatch: $${parser.currentOffset} != entry:${i}:${entry.header.attributesOffset}`,
)
}
entry.attributes = []
for (let j = 0; j < attributesHeaders.length; j++) {
const attrHeader = attributesHeaders[j]!
switch (attrHeader.type) {
// "null" string pointer
case -1:
entry.attributes[j] = Number(parser.readUint64())
break
// int
case 0:
entry.attributes[j] = Number(parser.readUint64())
break
// float/double
case 1:
entry.attributes[j] = parser.readDouble()
break
// string pointer
case 2:
entry.attributes[j] = Number(parser.readUint64())
break
default:
throw new Error(`Not implemented attribute type ${attrHeader.type}`)
}
}
}
if (Number(header.dataOffset) !== parser.currentOffset) {
throw new Error(`Data offset mismatch: $${header.dataOffset} != ${parser.currentOffset}`)
}
const decryptedStringsBuffer = decrypt(data.subarray(parser.currentOffset))
const stringMap = extractStringMap(decryptedStringsBuffer, parser.currentOffset)
for (let i = 0; i < attributesHeaders.length; i++) {
const attributeHeader = attributesHeaders[i]!
attributeHeader.name = stringMap.get(Number(attributeHeader.nameOffset))!
}
for (let i = 0; i < entries.length; i++) {
const entry = entries[i]!
for (let j = 0; j < header.attributeCount; j++) {
switch (attributesHeaders[j]!.type) {
case 2:
entry.attributes[j] = stringMap.get(entry.attributes[j] as number)!
break
case -1:
entry.attributes[j] = stringMap.get(entry.attributes[j] as number)!
if (entry.attributes[j] !== "" && entry.attributes[j] !== "\x00") {
throw new Error(
`Attribute with type -1 is not empty string: "${entry.attributes[j]}"`,
)
}
break
}
}
}
for (let i = 0; i < entries.length; i++) {
const entry = entries[i]!
entry.name = stringMap.get(Number(entry.header.nameOffset))!
entry.strings = {}
for (let j = 0; j < entry.header.contentOffsetsByLang.length; j++) {
const offset = Number(entry.header.contentOffsetsByLang[j])
entry.strings[languages[j]!] = stringMap.get(offset)!
}
}
}
const parsedEntries: REMsg["entries"] = []
for (const { header, name, attributes, strings } of entries) {
const hash = hashString(name)
if (hash !== header.hash) {
throw new Error(`Hash mismatch for entry ${name}: ${hash} != ${header.hash}`)
}
parsedEntries.push({
meta: {
id: header.id,
crc: header.crc,
hash: header.hash,
},
name,
attributes,
strings,
})
}
return {
meta: {
version: header.version,
attributes: attributesHeaders.map((attr) => ({
type: attr.type,
name: attr.name,
})),
},
entries: parsedEntries,
}
}