python cleanup

This commit is contained in:
scawful
2024-08-20 22:44:12 -04:00
parent d85530b14b
commit b98f0416bb
4 changed files with 128 additions and 46 deletions

View File

@@ -1,34 +0,0 @@
#include <Python.h>
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);
}

View File

@@ -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<void>(initializeFunc_.ptr(), boost::python::ptr(context));
}
static void cleanup() { boost::python::call<void>(cleanupFunc_.ptr()); }
static void manipulate_rom(z3_rom* rom) {
boost::python::call<void>(manipulateRomFunc_.ptr(), boost::python::ptr(rom));
}
static void extend_ui(yaze_editor_context* context) {
boost::python::call<void>(extendUIFunc_.ptr(), boost::python::ptr(context));
}
static void register_commands() {
boost::python::call<void>(registerCommandsFunc_.ptr());
}
static void register_custom_tools() {
boost::python::call<void>(registerCustomToolsFunc_.ptr());
}
static void register_event_hooks(yaze_event_type event,
yaze_event_hook_func hook) {
boost::python::call<void>(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_<PythonYazeExtensionWrapper>("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<reference_existing_object>());
class_<z3_rom>("z3_rom")
.def_readonly("filename", &z3_rom::filename)
.def_readonly("data", &z3_rom::data)

View File

@@ -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

View File

@@ -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);
};
/**