Skip to content

Commit 6f574a9

Browse files
author
Roberto De Ioris
committed
completed UScriptStruct object
1 parent 20c310f commit 6f574a9

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
322322
{ "post_edit_change", (PyCFunction)py_ue_post_edit_change, METH_VARARGS, "" },
323323

324324
#if WITH_EDITOR
325+
{ "save_config", (PyCFunction)py_ue_save_config, METH_VARARGS, "" },
325326
{ "get_actor_label", (PyCFunction)py_ue_get_actor_label, METH_VARARGS, "" },
326327
{ "set_actor_label", (PyCFunction)py_ue_set_actor_label, METH_VARARGS, "" },
327328

@@ -1383,7 +1384,7 @@ PyObject *ue_py_convert_property(UProperty *prop, uint8 *buffer) {
13831384
FLinearColor color = *casted_prop->ContainerPtrToValuePtr<FLinearColor>(buffer);
13841385
return py_ue_new_flinearcolor(color);
13851386
}
1386-
return (PyObject *)py_ue_new_uscriptstruct(casted_struct, casted_prop->ContainerPtrToValuePtr<uint8>(buffer));
1387+
return py_ue_new_uscriptstruct(casted_struct, casted_prop->ContainerPtrToValuePtr<uint8>(buffer));
13871388
}
13881389
return PyErr_Format(PyExc_TypeError, "unsupported UStruct type");
13891390
}
@@ -1415,22 +1416,7 @@ PyObject *ue_py_convert_property(UProperty *prop, uint8 *buffer) {
14151416
FScriptArrayHelper_InContainer array_helper(casted_prop, buffer);
14161417
PyObject *py_list = PyList_New(0);
14171418
UProperty *array_prop = casted_prop->Inner;
1418-
/*if (array_prop->IsA<UStructProperty>()) {
1419-
uint8 *array_buffer = (uint8 *)FMemory::Malloc(array_prop->GetSize());
1420-
array_prop->InitializeValue(array_buffer);
1421-
for (int i = 0; i < array_helper.Num(); i++) {
1422-
array_prop->CopyCompleteValueFromScriptVM(array_buffer, array_helper.GetRawPtr(i));
1423-
PyObject *item = ue_py_convert_property(array_prop, array_buffer);
1424-
//array_prop->DestroyValue(array_buffer);
1425-
if (!item) {
1426-
Py_DECREF(py_list);
1427-
return NULL;
1428-
}
1429-
PyList_Append(py_list, item);
1430-
Py_DECREF(item);
1431-
}
1432-
}
1433-
else {*/
1419+
14341420
for (int i = 0; i < array_helper.Num(); i++) {
14351421
PyObject *item = ue_py_convert_property(array_prop, array_helper.GetRawPtr(i));
14361422
if (!item) {
@@ -1440,7 +1426,7 @@ PyObject *ue_py_convert_property(UProperty *prop, uint8 *buffer) {
14401426
PyList_Append(py_list, item);
14411427
Py_DECREF(item);
14421428
}
1443-
//}
1429+
14441430
return py_list;
14451431
}
14461432

@@ -1522,29 +1508,12 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer) {
15221508
helper.RemoveValues(pylist_len, helper.Num() - pylist_len);
15231509
}
15241510

1525-
/*if (array_prop->IsA<UStructProperty>()) {
1526-
for (int i = 0; i < (int)pylist_len; i++) {
1527-
PyObject *py_item = PyList_GetItem(py_obj, i);
1528-
// ensure py_item is a UScriptStruct
1529-
if (ue_PyUScriptStruct *u_struct = py_ue_is_uscriptstruct(py_item)) {
1530-
array_prop->CopySingleValue(helper.GetRawPtr(i), u_struct->data);
1531-
}
1532-
Py_DECREF(py_item);
1533-
}
1534-
return true;
1535-
}
1536-
else {*/
1537-
15381511
for (int i = 0; i < (int)pylist_len; i++) {
1539-
uint8 *item_buf = helper.GetRawPtr(i);
15401512
PyObject *py_item = PyList_GetItem(py_obj, i);
1541-
if (!ue_py_convert_pyobject(py_item, array_prop, item_buf)) {
1542-
Py_DECREF(py_item);
1513+
if (!ue_py_convert_pyobject(py_item, array_prop, helper.GetRawPtr(i))) {
15431514
return false;
15441515
}
1545-
Py_DECREF(py_item);
15461516
}
1547-
//}
15481517
return true;
15491518
}
15501519

Source/UnrealEnginePython/Private/UEPyObject.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ PyObject *py_ue_get_path_name(ue_PyUObject *self, PyObject * args) {
319319
return PyUnicode_FromString(TCHAR_TO_UTF8(*(self->ue_object->GetPathName())));
320320
}
321321

322+
PyObject *py_ue_save_config(ue_PyUObject *self, PyObject * args) {
323+
324+
ue_py_check(self);
325+
326+
self->ue_object->SaveConfig();
327+
328+
Py_INCREF(Py_None);
329+
return Py_None;
330+
}
331+
322332
PyObject *py_ue_set_property(ue_PyUObject *self, PyObject * args) {
323333

324334
ue_py_check(self);

Source/UnrealEnginePython/Private/UEPyObject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ PyObject *py_ue_is_rooted(ue_PyUObject *, PyObject *);
2121
PyObject *py_ue_add_to_root(ue_PyUObject *, PyObject *);
2222
PyObject *py_ue_remove_from_root(ue_PyUObject *, PyObject *);
2323

24+
PyObject *py_ue_save_config(ue_PyUObject *, PyObject *);
25+
2426
PyObject *py_ue_get_cdo(ue_PyUObject *, PyObject *);
2527
PyObject *py_ue_enum_values(ue_PyUObject *, PyObject *);
2628

Source/UnrealEnginePython/Private/UEPyUScriptStruct.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,31 @@ static PyObject *ue_PyUScriptStruct_str(ue_PyUScriptStruct *self)
7474
TCHAR_TO_UTF8(*self->u_struct->GetName()), self->u_struct->GetStructureSize());
7575
}
7676

