| 10 | } |
| 11 | |
| 12 | int32 UPyCommandlet::Main(const FString& CommandLine) |
| 13 | { |
| 14 | TArray<FString> Tokens, Switches; |
| 15 | TMap<FString, FString> Params; |
| 16 | ParseCommandLine(*CommandLine, Tokens, Switches, Params); |
| 17 | |
| 18 | FString Filepath = Tokens[0]; |
| 19 | if (!FPaths::FileExists(*Filepath)) |
| 20 | { |
| 21 | UE_LOG(LogPython, Error, TEXT("Python file could not be found: %s"), *Filepath); |
| 22 | return -1; |
| 23 | } |
| 24 | |
| 25 | FString RegexString = FString::Printf(TEXT("(?<=%s).*"), *(Filepath.Replace(TEXT("\\"), TEXT("\\\\")))); |
| 26 | const FRegexPattern myPattern(RegexString); |
| 27 | FRegexMatcher myMatcher(myPattern, *CommandLine); |
| 28 | myMatcher.FindNext(); |
| 29 | FString PyCommandLine = myMatcher.GetCaptureGroup(0).Trim().TrimTrailing(); |
| 30 | |
| 31 | TArray<FString> PyArgv; |
| 32 | PyArgv.Add(FString()); |
| 33 | bool escaped = false; |
| 34 | for (int i = 0; i < PyCommandLine.Len(); i++) |
| 35 | { |
| 36 | if(PyCommandLine[i] == ' ') |
| 37 | { |
| 38 | PyArgv.Add(FString()); |
| 39 | continue; |
| 40 | } |
| 41 | else if(PyCommandLine[i] == '\"' && !escaped) |
| 42 | { |
| 43 | i++; |
| 44 | while(i < PyCommandLine.Len() && !(PyCommandLine[i] == '"')) |
| 45 | { |
| 46 | PyArgv[PyArgv.Num() - 1].AppendChar(PyCommandLine[i]); |
| 47 | i++; |
| 48 | if (i == PyCommandLine.Len()) |
| 49 | { |
| 50 | PyArgv[PyArgv.Num() - 1].InsertAt(0, "\""); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | if (PyCommandLine[i] == '\\') |
| 57 | escaped = true; |
| 58 | else |
| 59 | escaped = false; |
| 60 | PyArgv[PyArgv.Num() - 1].AppendChar(PyCommandLine[i]); |
| 61 | } |
| 62 | } |
| 63 | PyArgv.Insert(Filepath, 0); |
| 64 | |
| 65 | #if PY_MAJOR_VERSION >= 3 |
| 66 | wchar_t **argv = (wchar_t **)malloc(PyArgv.Num() * sizeof(void*)); |
| 67 | #else |
| 68 | char **argv = (char **)malloc(PyArgv.Num() * sizeof(void*)); |
| 69 | #endif |