diff --git a/src/ext/extension.cc b/src/ext/extension.cc index c23bdd7b..a66831e0 100644 --- a/src/ext/extension.cc +++ b/src/ext/extension.cc @@ -41,37 +41,40 @@ void yaze_load_c_extension(const char* extension_path) { } void yaze_load_py_extension(const char* script_path) { - Py_Initialize(); + Py_Initialize(); - FILE* fp = fopen(script_path, "r"); - if (fp) { - PyRun_SimpleFile(fp, script_path); - fclose(fp); + FILE* fp = fopen(script_path, "r"); + if (fp) { + PyRun_SimpleFile(fp, script_path); + fclose(fp); + } else { + std::cerr << "Cannot open Python script: " << script_path << std::endl; + return; + } + + PyObject* pModule = PyImport_AddModule("__main__"); + PyObject* pFunc = PyObject_GetAttrString(pModule, "get_yaze_extension"); + + if (pFunc && PyCallable_Check(pFunc)) { + PyObject* pExtension = PyObject_CallObject(pFunc, nullptr); + if (pExtension) { + extension = + reinterpret_cast(PyLong_AsVoidPtr(pExtension)); + if (extension && extension->initialize) { + extension->initialize(); + } } else { - std::cerr << "Cannot open Python script: " << script_path << std::endl; - return; + std::cerr << "Failed to get extension from Python script." << std::endl; } + Py_XDECREF(pExtension); + } else { + std::cerr + << "Python function 'get_yaze_extension' not found or not callable." + << std::endl; + } - PyObject* pModule = PyImport_AddModule("__main__"); - PyObject* pFunc = PyObject_GetAttrString(pModule, "get_yaze_extension"); - - if (pFunc && PyCallable_Check(pFunc)) { - PyObject* pExtension = PyObject_CallObject(pFunc, nullptr); - if (pExtension) { - extension = reinterpret_cast(PyLong_AsVoidPtr(pExtension)); - if (extension && extension->initialize) { - extension->initialize(); - } - } else { - std::cerr << "Failed to get extension from Python script." << std::endl; - } - Py_XDECREF(pExtension); - } else { - std::cerr << "Python function 'get_yaze_extension' not found or not callable." << std::endl; - } - - Py_XDECREF(pFunc); - Py_Finalize(); + Py_XDECREF(pFunc); + Py_Finalize(); } void yaze_cleanup_extension() { diff --git a/src/ext/extension.h b/src/ext/extension.h index 30234737..595b8be6 100644 --- a/src/ext/extension.h +++ b/src/ext/extension.h @@ -9,6 +9,11 @@ typedef void (*yaze_imgui_render_callback)(void* editor_context); typedef void (*yaze_rom_operation)(z3_rom* rom); +/** + * @brief Extension interface for Yaze. + * + * @details Yaze extensions can be written in C or Python. + */ typedef struct yaze_extension { const char* name; const char* version; @@ -30,12 +35,26 @@ typedef struct yaze_extension { } yaze_extension; +/** + * @brief Get the extension interface. + */ yaze_extension* get_yaze_extension(); +/** + * @brief Load a C extension. + */ void yaze_load_c_extension(const char* extension_path); +/** + * @brief Load a Python extension. + */ void yaze_load_py_extension(const char* script_path); +/** + * @brief Clean up the extension. + */ +void yaze_cleanup_extension(); + #ifdef __cplusplus } #endif diff --git a/src/ext/sample.c b/src/ext/sample.c new file mode 100644 index 00000000..0f4d9ca2 --- /dev/null +++ b/src/ext/sample.c @@ -0,0 +1,26 @@ +#include "ext/extension.h" +#include "yaze.h" + +void my_extension_initialize() { + // Custom initialization code +} + +void my_extension_cleanup() { + // Custom cleanup code +} + +void my_extension_manipulate_rom(z3_rom* rom) { + // Modify ROM data +} + +yaze_extension* get_yaze_extension() { + static yaze_extension ext = { + "My Extension", "1.0", + my_extension_initialize, + my_extension_cleanup, + nullptr, + nullptr, + my_extension_manipulate_rom + }; + return &ext; +} \ No newline at end of file diff --git a/src/ext/sample.py b/src/ext/sample.py new file mode 100644 index 00000000..9f563166 --- /dev/null +++ b/src/ext/sample.py @@ -0,0 +1,29 @@ +import yaze + + +class MyExtension: + def __init__(self): + self.name = "My Python Extension" + self.version = "1.0" + + def initialize(self): + print(f"{self.name} Initialized") + + def cleanup(self): + print(f"{self.name} Cleaned Up") + + def render_ui(self, editor_context): + import imgui + imgui.begin("My Python Extension Window") + imgui.text("Hello from My Python Extension!") + imgui.end() + + def manipulate_rom(self, rom): + if rom and rom.data: + print(f"First byte of ROM: 0x{rom.data[0]:02X}") + else: + print("ROM data is not loaded.") + + +def get_yaze_extension(): + return MyExtension()