-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.cpp
More file actions
244 lines (225 loc) · 7.15 KB
/
Copy pathstart.cpp
File metadata and controls
244 lines (225 loc) · 7.15 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
#include <windows.h>
#include <shellapi.h>
#include <filesystem>
#include <string>
#include <vector>
#include <algorithm>
#include <cwctype>
#include <cstdio>
namespace {
std::wstring trim(const std::wstring& value) {
size_t start = 0;
while (start < value.size() && (value[start] == L' ' || value[start] == L'\r' || value[start] == L'\n' || value[start] == L'\t')) {
++start;
}
size_t end = value.size();
while (end > start && (value[end - 1] == L' ' || value[end - 1] == L'\r' || value[end - 1] == L'\n' || value[end - 1] == L'\t')) {
--end;
}
return value.substr(start, end - start);
}
bool fileExists(const std::wstring& path) {
DWORD attrs = GetFileAttributesW(path.c_str());
return attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY);
}
std::wstring quoteArg(const std::wstring& arg) {
if (arg.empty()) {
return L"\"\"";
}
bool needsQuotes = false;
for (wchar_t ch : arg) {
if (ch == L' ' || ch == L'\t' || ch == L'"') {
needsQuotes = true;
break;
}
}
if (!needsQuotes) {
return arg;
}
std::wstring result = L"\"";
int backslashes = 0;
for (wchar_t ch : arg) {
if (ch == L'\\') {
++backslashes;
continue;
}
if (ch == L'"') {
result.append(backslashes * 2 + 1, L'\\');
result.push_back(L'"');
backslashes = 0;
continue;
}
if (backslashes > 0) {
result.append(backslashes, L'\\');
backslashes = 0;
}
result.push_back(ch);
}
if (backslashes > 0) {
result.append(backslashes * 2, L'\\');
}
result.push_back(L'"');
return result;
}
std::vector<std::wstring> runWhere(const std::wstring& executable) {
std::vector<std::wstring> results;
std::wstring command = L"where.exe " + executable + L" 2>nul";
FILE* pipe = _wpopen(command.c_str(), L"rt");
if (!pipe) {
return results;
}
wchar_t buffer[4096];
while (fgetws(buffer, sizeof(buffer) / sizeof(wchar_t), pipe)) {
std::wstring line = trim(buffer);
if (!line.empty() && fileExists(line)) {
results.push_back(line);
}
}
_pclose(pipe);
return results;
}
std::wstring newestPath(const std::vector<std::wstring>& paths) {
std::wstring best;
FILETIME bestTime{};
for (const auto& path : paths) {
WIN32_FILE_ATTRIBUTE_DATA data{};
if (!GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &data)) {
continue;
}
if (best.empty() || CompareFileTime(&data.ftLastWriteTime, &bestTime) > 0) {
best = path;
bestTime = data.ftLastWriteTime;
}
}
return best;
}
void appendIfExists(std::vector<std::wstring>& items, const std::wstring& value) {
std::wstring candidate = trim(value);
if (!candidate.empty() && fileExists(candidate)) {
items.push_back(candidate);
}
}
std::vector<std::wstring> pythonFromEnv() {
std::vector<std::wstring> items;
wchar_t buffer[32767];
for (const wchar_t* key : {L"PYTHON", L"PYTHON_HOME", L"PYTHONHOME", L"FLATLINE_PYTHON", L"FLATLINE_CHILD_PYTHON"}) {
DWORD length = GetEnvironmentVariableW(key, buffer, 32767);
if (length == 0 || length >= 32767) {
continue;
}
std::wstring raw(buffer, length);
appendIfExists(items, raw);
if (!raw.empty()) {
std::filesystem::path asPath(raw);
if (std::filesystem::is_directory(asPath)) {
appendIfExists(items, (asPath / L"python.exe").wstring());
}
}
}
return items;
}
std::vector<std::wstring> pythonFromCDrive() {
std::vector<std::wstring> items;
std::error_code ec;
std::filesystem::path root(L"C:\\");
if (!std::filesystem::exists(root, ec)) {
return items;
}
for (const auto& entry : std::filesystem::directory_iterator(root, ec)) {
if (ec) {
break;
}
if (!entry.is_directory()) {
continue;
}
std::wstring name = entry.path().filename().wstring();
std::wstring lower = name;
std::transform(lower.begin(), lower.end(), lower.begin(), towlower);
if (lower.rfind(L"python", 0) != 0) {
continue;
}
appendIfExists(items, (entry.path() / L"python.exe").wstring());
}
return items;
}
std::wstring findPython() {
auto fromWhere = runWhere(L"python.exe");
std::wstring best = newestPath(fromWhere);
if (!best.empty()) {
return best;
}
auto envHits = pythonFromEnv();
best = newestPath(envHits);
if (!best.empty()) {
return best;
}
auto cHits = pythonFromCDrive();
best = newestPath(cHits);
return best;
}
std::wstring moduleDir() {
wchar_t buffer[MAX_PATH * 4] = {0};
DWORD len = GetModuleFileNameW(nullptr, buffer, static_cast<DWORD>(sizeof(buffer) / sizeof(buffer[0])));
if (!len) {
return L".";
}
std::filesystem::path full(buffer);
return full.parent_path().wstring();
}
std::wstring joinCommand(const std::vector<std::wstring>& args) {
std::wstring joined;
for (size_t index = 0; index < args.size(); ++index) {
if (index) {
joined += L' ';
}
joined += quoteArg(args[index]);
}
return joined;
}
}
int wmain(int argc, wchar_t* argv[]) {
std::wstring python = findPython();
std::wstring baseDir = moduleDir();
std::wstring startPy = (std::filesystem::path(baseDir) / L"start.py").wstring();
if (python.empty() || !fileExists(startPy)) {
std::wstring message = L"Application could not find a usable Python runtime or start.py next to start.exe.\n\n";
message += L"Python: " + (python.empty() ? std::wstring(L"(not found)") : python) + L"\n";
message += L"start.py: " + startPy + L"\n\n";
message += L"Install Python, keep start.py next to start.exe, then try again.";
MessageBoxW(nullptr, message.c_str(), L"Application launcher warning", MB_OK | MB_ICONWARNING);
return 1;
}
std::vector<std::wstring> forwarded;
forwarded.push_back(python);
forwarded.push_back(startPy);
for (int index = 1; index < argc; ++index) {
forwarded.push_back(argv[index] ? argv[index] : L"");
}
std::wstring command = joinCommand(forwarded);
std::vector<wchar_t> mutableCommand(command.begin(), command.end());
mutableCommand.push_back(L'\0');
_wputenv_s(L"FLATLINE_LAUNCHED_FROM_START_EXE", L"1");
STARTUPINFOW si{};
si.cb = sizeof(si);
PROCESS_INFORMATION pi{};
BOOL ok = CreateProcessW(
nullptr,
mutableCommand.data(),
nullptr,
nullptr,
TRUE,
0,
nullptr,
baseDir.c_str(),
&si,
&pi
);
if (!ok) {
std::wstring message = L"Application could not launch start.py through Python.\n\nCommand:\n" + command;
MessageBoxW(nullptr, message.c_str(), L"Application launcher warning", MB_OK | MB_ICONWARNING);
return 1;
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}