-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathas_plugin_codegen.py
More file actions
751 lines (667 loc) · 18.7 KB
/
as_plugin_codegen.py
File metadata and controls
751 lines (667 loc) · 18.7 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
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
import os, re
from pathlib import Path
docs_path = 'docs'
asgen_path = 'scripts/generated_props'
headers_path = 'include/sven'
as_plugin_output_path = '../../../../../svencoop/scripts/plugins/store/ApiGenerator'
classes_to_generate = [
'CBaseEntity',
'CBaseDelay',
'CBaseAnimating',
'CBaseToggle',
'CBasePlayerItem',
'CBasePlayerWeapon',
'CBasePlayerAmmo',
'CBaseMonster',
'CBasePlayer',
'CBaseTank',
'CBaseButton',
'CBaseDoor',
'CItemInventory',
'CItem',
'CGrenade',
'CCineMonster',
'CSprite',
'CPathTrack',
'CBeam',
'CLaser',
'CBaseTank'
]
# 2nd value is prefix for each enum value name
enums_to_generate = [
["Activity", ""],
["EdictFlags", ""],
["EFFECTS", ""],
["FixAngleMode", ""],
["CLASS", ""],
["DMG", ""],
["GIB", ""],
["USE_TYPE", ""],
["Bullet", ""],
["AddPlayerItemResult", ""],
["HITGROUP", ""],
["PFLAG", ""],
["ObserverMode", ""],
["Train", ""],
["PLAYER_ANIM", ""],
["PlayerViewMode", ""],
["ButtonCode", ""],
["WeaponIds", ""],
["BeamType", ""],
["BeamFlags", ""],
["CBeamSpawnflags", ""],
["TANKBULLET", ""],
["SoundFlag", ""],
["SOUND_CHANNEL", ""],
["AdminLevel_t", ""],
["FFADE", ""],
["HUD_EFFECT", ""],
["HUD_ELEM", ""],
["HUD_SPR", ""],
["HUD_NUM", ""],
["HUD_TIME", ""],
["NetworkMessageType", "MSG_"],
["FCAP", ""]
]
prop_code = {
'bool': {
'astype': 'FIELD_BOOL',
'setter': '<FIELD> = value.v8 != 0;',
'getter': 'return uint8(<FIELD> ? 1 : 0);'
},
'int8': {
'astype': 'FIELD_BYTE',
'setter': '<FIELD> = value.v8;',
'getter': 'return <FIELD>;'
},
'int': {
'astype': 'FIELD_INT',
'setter': '<FIELD> = value.v32;',
'getter': 'return <FIELD>;'
},
'uint': {
'astype': 'FIELD_UINT',
'setter': '<FIELD> = value.v32;',
'getter': 'return <FIELD>;'
},
'TOGGLE_STATE': {
'astype': 'FIELD_INT',
'setter': '<FIELD> = TOGGLE_STATE(value.v32);',
'getter': 'return int(<FIELD>);'
},
'Activity': {
'astype': 'FIELD_INT',
'setter': '<FIELD> = Activity(value.v32);',
'getter': 'return int(<FIELD>);'
},
'MONSTERSTATE': {
'astype': 'FIELD_INT',
'setter': '<FIELD> = MONSTERSTATE(value.v32);',
'getter': 'return int(<FIELD>);'
},
'SCRIPTSTATE': {
'astype': 'FIELD_INT',
'setter': '<FIELD> = SCRIPTSTATE(value.v32);',
'getter': 'return int(<FIELD>);'
},
'TANKBULLET': {
'astype': 'FIELD_INT',
'setter': '<FIELD> = TANKBULLET(value.v32);',
'getter': 'return int(<FIELD>);'
},
'float': {
'astype': 'FIELD_FLT',
'setter': '<FIELD> = value.vf;',
'getter': 'return <FIELD>;'
},
'Vector': {
'astype': 'FIELD_VEC3',
'setter': '<FIELD> = value.vec3;',
'getter': 'return <FIELD>;'
},
'string_t': {
'astype': 'FIELD_STR_T',
'setter': '<FIELD> = value.str_t;',
'getter': 'return <FIELD>;'
},
'string': {
'astype': 'FIELD_STR',
'setter': '/* setting string values crashes the game??? */', # '<FIELD> = value.str;',
'getter': 'return <FIELD>;'
},
'entvars_t@': {
'astype': 'FIELD_ENTVARS',
'setter': '@<FIELD> = @value.pev;',
'getter': 'return @<FIELD>;'
},
'EHandle': {
'astype': 'FIELD_EHANDLE',
'setter': '<FIELD> = value.ehandle;',
'getter': 'return <FIELD>;'
},
'Schedule@': {
'astype': 'FIELD_SCHED',
'setter': '@<FIELD> = @value.sched;',
'getter': 'return @<FIELD>;'
},
'const int': {
'astype': 'READ_ONLY',
'setter': '<FIELD> = 0;',
'getter': 'return <FIELD>;'
}
}
func_vals = {
'int&': {
'code': 'int i;', # do we need to set up any local variables?
'argval': 'i', # what value should angelscript use when calling the function?
"ctype": "int&", # what type should C code use?
"size": 4 # how many bytes does this argument use in C++?
},
'bool': {
'argval': 'false',
"ctype": "bool",
"size": 1
},
'int8': {
'argval': '0',
"ctype": "char",
"size": 1
},
'int': {
'argval': '0',
"ctype": "int",
"size": 4
},
'int16': {
'argval': '0',
"ctype": "short",
"size": 2
},
'uint': {
'argval': '0',
"ctype": "unsigned int",
"size": 4
},
'TOGGLE_STATE': {
'argval': '0',
"ctype": "int",
"size": 4
},
'Activity': {
'argval': 'Activity(0)',
"ctype": "int",
"size": 4
},
'MONSTERSTATE': {
'argval': 'MONSTERSTATE(0)',
"ctype": "int",
"size": 4
},
'SCRIPTSTATE': {
'argval': 'SCRIPTSTATE(0)',
"ctype": "int",
"size": 4
},
'TANKBULLET': {
'argval': 'TANKBULLET(0)',
"ctype": "int",
"size": 4
},
'USE_TYPE': {
'argval': 'USE_TYPE(0)',
"ctype": "int",
"size": 4
},
'Bullet': {
'argval': 'Bullet(0)',
"ctype": "int",
"size": 4
},
'FireBulletsDrawMode': {
'argval': 'FireBulletsDrawMode(0)',
"ctype": "int",
"size": 4
},
'PLAYER_ANIM': {
'argval': 'PLAYER_ANIM(0)',
"ctype": "int",
"size": 4
},
'PlayerViewMode': {
'argval': 'PlayerViewMode(0)',
"ctype": "int",
"size": 4
},
'BeamType': {
'argval': 'BeamType(0)',
"ctype": "int",
"size": 4
},
'CLASS': {
'argval': 'CLASS(0)',
"ctype": "int",
"size": 4
},
'float': {
'argval': '0',
"ctype": "float",
"size": 4
},
'Vector': {
'argval': 'Vector(0,0,0)',
"ctype": "Vector",
"size": 12
},
'string': {
'argval': '""',
"ctype": "const char*",
"size": 4
},
'string_t': {
'argval': 'string_t(0)',
"ctype": "string_t",
"size": 4
},
'EHandle': {
'argval': 'null',
"ctype": "EHandle",
"size": 4
},
'const int': {
'argval': '0',
"ctype": "int",
"size": 4
},
'Vector&': {
'code': 'Vector vec;',
'argval': 'vec',
"ctype": "Vector",
"size": 12
},
'string&': {
'code': 'string str;',
'argval': 'str',
"ctype": "const char*",
"size": 4
},
'TraceResult&': {
'code': 'TraceResult tr;',
'argval': 'tr',
"ctype": "TraceResult*",
"size": 4
},
'float&': {
'code': 'float f;',
'argval': 'f',
"ctype": "float&",
"size": 4
},
'size_t': {
'argval': '0',
"ctype": "size_t",
"size": 4
},
'ItemInfo&': {
'code': 'ItemInfo iteminfo;',
'argval': 'iteminfo',
"ctype": "ItemInfo&",
"size": 4
},
'AddPlayerItemResult': {
'argval': '0',
"ctype": "int",
"size": 4
},
'EHandle&': {
'argval': 'EHandle(g_EntityFuncs.Instance(0))',
"ctype": "EHandle&",
"size": 4
},
'int8&': {
'code': 'int8 i;',
'argval': 'i',
"ctype": "int8&",
"size": 4
},
'Waypoint&': {
'code': 'Waypoint w;',
'argval': 'w',
"ctype": "Waypoint&",
"size": 4
}
}
# value names that are in the HLSDK and will conflict with the sven names
ignore_enum_values = [
"FL_FLY",
"FL_SWIM",
"FL_CONVEYOR",
"FL_CLIENT",
"FL_INWATER",
"FL_MONSTER",
"FL_GODMODE",
"FL_NOTARGET",
"FL_SKIPLOCALHOST",
"FL_ONGROUND",
"FL_PARTIALGROUND",
"FL_WATERJUMP",
"FL_FROZEN",
"FL_FAKECLIENT",
"FL_DUCKING",
"FL_FLOAT",
"FL_GRAPHED",
"FL_IMMUNE_WATER",
"FL_IMMUNE_SLIME",
"FL_IMMUNE_LAVA",
"FL_PROXY",
"FL_ALWAYSTHINK",
"FL_BASEVELOCITY",
"FL_MONSTERCLIP",
"FL_ONTRAIN",
"FL_WORLDBRUSH",
"FL_SPECTATOR",
"FL_CUSTOMENTITY",
"FL_KILLME",
"FL_DORMANT",
"EF_BRIGHTFIELD",
"EF_MUZZLEFLASH",
"EF_BRIGHTLIGHT",
"EF_DIMLIGHT",
"EF_INVLIGHT",
"EF_NOINTERP",
"EF_LIGHT",
"EF_NODRAW",
"MAX_WEAPONS",
"SND_STOP",
"SND_CHANGE_VOL",
"SND_CHANGE_PITCH",
"CHAN_AUTO",
"CHAN_WEAPON",
"CHAN_VOICE",
"CHAN_ITEM",
"CHAN_BODY",
"CHAN_STREAM",
"CHAN_STATIC",
"SVC_TEMPENTITY",
"SVC_INTERMISSION"
]
ignore_func_names = [
# cast functions (why are these listed?)
'opImplCast',
'opCast',
# these aren't exposed to angelscript but are still in the documentation
'GetUserData',
'ClearUserData',
# we know these aren't virtual (see the hlsdk)
'edict',
'entindex',
'Intersects',
'MakeDormant',
'IsDormant',
'GetOrigin',
'GetClassname',
'GetTargetname',
# useless functions. Don't waste time on them
'opEquals',
'SUB_DoNothing',
'SUB_Remove',
'KeyValue', # there's a dll func in metamod for this
# these share offsets with other functions. Enable these again if the generator is able to resolve that,
# or not because these don't seem very useful
'SetOrigin',
'SUB_StartFadeOut',
'SetObjectCollisionBox',
'IRelationshipByClass',
'SUB_CallUseToggle'
]
if not os.path.exists(docs_path):
if os.path.exists('asdocs.txt') and (os.path.exists('ASDocGenerator.exe') or os.path.exists('ASDocGenerator')):
print("Angelscript docs are missing. Generating them from the asdocs.txt file")
os.system('ASDocGenerator -i asdocs.txt -o docs')
else:
print("Angelscript docs are missing, and so are the files required to generate them.")
print("- Download or compile ASDocGenerator and place it in this folder")
print("- Run the game with the -as_outputdocs launch option to generate asdocs.txt, then place in this folder")
print("- Run this script again")
Path(asgen_path).mkdir(parents=True, exist_ok=True)
Path(as_plugin_output_path).mkdir(parents=True, exist_ok=True)
print("Generating angelscript plugin code for...")
include_code = open(os.path.join(asgen_path, 'includes.as'), 'w')
func_name_counts = {}
virtual_func_names = set({})
# on the first pass, just look for functions that appear in multiple classes.
# these will likely be virtual functions and so can be found by the vtable replacement method
# anything else is probably a normal method and not easy to find the address for
for class_to_gen in classes_to_generate:
with open(os.path.join(docs_path, class_to_gen + '.htm')) as htm:
is_parsing_funcs = False
next_td_is_decl = False
for line in htm.readlines():
if '<h2>Methods</h2>' in line:
is_parsing_funcs = True
if '<h2>Properties</h2>' in line:
break
if is_parsing_funcs:
if '<tr>' in line:
next_td_is_decl = True
if '<td>' in line:
if next_td_is_decl:
next_td_is_decl = False
func = line[line.find("<td>")+len("<td>"):line.find("</td>")]
first_space = func.find(" ")
ret_type = func[:first_space]
func = func[first_space+1:]
if ret_type == "const":
first_space = func.find(" ")
ret_type = func[:first_space]
func = func[first_space+1:]
args_begin = func.find("(")
func_name = func[:args_begin]
if func_name in ignore_func_names:
continue
if func_name not in func_name_counts:
func_name_counts[func_name] = 1
else:
func_name_counts[func_name] += 1
for key, value in func_name_counts.items():
if value > 1:
virtual_func_names.add(key)
# Now generate property and function information for angelscript
for class_to_gen in classes_to_generate:
print(class_to_gen)
asClass = class_to_gen + "Pv"
include_code.write("#include \"" + asClass + "\"\n")
with open(os.path.join(docs_path, class_to_gen + '.htm')) as htm:
with open(os.path.join(asgen_path, asClass + '.as'), 'w') as code:
is_parsing_funcs = False
is_parsing_props = False
next_td_is_decl = False
# not using a DOM parsing library because the doc format is simple and static
props = []
funcs = []
for line in htm.readlines():
if '<h2>Methods</h2>' in line:
is_parsing_funcs = True
is_parsing_props = False
if '<h2>Properties</h2>' in line:
is_parsing_props = True
is_parsing_funcs = False
if is_parsing_props:
if '<tr>' in line:
next_td_is_decl = True
if '<td>' in line:
if next_td_is_decl:
next_td_is_decl = False
prop = line[line.find("<td>")+len("<td>"):line.find("</td>")].split()
prop_type = " ".join(prop[0:-1])
prop_name = prop[-1]
props.append([prop_type, prop_name, ""])
elif len(props) > 0:
desc = line[line.find("<td>")+len("<td>"):line.find("</td>")].replace('"', '\\"')
props[-1][2] = desc
if is_parsing_funcs:
if '<tr>' in line:
next_td_is_decl = True
if '<td>' in line:
if next_td_is_decl:
next_td_is_decl = False
func = line[line.find("<td>")+len("<td>"):line.find("</td>")]
first_space = func.find(" ")
ret_type = func[:first_space]
func = func[first_space+1:]
if ret_type == "const":
first_space = func.find(" ")
ret_type = func[:first_space]
func = func[first_space+1:]
args_begin = func.find("(")
func_name = func[:args_begin]
args = func[args_begin+1:func.find(")")].split(",")
if func_name not in virtual_func_names:
continue
if class_to_gen != 'CBaseEntity':
continue # work needed to find vtables in derived classes
for idx, arg in enumerate(args):
if len(arg) == 0:
continue
# strip qualifiers that don't matter for testing
arg = arg.replace("const", "")
arg = arg.replace(" in ", "")
arg = arg.replace(" out ", "")
arg = arg.replace(" inout ", "")
# convert html entities
arg = arg.replace("<", "<")
arg = arg.replace(">", ">")
# strip default values
if '=' in arg:
arg = arg[:arg.find("=")]
arg = arg.strip()
endType = arg.find(" ")
if endType == -1:
endType = arg.find("&")
if endType == -1:
endType = arg.find("@")
if endType == -1:
print("Failed to find separator for argument type and name: %s" % args[idx])
continue
argType = arg[:endType+1].strip()
argName = arg[endType+1:]
args[idx] = (argType, argName)
if len(args) == 1 and args[0] == '':
args = []
#print("Got func: %s %s(%s)" % (ret_type, func_name, args))
func_data = [func_name, ret_type, "", args]
# if multiple funcs have the same name, keep the function that has the most number of arguments
skip_func = False
for idx, f in enumerate(funcs):
if f[0] == func_name:
if len(f[3]) >= len(args):
skip_func = True
else:
funcs[idx] = func_data
if not skip_func:
funcs.append(func_data)
elif len(funcs) > 0:
desc = line[line.find("<td>")+len("<td>"):line.find("</td>")].replace('"', '\\"')
funcs[-1][2] = desc
code.write("// This code is automatically generated.\n")
code.write("// Update the python script instead of editing this directly.\n\n")
code.write("array<PvProp> " + asClass + " = {\n")
for idx, prop in enumerate(props):
prop_type = prop[0]
prop_name = prop[1]
prop_desc = prop[2]
ent_replace = "cast<" + class_to_gen + "@>(ent)"
if prop_type not in prop_code:
print("ERROR: New prop type encountered (%s %s). Update the prop_code in this script to use this property." % (prop_type, prop_name))
continue
astype = prop_code[prop_type]['astype']
getter = prop_code[prop_type]['getter'].replace("<FIELD>", ent_replace + "." + prop_name)
setter = prop_code[prop_type]['setter'].replace("<FIELD>", ent_replace + "." + prop_name)
if astype == 'READ_ONLY':
code.write("\t// " + prop_type + " " + prop_name + "\n")
continue
code.write('\tPvProp("' + prop_name + '", ' + astype + ', "' + prop_desc + '",\n')
code.write("\t\tfunction(ent) { " + getter + " },\n")
code.write("\t\tfunction(ent, value) { " + setter + " }\n")
if (idx < len(props)-1):
code.write("\t),\n")
else:
code.write("\t)\n")
code.write("};\n")
code.write("\narray<PvFunc> " + class_to_gen + "Funcs = {\n")
for idx, func in enumerate(funcs):
func_name = func[0]
ret_type = func[1]
desc = func[2]
args = func[3]
ctype = 'unknown_type'
if ret_type[-1] == "@":
ctype = ret_type[:-1] + "*"
elif ret_type == "void":
ctype = "void"
elif ret_type in func_vals:
ctype = func_vals[ret_type]['ctype']
else:
print("Unknown func return type '%s'" % ret_type)
code.write('\tPvFunc("' + func_name + '", "' + ret_type + '", "' + ctype + '", "' + desc + '",\n')
cast_code = "cast<" + class_to_gen + "@>(ent)"
if class_to_gen == "CBaseEntity":
cast_code = "ent"
func_call = cast_code + "." + func_name + "("
local_vars_code = ""
unique_locals = set({})
code.write("\t\t{")
for idx2, arg in enumerate(args):
argtype = arg[0]
argval = "0"
argsz = 4
ctype = "unknown_type"
if arg[0][-1] == "@":
argval = "null"
argsz = 4
ctype = arg[0][:-1] + "*"
elif arg[0] in func_vals:
argval = func_vals[argtype]['argval']
argsz = func_vals[argtype]['size']
ctype = func_vals[argtype]['ctype']
else:
print("Unknown func arg type '%s'" % arg[0])
if argtype in func_vals and 'code' in func_vals[argtype] and argtype not in unique_locals:
local_vars_code += func_vals[argtype]['code'] + " "
unique_locals.add(argtype)
func_call += argval
code.write('PvFuncArg("' + arg[0] + '", "' + ctype + '", "' + arg[1] + '", ' + ('%d' % argsz) + ')')
if idx2 < len(args)-1:
code.write(', ')
func_call += ', '
code.write("},\n")
func_call += ");"
code.write("\t\tfunction(ent) { " + local_vars_code + func_call + " }\n")
if (idx < len(funcs)-1):
code.write("\t),\n")
else:
code.write("\t)\n")
code.write("};\n")
print('\nGenerating enums for...')
enum_code = ''
for enum_to_gen in enums_to_generate:
print(enum_to_gen[0])
prefix = enum_to_gen[1]
with open(os.path.join(docs_path, enum_to_gen[0] + '.htm')) as file:
html = file.read()
enum_name = re.search(r'<h1>(.*?)</h1>', html).group(1)
values = re.findall(r'<td>(.*?)</td>\s*<td>(.*?)</td>\s*<td>(.*?)</td>', html)
enum_str = "\n// " + enum_name + "\n"
for i, value in enumerate(values):
if value[0] in ignore_enum_values:
enum_str += "// "
enum_str += "#define " + prefix + value[0] + " " + value[1]
comment = value[2].strip()
if comment:
enum_str += " // " + comment
enum_str += "\n"
enum_code += enum_str
with open(os.path.join(headers_path, 'sc_enums.h'), 'w') as file:
file.write("// This code was automatically generated by the ApiGenerator plugin\n")
file.write("// Value names that conflict with the HLSDK are commented out\n")
file.write(enum_code)
print("\nDone!")