/
guiinit.cpp
3732 lines (3235 loc) · 123 KB
/
guiinit.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
// initialize menus & dialogs, etc.
// for most of the prefs dialogs, all code resides here in the form of
// event handlers & validators
// other non-viewer dialogs are at least validated enough that they won't crash
// viewer dialogs are not commonly used, so they are initialized on demand
#include "wxvbam.h"
#include <algorithm>
#include <stdexcept>
#include <typeinfo>
#include <wx/checkedlistctrl.h>
#include <wx/clrpicker.h>
#include <wx/dir.h>
#include <wx/filepicker.h>
#include <wx/progdlg.h>
#include <wx/spinctrl.h>
#include <wx/stockitem.h>
#include <wx/tokenzr.h>
#include <wx/txtstrm.h>
#include <wx/wfstream.h>
#include "../common/ConfigManager.h"
#include "../gba/CheatSearch.h"
// The program icon, in case it's missing from .xrc (MSW gets it from .rc file)
#if !defined(__WXMSW__) && !defined(__WXPM__)
// ImageMagick makes the name wxvbam, but wx expects wxvbam_xpm
#define wxvbam wxvbam_xpm
const
#include "xrc/vbam.xpm"
#undef wxvbam
#endif
// this is supposed to happen automatically if a parent is marked recursive
// but some dialogs don't do it (propertydialog?)
// so go ahead and mark all dialogs for fully recursive validation
static void
mark_recursive(wxWindowBase* w)
{
w->SetExtraStyle(w->GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY);
wxWindowList l = w->GetChildren();
for (wxWindowList::iterator ch = l.begin(); ch != l.end(); ++ch)
mark_recursive(*ch);
}
#if (wxMAJOR_VERSION < 3)
#define GetXRCDialog(n) \
wxStaticCast(wxGetApp().frame->FindWindow(XRCID(n)), wxDialog)
#else
#define GetXRCDialog(n) \
wxStaticCast(wxGetApp().frame->FindWindowByName(n), wxDialog)
#endif
// Event handlers must be methods of wxEvtHandler-derived objects
// manage the network link dialog
#ifndef NO_LINK
static class NetLink_t : public wxEvtHandler {
public:
wxDialog* dlg;
int n_players;
bool server;
NetLink_t()
: n_players(2)
, server(false)
{
}
wxButton* okb;
void ServerOKButton(wxCommandEvent& ev)
{
okb->SetLabel(_("Start!"));
}
void ClientOKButton(wxCommandEvent& ev)
{
okb->SetLabel(_("Connect"));
}
// attached to OK, so skip when OK
void NetConnect(wxCommandEvent& ev)
{
static const int length = 256;
if (!dlg->Validate() || !dlg->TransferDataFromWindow())
return;
if (!server) {
bool valid = SetLinkServerHost(gopts.link_host.mb_str());
if (!valid) {
wxMessageBox(_("You must enter a valid host name"),
_("Host name invalid"), wxICON_ERROR | wxOK);
return;
}
}
linkNumPlayers = n_players;
update_opts(); // save fast flag and client host
// Close any previous link
CloseLink();
wxString connmsg;
wxString title;
SetLinkTimeout(linkTimeout);
EnableSpeedHacks(linkHacks);
EnableLinkServer(server, linkNumPlayers - 1);
if (server) {
char host[length];
GetLinkServerHost(host, length);
title.Printf(_("Waiting for clients..."));
connmsg.Printf(_("Server IP address is: %s\n"), wxString(host, wxConvLibc).mb_str());
} else {
title.Printf(_("Waiting for connection..."));
connmsg.Printf(_("Connecting to %s\n"), gopts.link_host.mb_str());
}
// Init link
MainFrame* mf = wxGetApp().frame;
ConnectionState state = InitLink(mf->GetConfiguredLinkMode());
// Display a progress dialog while the connection is establishing
if (state == LINK_NEEDS_UPDATE) {
wxProgressDialog pdlg(title, connmsg,
100, dlg, wxPD_APP_MODAL | wxPD_CAN_ABORT | wxPD_ELAPSED_TIME);
while (state == LINK_NEEDS_UPDATE) {
// Ask the core for updates
char message[length];
state = ConnectLinkUpdate(message, length);
connmsg = wxString(message, wxConvLibc);
// Does the user want to abort?
if (!pdlg.Pulse(connmsg)) {
state = LINK_ABORT;
}
}
}
// The user canceled the connection attempt
if (state == LINK_ABORT) {
CloseLink();
}
// Something failed during init
if (state == LINK_ERROR) {
CloseLink();
wxLogError(_("Error occurred.\nPlease try again."));
}
if (GetLinkMode() != LINK_DISCONNECTED) {
connmsg.Replace(wxT("\n"), wxT(" "));
systemScreenMessage(connmsg);
ev.Skip(); // all OK
}
}
} net_link_handler;
#endif
// manage the cheat list dialog
static class CheatList_t : public wxEvtHandler {
public:
wxDialog* dlg;
wxCheckedListCtrl* list;
wxListItem item0, item1;
int col1minw;
wxString cheatdir, cheatfn, deffn;
bool isgb;
bool* dirty;
// add/edit dialog
wxString ce_desc;
wxString ce_codes;
wxChoice* ce_type_ch;
wxControl* ce_codes_tc;
int ce_type;
void Reload()
{
list->DeleteAllItems();
Reload(0);
}
void Reload(int start)
{
if (isgb) {
for (int i = start; i < gbCheatNumber; i++) {
item0.SetId(i);
item0.SetText(wxString(gbCheatList[i].cheatCode, wxConvLibc));
list->InsertItem(item0);
item1.SetId(i);
item1.SetText(wxString(gbCheatList[i].cheatDesc, wxConvUTF8));
list->SetItem(item1);
list->Check(i, gbCheatList[i].enabled);
}
} else {
for (int i = start; i < cheatsNumber; i++) {
item0.SetId(i);
item0.SetText(wxString(cheatsList[i].codestring, wxConvLibc));
list->InsertItem(item0);
item1.SetId(i);
item1.SetText(wxString(cheatsList[i].desc, wxConvUTF8));
list->SetItem(item1);
list->Check(i, cheatsList[i].enabled);
}
}
AdjustDescWidth();
}
void Tool(wxCommandEvent& ev)
{
switch (ev.GetId()) {
case wxID_OPEN: {
wxFileDialog subdlg(dlg, _("Select cheat file"), cheatdir, cheatfn,
_("VBA cheat lists (*.clt)|*.clt|CHT cheat lists (*.cht)|*.cht"),
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
int ret = subdlg.ShowModal();
cheatdir = subdlg.GetDirectory();
cheatfn = subdlg.GetPath();
if (ret != wxID_OK)
break;
bool cld;
if (isgb)
cld = gbCheatsLoadCheatList(cheatfn.mb_fn_str());
else {
if (cheatfn.EndsWith(wxT(".clt"))) {
cld = cheatsLoadCheatList(cheatfn.mb_fn_str());
if (cld) {
*dirty = cheatfn != deffn;
systemScreenMessage(_("Loaded cheats"));
} else
*dirty = true; // attempted load always clears
} else {
// cht format
wxFileInputStream input(cheatfn);
wxTextInputStream text(input, wxT("\x09"), wxConvUTF8);
wxString cheat_desc = wxT("");
while (input.IsOk() && !input.Eof()) {
wxString line = text.ReadLine().Trim();
if (line.Contains(wxT("[")) && !line.Contains(wxT("="))) {
cheat_desc = line.AfterFirst('[').BeforeLast(']');
}
if (line.Contains(wxT("=")) && cheat_desc != wxT("GameInfo")) {
while ((input.IsOk() && !input.Eof()) && (line.EndsWith(wxT(";")) || line.EndsWith(wxT(",")))) {
line = line + text.ReadLine().Trim();
}
ParseChtLine(cheat_desc, line);
}
}
*dirty = true;
}
}
Reload();
} break;
case wxID_SAVE: {
wxFileDialog subdlg(dlg, _("Select cheat file"), cheatdir,
cheatfn, _("VBA cheat lists (*.clt)|*.clt"),
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
int ret = subdlg.ShowModal();
cheatdir = subdlg.GetDirectory();
cheatfn = subdlg.GetPath();
if (ret != wxID_OK)
break;
// note that there is no way to test for succes of save
if (isgb)
gbCheatsSaveCheatList(cheatfn.mb_fn_str());
else
cheatsSaveCheatList(cheatfn.mb_fn_str());
if (cheatfn == deffn)
*dirty = false;
systemScreenMessage(_("Saved cheats"));
} break;
case wxID_ADD: {
int ncheats = isgb ? gbCheatNumber : cheatsNumber;
ce_codes = wxEmptyString;
wxDialog* subdlg = GetXRCDialog("CheatEdit");
dlg->SetWindowStyle(wxCAPTION | wxRESIZE_BORDER);
if (gopts.keep_on_top)
subdlg->SetWindowStyle(subdlg->GetWindowStyle() | wxSTAY_ON_TOP);
else
subdlg->SetWindowStyle(subdlg->GetWindowStyle() & ~wxSTAY_ON_TOP);
subdlg->ShowModal();
AddCheat();
Reload(ncheats);
} break;
case wxID_REMOVE: {
bool asked = false, restore;
for (int i = list->GetItemCount() - 1; i >= 0; i--)
if (list->GetItemState(i, wxLIST_STATE_SELECTED)) {
list->DeleteItem(i);
if (isgb)
gbCheatRemove(i);
else {
if (!asked) {
asked = true;
restore = wxMessageBox(_("Restore old values?"),
_("Removing cheats"),
wxYES_NO | wxICON_QUESTION)
== wxYES;
}
cheatsDelete(i, restore);
}
}
} break;
case wxID_CLEAR:
if (isgb) {
if (gbCheatNumber) {
*dirty = true;
gbCheatRemoveAll();
}
} else {
if (cheatsNumber) {
bool restore = wxMessageBox(_("Restore old values?"),
_("Removing cheats"),
wxYES_NO | wxICON_QUESTION)
== wxYES;
*dirty = true;
cheatsDeleteAll(restore);
}
}
Reload();
break;
case wxID_SELECTALL:
// FIXME: probably ought to limit to selected items if any items
// are selected
*dirty = true;
if (isgb) {
int i;
for (i = 0; i < gbCheatNumber; i++)
if (!gbCheatList[i].enabled)
break;
if (i < gbCheatNumber)
for (; i < gbCheatNumber; i++) {
gbCheatEnable(i);
list->Check(i, true);
}
else
for (i = 0; i < gbCheatNumber; i++) {
gbCheatDisable(i);
list->Check(i, false);
}
} else {
int i;
for (i = 0; i < cheatsNumber; i++)
if (!cheatsList[i].enabled)
break;
if (i < cheatsNumber)
for (; i < cheatsNumber; i++) {
cheatsEnable(i);
list->Check(i, true);
}
else
for (i = 0; i < cheatsNumber; i++) {
cheatsDisable(i);
list->Check(i, false);
}
}
break;
}
}
void Check(wxListEvent& ev)
{
int ch = ev.GetIndex();
if (isgb) {
if (!gbCheatList[ch].enabled) {
gbCheatEnable(ev.GetIndex());
*dirty = true;
}
} else {
if (!cheatsList[ch].enabled) {
cheatsEnable(ev.GetIndex());
*dirty = true;
}
}
}
void UnCheck(wxListEvent& ev)
{
int ch = ev.GetIndex();
if (isgb) {
if (gbCheatList[ch].enabled) {
gbCheatDisable(ev.GetIndex());
*dirty = true;
}
} else {
if (cheatsList[ch].enabled) {
cheatsDisable(ev.GetIndex());
*dirty = true;
}
}
}
void AddCheat()
{
wxStringTokenizer tk(ce_codes.MakeUpper());
while (tk.HasMoreTokens()) {
wxString tok = tk.GetNextToken();
if (isgb) {
if (!ce_type)
gbAddGsCheat(tok.mb_str(), ce_desc.mb_str());
else
gbAddGgCheat(tok.mb_str(), ce_desc.mb_str());
} else {
// Flashcart CHT format
if (tok.Contains(wxT("="))) {
ParseChtLine(ce_desc, tok);
}
// Generic Code
else if (tok.Contains(wxT(":")))
cheatsAddCheatCode(tok.mb_str(), ce_desc.mb_str());
// following determination of type by lengths is
// same used by win32 and gtk code
// and like win32/gtk code, user-chosen fmt is ignored
else if (tok.size() == 12) {
tok = tok.substr(0, 8) + wxT(' ') + tok.substr(8);
cheatsAddCBACode(tok.mb_str(), ce_desc.mb_str());
} else if (tok.size() == 16)
// not sure why 1-tok is !v3 and 2-tok is v3..
cheatsAddGSACode(tok.mb_str(), ce_desc.mb_str(), false);
// CBA codes are assumed to be N+4, and anything else
// is assumed to be GSA v3 (although I assume the
// actual formats should be 8+4 and 8+8)
else {
if (!tk.HasMoreTokens()) {
// throw an error appropriate to chosen type
if (ce_type == 1) // GSA
cheatsAddGSACode(tok.mb_str(), ce_desc.mb_str(), false);
else
cheatsAddCBACode(tok.mb_str(), ce_desc.mb_str());
} else {
wxString tok2 = tk.GetNextToken();
if (tok2.size() == 4) {
tok += wxT(' ') + tok2;
cheatsAddCBACode(tok.mb_str(), ce_desc.mb_str());
} else {
tok += tok2;
cheatsAddGSACode(tok.mb_str(), ce_desc.mb_str(), true);
}
}
}
}
}
}
void ParseChtLine(wxString desc, wxString tok);
void Edit(wxListEvent& ev)
{
int id = ev.GetIndex();
// GetItem() followed by GetText doesn't work, so retrieve from
// source
wxString odesc, ocode;
bool ochecked;
int otype;
if (isgb) {
ochecked = gbCheatList[id].enabled;
ce_codes = ocode = wxString(gbCheatList[id].cheatCode, wxConvLibc);
ce_desc = odesc = wxString(gbCheatList[id].cheatDesc, wxConvUTF8);
if (ce_codes.find(wxT('-')) == wxString::npos)
otype = ce_type = 0;
else
otype = ce_type = 1;
} else {
ochecked = cheatsList[id].enabled;
ce_codes = ocode = wxString(cheatsList[id].codestring, wxConvLibc);
ce_desc = odesc = wxString(cheatsList[id].desc, wxConvUTF8);
if (ce_codes.find(wxT(':')) != wxString::npos)
otype = ce_type = 0;
else if (ce_codes.find(wxT(' ')) == wxString::npos)
otype = ce_type = 1;
else
otype = ce_type = 2;
}
wxDialog* subdlg = GetXRCDialog("CheatEdit");
dlg->SetWindowStyle(wxCAPTION | wxRESIZE_BORDER);
if (gopts.keep_on_top)
subdlg->SetWindowStyle(subdlg->GetWindowStyle() | wxSTAY_ON_TOP);
else
subdlg->SetWindowStyle(subdlg->GetWindowStyle() & ~wxSTAY_ON_TOP);
if (subdlg->ShowModal() != wxID_OK)
return;
if (otype != ce_type || ocode != ce_codes) {
// vba core certainly doesn't make this easy
// there is no "change" function, so the only way to retain
// the old order is to delete this and all subsequent items, and
// then re-add them
// The MFC code got around this by not even supporting edits on
// gba codes (which have order dependencies) and just forcing
// edited codes to the rear on gb codes.
// It might be safest to only support desc edits, and force the
// user to re-enter codes to change them
int ncodes = isgb ? gbCheatNumber : cheatsNumber;
if (ncodes > id + 1) {
wxString codes[MAX_CHEATS];
wxString descs[MAX_CHEATS];
bool checked[MAX_CHEATS];
bool v3[MAX_CHEATS];
for (int i = id + 1; i < ncodes; i++) {
codes[i - id - 1] = wxString(isgb ? gbCheatList[i].cheatCode : cheatsList[i].codestring,
wxConvLibc);
descs[i - id - 1] = wxString(isgb ? gbCheatList[i].cheatDesc : cheatsList[i].desc,
wxConvUTF8);
checked[i - id - 1] = isgb ? gbCheatList[i].enabled : cheatsList[i].enabled;
v3[i - id - 1] = isgb ? false : cheatsList[i].code == 257;
}
for (int i = ncodes - 1; i >= id; i--) {
list->DeleteItem(i);
if (isgb)
gbCheatRemove(i);
else
cheatsDelete(i, cheatsList[i].enabled);
}
AddCheat();
if (!ochecked) {
if (isgb)
gbCheatDisable(id);
else
cheatsDisable(id);
}
for (int i = id + 1; i < ncodes; i++) {
ce_codes = codes[i - id - 1];
ce_desc = descs[i - id - 1];
if (isgb) {
if (ce_codes.find(wxT('-')) == wxString::npos)
ce_type = 0;
else
ce_type = 1;
} else {
if (ce_codes.find(wxT(':')) != wxString::npos)
ce_type = 0;
else if (ce_codes.find(wxT(' ')) == wxString::npos) {
ce_type = 1;
if (v3[i - id - 1])
ce_codes.insert(8, 1, wxT(' '));
} else {
ce_type = 2;
}
}
AddCheat();
if (!checked[i - id - 1]) {
if (isgb)
gbCheatDisable(i);
else
cheatsDisable(i);
}
}
} else {
list->DeleteItem(id);
if (isgb)
gbCheatRemove(id);
else
cheatsDelete(id, cheatsList[id].enabled);
AddCheat();
if (!ochecked) {
if (isgb)
gbCheatDisable(id);
else
cheatsDisable(id);
}
}
Reload(id);
} else if (ce_desc != odesc) {
*dirty = true;
char* p = isgb ? gbCheatList[id].cheatDesc : cheatsList[id].desc;
strncpy(p, ce_desc.mb_str(), sizeof(cheatsList[0].desc));
p[sizeof(cheatsList[0].desc) - 1] = 0;
item1.SetId(id);
item1.SetText(wxString(p, wxConvUTF8));
list->SetItem(item1);
}
}
void AdjustDescWidth()
{
// why is it so hard to get an accurate measurement out of wx?
// on msw, wxLIST_AUTOSIZE might actually be accurate. On wxGTK,
// and probably wxMAC (both of which use generic impl) wrong
// font is used both for rendering (col 0's font) and for
// wxLIST_AUTOSIZE calculation (the widget's font).
// The only way to defeat this is to calculate size manually
// Instead, this just allows user to set max size, and retains
// it.
int ow = list->GetColumnWidth(1);
list->SetColumnWidth(1, wxLIST_AUTOSIZE);
int cw = list->GetColumnWidth(1);
// subtracted in renderer from width avail for text
// but not added in wxLIST_AUTOSIZE
cw += 8;
if (cw < col1minw)
cw = col1minw;
if (cw < ow)
cw = ow;
list->SetColumnWidth(1, cw);
}
} cheat_list_handler;
void CheatList_t::ParseChtLine(wxString desc, wxString tok)
{
wxString cheat_opt = tok.BeforeFirst(wxT('='));
wxString cheat_set = tok.AfterFirst(wxT('='));
wxStringTokenizer addr_tk(cheat_set.MakeUpper(), wxT(";"));
while (addr_tk.HasMoreTokens()) {
wxString addr_token = addr_tk.GetNextToken();
wxString cheat_addr = addr_token.BeforeFirst(wxT(','));
wxString values = addr_token.AfterFirst(wxT(','));
wxString cheat_desc = desc + wxT(":") + cheat_opt;
wxString cheat_line;
wxString cheat_value;
uint32_t address = 0;
uint32_t value = 0;
sscanf(cheat_addr.mb_str(), "%8x", &address);
if (address < 0x40000)
address += 0x2000000;
else
address += 0x3000000 - 0x40000;
wxStringTokenizer value_tk(values.MakeUpper(), wxT(","));
while (value_tk.HasMoreTokens()) {
wxString value_token = value_tk.GetNextToken();
sscanf(value_token.mb_str(), "%2x", &value);
cheat_line.Printf(wxT("%08X"), address);
cheat_value.Printf(wxT("%02X"), value);
cheat_line = cheat_line + wxT(":") + cheat_value;
cheatsAddCheatCode(cheat_line.mb_str(), cheat_desc.mb_str());
address++;
}
}
}
// onshow handler for above, in the form of an overzealous validator
class CheatListFill : public wxValidator {
public:
CheatListFill()
: wxValidator()
{
}
CheatListFill(const CheatListFill& e)
: wxValidator()
{
}
wxObject* Clone() const { return new CheatListFill(*this); }
bool TransferFromWindow() { return true; }
bool Validate(wxWindow* p) { return true; }
bool TransferToWindow()
{
CheatList_t& clh = cheat_list_handler;
GameArea* panel = wxGetApp().frame->GetPanel();
clh.isgb = panel->game_type() == IMAGE_GB;
clh.dirty = &panel->cheats_dirty;
clh.cheatfn = panel->game_name() + wxT(".clt");
clh.cheatdir = panel->game_dir();
clh.deffn = wxFileName(clh.cheatdir, clh.cheatfn).GetFullPath();
clh.Reload();
clh.ce_desc = wxEmptyString;
wxChoice* ch = clh.ce_type_ch;
ch->Clear();
if (clh.isgb) {
ch->Append(_("GameShark"));
ch->Append(_("GameGenie"));
} else {
ch->Append(_("Generic Code"));
ch->Append(_("GameShark Advance"));
ch->Append(_("CodeBreaker Advance"));
ch->Append(_("Flashcart CHT"));
}
ch->SetSelection(0);
return true;
}
};
// manage the cheat search dialog
enum cf_vfmt {
CFVFMT_SD,
CFVFMT_UD,
CFVFMT_UH
};
// virtual ListCtrl for cheat search results
class CheatListCtrl : public wxListCtrl {
public:
wxArrayInt addrs;
int cap_size; // size in effect when addrs were generated
int count8, count16, count32; // number of aligned addresses in addrs
wxString OnGetItemText(long item, long column) const;
DECLARE_DYNAMIC_CLASS(CheatListCtrl)
};
IMPLEMENT_DYNAMIC_CLASS(CheatListCtrl, wxListCtrl);
static class CheatFind_t : public wxEvtHandler {
public:
wxDialog* dlg;
int valsrc, size, op, fmt;
int ofmt, osize;
wxString val_s;
wxTextCtrl* val_tc;
CheatListCtrl* list;
// for enable/disable
wxRadioButton *old_rb, *val_rb;
wxControl *update_b, *clear_b, *add_b;
bool isgb;
// add dialog
wxString ca_desc, ca_val;
wxTextCtrl* ca_val_tc;
wxControl *ca_fmt, *ca_addr;
CheatFind_t()
: wxEvtHandler()
, valsrc(0)
, size(0)
, op(0)
, fmt(0)
, val_s()
{
}
~CheatFind_t()
{
// not that it matters to anyone but mem leak detectors..
cheatSearchCleanup(&cheatSearchData);
}
void Search(wxCommandEvent& ev)
{
dlg->TransferDataFromWindow();
if (!valsrc && val_s.empty()) {
wxLogError(_("Number cannot be empty"));
return;
}
if (!cheatSearchData.count)
ResetSearch(ev);
if (valsrc)
cheatSearch(&cheatSearchData, op, size, fmt == CFVFMT_SD);
else
cheatSearchValue(&cheatSearchData, op, size, fmt == CFVFMT_SD,
SignedValue());
Deselect();
list->addrs.clear();
list->count8 = list->count16 = list->count32 = 0;
list->cap_size = size;
for (int i = 0; i < cheatSearchData.count; i++) {
CheatSearchBlock* block = &cheatSearchData.blocks[i];
for (int j = 0; j < block->size; j += (1 << size)) {
if (IS_BIT_SET(block->bits, j)) {
list->addrs.push_back((i << 28) + j);
if (!(j & 1))
list->count16++;
if (!(j & 3))
list->count32++;
// since listctrl is virtual, it should be able to handle
// at least 256k results, which is about the most you
// will ever get
#if 0
if (list->addrs.size() > 1000)
{
wxLogError(_("Search produced %d results. Please refine better"),
list->addrs.size());
list->addrs.clear();
return;
}
#endif
}
}
}
if (list->addrs.empty()) {
wxLogError(_("Search produced no results"));
// no point in keeping empty search results around
ResetSearch(ev);
if (old_rb->GetValue()) {
val_rb->SetValue(true);
// SetValue doesn't generate an event
val_tc->Enable();
}
old_rb->Disable();
update_b->Disable();
clear_b->Disable();
} else {
switch (size) {
case BITS_32:
list->count16 = list->count32 * 2;
// fall through
case BITS_16:
list->count8 = list->count16 * 2;
break;
case BITS_8:
list->count8 = list->addrs.size();
}
old_rb->Enable();
update_b->Enable();
clear_b->Enable();
}
list->SetItemCount(list->addrs.size());
list->Refresh();
}
void UpdateVals(wxCommandEvent& ev)
{
if (cheatSearchData.count) {
cheatSearchUpdateValues(&cheatSearchData);
if (list->count8)
list->Refresh();
update_b->Disable();
}
}
void ResetSearch(wxCommandEvent& ev)
{
if (!cheatSearchData.count) {
CheatSearchBlock* block = cheatSearchData.blocks;
if (isgb) {
block->offset = 0xa000;
if (gbRam)
block->data = gbRam;
else
block->data = &gbMemory[0xa000];
block->saved = (uint8_t*)malloc(gbRamSize);
block->size = gbRamSize;
block->bits = (uint8_t*)malloc(gbRamSize >> 3);
if (gbCgbMode) {
block++;
block->offset = 0xc000;
block->data = &gbMemory[0xc000];
block->saved = (uint8_t*)malloc(0x1000);
block->size = 0x1000;
block->bits = (uint8_t*)malloc(0x1000 >> 3);
block++;
block->offset = 0xd000;
block->data = gbWram;
block->saved = (uint8_t*)malloc(0x8000);
block->size = 0x8000;
block->bits = (uint8_t*)malloc(0x8000 >> 3);
} else {
block++;
block->offset = 0xc000;
block->data = &gbMemory[0xc000];
block->saved = (uint8_t*)malloc(0x2000);
block->size = 0x2000;
block->bits = (uint8_t*)malloc(0x2000 >> 3);
}
} else {
block->size = 0x40000;
block->offset = 0x2000000;
block->bits = (uint8_t*)malloc(0x40000 >> 3);
block->data = workRAM;
block->saved = (uint8_t*)malloc(0x40000);
block++;
block->size = 0x8000;
block->offset = 0x3000000;
block->bits = (uint8_t*)malloc(0x8000 >> 3);
block->data = internalRAM;
block->saved = (uint8_t*)malloc(0x8000);
}
cheatSearchData.count = (int)((block + 1) - cheatSearchData.blocks);
}
cheatSearchStart(&cheatSearchData);
if (list->count8) {
Deselect();
list->count8 = list->count16 = list->count32 = 0;
list->addrs.clear();
list->SetItemCount(0);
list->Refresh();
if (old_rb->GetValue()) {
val_rb->SetValue(true);
// SetValue doesn't generate an event
val_tc->Enable();
}
old_rb->Disable();
update_b->Disable();
clear_b->Disable();
}
}
void Deselect()
{
int idx = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (idx >= 0)
list->SetItemState(idx, 0, wxLIST_STATE_SELECTED);
add_b->Disable();
}
void Select(wxListEvent& ev)
{
add_b->Enable(list->GetItemState(ev.GetIndex(), wxLIST_STATE_SELECTED) != 0);
}
void AddCheatB(wxCommandEvent& ev)
{
int idx = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (idx >= 0)
AddCheat(idx);
}
void AddCheatL(wxListEvent& ev)
{
AddCheat(ev.GetIndex());
}