Skip to content

Commit 39ab81e

Browse files
author
Roberto De Ioris
committed
added UDataTable api
1 parent 95857d1 commit 39ab81e

File tree

4 files changed

+308
-45
lines changed

4 files changed

+308
-45
lines changed

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "UObject/UEPyCapture.h"
3737
#include "UObject/UEPyLandscape.h"
3838
#include "UObject/UEPyUserDefinedStruct.h"
39+
#include "UObject/UEPyDataTable.h"
3940

4041

4142
#include "UEPyAssetUserData.h"
@@ -535,6 +536,14 @@ static PyMethodDef ue_PyUObject_methods[] = {
535536
{ "struct_remove_variable", (PyCFunction)py_ue_struct_remove_variable, METH_VARARGS, "" },
536537
{ "struct_move_variable_up", (PyCFunction)py_ue_struct_move_variable_up, METH_VARARGS, "" },
537538
{ "struct_move_variable_down", (PyCFunction)py_ue_struct_move_variable_down, METH_VARARGS, "" },
539+
540+
{ "data_table_add_row", (PyCFunction)py_ue_data_table_add_row, METH_VARARGS, "" },
541+
{ "data_table_remove_row", (PyCFunction)py_ue_data_table_remove_row, METH_VARARGS, "" },
542+
{ "data_table_rename_row", (PyCFunction)py_ue_data_table_rename_row, METH_VARARGS, "" },
543+
{ "data_table_as_dict", (PyCFunction)py_ue_data_table_as_dict, METH_VARARGS, "" },
544+
{ "data_table_as_json", (PyCFunction)py_ue_data_table_as_json, METH_VARARGS, "" },
545+
{ "data_table_find_row", (PyCFunction)py_ue_data_table_find_row, METH_VARARGS, "" },
546+
{ "data_table_get_all_rows", (PyCFunction)py_ue_data_table_get_all_rows, METH_VARARGS, "" },
538547
#endif
539548

540549
{ "is_rooted", (PyCFunction)py_ue_is_rooted, METH_VARARGS, "" },
@@ -2771,14 +2780,14 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer)
27712780
casted_prop->SetPropertyValue_InContainer(buffer, ue_obj->ue_object);
27722781
return true;
27732782
}
2774-
else if (auto casted_prop = Cast<USoftClassProperty>(prop))
2783+
else if (auto casted_prop_soft_class = Cast<USoftClassProperty>(prop))
27752784
{
2776-
casted_prop->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object));
2785+
casted_prop_soft_class->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object));
27772786
return true;
27782787
}
2779-
else if (auto casted_prop = Cast<USoftObjectProperty>(prop))
2788+
else if (auto casted_prop_soft_object = Cast<USoftObjectProperty>(prop))
27802789
{
2781-
casted_prop->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object));
2790+
casted_prop_soft_object->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object));
27822791
return true;
27832792
}
27842793

@@ -2796,12 +2805,12 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer)
27962805
casted_prop->SetObjectPropertyValue_InContainer(buffer, ue_obj->ue_object);
27972806
return true;
27982807
}
2799-
else if (auto casted_prop = Cast<USoftObjectProperty>(prop))
2808+
else if (auto casted_prop_soft_object = Cast<USoftObjectProperty>(prop))
28002809

28012810
{
2802-
if (!ue_obj->ue_object->IsA(casted_prop->PropertyClass))
2811+
if (!ue_obj->ue_object->IsA(casted_prop_soft_object->PropertyClass))
28032812
return false;
2804-
casted_prop->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object));
2813+
casted_prop_soft_object->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object));
28052814
return true;
28062815
}
28072816

Source/UnrealEnginePython/Private/UEPyUScriptStruct.cpp

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#include "UnrealEnginePythonPrivatePCH.h"
44

55

