-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleEmulator.cpp
More file actions
496 lines (438 loc) · 13.8 KB
/
ConsoleEmulator.cpp
File metadata and controls
496 lines (438 loc) · 13.8 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
#include "pch.h"
#include "resource.h"
#include "ConsoleEmulator.h"
#include <shellapi.h> // for CommandLineToArgvW
#include <vector>
#include <string>
#include <commctrl.h>
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
// 3) Add a script that sits in the Debug/Release folders, checks if each executable or .dll file is signed, and signs it if it isn't.
// 6) Transition patcher to window mode, will probably have to do something very similar.
#include "YesNoCancel.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
bool force = false;
HFONT font = NULL;
HWND mainWindow = NULL;
HWND textEdit = NULL;
HANDLE eventToInjectorThread = NULL;
std::wstring* getLineLine = nullptr;
bool wantAnyKey = false;
bool anyKeyPressed = false;
int nCmdShowToUse = 0;
extern void GetLine(std::wstring& line);
extern void AskYesNoCancel(const char* prompt, YesNoCancel* result);
void OutputStringA(const char* text);
void OutputStringW(const wchar_t* text);
void pressAnyKeyBegin();
void pressAnyKeyEnd();
bool isAnyKeyPressed();
void onPressAnyKeyBegin();
void onPressAnyKeyEnd();
void onIsAnyKeyPressed(bool* theValue);
void OutputStringAImpl(const char* text);
void OutputStringWImpl(const wchar_t* text);
void onTimer();
LRESULT __stdcall TextEditSubclass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
void onGetLine(WPARAM wParam);
void onAskYesNoCancel(WPARAM wParam, LPARAM lParam);
int countChar(const char* text, int chr);
void onCommand(WPARAM wParam, LPARAM lParam, LRESULT* lResult);
void onKeyDown(WPARAM wParam, LRESULT* lResult, bool handleEventOnYourOwn);
void onSize(WPARAM wParam, LPARAM lParam, LRESULT* lResult);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
int numArgs = 0;
LPWSTR* contiguousBlockOfMemory = CommandLineToArgvW(lpCmdLine, &numArgs);
if (contiguousBlockOfMemory) {
int exitCode;
if (!parseArgs(numArgs, contiguousBlockOfMemory, &exitCode)) {
return exitCode;
}
LocalFree(contiguousBlockOfMemory);
}
LoadStringW(hInstance, windowAppTitleResourceId, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, windowClassNameResourceId, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow)) {
return FALSE;
}
HACCEL hAccelTable = LoadAcceleratorsW(hInstance, windowAcceleratorId);
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0)) {
if (!TranslateAcceleratorW(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance) {
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIconW(hInstance, windowIconId);
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = windowMenuName;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIconW(wcex.hInstance, windowIconId);
return RegisterClassExW(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
hInst = hInstance;
mainWindow = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!mainWindow) {
return FALSE;
}
RECT clientRect;
GetClientRect(mainWindow, &clientRect);
NONCLIENTMETRICSW nonClientMetrics { 0 };
nonClientMetrics.cbSize = sizeof(NONCLIENTMETRICSW);
SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &nonClientMetrics, NULL);
font = CreateFontIndirectW(&nonClientMetrics.lfCaptionFont);
textEdit = CreateWindowW(WC_EDITW, L"",
WS_CHILD
| WS_OVERLAPPED
| WS_VISIBLE
| ES_MULTILINE
| WS_VSCROLL
| ES_AUTOVSCROLL
| ES_READONLY, // change with EM_SETREADONLY
5, 5, clientRect.bottom - 10, clientRect.right - 10, mainWindow, NULL, hInst, NULL);
SendMessageW(textEdit, WM_SETFONT, (WPARAM)font, TRUE);
SetWindowSubclass(textEdit, TextEditSubclass, 1, NULL);
eventToInjectorThread = CreateEventW(NULL, FALSE, FALSE, NULL);
if (force && forceAllowed) {
// delay showing the window by a little bit
nCmdShowToUse = nCmdShow;
SetTimer(mainWindow, 1, 8 * 1000, NULL);
} else {
ShowWindow(mainWindow, nCmdShow);
UpdateWindow(mainWindow);
}
SetFocus(textEdit);
CloseHandle(CreateThread(0, 0, taskThreadProc, NULL, 0, NULL));
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
LRESULT result = 0;
switch (message) {
case WM_COMMAND:
onCommand(wParam, lParam, &result);
break;
case WM_CHAR:
onKeyDown(wParam, &result, true);
return DefWindowProcW(hWnd, message, wParam, lParam);
case WM_SIZE:
onSize(wParam, lParam, &result);
return DefWindowProcW(hWnd, message, wParam, lParam);
case WM_GET_LINE:
onGetLine(wParam);
break;
case WM_ASK_YES_NO_CANCEL:
onAskYesNoCancel(wParam, lParam);
break;
case WM_TASK_ENDED:
DestroyWindow(hWnd);
break;
case WM_OUTPUT_STRING_A:
OutputStringAImpl((const char*)wParam);
break;
case WM_OUTPUT_STRING_W:
OutputStringWImpl((const wchar_t*)wParam);
break;
case WM_PRESS_ANY_KEY_BEGIN:
onPressAnyKeyBegin();
break;
case WM_PRESS_ANY_KEY_END:
onPressAnyKeyEnd();
break;
case WM_IS_ANY_KEY_PRESSED:
onIsAnyKeyPressed((bool*)wParam);
break;
case WM_TIMER:
onTimer();
break;
case WM_CLOSE:
if (!getLineLine && !wantAnyKey) {
int answer = MessageBoxW(hWnd, L"Process is currently busy."
L" Interrupting it is not as good as waiting for it to finish normally."
L" Do you want to close this program anyway?", szTitle, MB_OKCANCEL);
if (answer != IDOK) {
break;
}
}
return DefWindowProcW(hWnd, message, wParam, lParam);
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
UNREFERENCED_PARAMETER(lParam);
switch (message) {
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void onCommand(WPARAM wParam, LPARAM lParam, LRESULT* lResult) {
int wmId = LOWORD(wParam);
switch (wmId) {
case IDM_ABOUT:
DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_ABOUTBOX), mainWindow, About);
break;
case IDM_EXIT:
DestroyWindow(mainWindow);
break;
default:
*lResult = DefWindowProcW(mainWindow, WM_COMMAND, wParam, lParam);
}
}
void onKeyDown(WPARAM wParam, LRESULT* lResult, bool handleEventOnYourOwn) {
if (wParam == L'\r' && getLineLine) {
std::vector<wchar_t> buf;
size_t length = GetWindowTextLengthW(textEdit);
buf.resize(length + 1);
wchar_t* start = buf.data();
if (buf.size() > 0xFFFFFFFF) {
buf.erase(buf.begin(), buf.begin() + (buf.size() - 0xFFFFFFFF));
}
GetWindowTextW(textEdit, start, buf.size() & 0xFFFFFFFF);
length = wcslen(start);
wchar_t* ptr = start + length - 1;
while (ptr >= start && *ptr != L'\n') {
--ptr;
}
if (ptr < start) {
*getLineLine = start;
} else {
*getLineLine = ptr + 1;
}
getLineLine = nullptr;
if (handleEventOnYourOwn) {
SendMessageW(textEdit, EM_SETREADONLY, TRUE, 0);
SetEvent(eventToInjectorThread);
}
}
if (wantAnyKey
&& wParam != 9) { // TAB, as in Alt+TAB
if (!anyKeyPressed) {
anyKeyPressed = true;
}
}
}
void onSize(WPARAM wParam, LPARAM lParam, LRESULT* lResult) {
int newWidth = (int)LOWORD(lParam);
int newHeight = (int)HIWORD(lParam);
if (textEdit) {
MoveWindow(textEdit, 5, 5, newWidth - 10, newHeight - 10, TRUE);
}
}
int countChar(const char* text, int chr) {
int result = 0;
const char* c;
const char* cNext = strchr(text, chr);
while (cNext) {
++result;
c = cNext + 1;
cNext = strchr(c, chr);
}
return result;
}
int countChar(const wchar_t* text, wchar_t chr) {
int result = 0;
const wchar_t* c;
const wchar_t* cNext = wcschr(text, chr);
while (cNext) {
++result;
c = cNext + 1;
cNext = wcschr(c, chr);
}
return result;
}
inline size_t OutputString_GetLen(const char* text) {
size_t result = strlen(text);
int newlinesCount = countChar(text, '\n');
return result + newlinesCount;
}
inline size_t OutputString_GetLen(const wchar_t* text) {
size_t result = wcslen(text);
int newlinesCount = countChar(text, L'\n');
return result + newlinesCount;
}
inline wchar_t* OutputString_Concat(wchar_t* dest, const char* src) {
const char* c = src;
while (*c != '\0') {
if (*c == '\n') {
*dest = L'\r';
++dest;
}
*dest = *c;
++c;
++dest;
}
*dest = L'\0';
return dest;
}
inline wchar_t* OutputString_Concat(wchar_t* dest, const wchar_t* src) {
const wchar_t* srcNext = wcschr(src, L'\n');
while (srcNext) {
if (src != srcNext) {
memcpy(dest, src, (BYTE*)srcNext - (BYTE*)src);
dest += srcNext - src;
}
*dest = L'\r';
++dest;
*dest = L'\n';
++dest;
src = srcNext + 1;
srcNext = wcschr(src, L'\n');
}
if (*src != L'\0') {
const wchar_t* endPos = wcschr(src, L'\0');
memcpy(dest, src, (BYTE*)endPos - (BYTE*)src);
dest += endPos - src;
}
*dest = L'\0';
return dest;
}
void OutputStringA(const char* text) {
PostMessageW(mainWindow, WM_OUTPUT_STRING_A, (WPARAM)text, 0);
WaitForSingleObject(eventToInjectorThread, INFINITE);
}
void OutputStringW(const wchar_t* text) {
PostMessageW(mainWindow, WM_OUTPUT_STRING_W, (WPARAM)text, 0);
WaitForSingleObject(eventToInjectorThread, INFINITE);
}
void OutputStringAImpl(const char* text) {
std::vector<wchar_t> buf;
int length = GetWindowTextLengthW(textEdit);
size_t textLength = OutputString_GetLen(text);
buf.resize(length
+ textLength
+ 1); // null character
GetWindowTextW(textEdit, buf.data(), length + 1);
buf[length] = L'\0';
size_t newLength = wcslen(buf.data());
newLength = OutputString_Concat(buf.data() + newLength, text) - buf.data();
SetWindowTextW(textEdit, buf.data());
SendMessageW(textEdit, EM_SETSEL, newLength, newLength);
SendMessageW(textEdit, EM_SCROLLCARET, 0, 0);
SetEvent(eventToInjectorThread);
}
void OutputStringWImpl(const wchar_t* text) {
std::vector<wchar_t> buf;
int length = GetWindowTextLengthW(textEdit);
size_t textLength = OutputString_GetLen(text);
buf.resize(length
+ textLength
+ 1); // null character
GetWindowTextW(textEdit, buf.data(), length + 1);
buf[length] = L'\0';
size_t newLength = wcslen(buf.data());
newLength = OutputString_Concat(buf.data() + newLength, text) - buf.data();
SetWindowTextW(textEdit, buf.data());
SendMessageW(textEdit, EM_SETSEL, newLength, newLength);
SendMessageW(textEdit, EM_SCROLLCARET, 0, 0);
SetEvent(eventToInjectorThread);
}
void GetLine(std::wstring& line) {
line.clear();
PostMessageW(mainWindow, WM_GET_LINE, (WPARAM)&line, 0);
WaitForSingleObject(eventToInjectorThread, INFINITE);
}
void AskYesNoCancel(const char* prompt, YesNoCancel* result) {
PostMessageW(mainWindow, WM_ASK_YES_NO_CANCEL, (WPARAM)prompt, (LPARAM)result);
WaitForSingleObject(eventToInjectorThread, INFINITE);
}
LRESULT __stdcall TextEditSubclass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
bool wasNotNull = getLineLine != nullptr;
if (hWnd == textEdit && uIdSubclass == 1 && uMsg == WM_CHAR) {
LRESULT unused;
onKeyDown(wParam, &unused, false);
}
LRESULT result = DefSubclassProc(hWnd, uMsg, wParam, lParam);
if (getLineLine == nullptr && wasNotNull) {
SendMessageW(textEdit, EM_SETREADONLY, TRUE, 0);
SetEvent(eventToInjectorThread);
}
return result;
}
void onGetLine(WPARAM wParam) {
getLineLine = (std::wstring*)wParam;
SendMessageW(textEdit, EM_SETREADONLY, FALSE, 0);
SetFocus(textEdit);
}
void onAskYesNoCancel(WPARAM wParam, LPARAM lParam) {
const char* prompt = (const char*)wParam;
YesNoCancel* result = (YesNoCancel*)lParam;
int msgBoxResult = MessageBoxA(mainWindow, prompt, "Question", MB_YESNOCANCEL);
if (msgBoxResult == IDYES) {
*result = YesNoCancel_Yes;
} else if (msgBoxResult == IDNO) {
*result = YesNoCancel_No;
} else if (msgBoxResult == IDCANCEL) {
*result = YesNoCancel_Cancel;
} else {
MessageBoxA(mainWindow, "Couldn't understand what choice you made. Selecting 'Cancel'.", "Error", MB_OK);
*result = YesNoCancel_Cancel;
}
SetEvent(eventToInjectorThread);
}
void pressAnyKeyBegin() {
PostMessageW(mainWindow, WM_PRESS_ANY_KEY_BEGIN, 0, 0);
}
void pressAnyKeyEnd() {
PostMessageW(mainWindow, WM_PRESS_ANY_KEY_END, 0, 0);
}
bool isAnyKeyPressed() {
bool theValue = false;
PostMessageW(mainWindow, WM_IS_ANY_KEY_PRESSED, (WPARAM)&theValue, 0);
WaitForSingleObject(eventToInjectorThread, INFINITE);
return theValue;
}
void onPressAnyKeyBegin() {
wantAnyKey = true;
SendMessageW(textEdit, EM_SETREADONLY, FALSE, 0);
}
void onPressAnyKeyEnd() {
wantAnyKey = false;
anyKeyPressed = false;
SendMessageW(textEdit, EM_SETREADONLY, TRUE, 0);
}
void onIsAnyKeyPressed(bool* theValue) {
*theValue = anyKeyPressed;
SetEvent(eventToInjectorThread);
}
void onTimer() {
KillTimer(mainWindow, 1);
ShowWindow(mainWindow, nCmdShowToUse);
UpdateWindow(mainWindow);
}