Files
AVA2/tools/generate_gameplay_modules.py

91 lines
4.0 KiB
Python

#!/usr/bin/env python3
import re
from pathlib import Path
root = Path(__file__).resolve().parents[1]
upstream_root = root / "upstream/ava2-client"
modules_spec = [
("item", "initItem", "UserInterface/PythonItemModule.cpp"),
("player", "initPlayer", "UserInterface/PythonPlayerModule.cpp"),
("chrmgr", "initchrmgr", "UserInterface/PythonCharacterManagerModule.cpp"),
("chr", "initchr", "UserInterface/PythonCharacterModule.cpp"),
("chat", "initChat", "UserInterface/PythonChatModule.cpp"),
("snd", "initsnd", "UserInterface/PythonSoundManagerModule.cpp"),
("background", "initBackground", "UserInterface/PythonBackgroundModule.cpp"),
]
h_output = ["#pragma once\n", "namespace ava2 {\n"]
cpp_output = [
'#include "python_gameplay_modules.h"\n',
'#include "../upstream/ava2-client/UserInterface/Locale_inc.h"\n',
'#include "../upstream/ava2-client/GameLib/ItemData.h"\n',
'#include <Python.h>\n',
'#include <string>\n',
'#include <vector>\n\n',
'namespace ava2 {\n',
'namespace {\n',
'PyObject* dummy_none(PyObject*, PyObject*) { Py_RETURN_NONE; }\n',
'PyObject* dummy_zero(PyObject*, PyObject*) { return PyInt_FromLong(0); }\n',
'PyObject* dummy_one(PyObject*, PyObject*) { return PyInt_FromLong(1); }\n',
'PyObject* dummy_str(PyObject*, PyObject*) { return PyString_FromString(""); }\n',
'PyObject* dummy_tuple2(PyObject*, PyObject*) { return Py_BuildValue("ii", 0, 0); }\n',
'PyObject* dummy_tuple3(PyObject*, PyObject*) { return Py_BuildValue("iii", 0, 0, 0); }\n',
'PyObject* dummy_tuple4(PyObject*, PyObject*) { return Py_BuildValue("iiii", 0, 0, 0, 0); }\n',
'} // namespace\n\n'
]
for mod_name, init_fn, rel_path in modules_spec:
h_output.append(f"void {init_fn}();\n")
src_file = upstream_root / rel_path
with open(src_file, "r", encoding="latin1") as f:
content = f.read()
methods = re.findall(r'\{\s*"(\w+)"\s*,\s*(\w+)\s*,', content)
method_names = [m[0] for m in methods]
init_idx = content.find(f"void {init_fn}()")
body = content[init_idx:] if init_idx != -1 else ""
consts = re.findall(r'PyModule_AddIntConstant\s*\(\s*\w+,\s*\"(\w+)\"\s*,\s*([^)]+)\)', body)
cpp_output.append(f"void {init_fn}() {{\n")
cpp_output.append(f" static PyMethodDef methods[] = {{\n")
for m in method_names:
if m.startswith("Is") or m.startswith("Get"):
if any(k in m for k in ["Name", "String", "Text", "Title", "Path"]):
cpp_output.append(f' {{"{m}", dummy_str, METH_VARARGS, nullptr}},\n')
elif any(k in m for k in ["Position", "PixelPosition", "Coord"]):
cpp_output.append(f' {{"{m}", dummy_tuple3, METH_VARARGS, nullptr}},\n')
else:
cpp_output.append(f' {{"{m}", dummy_zero, METH_VARARGS, nullptr}},\n')
else:
cpp_output.append(f' {{"{m}", dummy_none, METH_VARARGS, nullptr}},\n')
cpp_output.append(f" {{nullptr, nullptr, 0, nullptr}}\n }};\n")
cpp_output.append(f' auto* module = Py_InitModule("{mod_name}", methods);\n\n')
for cname, cval in consts:
val_str = cval.strip()
# Keep CItemData:: constants directly since we included ItemData.h!
# If it's a known CItemData enum:
if "CItemData::" in val_str:
final_val = val_str
elif val_str.lstrip("-").isdigit() or val_str.startswith("0x"):
final_val = val_str
else:
# Check if it has a prefix or can be passed directly or default
final_val = val_str
cpp_output.append(f' PyModule_AddIntConstant(module, "{cname}", {final_val});\n')
cpp_output.append("}\n\n")
h_output.append("} // namespace ava2\n")
cpp_output.append("} // namespace ava2\n")
(root / "src/python_gameplay_modules.h").write_text("".join(h_output))
(root / "src/python_gameplay_modules.cpp").write_text("".join(cpp_output))
print("Regenerated python_gameplay_modules.cpp with CItemData enums included directly!")