6-
static PyObject *py_ue_uscriptstruct_get_field(ue_PyUScriptStruct *self, PyObject * args) {
6+
static PyObject *py_ue_uscriptstruct_get_field(ue_PyUScriptStruct *self, PyObject * args)
7+
{
78
char *name;
8-
if (!PyArg_ParseTuple(args, "s:get_field", &name)) {
9+
if (!PyArg_ParseTuple(args, "s:get_field", &name))
10+
{
911
return NULL;
1012
}
1113

@@ -16,10 +18,12 @@ static PyObject *py_ue_uscriptstruct_get_field(ue_PyUScriptStruct *self, PyObjec
1618
return ue_py_convert_property(u_property, self->data);
1719
}
1820

19-
static PyObject *py_ue_uscriptstruct_set_field(ue_PyUScriptStruct *self, PyObject * args) {
21+
static PyObject *py_ue_uscriptstruct_set_field(ue_PyUScriptStruct *self, PyObject * args)
22+
{
2023
char *name;
2124
PyObject *value;
22-
if (!PyArg_ParseTuple(args, "sO:set_field", &name, &value)) {
25+
if (!PyArg_ParseTuple(args, "sO:set_field", &name, &value))
26+
{
2327
return NULL;
2428
}
2529

@@ -28,7 +32,8 @@ static PyObject *py_ue_uscriptstruct_set_field(ue_PyUScriptStruct *self, PyObjec
2832
return PyErr_Format(PyExc_Exception, "unable to find property %s", name);
2933

3034

31-
if (!ue_py_convert_pyobject(value, u_property, self->data)) {
35+
if (!ue_py_convert_pyobject(value, u_property, self->data))
36+
{
3237
return PyErr_Format(PyExc_Exception, "unable to set property %s", name);
3338
}
3439

@@ -37,7 +42,8 @@ static PyObject *py_ue_uscriptstruct_set_field(ue_PyUScriptStruct *self, PyObjec
3742

3843
}
3944

40-
static PyObject *py_ue_uscriptstruct_fields(ue_PyUScriptStruct *self, PyObject * args) {
45+
static PyObject *py_ue_uscriptstruct_fields(ue_PyUScriptStruct *self, PyObject * args)
46+
{
4147
PyObject *ret = PyList_New(0);
4248

4349
for (TFieldIterator<UProperty> PropIt(self->u_struct); PropIt; ++PropIt)
@@ -51,29 +57,52 @@ static PyObject *py_ue_uscriptstruct_fields(ue_PyUScriptStruct *self, PyObject *
5157
return ret;
5258
}
5359

54-
static PyObject *py_ue_uscriptstruct_get_struct(ue_PyUScriptStruct *self, PyObject * args) {
60+
static PyObject *py_ue_uscriptstruct_get_struct(ue_PyUScriptStruct *self, PyObject * args)
61+
{
5562
ue_PyUObject *ret = ue_get_python_wrapper(self->u_struct);
5663
if (!ret)
5764
return PyErr_Format(PyExc_Exception, "PyUObject is in invalid state");
5865
Py_INCREF(ret);
5966
return (PyObject *)ret;
6067
}
6168

62-
static PyObject *py_ue_uscriptstruct_clone(ue_PyUScriptStruct *self, PyObject * args) {
69+
static PyObject *py_ue_uscriptstruct_clone(ue_PyUScriptStruct *self, PyObject * args)
70+
{
6371
return py_ue_new_uscriptstruct(self->u_struct, self->data);
6472
}
6573

66-
PyObject *py_ue_uscriptstruct_as_dict(ue_PyUScriptStruct * self, PyObject * args) {
74+
PyObject *py_ue_uscriptstruct_as_dict(ue_PyUScriptStruct * self, PyObject * args)
75+
{
76+
77+
PyObject *py_bool = nullptr;
78+
if (!PyArg_ParseTuple(args, "|O:as_dict", &py_bool))
79+
{
80+
return nullptr;
81+
}
82+
83+
static const FName DisplayNameKey(TEXT("DisplayName"));
6784

6885
PyObject *py_struct_dict = PyDict_New();
6986
TFieldIterator<UProperty> SArgs(self->u_struct);
70-
for (; SArgs; ++SArgs) {
87+
for (; SArgs; ++SArgs)
88+
{
7189
PyObject *struct_value = ue_py_convert_property(*SArgs, self->data);
72-
if (!struct_value) {
90+
if (!struct_value)
91+
{
7392
Py_DECREF(py_struct_dict);
7493
return NULL;
7594
}
76-
PyDict_SetItemString(py_struct_dict, TCHAR_TO_UTF8(*SArgs->GetName()), struct_value);
95+
FString prop_name = SArgs->GetName();
96+
if (py_bool && PyObject_IsTrue(py_bool))
97+
{
98+
if (SArgs->HasMetaData(DisplayNameKey))
99+
{
100+
FString display_name = SArgs->GetMetaData(DisplayNameKey);
101+
if (display_name.Len() > 0)
102+
prop_name = display_name;
103+
}
104+
}
105+
PyDict_SetItemString(py_struct_dict, TCHAR_TO_UTF8(*prop_name), struct_value);
77106
}
78107
return py_struct_dict;
79108
}
@@ -96,36 +125,49 @@ static PyObject *ue_PyUScriptStruct_str(ue_PyUScriptStruct *self)
96125
TCHAR_TO_UTF8(*self->u_struct->GetName()), self->u_struct->GetStructureSize());
97126
}
98127

99-
static UProperty *get_field_from_name(UScriptStruct *u_struct, char *name) {
128+
static UProperty *get_field_from_name(UScriptStruct *u_struct, char *name)
129+
{
100130
FString attr = UTF8_TO_TCHAR(name);
101131
UProperty *u_property = u_struct->FindPropertyByName(FName(*attr));
102132
if (u_property)
103133
return u_property;
104-
// if the property is not found, attempt to search for name_XXXX
105-
attr += FString("_");
134+
135+
static const FName DisplayNameKey(TEXT("DisplayName"));
136+
137+
// if the property is not found, attempt to search for DisplayName
106138
for (TFieldIterator<UProperty> prop(u_struct); prop; ++prop)
107139
{
108140
UProperty *property = *prop;
109-
if (property->GetName().StartsWith(attr)) {
110-
return property;
141+
if (property->HasMetaData(DisplayNameKey))
142+
{
143+
FString display_name = property->GetMetaData(DisplayNameKey);
144+
if (display_name.Len() > 0 && attr.Equals(display_name))
145+
{
146+
return property;
147+
}
111148
}
112149
}
113150

114151
return nullptr;
115152
}
116153

117-
UProperty *ue_struct_get_field_from_name(UScriptStruct *u_struct, char *name) {
154+
UProperty *ue_struct_get_field_from_name(UScriptStruct *u_struct, char *name)
155+
{
118156
return get_field_from_name(u_struct, name);
119157
}
120158

121-
static PyObject *ue_PyUScriptStruct_getattro(ue_PyUScriptStruct *self, PyObject *attr_name) {
159+
static PyObject *ue_PyUScriptStruct_getattro(ue_PyUScriptStruct *self, PyObject *attr_name)
160+
{
122161
PyObject *ret = PyObject_GenericGetAttr((PyObject *)self, attr_name);
123-
if (!ret) {
124-
if (PyUnicodeOrString_Check(attr_name)) {
162+
if (!ret)
163+
{
164+
if (PyUnicodeOrString_Check(attr_name))
165+
{
125166
char *attr = PyUnicode_AsUTF8(attr_name);
126167
// first check for property
127168
UProperty *u_property = get_field_from_name(self->u_struct, attr);
128-
if (u_property) {
169+
if (u_property)
170+
{
129171
// swallow previous exception
130172
PyErr_Clear();
131173
return ue_py_convert_property(u_property, self->data);
@@ -135,14 +177,18 @@ static PyObject *ue_PyUScriptStruct_getattro(ue_PyUScriptStruct *self, PyObject
135177
return ret;
136178
}
137179

138-
static int ue_PyUScriptStruct_setattro(ue_PyUScriptStruct *self, PyObject *attr_name, PyObject *value) {
180+
static int ue_PyUScriptStruct_setattro(ue_PyUScriptStruct *self, PyObject *attr_name, PyObject *value)
181+
{
139182
// first of all check for UProperty
140-
if (PyUnicodeOrString_Check(attr_name)) {
183+
if (PyUnicodeOrString_Check(attr_name))
184+
{
141185
char *attr = PyUnicode_AsUTF8(attr_name);
142186
// first check for property
143187
UProperty *u_property = get_field_from_name(self->u_struct, attr);
144-
if (u_property) {
145-
if (ue_py_convert_pyobject(value, u_property, self->data)) {
188+
if (u_property)
189+
{
190+
if (ue_py_convert_pyobject(value, u_property, self->data))
191+
{
146192
return 0;
147193
}
148194
PyErr_SetString(PyExc_ValueError, "invalid value for UProperty");
@@ -153,7 +199,8 @@ static int ue_PyUScriptStruct_setattro(ue_PyUScriptStruct *self, PyObject *attr_
153199
}
154200

155201
// destructor
156-
static void ue_PyUScriptStruct_dealloc(ue_PyUScriptStruct *self) {
202+
static void ue_PyUScriptStruct_dealloc(ue_PyUScriptStruct *self)
203+
{
157204
#if defined(UEPY_MEMORY_DEBUG)
158205
UE_LOG(LogPython, Warning, TEXT("Destroying ue_PyUScriptStruct %p with size %d"), self, self->u_struct->GetStructureSize());
159206
#endif
@@ -194,18 +241,21 @@ static PyTypeObject ue_PyUScriptStructType = {
194241
0,
195242
};
196243

197-
static int ue_py_uscriptstruct_init(ue_PyUScriptStruct *self, PyObject *args, PyObject *kwargs) {
244+
static int ue_py_uscriptstruct_init(ue_PyUScriptStruct *self, PyObject *args, PyObject *kwargs)
245+
{
198246
PyObject *py_struct;
199247
if (!PyArg_ParseTuple(args, "O", &py_struct))
200248
return -1;
201249

202-
if (!ue_is_pyuobject(py_struct)) {
250+
if (!ue_is_pyuobject(py_struct))
251+
{
203252
PyErr_SetString(PyExc_Exception, "argument is not a UScriptStruct");
204253
return -1;
205254
}
206255

207256
ue_PyUObject *py_u_obj = (ue_PyUObject *)py_struct;
208-
if (!py_u_obj->ue_object->IsA<UScriptStruct>()) {
257+
if (!py_u_obj->ue_object->IsA<UScriptStruct>())
258+
{
209259
PyErr_SetString(PyExc_Exception, "argument is not a UScriptStruct");
210260
return -1;
211261
}
@@ -219,24 +269,29 @@ static int ue_py_uscriptstruct_init(ue_PyUScriptStruct *self, PyObject *args, Py
219269
return 0;
220270
}
221271

222-
static PyObject *ue_py_uscriptstruct_richcompare(ue_PyUScriptStruct *u_struct1, PyObject *py_obj, int op) {
272+
static PyObject *ue_py_uscriptstruct_richcompare(ue_PyUScriptStruct *u_struct1, PyObject *py_obj, int op)
273+
{
223274
ue_PyUScriptStruct *u_struct2 = py_ue_is_uscriptstruct(py_obj);
224-
if (!u_struct2 || (op != Py_EQ && op != Py_NE)) {
275+
if (!u_struct2 || (op != Py_EQ && op != Py_NE))
276+
{
225277
return PyErr_Format(PyExc_NotImplementedError, "can only compare with another UScriptStruct");
226278
}
227279

228280
bool equals = (u_struct1->u_struct == u_struct2->u_struct && !memcmp(u_struct1->data, u_struct2->data, u_struct1->u_struct->GetStructureSize()));
229281

230-
if (op == Py_EQ) {
231-
if (equals) {
282+
if (op == Py_EQ)
283+
{
284+
if (equals)
285+
{
232286
Py_INCREF(Py_True);
233287
return Py_True;
234288
}
235289
Py_INCREF(Py_False);
236290
return Py_False;
237291
}
238292

239-
if (equals) {
293+
if (equals)
294+
{
240295
Py_INCREF(Py_False);
241296
return Py_False;
242297
}
@@ -245,7 +300,8 @@ static PyObject *ue_py_uscriptstruct_richcompare(ue_PyUScriptStruct *u_struct1,
245300
}
246301

247302

248-
void ue_python_init_uscriptstruct(PyObject *ue_module) {
303+
void ue_python_init_uscriptstruct(PyObject *ue_module)
304+
{
249305
ue_PyUScriptStructType.tp_new = PyType_GenericNew;
250306

251307
ue_PyUScriptStructType.tp_richcompare = (richcmpfunc)ue_py_uscriptstruct_richcompare;
@@ -259,7 +315,8 @@ void ue_python_init_uscriptstruct(PyObject *ue_module) {
259315
PyModule_AddObject(ue_module, "UScriptStruct", (PyObject *)&ue_PyUScriptStructType);
260316
}
261317

262-
PyObject *py_ue_new_uscriptstruct(UScriptStruct *u_struct, uint8 *data) {
318+
PyObject *py_ue_new_uscriptstruct(UScriptStruct *u_struct, uint8 *data)
319+
{
263320
ue_PyUScriptStruct *ret = (ue_PyUScriptStruct *)PyObject_New(ue_PyUScriptStruct, &ue_PyUScriptStructType);
264321
ret->u_struct = u_struct;
265322
uint8 *struct_data = (uint8*)FMemory::Malloc(u_struct->GetStructureSize());
@@ -269,7 +326,8 @@ PyObject *py_ue_new_uscriptstruct(UScriptStruct *u_struct, uint8 *data) {
269326
return (PyObject *)ret;
270327
}
271328

272-
ue_PyUScriptStruct *py_ue_is_uscriptstruct(PyObject *obj) {
329+
ue_PyUScriptStruct *py_ue_is_uscriptstruct(PyObject *obj)
330+
{
273331
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PyUScriptStructType))
274332
return nullptr;
275333
return (ue_PyUScriptStruct *)obj;

0 commit comments

Comments
 (0)