-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpp
1161 lines (916 loc) · 27.3 KB
/
core.cpp
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Windows.h>
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
#include "gui.hpp"
#include "globals.hpp"
#include "wrap.hpp"
#include "security.hpp"
#include "Utilities/Scan.h"
#include "Utilities/Instance.h"
#include "Lua/lua.hpp"
DWORD ScriptContext = 0;
DWORD int3breakpoint = 0;
#include <Windows.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <TlHelp32.h>
#define DLURL_SUCCESS 0
#define DLURL_FAILED_REQUEST 1
#define DLURL_FAILED_CERT_QUERY 2
#define DLURL_FAILED_CERT_CHECK 3
// athread
namespace util {
class athread {
private:
HANDLE thread;
int threadID;
public:
athread(void* func, void* args);
int terminate();
int wait();
HANDLE getThread();
int getExitCode();
int getThreadId();
int running();
};
class timer {
private:
unsigned __int64 _starting_tick;
unsigned __int64 _finish_tick;
public:
timer() { Reset(); }
/* resets the timer */
void Reset() {
_starting_tick = 0;
_finish_tick = 0;
}
/* starts the timer */
void Start() {
Reset();
_starting_tick = GetTickCount64();
}
/* stops the timer and returns the elapsed time */
double Stop() {
_finish_tick = GetTickCount64();
return GetElapsedTime();
}
/* returns elapsed time */
double GetElapsedTime() {
if (_starting_tick & _finish_tick)
return (_finish_tick - _starting_tick) / 1000.0;
return 0.0;
}
};
template <typename T>
class singleton {
private:
static T* _instance;
public:
static T* instance() {
if (_instance)
return _instance;
_instance = new T;
return _instance;
}
singleton(singleton const&) = delete;
void operator=(singleton const&) = delete;
};
// other
void GetFile(const char* dllName, const char* fileName, char* buffer, int bfSize);
long int GetFileSize(FILE* ifile);
int DownloadURL(const std::string& server, const std::string& path, const std::string& params, std::string& out, unsigned char useSSL, unsigned char dontCache, unsigned char direct);
int ReadFile(const std::string& path, std::string& out, unsigned char binary);
int WriteFile(const std::string& path, std::string data, unsigned char binary);
std::vector<std::string> GetArguments(std::string input);
int GetProcessByImageName(const char* imageName);
std::string lowercase(std::string& input);
std::wstring s2ws(const std::string& str);
std::string ws2s(const std::wstring& wstr);
std::string GetRawStringAtDelim(std::string input, int arg, char delim);
void GetFilesInDirectory(std::vector<std::string> &out, const std::string &directory, unsigned char includePath);
int GetEIP();
void pause();
namespace registry {
int ReadString(HKEY key, const char* value, std::string& out);
}
};
#include <WinInet.h>
#pragma comment(lib, "WinInet.lib")
#include <locale>
#include <utility>
#include <codecvt>
namespace util {
athread::athread(void* func, void* args) {
thread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)func, args, NULL, (LPDWORD)&threadID);
}
int athread::terminate() {
if (this->running()) {
if (TerminateThread(thread, 0)) {
thread = NULL;
threadID = 0;
}
}
return getExitCode();
}
HANDLE athread::getThread() {
return thread;
}
int athread::getExitCode() {
int exitCode = 0;
if (thread)
GetExitCodeThread(thread, (LPDWORD)&exitCode);
return exitCode;
}
int athread::getThreadId() {
return threadID;
}
int athread::wait() {
DWORD exitCode = 0;
if (thread) {
DWORD result;
do {
result = WaitForSingleObject(thread, 0);
if (result == WAIT_OBJECT_0) {
GetExitCodeThread(thread, &exitCode);
break;
}
Sleep(100);
} while (1);
}
return exitCode;
}
int athread::running() {
return this->getExitCode() == STILL_ACTIVE;
}
void GetFile(const char* dllName, const char* fileName, char* buffer, int bfSize) {
GetModuleFileName(GetModuleHandle(dllName), buffer, bfSize);
if (strlen(fileName) + strlen(buffer) < MAX_PATH) {
char* pathEnd = strrchr(buffer, '\\');
strcpy(pathEnd + 1, fileName);
}
else {
*buffer = 0;
}
}
long int GetFileSize(FILE* ifile) {
long int fsize = 0;
long int fpos = ftell(ifile);
fseek(ifile, 0, SEEK_END);
fsize = ftell(ifile);
fseek(ifile, fpos, SEEK_SET);
return fsize;
}
int DownloadURL(const std::string& server, const std::string& path, const std::string& params, std::string& out, unsigned char useSSL, unsigned char dontCache, unsigned char direct) {
HINTERNET interwebs = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
int rResults = 0;
std::string path_w_params = path + (params.empty() ? "" : "?" + params);
interwebs = InternetOpen("util/Agent", direct ? INTERNET_OPEN_TYPE_DIRECT : INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
if (interwebs)
hConnect = InternetConnect(interwebs, server.c_str(), useSSL ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (hConnect)
hRequest = HttpOpenRequest(hConnect, "GET", path_w_params.c_str(), NULL, NULL, NULL, (useSSL ? INTERNET_FLAG_SECURE : 0) | (dontCache ? INTERNET_FLAG_NO_CACHE_WRITE : 0), 0);
if (hRequest)
rResults = HttpSendRequest(hRequest, 0, 0, NULL, NULL);
if (rResults) {
char buffer[2000];
DWORD bytesRead = 0;
do {
InternetReadFile(hRequest, buffer, 2000, &bytesRead);
out.append(buffer, bytesRead);
memset(buffer, 0, 2000);
} while (bytesRead);
rResults = DLURL_SUCCESS;
}
else
rResults = DLURL_FAILED_REQUEST;
if (interwebs) InternetCloseHandle(interwebs);
if (hConnect) InternetCloseHandle(hConnect);
if (hRequest) InternetCloseHandle(hRequest);
return rResults;
}
int ReadFile(const std::string& path, std::string& out, unsigned char binary) {
std::ios::openmode mode = std::ios::in;
if (binary)
mode |= std::ios::binary;
std::ifstream file(path, mode);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
out = buffer.str();
file.close();
return 1;
}
file.close();
return 0;
}
int WriteFile(const std::string& path, std::string data, unsigned char binary) {
std::ios::openmode mode = std::ios::out;
if (binary)
mode |= std::ios::binary;
std::ofstream file(path, mode);
if (file.is_open()) {
file << data;
file.close();
return 1;
}
file.close();
return 0;
}
std::vector<std::string> GetArguments(std::string input) {
std::vector<std::string> rtn;
if (input[0] == ' ') {
input = input.substr(1);
}
BYTE size = input.size();
DWORD pos1 = 0;
for (int i = 0; i < size; ++i) {
if (input[i] == ' ') {
rtn.push_back(input.substr(pos1, i - pos1));
pos1 = i + 1;
}
else if (i == size - 1) {
rtn.push_back(input.substr(pos1, i - pos1 + 1));
pos1 = i + 1;
}
}
return rtn;
}
std::string GetRawStringAtDelim(std::string input, int arg, char delim) {
char lc = 0;
int c = 0;
for (int i = 0; i < input.size(); ++i) {
if (input[i] == delim && lc != delim)
c++;
if (c == arg)
return input.substr(i + 1);
}
return "";
}
std::string lowercase(std::string& input) {
std::string s;
for (int i = 0; i < input.size(); ++i) {
s.push_back(tolower(input[i]));
}
return s;
}
std::wstring s2ws(const std::string& str)
{
typedef std::codecvt_utf8<wchar_t> convert_typeX;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.from_bytes(str);
}
std::string ws2s(const std::wstring& wstr)
{
typedef std::codecvt_utf8<wchar_t> convert_typeX;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(wstr);
}
int GetProcessByImageName(const char* imageName) {
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (strcmp(entry.szExeFile, imageName) == 0)
{
CloseHandle(snapshot);
return entry.th32ProcessID;
}
}
}
CloseHandle(snapshot);
return 0;
}
void GetFilesInDirectory(std::vector<std::string> &out, const std::string &directory, unsigned char includePath) // thx stackoverflow
{
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const std::string file_name = file_data.cFileName;
const std::string full_file_name = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
out.push_back(includePath ? full_file_name : file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
}
/* Gets current time*/
int __declspec(naked) GetEIP() {
__asm pop eax
__asm ret
}
void pause() {
printf("Press enter to continue . . .");
getchar();
}
namespace registry {
int ReadString(HKEY key, const char* value, std::string& out) {
int val_sz;
int val_type;
char* val;
if (RegQueryValueEx(key, value, NULL, (LPDWORD)&val_type, NULL, (LPDWORD)&val_sz) != ERROR_SUCCESS)
return 0;
if (val_type != REG_SZ || !val_sz)
return 0;
val = new (std::nothrow) char[val_sz];
if (!val) return 0;
if (RegQueryValueEx(key, value, NULL, NULL, (LPBYTE)val, (LPDWORD)&val_sz) != ERROR_SUCCESS) {
delete[] val;
return 0;
}
out = val;
delete[] val;
return 1;
}
}
};
unsigned int danghui_rbase()
{
return (unsigned int)(GetModuleHandleA("RobloxPlayerBeta.exe"));
}
#define EUI_EXIT (WM_APP + 500)
#define EUI_SHOWCONSOLE (WM_APP + 501)
#define EUI_INITCMDTHREAD (WM_APP + 502)
#define EUI_CREDITS (WM_APP + 503)
// Elysian UI (600-699)
#define EUI_TEXTBOX (WM_APP + 600)
#define EUI_EXECUTE (WM_APP + 601)
#define EUI_CBSCRIPT (WM_APP + 602)
#define EUI_EXTEND (WM_APP + 603)
#define EUI_FILEVIEW (WM_APP + 604)
// Elysian UI Extra (700-749)
#define EUI_LV_RCM_EXEC (WM_APP + 700)
#define EUI_LV_RCM_STOP (WM_APP + 701)
// --- UI Config --- \\
#define EUI_PADDING 10
#define EUI_POSX 300
#define EUI_POSY 300
#define EUI_WIDTH 729
#define EUI_HEIGHT 430
#define CONSOLE_MESSAGE_LIMIT 255
// --- Utility Macros --- \\
#define VSCROLL_WIDTH 16
#define HSCROLL_HEIGHT 16
typedef void(*CONSOLE_THREAD)();
class Form {
private:
HWND _window = 0;
const char* _class = 0;
HINSTANCE _hinstance = 0;
util::athread* _thread = 0;
util::athread* _watch_directory_thread = 0;
util::athread* _console_thread = 0;
int _console_toggled = 0;
CONSOLE_THREAD _console_routine = 0;
const char* _init_error = 0;
HANDLE _init_event = 0;
HMENU _menu;
HWND _file_listview;
HWND _script_edit;
HWND _console_edit;
HWND _execute_button;
public:
~Form();
/* creates a new thread and initiates the window */
void Start(const char* window_name, const char* class_name, const char* local_module_name);
void SetTitle(const char* title);
/* returns 1 if the window is running */
int IsRunning();
/* waits for the window thread to exit or form to close */
void Wait();
/* sends a WM_DESTROY message to the window */
void Close();
void ToggleConsole();
int StartConsoleThread();
void AssignConsoleRoutine(CONSOLE_THREAD routine);
/* writes a formatted and colored message to the console textbox */
void print(COLORREF col, const char * format, ...);
HWND GetWindow() { return _window; };
HWND GetListView() { return _file_listview; };
HWND GetScriptTextbox() { return _script_edit; };
HWND GetConsoleTextbox() { return _console_edit; };
int IsConsoleToggled() { return _console_toggled; };
int IsConsoleThreadRunning() { if (!_console_thread) return 0; return _console_thread->running(); };
private:
static void init_stub(Form* form);
void init();
void message_loop();
int register_window();
HMENU create_window_menu();
void create_ui_elements();
static int watch_directory(Form* form);
void refresh_list_view(const char* path);
};
extern Form* form;
#include <Richedit.h>
#include <CommCtrl.h>
#pragma comment(lib, "Comctl32.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
HFONT gMainFont = CreateFontA(15, 0, 0, 0, FW_LIGHT, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Courier New"));
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CTLCOLORSTATIC: {
SetBkColor((HDC)wParam, RGB(255, 255, 255));
return (LRESULT)GetStockObject(WHITE_BRUSH);
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case EUI_SHOWCONSOLE:
form->ToggleConsole();
break;
case EUI_INITCMDTHREAD:
form->StartConsoleThread();
break;
case EUI_EXECUTE:
{
int length = SendMessage((HWND)form->GetScriptTextbox(), WM_GETTEXTLENGTH, NULL, NULL);
char* code = new char[length];
if (!code) {
form->print(RGB(255, 0, 0), "ERROR: allocation error");
break;
}
SendMessage((HWND)form->GetScriptTextbox(), WM_GETTEXT, length + 1, (LPARAM)code);
danghui_Aexecute(code);
delete[] code;
break;
}
case EUI_CREDITS:
MessageBox(form->GetWindow(), "", "Credits", MB_OK);
break;
case EUI_EXIT:
if (MessageBox(form->GetWindow(), "ROBLOX will now close. Continue?", "Exit", MB_OKCANCEL) == IDOK)
TerminateProcess(GetCurrentProcess(), 0);
break;
}
break;
case WM_NOTIFY:
{
LPNMHDR info = ((LPNMHDR)lParam);
switch (info->code) {
case NM_RCLICK:
if (info->idFrom == EUI_FILEVIEW) {
LPNMITEMACTIVATE lpnmitem = (LPNMITEMACTIVATE)lParam;
int iSelected = -1;
iSelected = SendMessage(((LPNMHDR)lParam)->hwndFrom, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
if (iSelected != -1) {
int result = GetMessagePos();
POINTS coords = MAKEPOINTS(result);
HMENU hPopupMenu = CreatePopupMenu();
InsertMenu(hPopupMenu, 0, MF_STRING, EUI_LV_RCM_EXEC, "Execute");
int ret = TrackPopupMenu(hPopupMenu, TPM_RETURNCMD | TPM_TOPALIGN | TPM_LEFTALIGN, coords.x, coords.y, 0, form->GetWindow(), NULL);
// Elysian UI ListView RightClickMenu Execute
if (ret == EUI_LV_RCM_EXEC) {
LVITEM lvitem;
ZeroMemory(&lvitem, sizeof(LVITEM));
char* name[MAX_PATH];
lvitem.iItem = lpnmitem->iItem;
lvitem.mask = LVIF_PARAM | LVIF_TEXT;
lvitem.pszText = (LPSTR)name;
lvitem.cchTextMax = MAX_PATH;
ListView_GetItem(info->hwndFrom, &lvitem);
//printf("iItem: %d, lParam: %x, pszText: %s\n", lpnmitem->iItem, lvitem.lParam, lvitem.pszText);
std::string* path = (std::string*)lvitem.lParam;
if (path) {
std::string data;
if (util::ReadFile(*path, data, 0)) {
danghui_Aexecute(data.c_str());
form->print(RGB(0, 150, 0), "executed file '%s'\r\n", lvitem.pszText);
}
else {
form->print(RGB(255, 0, 0), "ERROR: failed to open '%s'\r\n", lvitem.pszText);
}
}
else {
form->print(RGB(255, 0, 0), "ERROR: failed to get path for '%s'\r\n", lvitem.pszText);
}
}
return 1;
}
}
break;
}
break;
}
case WM_CLOSE:
//ShowWindow(hwnd, SW_MINIMIZE);
PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
// --- winapi ui utilities --- \\
int AddListViewItem(HWND listView, const char* name, void* lParam) {
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.pszText = (LPSTR)name;
lvi.lParam = (LPARAM)lParam;
if (ListView_InsertItem(listView, &lvi) == -1)
return 0;
return 1;
}
int ClearListView(HWND listView) {
int count = ListView_GetItemCount(listView);
if (!count)
return 0;
LVITEM lvitem;
ZeroMemory(&lvitem, sizeof(LVITEM));
for (int i = 0; i < count; i++) {
lvitem.iItem = i;
lvitem.mask = LVIF_PARAM;
ListView_GetItem(listView, &lvitem);
std::string* str = (std::string*)lvitem.lParam;
if (str)
delete str;
}
ListView_DeleteAllItems(listView);
}
void RemoveStyle(HWND hwnd, int style) {
LONG lExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
lExStyle &= ~(style);
SetWindowLong(hwnd, GWL_EXSTYLE, lExStyle);
}
HWND CreateToolTip(HWND hwndTool, PTSTR pszText)
{
if (!hwndTool || !pszText)
{
return FALSE;
}
// Get the window of the tool.
// Create the tooltip. g_hInst is the global instance handle.
HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hwndTool, NULL,
NULL, NULL);
if (!hwndTool || !hwndTip)
{
return (HWND)NULL;
}
// Associate the tooltip with the tool.
TOOLINFO toolInfo = { 0 };
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hwndTool;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hwndTool;
toolInfo.lpszText = pszText;
SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
return hwndTip;
}
void Form::Start(const char* window_name, const char* class_name, const char* module) {
if (IsRunning()) return;
_class = class_name;
_hinstance = GetModuleHandle(module);
/* set up init event for communication with window thread */
_init_event = CreateEventA(0, TRUE, FALSE, 0);
/* spawn window thread */
_thread = new util::athread(init_stub, this);
/* wait for init */
WaitForSingleObject(_init_event, INFINITE);
/* check for errors */
if (_init_error)
throw std::exception(_init_error);
/* set title*/
SetTitle(window_name);
/* clean up */
_init_error = 0;
CloseHandle(_init_event);
_init_event = 0;
}
Form::~Form() {
delete _thread;
delete _watch_directory_thread;
delete _console_thread;
}
int Form::IsRunning() {
if (!_thread)
return 0;
return _thread->running();
}
void Form::Wait() {
if (_thread)
_thread->wait();
}
void Form::Close() {
if (IsRunning())
PostMessage(_window, WM_DESTROY, 0, 0);
}
void Form::SetTitle(const char* title) {
SetWindowText(_window, title);
}
void Form::ToggleConsole() {
_console_toggled = !_console_toggled;
if (_console_toggled)
ShowWindow(GetConsoleWindow(), SW_NORMAL);
else
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
int Form::StartConsoleThread() {
if (_console_routine && !IsConsoleThreadRunning()) {
_console_thread = new util::athread(_console_routine, 0);
return 1;
}
return 0;
}
void Form::AssignConsoleRoutine(CONSOLE_THREAD routine) {
_console_routine = routine;
}
void Form::print(COLORREF col, const char * format, ...) {
char message[CONSOLE_MESSAGE_LIMIT];
memset(message, 0, sizeof(message));
va_list vl;
va_start(vl, format);
vsnprintf_s(message, CONSOLE_MESSAGE_LIMIT, format, vl);
va_end(vl);
int len = SendMessage(_console_edit, WM_GETTEXTLENGTH, NULL, NULL);
SendMessage(_console_edit, EM_SETSEL, len, len);
CHARFORMAT cfd; // default
CHARFORMAT cf;
SendMessage(_console_edit, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cfd);
memcpy(&cf, &cfd, sizeof(CHARFORMAT));
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR; // change color
cf.crTextColor = col;
cf.dwEffects = 0;
SendMessage(_console_edit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
SendMessage(_console_edit, EM_REPLACESEL, FALSE, (LPARAM)message);
SendMessage(_console_edit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfd);
}
void Form::init_stub(Form* form) {
if (form)
form->init();
}
void Form::init() {
#define init_error(error) _init_error = error; SetEvent(_init_event); return;
#define init_success() _init_error = 0; SetEvent(_init_event);
/* register window class */
if (!register_window()) {
init_error("failed to register window");
}
/* create window menu */
HMENU window_menu = create_window_menu();
/* create window */
_window = CreateWindowExA(
NULL,
_class,
0,
WS_SYSMENU | WS_MINIMIZEBOX,
EUI_POSX,
EUI_POSY,
EUI_WIDTH,
EUI_HEIGHT,
0,
window_menu,
_hinstance,
0);
if (!_window) {
init_error("failed to create window");
}
/* load msftedit.dll (richedit) */
if (!LoadLibraryA("Msftedit.dll")) {
init_error("failed to load ui component");
}
/* init common controls */
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = ICC_WIN95_CLASSES;
if (!InitCommonControlsEx(&icc)) {
init_error("failed to initiate common controls");
}
create_ui_elements();
_watch_directory_thread = new util::athread(watch_directory, this);
init_success();
ShowWindow(_window, SW_NORMAL);
message_loop();
delete _watch_directory_thread;
return;
}
void Form::message_loop() {
MSG message;
int ret;
while ((ret = GetMessage(&message, 0, 0, 0)) != 0) {
if (ret == 0) {
// quit message
return;
}
else if (ret == -1) {
// unexpected error
return;
}
else {
TranslateMessage(&message);
DispatchMessage(&message);
}
}
}
int Form::register_window() {
UnregisterClass(_class, _hinstance);
WNDCLASSEX nClass;
nClass.cbSize = sizeof(WNDCLASSEX);
nClass.style = CS_DBLCLKS;
nClass.lpfnWndProc = WindowProc;
nClass.cbClsExtra = 0;
nClass.cbWndExtra = 0;
nClass.hInstance = _hinstance;
nClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // TODO: make an icon for elysian
nClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
nClass.hCursor = LoadCursor(NULL, IDC_ARROW);
nClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
nClass.lpszMenuName = 0;
nClass.lpszClassName = _class;
if (!RegisterClassEx(&nClass))
return 0;
return 1;
}
HMENU Form::create_window_menu() {
HMENU _menu = CreateMenu();
if (!_menu)
return 0;
return _menu;
}
void Form::create_ui_elements() {
#define UI_SET_COLOR(h, r, g, b) SendMessage(h, EM_SETBKGNDCOLOR, 0, RGB(r, g, b))
#define UI_SET_FONT(h, f) SendMessage(h, WM_SETFONT, (WPARAM)f, MAKELPARAM(TRUE, 0))
_script_edit = CreateWindowEx(NULL, "RICHEDIT50W", 0, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | WS_BORDER, 12, 128, 540, 240, _window, (HMENU)EUI_TEXTBOX, 0, 0);
_console_edit = CreateWindowEx(NULL, "RICHEDIT50W", 0, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | WS_BORDER | ES_READONLY, 12, 12, 540, 110, _window, 0, 0, 0);
_execute_button = CreateWindowEx(NULL, "BUTTON", "Execute", WS_CHILD | WS_VISIBLE, 534, 374, 183, 23, _window, (HMENU)EUI_EXECUTE, 0, 0);
//HWND extend_button = CreateWindowEx(NULL, "BUTTON", ">", WS_CHILD | WS_VISIBLE, 570 - 22, EUI_PADDING, 12, 312, MainWindow, (HMENU)EUI_EXTEND, 0, 0);
_file_listview = CreateWindowEx(NULL, WC_LISTVIEW, 0, WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_SINGLESEL | LVS_LIST,558, 12, 159, 356, _window, (HMENU)EUI_FILEVIEW, 0, 0);
//
SendMessage(_script_edit, WM_SETFONT, (WPARAM)gMainFont, MAKELPARAM(TRUE, 0));
SendMessage(_script_edit, EM_SETLIMITTEXT, 0x7FFFFFFE, 0);
RemoveStyle(_script_edit, WS_EX_CLIENTEDGE);
SendMessage(_console_edit, WM_SETFONT, (WPARAM)gMainFont, MAKELPARAM(TRUE, 0));
RemoveStyle(_console_edit, WS_EX_CLIENTEDGE);
SendMessage(_execute_button, WM_SETFONT, (WPARAM)gMainFont, MAKELPARAM(TRUE, 0));
//SendMessage(extend_button, WM_SETFONT, (WPARAM)GlobalFont, MAKELPARAM(TRUE, 0));
SendMessage(_file_listview, WM_SETFONT, (WPARAM)gMainFont, MAKELPARAM(TRUE, 0));
//SendMessage(script_edit, EM_SETLIMITTEXT, EUI_TEXT_CAP, NULL);
// tooltips
CreateToolTip(_console_edit, "Console");
}
// --- file list view -- \\
void Form::refresh_list_view(const char* path) {
ClearListView(_file_listview);
std::vector<std::string> files;
std::string dir = path;