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