@@ -16,8 +16,54 @@ static PyObject *py_ue_uscriptstruct_get_field(ue_PyUScriptStruct *self, PyObjec
1616 return ue_py_convert_property (u_property, self->data );
1717}
1818
19+ static PyObject *py_ue_uscriptstruct_set_field (ue_PyUScriptStruct *self, PyObject * args) {
20+ char *name;
21+ PyObject *value;
22+ if (!PyArg_ParseTuple (args, " sO:set_field" , &name, &value)) {
23+ return NULL ;
24+ }
25+
26+ UProperty *u_property = self->u_struct ->FindPropertyByName (FName (UTF8_TO_TCHAR (name)));
27+ if (!u_property)
28+ return PyErr_Format (PyExc_Exception, " unable to find property %s" , name);
29+
30+
31+ if (!ue_py_convert_pyobject (value, u_property, self->data )) {
32+ return PyErr_Format (PyExc_Exception, " unable to set property %s" , name);
33+ }
34+
35+ Py_INCREF (Py_None);
36+ return Py_None;
37+
38+ }
39+
40+ static PyObject *py_ue_uscriptstruct_fields (ue_PyUScriptStruct *self, PyObject * args) {
41+ PyObject *ret = PyList_New (0 );
42+
43+ for (TFieldIterator<UProperty> PropIt (self->u_struct ); PropIt; ++PropIt)
44+ {
45+ UProperty* property = *PropIt;
46+ PyObject *property_name = PyUnicode_FromString (TCHAR_TO_UTF8 (*property->GetName ()));
47+ PyList_Append (ret, property_name);
48+ Py_DECREF (property_name);
49+ }
50+
51+ return ret;
52+ }
53+
54+ static PyObject *py_ue_uscriptstruct_get_struct (ue_PyUScriptStruct *self, PyObject * args) {
55+ ue_PyUObject *ret = ue_get_python_wrapper (self->u_struct );
56+ if (!ret)
57+ return PyErr_Format (PyExc_Exception, " PyUObject is in invalid state" );
58+ Py_INCREF (ret);
59+ return (PyObject *)ret;
60+ }
61+
1962static PyMethodDef ue_PyUScriptStruct_methods[] = {
2063 { " get_field" , (PyCFunction)py_ue_uscriptstruct_get_field, METH_VARARGS, " " },
64+ { " set_field" , (PyCFunction)py_ue_uscriptstruct_set_field, METH_VARARGS, " " },
65+ { " fields" , (PyCFunction)py_ue_uscriptstruct_fields, METH_VARARGS, " " },
66+ { " get_struct" , (PyCFunction)py_ue_uscriptstruct_get_struct, METH_VARARGS, " " },
2167 { NULL } /* Sentinel */
2268};
2369
@@ -28,6 +74,40 @@ static PyObject *ue_PyUScriptStruct_str(ue_PyUScriptStruct *self)
2874 TCHAR_TO_UTF8 (*self->u_struct ->GetName ()), self->u_struct ->GetStructureSize ());
2975}
3076
77+ static PyObject *ue_PyUScriptStruct_getattro (ue_PyUScriptStruct *self, PyObject *attr_name) {
78+ PyObject *ret = PyObject_GenericGetAttr ((PyObject *)self, attr_name);
79+ if (!ret) {
80+ if (PyUnicodeOrString_Check (attr_name)) {
81+ char *attr = PyUnicode_AsUTF8 (attr_name);
82+ // first check for property
83+ UProperty *u_property = self->u_struct ->FindPropertyByName (FName (UTF8_TO_TCHAR (attr)));
84+ if (u_property) {
85+ // swallow previous exception
86+ PyErr_Clear ();
87+ return ue_py_convert_property (u_property, self->data );
88+ }
89+ }
90+ }
91+ return ret;
92+ }
93+
94+ static int ue_PyUScriptStruct_setattro (ue_PyUScriptStruct *self, PyObject *attr_name, PyObject *value) {
95+ // first of all check for UProperty
96+ if (PyUnicodeOrString_Check (attr_name)) {
97+ char *attr = PyUnicode_AsUTF8 (attr_name);
98+ // first check for property
99+ UProperty *u_property = self->u_struct ->FindPropertyByName (FName (UTF8_TO_TCHAR (attr)));
100+ if (u_property) {
101+ if (ue_py_convert_pyobject (value, u_property, self->data )) {
102+ return 0 ;
103+ }
104+ PyErr_SetString (PyExc_ValueError, " invalid value for UProperty" );
105+ return -1 ;
106+ }
107+ }
108+ return PyObject_GenericSetAttr ((PyObject *)self, attr_name, value);
109+ }
110+
31111static PyTypeObject ue_PyUScriptStructType = {
32112 PyVarObject_HEAD_INIT (NULL , 0 )
33113 " unreal_engine.UScriptStruct" , /* tp_name */
@@ -45,8 +125,8 @@ static PyTypeObject ue_PyUScriptStructType = {
45125 0 , /* tp_hash */
46126 0 , /* tp_call */
47127 (reprfunc)ue_PyUScriptStruct_str, /* tp_str */
48- 0 , /* tp_getattro */
49- 0 , /* tp_setattro */
128+ (getattrofunc)ue_PyUScriptStruct_getattro, /* tp_getattro */
129+ (setattrofunc)ue_PyUScriptStruct_setattro, /* tp_setattro */
50130 0 , /* tp_as_buffer */
51131 Py_TPFLAGS_DEFAULT, /* tp_flags */
52132 " Unreal Engine Editor UScriptStruct" , /* tp_doc */
@@ -61,10 +141,37 @@ static PyTypeObject ue_PyUScriptStructType = {
61141 0 ,
62142};
63143
144+ static int ue_py_uscriptstruct_init (ue_PyUScriptStruct *self, PyObject *args, PyObject *kwargs) {
145+ PyObject *py_struct;
146+ if (!PyArg_ParseTuple (args, " O" , &py_struct))
147+ return -1 ;
148+
149+ if (!ue_is_pyuobject (py_struct)) {
150+ PyErr_SetString (PyExc_Exception, " argument is not a UScriptStruct" );
151+ return -1 ;
152+ }
153+
154+ ue_PyUObject *py_u_obj = (ue_PyUObject *)py_struct;
155+ if (!py_u_obj->ue_object ->IsA <UScriptStruct>()) {
156+ PyErr_SetString (PyExc_Exception, " argument is not a UScriptStruct" );
157+ return -1 ;
158+ }
159+
160+ self->u_struct = (UScriptStruct *)py_u_obj->ue_object ;
161+ self->data = (uint8*)FMemory::Malloc (self->u_struct ->GetStructureSize ());
162+ self->u_struct ->InitializeStruct (self->data );
163+ #if WITH_EDITOR
164+ self->u_struct ->InitializeDefaultValue (self->data );
165+ #endif
166+ return 0 ;
167+ }
168+
64169
65170void ue_python_init_uscriptstruct (PyObject *ue_module) {
66171 ue_PyUScriptStructType.tp_new = PyType_GenericNew;
67172
173+ ue_PyUScriptStructType.tp_init = (initproc)ue_py_uscriptstruct_init;
174+
68175 if (PyType_Ready (&ue_PyUScriptStructType) < 0 )
69176 return ;
70177
@@ -75,7 +182,9 @@ void ue_python_init_uscriptstruct(PyObject *ue_module) {
75182PyObject *py_ue_new_uscriptstruct (UScriptStruct *u_struct, uint8 *data) {
76183 ue_PyUScriptStruct *ret = (ue_PyUScriptStruct *)PyObject_New (ue_PyUScriptStruct, &ue_PyUScriptStructType);
77184 ret->u_struct = u_struct;
78- ret->data = data;
185+ uint8 *struct_data = (uint8*)FMemory::Malloc (u_struct->GetStructureSize ());
186+ FMemory::Memcpy (struct_data, data, u_struct->GetStructureSize ());
187+ ret->data = struct_data;
79188 return (PyObject *)ret;
80189}
81190
0 commit comments