update yaze_ext to match yaze_c style
This commit is contained in:
@@ -5,50 +5,82 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void* handle = nullptr;
|
static void* handle = nullptr;
|
||||||
yaze_extension* extension = nullptr;
|
static yaze_extension* extension = nullptr;
|
||||||
|
|
||||||
yaze_extension* GetExtension() { return nullptr; }
|
yaze_extension* get_yaze_extension() { return extension; }
|
||||||
|
|
||||||
void LoadCExtension(const char* extensionPath) {
|
void yaze_load_c_extension(const char* extension_path) {
|
||||||
handle = dlopen(extensionPath, RTLD_LAZY);
|
handle = dlopen(extension_path, RTLD_LAZY);
|
||||||
if (!handle) {
|
if (!handle) {
|
||||||
std::cerr << "Cannot open extension: " << dlerror() << std::endl;
|
std::cerr << "Cannot open extension: " << dlerror() << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
dlerror(); // Clear any existing error
|
dlerror(); // Clear any existing error
|
||||||
|
|
||||||
yaze_extension* (*getExtension)();
|
// Load the symbol to retrieve the extension
|
||||||
getExtension = (yaze_extension * (*)()) dlsym(handle, "getExtension");
|
auto get_extension = reinterpret_cast<yaze_extension* (*)()>(
|
||||||
|
dlsym(handle, "get_yaze_extension"));
|
||||||
const char* dlsym_error = dlerror();
|
const char* dlsym_error = dlerror();
|
||||||
if (dlsym_error) {
|
if (dlsym_error) {
|
||||||
std::cerr << "Cannot load symbol 'getExtension': " << dlsym_error
|
std::cerr << "Cannot load symbol 'get_yaze_extension': " << dlsym_error
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
dlclose(handle);
|
dlclose(handle);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
extension = getExtension();
|
extension = get_extension();
|
||||||
extension->initialize();
|
if (extension && extension->initialize) {
|
||||||
|
extension->initialize();
|
||||||
|
} else {
|
||||||
|
std::cerr << "Failed to initialize the extension." << std::endl;
|
||||||
|
dlclose(handle);
|
||||||
|
handle = nullptr;
|
||||||
|
extension = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadPythonExtension(const char* script_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);
|
|
||||||
}
|
|
||||||
|
|
||||||
PyObject* pModule = PyImport_AddModule("__main__");
|
FILE* fp = fopen(script_path, "r");
|
||||||
PyObject* pFunc = PyObject_GetAttrString(pModule, "get_extension");
|
if (fp) {
|
||||||
|
PyRun_SimpleFile(fp, script_path);
|
||||||
if (pFunc && PyCallable_Check(pFunc)) {
|
fclose(fp);
|
||||||
PyObject* pExtension = PyObject_CallObject(pFunc, nullptr);
|
} else {
|
||||||
if (pExtension) {
|
std::cerr << "Cannot open Python script: " << script_path << std::endl;
|
||||||
// Assume the Python extension has the same structure as the C extension
|
return;
|
||||||
extension =
|
|
||||||
reinterpret_cast<yaze_extension*>(PyLong_AsVoidPtr(pExtension));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<yaze_extension*>(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();
|
||||||
|
}
|
||||||
|
|
||||||
|
void yaze_cleanup_extension() {
|
||||||
|
if (extension && extension->cleanup) {
|
||||||
|
extension->cleanup();
|
||||||
|
}
|
||||||
|
if (handle) {
|
||||||
|
dlclose(handle);
|
||||||
|
handle = nullptr;
|
||||||
|
extension = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,10 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef void (*yaze_imgui_render_callback)(void* editor_context);
|
||||||
|
|
||||||
|
typedef void (*yaze_rom_operation)(z3_rom* rom);
|
||||||
|
|
||||||
typedef struct yaze_extension {
|
typedef struct yaze_extension {
|
||||||
const char* name;
|
const char* name;
|
||||||
const char* version;
|
const char* version;
|
||||||
@@ -16,16 +20,21 @@ typedef struct yaze_extension {
|
|||||||
void (*cleanup)(void);
|
void (*cleanup)(void);
|
||||||
|
|
||||||
// Function to extend editor functionality
|
// Function to extend editor functionality
|
||||||
void (*extendFunctionality)(void* editorContext);
|
void (*extend_functionality)(void* editor_context);
|
||||||
|
|
||||||
|
// ImGui rendering callback
|
||||||
|
yaze_imgui_render_callback render_ui;
|
||||||
|
|
||||||
|
// ROM manipulation callback
|
||||||
|
yaze_rom_operation manipulate_rom;
|
||||||
|
|
||||||
} yaze_extension;
|
} yaze_extension;
|
||||||
|
|
||||||
// Function to get the extension instance
|
yaze_extension* get_yaze_extension();
|
||||||
yaze_extension* GetExtension();
|
|
||||||
|
|
||||||
void LoadCExtension(const char* extension_path);
|
void yaze_load_c_extension(const char* extension_path);
|
||||||
|
|
||||||
// Function to load a Python script as an extension
|
void yaze_load_py_extension(const char* script_path);
|
||||||
void LoadPythonExtension(const char* script_path);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user