"""Generate crates/xcl/src/hle/xgameruntime/gdk_table.rs from a table of the GDK runtime's
functions: every function with the class, interface and vtable slot its thunk uses
(QueryApiImpl protocol), as extracted from the public PC GDK's xgameruntime.lib. Input lines:
`NAME(function, "class guid", "interface guid", slot)`.
usage: python scripts/gen_gdk_table.py
"""
import io, os, re, sys
src = io.open(sys.argv[1], encoding='utf-8').read()
origin = re.search(r'Source\s*:\s*(\S+)', src)
rows = re.findall(r'\w+\((\w+), "([0-9A-Fa-f-]{36})", "([0-9A-Fa-f-]{36})", (\d+)\)', src)
assert rows, 'no rows'
hexg = lambda g: '0x' + g.replace('-', '').lower()
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'crates', 'xcl', 'src', 'hle', 'xgameruntime', 'gdk_table.rs')
with io.open(OUT, 'w', encoding='utf-8', newline='\n') as f:
f.write('//! Generated by scripts/gen_gdk_table.py – do not edit.\n')
f.write(f'//! The GDK runtime\'s functions as the public PC GDK\'s xgameruntime.lib ({origin.group(1) if origin else "?"}) calls them:\n')
f.write('//! the (class, interface) its thunk asks QueryApiImpl for and the vtable slot it calls.\n\n')
f.write('/// (function, class id, interface id, slot)\n')
f.write('pub static API: &[(&str, u128, u128, u8)] = &[\n')
for name, c, i, slot in sorted(rows, key=lambda r: (r[1], r[2], int(r[3]))):
f.write(f' ("{name}", {hexg(c)}, {hexg(i)}, {slot}),\n')
f.write('];\n')
print(len(rows), 'rows ->', os.path.normpath(OUT))