#!/usr/bin/env python3 import re from pathlib import Path root = Path(__file__).resolve().parents[1] wnd_cpp = root / "upstream/ava2-client/EterPythonLib/PythonWindowManagerModule.cpp" with open(wnd_cpp, "r", encoding="latin1") as f: text = f.read() methods = re.findall(r'\{\s*"(\w+)"\s*,\s*(\w+)\s*,', text) method_names = [m[0] for m in methods] h_code = """#pragma once namespace ava2 { void initwndMgr(); } """ (root / "src/python_wndmgr.h").write_text(h_code) cpp_code = """#include "python_wndmgr.h" #include #include #include #include namespace ava2 { namespace { int s_screen_width = 1280; int s_screen_height = 720; long s_next_handle = 1; PyObject* s_mouse_handler = nullptr; struct WindowRecord { long handle = 0; PyObject* py_obj = nullptr; std::string name; int x = 0, y = 0, width = 0, height = 0; bool is_show = false; }; std::unordered_map s_windows; PyObject* wnd_set_screen_size(PyObject*, PyObject* args) { if (!PyArg_ParseTuple(args, "ii", &s_screen_width, &s_screen_height)) return nullptr; Py_RETURN_NONE; } PyObject* wnd_get_screen_width(PyObject*, PyObject*) { return PyInt_FromLong(s_screen_width); } PyObject* wnd_get_screen_height(PyObject*, PyObject*) { return PyInt_FromLong(s_screen_height); } PyObject* wnd_set_mouse_handler(PyObject*, PyObject* args) { PyObject* h = nullptr; if (!PyArg_ParseTuple(args, "O", &h)) return nullptr; Py_XDECREF(s_mouse_handler); s_mouse_handler = h; Py_XINCREF(s_mouse_handler); Py_RETURN_NONE; } PyObject* wnd_register(PyObject*, PyObject* args) { PyObject* po = nullptr; char* layer = nullptr; if (!PyArg_ParseTuple(args, "O|s", &po, &layer)) return nullptr; long handle = s_next_handle++; WindowRecord rec; rec.handle = handle; rec.py_obj = po; Py_XINCREF(po); s_windows[handle] = rec; return PyInt_FromLong(handle); } PyObject* wnd_destroy(PyObject*, PyObject* args) { long handle = 0; if (PyArg_ParseTuple(args, "l", &handle)) { auto it = s_windows.find(handle); if (it != s_windows.end()) { Py_XDECREF(it->second.py_obj); s_windows.erase(it); } } Py_RETURN_NONE; } PyObject* wnd_set_size(PyObject*, PyObject* args) { long handle = 0; int w = 0, h = 0; if (PyArg_ParseTuple(args, "lii", &handle, &w, &h)) { if (s_windows.count(handle)) { s_windows[handle].width = w; s_windows[handle].height = h; } } Py_RETURN_NONE; } PyObject* wnd_set_position(PyObject*, PyObject* args) { long handle = 0; int x = 0, y = 0; if (PyArg_ParseTuple(args, "lii", &handle, &x, &y)) { if (s_windows.count(handle)) { s_windows[handle].x = x; s_windows[handle].y = y; } } Py_RETURN_NONE; } PyObject* wnd_get_position(PyObject*, PyObject* args) { long handle = 0; if (PyArg_ParseTuple(args, "l", &handle)) { if (s_windows.count(handle)) { return Py_BuildValue("ii", s_windows[handle].x, s_windows[handle].y); } } return Py_BuildValue("ii", 0, 0); } PyObject* wnd_get_width(PyObject*, PyObject* args) { long handle = 0; if (PyArg_ParseTuple(args, "l", &handle) && s_windows.count(handle)) { return PyInt_FromLong(s_windows[handle].width); } return PyInt_FromLong(0); } PyObject* wnd_get_height(PyObject*, PyObject* args) { long handle = 0; if (PyArg_ParseTuple(args, "l", &handle) && s_windows.count(handle)) { return PyInt_FromLong(s_windows[handle].height); } return PyInt_FromLong(0); } PyObject* wnd_show(PyObject*, PyObject* args) { long handle = 0; if (PyArg_ParseTuple(args, "l", &handle) && s_windows.count(handle)) { s_windows[handle].is_show = true; } Py_RETURN_NONE; } PyObject* wnd_hide(PyObject*, PyObject* args) { long handle = 0; if (PyArg_ParseTuple(args, "l", &handle) && s_windows.count(handle)) { s_windows[handle].is_show = false; } Py_RETURN_NONE; } PyObject* wnd_is_show(PyObject*, PyObject* args) { long handle = 0; if (PyArg_ParseTuple(args, "l", &handle) && s_windows.count(handle)) { return PyInt_FromLong(s_windows[handle].is_show ? 1 : 0); } return PyInt_FromLong(0); } PyObject* wnd_dummy_none(PyObject*, PyObject*) { Py_RETURN_NONE; } PyObject* wnd_dummy_zero(PyObject*, PyObject*) { return PyInt_FromLong(0); } PyObject* wnd_dummy_one(PyObject*, PyObject*) { return PyInt_FromLong(1); } PyObject* wnd_dummy_register(PyObject* self, PyObject* args) { return wnd_register(self, args); } } // namespace void initwndMgr() { static PyMethodDef methods[] = { {"SetScreenSize", wnd_set_screen_size, METH_VARARGS, nullptr}, {"GetScreenWidth", wnd_get_screen_width, METH_VARARGS, nullptr}, {"GetScreenHeight", wnd_get_screen_height, METH_VARARGS, nullptr}, {"SetMouseHandler", wnd_set_mouse_handler, METH_VARARGS, nullptr}, {"Register", wnd_register, METH_VARARGS, nullptr}, {"Destroy", wnd_destroy, METH_VARARGS, nullptr}, {"SetWindowSize", wnd_set_size, METH_VARARGS, nullptr}, {"SetWindowPosition", wnd_set_position, METH_VARARGS, nullptr}, {"GetWindowPosition", wnd_get_position, METH_VARARGS, nullptr}, {"GetWindowWidth", wnd_get_width, METH_VARARGS, nullptr}, {"GetWindowHeight", wnd_get_height, METH_VARARGS, nullptr}, {"Show", wnd_show, METH_VARARGS, nullptr}, {"Hide", wnd_hide, METH_VARARGS, nullptr}, {"IsShow", wnd_is_show, METH_VARARGS, nullptr}, """ known = {"SetScreenSize", "GetScreenWidth", "GetScreenHeight", "SetMouseHandler", "Register", "Destroy", "SetWindowSize", "SetWindowPosition", "GetWindowPosition", "GetWindowWidth", "GetWindowHeight", "Show", "Hide", "IsShow"} method_entries = [] for name in method_names: if name in known: continue if name.startswith("Register"): method_entries.append(f' {{"{name}", wnd_dummy_register, METH_VARARGS, nullptr}},') elif name.startswith("Is") or name.startswith("Get"): method_entries.append(f' {{"{name}", wnd_dummy_zero, METH_VARARGS, nullptr}},') else: method_entries.append(f' {{"{name}", wnd_dummy_none, METH_VARARGS, nullptr}},') footer = """ {nullptr, nullptr, 0, nullptr} }; auto* module = Py_InitModule("wndMgr", methods); PyModule_AddIntConstant(module, "SLOT_STYLE_NONE", 0); PyModule_AddIntConstant(module, "SLOT_STYLE_PICK_UP", 1); PyModule_AddIntConstant(module, "SLOT_STYLE_SELECT", 2); PyModule_AddIntConstant(module, "TEXT_HORIZONTAL_ALIGN_LEFT", 0); PyModule_AddIntConstant(module, "TEXT_HORIZONTAL_ALIGN_RIGHT", 1); PyModule_AddIntConstant(module, "TEXT_HORIZONTAL_ALIGN_CENTER", 2); PyModule_AddIntConstant(module, "TEXT_VERTICAL_ALIGN_TOP", 0); PyModule_AddIntConstant(module, "TEXT_VERTICAL_ALIGN_BOTTOM", 1); PyModule_AddIntConstant(module, "TEXT_VERTICAL_ALIGN_CENTER", 2); PyModule_AddIntConstant(module, "HORIZONTAL_ALIGN_LEFT", 0); PyModule_AddIntConstant(module, "HORIZONTAL_ALIGN_CENTER", 1); PyModule_AddIntConstant(module, "HORIZONTAL_ALIGN_RIGHT", 2); PyModule_AddIntConstant(module, "VERTICAL_ALIGN_TOP", 0); PyModule_AddIntConstant(module, "VERTICAL_ALIGN_CENTER", 1); PyModule_AddIntConstant(module, "VERTICAL_ALIGN_BOTTOM", 2); PyModule_AddIntConstant(module, "RENDERING_MODE_MODULATE", 1); } } // namespace ava2 """ (root / "src/python_wndmgr.cpp").write_text(cpp_code + "\n".join(method_entries) + "\n" + footer) print(f"Generated python_wndmgr.cpp with {len(method_names)} AVA2 methods.")