-
Notifications
You must be signed in to change notification settings - Fork 2k
/
codegen.lua
639 lines (496 loc) · 11.7 KB
/
codegen.lua
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
-- setup environment for the codegen'd file to execute in
codeEnvironment = {
}
types = {}
natives = {}
rpcNatives = {}
local curType
local curNative
local codeEnvironment = codeEnvironment
local json = require('dkjson')
local cfx = require('cfx')
function codeEnvironment.type(typeName)
-- create a new type entry
types[typeName] = {
name = typeName
}
-- set a local
local typeEntry = types[typeName]
-- set a closure named after the type in the code environment
codeEnvironment[typeName] = function(argumentName)
local t = {
type = typeEntry,
name = argumentName
}
return setmetatable(t, {
__call = function(t, list)
t.annotations = json.decode(list)
return t
end
})
end
codeEnvironment[typeName .. 'Ptr'] = function(argumentName)
local t = codeEnvironment[typeName](argumentName)
t.pointer = true
return t
end
-- set the current type
curType = typeEntry
end
function codeEnvironment.extends(base)
curType.nativeType = types[base].nativeType
curType.parent = types[base]
end
function codeEnvironment.nativeType(typeName)
curType.nativeType = typeName
end
function codeEnvironment.subType(typeName)
curType.subType = typeName
end
function codeEnvironment.property(propertyName)
return function(data)
end
end
function codeEnvironment.method(methodName)
return function(data)
end
end
function codeEnvironment.native(nativeName)
-- create a new entry
local native = {
name = nativeName,
apiset = {},
hash = ("0x%x"):format(cfx.hash(nativeName:lower())),
arguments = {},
aliases = {}
}
table.insert(natives, native)
-- set the current native to said native
curNative = native
end
function codeEnvironment.doc(docString)
if not curNative then
return
end
curNative.doc = docString
end
function codeEnvironment.ns(nsString)
curNative.ns = nsString
end
function codeEnvironment.hash(hashString)
curNative.hash = hashString
end
function codeEnvironment.alias(name)
table.insert(curNative.aliases, name)
end
function codeEnvironment.jhash(hash)
curNative.jhash = hash
end
function codeEnvironment.arguments(list)
curNative.arguments = list
end
function codeEnvironment.returns(typeName)
if type(typeName) == 'table' then
curNative.returns = typeName
else
curNative.returns = types[typeName]
end
end
function codeEnvironment.game(gameName)
curNative.game = gameName
end
function codeEnvironment.annotations(list)
curNative.annotations = json.decode(list)
end
function codeEnvironment.apiset(setName)
table.insert(curNative.apiset, setName)
end
-- load the definition file
function loadDefinition(filename)
local chunk = loadfile(filename, 't', codeEnvironment)
chunk()
end
-- load an rpc definition file
local rpcEnvironment = {}
local function isType(type, baseName)
local valid = false
repeat
valid = valid or type.name == baseName
type = type.parent
until not type
return valid
end
local function isPrimitive(type)
return type.name == 'Any' or type.name == 'uint' or type.name == 'int' or
type.name == 'Hash' or type.name == 'charPtr' or type.name == 'float' or
type.name == 'vector3' or type.name == 'BOOL'
end
local function getNative(nativeName)
-- get the native
local n
for k, v in ipairs(natives) do
if v.name == nativeName and (#v.apiset == 0 or v.apiset[1] == 'client') then
n = v
break
end
end
return n
end
local function getServerNative(nativeName)
-- get the native
local n
for k, v in ipairs(natives) do
if v.name == nativeName and #v.apiset > 0 and v.apiset[1] == 'server' then
n = v
break
end
end
return n
end
local function removeServerNative(nativeName)
local n = getServerNative(nativeName)
if n then
for k, v in pairs(natives) do
if v == n then
table.remove(natives, k)
break
end
end
end
end
function rpcEnvironment.context_rpc(nativeName)
local n = getNative(nativeName)
if not n then
return
end
if #n.arguments == 0 then
error('Context natives are required to have arguments.')
end
local rn = {}
-- generate native arguments
local ctx
local args = {}
for k, v in ipairs(n.arguments) do
-- is this an entity?
if isType(v.type, 'Entity') then
-- if not the context, this is the context
if not ctx then
-- TODO: devise a way to disallow players for natives that need to
ctx = { idx = k - 1, type = 'Entity' }
end
table.insert(args, {
translate = true,
type = 'Entity'
})
elseif v.type.name == 'Player' then
-- if not the context, this is the context
if not ctx then
ctx = { idx = k - 1, type = 'Player' }
end
table.insert(args, {
translate = true,
type = 'Player'
})
elseif not isPrimitive(v.type) then
if not ctx then
if v.pointer then
ctx = { idx = k - 1, type = 'ObjDel' }
table.insert(args, {
translate = true,
type = 'ObjDel'
})
else
ctx = { idx = k - 1, type = 'ObjRef' }
table.insert(args, {
translate = true,
type = 'ObjRef'
})
end
else
error(('Type %s is not supported for RPC natives.'):format(v.type.name))
end
else
table.insert(args, {
type = v.type.name
})
end
end
if not ctx then
error('Context RPC natives are required to have a context.')
end
rn.ctx = ctx
rn.args = args
removeServerNative(nativeName)
codeEnvironment.native(nativeName)
codeEnvironment.arguments(n.arguments)
codeEnvironment.apiset('server')
codeEnvironment.returns('void')
curNative.rpc = true
curNative.ogHash = n.hash
if n.doc then
codeEnvironment.doc(n.doc)
end
rn.hash = n.hash
rn.name = nativeName
rn.type = 'ctx'
table.insert(rpcNatives, rn)
return function(tbl)
-- single getter
if type(tbl) == 'table' and #tbl == 1 then
tbl = { getter = tbl[1] }
end
-- if we need to define a getter
if tbl.getter then
local rvArg = n.arguments[2].type
local function isFloat(arg)
return arg.type.nativeType == 'float'
end
-- check if this is perhaps a vector3
if #n.arguments >= 4 and isFloat(n.arguments[2]) and isFloat(n.arguments[3]) and isFloat(n.arguments[4]) then
rvArg = types['Vector3']
end
rn.getter = {
returnType = rvArg.name,
returnArgStart = 1,
name = tbl.getter
}
local cxtRef
if ctx.type == 'Player' then
cxtRef = codeEnvironment.Player
elseif ctx.type == 'Entity' then
cxtRef = codeEnvironment.Entity
elseif ctx.type == 'ObjRef' then
cxtRef = codeEnvironment.int
end
if not getServerNative(tbl.getter) then
codeEnvironment.native(tbl.getter)
codeEnvironment.arguments {
cxtRef 'self'
}
codeEnvironment.apiset('server')
codeEnvironment.returns(rvArg)
end
end
end
end
function rpcEnvironment.entity_rpc(nativeName)
local n = getNative(nativeName)
if not n then
return
end
if not n.returns then
error('Entity natives are required to return an entity.')
end
if not isType(n.returns, 'Entity') then
error('Entity natives are required to return an entity.')
end
local rn = {}
-- generate native arguments
local args = {}
for k, v in ipairs(n.arguments) do
-- is this an entity?
if isType(v.type, 'Entity') then
table.insert(args, {
translate = true,
type = 'Entity'
})
elseif v.type.name == 'Player' then
table.insert(args, {
translate = true,
type = 'Player'
})
elseif not isPrimitive(v.type) then
error(('Type %s is not supported for RPC natives.'):format(v.type.name))
else
table.insert(args, {
type = v.type.name
})
end
end
rn.args = args
removeServerNative(nativeName)
codeEnvironment.native(nativeName)
codeEnvironment.arguments(n.arguments)
codeEnvironment.apiset('server')
codeEnvironment.returns('Entity')
curNative.rpc = true
curNative.ogHash = n.hash
if n.doc then
codeEnvironment.doc(n.doc)
end
rn.hash = n.hash
rn.name = nativeName
rn.type = 'entity'
table.insert(rpcNatives, rn)
end
function rpcEnvironment.object_rpc(nativeName)
local n = getNative(nativeName)
if not n then
return
end
if not n.returns then
error('Object natives are required to return an object.')
end
if isType(n.returns, 'Entity') then
error('Object natives are not entity natives.')
end
local rn = {}
-- generate native arguments
local args = {}
for k, v in ipairs(n.arguments) do
-- is this an entity?
if isType(v.type, 'Entity') then
table.insert(args, {
translate = true,
type = 'Entity'
})
elseif v.type.name == 'Player' then
table.insert(args, {
translate = true,
type = 'Player'
})
elseif not isPrimitive(v.type) then
error(('Type %s is not supported for RPC natives.'):format(v.type.name))
else
table.insert(args, {
type = v.type.name
})
end
end
rn.args = args
removeServerNative(nativeName)
codeEnvironment.native(nativeName)
codeEnvironment.arguments(n.arguments)
codeEnvironment.apiset('server')
codeEnvironment.returns(n.returns)
curNative.rpc = true
curNative.ogHash = n.hash
if n.doc then
codeEnvironment.doc(n.doc)
end
rn.hash = n.hash
rn.name = nativeName
rn.type = 'object'
table.insert(rpcNatives, rn)
end
function loadRpcDefinition(filename)
local chunk = loadfile(filename, 't', rpcEnvironment)
chunk()
end
function trim(s)
if not s then
return nil
end
return s:gsub("^%s*(.-)%s*$", "%1")
end
function parseDocString(native)
local docString = native.doc
if not docString then
return nil
end
local summary = trim(docString:match("<summary>(.+)</summary>"))
local params = docString:gmatch("<param name=\"([^\"]+)\">([^<]*)</param>")
local returns = docString:match("<returns>(.+)</returns>")
if not summary then
summary = ''
end
summary = trim(summary:gsub('^```(.+)```$', '%1'))
local paramsData = {}
local hasParams = false
for k, v in params do
table.insert(paramsData, { k, v })
hasParams = true
end
if summary == '' then
if not hasParams and trim(returns) == '' then
return nil
end
summary = native.name
end
return {
summary = summary,
params = paramsData,
hasParams = hasParams,
returns = returns
}
end
local gFilterRpc = false
local gApiSet = 'server'
local ourGame = 'gta5'
function matchApiSet(native)
local game = native.game
if ourGame and native.game and native.game ~= ourGame then
return false
end
if gFilterRpc and not native.rpc then
return false
end
local apisets = native.apiset
if #apisets == 0 then
apisets = { 'client' }
end
for _, v in ipairs(apisets) do
if v == gApiSet or v == 'shared' then
return true
end
end
return false
end
loadDefinition 'codegen_types.lua'
local outputType = 'lua'
local globalNatives = false
if #arg > 0 then
if arg[1]:match('gta_universal') then
arg[1] = 'inp/natives_global.lua'
end
if arg[1]:match('rdr3_universal') then
arg[1] = 'inp/natives_rdr3.lua'
end
if arg[1]:match('ny_universal') then
arg[1] = 'inp/natives_ny.lua'
end
if arg[1]:match('rdr3') then
ourGame = 'rdr3'
end
if arg[1]:match('_ny') then
ourGame = 'ny'
end
loadDefinition(arg[1])
if arg[1]:match('natives_global') or arg[1]:match('natives_rdr3') or arg[1]:match('natives_ny') then
globalNatives = true
end
gApiSet = 'client'
end
if #arg > 1 then
outputType = arg[2]
end
if #arg > 2 then
gApiSet = arg[3]
end
if #arg > 3 then
if arg[4] == 'rpc' then
gFilterRpc = true
end
end
-- server has Player be charPtr, not int
if gApiSet == 'server' then
types['Player'].nativeType = 'string'
end
if not gFilterRpc then
loadDefinition 'inp/natives_cfx.lua'
end
if not globalNatives then
loadDefinition 'codegen_dlc_natives.lua'
end
if arg[1]:match('ves_glo') then
loadRpcDefinition 'rpc_spec_natives.lua'
end
_natives = {}
for _, v in ipairs(natives) do
table.insert(_natives, v)
end
table.sort(_natives, function(a, b)
return a.name < b.name
end)
_G.gApiSet = gApiSet
dofile('codegen_out_' .. outputType .. '.lua')