mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-27 23:37:05 -05:00
Added Python API function to create structs and unions
This commit is contained in:
2
python_libs/lib/imhex.py
Normal file
2
python_libs/lib/imhex.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from _imhex import *
|
||||||
|
import imhex_python.types as types
|
||||||
BIN
python_libs/lib/imhex_python/__pycache__/types.cpython-38.pyc
Normal file
BIN
python_libs/lib/imhex_python/__pycache__/types.cpython-38.pyc
Normal file
Binary file not shown.
44
python_libs/lib/imhex_python/types.py
Normal file
44
python_libs/lib/imhex_python/types.py
Normal file
@@ -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
|
||||||
@@ -119,17 +119,48 @@ namespace hex {
|
|||||||
PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType");
|
PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType");
|
||||||
return nullptr;
|
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) {
|
// Array already is an object
|
||||||
PyErr_SetString(PyExc_TypeError, "member needs to have a annotation extending from ImHexType");
|
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);
|
Py_DECREF(memberTypeInstance);
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
code += " "s + memberTypeInstance->ob_type->tp_name + " "s + memberName + ";\n";
|
|
||||||
|
|
||||||
Py_DECREF(memberTypeInstance);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
code += "};\n";
|
code += "};\n";
|
||||||
|
|||||||
Reference in New Issue
Block a user