diff --git a/src/py/yaze_c_py.cc b/src/py/yaze_c_py.cc deleted file mode 100644 index f4bf64fc..00000000 --- a/src/py/yaze_c_py.cc +++ /dev/null @@ -1,34 +0,0 @@ -#include - -static PyObject *yaze_init(PyObject *self, PyObject *args); - -/** - * Python C API Example, in case I need more functionality than Boost.Python - */ -static PyMethodDef YazeMethods[] = { - {"system", yaze_init, METH_VARARGS, "Initialize the yaze lib."}, - {NULL, NULL, 0, NULL} /* Sentinel */ -}; - -static struct PyModuleDef yaze_module = {PyModuleDef_HEAD_INIT, - "yaze", // Module title - NULL, // Documentation - -1, // Interpreter state size - YazeMethods}; - -// PyMODINIT_FUNC PyInit_yaze(void) { return PyModule_Create(&yaze_module); } - -static PyObject *YazeError; - -static PyObject *yaze_init(PyObject *self, PyObject *args) { - const char *command; - int sts; - - if (!PyArg_ParseTuple(args, "s", &command)) return NULL; - sts = system(command); - if (sts < 0) { - PyErr_SetString(YazeError, "System command failed"); - return NULL; - } - return PyLong_FromLong(sts); -} \ No newline at end of file diff --git a/src/py/yaze_py.cc b/src/py/yaze_py.cc index c61841c9..8b7cd802 100644 --- a/src/py/yaze_py.cc +++ b/src/py/yaze_py.cc @@ -3,12 +3,139 @@ #include "base/overworld.h" #include "base/snes_color.h" #include "base/sprite.h" -#include "ext/extension.h" +#include "base/extension.h" #include "yaze.h" +class PythonYazeExtensionWrapper { + public: + PythonYazeExtensionWrapper() : ext_() { + // Initialize function pointers to nullptr or default implementations + ext_.initialize = nullptr; + ext_.cleanup = nullptr; + ext_.manipulate_rom = nullptr; + ext_.extend_ui = nullptr; + ext_.register_commands = nullptr; + ext_.register_custom_tools = nullptr; + ext_.register_event_hooks = nullptr; + } + + const char* getName() const { return ext_.name; } + + const char* getVersion() const { return ext_.version; } + + void setName(const std::string& name) { ext_.name = name.c_str(); } + + void setVersion(const std::string& version) { + ext_.version = version.c_str(); + } + + void setInitialize(boost::python::object func) { + ext_.initialize = &PythonYazeExtensionWrapper::initialize; + initializeFunc_ = func; + } + + void setCleanup(boost::python::object func) { + ext_.cleanup = &PythonYazeExtensionWrapper::cleanup; + cleanupFunc_ = func; + } + + void setManipulateRom(boost::python::object func) { + ext_.manipulate_rom = &PythonYazeExtensionWrapper::manipulate_rom; + manipulateRomFunc_ = func; + } + + void setExtendUI(boost::python::object func) { + ext_.extend_ui = &PythonYazeExtensionWrapper::extend_ui; + extendUIFunc_ = func; + } + + void setRegisterCommands(boost::python::object func) { + ext_.register_commands = &PythonYazeExtensionWrapper::register_commands; + registerCommandsFunc_ = func; + } + + void setRegisterCustomTools(boost::python::object func) { + ext_.register_custom_tools = + &PythonYazeExtensionWrapper::register_custom_tools; + registerCustomToolsFunc_ = func; + } + + void setRegisterEventHooks(boost::python::object func) { + ext_.register_event_hooks = + &PythonYazeExtensionWrapper::register_event_hooks; + registerEventHooksFunc_ = func; + } + + yaze_extension* getExtension() { return &ext_; } + + private: + static void initialize(yaze_editor_context* context) { + boost::python::call(initializeFunc_.ptr(), boost::python::ptr(context)); + } + + static void cleanup() { boost::python::call(cleanupFunc_.ptr()); } + + static void manipulate_rom(z3_rom* rom) { + boost::python::call(manipulateRomFunc_.ptr(), boost::python::ptr(rom)); + } + + static void extend_ui(yaze_editor_context* context) { + boost::python::call(extendUIFunc_.ptr(), boost::python::ptr(context)); + } + + static void register_commands() { + boost::python::call(registerCommandsFunc_.ptr()); + } + + static void register_custom_tools() { + boost::python::call(registerCustomToolsFunc_.ptr()); + } + + static void register_event_hooks(yaze_event_type event, + yaze_event_hook_func hook) { + boost::python::call(registerEventHooksFunc_.ptr(), event, hook); + } + + yaze_extension ext_; + static boost::python::object initializeFunc_; + static boost::python::object cleanupFunc_; + static boost::python::object manipulateRomFunc_; + static boost::python::object extendUIFunc_; + static boost::python::object registerCommandsFunc_; + static boost::python::object registerCustomToolsFunc_; + static boost::python::object registerEventHooksFunc_; +}; + +// Static members initialization +boost::python::object PythonYazeExtensionWrapper::initializeFunc_; +boost::python::object PythonYazeExtensionWrapper::cleanupFunc_; +boost::python::object PythonYazeExtensionWrapper::manipulateRomFunc_; +boost::python::object PythonYazeExtensionWrapper::extendUIFunc_; +boost::python::object PythonYazeExtensionWrapper::registerCommandsFunc_; +boost::python::object PythonYazeExtensionWrapper::registerCustomToolsFunc_; +boost::python::object PythonYazeExtensionWrapper::registerEventHooksFunc_; + BOOST_PYTHON_MODULE(yaze_py) { using namespace boost::python; + class_("YazeExtension") + .add_property("name", &PythonYazeExtensionWrapper::getName, + &PythonYazeExtensionWrapper::setName) + .add_property("version", &PythonYazeExtensionWrapper::getVersion, + &PythonYazeExtensionWrapper::setVersion) + .def("setInitialize", &PythonYazeExtensionWrapper::setInitialize) + .def("setCleanup", &PythonYazeExtensionWrapper::setCleanup) + .def("setManipulateRom", &PythonYazeExtensionWrapper::setManipulateRom) + .def("setExtendUI", &PythonYazeExtensionWrapper::setExtendUI) + .def("setRegisterCommands", + &PythonYazeExtensionWrapper::setRegisterCommands) + .def("setRegisterCustomTools", + &PythonYazeExtensionWrapper::setRegisterCustomTools) + .def("setRegisterEventHooks", + &PythonYazeExtensionWrapper::setRegisterEventHooks) + .def("getExtension", &PythonYazeExtensionWrapper::getExtension, + return_value_policy()); + class_("z3_rom") .def_readonly("filename", &z3_rom::filename) .def_readonly("data", &z3_rom::data) diff --git a/src/py/yaze_py.cmake b/src/py/yaze_py.cmake index 75d2a38f..2276f9b0 100644 --- a/src/py/yaze_py.cmake +++ b/src/py/yaze_py.cmake @@ -1,5 +1,3 @@ -find_package(PythonLibs 3.8 REQUIRED) -cmake_policy(SET CMP0167 NEW) find_package(Boost COMPONENTS python3 REQUIRED) # target x86_64 for module diff --git a/src/yaze.h b/src/yaze.h index 5be798c0..34a53c10 100644 --- a/src/yaze.h +++ b/src/yaze.h @@ -12,9 +12,6 @@ extern "C" { #include "base/snes_color.h" #include "base/sprite.h" -typedef struct ImGuiContext ImGuiContext; -typedef struct ImGuiIO ImGuiIO; - typedef struct z3_rom z3_rom; typedef struct yaze_flags yaze_flags; typedef struct yaze_project yaze_project; @@ -47,12 +44,6 @@ struct yaze_editor_context { yaze_command_registry* command_registry; yaze_event_dispatcher* event_dispatcher; - - ImGuiContext* imgui_context; - ImGuiIO* imgui_io; - - void (*ShowDialog)(const char* title, const char* message); - void (*LogMessage)(const char* message); }; /**