-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompiler.js
323 lines (284 loc) · 8.47 KB
/
compiler.js
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const util = require('util')
const kleur = require('kleur')
function padCeil(value, divisor = 0x10) {
return Math.ceil(value / divisor) * divisor
}
const Long = BigInt
function compile(obj) {
const endian = obj.endian || 'LE'
function Str({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer.write(value, index + offset)
}
function UInt({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer[`writeUInt32${endian}`](value, index + offset)
}
UInt.size = 0x04
function BigUInt({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer[`writeBigUInt64${endian}`](Long(value), index + offset)
}
BigUInt.size = 0x08
function Int({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer[`writeInt32${endian}`](value, index + offset)
}
Int.size = 0x04
function BigInt({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer[`writeBigInt64${endian}`](Long(value), index + offset)
}
BigInt.size = 0x08
function Float({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer[`writeFloat${endian}`](value, index + offset)
}
Float.size = 0x04
function Double({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer[`writeDouble${endian}`](value, index + offset)
}
Double.size = 0x08
function Hex({ buffer, index = 0x00 }, value, offset = 0x00) {
return buffer.write(value.padStart(8, '0'), index + offset, 'hex')
}
function Tuple(Type, size) {
const block = Type.size || 0x04
function TupleDef(cursor, value, offset) {
(value || [])
.slice(0, size)
.forEach((v, i) => Type(cursor, v, offset + i * block))
}
TupleDef.size = size * block
return TupleDef
}
const deferred = []
function Defer(cursor, buffer, offset = 0x00) {
deferred.push({ buffer, cursor: cursor.clone(), offset })
}
Defer.size = 0x04
function unrollDeferred() {
for(const { buffer, cursor, offset } of deferred) {
const allocated = malloc(padCeil(buffer.length))
buffer.copy(allocated.buffer)
Int(cursor, cursor.pointer(allocated.pos), offset)
}
}
const deferredStrings = new Set(obj.strings || [])
const stringDefers = []
function DeferStr(cursor, str, offset = 0x00) {
str = str.trim()
deferredStrings.add(str)
stringDefers.push({ str, cursor: cursor.clone(), offset })
}
DeferStr.size = 0x04
function unrollStrings() {
const nullString = '\0'
const strings = Array.from(deferredStrings)
if(!obj.strings) { // Sort unless order defined manually
strings.sort()
}
// SGO sorts this nullstring at the end, js wants it first
// For consistency, perform that manually
if(strings[0] === nullString) {
strings.splice(0, 1)
strings.push(nullString)
}
const stringCursors = {}
for(let str of strings) {
const written = str + '\0'
const cursor = malloc(stringBytes(written))
cursor.buffer.write(written, 'utf16le')
if(endian !== 'LE') cursor.buffer.swap16()
stringCursors[str] = cursor
}
for(const { str, cursor, offset } of stringDefers) {
Int(cursor, cursor.pointer(stringCursors[str].pos), offset)
}
}
function stringBytes(string) {
return Buffer.byteLength(string, 'utf16le')
}
// Like Ref, but null values are written as 0 instead of a pointer to nowhere
function DRef(cursor, value, offset) {
if(!value) return Int(cursor, 0, offset)
return Ref(cursor, value, offset)
}
function Ref(cursor, value, offset) {
if(!value) return Int(cursor, cursor.pointer(heapIdx), offset)
if(!(value instanceof Cursor)) {
throw new Error('Value expected to be a cursor')
}
const jump = cursor.pointer(value.pos)
Int(cursor, jump, offset)
}
function Null() {
return null
}
function Copy(cursor, buffer) {
buffer.copy(cursor.buffer)
}
class Cursor {
constructor(buffer, pos, index = 0x00) {
if(buffer instanceof Cursor) {
this.buffer = buffer.buffer
this.pos = buffer.pos
this.index = buffer.index
this.writeCount = buffer.writeCount
this.endian = buffer.endian
} else {
this.buffer = buffer
this.pos = pos
this.index = index
this.writeCount = 0
this.endian = endian
}
}
[util.inspect.custom]() {
const startAt = Math.max(0, Math.floor((this.pos / 0x10) - 1) * 0x10)
const endAt = Math.min(startAt + 0x40, this.buffer.length)
let bufferView = []
for(let i = startAt; i < endAt; i += 0x2) {
if(!(i % 0x10)) {
bufferView.push([kleur.magenta(`${i.toString(16).padStart(8, 0)}:`)])
}
let str = this.buffer.readUInt16BE(i).toString(16).padStart(4, 0)
if(this.pos === i) {
str = kleur.yellow(str)
}
bufferView[bufferView.length - 1].push(str)
}
return `Cursor 0x${this.pos.toString(16)} (${this.endian})
${bufferView.map(r => r.join(' ')).join('\n')}`
}
write(Type, value, off, tmp, opts = {}) {
if(this.index > this.buffer.length - Type.size) {
throw new Error('End of buffer exceeded')
}
if(!Type.size && !opts.size) {
throw new Error('Trying to write a type without a size!')
}
Type(this, value, off, tmp)
this.index += opts.size || Type.size || 0
this.writeCount++
return this
}
clone() {
return new Cursor(this)
}
pointer(pos) {
return pos - this.pos - this.index
}
}
const heap = []
let heapIdx = 0x00
function malloc(size, opts = {}) {
if(size == null) throw new Error('No size provided to allocate (0 is valid)')
if(opts.padding) {
size = padCeil(size, opts.padding)
}
if(opts.padding && heapIdx % opts.padding) {
malloc(opts.padding - (heapIdx % opts.padding))
}
const pos = heapIdx
const buffer = Buffer.alloc(size)
heap.push(buffer)
heapIdx += size
return new Cursor(buffer, pos)
}
function Allocate(Type, cb, opts = {}) {
function AllocateDef(data, cursor, off = 0x00, tmp = {}) {
if(isNaN(off)) { // tmp is third parameter
tmp = off
off = 0x00
}
const value = cb(data, cursor, tmp)
if(!value) return null
const writeOpts = {}
let size = Type.size
if(Buffer.isBuffer(value)) {
size = value.length
writeOpts.size = size
}
if(opts.padding) {
writeOpts.size = size
}
return malloc(size, opts).write(Type, value, off, tmp, writeOpts)
}
AllocateDef.size = 0x04
return AllocateDef
}
function Unknowns(cursor, obj, offset = 0x00) {
for(const key of Object.keys(obj)) {
if(!key.startsWith('0x')) continue
const pos = parseInt(key.slice(2), 16)
const value = obj[key]
Hex(cursor, value, offset + pos)
}
}
function Struct(definitions, size) {
const block = 0x04
if(!size) size = Math.max(...definitions).map(([k]) => +k) + block
function StructDef(cursor, data) {
if(!data) return null
Unknowns(cursor, data)
const tmp = {}
for(const [off, writeFn, valueFn] of definitions) {
const value = valueFn(data, cursor, tmp)
writeFn(cursor, value, off, tmp)
}
return cursor
}
StructDef.size = size
return StructDef
}
function Union(prop, Types, size) {
function UnionDef(cursor, data) {
const type = data[prop]
const cb = Types[type]
if(!cb) {
throw new Error(`Type not found: "${type}"`)
}
return cb(cursor, data)
}
UnionDef.size = size
return UnionDef
}
function Collection(Type, cb, opts = {}) {
return function CollectionDef(data) {
if(!data) return null
const entries = cb ? cb(data) : data
if(!(entries && entries.length)) return null
const size = entries.length * (opts.size || Type.size)
const cursor = malloc(size, opts)
entries.forEach(entry => cursor.write(Type, entry, 0x00, {}, opts))
return cursor
}
}
function compile(Entry) {
malloc(Entry.size).write(Entry, obj)
unrollDeferred()
unrollStrings()
return Buffer.concat(heap)
}
compile.compile = compile
compile.types = {
Str,
UInt,
BigUInt,
Int,
BigInt,
Float,
Double,
Hex,
Tuple,
Defer,
DeferStr,
Ref,
DRef,
Null,
Copy,
Allocate,
Struct,
Union,
Collection,
}
return compile
}
compile.compile = compile
compile.compiler = opts => obj => compile(obj, opts)
module.exports = compile