Skip to content

Commit 94bf527

Browse files
author
rdeioris
committed
added ScriptsPath, RelativeScriptsPath, ZipPath and RelativeZipPath as .ini options
1 parent 6d530c1 commit 94bf527

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

Source/PythonEditor/Private/PythonProject.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
UPythonProject::UPythonProject(const FObjectInitializer& ObjectInitializer)
77
: Super(ObjectInitializer)
88
{
9-
Path = FPaths::GameContentDir() / TEXT("Scripts");
9+
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
10+
Path = PythonModule.ScriptsPath;
1011
if (!FPaths::DirectoryExists(Path)) {
1112
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
1213
PlatformFile.CreateDirectory(*Path);

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ bool FUnrealEnginePythonModule::PythonGILAcquire() {
6161
return true;
6262
}
6363

64-
static void UESetupPythonInterpreter(bool verbose) {
64+
void FUnrealEnginePythonModule::UESetupPythonInterpreter(bool verbose) {
6565

6666
#if PY_MAJOR_VERSION >= 3
6767
wchar_t *argv[] = { UTF8_TO_TCHAR("UnrealEngine"), NULL };
@@ -77,11 +77,11 @@ static void UESetupPythonInterpreter(bool verbose) {
7777

7878
PyObject *py_path = PyDict_GetItemString(py_sys_dict, "path");
7979

80-
char *zip_path = TCHAR_TO_UTF8(*FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("ue_python.zip")));
80+
char *zip_path = TCHAR_TO_UTF8(*ZipPath);
8181
PyObject *py_zip_path = PyUnicode_FromString(zip_path);
8282
PyList_Insert(py_path, 0, py_zip_path);
8383

84-
char *scripts_path = TCHAR_TO_UTF8(*FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("Scripts")));
84+
char *scripts_path = TCHAR_TO_UTF8(*ScriptsPath);
8585
PyObject *py_scripts_path = PyUnicode_FromString(scripts_path);
8686
PyList_Insert(py_path, 0, py_scripts_path);
8787

@@ -110,17 +110,41 @@ static void setup_stdout_stderr() {
110110
void FUnrealEnginePythonModule::StartupModule()
111111
{
112112
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
113-
FString PyHome;
114-
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("Home"), PyHome, GEngineIni)) {
113+
FString IniValue;
114+
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("Home"), IniValue, GEngineIni)) {
115115
#if PY_MAJOR_VERSION >= 3
116-
wchar_t *home = (wchar_t *)*PyHome;
116+
wchar_t *home = (wchar_t *)*IniValue;
117117
#else
118-
char *home = TCHAR_TO_UTF8(*PyHome);
118+
char *home = TCHAR_TO_UTF8(*IniValue);
119119
#endif
120120

121121
Py_SetPythonHome(home);
122122
}
123123

124+
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ScriptsPath"), IniValue, GEngineIni)) {
125+
ScriptsPath = IniValue;
126+
}
127+
128+
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeScriptsPath"), IniValue, GEngineIni)) {
129+
ScriptsPath = FPaths::Combine(FPaths::GameContentDir(), IniValue);
130+
}
131+
132+
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("ZipPath"), IniValue, GEngineIni)) {
133+
ZipPath = IniValue;
134+
}
135+
136+
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeZipPath"), IniValue, GEngineIni)) {
137+
ZipPath = FPaths::Combine(FPaths::GameContentDir(), IniValue);
138+
}
139+
140+
if (ScriptsPath.IsEmpty()) {
141+
ScriptsPath = FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("Scripts"));
142+
}
143+
144+
if (ZipPath.IsEmpty()) {
145+
ZipPath = FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("ue_python.zip"));
146+
}
147+
124148
Py_Initialize();
125149

126150
PyEval_InitThreads();
@@ -211,14 +235,14 @@ void FUnrealEnginePythonModule::RunStringSandboxed(char *str) {
211235

212236
Py_EndInterpreter(py_new_state);
213237
PyThreadState_Swap(_main);
214-
}
238+
}
215239

216240
void FUnrealEnginePythonModule::RunFile(char *filename) {
217241
FScopePythonGIL gil;
218242
char *full_path = filename;
219243
if (!FPaths::FileExists(filename))
220244
{
221-
full_path = TCHAR_TO_UTF8(*FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("Scripts"), *FString("/"), UTF8_TO_TCHAR(filename)));
245+
full_path = TCHAR_TO_UTF8(*FPaths::Combine(ScriptsPath, UTF8_TO_TCHAR(filename)));
222246
}
223247
#if PY_MAJOR_VERSION >= 3
224248
FILE *fd = nullptr;
@@ -261,7 +285,7 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename) {
261285
char *full_path = filename;
262286
if (!FPaths::FileExists(filename))
263287
{
264-
full_path = TCHAR_TO_UTF8(*FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("Scripts"), *FString("/"), UTF8_TO_TCHAR(filename)));
288+
full_path = TCHAR_TO_UTF8(*FPaths::Combine(ScriptsPath, UTF8_TO_TCHAR(filename)));
265289
}
266290

267291
PyThreadState *_main = PyThreadState_Get();

Source/UnrealEnginePython/Public/UnrealEnginePython.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
2424
void RunFile(char *);
2525
void RunFileSandboxed(char *);
2626

27+
void UESetupPythonInterpreter(bool);
28+
29+
FString ScriptsPath;
30+
FString ZipPath;
31+
2732
private:
2833
void *ue_python_gil;
2934
// used by console

0 commit comments

Comments
 (0)