-
Notifications
You must be signed in to change notification settings - Fork 4
/
Unit1.cpp
1371 lines (1264 loc) · 38.9 KB
/
Unit1.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 <vcl.h>
#include <Psapi.h>
#include <Mmsystem.h>
#include <Registry.hpp>
#include <FileCtrl.hpp>
#include <vector>
#include <list>
#pragma hdrstop
#include "Unit1.h"
#include "osd.h"
#include "Utils.h"
#include "volume.h"
#ifdef _WIN64
#pragma comment(lib, "Psapi.a")
#else
#pragma comment(lib, "Psapi.lib")
#endif
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormPfreepanic *FormPfreepanic;
//---------------------------------------------------------------------------
#define MEM_OFFSET 0xC00 // offset padding relative to .dll file
typedef struct HexCode
{
DWORD offset;
std::vector<BYTE> off;
std::vector<BYTE> on;
} HexCode;
typedef struct Game
{
UnicodeString winclass;
UnicodeString modulename;
std::list<std::list<HexCode> > versions;
void clear() {
winclass = L"";
modulename = L"";
versions.clear();
}
} Game;
typedef struct TermWnd
{
UnicodeString wndtitle;
UnicodeString wndclass;
TermWnd(UnicodeString ttl, UnicodeString cls) {
wndtitle = ttl;
wndclass = cls;
}
} TermWnd;
//---------------------------------------------------------------------------
void BeginPrint();
void Print(UnicodeString msg);
void Error(UnicodeString msg);
void WinError(UnicodeString msg);
//---------------------------------------------------------------------------
HHOOK hHook = NULL;
bool Working = false;
bool Terminating = false;
bool Voluming = false;
typedef struct KeyStruct {
static const int Timeout0 = 6000000; // 10 000 000 == 1sec
static const int Timeout1 = 1000; // poll timer is 50ms anyway
DWORD key;
bool wasDown;
__int64 lastTime;
bool firstRepeat;
KeyStruct():key(0),wasDown(false),lastTime(0),firstRepeat(true) {}
} KeyStruct;
KeyStruct KeyS,
TermKeyS,
ScreenshotKeyS,
VolumeUpKeyS,
VolumeDownKeyS,
ToggleMuteKeyS,
LoginKeyS;
int VolumeIncrement = 5;
DWORD LoginWait = 0; // time to wait until input pin screen
DWORD LoginWait1 = 0; // time to wait until profile screen (press start)
DWORD LoginWait2 = 0; // time to wait until blah-blah confirm screen (press start)
DWORD PinKey[4] = {0}; // auto login pin
DWORD StartKey = 0; // start key (bind same key as Naive in spice)
int LoginOSD = 0; // show login keys OSD, or not
UnicodeString get_key_string(DWORD key)
{
switch (key % 256) {
case 0:
return L"none";
case 0x01:
return L"Left MB";
case 0x02:
return L"Right MB";
case 0x04:
return L"Middle MB";
case 0x05:
return L"X1 MB";
case 0x06:
return L"X2 MB";
case 0x08:
return L"Backspace";
case 0x09:
return L"Tab";
case 0x0C:
return L"Clear";
case 0x0D:
return L"Enter";
case 0x10:
return L"Shift";
case 0x11:
return L"Ctrl";
case 0x12:
if (key > 255)
return L"AltGr";
else
return L"Alt";
case 0x13:
return L"Pause";
case 0x14:
return L"Caps Lock";
case 0x1B:
return L"Escape";
case 0x20:
return L"Space";
case 0x21:
return L"Page Up";
case 0x22:
return L"Page Down";
case 0x23:
return L"End";
case 0x24:
return L"Home";
case 0x25:
return L"Left";
case 0x26:
return L"Up";
case 0x27:
return L"Right";
case 0x28:
return L"Down";
case 0x2C:
return L"Prt Scr";
case 0x2D:
return L"Insert";
case 0x2E:
return L"Delete";
case 0x30:
return L"0";
case 0x31:
return L"1";
case 0x32:
return L"2";
case 0x33:
return L"3";
case 0x34:
return L"4";
case 0x35:
return L"5";
case 0x36:
return L"6";
case 0x37:
return L"7";
case 0x38:
return L"8";
case 0x39:
return L"9";
case 0x41:
return L"A";
case 0x42:
return L"B";
case 0x43:
return L"C";
case 0x44:
return L"D";
case 0x45:
return L"E";
case 0x46:
return L"F";
case 0x47:
return L"G";
case 0x48:
return L"H";
case 0x49:
return L"I";
case 0x4A:
return L"J";
case 0x4B:
return L"K";
case 0x4C:
return L"L";
case 0x4D:
return L"M";
case 0x4E:
return L"N";
case 0x4F:
return L"O";
case 0x50:
return L"P";
case 0x51:
return L"Q";
case 0x52:
return L"R";
case 0x53:
return L"S";
case 0x54:
return L"T";
case 0x55:
return L"U";
case 0x56:
return L"V";
case 0x57:
return L"W";
case 0x58:
return L"X";
case 0x59:
return L"Y";
case 0x5A:
return L"Z";
case 0x5B:
return L"Left Windows";
case 0x5C:
return L"Right Windows";
case 0x5D:
return L"Apps";
case 0x60:
return L"Num 0";
case 0x61:
return L"Num 1";
case 0x62:
return L"Num 2";
case 0x63:
return L"Num 3";
case 0x64:
return L"Num 4";
case 0x65:
return L"Num 5";
case 0x66:
return L"Num 6";
case 0x67:
return L"Num 7";
case 0x68:
return L"Num 8";
case 0x69:
return L"Num 9";
case 0x6A:
return L"*";
case 0x6B:
return L"+";
case 0x6C:
return L"Seperator";
case 0x6D:
return L"-";
case 0x6E:
return L".";
case 0x6F:
return L"/";
case 0x70:
return L"F1";
case 0x71:
return L"F2";
case 0x72:
return L"F3";
case 0x73:
return L"F4";
case 0x74:
return L"F5";
case 0x75:
return L"F6";
case 0x76:
return L"F7";
case 0x77:
return L"F8";
case 0x78:
return L"F9";
case 0x79:
return L"F10";
case 0x7A:
return L"F11";
case 0x7B:
return L"F12";
case 0x7C:
return L"F13";
case 0x7D:
return L"F14";
case 0x7E:
return L"F15";
case 0x7F:
return L"F16";
case 0x80:
return L"F17";
case 0x81:
return L"F18";
case 0x82:
return L"F19";
case 0x83:
return L"F20";
case 0x84:
return L"F21";
case 0x85:
return L"F22";
case 0x86:
return L"F23";
case 0x87:
return L"F24";
case 0x90:
return L"Num Lock";
case 0x91:
return L"Scroll Lock";
case 0xA0:
return L"Left Shift";
case 0xA1:
return L"Right Shift";
case 0xA2:
return L"Left Control";
case 0xA3:
return L"Right Control";
case 0xA4:
return L"Left Menu";
case 0xA5:
return L"Right Menu";
default:
{
// check win API
wchar_t keyName[128];
if (GetKeyNameText((LONG) (MapVirtualKey(key, MAPVK_VK_TO_VSC) << 16), keyName, 128))
return keyName;
return L"Unknown";
}
}
}
UnicodeString getKeyString(DWORD key)
{
UnicodeString rv;
rv.sprintf(L"%s (%d)", get_key_string(key).w_str(), key);
return rv;
}
//---------------------------------------------------------------------------
std::list<Game> g_games;
TList *pTermList;
tOSD *pOSD;
//---------------------------------------------------------------------------
void InitGameCodes()
{
Game game;
std::list<HexCode> ver;
HexCode hex;
BYTE *data;
#ifdef _WIN64
// SDVX5
game.winclass = L"SOUND VOLTEX VIVID WAVE main";
game.modulename = L"soundvoltex.dll";
// KFC-2019070900
hex.offset = 0x4845DA;
data = (BYTE*)"\x48\x63\x81\xC8\x11\x00\x00\x48\x8B\xF2\x48\x8B\xF9\x83\xF8\x04\x0F\x8D\xE4\x01\x00\x00";
hex.off.assign(data, data + 22);
data = (BYTE*)"\x48\xC7\xC0\x01\x00\x00\x00\x48\x89\x81\xC8\x11\x00\x00\x48\x89\xD6\x48\x89\xCF\x90\x90";
hex.on.assign(data, data + 22);
ver.push_back(hex);
game.versions.push_back(ver);
ver.clear();
// KFC-2019100800 dubious update
hex.offset = 0x4E4F9A;
data = (BYTE*)"\x48\x63\x81\xE8\x11\x00\x00\x48\x8B\xF2\x48\x8B\xF9\x83\xF8\x04\x0F\x8D\xFB\x01\x00\x00";
hex.off.assign(data, data + 22);
data = (BYTE*)"\x48\xC7\xC0\x01\x00\x00\x00\x48\x89\x81\xE8\x11\x00\x00\x48\x89\xD6\x48\x89\xCF\x90\x90";
hex.on.assign(data, data + 22);
ver.push_back(hex);
game.versions.push_back(ver);
ver.clear();
// 2019103100 CN ver.
hex.offset = 0x536D5A;
data = (BYTE*)"\x48\x63\x81\xA8\x12\x00\x00\x48\x8B\xF2\x48\x8B\xF9\x83\xF8\x04\x0F\x8D\x01\x02\x00\x00";
hex.off.assign(data, data + 22);
data = (BYTE*)"\x48\xc7\xc0\x01\x00\x00\x00\x48\x89\x81\xa8\x12\x00\x00\x48\x89\xd6\x48\x89\xcf\x90\x90";
hex.on.assign(data, data + 22);
ver.push_back(hex);
game.versions.push_back(ver);
ver.clear();
g_games.push_back(game);
game.clear();
#else
// SDVX4
game.winclass = L"SOUND VOLTEX IV HEAVENLY HAVEN 1";
game.modulename = L"soundvoltex.dll";
// 2019020600 final
hex.offset = 0x1D6B8F;
data = (BYTE*)"\x00";
hex.off.assign(data, data + 1);
data = (BYTE*)"\x02";
hex.on.assign(data, data + 1);
ver.push_back(hex);
hex.offset = 0x1D6D76;
data = (BYTE*)"\x8B\x83\x64\x10\x00\x00\x8D\x48\x01\x83\xF9\x04\x56\x57\x7F\x52";
hex.off.assign(data, data + 16);
data = (BYTE*)"\xB8\x01\x00\x00\x00\x89\x83\x64\x10\x00\x00\x90\x56\x57\x90\x90";
hex.on.assign(data, data + 16);
ver.push_back(hex);
game.versions.push_back(ver);
ver.clear();
// 2018011602 enhanced continue
hex.offset = 0x1CEE4F;
data = (BYTE*)"\x00";
hex.off.assign(data, data + 1);
data = (BYTE*)"\x02";
hex.on.assign(data, data + 1);
ver.push_back(hex);
hex.offset = 0x1CF016;
data = (BYTE*)"\x8B\x83\x64\x10\x00\x00\x8D\x48\x01\x83\xF9\x04\x56\x57\x7F\x52";
hex.off.assign(data, data + 16);
data = (BYTE*)"\xB8\x01\x00\x00\x00\x89\x83\x64\x10\x00\x00\x90\x56\x57\x90\x90";
hex.on.assign(data, data + 16);
ver.push_back(hex);
game.versions.push_back(ver);
ver.clear();
g_games.push_back(game);
game.clear();
// SDVX3
game.winclass = L"SOUND VOLTEX III GRAVITY WARS";
game.modulename = L"soundvoltex.dll";
// 2016121200 season 2
hex.offset = 0x196BDF;
data = (BYTE*)"\x00";
hex.off.assign(data, data + 1);
data = (BYTE*)"\x02";
hex.on.assign(data, data + 1);
ver.push_back(hex);
hex.offset = 0x196C66;
data = (BYTE*)"\x8B\x83\x78\x0A\x00\x00\x8D\x48\x01\x83\xF9\x03\x56\x57\x7F\x52";
hex.off.assign(data, data + 16);
data = (BYTE*)"\xB8\x01\x00\x00\x00\x89\x83\x78\x0A\x00\x00\x90\x56\x57\x90\x90";
hex.on.assign(data, data + 16);
ver.push_back(hex);
game.versions.push_back(ver);
ver.clear();
g_games.push_back(game);
game.clear();
#endif
}
//---------------------------------------------------------------------------
// Return:
// 1 match
// 0 no match
// -1 read failed
int MemCompare(HANDLE hproc, LPVOID address, LPVOID data, size_t size)
{
int rv = 0;
BYTE *buffer = new BYTE[size];
if (!ReadProcessMemory(hproc, address, buffer, size, NULL)) {
WinError(L"Read process memory failed");
rv = -1;
goto exit_MemCompare;
}
if (memcmp(buffer, data, size) == 0) {
rv = 1;
}
exit_MemCompare:
delete []buffer;
return rv;
}
//---------------------------------------------------------------------------
// __OUT__ hProc
LPVOID GetBaseAddress(HWND hwindow, UnicodeString modulename, HANDLE &hProc)
{
DWORD procID = NULL;
HMODULE hMods[1024];
DWORD cbNeeded;
bool module_found = false;
LPVOID baseAddr = NULL;
if (!GetWindowThreadProcessId(hwindow, &procID)) {
WinError(L"Get window process ID failed");
goto exit_GetBaseAddress;
}
if (procID != NULL)
{
hProc = OpenProcess(/*PROCESS_ALL_ACCESS*/
PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION |
PROCESS_QUERY_INFORMATION, FALSE, procID);
}
if (!hProc) {
WinError(L"Can not open process");
goto exit_GetBaseAddress;
}
if (!EnumProcessModules(hProc, hMods, sizeof(hMods), &cbNeeded)) {
WinError(L"Enumerating process modules failed");
goto exit_GetBaseAddress;
}
for (unsigned int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
TCHAR szModName[MAX_PATH];
// Get the full path to the module's file.
if (GetModuleFileNameEx(hProc, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR)))
{
UnicodeString name(szModName);
if (name.Pos(modulename) > 0) { // find our module and get base address
module_found = true;
MODULEINFO info;
if (!GetModuleInformation(hProc, hMods[i], &info, sizeof(MODULEINFO))) {
WinError(L"Get module information failed");
goto exit_GetBaseAddress;
}
baseAddr = info.lpBaseOfDll;
break;
}
}
}
if (!module_found) {
Error(UnicodeString(L"Module ")+ modulename + L" not found");
}
exit_GetBaseAddress:
return baseAddr;
}
//---------------------------------------------------------------------------
void Toggle()
{
bool version_found = false;
HANDLE hproc = NULL;
BeginPrint();
std::list<Game>::iterator i = g_games.begin();
while (i != g_games.end()) {
Game &game = *i; i++; // next game
HWND hgame_wnd = FindWindow(game.winclass.c_str(), NULL);
if (!hgame_wnd) {
continue;
}
Print(game.winclass);
LPVOID baseAddr = GetBaseAddress(hgame_wnd, game.modulename, hproc);
if (!baseAddr || !hproc) {
goto exit_Toggle;
}
std::list<std::list<HexCode> >::iterator j = game.versions.begin();
while (j != game.versions.end()) {
std::list<HexCode> &ver = *j; j++; // next version
size_t num_total = ver.size(); // number of hex codes
for (int onoff = 0; onoff < 2; onoff++) {
std::vector<BYTE> *hexdata; // on/off flipper
// Test
size_t num_found = 0;
std::list<HexCode>::iterator k = ver.begin();
while (k != ver.end()) {
HexCode &hex = *k; k++;
hexdata = (onoff == 0)? &hex.on : &hex.off; // test PFREE first
int result = MemCompare(hproc, (BYTE*)baseAddr + MEM_OFFSET + hex.offset,
&(*hexdata).front(), (*hexdata).size());
if (result == -1)
goto exit_Toggle;
else if (result)
num_found++;
}
if (num_found == num_total) {
version_found = true;
// Write
k = ver.begin();
while (k != ver.end()) {
HexCode &hex = *k; k++;
hexdata = (onoff == 0)? &hex.off : &hex.on; // write NORMAL first
if (!WriteProcessMemory(hproc, (BYTE*)baseAddr + MEM_OFFSET + hex.offset,
&(*hexdata).front(), (*hexdata).size(), NULL)) {
WinError(L"Write process memory failed");
goto exit_Toggle;
}
UnicodeString str;
str.sprintf(L"%X: %s", hex.offset, BinToHexW(&(*hexdata).front(), (*hexdata).size()).w_str());
Print(str);
}
// Write success, show notification
if (onoff == 0) { // notify NORMAL first
FormPfreepanic->Caption = UnicodeString(L"NORMAL - ") + Application->Title;
if (FormPfreepanic->chkOSDEnabled->Checked)
pOSD->SendMessage(L"Credit Mode", FormPfreepanic->udOSDDuration->Position);
if (FormPfreepanic->chkVoiceEnabled->Checked)
PlaySound((ExtractFilePath(Application->ExeName) + (FormPfreepanic->rbVoiceEnglish->Checked? L"off-eng.wav" : L"off.wav")).c_str(), NULL, SND_FILENAME|SND_ASYNC);
} else {
FormPfreepanic->Caption = UnicodeString(L"PFREE - ") + Application->Title;
if (FormPfreepanic->chkOSDEnabled->Checked)
pOSD->SendMessage(L"Premium Free Mode", FormPfreepanic->udOSDDuration->Position);
if (FormPfreepanic->chkVoiceEnabled->Checked)
PlaySound((ExtractFilePath(Application->ExeName) + (FormPfreepanic->rbVoiceEnglish->Checked? L"on-eng.wav" : L"on.wav")).c_str(), NULL, SND_FILENAME|SND_ASYNC);
}
// Done, exiting
goto exit_Toggle;
}
}
}
if (!version_found) {
Error(L"Invalid game data.\nPress [Information...] for supported game version.");
goto exit_Toggle;
}
}
Error(L"Game window not found");
exit_Toggle:
if (hproc) CloseHandle(hproc);
}
//---------------------------------------------------------------------------
void BeginPrint()
{
FormPfreepanic->Memo1->Color = clWindow;
FormPfreepanic->Memo1->Font->Color = clWindowText;
FormPfreepanic->Memo1->Font->Style = TFontStyles();
FormPfreepanic->Memo1->Clear();
}
void Print(UnicodeString msg)
{
FormPfreepanic->Memo1->Lines->Add(msg);
}
UnicodeString WinFormatError(DWORD errNo)
{
LPVOID lpMsgBuf = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, errNo, 0,
(LPTSTR)&lpMsgBuf, 0, NULL);
UnicodeString errMsg; errMsg.sprintf(L"%d: %s", errNo, (TCHAR*)lpMsgBuf);
LocalFree(lpMsgBuf);
return errMsg;
}
void Error(UnicodeString msg)
{
FormPfreepanic->Memo1->Color = clRed;
FormPfreepanic->Memo1->Font->Color = clYellow;
FormPfreepanic->Memo1->Font->Style = TFontStyles() << fsBold;
FormPfreepanic->Memo1->Lines->Text = UnicodeString(L"ERROR:\n") + msg;
FormPfreepanic->Memo1->SetFocus();
MessageBeep(MB_ICONHAND);
}
void WinError(UnicodeString msg)
{
Error(msg + L"\n" + WinFormatError(GetLastError()));
}
//---------------------------------------------------------------------------
void ThreadWatchDir()
{
// Directory to watch.
UnicodeString csDirectory = FormPfreepanic->edtSSNpath->Text; //L"C:\\";
HANDLE hDirectory = CreateFile( csDirectory.c_str(),
FILE_LIST_DIRECTORY,
FILE_SHARE_READ|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL );
if (hDirectory == INVALID_HANDLE_VALUE) {
FormPfreepanic->lblSSNstatus->Caption = L"Error";
return;
}
while (1)
{
// Buffer to get the notification information.
const int MAX_BUFFER = 1024;
BYTE Buffer[MAX_BUFFER];
BYTE *pBuffer = Buffer;
DWORD dwBytesReturned = 0;
BOOL readsuccess = ReadDirectoryChangesW( hDirectory,
Buffer,
MAX_BUFFER,
FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME,
&dwBytesReturned,
0, 0 );
if (!readsuccess) {
CloseHandle(hDirectory);
FormPfreepanic->lblSSNstatus->Caption = L"Error";
return;
}
// go through buffer
FILE_NOTIFY_INFORMATION* pNotifyInfo = (FILE_NOTIFY_INFORMATION*)Buffer;
UnicodeString files;
bool first = true;
while (1)
{
if (pNotifyInfo->Action == FILE_ACTION_ADDED) {
if (!first) files += L"\n";
// get file
size_t fnlen = pNotifyInfo->FileNameLength / 2;
wchar_t fname[MAX_PATH];
wcsncpy(fname, pNotifyInfo->FileName, fnlen); fname[fnlen] = L'\0';
files += fname;
first = false;
}
// next buffer entry
if (pNotifyInfo->NextEntryOffset) {
pBuffer += pNotifyInfo->NextEntryOffset;
pNotifyInfo = (FILE_NOTIFY_INFORMATION*)pBuffer;
} else {
break;
}
}
// notify of files
if (files.Length()) {
pOSD->SendMessage(UnicodeString(L"New screenshot:\n") + files, FormPfreepanic->udOSDDuration->Position);
}
}
}
//---------------------------------------------------------------------------
HWND FindTermWnd()
{
HWND hWnd = NULL;
for (int i = 0; i < pTermList->Count; i++) {
TermWnd *pwi = (TermWnd*)pTermList->Items[i];
hWnd = FindWindow(pwi->wndclass.c_str(), pwi->wndtitle.c_str());
if (hWnd) {
break;
}
}
return hWnd;
}
//---------------------------------------------------------------------------
void Terminate()
{
HANDLE hProc;
HWND hWnd;
DWORD procID = 0;
hWnd = FindTermWnd();
if (!hWnd) {
goto exit_Terminate;
}
// get process ID
if (!GetWindowThreadProcessId(hWnd, &procID)) {
WinError(L"Get window process ID failed");
goto exit_Terminate;
}
// soft close window first
SendNotifyMessage(hWnd, WM_CLOSE, 0, 0);
Sleep(500);
// hard terminate process
hProc = OpenProcess(PROCESS_TERMINATE, FALSE, procID);
if (!hProc) { // already closed or some error, whatever
goto exit_Terminate;
}
TerminateProcess(hProc, 0);
exit_Terminate:
if (hProc) CloseHandle(hProc);
}
//---------------------------------------------------------------------------
// callback function for keyboard hook
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
// struct to get virtual key codes
KBDLLHOOKSTRUCT kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
if (nCode == HC_ACTION && wParam == WM_KEYUP && kbdStruct.vkCode > 0)
{
if (FormPfreepanic->edtKey->Tag) {
KeyS.key = kbdStruct.vkCode;
FormPfreepanic->Memo1->SetFocus();
FormPfreepanic->Save();
} else
if (FormPfreepanic->edtTermKey->Tag) {
TermKeyS.key = kbdStruct.vkCode;
FormPfreepanic->Memo1->SetFocus();
FormPfreepanic->Save();
} else
if (FormPfreepanic->edtScreenshotKey->Tag) {
ScreenshotKeyS.key = kbdStruct.vkCode;
FormPfreepanic->Memo1->SetFocus();
FormPfreepanic->Save();
} else
if (FormPfreepanic->edtVolumeUpKey->Tag) {
VolumeUpKeyS.key = kbdStruct.vkCode;
FormPfreepanic->Memo1->SetFocus();
FormPfreepanic->Save();
} else
if (FormPfreepanic->edtVolumeDownKey->Tag) {
VolumeDownKeyS.key = kbdStruct.vkCode;
FormPfreepanic->Memo1->SetFocus();
FormPfreepanic->Save();
} else
if (FormPfreepanic->edtToggleMuteKey->Tag) {
ToggleMuteKeyS.key = kbdStruct.vkCode;
FormPfreepanic->Memo1->SetFocus();
FormPfreepanic->Save();
} else
if (FormPfreepanic->edtLoginKey->Tag) {
LoginKeyS.key = kbdStruct.vkCode;
FormPfreepanic->Memo1->SetFocus();
FormPfreepanic->Save();
}
}
}
// if the message does not get processed
return CallNextHookEx (0, nCode, wParam, lParam);
}
//---------------------------------------------------------------------------
__fastcall TFormPfreepanic::TFormPfreepanic(TComponent* Owner)
: TForm(Owner)
{
#ifdef _WIN64
Application->Title = L"pfreepanic-vw";
Caption = L"pfreepanic-vw";
#else
Application->Title = L"pfreepanic";
Caption = L"pfreepanic";
#endif
InitGameCodes();
pTermList = new TList();
pTermList->Add(new TermWnd(L"SOUND VOLTEX EXCEED GEAR - Main Screen", L"SOUND VOLTEX EXCEED GEAR - Main Screen"));
pTermList->Add(new TermWnd(L"SOUND VOLTEX VIVID WAVE main", L"SOUND VOLTEX VIVID WAVE main"));
pTermList->Add(new TermWnd(L"SOUND VOLTEX IV HEAVENLY HAVEN 1", L"SOUND VOLTEX IV HEAVENLY HAVEN 1"));
pTermList->Add(new TermWnd(L"SOUND VOLTEX III GRAVITY WARS", L"SOUND VOLTEX III GRAVITY WARS"));
pTermList->Add(new TermWnd(L"SOUND VOLTEX II -infinite infection-", L"SOUND VOLTEX II -infinite infection-"));
pTermList->Add(new TermWnd(L"beatmania IIDX 24 SINOBUZ", L"beatmania IIDX 24 SINOBUZ"));
pTermList->Add(new TermWnd(L"pop'n music eclale", L"pop'n music eclale"));
pTermList->Add(new TermWnd(L"MUSECA", L"MUSECA"));
pTermList->Add(new TermWnd(L"BeatStream", L"BeatStream"));
pTermList->Add(new TermWnd(L"", L"_")); // GITADORA Tri-Boost Re:EVOLVE / Matixx
// load .ini
Load();
// OSD object
pOSD = new tOSD();
// Screenshot folder monitoring thread
if (edtSSNpath->Text.Length()) {
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadWatchDir, 0, 0, 0);
CloseHandle(h);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPfreepanic::FormDestroy(TObject *Sender)
{
Unhook(false);
Save();
delete pTermList;
delete pOSD;
}
//---------------------------------------------------------------------------
void TFormPfreepanic::Load()
{
TIniFile *ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
KeyS.key = (DWORD)ini->ReadInteger(L"GENERAL", L"Hotkey", 0);
edtKey->Text = getKeyString(KeyS.key);
TermKeyS.key = (DWORD)ini->ReadInteger(L"GENERAL", L"TerminateHotkey", 0);
edtTermKey->Text = getKeyString(TermKeyS.key);
ScreenshotKeyS.key = (DWORD)ini->ReadInteger(L"GENERAL", L"ScreenshotHotkey", 0);
edtScreenshotKey->Text = getKeyString(ScreenshotKeyS.key);
VolumeUpKeyS.key = (DWORD)ini->ReadInteger(L"GENERAL", L"VolumeUpHotkey", 0);
edtVolumeUpKey->Text = getKeyString(VolumeUpKeyS.key);
VolumeDownKeyS.key = (DWORD)ini->ReadInteger(L"GENERAL", L"VolumeDownHotkey", 0);
edtVolumeDownKey->Text = getKeyString(VolumeDownKeyS.key);
ToggleMuteKeyS.key = (DWORD)ini->ReadInteger(L"GENERAL", L"ToggleMuteHotkey", 0);
edtToggleMuteKey->Text = getKeyString(ToggleMuteKeyS.key);
VolumeIncrement = ini->ReadInteger(L"GENERAL", L"VolumeIncrement", 2);
LoginKeyS.key = (DWORD)ini->ReadInteger(L"GENERAL", L"LoginHotkey", 0);
edtLoginKey->Text = getKeyString(LoginKeyS.key);
LoginWait = (DWORD)ini->ReadInteger(L"GENERAL", L"LoginWait", 9000);
LoginWait1 = (DWORD)ini->ReadInteger(L"GENERAL", L"LoginWait1", 5000); // 0-disable
LoginWait2 = (DWORD)ini->ReadInteger(L"GENERAL", L"LoginWait2", 2500); // 0-disable
PinKey[0] = (DWORD)ini->ReadInteger(L"GENERAL", L"PinKey0", 97); // numpad 1234
PinKey[1] = (DWORD)ini->ReadInteger(L"GENERAL", L"PinKey1", 98);
PinKey[2] = (DWORD)ini->ReadInteger(L"GENERAL", L"PinKey2", 99);
PinKey[3] = (DWORD)ini->ReadInteger(L"GENERAL", L"PinKey3", 100);
StartKey = (DWORD)ini->ReadInteger(L"GENERAL", L"StartKey", 49); // '1', 0-disable
LoginOSD = ini->ReadInteger(L"GENERAL", L"LoginOSD", 1); // 1-enable, 0-disable
if (1 == ini->ReadInteger(L"GENERAL", L"VoiceEnglish", 1))
rbVoiceEnglish->Checked = true;
else
rbVoiceJapanese->Checked = true;
chkVoiceEnabled->Checked = (1 == ini->ReadInteger(L"GENERAL", L"VoiceEnabled", 1));
udOSDDuration->Position = ini->ReadInteger(L"GENERAL", L"OSDDuration", 120);
chkOSDEnabled->Checked = ini->ReadInteger(L"GENERAL", L"OSDEnabled", 0) == 1;
edtSSNpath->Text = HexToStr(ini->ReadString(L"GENERAL", L"OSDScreenshotPath", L""));
lblSSNstatus->Caption = edtSSNpath->Text.Length()? L"Monitoring..." : L"Disabled";
delete ini;
}
//---------------------------------------------------------------------------
void TFormPfreepanic::Save()
{
TIniFile *ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
ini->WriteInteger(L"GENERAL", L"Hotkey", (int)KeyS.key);
ini->WriteInteger(L"GENERAL", L"TerminateHotkey", (int)TermKeyS.key);
ini->WriteInteger(L"GENERAL", L"ScreenshotHotkey", (int)ScreenshotKeyS.key);
ini->WriteInteger(L"GENERAL", L"VolumeUpHotkey", (int)VolumeUpKeyS.key);
ini->WriteInteger(L"GENERAL", L"VolumeDownHotkey", (int)VolumeDownKeyS.key);
ini->WriteInteger(L"GENERAL", L"ToggleMuteHotkey", (int)ToggleMuteKeyS.key);
ini->WriteInteger(L"GENERAL", L"VolumeIncrement", VolumeIncrement);
ini->WriteInteger(L"GENERAL", L"LoginHotkey", (int)LoginKeyS.key);
ini->WriteInteger(L"GENERAL", L"LoginWait", (int)LoginWait);
ini->WriteInteger(L"GENERAL", L"LoginWait1", (int)LoginWait1);
ini->WriteInteger(L"GENERAL", L"LoginWait2", (int)LoginWait2);
ini->WriteInteger(L"GENERAL", L"PinKey0", (int)PinKey[0]);
ini->WriteInteger(L"GENERAL", L"PinKey1", (int)PinKey[1]);
ini->WriteInteger(L"GENERAL", L"PinKey2", (int)PinKey[2]);
ini->WriteInteger(L"GENERAL", L"PinKey3", (int)PinKey[3]);
ini->WriteInteger(L"GENERAL", L"StartKey", (int)StartKey);
ini->WriteInteger(L"GENERAL", L"LoginOSD", LoginOSD);
ini->WriteInteger(L"GENERAL", L"VoiceEnglish", rbVoiceEnglish->Checked?1:0);
ini->WriteInteger(L"GENERAL", L"VoiceEnabled", chkVoiceEnabled->Checked?1:0);
ini->WriteInteger(L"GENERAL", L"OSDDuration", udOSDDuration->Position);
ini->WriteInteger(L"GENERAL", L"OSDEnabled", chkOSDEnabled->Checked?1:0);
ini->WriteString (L"GENERAL", L"OSDScreenshotPath", StrToHex(edtSSNpath->Text));
delete ini;
}
//---------------------------------------------------------------------------
void __fastcall TFormPfreepanic::edtKeyEnter(TObject *Sender)
{
edtKey->Tag = 1;
edtKey->Text = L"Press single key";
Hook();
}
void __fastcall TFormPfreepanic::edtKeyExit(TObject *Sender)
{
edtKey->Tag = 0;
edtKey->Text = getKeyString(KeyS.key);
Unhook();
}
//---------------------------------------------------------------------------
void __fastcall TFormPfreepanic::edtTermKeyEnter(TObject *Sender)
{