Repository navigation
Expand file tree
/
Copy pathUEPySubclassing.cpp
More file actions
802 lines (738 loc) · 26.4 KB
/
Copy pathUEPySubclassing.cpp
File metadata and controls
802 lines (738 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#include "UEPyModule.h"
#include "PythonClass.h"
#include "UObject/UEPyObject.h"
//#include "Runtime/CoreUObject/Public/UObject/Field.h"
// hack for avoiding loops in class constructors (thanks to the Unreal.js project for the idea)
UClass *ue_py_class_constructor_placeholder = nullptr;
static void UEPyClassConstructor(UClass *u_class, const FObjectInitializer &ObjectInitializer)
{
if (UPythonClass *u_py_class_casted = Cast<UPythonClass>(u_class))
{
ue_py_class_constructor_placeholder = u_class;
}
u_class->ClassConstructor(ObjectInitializer);
ue_py_class_constructor_placeholder = nullptr;
}
int unreal_engine_py_init(ue_PyUObject *self, PyObject *args, PyObject *kwds)
{
EXTRA_UE_LOG(LogPython, Warning, TEXT("unreal_engine_py_init (uobject) - subclassing args %d"), PyTuple_Size(args));
// is it subclassing ?
if (PyTuple_Size(args) == 3)
{
// TODO make it smarter on error checking
UE_LOG(LogPython, Warning, TEXT("%s"), UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(PyObject_Str(PyTuple_GetItem(args, 0)))));
UE_LOG(LogPython, Warning, TEXT("%s"), UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(PyObject_Str(PyTuple_GetItem(args, 1)))));
UE_LOG(LogPython, Warning, TEXT("%s"), UTF8_TO_TCHAR(UEPyUnicode_AsUTF8(PyObject_Str(PyTuple_GetItem(args, 2)))));
PyObject *parents = PyTuple_GetItem(args, 1);
ue_PyUObject *parent = (ue_PyUObject *)PyTuple_GetItem(parents, 0);
PyObject *class_attributes = PyTuple_GetItem(args, 2);
PyObject *class_name = PyDict_GetItemString(class_attributes, (char *)"__qualname__");
const char *name = UEPyUnicode_AsUTF8(class_name);
// check if parent is a uclass
UClass *new_class = unreal_engine_new_uclass((char *)name, (UClass *)parent->ue_object);
if (!new_class)
return -1;
// map the class to the python object
self->ue_object = new_class;
self->py_proxy = nullptr;
self->auto_rooted = 0;
self->py_dict = PyDict_New();
FUnrealEnginePythonHouseKeeper::Get()->RegisterPyUObject(new_class, self);
PyObject *py_additional_properties = PyDict_New();
PyObject *class_attributes_keys = PyObject_GetIter(class_attributes);
for (;;)
{
PyObject *key = PyIter_Next(class_attributes_keys);
if (!key)
{
if (PyErr_Occurred())
return -1;
break;
}
if (!PyUnicodeOrString_Check(key))
continue;
const char *class_key = UEPyUnicode_AsUTF8(key);
PyObject *value = PyDict_GetItem(class_attributes, key);
if (strlen(class_key) > 2 && class_key[0] == '_' && class_key[1] == '_')
{
continue;
}
//EXTRA_UE_LOG(LogPython, Warning, TEXT("object init attr value %p"), value);
bool prop_added = false;
//EXTRA_UE_LOG(LogPython, Warning, TEXT("offset tp_alloc %d %x"), offsetof(PyTypeObject, tp_alloc), offsetof(PyTypeObject, tp_alloc));
#if ENGINE_MINOR_VERSION >= 25
if (FProperty *f_property = new_class->FindPropertyByName(FName(UTF8_TO_TCHAR(class_key))))
{
UE_LOG(LogPython, Warning, TEXT("Found FProperty %s"), UTF8_TO_TCHAR(class_key));
PyDict_SetItem(py_additional_properties, key, value);
prop_added = true;
}
#else
if (UProperty *u_property = new_class->FindPropertyByName(FName(UTF8_TO_TCHAR(class_key))))
{
UE_LOG(LogPython, Warning, TEXT("Found UProperty %s"), UTF8_TO_TCHAR(class_key));
PyDict_SetItem(py_additional_properties, key, value);
prop_added = true;
}
#endif
#if ENGINE_MINOR_VERSION >= 25
// add simple property
// this is confusing because it appears when properties were uobjects UEP allowed both general
// uobject classes and UProperty classes as arguments - creating a UOBjectProperty/UScriptStructProperty for a non-UProperty class
// this now has to be separate code
else if (ue_is_pyfproperty(value))
{
// So I dont think an FProperty is valid as an argument here - we need a class object
// which for FProperties is now an FFieldClass
// we could of course take the class of the FProperty and add a new property
//ue_PyFProperty *py_obj = (ue_PyFProperty *)value;
UE_LOG(LogPython, Error, TEXT("fproperty not valid as property argument %s"), UTF8_TO_TCHAR(class_key));
return -1;
}
else if (ue_is_pyffieldclass(value))
{
//ue_PyFFieldClass *py_obj = (ue_PyFFieldClass *)value;
//UE_LOG(LogPython, Error, TEXT("simple property NEEDS FIXING %s"), UTF8_TO_TCHAR(class_key));
//return -1;
// so I think we just add the property here now - in old terms we essentially know
// this is a UClass of a UProperty subclass - ie an FFieldClass
// think we should disallow a plain FFieldClass - we need a subclass
ue_PyFFieldClass *py_obj = (ue_PyFFieldClass *)value;
//if (FFieldClass::IsChildOf(py_obj->ue_ffieldclass))
//{
if (!py_ue_add_property(self, Py_BuildValue("(Os)", value, class_key)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
//}
}
// allow for adding UObject/UScriptStruct properties by simply specifying the UObject/UScriptStruct class here
// this appears to be what the UProperty code did
else if (ue_is_pyuobject(value))
{
ue_PyUObject *py_obj = (ue_PyUObject *)value;
if (py_obj->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj->ue_object;
// So apparently this wont work - UProperty subclasses seems to be mapped to FFieldClass automagically
//if (p_class->IsChildOf<UProperty>())
//{
// we may be able to leave this in as it appears we still have access to UProperty subclasses
// for the moment error out here
//if (!py_ue_add_property(self, Py_BuildValue("(Os)", value, class_key)))
//{
// unreal_engine_py_log_error();
// return -1;
//}
//prop_added = true;
//UE_LOG(LogPython, Error, TEXT("uproperty as property implementation disabled "));
//return -1;
//}
//else
{
if (!py_ue_add_property(self, Py_BuildValue("(OsO)", (PyObject *)ue_get_python_ffieldclass(FObjectProperty::StaticClass()), class_key, value)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
else if (py_obj->ue_object->IsA<UScriptStruct>())
{
if (!py_ue_add_property(self, Py_BuildValue("(OsO)", (PyObject *)ue_get_python_ffieldclass(FStructProperty::StaticClass()), class_key, value)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
#else
// add simple property
else if (ue_is_pyuobject(value))
{
ue_PyUObject *py_obj = (ue_PyUObject *)value;
if (py_obj->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj->ue_object;
if (p_class->IsChildOf<UProperty>())
{
if (!py_ue_add_property(self, Py_BuildValue("(Os)", value, class_key)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
else
{
if (!py_ue_add_property(self, Py_BuildValue("(OsO)", (PyObject *)ue_get_python_uobject(UObjectProperty::StaticClass()), class_key, value)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
else if (py_obj->ue_object->IsA<UScriptStruct>())
{
if (!py_ue_add_property(self, Py_BuildValue("(OsO)", (PyObject *)ue_get_python_uobject(UStructProperty::StaticClass()), class_key, value)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
#endif
// add array property
else if (PyList_Check(value))
{
if (PyList_Size(value) == 1)
{
PyObject *first_item = PyList_GetItem(value, 0);
#if ENGINE_MINOR_VERSION >= 25
// pigs - we need same code as above - allow for direct FFieldClass plus UObject/UScriptStruct classes
if (ue_is_pyfproperty(first_item))
{
// So I dont think an FProperty is valid as an argument here - we need a class object
// which for FProperties is now an FFieldClass
// we could of course take the class of the FProperty and add a new property
//ue_PyFProperty *py_obj = (ue_PyFProperty *)first_item;
UE_LOG(LogPython, Error, TEXT("fproperty not valid as property argument %s"), UTF8_TO_TCHAR(class_key));
return -1;
}
else if (ue_is_pyffieldclass(first_item))
{
//ue_PyFFieldClass *py_obj = (ue_PyFFieldClass *)value;
//UE_LOG(LogPython, Error, TEXT("simple property NEEDS FIXING %s"), UTF8_TO_TCHAR(class_key));
//return -1;
// so I think we just add the property here now - in old terms we essentially know
// this is a UClass of a UProperty subclass - ie an FFieldClass
// think we should disallow a plain FFieldClass - we need a subclass
ue_PyFFieldClass *py_obj = (ue_PyFFieldClass *)first_item;
//if (py_obj->ue_ffieldclass->IsChildOf(FFieldClass::StaticClass()))
//{
if (!py_ue_add_property(self, Py_BuildValue("(Os)", value, class_key)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
//}
}
else if (ue_is_pyuobject(first_item))
{
ue_PyUObject *py_obj = (ue_PyUObject *)first_item;
if (py_obj->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj->ue_object;
// So apparently this wont work - UProperty subclasses seems to be mapped to FFieldClass automagically
//if (p_class->IsChildOf<UProperty>())
//{
// we may be able to leave this in as it appears we still have access to UProperty subclasses
// for the moment error out here
//if (!py_ue_add_property(self, Py_BuildValue("(Os)", value, class_key)))
//{
// unreal_engine_py_log_error();
// return -1;
//}
//prop_added = true;
//UE_LOG(LogPython, Error, TEXT("uproperty as property array implementation disabled "));
//return -1;
//}
//else
{
if (!py_ue_add_property(self, Py_BuildValue("([O]sO)", (PyObject *)ue_get_python_ffieldclass(FObjectProperty::StaticClass()), class_key, first_item)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
else if (py_obj->ue_object->IsA<UScriptStruct>())
{
if (!py_ue_add_property(self, Py_BuildValue("([O]sO)", (PyObject *)ue_get_python_ffieldclass(FStructProperty::StaticClass()), class_key, first_item)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
#else
if (ue_is_pyuobject(first_item))
{
ue_PyUObject *py_obj = (ue_PyUObject *)first_item;
if (py_obj->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj->ue_object;
if (p_class->IsChildOf<UProperty>())
{
if (!py_ue_add_property(self, Py_BuildValue("(Os)", value, class_key)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
else
{
if (!py_ue_add_property(self, Py_BuildValue("([O]sO)", (PyObject *)ue_get_python_uobject(UObjectProperty::StaticClass()), class_key, first_item)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
else if (py_obj->ue_object->IsA<UScriptStruct>())
{
if (!py_ue_add_property(self, Py_BuildValue("([O]sO)", (PyObject *)ue_get_python_uobject(UStructProperty::StaticClass()), class_key, first_item)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
#endif
}
}
#if ENGINE_MINOR_VERSION >= 15
else if (PyDict_Check(value))
{
if (PyDict_Size(value) == 1)
{
PyObject *py_key = nullptr;
PyObject *py_value = nullptr;
Py_ssize_t pos = 0;
PyDict_Next(value, &pos, &py_key, &py_value);
#if ENGINE_MINOR_VERSION >= 25
if (ue_is_pyfproperty(py_key) || ue_is_pyfproperty(py_value))
{
// So I dont think an FProperty is valid as an argument here - we need a class object
// which for FProperties is now an FFieldClass
// we could of course take the class of the FProperty and add a new property
//ue_PyFProperty *py_obj = (ue_PyFProperty *)first_item;
UE_LOG(LogPython, Error, TEXT("fproperty not valid as property argument %s"), UTF8_TO_TCHAR(class_key));
return -1;
}
else
{
PyObject *first_item = nullptr;
PyObject *second_item = nullptr;
if (ue_is_pyffieldclass(py_key))
{
ue_PyFFieldClass *py_obj = (ue_PyFFieldClass *)py_key;
//if (py_obj->ue_ffieldclass->IsChildOf(FFieldClass::StaticClass()))
//{
first_item = py_key;
//}
}
else if (ue_is_pyuobject(py_key))
{
ue_PyUObject *py_obj = (ue_PyUObject *)py_key;
if (py_obj->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj->ue_object;
// So apparently this wont work - UProperty subclasses seems to be mapped to FFieldClass automagically
//if (p_class->IsChildOf<UProperty>())
//{
// we may be able to leave this in as it appears we still have access to UProperty subclasses
// for the moment error out here
//first_item = py_key;
//UE_LOG(LogPython, Error, TEXT("uproperty as property map key implementation disabled "));
//return -1;
//}
//else
{
first_item = (PyObject *)ue_get_python_ffieldclass(FObjectProperty::StaticClass());
}
}
else if (py_obj->ue_object->IsA<UScriptStruct>())
{
first_item = (PyObject *)ue_get_python_ffieldclass(FStructProperty::StaticClass());
}
}
if (ue_is_pyffieldclass(py_value))
{
ue_PyFFieldClass *py_obj2 = (ue_PyFFieldClass *)py_key;
//if (py_obj2->ue_ffieldclass->IsChildOf(FFieldClass::StaticClass()))
//{
second_item = py_value;
//}
}
else if (ue_is_pyuobject(py_value))
{
ue_PyUObject *py_obj2 = (ue_PyUObject *)py_value;
if (py_obj2->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj2->ue_object;
// So apparently this wont work - UProperty subclasses seems to be mapped to FFieldClass automagically
//if (p_class->IsChildOf<UProperty>())
//{
// we may be able to leave this in as it appears we still have access to UProperty subclasses
// for the moment error out here
//second_item = py_value;
//UE_LOG(LogPython, Error, TEXT("uproperty as property map value implementation disabled "));
//return -1;
//}
//else
{
second_item = (PyObject *)ue_get_python_ffieldclass(FObjectProperty::StaticClass());
}
}
else if (py_obj2->ue_object->IsA<UScriptStruct>())
{
second_item = (PyObject *)ue_get_python_ffieldclass(FStructProperty::StaticClass());
}
}
if (first_item && second_item)
{
if (!py_ue_add_property(self, Py_BuildValue("([OO]sOO)", first_item, second_item, class_key, py_key, py_value)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
}
#else
if (ue_is_pyuobject(py_key) && ue_is_pyuobject(py_value))
{
PyObject *first_item = nullptr;
PyObject *second_item = nullptr;
ue_PyUObject *py_obj = (ue_PyUObject *)py_key;
if (py_obj->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj->ue_object;
if (p_class->IsChildOf<UProperty>())
{
first_item = py_key;
}
else
{
first_item = (PyObject *)ue_get_python_uobject(UObjectProperty::StaticClass());
}
}
else if (py_obj->ue_object->IsA<UScriptStruct>())
{
first_item = (PyObject *)ue_get_python_uobject(UStructProperty::StaticClass());
}
ue_PyUObject *py_obj2 = (ue_PyUObject *)py_value;
if (py_obj2->ue_object->IsA<UClass>())
{
UClass *p_class = (UClass *)py_obj2->ue_object;
if (p_class->IsChildOf<UProperty>())
{
second_item = py_value;
}
else
{
second_item = (PyObject *)ue_get_python_uobject(UObjectProperty::StaticClass());
}
}
else if (py_obj2->ue_object->IsA<UScriptStruct>())
{
second_item = (PyObject *)ue_get_python_uobject(UStructProperty::StaticClass());
}
if (!py_ue_add_property(self, Py_BuildValue("([OO]sOO)", first_item, second_item, class_key, py_key, py_value)))
{
unreal_engine_py_log_error();
return -1;
}
prop_added = true;
}
#endif
}
}
#endif
// function ?
else if (PyCallable_Check(value) && class_key[0] >= 'A' && class_key[0] <= 'Z')
{
uint32 func_flags = FUNC_Native | FUNC_BlueprintCallable | FUNC_Public;
PyObject *is_event = PyObject_GetAttrString(value, (char *)"event");
if (is_event && PyObject_IsTrue(is_event))
{
func_flags |= FUNC_Event | FUNC_BlueprintEvent;
}
else if (!is_event)
PyErr_Clear();
PyObject *is_multicast = PyObject_GetAttrString(value, (char *)"multicast");
if (is_multicast && PyObject_IsTrue(is_multicast))
{
func_flags |= FUNC_NetMulticast;
}
else if (!is_multicast)
PyErr_Clear();
PyObject *is_server = PyObject_GetAttrString(value, (char *)"server");
if (is_server && PyObject_IsTrue(is_server))
{
func_flags |= FUNC_NetServer;
}
else if (!is_server)
PyErr_Clear();
PyObject *is_client = PyObject_GetAttrString(value, (char *)"client");
if (is_client && PyObject_IsTrue(is_client))
{
func_flags |= FUNC_NetClient;
}
else if (!is_client)
PyErr_Clear();
PyObject *is_reliable = PyObject_GetAttrString(value, (char *)"reliable");
if (is_reliable && PyObject_IsTrue(is_reliable))
{
func_flags |= FUNC_NetReliable;
}
else if (!is_reliable)
PyErr_Clear();
PyObject *is_pure = PyObject_GetAttrString(value, (char *)"pure");
if (is_pure && PyObject_IsTrue(is_pure))
{
func_flags |= FUNC_BlueprintPure;
}
else if (!is_pure)
PyErr_Clear();
PyObject *is_static = PyObject_GetAttrString(value, (char *)"static");
if (is_static && PyObject_IsTrue(is_static))
{
func_flags |= FUNC_Static;
}
else if (!is_static)
PyErr_Clear();
PyObject *override_name = PyObject_GetAttrString(value, (char *)"override");
if (override_name && PyUnicodeOrString_Check(override_name))
{
class_key = UEPyUnicode_AsUTF8(override_name);
}
else if (override_name && PyUnicodeOrString_Check(override_name))
{
class_key = UEPyUnicode_AsUTF8(override_name);
}
else if (!override_name)
PyErr_Clear();
if (!unreal_engine_add_function(new_class, (char *)class_key, value, func_flags))
{
UE_LOG(LogPython, Error, TEXT("unable to add function %s"), UTF8_TO_TCHAR(class_key));
return -1;
}
prop_added = true;
}
if (!prop_added)
{
UE_LOG(LogPython, Warning, TEXT("Adding %s as attr"), UTF8_TO_TCHAR(class_key));
PyObject_SetAttr((PyObject *)self, key, value);
}
}
if (PyDict_Size(py_additional_properties) > 0)
{
PyObject_SetAttrString((PyObject *)self, (char*)"__additional_uproperties__", py_additional_properties);
}
UPythonClass *new_u_py_class = (UPythonClass *)new_class;
// TODO: check if we can use this to decref the ue_PyUbject mapped to the class
new_u_py_class->py_uobject = self;
new_u_py_class->ClassConstructor = [](const FObjectInitializer &ObjectInitializer)
{
FScopePythonGIL gil;
UClass *u_class = ue_py_class_constructor_placeholder ? ue_py_class_constructor_placeholder : ObjectInitializer.GetClass();
ue_py_class_constructor_placeholder = nullptr;
UEPyClassConstructor(u_class->GetSuperClass(), ObjectInitializer);
if (UPythonClass *u_py_class_casted = Cast<UPythonClass>(u_class))
{
ue_PyUObject *new_self = ue_get_python_uobject(ObjectInitializer.GetObj());
if (!new_self)
{
unreal_engine_py_log_error();
return;
}
// fill __dict__ from class
if (u_py_class_casted->py_uobject && u_py_class_casted->py_uobject->py_dict)
{
PyObject *found_additional_props = PyDict_GetItemString(u_py_class_casted->py_uobject->py_dict, (char *)"__additional_uproperties__");
// manage UProperties (and automatically maps multicast properties)
if (found_additional_props)
{
PyObject *keys = PyDict_Keys(found_additional_props);
Py_ssize_t items_len = PyList_Size(keys);
for (Py_ssize_t i = 0; i < items_len; i++)
{
PyObject *mc_key = PyList_GetItem(keys, i);
PyObject *mc_value = PyDict_GetItem(found_additional_props, mc_key);
const char *mc_name = UEPyUnicode_AsUTF8(mc_key);
#if ENGINE_MINOR_VERSION >= 25
FProperty *f_property = ObjectInitializer.GetObj()->GetClass()->FindPropertyByName(FName(UTF8_TO_TCHAR(mc_name)));
if (f_property)
{
if (auto casted_prop = CastField<FMulticastDelegateProperty>(f_property))
{
#else
UProperty *u_property = ObjectInitializer.GetObj()->GetClass()->FindPropertyByName(FName(UTF8_TO_TCHAR(mc_name)));
if (u_property)
{
if (auto casted_prop = Cast<UMulticastDelegateProperty>(u_property))
{
#endif
#if ENGINE_MINOR_VERSION >= 23
FMulticastScriptDelegate multiscript_delegate = *casted_prop->GetMulticastDelegate(ObjectInitializer.GetObj());
#else
FMulticastScriptDelegate multiscript_delegate = casted_prop->GetPropertyValue_InContainer(ObjectInitializer.GetObj());
#endif
FScriptDelegate script_delegate;
#if ENGINE_MINOR_VERSION >= 25
// can we reuse UPythonDelegate here??
UPythonDelegate *py_delegate = FUnrealEnginePythonHouseKeeper::Get()->NewDelegate(ObjectInitializer.GetObj(), mc_value, casted_prop->SignatureFunction);
#else
UPythonDelegate *py_delegate = FUnrealEnginePythonHouseKeeper::Get()->NewDelegate(ObjectInitializer.GetObj(), mc_value, casted_prop->SignatureFunction);
#endif
// fake UFUNCTION for bypassing checks
script_delegate.BindUFunction(py_delegate, FName("PyFakeCallable"));
// add the new delegate
multiscript_delegate.Add(script_delegate);
// re-assign multicast delegate
#if ENGINE_MINOR_VERSION >= 23
casted_prop->SetMulticastDelegate(ObjectInitializer.GetObj(), multiscript_delegate);
#else
casted_prop->SetPropertyValue_InContainer(ObjectInitializer.GetObj(), multiscript_delegate);
#endif
}
else
{
PyObject_SetAttr((PyObject *)new_self, mc_key, mc_value);
}
}
}
Py_DECREF(keys);
}
else
{
PyErr_Clear();
}
PyObject *keys = PyDict_Keys(u_py_class_casted->py_uobject->py_dict);
Py_ssize_t keys_len = PyList_Size(keys);
for (Py_ssize_t i = 0; i < keys_len; i++)
{
PyObject *key = PyList_GetItem(keys, i);
PyObject *value = PyDict_GetItem(u_py_class_casted->py_uobject->py_dict, key);
if (PyUnicodeOrString_Check(key))
{
const char *key_name = UEPyUnicode_AsUTF8(key);
if (!strcmp(key_name, (char *)"__additional_uproperties__"))
continue;
}
// special case to bound function to method
if (PyFunction_Check(value))
{
PyObject *bound_function = PyObject_CallMethod(value, (char*)"__get__", (char*)"O", (PyObject *)new_self);
if (bound_function)
{
PyObject_SetAttr((PyObject *)new_self, key, bound_function);
Py_DECREF(bound_function);
}
else
{
unreal_engine_py_log_error();
}
}
else
{
PyObject_SetAttr((PyObject *)new_self, key, value);
}
}
Py_DECREF(keys);
}
// call __init__
u_py_class_casted->CallPyConstructor(new_self);
}
};
if (self->py_dict)
{
ue_PyUObject *new_default_self = ue_get_python_uobject(new_u_py_class->ClassDefaultObject);
if (!new_default_self)
{
unreal_engine_py_log_error();
UE_LOG(LogPython, Error, TEXT("unable to set dict on new ClassDefaultObject"));
return -1;
}
PyObject *keys = PyDict_Keys(self->py_dict);
Py_ssize_t keys_len = PyList_Size(keys);
for (Py_ssize_t i = 0; i < keys_len; i++)
{
PyObject *key = PyList_GetItem(keys, i);
PyObject *value = PyDict_GetItem(self->py_dict, key);
// special case to bound function to method
if (PyFunction_Check(value))
{
PyObject *bound_function = PyObject_CallMethod(value, (char*)"__get__", (char*)"O", (PyObject *)new_default_self);
if (bound_function)
{
PyObject_SetAttr((PyObject *)new_default_self, key, bound_function);
Py_DECREF(bound_function);
}
else
{
unreal_engine_py_log_error();
}
}
else
{
PyObject_SetAttr((PyObject *)new_default_self, key, value);
}
}
Py_DECREF(keys);
}
// add default uproperties values
if (py_additional_properties)
{
ue_PyUObject *new_default_self = ue_get_python_uobject(new_u_py_class->ClassDefaultObject);
if (!new_default_self)
{
unreal_engine_py_log_error();
UE_LOG(LogPython, Error, TEXT("unable to set properties on new ClassDefaultObject"));
return -1;
}
PyObject *keys = PyDict_Keys(py_additional_properties);
Py_ssize_t keys_len = PyList_Size(keys);
for (Py_ssize_t i = 0; i < keys_len; i++)
{
PyObject *key = PyList_GetItem(keys, i);
PyObject *value = PyDict_GetItem(py_additional_properties, key);
PyObject_SetAttr((PyObject *)new_default_self, key, value);
}
Py_DECREF(keys);
}
// add custom constructor (__init__)
PyObject *py_init = PyDict_GetItemString(class_attributes, (char *)"__init__");
if (py_init && PyCallable_Check(py_init))
{
// fake initializer
FObjectInitializer initializer(new_u_py_class->ClassDefaultObject, nullptr, false, true);
new_u_py_class->SetPyConstructor(py_init);
ue_PyUObject *new_default_self = ue_get_python_uobject(new_u_py_class->ClassDefaultObject);
if (!new_default_self)
{
unreal_engine_py_log_error();
UE_LOG(LogPython, Error, TEXT("unable to call __init__ on new ClassDefaultObject"));
return -1;
}
new_u_py_class->CallPyConstructor(new_default_self);
}
}
return 0;
}
#if ENGINE_MINOR_VERSION >= 25
int unreal_engine_py_fproperty_init(ue_PyUObject *self, PyObject *args, PyObject *kwds)
{
UE_LOG(LogPython, Warning, TEXT("__init__ called (tp_init) for FProperty"));
return -1;
}
int unreal_engine_py_ffieldclass_init(ue_PyUObject *self, PyObject *args, PyObject *kwds)
{
UE_LOG(LogPython, Warning, TEXT("__init__ called (tp_init) for FFieldClass"));
return -1;
}
#endif