StrokeSeg 1.0.0
Loading...
Searching...
No Matches
commandLineTools.cpp
1#include "deps.h"
2#include "commandLineTools.h"
3
4
5LPWSTR* extractAndCheck(int& argc, std::filesystem::path& currentExecutableDirPath, std::filesystem::path& pythonExecutablePath)
6{
7 LPWSTR* argvW = nullptr; // Initialize to nullptr
8
9 LPWSTR lpCmdLineW = GetCommandLineW();
10
11 argvW = CommandLineToArgvW(lpCmdLineW, &argc);
12
13 if (argvW)
14 {
15 if (argc >= 1)
16 {
17 // Get the directory of the current executable using wide characters
18 wchar_t currentExecutablePathBuf[MAX_PATH];
19 GetModuleFileNameW(NULL, currentExecutablePathBuf, MAX_PATH);
20
21 currentExecutableDirPath = std::filesystem::path(currentExecutablePathBuf).parent_path(); // The directory path of the current executable
22 pythonExecutablePath = currentExecutableDirPath / L".." / L"Python" / PYTHON_BIN_NAME; // The path to the Python executable
23
24 if (!std::filesystem::exists(pythonExecutablePath))
25 {
26 MessageBoxW(NULL, (L"Error: python.exe not found at " + pythonExecutablePath.wstring() + L"\nPlease ensure python.exe is in the same directory as this executable.").c_str(), L"Launcher Error", MB_OK | MB_ICONERROR);
27 LocalFree(argvW);
28 argvW = nullptr;
29 }
30 }
31 else
32 {
33 MessageBoxW(NULL, L"Error: No command line arguments provided.", L"Launcher Error", MB_OK | MB_ICONERROR);
34 LocalFree(argvW);
35 argvW = nullptr;
36 }
37 }
38
39 return argvW;
40}
41
42std::wstring commandLineConverter(std::wstring scriptFileName, std::filesystem::path pythonExecutablePath, LPWSTR* argvW, int argc, bool& console)
43{
44 // Build the command line for the new Python process
45 std::wstringstream commandLineStream;
46
47 commandLineStream << L"\"" << pythonExecutablePath.wstring() << L"\""; // The Python executable path itself is the first part of the command line. It must be quoted if it contains spaces.
48 commandLineStream << L" \"" << scriptFileName << L"\""; // Enclose in quotes to handle spaces
49
50 bool bOtherArgs = false; // Flag to check if there are any other arguments
51 bool bImportModel = false; // Flag to check if there are any other arguments
52 bool gui = false; // Flag to check if GUI mode is needed
53 bool verbose = false; // Flag to check if verbose mode is needed
54
55 for (int i = 1; i < argc; ++i) // Append the rest of the arguments from the original process, starting from argvW[1]
56 {
57 std::wstring arglo = argvW[i];
58 std::transform(arglo.begin(), arglo.end(), arglo.begin(), [](auto c) { return towlower(c); });
59 if (arglo == std::wstring(L"--console"))
60 {
61 console = true;
62 continue;
63 }
64 else if (arglo == std::wstring(L"--verbose"))
65 {
66 verbose = true; // just to skip the argument from bOtherArgs
67 }
68 else if (arglo == std::wstring(L"--gui"))
69 {
70 gui = true; // just to skip the argument from bOtherArgs
71 }
72 else if (arglo == std::wstring(L"--import-model"))
73 {
74 bImportModel = true;
75 }
76 else
77 {
78 bOtherArgs = true; // If any other argument is found, set the flag to true
79 }
80 commandLineStream << L" \"" << argvW[i] << L"\"";
81 }
82
83 console = console || (bOtherArgs && !gui) || bImportModel; // If there are other arguments, we assume console mode is needed, or if --import-model is specified, we also assume console mode is needed.
84 //gui = (gui || !bOtherArgs) && !bImportModel; // If no other arguments are provided, we assume GUI mode is needed, and if --import-model is specified, we assume GUI mode is not needed.
85
86
87 return commandLineStream.str();
88}
Header file for the CommandLineTools module. This file contains declarations for functions related to...