mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-30 05:05:19 -05:00
fix: ImHex not loading python libraries correctly
This commit is contained in:
@@ -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")) {
|
||||
|
||||
Reference in New Issue
Block a user