Repository navigation
Expand file tree
/
Copy pathPythonDelegate.cpp
More file actions
132 lines (115 loc) · 2.89 KB
/
Copy pathPythonDelegate.cpp
File metadata and controls
132 lines (115 loc) · 2.89 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
#include "PythonDelegate.h"
#include "UEPyModule.h"
#include "UEPyCallable.h"
UPythonDelegate::UPythonDelegate()
{
py_callable = nullptr;
signature_set = false;
}
void UPythonDelegate::SetPyCallable(PyObject *callable)
{
// do not acquire the gil here as we set the callable in python call themselves
py_callable = callable;
Py_INCREF(py_callable);
}
void UPythonDelegate::SetSignature(UFunction *original_signature)
{
signature = original_signature;
signature_set = true;
}
void UPythonDelegate::ProcessEvent(UFunction *function, void *Parms)
{
if (!py_callable)
return;
FScopePythonGIL gil;
PyObject *py_args = nullptr;
if (signature_set)
{
py_args = PyTuple_New(signature->NumParms);
Py_ssize_t argn = 0;
#if ENGINE_MINOR_VERSION >= 25
TFieldIterator<FProperty> PArgs(signature);
#else
TFieldIterator<UProperty> PArgs(signature);
#endif
for (; PArgs && argn < signature->NumParms && ((PArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm); ++PArgs)
{
#if ENGINE_MINOR_VERSION >= 25
FProperty *prop = *PArgs;
#else
UProperty *prop = *PArgs;
#endif
PyObject *arg = ue_py_convert_property(prop, (uint8 *)Parms, 0);
if (!arg)
{
unreal_engine_py_log_error();
Py_DECREF(py_args);
return;
}
PyTuple_SetItem(py_args, argn, arg);
argn++;
}
}
PyObject *ret = PyObject_CallObject(py_callable, py_args);
Py_XDECREF(py_args);
if (!ret)
{
unreal_engine_py_log_error();
return;
}
// currently useless as events do not return a value
/*
if (signature_set) {
#if ENGINE_MINOR_VERSION >= 25
FProperty *return_property = signature->GetReturnProperty();
#else
UProperty *return_property = signature->GetReturnProperty();
#endif
if (return_property && signature->ReturnValueOffset != MAX_uint16) {
if (!ue_py_convert_pyobject(ret, return_property, (uint8 *)Parms)) {
UE_LOG(LogPython, Error, TEXT("Invalid return value type for delegate"));
}
}
}
*/
Py_DECREF(ret);
}
void UPythonDelegate::PyFakeCallable()
{
}
void UPythonDelegate::PyInputHandler()
{
FScopePythonGIL gil;
PyObject *ret = PyObject_CallObject(py_callable, NULL);
if (!ret)
{
unreal_engine_py_log_error();
return;
}
Py_DECREF(ret);
}
void UPythonDelegate::PyInputAxisHandler(float value)
{
FScopePythonGIL gil;
PyObject *ret = PyObject_CallFunction(py_callable, (char *)"f", value);
if (!ret)
{
unreal_engine_py_log_error();
return;
}
Py_DECREF(ret);
}
bool UPythonDelegate::UsesPyCallable(PyObject *other)
{
ue_PyCallable *other_callable = (ue_PyCallable*)other;
ue_PyCallable *this_callable = (ue_PyCallable*)py_callable;
return other_callable->u_function == this_callable->u_function && other_callable->u_target == this_callable->u_target;
}
UPythonDelegate::~UPythonDelegate()
{
FScopePythonGIL gil;
Py_XDECREF(py_callable);
#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Warning, TEXT("PythonDelegate %p callable XDECREF'ed"), this);
#endif
}