-
Notifications
You must be signed in to change notification settings - Fork 0
/
ramwatch.cpp
1172 lines (1032 loc) · 32.9 KB
/
ramwatch.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
//RamWatch dialog was copied and adapted from GENS11: http://code.google.com/p/gens-rerecording/
//Authors: Upthorn, Nitsuja, adelikat
#include "main.h"
#include "types.h"
#include "resource.h"
#include "ramwatch.h"
#include "ramsearch.h"
#include <assert.h>
#include <windows.h>
#include <commctrl.h>
#include <string>
HWND RamWatchHWnd = NULL;
extern HWND g_hWnd;
extern WNDCLASSEX winClass;
#define MESSAGEBOXPARENT (RamWatchHWnd ? RamWatchHWnd : g_hWnd)
char Str_Tmp[1024];
std::string Rom_Name;
static HMENU ramwatchmenu;
static HMENU rwrecentmenu;
static HACCEL RamWatchAccels = NULL;
char rw_recent_files[MAX_RECENT_WATCHES][1024];
char Watch_Dir[1024]="";
const unsigned int RW_MENU_FIRST_RECENT_FILE = 600;
bool RWfileChanged = false; //Keeps track of whether the current watch file has been changed, if so, ramwatch will prompt to save changes
bool AutoRWLoad = false; //Keeps track of whether Auto-load is checked
bool RWSaveWindowPos = false; //Keeps track of whether Save Window position is checked
char currentWatch[1024];
int ramw_x, ramw_y; //Used to store ramwatch dialog window positions
AddressWatcher rswatches[MAX_WATCH_COUNT];
int WatchCount=0;
bool QuickSaveWatches();
bool ResetWatches();
extern uint8 BaseRAM[32768];
#include "vb.h"
void RefreshWatchListSelectedCountControlStatus(HWND hDlg);
unsigned int GetCurrentValue(AddressWatcher& watch)
{
// if(watch.Address > 0x1F8000 || watch.Address < 0x1F0000)
// return 1 ;
v810_timestamp_t v;
v = 10;
switch (watch.Size)
{
case 0x62: return MDFN_IEN_VB::MemRead8(v,watch.Address);
case 0x77: return MDFN_IEN_VB::MemRead16(v,watch.Address);
case 0x64: return BaseRAM[watch.Address];
default: return 0;
}
return 1;
/* char buf[4];
MMU_DumpMemBlock(0, watch.Address, 4, (uint8*)buf);
uint32 val_u32 = *(u32*)buf;
u16 val_u16 = *(u16*)buf;
u8 val_u8 = *(u8*)buf;
switch (watch.Size)
{
case 0x62: return val_u8;
case 0x77: return val_u16;
case 0x64: return val_u32;
default: return 0;
}*/
// return 1; // ReadValueAtHardwareAddress(watch.Address, watch.Size == 'd' ? 4 : watch.Size == 'w' ? 2 : 1);
}
bool IsSameWatch(const AddressWatcher& l, const AddressWatcher& r)
{
if (r.Size == 'S') return false;
return ((l.Address == r.Address) && (l.Size == r.Size) && (l.Type == r.Type)/* && (l.WrongEndian == r.WrongEndian)*/);
}
bool VerifyWatchNotAlreadyAdded(const AddressWatcher& watch)
{
for (int j = 0; j < WatchCount; j++)
{
if (IsSameWatch(rswatches[j], watch))
{
if(RamWatchHWnd)
SetForegroundWindow(RamWatchHWnd);
return false;
}
}
return true;
}
bool InsertWatch(const AddressWatcher& Watch, char *Comment)
{
if(!VerifyWatchNotAlreadyAdded(Watch))
return false;
if(WatchCount >= MAX_WATCH_COUNT)
return false;
int i = WatchCount++;
AddressWatcher& NewWatch = rswatches[i];
NewWatch = Watch;
//if (NewWatch.comment) free(NewWatch.comment);
NewWatch.comment = (char *) malloc(strlen(Comment)+2);
NewWatch.CurValue = GetCurrentValue(NewWatch);
strcpy(NewWatch.comment, Comment);
ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount);
RWfileChanged=true;
return true;
}
LRESULT CALLBACK PromptWatchNameProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) //Gets the description of a watched address
{
RECT r;
RECT r2;
int dx1, dy1, dx2, dy2;
switch(uMsg)
{
case WM_INITDIALOG:
GetWindowRect(g_hWnd, &r);
dx1 = (r.right - r.left) / 2;
dy1 = (r.bottom - r.top) / 2;
GetWindowRect(hDlg, &r2);
dx2 = (r2.right - r2.left) / 2;
dy2 = (r2.bottom - r2.top) / 2;
SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
strcpy(Str_Tmp,"Enter a name for this RAM address.");
//SendDlgItemMessage(hDlg,IDC_PROMPT_TEXT,WM_SETTEXT,0,(LPARAM)Str_Tmp);
strcpy(Str_Tmp,"");
//SendDlgItemMessage(hDlg,IDC_PROMPT_TEXT2,WM_SETTEXT,0,(LPARAM)Str_Tmp);
return true;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
{
GetDlgItemText(hDlg,IDC_PROMPT_EDIT,Str_Tmp,80);
InsertWatch(rswatches[WatchCount],Str_Tmp);
EndDialog(hDlg, true);
return true;
break;
}
case IDCANCEL:
EndDialog(hDlg, false);
return false;
break;
}
break;
case WM_CLOSE:
EndDialog(hDlg, false);
return false;
break;
}
return false;
}
bool InsertWatch(const AddressWatcher& Watch, HWND parent)
{
if(!VerifyWatchNotAlreadyAdded(Watch))
return false;
if(!parent)
parent = RamWatchHWnd;
if(!parent)
parent = g_hWnd;
int prevWatchCount = WatchCount;
rswatches[WatchCount] = Watch;
rswatches[WatchCount].CurValue = GetCurrentValue(rswatches[WatchCount]);
DialogBox(winClass.hInstance, MAKEINTRESOURCE(IDD_PROMPT), parent, (DLGPROC) PromptWatchNameProc);
return WatchCount > prevWatchCount;
}
void Update_RAM_Watch()
{
if (!RamWatchHWnd) return;
// update cached values and detect changes to displayed listview items
BOOL watchChanged[MAX_WATCH_COUNT] = {0};
for(int i = 0; i < WatchCount; i++)
{
unsigned int prevCurValue = rswatches[i].CurValue;
unsigned int newCurValue = GetCurrentValue(rswatches[i]);
if(prevCurValue != newCurValue)
{
rswatches[i].CurValue = newCurValue;
watchChanged[i] = TRUE;
}
}
// refresh any visible parts of the listview box that changed
HWND lv = GetDlgItem(RamWatchHWnd,IDC_WATCHLIST);
int top = ListView_GetTopIndex(lv);
int bottom = top + ListView_GetCountPerPage(lv) + 1; // +1 is so we will update a partially-displayed last item
if(top < 0) top = 0;
if(bottom > WatchCount) bottom = WatchCount;
int start = -1;
for(int i = top; i <= bottom; i++)
{
if(start == -1)
{
if(i != bottom && watchChanged[i])
{
start = i;
//somethingChanged = true;
}
}
else
{
if(i == bottom || !watchChanged[i])
{
ListView_RedrawItems(lv, start, i-1);
start = -1;
}
}
}
}
bool AskSave()
{
//This function asks to save changes if the watch file contents have changed
//returns false only if a save was attempted but failed or was cancelled
if (RWfileChanged)
{
HWND Hwnd = g_hWnd;
int answer = MessageBox(Hwnd, "Save Changes?", "Ram Watch", MB_YESNOCANCEL);
if(answer == IDYES)
if(!QuickSaveWatches())
return false;
return (answer != IDCANCEL);
}
return true;
}
void UpdateRW_RMenu(HMENU menu, unsigned int mitem, unsigned int baseid)
{
MENUITEMINFO moo;
int x;
moo.cbSize = sizeof(moo);
moo.fMask = MIIM_SUBMENU | MIIM_STATE;
GetMenuItemInfo(GetSubMenu(ramwatchmenu, 0), mitem, FALSE, &moo);
moo.hSubMenu = menu;
moo.fState = strlen(rw_recent_files[0]) ? MFS_ENABLED : MFS_GRAYED;
SetMenuItemInfo(GetSubMenu(ramwatchmenu, 0), mitem, FALSE, &moo);
// Remove all recent files submenus
for(x = 0; x < MAX_RECENT_WATCHES; x++)
{
RemoveMenu(menu, baseid + x, MF_BYCOMMAND);
}
// Recreate the menus
for(x = MAX_RECENT_WATCHES - 1; x >= 0; x--)
{
char tmp[128 + 5];
// Skip empty strings
if(!strlen(rw_recent_files[x]))
{
continue;
}
moo.cbSize = sizeof(moo);
moo.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE;
// Fill in the menu text.
if(strlen(rw_recent_files[x]) < 128)
{
sprintf(tmp, "&%d. %s", ( x + 1 ) % 10, rw_recent_files[x]);
}
else
{
sprintf(tmp, "&%d. %s", ( x + 1 ) % 10, rw_recent_files[x] + strlen( rw_recent_files[x] ) - 127);
}
// Insert the menu item
moo.cch = strlen(tmp);
moo.fType = 0;
moo.wID = baseid + x;
moo.dwTypeData = tmp;
InsertMenuItem(menu, 0, 1, &moo);
}
}
void UpdateRWRecentArray(const char* addString, unsigned int arrayLen, HMENU menu, unsigned int menuItem, unsigned int baseId)
{
// Try to find out if the filename is already in the recent files list.
for(unsigned int x = 0; x < arrayLen; x++)
{
if(strlen(rw_recent_files[x]))
{
if(!strcmp(rw_recent_files[x], addString)) // Item is already in list.
{
// If the filename is in the file list don't add it again.
// Move it up in the list instead.
int y;
char tmp[1024];
// Save pointer.
strcpy(tmp,rw_recent_files[x]);
for(y = x; y; y--)
{
// Move items down.
strcpy(rw_recent_files[y],rw_recent_files[y - 1]);
}
// Put item on top.
strcpy(rw_recent_files[0],tmp);
// Update the recent files menu
UpdateRW_RMenu(menu, menuItem, baseId);
return;
}
}
}
// The filename wasn't found in the list. That means we need to add it.
// Move the other items down.
for(unsigned int x = arrayLen - 1; x; x--)
{
strcpy(rw_recent_files[x],rw_recent_files[x - 1]);
}
// Add the new item.
strcpy(rw_recent_files[0], addString);
// Update the recent files menu
UpdateRW_RMenu(menu, menuItem, baseId);
}
void RWAddRecentFile(const char *filename)
{
UpdateRWRecentArray(filename, MAX_RECENT_WATCHES, rwrecentmenu, RAMMENU_FILE_RECENT, RW_MENU_FIRST_RECENT_FILE);
}
void OpenRWRecentFile(int memwRFileNumber)
{
if(!ResetWatches())
return;
int rnum = memwRFileNumber;
if ((unsigned int)rnum >= MAX_RECENT_WATCHES)
return; //just in case
char* x;
while(true)
{
x = rw_recent_files[rnum];
if (!*x)
return; //If no recent files exist just return. Useful for Load last file on startup (or if something goes screwy)
if (rnum) //Change order of recent files if not most recent
{
RWAddRecentFile(x);
rnum = 0;
}
else
{
break;
}
}
strcpy(currentWatch,x);
strcpy(Str_Tmp,currentWatch);
//loadwatches here
FILE *WatchFile = fopen(Str_Tmp,"rb");
if (!WatchFile)
{
int answer = MessageBox(MESSAGEBOXPARENT,"Error opening file.","ERROR",MB_OKCANCEL);
if (answer == IDOK)
{
rw_recent_files[rnum][0] = '\0'; //Clear file from list
if (rnum) //Update the ramwatch list
RWAddRecentFile(rw_recent_files[0]);
else
RWAddRecentFile(rw_recent_files[1]);
}
return;
}
const char DELIM = '\t';
AddressWatcher Temp;
char mode;
fgets(Str_Tmp,1024,WatchFile);
sscanf(Str_Tmp,"%c%*s",&mode);
int WatchAdd;
fgets(Str_Tmp,1024,WatchFile);
sscanf(Str_Tmp,"%d%*s",&WatchAdd);
WatchAdd+=WatchCount;
for (int i = WatchCount; i < WatchAdd; i++)
{
while (i < 0)
i++;
do {
fgets(Str_Tmp,1024,WatchFile);
} while (Str_Tmp[0] == '\n');
sscanf(Str_Tmp,"%*05X%*c%08X%*c%c%*c%c%*c%d",&(Temp.Address),&(Temp.Size),&(Temp.Type),&(Temp.WrongEndian));
Temp.WrongEndian = 0;
char *Comment = strrchr(Str_Tmp,DELIM) + 1;
*strrchr(Comment,'\n') = '\0';
InsertWatch(Temp,Comment);
}
fclose(WatchFile);
if (RamWatchHWnd) {
ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount);
RefreshWatchListSelectedCountControlStatus(RamWatchHWnd);
}
RWfileChanged=false;
return;
}
char Gens_Path[64]= "M:\\"; //TODO
int Change_File_S(char *Dest, char *Dir, char *Titre, char *Filter, char *Ext, HWND hwnd)
{
OPENFILENAME ofn;
SetCurrentDirectory(Gens_Path);
if (!strcmp(Dest, ""))
{
strcpy(Dest, "default.");
strcat(Dest, Ext);
}
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = winClass.hInstance;
ofn.lpstrFile = Dest;
ofn.nMaxFile = 2047;
ofn.lpstrFilter = Filter;
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = Dir;
ofn.lpstrTitle = Titre;
ofn.lpstrDefExt = Ext;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
if (GetSaveFileName(&ofn)) return 1;
return 0;
}
bool Save_Watches()
{
Rom_Name = GetGameName();
strncpy(Str_Tmp,Rom_Name.c_str(),512);
strcat(Str_Tmp,".wch");
if(Change_File_S(Str_Tmp, Gens_Path, "Save Watches", "Watchlist\0*.wch\0All Files\0*.*\0\0", "wch", RamWatchHWnd))
{
FILE *WatchFile = fopen(Str_Tmp,"r+b");
if (!WatchFile) WatchFile = fopen(Str_Tmp,"w+b");
fputc('\n',WatchFile);
strcpy(currentWatch,Str_Tmp);
RWAddRecentFile(currentWatch);
sprintf(Str_Tmp,"%d\n",WatchCount);
fputs(Str_Tmp,WatchFile);
const char DELIM = '\t';
for (int i = 0; i < WatchCount; i++)
{
sprintf(Str_Tmp,"%05X%c%08X%c%c%c%c%c%d%c%s\n",i,DELIM,rswatches[i].Address,DELIM,rswatches[i].Size,DELIM,rswatches[i].Type,DELIM,rswatches[i].WrongEndian,DELIM,rswatches[i].comment);
fputs(Str_Tmp,WatchFile);
}
fclose(WatchFile);
RWfileChanged=false;
return true;
}
return false;
}
bool QuickSaveWatches()
{
if (RWfileChanged==false) return true; //If file has not changed, no need to save changes
if (currentWatch[0] == NULL) //If there is no currently loaded file, run to Save as and then return
{
return Save_Watches();
}
strcpy(Str_Tmp,currentWatch);
FILE *WatchFile = fopen(Str_Tmp,"r+b");
if (!WatchFile) WatchFile = fopen(Str_Tmp,"w+b");
fputc('\n',WatchFile);
sprintf(Str_Tmp,"%d\n",WatchCount);
fputs(Str_Tmp,WatchFile);
const char DELIM = '\t';
for (int i = 0; i < WatchCount; i++)
{
sprintf(Str_Tmp,"%05X%c%08X%c%c%c%c%c%d%c%s\n",i,DELIM,rswatches[i].Address,DELIM,rswatches[i].Size,DELIM,rswatches[i].Type,DELIM,rswatches[i].WrongEndian,DELIM,rswatches[i].comment);
fputs(Str_Tmp,WatchFile);
}
fclose(WatchFile);
RWfileChanged=false;
return true;
}
bool Load_Watches(bool clear, const char* filename)
{
const char DELIM = '\t';
FILE* WatchFile = fopen(filename,"rb");
if (!WatchFile)
{
MessageBox(MESSAGEBOXPARENT,"Error opening file.","ERROR",MB_OK);
return false;
}
if(clear)
{
if(!ResetWatches())
{
fclose(WatchFile);
return false;
}
}
strcpy(currentWatch,filename);
RWAddRecentFile(currentWatch);
AddressWatcher Temp;
char mode;
fgets(Str_Tmp,1024,WatchFile);
sscanf(Str_Tmp,"%c%*s",&mode);
int WatchAdd;
fgets(Str_Tmp,1024,WatchFile);
sscanf(Str_Tmp,"%d%*s",&WatchAdd);
WatchAdd+=WatchCount;
for (int i = WatchCount; i < WatchAdd; i++)
{
while (i < 0)
i++;
do {
fgets(Str_Tmp,1024,WatchFile);
} while (Str_Tmp[0] == '\n');
sscanf(Str_Tmp,"%*05X%*c%08X%*c%c%*c%c%*c%d",&(Temp.Address),&(Temp.Size),&(Temp.Type),&(Temp.WrongEndian));
Temp.WrongEndian = 0;
char *Comment = strrchr(Str_Tmp,DELIM) + 1;
*strrchr(Comment,'\n') = '\0';
InsertWatch(Temp,Comment);
}
fclose(WatchFile);
if (RamWatchHWnd)
ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount);
RWfileChanged=false;
return true;
}
int Change_File_L(char *Dest, char *Dir, char *Titre, char *Filter, char *Ext, HWND hwnd)
{
OPENFILENAME ofn;
SetCurrentDirectory(Gens_Path);
if (!strcmp(Dest, ""))
{
strcpy(Dest, "default.");
strcat(Dest, Ext);
}
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = winClass.hInstance;
ofn.lpstrFile = Dest;
ofn.nMaxFile = 2047;
ofn.lpstrFilter = Filter;
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = Dir;
ofn.lpstrTitle = Titre;
ofn.lpstrDefExt = Ext;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn)) return 1;
return 0;
}
bool Load_Watches(bool clear)
{
Rom_Name = GetGameName();
strncpy(Str_Tmp,Rom_Name.c_str(),512);
strcat(Str_Tmp,".wch");
if(Change_File_L(Str_Tmp, Watch_Dir, "Load Watches", "GENs Watchlist\0*.wch\0All Files\0*.*\0\0", "wch", RamWatchHWnd))
{
return Load_Watches(clear, Str_Tmp);
}
return false;
}
bool ResetWatches()
{
if(!AskSave())
return false;
for (;WatchCount>=0;WatchCount--)
{
free(rswatches[WatchCount].comment);
rswatches[WatchCount].comment = NULL;
}
WatchCount++;
if (RamWatchHWnd) {
ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount);
RefreshWatchListSelectedCountControlStatus(RamWatchHWnd);
}
RWfileChanged = false;
currentWatch[0] = NULL;
return true;
}
void RemoveWatch(int watchIndex)
{
free(rswatches[watchIndex].comment);
rswatches[watchIndex].comment = NULL;
for (int i = watchIndex; i <= WatchCount; i++)
rswatches[i] = rswatches[i+1];
WatchCount--;
}
LRESULT CALLBACK EditWatchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) //Gets info for a RAM Watch, and then inserts it into the Watch List
{
RECT r;
RECT r2;
int dx1, dy1, dx2, dy2;
static int index;
static char s,t = s = 0;
switch(uMsg)
{
case WM_INITDIALOG:
GetWindowRect(g_hWnd, &r);
dx1 = (r.right - r.left) / 2;
dy1 = (r.bottom - r.top) / 2;
GetWindowRect(hDlg, &r2);
dx2 = (r2.right - r2.left) / 2;
dy2 = (r2.bottom - r2.top) / 2;
SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
index = (int)lParam;
sprintf(Str_Tmp,"%08X",rswatches[index].Address);
SetDlgItemText(hDlg,IDC_EDIT_COMPAREADDRESS,Str_Tmp);
if (rswatches[index].comment != NULL)
SetDlgItemText(hDlg,IDC_PROMPT_EDIT,rswatches[index].comment);
s = rswatches[index].Size;
t = rswatches[index].Type;
switch (s)
{
case 'b':
SendDlgItemMessage(hDlg, IDC_1_BYTE, BM_SETCHECK, BST_CHECKED, 0);
break;
case 'w':
SendDlgItemMessage(hDlg, IDC_2_BYTES, BM_SETCHECK, BST_CHECKED, 0);
break;
case 'd':
SendDlgItemMessage(hDlg, IDC_4_BYTES, BM_SETCHECK, BST_CHECKED, 0);
break;
default:
s = 0;
break;
}
switch (t)
{
case 's':
SendDlgItemMessage(hDlg, IDC_SIGNED, BM_SETCHECK, BST_CHECKED, 0);
break;
case 'u':
SendDlgItemMessage(hDlg, IDC_UNSIGNED, BM_SETCHECK, BST_CHECKED, 0);
break;
case 'h':
SendDlgItemMessage(hDlg, IDC_HEX, BM_SETCHECK, BST_CHECKED, 0);
break;
default:
t = 0;
break;
}
return true;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_SIGNED:
t='s';
return true;
case IDC_UNSIGNED:
t='u';
return true;
case IDC_HEX:
t='h';
return true;
case IDC_1_BYTE:
s = 'b';
return true;
case IDC_2_BYTES:
s = 'w';
return true;
case IDC_4_BYTES:
s = 'd';
return true;
case IDOK:
{
if (s && t)
{
AddressWatcher Temp;
Temp.Size = s;
Temp.Type = t;
Temp.WrongEndian = false; //replace this when I get little endian working properly
GetDlgItemText(hDlg,IDC_EDIT_COMPAREADDRESS,Str_Tmp,1024);
char *addrstr = Str_Tmp;
if (strlen(Str_Tmp) > 8) addrstr = &(Str_Tmp[strlen(Str_Tmp) - 9]);
for(int i = 0; addrstr[i]; i++) {if(toupper(addrstr[i]) == 'O') addrstr[i] = '0';}
sscanf(addrstr,"%08X",&(Temp.Address));
if((Temp.Address & ~0xFFFFFF) == ~0xFFFFFF)
Temp.Address &= 0xFFFFFF;
if(IsHardwareRAMAddressValid(Temp.Address))
{
GetDlgItemText(hDlg,IDC_PROMPT_EDIT,Str_Tmp,80);
if (index < WatchCount) RemoveWatch(index);
InsertWatch(Temp,Str_Tmp);
if(RamWatchHWnd)
{
ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount);
}
EndDialog(hDlg, true);
}
else
{
MessageBox(hDlg,"Invalid Address","ERROR",MB_OK);
}
}
else
{
strcpy(Str_Tmp,"Error:");
if (!s)
strcat(Str_Tmp," Size must be specified.");
if (!t)
strcat(Str_Tmp," Type must be specified.");
MessageBox(hDlg,Str_Tmp,"ERROR",MB_OK);
}
RWfileChanged=true;
return true;
break;
}
case IDCANCEL:
EndDialog(hDlg, false);
return false;
break;
}
break;
case WM_CLOSE:
EndDialog(hDlg, false);
return false;
break;
}
return false;
}
void init_list_box(HWND Box, const char* Strs[], int numColumns, int *columnWidths) //initializes the ram search and/or ram watch listbox
{
LVCOLUMN Col;
Col.mask = LVCF_FMT | LVCF_ORDER | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
Col.fmt = LVCFMT_CENTER;
for (int i = 0; i < numColumns; i++)
{
Col.iOrder = i;
Col.iSubItem = i;
Col.pszText = (LPSTR)(Strs[i]);
Col.cx = columnWidths[i];
ListView_InsertColumn(Box,i,&Col);
}
ListView_SetExtendedListViewStyle(Box, LVS_EX_FULLROWSELECT);
}
void RamWatchEnableCommand(HWND hDlg, HMENU hMenu, UINT uIDEnableItem, bool enable)
{
EnableWindow(GetDlgItem(hDlg, uIDEnableItem), (enable?TRUE:FALSE));
if (hMenu != NULL) {
if (uIDEnableItem == ID_WATCHES_UPDOWN) {
EnableMenuItem(hMenu, IDC_C_WATCH_UP, MF_BYCOMMAND | (enable?MF_ENABLED:MF_GRAYED));
EnableMenuItem(hMenu, IDC_C_WATCH_DOWN, MF_BYCOMMAND | (enable?MF_ENABLED:MF_GRAYED));
}
else
EnableMenuItem(hMenu, uIDEnableItem, MF_BYCOMMAND | (enable?MF_ENABLED:MF_GRAYED));
}
}
void RefreshWatchListSelectedCountControlStatus(HWND hDlg)
{
static int prevSelCount=-1;
int selCount = ListView_GetSelectedCount(GetDlgItem(hDlg,IDC_WATCHLIST));
if(selCount != prevSelCount)
{
if(selCount < 2 || prevSelCount < 2)
{
RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_WATCH_EDIT, selCount == 1);
RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_WATCH_REMOVE, selCount >= 1);
RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_WATCH_DUPLICATE, selCount == 1);
RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_ADDCHEAT, selCount == 1);
RamWatchEnableCommand(hDlg, ramwatchmenu, ID_WATCHES_UPDOWN, selCount == 1);
}
prevSelCount = selCount;
}
}
LRESULT CALLBACK RamWatchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
RECT r;
RECT r2;
int dx1, dy1, dx2, dy2;
static int watchIndex=0;
Update_RAM_Watch();
switch(uMsg)
{
case WM_MOVE: {
RECT wrect;
GetWindowRect(hDlg,&wrect);
ramw_x = wrect.left;
ramw_y = wrect.top;
break;
};
case WM_INITDIALOG: {
GetWindowRect(g_hWnd, &r); //Ramwatch window
dx1 = (r.right - r.left) / 2;
dy1 = (r.bottom - r.top) / 2;
GetWindowRect(hDlg, &r2); // Gens window
dx2 = (r2.right - r2.left) / 2;
dy2 = (r2.bottom - r2.top) / 2;
// push it away from the main window if we can
const int width = (r.right-r.left);
const int height = (r.bottom - r.top);
const int width2 = (r2.right-r2.left);
if(r.left+width2 + width < GetSystemMetrics(SM_CXSCREEN))
{
r.right += width;
r.left += width;
}
else if((int)r.left - (int)width2 > 0)
{
r.right -= width2;
r.left -= width2;
}
//-----------------------------------------------------------------------------------
//If user has Save Window Pos selected, override default positioning
if (RWSaveWindowPos)
{
//If ramwindow is for some reason completely off screen, use default instead
if (ramw_x > (-width*2) || ramw_x < (width*2 + GetSystemMetrics(SM_CYSCREEN)) )
r.left = ramw_x; //This also ignores cases of windows -32000 error codes
//If ramwindow is for some reason completely off screen, use default instead
if (ramw_y > (0-height*2) ||ramw_y < (height*2 + GetSystemMetrics(SM_CYSCREEN)) )
r.top = ramw_y; //This also ignores cases of windows -32000 error codes
}
//-------------------------------------------------------------------------------------
SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
ramwatchmenu=GetMenu(hDlg);
rwrecentmenu=CreateMenu();
UpdateRW_RMenu(rwrecentmenu, RAMMENU_FILE_RECENT, RW_MENU_FIRST_RECENT_FILE);
const char* names[3] = {"Address","Value","Notes"};
int widths[3] = {62,64,64+51+53};
init_list_box(GetDlgItem(hDlg,IDC_WATCHLIST),names,3,widths);
/* if (!ResultCount) //TODO what do these do
reset_address_info();
else
signal_new_frame();*/
ListView_SetItemCount(GetDlgItem(hDlg,IDC_WATCHLIST),WatchCount);
// if (!noMisalign) SendDlgItemMessage(hDlg, IDC_MISALIGN, BM_SETCHECK, BST_CHECKED, 0);
// if (littleEndian) SendDlgItemMessage(hDlg, IDC_ENDIAN, BM_SETCHECK, BST_CHECKED, 0);
// RamWatchAccels = LoadAccelerators(hAppInst, MAKEINTRESOURCE(IDR_ACCELERATOR1));
// due to some bug in windows, the arrow button width from the resource gets ignored, so we have to set it here
SetWindowPos(GetDlgItem(hDlg,ID_WATCHES_UPDOWN), 0,0,0, 30,60, SWP_NOMOVE);
Update_RAM_Watch();
DragAcceptFiles(hDlg, TRUE);
RefreshWatchListSelectedCountControlStatus(hDlg);
return true;
break;
}
case WM_INITMENU:
CheckMenuItem(ramwatchmenu, RAMMENU_FILE_AUTOLOAD, AutoRWLoad ? MF_CHECKED : MF_UNCHECKED);
CheckMenuItem(ramwatchmenu, RAMMENU_FILE_SAVEWINDOW, RWSaveWindowPos ? MF_CHECKED : MF_UNCHECKED);
break;
case WM_MENUSELECT:
case WM_ENTERSIZEMOVE:
break;
case WM_NOTIFY:
{
switch(wParam)
{
case ID_WATCHES_UPDOWN:
{
switch(((LPNMUPDOWN)lParam)->hdr.code)
{
case UDN_DELTAPOS:
int delta = ((LPNMUPDOWN)lParam)->iDelta;
SendMessage(hDlg, WM_COMMAND, delta<0 ? IDC_C_WATCH_UP : IDC_C_WATCH_DOWN,0);
break;
}
}
default:
{
LPNMHDR lP = (LPNMHDR) lParam;
switch (lP->code)
{
case LVN_ITEMCHANGED: // selection changed event
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)lP;
if(pNMListView->uNewState & LVIS_FOCUSED ||
(pNMListView->uNewState ^ pNMListView->uOldState) & LVIS_SELECTED)
{
// disable buttons that we don't have the right number of selected items for
RefreshWatchListSelectedCountControlStatus(hDlg);
}
} break;
case LVN_GETDISPINFO:
{
LV_DISPINFO *Item = (LV_DISPINFO *)lParam;
Item->item.mask = LVIF_TEXT;
Item->item.state = 0;
Item->item.iImage = 0;
const unsigned int iNum = Item->item.iItem;
static char num[11];
switch (Item->item.iSubItem)
{
case 0:
sprintf(num,"%08X",rswatches[iNum].Address);
Item->item.pszText = num;
return true;
case 1: {
int i = rswatches[iNum].CurValue;
int t = rswatches[iNum].Type;
int size = rswatches[iNum].Size;
const char* formatString = ((t=='s') ? "%d" : (t=='u') ? "%u" : (size=='d' ? "%08X" : size=='w' ? "%04X" : "%02X"));
switch (size)
{
case 'b':
default: sprintf(num, formatString, t=='s' ? (char)(i&0xff) : (unsigned char)(i&0xff)); break;
case 'w': sprintf(num, formatString, t=='s' ? (short)(i&0xffff) : (unsigned short)(i&0xffff)); break;
case 'd': sprintf(num, formatString, t=='s' ? (long)(i&0xffffffff) : (unsigned long)(i&0xffffffff)); break;
}
Item->item.pszText = num;
} return true;
case 2:
Item->item.pszText = rswatches[iNum].comment ? rswatches[iNum].comment : "";
return true;
default: