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 *, 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);
|
||||
|
||||
|
||||
|
@ -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")) {
|
||||
|
@ -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 = []
|
||||
|
||||
|
||||
@ -60,7 +62,7 @@ class UI:
|
||||
|
||||
_script_loader.registerViewV1(ctypes.create_string_buffer(icon.encode("utf-8")),
|
||||
ctypes.create_string_buffer(name.encode("utf-8")),
|
||||
draw_function(draw_callback))
|
||||
_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):
|
||||
|
Loading…
Reference in New Issue
Block a user