-
Notifications
You must be signed in to change notification settings - Fork 765
Expand file tree
/
Copy pathUnrealEnginePython.cpp
More file actions
713 lines (609 loc) · 19.9 KB
/
UnrealEnginePython.cpp
File metadata and controls
713 lines (609 loc) · 19.9 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
// Copyright 20Tab S.r.l.
#include "UnrealEnginePython.h"
#include "UEPyModule.h"
#include "PythonBlueprintFunctionLibrary.h"
#include "HAL/IConsoleManager.h"
#include "HAL/PlatformFilemanager.h"
#if ENGINE_MINOR_VERSION < 13
#include "ClassIconFinder.h"
#endif
#include "Styling/SlateStyleRegistry.h"
#if WITH_EDITOR
#include "Interfaces/IPluginManager.h"
#endif
#if ENGINE_MINOR_VERSION >= 18
#define PROJECT_CONTENT_DIR FPaths::ProjectContentDir()
#else
#define PROJECT_CONTENT_DIR FPaths::GameContentDir()
#endif
#if PLATFORM_MAC
#include "Runtime/Core/Public/Mac/CocoaThread.h"
#endif
void unreal_engine_init_py_module();
void init_unreal_engine_builtin();
#if PLATFORM_LINUX
const char *ue4_module_options = "linux_global_symbols";
#endif
#include "Runtime/Core/Public/Misc/CommandLine.h"
#include "Runtime/Core/Public/Misc/ConfigCacheIni.h"
#include "Runtime/Core/Public/GenericPlatform/GenericPlatformFile.h"
#include "Runtime/Core/Public/HAL/FileManagerGeneric.h"
#if PLATFORM_ANDROID
#include "Android/AndroidJNI.h"
#include "Android/AndroidApplication.h"
#endif
const char *UEPyUnicode_AsUTF8(PyObject *py_str)
{
#if PY_MAJOR_VERSION < 3
if (PyUnicode_Check(py_str))
{
PyObject *unicode = PyUnicode_AsUTF8String(py_str);
if (unicode)
{
return PyString_AsString(unicode);
}
// just a hack to avoid crashes
return (char *)"<invalid_string>";
}
return (const char *)PyString_AsString(py_str);
#elif PY_MINOR_VERSION < 7
return (const char *)PyUnicode_AsUTF8(py_str);
#else
return PyUnicode_AsUTF8(py_str);
#endif
}
#if PY_MAJOR_VERSION < 3
int PyGILState_Check()
{
PyThreadState * tstate = _PyThreadState_Current;
return tstate && (tstate == PyGILState_GetThisThreadState());
}
#endif
bool PyUnicodeOrString_Check(PyObject *py_obj)
{
if (PyUnicode_Check(py_obj))
{
return true;
}
#if PY_MAJOR_VERSION < 3
else if (PyString_Check(py_obj))
{
return true;
}
#endif
return false;
}
#define LOCTEXT_NAMESPACE "FUnrealEnginePythonModule"
void FUnrealEnginePythonModule::UESetupPythonInterpreter(bool verbose)
{
const TCHAR* CommandLine = FCommandLine::GetOriginal();
const SIZE_T CommandLineSize = FCString::Strlen(CommandLine) + 1;
TCHAR* CommandLineCopy = new TCHAR[CommandLineSize];
FCString::Strcpy(CommandLineCopy, CommandLineSize, CommandLine);
const TCHAR* ParsedCmdLine = CommandLineCopy;
TArray<FString> Args;
for (;;)
{
FString Arg = FParse::Token(ParsedCmdLine, 0);
if (Arg.Len() <= 0)
break;
Args.Add(Arg);
}
#if PY_MAJOR_VERSION >= 3
wchar_t **argv = (wchar_t **)FMemory::Malloc(sizeof(wchar_t *) * (Args.Num() + 1));
#else
char **argv = (char **)FMemory::Malloc(sizeof(char *) * (Args.Num() + 1));
#endif
argv[Args.Num()] = nullptr;
for (int32 i = 0; i < Args.Num(); i++)
{
#if PY_MAJOR_VERSION >= 3
argv[i] = (wchar_t *)(*Args[i]);
#else
argv[i] = TCHAR_TO_UTF8(*Args[i]);
#endif
}
PySys_SetArgv(Args.Num(), argv);
unreal_engine_init_py_module();
PyObject *py_sys = PyImport_ImportModule("sys");
PyObject *py_sys_dict = PyModule_GetDict(py_sys);
PyObject *py_path = PyDict_GetItemString(py_sys_dict, "path");
char *zip_path = TCHAR_TO_UTF8(*ZipPath);
PyObject *py_zip_path = PyUnicode_FromString(zip_path);
PyList_Insert(py_path, 0, py_zip_path);
int i = 0;
for (FString ScriptsPath : ScriptsPaths)
{
char *scripts_path = TCHAR_TO_UTF8(*ScriptsPath);
PyObject *py_scripts_path = PyUnicode_FromString(scripts_path);
PyList_Insert(py_path, i++, py_scripts_path);
if (verbose)
{
UE_LOG(LogPython, Log, TEXT("Python Scripts search path: %s"), UTF8_TO_TCHAR(scripts_path));
}
}
char *additional_modules_path = TCHAR_TO_UTF8(*AdditionalModulesPath);
PyObject *py_additional_modules_path = PyUnicode_FromString(additional_modules_path);
PyList_Insert(py_path, 0, py_additional_modules_path);
if (verbose)
{
UE_LOG(LogPython, Log, TEXT("Python VM initialized: %s"), UTF8_TO_TCHAR(Py_GetVersion()));
}
}
static void setup_stdout_stderr()
{
// Redirecting stdout
char const* code = "import sys\n"
"import unreal_engine\n"
"class UnrealEngineOutput:\n"
" def __init__(self, logger):\n"
" self.logger = logger\n"
" def write(self, buf):\n"
" self.logger(buf)\n"
" def flush(self):\n"
" return\n"
" def isatty(self):\n"
" return False\n"
"sys.stdout = UnrealEngineOutput(unreal_engine.log)\n"
"sys.stderr = UnrealEngineOutput(unreal_engine.log_error)\n"
"\n"
"class event:\n"
" def __init__(self, event_signature):\n"
" self.event_signature = event_signature\n"
" def __call__(self, f):\n"
" f.ue_event = self.event_signature\n"
" return f\n"
"\n"
"unreal_engine.event = event";
PyRun_SimpleString(code);
}
namespace
{
static void consoleExecScript(const TArray<FString>& Args)
{
if (Args.Num() != 1)
{
UE_LOG(LogPython, Warning, TEXT("Usage: 'py.exec <scriptname>'."));
UE_LOG(LogPython, Warning, TEXT(" scriptname: Name of script, must reside in Scripts folder. Ex: myscript.py"));
}
else
{
UPythonBlueprintFunctionLibrary::ExecutePythonScript(Args[0]);
}
}
static void consoleExecString(const TArray<FString>& Args)
{
if (Args.Num() == 0)
{
UE_LOG(LogPython, Warning, TEXT("Usage: 'py.cmd <command string>'."));
UE_LOG(LogPython, Warning, TEXT(" scriptname: Name of script, must reside in Scripts folder. Ex: myscript.py"));
}
else
{
FString cmdString;
for (const FString& argStr : Args)
{
cmdString += argStr.TrimQuotes() + '\n';
}
UPythonBlueprintFunctionLibrary::ExecutePythonString(cmdString);
}
}
}
FAutoConsoleCommand ExecPythonScriptCommand(
TEXT("py.exec"),
*NSLOCTEXT("UnrealEnginePython", "CommandText_Exec", "Execute python script").ToString(),
FConsoleCommandWithArgsDelegate::CreateStatic(consoleExecScript));
FAutoConsoleCommand ExecPythonStringCommand(
TEXT("py.cmd"),
*NSLOCTEXT("UnrealEnginePython", "CommandText_Cmd", "Execute python string").ToString(),
FConsoleCommandWithArgsDelegate::CreateStatic(consoleExecString));
void FUnrealEnginePythonModule::StartupModule()
{
BrutalFinalize = false;
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
FString PythonHome;
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("Home"), PythonHome, GEngineIni))
{
#if PY_MAJOR_VERSION >= 3
wchar_t *home = (wchar_t *)*PythonHome;
#else
char *home = TCHAR_TO_UTF8(*PythonHome);
#endif
FPlatformMisc::SetEnvironmentVar(TEXT("PYTHONHOME"), *PythonHome);
Py_SetPythonHome(home);
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeHome"), PythonHome, GEngineIni))
{
PythonHome = FPaths::Combine(*PROJECT_CONTENT_DIR, *PythonHome);
FPaths::NormalizeFilename(PythonHome);
PythonHome = FPaths::ConvertRelativePathToFull(PythonHome);
#if PY_MAJOR_VERSION >= 3
wchar_t *home = (wchar_t *)*PythonHome;
#else
char *home = TCHAR_TO_UTF8(*PythonHome);
#endif
Py_SetPythonHome(home);
}
TArray<FString> ImportModules;
FString IniValue;
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ProgramName"), IniValue, GEngineIni))
{
#if PY_MAJOR_VERSION >= 3
wchar_t *program_name = (wchar_t *)*IniValue;
#else
char *program_name = TCHAR_TO_UTF8(*IniValue);
#endif
Py_SetProgramName(program_name);
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeProgramName"), IniValue, GEngineIni))
{
IniValue = FPaths::Combine(*PROJECT_CONTENT_DIR, *IniValue);
FPaths::NormalizeFilename(IniValue);
IniValue = FPaths::ConvertRelativePathToFull(IniValue);
#if PY_MAJOR_VERSION >= 3
wchar_t *program_name = (wchar_t *)*IniValue;
#else
char *program_name = TCHAR_TO_UTF8(*IniValue);
#endif
Py_SetProgramName(program_name);
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ScriptsPath"), IniValue, GEngineIni))
{
ScriptsPaths.Add(IniValue);
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeScriptsPath"), IniValue, GEngineIni))
{
ScriptsPaths.Add(FPaths::Combine(*PROJECT_CONTENT_DIR, *IniValue));
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("AdditionalModulesPath"), IniValue, GEngineIni))
{
AdditionalModulesPath = IniValue;
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeAdditionalModulesPath"), IniValue, GEngineIni))
{
AdditionalModulesPath = FPaths::Combine(*PROJECT_CONTENT_DIR, *IniValue);
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ZipPath"), IniValue, GEngineIni))
{
ZipPath = IniValue;
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeZipPath"), IniValue, GEngineIni))
{
ZipPath = FPaths::Combine(*PROJECT_CONTENT_DIR, *IniValue);
}
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ImportModules"), IniValue, GEngineIni))
{
const TCHAR* separators[] = { TEXT(" "), TEXT(";"), TEXT(",") };
IniValue.ParseIntoArray(ImportModules, separators, 3);
}
FString ProjectScriptsPath = FPaths::Combine(*PROJECT_CONTENT_DIR, UTF8_TO_TCHAR("Scripts"));
if (!FPaths::DirectoryExists(ProjectScriptsPath))
{
FPlatformFileManager::Get().GetPlatformFile().CreateDirectory(*ProjectScriptsPath);
}
ScriptsPaths.Add(ProjectScriptsPath);
#if WITH_EDITOR
for (TSharedRef<IPlugin>plugin : IPluginManager::Get().GetEnabledPlugins())
{
FString PluginScriptsPath = FPaths::Combine(plugin->GetContentDir(), UTF8_TO_TCHAR("Scripts"));
if (FPaths::DirectoryExists(PluginScriptsPath))
{
ScriptsPaths.Add(PluginScriptsPath);
}
// allows third parties to include their code in the main plugin directory
if (plugin->GetName() == "UnrealEnginePython")
{
ScriptsPaths.Add(plugin->GetBaseDir());
}
}
#endif
if (ZipPath.IsEmpty())
{
ZipPath = FPaths::Combine(*PROJECT_CONTENT_DIR, UTF8_TO_TCHAR("ue_python.zip"));
}
// To ensure there are no path conflicts, if we have a valid python home at this point,
// we override the current environment entirely with the environment we want to use,
// removing any paths to other python environments we aren't using.
if (PythonHome.Len() > 0)
{
FPlatformMisc::SetEnvironmentVar(TEXT("PYTHONHOME"), *PythonHome);
const int32 MaxPathVarLen = 32768;
FString OrigPathVar = FString::ChrN(MaxPathVarLen, TEXT('\0'));
FPlatformMisc::GetEnvironmentVariable(TEXT("PATH"), OrigPathVar.GetCharArray().GetData(), MaxPathVarLen);
// Get the current path and remove elements with python in them, we don't want any conflicts
const TCHAR* PathDelimiter = FPlatformMisc::GetPathVarDelimiter();
TArray<FString> PathVars;
OrigPathVar.ParseIntoArray(PathVars, PathDelimiter, true);
for (int32 PathRemoveIndex = PathVars.Num() - 1; PathRemoveIndex >= 0; --PathRemoveIndex)
{
if (PathVars[PathRemoveIndex].Contains(TEXT("python"), ESearchCase::IgnoreCase))
{
UE_LOG(LogPython, Verbose, TEXT("Removing other python Path: '%s'"), *PathVars[PathRemoveIndex]);
PathVars.RemoveAt(PathRemoveIndex);
}
}
// Setup our own paths for PYTHONPATH
TArray<FString> OurPythonPaths = {
PythonHome,
FPaths::Combine(PythonHome, TEXT("Lib")),
FPaths::Combine(PythonHome, TEXT("Lib/site-packages")),
};
FString PythonPathVars = FString::Join(OurPythonPaths, PathDelimiter);
FPlatformMisc::SetEnvironmentVar(TEXT("PYTHONPATH"), *PythonPathVars);
// Also add our paths to PATH, just so any searching will find our local python
PathVars.Append(OurPythonPaths);
FString ModifiedPath = FString::Join(PathVars, PathDelimiter);
FPlatformMisc::SetEnvironmentVar(TEXT("PATH"), *ModifiedPath);
}
#if PY_MAJOR_VERSION >= 3
init_unreal_engine_builtin();
#if PLATFORM_ANDROID
extern FString GOBBFilePathBase;
extern FString GFilePathBase;
extern FString GExternalFilePath;
extern FString GPackageName;
extern int32 GAndroidPackageVersion;
FString OBBDir1 = GOBBFilePathBase + FString(TEXT("/Android/obb/") + GPackageName);
FString OBBDir2 = GOBBFilePathBase + FString(TEXT("/obb/") + GPackageName);
FString MainOBBName = FString::Printf(TEXT("main.%d.%s.obb"), GAndroidPackageVersion, *GPackageName);
FString PatchOBBName = FString::Printf(TEXT("patch.%d.%s.obb"), GAndroidPackageVersion, *GPackageName);
FString UnrealEnginePython_OBBPath;
if (FPaths::FileExists(*(OBBDir1 / MainOBBName)))
{
UnrealEnginePython_OBBPath = OBBDir1 / MainOBBName / FApp::GetProjectName() / FString(TEXT("Content/Scripts"));
}
else if (FPaths::FileExists(*(OBBDir2 / MainOBBName)))
{
UnrealEnginePython_OBBPath = OBBDir2 / MainOBBName / FApp::GetProjectName() / FString(TEXT("Content/Scripts"));
}
if (FPaths::FileExists(*(OBBDir1 / PatchOBBName)))
{
UnrealEnginePython_OBBPath = OBBDir1 / PatchOBBName / FApp::GetProjectName() / FString(TEXT("Content/Scripts"));
}
else if (FPaths::FileExists(*(OBBDir2 / PatchOBBName)))
{
UnrealEnginePython_OBBPath = OBBDir1 / PatchOBBName / FApp::GetProjectName() / FString(TEXT("Content/Scripts"));
}
if (!UnrealEnginePython_OBBPath.IsEmpty())
{
ScriptsPaths.Add(UnrealEnginePython_OBBPath);
}
FString FinalPath = GFilePathBase / FString("UE4Game") / FApp::GetProjectName() / FApp::GetProjectName() / FString(TEXT("Content/Scripts"));
ScriptsPaths.Add(FinalPath);
FString BasePythonPath = FinalPath / FString(TEXT("stdlib.zip")) + FString(":") + FinalPath;
if (!UnrealEnginePython_OBBPath.IsEmpty())
{
BasePythonPath += FString(":") + UnrealEnginePython_OBBPath;
}
UE_LOG(LogPython, Warning, TEXT("Setting Android Base Path to %s"), *BasePythonPath);
Py_SetPath(Py_DecodeLocale(TCHAR_TO_UTF8(*BasePythonPath), NULL));
#elif PLATFORM_IOS
FString IOSContentPath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*IFileManager::Get().GetFilenameOnDisk(*FPaths::ConvertRelativePathToFull(PROJECT_CONTENT_DIR)));
FString PyScriptsSearchPath = IOSContentPath / FString(TEXT("lib")) + FString(":") +
IOSContentPath / FString(TEXT("lib/stdlib.zip")) + FString(":") +
IOSContentPath / FString(TEXT("scripts")); // the name of directory must be lower-case.
Py_SetPath(Py_DecodeLocale(TCHAR_TO_UTF8(*PyScriptsSearchPath), NULL));
UE_LOG(LogPython, Log, TEXT("Setting IOS Python Scripts Search Path to %s"), *PyScriptsSearchPath);
#endif
#endif
#if PLATFORM_IOS || PLATFORM_ANDROID
Py_NoSiteFlag = 1;
Py_NoUserSiteDirectory = 1;
Py_InitializeEx(0);
#else
Py_Initialize();
#endif
PyEval_InitThreads();
#if WITH_EDITOR
StyleSet = MakeShareable(new FSlateStyleSet("UnrealEnginePython"));
StyleSet->SetContentRoot(IPluginManager::Get().FindPlugin("UnrealEnginePython")->GetBaseDir() / "Resources");
StyleSet->Set("ClassThumbnail.PythonScript", new FSlateImageBrush(StyleSet->RootToContentDir("Icon128.png"), FVector2D(128.0f, 128.0f)));
FSlateStyleRegistry::RegisterSlateStyle(*StyleSet.Get());
#if ENGINE_MINOR_VERSION < 13
FClassIconFinder::RegisterIconSource(StyleSet.Get());
#endif
#endif
UESetupPythonInterpreter(true);
main_module = PyImport_AddModule("__main__");
main_dict = PyModule_GetDict((PyObject*)main_module);
local_dict = main_dict;// PyDict_New();
setup_stdout_stderr();
if (PyImport_ImportModule("ue_site"))
{
UE_LOG(LogPython, Log, TEXT("ue_site Python module successfully imported"));
}
else
{
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 6
if (PyErr_ExceptionMatches(PyExc_ModuleNotFoundError))
{
UE_LOG(LogPython, Log, TEXT("ue_site Python module not found"));
PyErr_Clear();
}
else
{
unreal_engine_py_log_error();
}
#else
unreal_engine_py_log_error();
#endif
}
for (FString ImportModule : ImportModules)
{
if (PyImport_ImportModule(TCHAR_TO_UTF8(*ImportModule)))
{
UE_LOG(LogPython, Log, TEXT("%s Python module successfully imported"), *ImportModule);
}
else
{
unreal_engine_py_log_error();
}
}
// release the GIL
PyThreadState *UEPyGlobalState = PyEval_SaveThread();
}
void FUnrealEnginePythonModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
// we call this function before unloading the module.
UE_LOG(LogPython, Log, TEXT("Goodbye Python"));
if (!BrutalFinalize)
{
PyGILState_Ensure();
Py_Finalize();
}
}
void FUnrealEnginePythonModule::RunString(char *str)
{
FScopePythonGIL gil;
PyObject *eval_ret = PyRun_String(str, Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
if (!eval_ret)
{
if (PyErr_ExceptionMatches(PyExc_SystemExit))
{
PyErr_Clear();
return;
}
unreal_engine_py_log_error();
return;
}
Py_DECREF(eval_ret);
}
#if PLATFORM_MAC
void FUnrealEnginePythonModule::RunStringInMainThread(char *str)
{
MainThreadCall(^{
RunString(str);
});
}
void FUnrealEnginePythonModule::RunFileInMainThread(char *filename)
{
MainThreadCall(^{
RunFile(filename);
});
}
#endif
FString FUnrealEnginePythonModule::Pep8ize(FString Code)
{
FScopePythonGIL gil;
PyObject *pep8izer_module = PyImport_ImportModule("autopep8");
if (!pep8izer_module)
{
unreal_engine_py_log_error();
UE_LOG(LogPython, Error, TEXT("unable to load autopep8 module, please install it"));
// return the original string to avoid losing data
return Code;
}
PyObject *pep8izer_func = PyObject_GetAttrString(pep8izer_module, (char*)"fix_code");
if (!pep8izer_func)
{
unreal_engine_py_log_error();
UE_LOG(LogPython, Error, TEXT("unable to get autopep8.fix_code function"));
// return the original string to avoid losing data
return Code;
}
PyObject *ret = PyObject_CallFunction(pep8izer_func, (char *)"s", TCHAR_TO_UTF8(*Code));
if (!ret)
{
unreal_engine_py_log_error();
// return the original string to avoid losing data
return Code;
}
if (!PyUnicodeOrString_Check(ret))
{
UE_LOG(LogPython, Error, TEXT("returned value is not a string"));
// return the original string to avoid losing data
return Code;
}
const char *pep8ized = UEPyUnicode_AsUTF8(ret);
FString NewCode = FString(pep8ized);
Py_DECREF(ret);
return NewCode;
}
void FUnrealEnginePythonModule::RunFile(char *filename)
{
FScopePythonGIL gil;
FString full_path = UTF8_TO_TCHAR(filename);
FString original_path = full_path;
bool foundFile = false;
if (!FPaths::FileExists(filename))
{
for (FString ScriptsPath : ScriptsPaths)
{
full_path = FPaths::Combine(*ScriptsPath, original_path);
if (FPaths::FileExists(full_path))
{
foundFile = true;
break;
}
}
}
else
{
foundFile = true;
}
if (!foundFile)
{
UE_LOG(LogPython, Error, TEXT("Unable to find file %s"), UTF8_TO_TCHAR(filename));
return;
}
#if PY_MAJOR_VERSION >= 3
FILE *fd = nullptr;
#if PLATFORM_WINDOWS
if (fopen_s(&fd, TCHAR_TO_UTF8(*full_path), "r") != 0)
{
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
return;
}
#else
fd = fopen(TCHAR_TO_UTF8(*full_path), "r");
if (!fd)
{
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), *full_path);
return;
}
#endif
PyObject *eval_ret = PyRun_File(fd, TCHAR_TO_UTF8(*full_path), Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
fclose(fd);
if (!eval_ret)
{
if (PyErr_ExceptionMatches(PyExc_SystemExit))
{
PyErr_Clear();
return;
}
unreal_engine_py_log_error();
return;
}
Py_DECREF(eval_ret);
#else
// damn, this is horrible, but it is the only way i found to avoid the CRT error :(
FString command = FString::Printf(TEXT("execfile(\"%s\")"), *full_path);
PyObject *eval_ret = PyRun_String(TCHAR_TO_UTF8(*command), Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
if (!eval_ret)
{
if (PyErr_ExceptionMatches(PyExc_SystemExit))
{
PyErr_Clear();
return;
}
unreal_engine_py_log_error();
return;
}
#endif
}
void ue_py_register_magic_module(char *name, PyObject *(*func)())
{
PyObject *py_sys = PyImport_ImportModule("sys");
PyObject *py_sys_dict = PyModule_GetDict(py_sys);
PyObject *py_sys_modules = PyDict_GetItemString(py_sys_dict, "modules");
PyObject *u_module = func();
Py_INCREF(u_module);
PyDict_SetItemString(py_sys_modules, name, u_module);
}
PyObject *ue_py_register_module(const char *name)
{
return PyImport_AddModule(name);
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FUnrealEnginePythonModule, UnrealEnginePython)