77+
static UProperty *get_field_from_name(UScriptStruct *u_struct, char *name) {
78+
FString attr = UTF8_TO_TCHAR(name);
79+
UProperty *u_property = u_struct->FindPropertyByName(FName(*attr));
80+
if (u_property)
81+
return u_property;
82+
// if the property is not found, attempt to search for name_XXXX
83+
attr += FString("_");
84+
for (TFieldIterator<UProperty> prop(u_struct); prop; ++prop)
85+
{
86+
UProperty *property = *prop;
87+
if (property->GetName().StartsWith(attr)) {
88+
return property;
89+
}
90+
}
91+
92+
return nullptr;
93+
}
94+
7795
static PyObject *ue_PyUScriptStruct_getattro(ue_PyUScriptStruct *self, PyObject *attr_name) {
7896
PyObject *ret = PyObject_GenericGetAttr((PyObject *)self, attr_name);
7997
if (!ret) {
8098
if (PyUnicodeOrString_Check(attr_name)) {
8199
char *attr = PyUnicode_AsUTF8(attr_name);
82100
// first check for property
83-
UProperty *u_property = self->u_struct->FindPropertyByName(FName(UTF8_TO_TCHAR(attr)));
101+
UProperty *u_property = get_field_from_name(self->u_struct, attr);
84102
if (u_property) {
85103
// swallow previous exception
86104
PyErr_Clear();
@@ -96,7 +114,7 @@ static int ue_PyUScriptStruct_setattro(ue_PyUScriptStruct *self, PyObject *attr_
96114
if (PyUnicodeOrString_Check(attr_name)) {
97115
char *attr = PyUnicode_AsUTF8(attr_name);
98116
// first check for property
99-
UProperty *u_property = self->u_struct->FindPropertyByName(FName(UTF8_TO_TCHAR(attr)));
117+
UProperty *u_property = get_field_from_name(self->u_struct, attr);
100118
if (u_property) {
101119
if (ue_py_convert_pyobject(value, u_property, self->data)) {
102120
return 0;
@@ -108,12 +126,21 @@ static int ue_PyUScriptStruct_setattro(ue_PyUScriptStruct *self, PyObject *attr_
108126
return PyObject_GenericSetAttr((PyObject *)self, attr_name, value);
109127
}
110128

129+
// destructor
130+
static void ue_PyUScriptStruct_dealloc(ue_PyUScriptStruct *self) {
131+
#if UEPY_MEMORY_DEBUG
132+
UE_LOG(LogPython, Warning, TEXT("Destroying ue_PyUScriptStruct %p with size %d"), self, self->u_struct->GetStructureSize());
133+
#endif
134+
FMemory::Free(self->data);
135+
Py_TYPE(self)->tp_free((PyObject *)self);
136+
}
137+
111138
static PyTypeObject ue_PyUScriptStructType = {
112139
PyVarObject_HEAD_INIT(NULL, 0)
113140
"unreal_engine.UScriptStruct", /* tp_name */
114141
sizeof(ue_PyUScriptStruct), /* tp_basicsize */
115142
0, /* tp_itemsize */
116-
0, /* tp_dealloc */
143+
(destructor)ue_PyUScriptStruct_dealloc, /* tp_dealloc */
117144
0, /* tp_print */
118145
0, /* tp_getattr */
119146
0, /* tp_setattr */

0 commit comments

Comments
 (0)