2021-12-07 22:47:41 +01:00
|
|
|
#include <hex/helpers/loader_script_handler.hpp>
|
2020-12-01 02:21:40 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2021-09-03 02:33:15 +02:00
|
|
|
#include <hex/helpers/paths.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/helpers/file.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/views/view.hpp>
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2020-12-01 02:21:40 +01:00
|
|
|
|
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
#include <Python.h>
|
2020-12-01 16:41:38 +01:00
|
|
|
#include <structmember.h>
|
|
|
|
|
2020-12-01 02:21:40 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <filesystem>
|
|
|
|
|
2020-12-01 16:41:38 +01:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
2020-12-01 02:21:40 +01:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
PyObject* LoaderScript::Py_getFilePath(PyObject *self, PyObject *args) {
|
2022-01-16 01:51:31 +01:00
|
|
|
return PyUnicode_FromString(LoaderScript::s_filePath.string().c_str());
|
2020-12-01 02:21:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* LoaderScript::Py_addPatch(PyObject *self, PyObject *args) {
|
|
|
|
u64 address;
|
|
|
|
u8 *patches;
|
|
|
|
Py_ssize_t count;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "K|y#", &address, &patches, &count)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (patches == nullptr || count == 0) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Invalid patch provided");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address >= LoaderScript::s_dataProvider->getActualSize()) {
|
|
|
|
PyErr_SetString(PyExc_IndexError, "address out of range");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoaderScript::s_dataProvider->write(address, patches, count);
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* LoaderScript::Py_addBookmark(PyObject *self, PyObject *args) {
|
2021-01-20 20:16:24 +01:00
|
|
|
u64 address;
|
|
|
|
size_t size;
|
2020-12-01 02:21:40 +01:00
|
|
|
|
|
|
|
char *name = nullptr;
|
|
|
|
char *comment = nullptr;
|
|
|
|
|
2021-01-20 20:16:24 +01:00
|
|
|
if (!PyArg_ParseTuple(args, "K|n|s|s", &address, &size, &name, &comment)) {
|
2020-12-01 02:21:40 +01:00
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == nullptr || comment == nullptr) {
|
|
|
|
PyErr_SetString(PyExc_IndexError, "address out of range");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:16:24 +01:00
|
|
|
ImHexApi::Bookmarks::add(address, size, name, comment);
|
2020-12-01 02:21:40 +01:00
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:41:38 +01:00
|
|
|
static PyObject* createStructureType(std::string keyword, PyObject *args) {
|
|
|
|
auto type = PyTuple_GetItem(args, 0);
|
|
|
|
if (type == nullptr) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto instance = PyObject_CallObject(type, nullptr);
|
|
|
|
if (instance == nullptr) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-31 22:54:43 +02:00
|
|
|
ON_SCOPE_EXIT { Py_DECREF(instance); };
|
2020-12-01 16:41:38 +01:00
|
|
|
|
|
|
|
if (instance->ob_type->tp_base == nullptr || instance->ob_type->tp_base->tp_name != "ImHexType"s) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "class type must extend from ImHexType");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dict = instance->ob_type->tp_dict;
|
|
|
|
if (dict == nullptr) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto annotations = PyDict_GetItemString(dict, "__annotations__");
|
|
|
|
if (annotations == nullptr) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto list = PyDict_Items(annotations);
|
|
|
|
if (list == nullptr) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-31 22:54:43 +02:00
|
|
|
ON_SCOPE_EXIT { Py_DECREF(list); };
|
2020-12-01 16:41:38 +01:00
|
|
|
|
|
|
|
std::string code = keyword + " " + instance->ob_type->tp_name + " {\n";
|
|
|
|
|
2022-01-15 14:14:53 +01:00
|
|
|
for (Py_ssize_t i = 0; i < PyList_Size(list); i++) {
|
2020-12-01 16:41:38 +01:00
|
|
|
auto item = PyList_GetItem(list, i);
|
|
|
|
|
|
|
|
auto memberName = PyUnicode_AsUTF8(PyTuple_GetItem(item, 0));
|
|
|
|
auto memberType = PyTuple_GetItem(item, 1);
|
|
|
|
if (memberType == nullptr) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:21:29 +01:00
|
|
|
// Array already is an object
|
|
|
|
if (memberType->ob_type->tp_name == "array"s) {
|
|
|
|
|
|
|
|
auto arrayType = PyObject_GetAttrString(memberType, "array_type");
|
|
|
|
if (arrayType == nullptr) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
code += " "s + arrayType->ob_type->tp_name + " " + memberName;
|
|
|
|
|
|
|
|
auto arraySize = PyObject_GetAttrString(memberType, "size");
|
|
|
|
if (arraySize == nullptr) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-12-01 16:41:38 +01:00
|
|
|
|
2020-12-01 18:21:29 +01:00
|
|
|
if (PyUnicode_Check(arraySize))
|
|
|
|
code += "["s + PyUnicode_AsUTF8(arraySize) + "];\n";
|
|
|
|
else if (PyLong_Check(arraySize))
|
|
|
|
code += "["s + std::to_string(PyLong_AsLong(arraySize)) + "];\n";
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "invalid array size type. Expected string or int");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-12-01 16:41:38 +01:00
|
|
|
|
2020-12-01 18:21:29 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
auto memberTypeInstance = PyObject_CallObject(memberType, nullptr);
|
|
|
|
if (memberTypeInstance == nullptr || memberTypeInstance->ob_type->tp_base == nullptr || memberTypeInstance->ob_type->tp_base->tp_name != "ImHexType"s) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType");
|
|
|
|
if (memberTypeInstance != nullptr)
|
|
|
|
Py_DECREF(memberTypeInstance);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
code += " "s + memberTypeInstance->ob_type->tp_name + " "s + memberName + ";\n";
|
|
|
|
|
|
|
|
Py_DECREF(memberTypeInstance);
|
|
|
|
}
|
2020-12-01 16:41:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
code += "};\n";
|
|
|
|
|
2021-09-26 21:18:25 +02:00
|
|
|
EventManager::post<RequestSetPatternLanguageCode>(code);
|
2020-12-01 16:41:38 +01:00
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* LoaderScript::Py_addStruct(PyObject *self, PyObject *args) {
|
|
|
|
return createStructureType("struct", args);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* LoaderScript::Py_addUnion(PyObject *self, PyObject *args) {
|
|
|
|
return createStructureType("union", args);
|
|
|
|
}
|
|
|
|
|
2022-01-16 01:51:31 +01:00
|
|
|
bool LoaderScript::processFile(const fs::path &scriptPath) {
|
2021-01-12 23:28:41 +01:00
|
|
|
Py_SetProgramName(Py_DecodeLocale((SharedData::mainArgv)[0], nullptr));
|
2020-12-01 02:21:40 +01:00
|
|
|
|
2021-03-01 08:56:49 +01:00
|
|
|
for (const auto &dir : hex::getPath(ImHexPath::Python)) {
|
2022-01-13 14:33:30 +01:00
|
|
|
if (fs::exists(fs::path(dir / "lib" / "python" PYTHON_VERSION_MAJOR_MINOR))) {
|
|
|
|
Py_SetPythonHome(Py_DecodeLocale(dir.string().c_str(), nullptr));
|
2021-03-01 08:56:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 16:41:38 +01:00
|
|
|
|
|
|
|
PyImport_AppendInittab("_imhex", []() -> PyObject* {
|
2020-12-01 02:21:40 +01:00
|
|
|
|
|
|
|
static PyMethodDef ImHexMethods[] = {
|
2020-12-01 16:41:38 +01:00
|
|
|
{ "get_file_path", &LoaderScript::Py_getFilePath, METH_NOARGS, "Returns the path of the file being loaded." },
|
|
|
|
{ "patch", &LoaderScript::Py_addPatch, METH_VARARGS, "Patches a region of memory" },
|
|
|
|
{ "add_bookmark", &LoaderScript::Py_addBookmark, METH_VARARGS, "Adds a bookmark" },
|
|
|
|
{ "add_struct", &LoaderScript::Py_addStruct, METH_VARARGS, "Adds a struct" },
|
|
|
|
{ "add_union", &LoaderScript::Py_addUnion, METH_VARARGS, "Adds a union" },
|
|
|
|
{ nullptr, nullptr, 0, nullptr }
|
2020-12-01 02:21:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyModuleDef ImHexModule = {
|
|
|
|
PyModuleDef_HEAD_INIT, "imhex", nullptr, -1, ImHexMethods, nullptr, nullptr, nullptr, nullptr
|
|
|
|
};
|
|
|
|
|
2020-12-01 16:41:38 +01:00
|
|
|
auto module = PyModule_Create(&ImHexModule);
|
|
|
|
if (module == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return module;
|
2020-12-01 02:21:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
Py_Initialize();
|
|
|
|
|
2020-12-01 16:41:38 +01:00
|
|
|
{
|
|
|
|
auto sysPath = PySys_GetObject("path");
|
|
|
|
auto path = PyUnicode_FromString("lib");
|
|
|
|
|
|
|
|
PyList_Insert(sysPath, 0, path);
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
File scriptFile(scriptPath, File::Mode::Read);
|
2022-01-16 01:51:31 +01:00
|
|
|
PyRun_SimpleFile(scriptFile.getHandle(), scriptFile.getPath().string().c_str());
|
2020-12-01 02:21:40 +01:00
|
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|