#!/usr/bin/env python3 # Writes the core's Lua API reference next to this script: one api-.md per function table the # core registers with Lua (the luaL_Reg tables in core/lib*.cpp, named by the lua_setglobal that # follows each luaL_newlib) and api-globals.md for the globals interpreter.cpp sets. Ported from # yugioh-legacy-server's duel-engine/ocgcore/lua-reference.sh (bash + awk) so it runs on the Windows # build machine too. The committed copy is the contract; regenerate after any core change that touches # a lib*.cpp table and commit the result with it. # python contract/lua-reference/generate.py [] [] import re, sys from pathlib import Path here = Path(__file__).resolve().parent core = Path(sys.argv[1]) if len(sys.argv) > 1 else here.parent.parent / "core" out = Path(sys.argv[2]) if len(sys.argv) > 2 else here RE_FN = re.compile(r"^int32_t scriptlib::([a-z_0-9]+)\(") RE_ARGC = re.compile(r"check_param_count\(L, *([0-9]+)\)") RE_TABLE = re.compile(r"static const struct luaL_Reg ([a-z]+)\[\] *= *\{") RE_QUOTED = re.compile(r'"([^"]*)"') RE_NEWLIB = re.compile(r"luaL_newlib\(L, *([a-z]+)\)") RE_SETGLOBAL = re.compile(r'lua_setglobal\(L, *"([^"]*)"') def fail(msg): print("lua-reference: " + msg, file=sys.stderr) sys.exit(2) rows = set() # (class, name, cpp function, least arguments or "") for path in sorted(core.glob("lib*.cpp")): argc, entries, names, cls = {}, {}, {}, {} table = lib = fn = "" for raw in path.read_text(encoding="utf-8", errors="replace").splitlines(): line = raw.rstrip("\r") m = RE_FN.match(line) if m: fn = m.group(1) m = RE_ARGC.search(line) if fn and m and fn not in argc: argc[fn] = m.group(1) m = RE_TABLE.search(line) if m: table = m.group(1) continue if table: if re.match(r"^[ \t]*(//.*)?$", line): continue if re.match(r"^[ \t]*#", line): fail("a preprocessor line inside " + table + ": " + line) if re.match(r"^[ \t]*\};", line): table = "" continue if "nullptr" in line: continue q = RE_QUOTED.search(line) if not q: fail("unreadable entry in " + table + ": " + line) f = re.sub(r"[^a-z_0-9].*", "", re.sub(r".*scriptlib::", "", line)) entries[(table, q.group(1))] = f names.setdefault(table, []).append(q.group(1)) continue m = RE_NEWLIB.search(line) if m: lib = m.group(1) m = RE_SETGLOBAL.search(line) if lib and m: cls[lib] = m.group(1) lib = "" for t, lst in names.items(): if t not in cls: fail("no global for table " + t + " in " + path.name) for name in lst: f = entries[(t, name)] rows.add((cls[t], name, f, argc.get(f, ""))) if not rows: fail("no function tables found under " + str(core)) for old in out.glob("api-*.md"): old.unlink() def header(title, note): return ("# `" + title + "`\n\n> Generated from `core/` by `contract/lua-reference/generate.py`; " "never edited by hand. " + note + "\n\n") classes = sorted({r[0] for r in rows}) for c in classes: lines = [header(c, "Every function the core registers on `" + c + "` is listed, and nothing else: a name " "that isn't here isn't in the core (the card scripts' `library/` defines the rest). A method is " "called on an object too (`c:IsCode(id)` is `Card.IsCode(c, id)`). *Arguments* is the fewest " "the core accepts, counting the object."), "| Function | Arguments |\n|---|---|\n"] for r in sorted(rows): if r[0] == c: lines.append("| `" + r[0] + "." + r[1] + "` | " + (r[3] or "any") + " |\n") (out / ("api-" + c.lower() + ".md")).write_text("".join(lines), encoding="utf-8", newline="\n") globals_ = set() for raw in (core / "interpreter.cpp").read_text(encoding="utf-8", errors="replace").splitlines(): if re.match(r"^\s*//", raw): continue m = re.search(r'lua_setglobal\(lua_state, *"([A-Za-z_0-9]*)"\)', raw) if m: globals_.add(m.group(1)) lines = [header("Globals", "The values the core itself sets as Lua globals, beside the card scripts' `constant.lua`."), "| Global |\n|---|\n"] lines += ["| `" + g + "` |\n" for g in sorted(globals_)] (out / "api-globals.md").write_text("".join(lines), encoding="utf-8", newline="\n") print("lua-reference: %d functions in %d tables, %d globals" % (len(rows), len(classes), len(globals_)), file=sys.stderr)