-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCheatSearch.c
More file actions
1693 lines (1408 loc) · 47.7 KB
/
Copy pathCheatSearch.c
File metadata and controls
1693 lines (1408 loc) · 47.7 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
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
/* Witten 20110303 ****************************************/
/**********************************************************/
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <stdio.h>
#include <winuser.h>
#include "main.h"
#include "CPU.h"
#include "debugger.h"
#include "resource_cheat.h"
#include "rom.h"
#include "cheats.h"
#include "cheatsearch.h"
#include "Memory.h"
#include "MemDump.h"
#include "Cheats.h"
#include "Cheats_Preprocessor.h"
#include "CheatSearch_Input.h"
#include "RomTools_Common.h"
#include "CheatSearch_Search.h"
#include "CheatSearch_Dev.h"
const char* N64GSCODETYPES8BIT[] = { "8-bit Constant Write (80)", "80",
"8-bit Uncached Write (A0)", "A0",
"8-bit GS Button (88)", "88",
"8-Bit Equal To Activator (D0)", "D0",
"8-Bit Different To Activator (D2)", "D2" };
const char* N64GSCODETYPES16BIT[] = { "16-bit Constant Write (81)", "81",
"16-bit Uncached Write (A1)", "A1",
"16-bit GS Button (89)", "89",
"16-Bit Equal To Activator (D1)", "D1",
"16-Bit Different To Activator (D3)", "D3" };
#define LISTVIEW_MAXSHOWN 4096
#define WM_LV_UPDATE (WM_APP + 1)
/////////////////////////////////////
// Start of Function Prototypes
/////////////////////////////////////
void Search(HWND hDlg);
void InitSearchFormatList(HWND hDlg);
void InitSearchNumBitsList(HWND hDlg);
void InitSearchResultList(HWND hDlg);
void PopulateSearchResultList(HWND hDlg);
void InitCheatCreateList(HWND hDlg);
void UpdateCheatCreateList(HWND hDlg);
void UpdateCheatCodePreview(HWND hDlg);
void Defaults_CheatSearchDlg();
void CE_UpdateControls(HWND hDlg, int item);
void CE_SaveCheat(int item);
void CS_UpdateSearchProc(HWND hDlg);
DWORD WINAPI LiveUpdate(LPVOID arg);
void StopLiveUpdate();
void __cdecl Create_Memory_Window(int Child);
// The following were done to clean up the message map a little
LRESULT CheatSearchResults_FindItem(NMHDR* lParam);
LRESULT CheatSearchResults_DrawItem(HANDLE hDlg, NMHDR* lParam);
LRESULT CheatSearchResults_ReadItem(NMHDR* lParam);
BOOL CALLBACK CheatSearchDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
BYTE Value_ReadByte(DWORD addr);
WORD Value_ReadWord(DWORD addr);
BYTE Text_ReadByte(DWORD addr);
/////////////////////////////////////
// End of Function Prototypes
/////////////////////////////////////
/////////////////////////////////////
// Global Variables
/////////////////////////////////////
CS_SEARCH search;
CS_RESULTS results = { 0 };
CS_DEV cheat_dev;
BOOL secondSearch = FALSE;
BOOL searched = FALSE;
HANDLE hThread = NULL;
BOOL doingLiveUpdate = FALSE;
HANDLE hLUMutex;
struct FIND_STATE {
DWORD address_prefix;
int shift;
};
struct FIND_STATE find_state = { 0 };
/////////////////////////////////////
// End of Global Variables
/////////////////////////////////////
/////////////////////////////////////
// Start of function implementation
/////////////////////////////////////
void Show_CheatSearchDlg(HWND hParent) {
// Ignore unused parameter
(void)hParent;
if (hCheatSearchDlg != NULL) { // Stop multiple instances
SetForegroundWindow(hCheatSearchDlg);
return;
}
// The third argument used to be hParent, this made the Dialog Window being created act as always on top of the parent window
// Setting this to NULL makes it behave as a "normal" window that can be behind the main application instead of always on top of
hCheatSearchDlg = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_Cheats_Search), NULL, CheatSearchDlgProc);
if (hCheatSearchDlg != NULL)
{
Setup_CheatSearch_Window(hCheatSearchDlg);
ShowWindow(hCheatSearchDlg, SW_SHOW);
}
}
void Close_CheatSearchDlg() {
CS_ClearResults(&results);
CS_ClearDev(&cheat_dev);
// Stop the live update
StopLiveUpdate();
if (hCheatSearchDlg != NULL) {
DestroyWindow(hCheatSearchDlg);
hCheatSearchDlg = NULL;
}
if (inFullScreen)
ShowCursor(FALSE);
}
void Defaults_CheatSearchDlg() {
searched = FALSE;
secondSearch = FALSE;
StopLiveUpdate();
Button_Enable(GetDlgItem(hCheatSearchDlg, IDB_CS_CHEAT_EDIT), FALSE);
Button_Enable(GetDlgItem(hCheatSearchDlg, IDB_CS_CHEAT_REMOVE), FALSE);
Button_Enable(GetDlgItem(hCheatSearchDlg, IDB_CS_CHEAT_MOVEUP), FALSE);
Button_Enable(GetDlgItem(hCheatSearchDlg, IDB_CS_CHEAT_MOVEDOWN), FALSE);
Button_Enable(GetDlgItem(hCheatSearchDlg, IDB_CS_CHEAT_ENABLE), FALSE);
Button_Enable(GetDlgItem(hCheatSearchDlg, IDB_CS_CHEAT_SAVE), FALSE);
}
char* trimwhitespace(char* str) {
char* end;
// Trim leading space
while (isspace(*str)) str++;
if (*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) end--;
// Write new null terminator
*(end + 1) = 0;
return str;
}
BOOL CALLBACK CheatSearchDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CLOSE:
Close_CheatSearchDlg();
break;
case WM_NOTIFY: {
switch (((LPNMHDR)lParam)->code) {
case NM_DBLCLK: {
switch (((LPNMHDR)lParam)->idFrom) {
case IDL_SEARCH_RESULT_LIST: {
int item = ((LPNMITEMACTIVATE)lParam)->iItem;
if (item == -1) {
break;
}
DWORD address = results.addresses[item];
CS_HIT hit = { 0 };
if (
(search.searchNumBits == bits8 && !CS_GetHitByte(&hit, &results, address)) ||
(search.searchNumBits == bits16 && !CS_GetHitWord(&hit, &results, address))
) {
// TODO: Error handling
break;
}
CODEENTRY tmp = { 0 };
tmp.Address = hit.address;
tmp.Value = hit.value;
tmp.numBits = search.searchNumBits;
tmp.Enabled = FALSE;
tmp.searchBy = search.searchBy;
strcpy(tmp.Name, "");
strcpy(tmp.Note, "");
strcpy(tmp.Text, "");
cheat_dev.modify = &tmp;
// Only save the code if okay was pressed
if (DialogBox(hInst, MAKEINTRESOURCE(IDD_Cheats_Search_Edit), hDlg, (DLGPROC)CheatSearch_Add_Proc) == IDOK) {
CS_AddCode(&cheat_dev, tmp);
UpdateCheatCreateList(hDlg);
}
break;
}
case IDL_CS_CHEAT_CREATE: { // Double click to allow Edit cheat shortcut
int item = ((LPNMITEMACTIVATE)lParam)->iItem;
CODEENTRY tmp = { 0 };
tmp.Address = 0;
tmp.Value = 0;
tmp.numBits = search.searchNumBits;
tmp.Enabled = FALSE;
tmp.searchBy = search.searchBy;
strcpy(tmp.Name, "");
strcpy(tmp.Note, "");
strcpy(tmp.Text, "");
cheat_dev.modify = &tmp;
// Double clicking on the empty space in the list for the cheat dev area will add a new entry
if (item == -1) {
if (DialogBox(hInst, MAKEINTRESOURCE(IDD_Cheats_Search_Edit), hDlg, (DLGPROC)CheatSearch_Add_Proc) == IDOK) {
tmp.numBits = search.searchNumBits;
CS_AddCode(&cheat_dev, tmp);
UpdateCheatCreateList(hDlg);
}
break;
}
// Quick sanity check, the cheat list and the list control should both line up
// If they do not then something horrible happened, either the list was deallocated or the cheat was removed
if (CS_GetCodeAt(&cheat_dev, item) == NULL)
break;
// Simulate the Edit button being clicked, it already has the appropriate code
SendMessage(GetDlgItem(hDlg, IDB_CS_CHEAT_EDIT), BM_CLICK, (WPARAM)NULL, (LPARAM)NULL);
break;
}
}
break;
} // End of NM_DBLCLK
case NM_CLICK: {
switch (((LPNMHDR)lParam)->idFrom) {
case IDL_CS_CHEAT_CREATE: // To toggle the buttons as the item is clicked in the cheat creation section
CE_UpdateControls(hDlg, ((LPNMITEMACTIVATE)lParam)->iItem);
break;
}
break;
}
case NM_CUSTOMDRAW: {
if (wParam == IDL_SEARCH_RESULT_LIST)
return CheatSearchResults_DrawItem(hDlg, (LPNMHDR)lParam);
break;
}
case LVN_GETDISPINFO: {
if (wParam == IDL_SEARCH_RESULT_LIST)
return CheatSearchResults_ReadItem((LPNMHDR)lParam);
break;
}
// Windows doesn't send the right search string with the `LVN_ODFINDITEM`
// notification for unknown reasons. For instance, when the same key is pressed
// twice, this notification ONLY receives a search string with a single character!
// When you press a different key following a potentially long string of identical
// characters, then suddenly this notification gets the full string.
//
// We want to avoid doing the search in `LVN_ODFINDITEM` because of this. Using the
// `LVN_INCREMENTALSEARCH` notification instead receives the proper search string.
//
// On the other hand, `LVN_INCREMENTALSEARCH` doesn't provide an audio indicator when
// the find fails.
//
// The workaround is setting a global `FIND_STATE` struct in `LVN_INCREMENTALSEARCH`,
// and reading it in `LVN_ODFINDITEM`.
case LVN_INCREMENTALSEARCH:
{
if (wParam == IDL_SEARCH_RESULT_LIST) {
NMLVFINDITEM *findInfo = (NMLVFINDITEM *)lParam;
if (findInfo->lvfi.flags & LVFI_STRING) {
find_state.address_prefix = strtoul(findInfo->lvfi.psz, NULL, 16);
find_state.shift = (6 - strlen(findInfo->lvfi.psz)) << 2;
}
}
break;
}
case LVN_ODFINDITEM: {
if (wParam == IDL_SEARCH_RESULT_LIST) {
SetWindowLong(hDlg, DWL_MSGRESULT, CheatSearchResults_FindItem((LPNMHDR)lParam));
return TRUE;
}
break;
}
}
break;
} // End of WM_NOTIFY
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case IDR_SEARCH_VALUE: {
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_TEXT), FALSE);
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_VALUE), TRUE);
Button_Enable(GetDlgItem(hDlg, IDC_CASE_SENSITIVE), FALSE);
break;
}
case IDR_SEARCH_TEXT: {
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_TEXT), TRUE);
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_VALUE), FALSE);
Button_Enable(GetDlgItem(hDlg, IDC_CASE_SENSITIVE), TRUE);
break;
}
case IDB_CS_SEARCH: {
Search(hDlg);
break;
}
case IDB_CS_RESET: {
ListView_SetItemCount(GetDlgItem(hDlg, IDL_SEARCH_RESULT_LIST), 0);
CS_ClearResults(&results);
Defaults_CheatSearchDlg();
InitSearchFormatList(hDlg);
ComboBox_Enable(GetDlgItem(hDlg, IDC_SEARCH_NUMBITS), TRUE);
ComboBox_Enable(GetDlgItem(hDlg, IDC_ADDRESS_RANGE), TRUE);
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_VALUE), TRUE);
Edit_SetText(GetDlgItem(hDlg, IDT_SEARCH_VALUE), "");
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_TEXT), FALSE);
Edit_SetText(GetDlgItem(hDlg, IDT_SEARCH_TEXT), "");
Button_SetCheck(GetDlgItem(hDlg, IDR_SEARCH_VALUE), BST_CHECKED);
Button_SetCheck(GetDlgItem(hDlg, IDR_SEARCH_TEXT), BST_UNCHECKED);
Button_Enable(GetDlgItem(hDlg, IDC_CASE_SENSITIVE), FALSE);
Button_SetCheck(GetDlgItem(hDlg, IDC_CASE_SENSITIVE), BST_CHECKED);
SetWindowText(GetDlgItem(hDlg, IDGB_CS_RESULTS), "Results");
break;
}
case IDB_CS_DUMPMEM: {
Show_MemDumpDlg(hDlg);
//SaveMemDump(hDlg);
//DumpPCAndDisassembled(hDlg, 0x80000400, 0x80001000);
break;
}
case IDC_SEARCH_HEXDEC: {
if (HIWORD(wParam) == CBN_SELCHANGE) {
search.searchType = (SEARCHTYPE)ComboBox_GetCurSel((HWND)lParam);
switch (search.searchType) {
case dec:
case hex:
CS_UpdateSearchProc(hDlg);
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_VALUE), TRUE);
Edit_SetText(GetDlgItem(hDlg, IDT_SEARCH_VALUE), "");
break;
case unknown:
case changed:
case unchanged:
case lower:
case higher:
Edit_Enable(GetDlgItem(hDlg, IDT_SEARCH_VALUE), FALSE);
Edit_SetText(GetDlgItem(hDlg, IDT_SEARCH_VALUE), "");
break;
}
}
break;
}
case IDC_SEARCH_NUMBITS: {
if (HIWORD(wParam) == CBN_SELCHANGE) {
search.searchNumBits = (NUMBITS)ComboBox_GetCurSel((HWND)lParam);
CS_UpdateSearchProc(hDlg);
Edit_SetText(GetDlgItem(hDlg, IDT_SEARCH_VALUE), "");
}
break;
}
case IDCANCEL: {
EndDialog(hDlg, IDCANCEL);
break;
}
case IDB_CS_CHEAT_EDIT: {
int sItem = ListView_GetNextItem(GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE), -1, LVNI_FOCUSED);
CODEENTRY* cItem = CS_GetCodeAt(&cheat_dev, sItem);
// Should not happen (Out of bounds)
if (sItem == -1 || cItem == NULL) break;
cheat_dev.modify = cItem; // Address of item to be modified
if (DialogBox(hInst, MAKEINTRESOURCE(IDD_Cheats_Search_Edit), hDlg, (DLGPROC)CheatSearch_Add_Proc) == IDOK) {
UpdateCheatCreateList(hDlg);
CE_UpdateControls(hDlg, sItem);
}
break;
}
case IDB_CS_CHEAT_REMOVE: {
HANDLE hWnd = GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE);
int count = ListView_GetSelectedCount(hWnd);
int next = -1;
for (int i = 0; i < count; i++) {
next = ListView_GetNextItem(hWnd, next, LVNI_SELECTED);
CS_RemoveCodeAt(&cheat_dev, next - i);
}
UpdateCheatCreateList(hDlg);
CE_UpdateControls(hDlg, -1);
break;
}
case IDB_CS_CHEAT_MOVEUP: {
int item = ListView_GetNextItem(GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE), -1, LVNI_SELECTED);
// Nothing selected or already top-most item
if (item == -1 || item == 0)
break;
CS_SwapDev(&cheat_dev, item, item - 1);
// Repopulate the list and update controls
UpdateCheatCreateList(hDlg);
CE_UpdateControls(hDlg, item - 1);
break;
}
case IDB_CS_CHEAT_MOVEDOWN: {
int item = ListView_GetNextItem(GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE), -1, LVNI_SELECTED);
// Nothing selected
if (item == -1)
break;
CS_SwapDev(&cheat_dev, item, item + 1);
// Repopulate the list and update controls
UpdateCheatCreateList(hDlg);
CE_UpdateControls(hDlg, item + 1);
break;
}
case IDB_CS_CHEAT_ENABLE: {
int item = ListView_GetNextItem(GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE), -1, LVNI_SELECTED);
CODEENTRY* cheat;
if (item == -1)
break;
cheat = CS_GetCodeAt(&cheat_dev, item);
if (cheat != NULL)
cheat->Enabled = !cheat->Enabled;
// Repopulate the list and update controls
UpdateCheatCreateList(hDlg);
CE_UpdateControls(hDlg, item);
break;
}
case IDB_CS_CHEAT_SAVE: {
int item = ListView_GetNextItem(GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE), -1, LVNI_SELECTED);
if (item == -1)
break;
CE_SaveCheat(item);
MessageBox(hDlg, "Cheat has been added to the database.\nAccess through normal cheat window.", "Cheat Saved", MB_OK);
UpdateCheatCreateList(hDlg);
CE_UpdateControls(hDlg, -1); // Save eats the item so default to nothing
break;
}
case IDB_CS_CHEAT_MEMVIEWER:
{
Create_Memory_Window(FALSE);
break;
}
case IDT_SEARCH_VALUE: {
if (HIWORD(wParam) == EN_CHANGE) {
char strVal[8];
Edit_GetText((HWND)lParam, strVal, 8);
if (search.searchType == hex) {
VALUE_SEARCH.value = AsciiToHex(strVal);
if (VALUE_SEARCH.value > VALUE_SEARCH.max_value)
VALUE_SEARCH.value = VALUE_SEARCH.max_value;
sprintf_s(strVal, 7, "%X", VALUE_SEARCH.value);
}
else {
VALUE_SEARCH.value = atoi(strVal);
if (VALUE_SEARCH.value > VALUE_SEARCH.max_value)
VALUE_SEARCH.value = VALUE_SEARCH.max_value;
sprintf_s(strVal, 7, "%u", VALUE_SEARCH.value);
}
Edit_SetText((HWND)lParam, strVal);
Edit_SetSel((HWND)lParam, strlen(strVal), strlen(strVal));
}
break;
}
}
break;
} // End of WM_COMMAND
case WM_LV_UPDATE: {
InvalidateRect(GetDlgItem(hDlg, IDL_SEARCH_RESULT_LIST), NULL, TRUE);
break;
}
default:
return FALSE;
}
return TRUE;
}
void Setup_CheatSearch_Window(HWND hParent) {
DWORD X, Y, dwStyle;
DWORD WindowWidth = 683;
DWORD WindowHeight = 341;
Defaults_CheatSearchDlg(); // Load sane defaults
CS_InitDev(&cheat_dev);
CheckRadioButton(hParent, IDR_SEARCH_VALUE, IDR_SEARCH_TEXT, IDR_SEARCH_VALUE);
Button_SetCheck(GetDlgItem(hParent, IDC_CASE_SENSITIVE), BST_CHECKED);
if (RdramSize == 0x400000) {
Edit_SetText(GetDlgItem(hParent, IDS_RDRAMSIZE), "RDRam size: 4MB");
Edit_SetText(GetDlgItem(hParent, IDT_ADDRESS_START), "0x00000000");
Edit_SetText(GetDlgItem(hParent, IDT_ADDRESS_END), "0x003FFFFF");
}
else {
Edit_SetText(GetDlgItem(hParent, IDS_RDRAMSIZE), "RDRam size: 8MB");
Edit_SetText(GetDlgItem(hParent, IDT_ADDRESS_START), "0x00000000");
Edit_SetText(GetDlgItem(hParent, IDT_ADDRESS_END), "0x007FFFFF");
}
InitSearchFormatList(hParent);
InitSearchNumBitsList(hParent);
CS_UpdateSearchProc(hParent);
dwStyle = ListView_GetExtendedListViewStyle(GetDlgItem(hParent, IDL_SEARCH_RESULT_LIST));
dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER;
ListView_SetExtendedListViewStyle(GetDlgItem(hParent, IDL_SEARCH_RESULT_LIST), dwStyle);
InitSearchResultList(hParent);
dwStyle = ListView_GetExtendedListViewStyle(GetDlgItem(hParent, IDL_CS_CHEAT_CREATE));
dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER;
ListView_SetExtendedListViewStyle(GetDlgItem(hParent, IDL_CS_CHEAT_CREATE), dwStyle);
InitCheatCreateList(hParent);
X = 0;
Y = 0;
if (!GetStoredWinPos("CheatSearch", &X, &Y)) {
X = (GetSystemMetrics(SM_CXSCREEN) - WindowWidth) / 2;
Y = (GetSystemMetrics(SM_CYSCREEN) - WindowHeight) / 2;
}
SetWindowPos(hParent, NULL, X, Y, WindowWidth, WindowHeight, SWP_NOZORDER | SWP_SHOWWINDOW | SWP_NOSIZE);
}
void InitSearchFormatList(HWND hDlg) {
HWND hComboBox;
int prevSelection;
hComboBox = GetDlgItem(hDlg, IDC_SEARCH_HEXDEC);
prevSelection = ComboBox_GetCurSel(hComboBox);
ComboBox_ResetContent(hComboBox);
ComboBox_AddString(hComboBox, "Unknown");
ComboBox_AddString(hComboBox, "Decimal");
ComboBox_AddString(hComboBox, "Hexadecimal");
if (!searched) {
search.searchType = dec;
Edit_SetText(GetDlgItem(hDlg, IDT_SEARCH_VALUE), "0");
}
else {
ComboBox_AddString(hComboBox, "Changed");
ComboBox_AddString(hComboBox, "Unchanged");
ComboBox_AddString(hComboBox, "Higher");
ComboBox_AddString(hComboBox, "Lower");
if (prevSelection == CB_ERR)
search.searchType = unknown;
}
ComboBox_SetCurSel(hComboBox, search.searchType);
}
void InitSearchNumBitsList(HWND hDlg) {
HWND hComboBox;
hComboBox = GetDlgItem(hDlg, IDC_SEARCH_NUMBITS);
ComboBox_ResetContent(hComboBox);
ComboBox_AddString(hComboBox, "8bit");
ComboBox_AddString(hComboBox, "16bit");
search.searchNumBits = bits8;
ComboBox_SetCurSel(hComboBox, search.searchNumBits);
}
void InitSearchResultList(HWND hDlg) {
LV_COLUMN col;
HWND hListView;
long state;
hListView = GetDlgItem(hDlg, IDL_SEARCH_RESULT_LIST);
ListView_SetItemCount(hListView, 0);
col.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
col.fmt = LVCFMT_LEFT;
// Clear and remove any previous incarnations of the ListView control
while (ListView_DeleteColumn(hListView, 0));
state = Button_GetCheck(GetDlgItem(hDlg, IDR_SEARCH_TEXT));
if (state == BST_CHECKED) {
// Build the results list for Text Search
col.pszText = "Address";
col.cx = 110;
col.iSubItem = 0;
ListView_InsertColumn(hListView, 0, &col);
col.pszText = "Text";
col.cx = 150;
col.iSubItem = 1;
ListView_InsertColumn(hListView, 1, &col);
col.pszText = "Old Text";
col.cx = 150;
col.iSubItem = 2;
ListView_InsertColumn(hListView, 2, &col);
}
else {
// Build the results list for Value
col.pszText = "Address";
col.cx = 110;
col.iSubItem = 0;
ListView_InsertColumn(hListView, 0, &col);
col.pszText = "Value (Hex)";
col.cx = 70;
col.iSubItem = 1;
ListView_InsertColumn(hListView, 1, &col);
col.pszText = "Value (Dec)";
col.cx = 70;
col.iSubItem = 2;
ListView_InsertColumn(hListView, 2, &col);
col.pszText = "Old (Hex)";
col.cx = 70;
col.iSubItem = 3;
ListView_InsertColumn(hListView, 3, &col);
}
}
void PopulateSearchResultList(HWND hDlg) {
char title[STRING_MAX];
ListView_SetItemCount(GetDlgItem(hDlg, IDL_SEARCH_RESULT_LIST), results.num_stored);
sprintf(title, "Results (found %u)", results.num_stored);
SetWindowText(GetDlgItem(hDlg, IDGB_CS_RESULTS), title);
}
void InitCheatCreateList(HWND hDlg) {
HWND hListView;
LVCOLUMN col;
hListView = GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE);
col.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER;
col.iOrder = 0;
col.pszText = "Act.";
col.fmt = LVCFMT_IMAGE;
col.cx = 40;
col.iSubItem = 0;
ListView_InsertColumn(hListView, 0, &col);
col.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER;
col.iOrder = 1;
col.pszText = "Code";
col.fmt = LVCFMT_LEFT;
col.cx = 100;
col.iSubItem = 1;
ListView_InsertColumn(hListView, 1, &col);
col.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER;
col.iOrder = 2;
col.pszText = ROM_NAME;
col.fmt = LVCFMT_LEFT;
col.cx = 220;
col.iSubItem = 2;
ListView_InsertColumn(hListView, 2, &col);
}
void UpdateCheatCreateList(HWND hDlg) {
HWND hListView;
LV_ITEM item;
int ctr;
char s[14];
CODEENTRY* curr;
// Add to the list view
hListView = GetDlgItem(hDlg, IDL_CS_CHEAT_CREATE);
ListView_DeleteAllItems(hListView);
ctr = 0;
curr = CS_GetCodeAt(&cheat_dev, 0);
while (curr != NULL) {
item.mask = LVIF_TEXT;
if (curr->Enabled)
item.pszText = "On";
else
item.pszText = "Off";
item.iItem = ctr;
item.iSubItem = 0;
ListView_InsertItem(hListView, &item);
if (strlen(curr->Text) > 0) {
sprintf(s, "80%06X ...", curr->Address);
} else if (curr->numBits == bits8) {
sprintf(s, "%02X%06X %02X", curr->Activator, curr->Address, curr->Value);
} else {
sprintf(s, "%02X%06X %04X", curr->Activator, curr->Address, curr->Value);
}
item.pszText = s;
item.iSubItem = 1;
ListView_SetItem(hListView, &item);
item.pszText = curr->Name;
item.iSubItem = 2;
ListView_SetItem(hListView, &item);
// Increment to the next item
ctr++;
curr = CS_GetCodeAt(&cheat_dev, ctr);
}
}
void Search(HWND hDlg) {
char startAddress[11], endAddress[11];
DWORD dwstartAddress, dwendAddress, count;
StopLiveUpdate();
// Reset the results window, this depends on what was ticked.
InitSearchResultList(hDlg);
// Address search range
Edit_GetText(GetDlgItem(hDlg, IDT_ADDRESS_START), startAddress, 11);
Edit_GetText(GetDlgItem(hDlg, IDT_ADDRESS_END), endAddress, 11);
dwstartAddress = strtoul(startAddress, NULL, 16);
dwendAddress = strtoul(endAddress, NULL, 16) + 1;
// Value search
if (Button_GetCheck(GetDlgItem(hDlg, IDR_SEARCH_VALUE)) == BST_CHECKED) {
WORD searchValue, memValue;
search.searchBy = searchbyvalue;
// Value to search by, it's either in Hex or Dec
Edit_GetText(GetDlgItem(hDlg, IDT_SEARCH_VALUE), search.search_string, 9);
if (search.searchType == hex)
searchValue = (WORD)strtoul(search.search_string, NULL, 16);
else
searchValue = (WORD)strtoul(search.search_string, NULL, 10);
// The initial search will either add all addresses (unknown search)
// or addresses that match the search value (8bit or 16bit)
if (!searched) {
// Reserve space for the initial search
CS_ReserveSpace(&results, dwendAddress - dwstartAddress);
if (search.searchNumBits == bits8) {
for (count = dwstartAddress; count < dwendAddress; count++) {
memValue = Value_ReadByte(count);
if (search.searchType == unknown || ((BYTE)searchValue == memValue))
CS_AddResultByte(&results, count, (BYTE)memValue);
}
}
else {
for (count = dwstartAddress; count < dwendAddress; count += 2) {
memValue = Value_ReadWord(count);
if (search.searchType == unknown || (searchValue == memValue))
CS_AddResultWord(&results, count, memValue);
}
}
if (search.searchType != unknown)
searched = TRUE;
ComboBox_Enable(GetDlgItem(hDlg, IDC_SEARCH_NUMBITS), FALSE);
ComboBox_Enable(GetDlgItem(hDlg, IDC_ADDRESS_RANGE), FALSE);
}
// Search performed using previous (or the initial) search result list
else {
struct CS_RESULTS tmp = { 0 };
WORD mem_value;
CS_HIT hit = { 0 };
BOOL matched;
DWORD address;
CS_ReserveSpace(&tmp, dwendAddress - dwstartAddress);
// Subsequent searches of Hex/Dec values
for (count = 0; count < results.num_stored; count++) {
address = results.addresses[count];
if (
(search.searchNumBits == bits8 && !CS_GetHitByte(&hit, &results, address)) ||
(search.searchNumBits == bits16 && !CS_GetHitWord(&hit, &results, address))
) {
// Attempted to access memory out of bounds
// TODO: Show error message
return;
}
if (search.searchNumBits == bits8) {
mem_value = Value_ReadByte(hit.address);
} else {
mem_value = Value_ReadWord(hit.address);
}
switch (search.searchType) {
case dec:
case hex:
matched = (searchValue == mem_value);
break;
case unknown:
matched = TRUE;
break;
case changed:
matched = (hit.value != mem_value);
break;
case unchanged:
matched = (hit.value == mem_value);
break;
case lower:
matched = (hit.value > mem_value);
break;
case higher:
matched = (hit.value < mem_value);
break;
default:
matched = FALSE;
break;
}
if (!matched)
continue;
hit.prev_value = hit.value;
hit.value = mem_value;
if (search.searchNumBits == bits8) {
CS_AddHitByte(&tmp, &hit);
} else {
CS_AddHitWord(&tmp, &hit);
}
}
secondSearch = TRUE;
CS_ClearResults(&results);
results = tmp;
}
searched = TRUE;
}
// Text search
else if (Button_GetCheck(GetDlgItem(hDlg, IDR_SEARCH_TEXT)) == BST_CHECKED) {
DWORD possibleAddress, count2, textsearch_len;
search.searchBy = searchbytext;
CS_ReserveSpace(&results, dwendAddress - dwstartAddress);
possibleAddress = 0;
// Fetch the string being searched for
Edit_GetText(GetDlgItem(hDlg, IDT_SEARCH_TEXT), search.search_string, STRING_MAX - 1);
BOOL case_sensitive = ((Button_GetCheck(GetDlgItem(hDlg, IDC_CASE_SENSITIVE)) & BST_CHECKED) == BST_CHECKED);
textsearch_len = strlen(search.search_string);
// Case insensitive searches always convert the string to lowercase
if (!case_sensitive) {
for (DWORD i = 0; i < textsearch_len; i++) {
search.search_string[i] = (char)tolower(search.search_string[i]);
}
}
for (count = dwstartAddress, count2 = 0; count < dwendAddress; count++) {
// Pattern match
BYTE ch = Text_ReadByte(count);
if ((BYTE)search.search_string[count2] == (case_sensitive ? ch : (BYTE)tolower(ch))) {
if (count2 == 0) {
possibleAddress = count;
}
count2++;
if (count2 == textsearch_len) {
CS_AddTextResult(&results, possibleAddress, search.search_string);
count2 = 0;
}
}
else
count2 = 0;
}
}
else
return;
PopulateSearchResultList(hDlg);
// Update to include changed, unchanged, higher, lower, etc...
InitSearchFormatList(hDlg);
// The Live Update thread (hThread should always be NULL since Live Update is stopped before searching)
if (hThread == NULL) {
hThread = CreateThread(NULL, 0, LiveUpdate, hDlg, 0, NULL);
}
}
LRESULT CALLBACK CheatSearch_Add_Proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
HWND hComboBox;
int Count;
char strAddress[9], strValue[7];
switch (uMsg) {
case WM_INITDIALOG: {
// Populate ComboBox with GS Cheat Types
hComboBox = GetDlgItem(hDlg, IDC_CSE_GSCODETYPE);
ComboBox_ResetContent(hComboBox);
// Subclass HexEditControls for Address
ADDR_HEX.hWnd = GetDlgItem(hDlg, IDT_CSE_ADDRESS);
ADDR_HEX.callBack = (WNDPROC)SetWindowLongPtr(ADDR_HEX.hWnd, GWLP_WNDPROC, (LONG_PTR)HexEditControlProc);
ADDR_HEX.value = cheat_dev.modify->Address;
ADDR_HEX.max_value = RdramSize - 1;
if (cheat_dev.modify->searchBy == searchbyvalue) {
Edit_Enable(GetDlgItem(hDlg, IDT_CSE_VALUE_HEX), TRUE);
Edit_Enable(GetDlgItem(hDlg, IDT_CSE_VALUE_DEC), TRUE);
Edit_Enable(GetDlgItem(hDlg, IDT_CSE_TEXT), FALSE);
Edit_Enable(GetDlgItem(hDlg, IDT_CSE_PREVIEW), TRUE);
switch (cheat_dev.modify->numBits) {
int loc;
char check[3];
case bits8:
for (Count = 0, loc = 0; Count < 5; Count++) {
ComboBox_AddString(hComboBox, N64GSCODETYPES8BIT[Count * 2]);
sprintf(check, "%2X", cheat_dev.modify->Activator);
if (strcmp(check, N64GSCODETYPES8BIT[Count * 2 + 1]) == 0)
loc = Count;
}
ComboBox_SetCurSel(hComboBox, loc);
break;
case bits16:
for (Count = 0, loc = 0; Count < 5; Count++) {
ComboBox_AddString(hComboBox, N64GSCODETYPES16BIT[Count * 2]);
sprintf(check, "%2X", cheat_dev.modify->Activator);
if (strcmp(check, N64GSCODETYPES16BIT[Count * 2 + 1]) == 0)
loc = Count;
}
ComboBox_SetCurSel(hComboBox, loc);
break;
}
// Subclass HexEditControls for Hex Value (Linked with VALUE_DEC later on)
VALUE_HEX.hWnd = GetDlgItem(hDlg, IDT_CSE_VALUE_HEX);
VALUE_HEX.callBack = (WNDPROC)SetWindowLongPtr(VALUE_HEX.hWnd, GWLP_WNDPROC, (LONG_PTR)HexEditControlProc);
VALUE_HEX.value = cheat_dev.modify->Value;
if (cheat_dev.modify->numBits == bits8)
VALUE_HEX.max_value = 0xFF;
else
VALUE_HEX.max_value = 0xFFFF;