1
0
mirror of synced 2024-11-23 23:31:02 +01:00

fix: ImHex not loading python libraries correctly

This commit is contained in:
WerWolv 2024-03-24 16:55:56 +01:00
parent 8b9d09aa97
commit 16b91eb715
3 changed files with 27 additions and 16 deletions

View File

@ -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 *, PyObject_GetAttrString, (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() {
void *pythonLibrary = nullptr;
@ -63,6 +65,7 @@ bool initPythonLoader() {
INIT_FUNCTION(Py_PreInitialize);
INIT_FUNCTION(Py_Initialize);
INIT_FUNCTION(Py_Finalize);
INIT_FUNCTION(PySys_GetObject);
INIT_FUNCTION(PyEval_SaveThread);
INIT_FUNCTION(PyEval_RestoreThread);
@ -95,13 +98,13 @@ bool initPythonLoader() {
INIT_FUNCTION(PyDict_GetItemString);
INIT_FUNCTION(PyDict_SetItemString);
INIT_FUNCTION(PyList_Append);
INIT_FUNCTION(PyCallable_Check);
INIT_FUNCTION(PyObject_CallObject);
INIT_FUNCTION(PyObject_GetAttrString);
INIT_FUNCTION(PyObject_HasAttrString);
INIT_FUNCTION(_Py_Dealloc);

View File

@ -114,12 +114,13 @@ namespace hex::script::loader {
if (!entry.is_directory())
continue;
const auto scriptPath = entry.path() / "main.py";
const auto &scriptFolder = entry.path();
const auto scriptPath = scriptFolder / "main.py";
if (!std::fs::exists(scriptPath))
continue;
auto pathString = wolv::util::toUTF8String(scriptPath);
wolv::io::File scriptFile(pathString, wolv::io::File::Mode::Read);
auto scriptPathString = wolv::util::toUTF8String(scriptPath);
wolv::io::File scriptFile(scriptPathString, wolv::io::File::Mode::Read);
if (!scriptFile.isValid())
continue;
@ -131,12 +132,14 @@ namespace hex::script::loader {
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());
if (PyObject_HasAttrString(mainModule, "main")) {

View File

@ -1,9 +1,11 @@
import __imhex_internal__
import ctypes
from enum import Enum
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 = []
@ -35,13 +37,13 @@ class UI:
def show_yes_no_question_box(title: str, message: str):
result = ctypes.c_bool()
_script_loader.showYesNoQuestionBoxV1(ctypes.create_string_buffer(title.encode("utf-8")),
ctypes.create_string_buffer(message.encode("utf-8")),
ctypes.byref(result))
ctypes.create_string_buffer(message.encode("utf-8")),
ctypes.byref(result))
class ToastType(Enum):
INFO = 0
INFO = 0
WARNING = 1
ERROR = 2
ERROR = 2
@staticmethod
def show_toast(message: str, toast_type: ToastType):
@ -59,8 +61,8 @@ class UI:
_callback_refs.append(draw_function(draw_callback))
_script_loader.registerViewV1(ctypes.create_string_buffer(icon.encode("utf-8")),
ctypes.create_string_buffer(name.encode("utf-8")),
draw_function(draw_callback))
ctypes.create_string_buffer(name.encode("utf-8")),
_callback_refs[-1])
@staticmethod
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")),
_callback_refs[-1])
class Bookmarks:
@staticmethod
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(description.encode("utf-8")))
class Logger:
@staticmethod
def print(message: str):
@ -111,6 +115,7 @@ class Logger:
def fatal(message: str):
_script_loader.logFatalV1(ctypes.create_string_buffer(message.encode("utf-8")))
class Memory:
@staticmethod
def read(address: int, size: int):
@ -172,4 +177,4 @@ class Memory:
ctypes.create_string_buffer(provider.name.encode("utf-8")),
_callback_refs[-3],
_callback_refs[-2],
_callback_refs[-1])
_callback_refs[-1])