Repository navigation
Expand file tree
/
Copy pathUnrealEnginePython.h
More file actions
151 lines (113 loc) · 3.47 KB
/
Copy pathUnrealEnginePython.h
File metadata and controls
151 lines (113 loc) · 3.47 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
// Copyright 20Tab S.r.l.
#pragma once
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wdelete-non-virtual-dtor"
#endif
//#define UEPY_MEMORY_DEBUG 1
//#define UEPY_THREADING 1
#if defined(UNREAL_ENGINE_PYTHON_ON_MAC)
#include <Headers/Python.h>
#include <Headers/structmember.h>
#elif defined(UNREAL_ENGINE_PYTHON_ON_LINUX)
#include <Python.h>
#include <structmember.h>
#else
#include <include/Python.h>
#include <include/structmember.h>
#endif
#include "CoreMinimal.h"
#include "ModuleManager.h"
#include "Styling/SlateStyle.h"
#include "UObject/ScriptMacros.h"
#include "Runtime/Launch/Resources/Version.h"
// We need to make sure reference structs do not mistaken for callable
#define PyCalllable_Check_Extended(value) PyCallable_Check(value) && py_ue_is_uscriptstruct(value) == nullptr
typedef struct
{
PyObject_HEAD
/* Type-specific fields go here. */
UObject *ue_object;
// reference to proxy class (can be null)
PyObject *py_proxy;
// the __dict__
PyObject *py_dict;
// if true RemoveFromRoot will be called at object destruction time
int auto_rooted;
} ue_PyUObject;
#if ENGINE_MINOR_VERSION >= 18
#define FStringAssetReference FSoftObjectPath
#endif
#define ue_py_check(py_u) if (!FUnrealEnginePythonHouseKeeper::Get()->IsValidPyUObject(py_u))\
return PyErr_Format(PyExc_Exception, "PyUObject is in invalid state")
#define ue_py_check_int(py_u) if (!FUnrealEnginePythonHouseKeeper::Get()->IsValidPyUObject(py_u))\
{\
PyErr_SetString(PyExc_Exception, "PyUObject is in invalid state");\
return -1;\
}
#if PY_MAJOR_VERSION < 3
char *PyUnicode_AsUTF8(PyObject *py_str);
int PyGILState_Check();
#endif
bool PyUnicodeOrString_Check(PyObject *py_obj);
#define Py_RETURN_UOBJECT(py_uobj) ue_PyUObject *ret = ue_get_python_uobject_inc(py_uobj);\
if (!ret)\
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");\
return (PyObject *)ret;
#define Py_RETURN_UOBJECT_NOINC(py_uobj) ue_PyUObject *ret = ue_get_python_uobject(py_uobj);\
if (!ret)\
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");\
return (PyObject *)ret;
#if ENGINE_MINOR_VERSION < 16
template<class CPPSTRUCT>
struct TStructOpsTypeTraitsBase2 : TStructOpsTypeTraitsBase
{
};
#endif
DECLARE_LOG_CATEGORY_EXTERN(LogPython, Log, All);
class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
{
public:
bool PythonGILAcquire();
void PythonGILRelease();
virtual void StartupModule() override;
virtual void ShutdownModule() override;
void RunString(char *);
void RunStringSandboxed(char *);
void RunFile(char *);
void RunFileSandboxed(char *, void(*callback)(void *arg), void *arg);
void UESetupPythonInterpreter(bool);
FString ScriptsPath;
FString ZipPath;
FString AdditionalModulesPath;
bool BrutalFinalize;
// pep8ize a string using various strategy (currently only autopep8 is supported)
FString Pep8ize(FString Code);
private:
void *ue_python_gil;
// used by console
void *main_dict;
void *local_dict;
void *main_module;
TSharedPtr<FSlateStyleSet> StyleSet;
};
struct FScopePythonGIL
{
FScopePythonGIL()
{
#if defined(UEPY_THREADING)
UnrealEnginePythonModule = FModuleManager::LoadModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
safeForRelease = UnrealEnginePythonModule.PythonGILAcquire();
#endif
}
~FScopePythonGIL()
{
#if defined(UEPY_THREADING)
if (safeForRelease)
{
UnrealEnginePythonModule.PythonGILRelease();
}
#endif
}
FUnrealEnginePythonModule UnrealEnginePythonModule;
bool safeForRelease;
};