fix: ImHex not loading python libraries correctly
This commit is contained in:
parent
8b9d09aa97
commit
16b91eb715
@ -46,6 +46,8 @@ FUNCTION_DEFINITION(PyObject *, PyImport_AddModule, (const char *name), (name))
|
|||||||
FUNCTION_DEFINITION(PyObject *, PyModule_New, (const char *name), (name))
|
FUNCTION_DEFINITION(PyObject *, PyModule_New, (const char *name), (name))
|
||||||
FUNCTION_DEFINITION(PyObject *, PyObject_GetAttrString, (PyObject *pobj, const char *name), (pobj, name))
|
FUNCTION_DEFINITION(PyObject *, PyObject_GetAttrString, (PyObject *pobj, const char *name), (pobj, name))
|
||||||
FUNCTION_DEFINITION(int, PyObject_HasAttrString, (PyObject *pobj, const char *name), (pobj, name))
|
FUNCTION_DEFINITION(int, PyObject_HasAttrString, (PyObject *pobj, const char *name), (pobj, name))
|
||||||
|
FUNCTION_DEFINITION(PyObject*, PySys_GetObject, (const char *name), (name))
|
||||||
|
FUNCTION_DEFINITION(int, PyList_Append, (PyObject *plist, PyObject *pvalue), (plist, pvalue))
|
||||||
|
|
||||||
bool initPythonLoader() {
|
bool initPythonLoader() {
|
||||||
void *pythonLibrary = nullptr;
|
void *pythonLibrary = nullptr;
|
||||||
@ -63,6 +65,7 @@ bool initPythonLoader() {
|
|||||||
INIT_FUNCTION(Py_PreInitialize);
|
INIT_FUNCTION(Py_PreInitialize);
|
||||||
INIT_FUNCTION(Py_Initialize);
|
INIT_FUNCTION(Py_Initialize);
|
||||||
INIT_FUNCTION(Py_Finalize);
|
INIT_FUNCTION(Py_Finalize);
|
||||||
|
INIT_FUNCTION(PySys_GetObject);
|
||||||
|
|
||||||
INIT_FUNCTION(PyEval_SaveThread);
|
INIT_FUNCTION(PyEval_SaveThread);
|
||||||
INIT_FUNCTION(PyEval_RestoreThread);
|
INIT_FUNCTION(PyEval_RestoreThread);
|
||||||
@ -95,13 +98,13 @@ bool initPythonLoader() {
|
|||||||
|
|
||||||
INIT_FUNCTION(PyDict_GetItemString);
|
INIT_FUNCTION(PyDict_GetItemString);
|
||||||
INIT_FUNCTION(PyDict_SetItemString);
|
INIT_FUNCTION(PyDict_SetItemString);
|
||||||
|
INIT_FUNCTION(PyList_Append);
|
||||||
|
|
||||||
INIT_FUNCTION(PyCallable_Check);
|
INIT_FUNCTION(PyCallable_Check);
|
||||||
INIT_FUNCTION(PyObject_CallObject);
|
INIT_FUNCTION(PyObject_CallObject);
|
||||||
INIT_FUNCTION(PyObject_GetAttrString);
|
INIT_FUNCTION(PyObject_GetAttrString);
|
||||||
INIT_FUNCTION(PyObject_HasAttrString);
|
INIT_FUNCTION(PyObject_HasAttrString);
|
||||||
|
|
||||||
|
|
||||||
INIT_FUNCTION(_Py_Dealloc);
|
INIT_FUNCTION(_Py_Dealloc);
|
||||||
|
|
||||||
|
|
||||||
|
@ -114,12 +114,13 @@ namespace hex::script::loader {
|
|||||||
if (!entry.is_directory())
|
if (!entry.is_directory())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const auto scriptPath = entry.path() / "main.py";
|
const auto &scriptFolder = entry.path();
|
||||||
|
const auto scriptPath = scriptFolder / "main.py";
|
||||||
if (!std::fs::exists(scriptPath))
|
if (!std::fs::exists(scriptPath))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto pathString = wolv::util::toUTF8String(scriptPath);
|
auto scriptPathString = wolv::util::toUTF8String(scriptPath);
|
||||||
wolv::io::File scriptFile(pathString, wolv::io::File::Mode::Read);
|
wolv::io::File scriptFile(scriptPathString, wolv::io::File::Mode::Read);
|
||||||
if (!scriptFile.isValid())
|
if (!scriptFile.isValid())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -131,12 +132,14 @@ namespace hex::script::loader {
|
|||||||
PyThreadState_DeleteCurrent();
|
PyThreadState_DeleteCurrent();
|
||||||
};
|
};
|
||||||
|
|
||||||
PyObject *libraryModule = PyImport_AddModule("imhex");
|
PyObject* sysPath = PySys_GetObject("path");
|
||||||
|
PyList_Append(sysPath, PyUnicode_FromString(wolv::util::toUTF8String(scriptFolder).c_str()));
|
||||||
|
|
||||||
PyModule_AddStringConstant(libraryModule, "__script_loader__", hex::format("{}", reinterpret_cast<intptr_t>(hex::getContainingModule((void*)&getCurrentTraceback))).c_str());
|
|
||||||
populateModule(libraryModule, romfs::get("python/imhex.py").data<const char>());
|
|
||||||
|
|
||||||
PyObject *mainModule = PyModule_New(pathString.c_str());
|
PyObject *imhexInternalModule = PyImport_AddModule("__imhex_internal__");
|
||||||
|
PyModule_AddStringConstant(imhexInternalModule, "script_loader_handle", hex::format("{}", reinterpret_cast<intptr_t>(hex::getContainingModule((void*)&getCurrentTraceback))).c_str());
|
||||||
|
|
||||||
|
PyObject *mainModule = PyModule_New(scriptPathString.c_str());
|
||||||
populateModule(mainModule, scriptFile.readString());
|
populateModule(mainModule, scriptFile.readString());
|
||||||
|
|
||||||
if (PyObject_HasAttrString(mainModule, "main")) {
|
if (PyObject_HasAttrString(mainModule, "main")) {
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
|
import __imhex_internal__
|
||||||
|
|
||||||
import ctypes
|
import ctypes
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
_script_loader = ctypes.CDLL("Script Loader", ctypes.DEFAULT_MODE, int(__script_loader__))
|
_script_loader = ctypes.CDLL("Script Loader", ctypes.DEFAULT_MODE, int(__imhex_internal__.script_loader_handle))
|
||||||
_callback_refs = []
|
_callback_refs = []
|
||||||
|
|
||||||
|
|
||||||
@ -35,13 +37,13 @@ class UI:
|
|||||||
def show_yes_no_question_box(title: str, message: str):
|
def show_yes_no_question_box(title: str, message: str):
|
||||||
result = ctypes.c_bool()
|
result = ctypes.c_bool()
|
||||||
_script_loader.showYesNoQuestionBoxV1(ctypes.create_string_buffer(title.encode("utf-8")),
|
_script_loader.showYesNoQuestionBoxV1(ctypes.create_string_buffer(title.encode("utf-8")),
|
||||||
ctypes.create_string_buffer(message.encode("utf-8")),
|
ctypes.create_string_buffer(message.encode("utf-8")),
|
||||||
ctypes.byref(result))
|
ctypes.byref(result))
|
||||||
|
|
||||||
class ToastType(Enum):
|
class ToastType(Enum):
|
||||||
INFO = 0
|
INFO = 0
|
||||||
WARNING = 1
|
WARNING = 1
|
||||||
ERROR = 2
|
ERROR = 2
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def show_toast(message: str, toast_type: ToastType):
|
def show_toast(message: str, toast_type: ToastType):
|
||||||
@ -59,8 +61,8 @@ class UI:
|
|||||||
_callback_refs.append(draw_function(draw_callback))
|
_callback_refs.append(draw_function(draw_callback))
|
||||||
|
|
||||||
_script_loader.registerViewV1(ctypes.create_string_buffer(icon.encode("utf-8")),
|
_script_loader.registerViewV1(ctypes.create_string_buffer(icon.encode("utf-8")),
|
||||||
ctypes.create_string_buffer(name.encode("utf-8")),
|
ctypes.create_string_buffer(name.encode("utf-8")),
|
||||||
draw_function(draw_callback))
|
_callback_refs[-1])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_menu_item(icon: str, menu_name: str, item_name: str, callback):
|
def add_menu_item(icon: str, menu_name: str, item_name: str, callback):
|
||||||
@ -74,6 +76,7 @@ class UI:
|
|||||||
ctypes.create_string_buffer(item_name.encode("utf-8")),
|
ctypes.create_string_buffer(item_name.encode("utf-8")),
|
||||||
_callback_refs[-1])
|
_callback_refs[-1])
|
||||||
|
|
||||||
|
|
||||||
class Bookmarks:
|
class Bookmarks:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_bookmark(address: int, size: int, color: Color, name: str, description: str = ""):
|
def create_bookmark(address: int, size: int, color: Color, name: str, description: str = ""):
|
||||||
@ -82,6 +85,7 @@ class Bookmarks:
|
|||||||
ctypes.create_string_buffer(name.encode("utf-8")),
|
ctypes.create_string_buffer(name.encode("utf-8")),
|
||||||
ctypes.create_string_buffer(description.encode("utf-8")))
|
ctypes.create_string_buffer(description.encode("utf-8")))
|
||||||
|
|
||||||
|
|
||||||
class Logger:
|
class Logger:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def print(message: str):
|
def print(message: str):
|
||||||
@ -111,6 +115,7 @@ class Logger:
|
|||||||
def fatal(message: str):
|
def fatal(message: str):
|
||||||
_script_loader.logFatalV1(ctypes.create_string_buffer(message.encode("utf-8")))
|
_script_loader.logFatalV1(ctypes.create_string_buffer(message.encode("utf-8")))
|
||||||
|
|
||||||
|
|
||||||
class Memory:
|
class Memory:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(address: int, size: int):
|
def read(address: int, size: int):
|
||||||
@ -172,4 +177,4 @@ class Memory:
|
|||||||
ctypes.create_string_buffer(provider.name.encode("utf-8")),
|
ctypes.create_string_buffer(provider.name.encode("utf-8")),
|
||||||
_callback_refs[-3],
|
_callback_refs[-3],
|
||||||
_callback_refs[-2],
|
_callback_refs[-2],
|
||||||
_callback_refs[-1])
|
_callback_refs[-1])
|
||||||
|
Loading…
Reference in New Issue
Block a user