From 192e7d50607a3033312f45c84e69f259ab8f2f56 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 1 Dec 2020 18:21:29 +0100 Subject: [PATCH] Added Python API function to create structs and unions --- python_libs/lib/imhex.py | 2 + .../__pycache__/types.cpython-38.pyc | Bin 0 -> 2552 bytes python_libs/lib/imhex_python/types.py | 44 ++++++++++++++++ source/helpers/loader_script_handler.cpp | 49 ++++++++++++++---- 4 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 python_libs/lib/imhex.py create mode 100644 python_libs/lib/imhex_python/__pycache__/types.cpython-38.pyc create mode 100644 python_libs/lib/imhex_python/types.py diff --git a/python_libs/lib/imhex.py b/python_libs/lib/imhex.py new file mode 100644 index 000000000..438396caa --- /dev/null +++ b/python_libs/lib/imhex.py @@ -0,0 +1,2 @@ +from _imhex import * +import imhex_python.types as types \ No newline at end of file diff --git a/python_libs/lib/imhex_python/__pycache__/types.cpython-38.pyc b/python_libs/lib/imhex_python/__pycache__/types.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..97f84d2958a0c121f4bcb5c86bf51cc2dca42bb6 GIT binary patch literal 2552 zcmb7`L2uhO7=}qvvK7Zp+%!p(G)=R$>)=a)c0;j4Fa#)uZHM`i^9hpOf%`*$qlEaA zFkd3TK5F(Cns0>>MqZ#=ajpgu92f;wR+{ivH3%>gnFdB0Hoyu6ZkiYzdk(=1rUh;QMA9yzO}j{J`vhcRVkfhXG|~hY z@jw}6!V577--}QTB4|WK=lk(@_T7&s$My%CC&Tbsf?w=6P$`?OpvlF#_)VUPGkF%A zso%o`aVh(~pop^Bv2{gL>xunE>t0x>VVV`;BpKTxI!rQ~!S*n}l*NM9!!*eh3;W*cmMU*&~oWSESv*}S0{hXvT>$vu+Swyc`)cPlzk8(TKn&+%^Ve_^NA6E4m zMGTMdwptf0t;bU{OWAL0{dSh5S2O;d+%ipcu6JQ*(^Ts@2V%NHFPucfnsFy$*;amJ z2)I$WWFIwaqbWmfBtjb$3m+WrUnzuljE7|7_jE1dW!q$hG9v?AeP7{SD}K=Jd%m^}l#tp}z?|#!MXqf5j zFtrsXK856?ITPf6nNS$q=b|TQ$~*8C{th7NiGi}Ajf~n8wt??(4}S%4J2o6I%po5w z3;O};OH`lkkUNk;v_P^G8o9L0BSF#fc|jy9dBUApOoWW=6LuWF9pYA>iGL|JWq}%)$Oujz#T*4 dCI8EQ5$|0k!>=WlWmM`mS5!B+{@m=Te*hR`giZhe literal 0 HcmV?d00001 diff --git a/python_libs/lib/imhex_python/types.py b/python_libs/lib/imhex_python/types.py new file mode 100644 index 000000000..327976930 --- /dev/null +++ b/python_libs/lib/imhex_python/types.py @@ -0,0 +1,44 @@ +class ImHexTypeMeta(type): + def __new__(cls, name, bases, dct): + return super().__new__(cls, name, bases, dct) + + def __getitem__(self, value): + return array(self, value) + +class ImHexType(metaclass=ImHexTypeMeta): + pass + +class u8(ImHexType): + pass +class u16(ImHexType): + pass +class u32(ImHexType): + pass +class u64(ImHexType): + pass +class u128(ImHexType): + pass + +class s8(ImHexType): + pass +class s16(ImHexType): + pass +class s32(ImHexType): + pass +class s64(ImHexType): + pass +class s128(ImHexType): + pass + +class float(ImHexType): + pass +class double(ImHexType): + pass + +class array(ImHexType): + def __init__(self, array_type, size): + self.array_type = array_type() + self.size = size + + array_type : type + size : int \ No newline at end of file diff --git a/source/helpers/loader_script_handler.cpp b/source/helpers/loader_script_handler.cpp index 69c51e7cb..10507cd21 100644 --- a/source/helpers/loader_script_handler.cpp +++ b/source/helpers/loader_script_handler.cpp @@ -119,17 +119,48 @@ namespace hex { PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType"); return nullptr; } - 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"); + + // 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; + } + + 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; + } + + + } 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); - - return nullptr; } - - code += " "s + memberTypeInstance->ob_type->tp_name + " "s + memberName + ";\n"; - - Py_DECREF(memberTypeInstance); } code += "};\n";