forked from PlexaryDamato/nrage-input
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFileAccess.cpp
1680 lines (1504 loc) · 52.3 KB
/
FileAccess.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
/*
N-Rage`s Dinput8 Plugin
(C) 2002, 2006 Norbert Wladyka
Author`s Email: [email protected]
Website: http://go.to/nrage
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "commonIncludes.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <shlobj.h>
#include "NRagePluginV2.h"
#include "PakIO.h"
#include "Interface.h"
#include "FileAccess.h"
#include "DirectInput.h"
#include <string>
using std::string;
#ifndef IDR_PROFILE_DEFAULT1
#define IDR_PROFILE_DEFAULT1 -1
#endif
#ifndef IDR_PROFILE_DEFAULT2
#define IDR_PROFILE_DEFAULT2 -1
#endif
#ifndef IDR_PROFILE_DEFAULT3
#define IDR_PROFILE_DEFAULT3 -1
#endif
#ifndef IDR_PROFILE_DEFAULT4
#define IDR_PROFILE_DEFAULT4 -1
#endif
void DumpStreams(FILE * fFile, string strMouse, string strDevs[], string strNull, bool bIsINI);
void DumpControllerSettings(FILE * fFile, int i, bool bIsINI);
void FormatControlsBlock(string * strMouse, string strDevs[], string * strNull, int i);
void FormatModifiersBlock(string * strMouse, string strDevs[], string * strNull, int i);
// return true if the file exists... let's just use CreateFile with OPEN_EXISTING
bool CheckFileExists( LPCTSTR FileName )
{
HANDLE hFile = CreateFile(FileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE )
{
return false;
}
else
{
CloseHandle(hFile);
return true;
}
}
// A rather ugly function, but does its job. Called by LoadProfile and LoadProfileFromResource.
// Parses the config data that gets written to profile files.
// returns:
// PL_CATEGORY and removes the brackets if the line looks like a keymapping
// PL_VERSIONSTRING and returns the version number if it's a version line
// for other cases, another PL return value and truncates before the equal sign; so strings like "Button=blah" -> "blah"
// TODO: Perhaps buffer overflow and crash potential here... needs auditing
DWORD ParseLine( LPSTR pszLine )
{
DWORD dwReturn = PL_NOHIT;
char *pChar = pszLine;
switch (pszLine[0])
{
case '\0': // shortcut out on null string
case '#': // # indicates comment line
return PL_NOHIT;
case '[':
while( *pChar != ']' && *pChar != '\0' )
{
*pChar = (char)toupper(*pChar);
++pChar;
}
if( *pChar == ']' )
{
MoveMemory( pszLine, pszLine+1, (pChar-pszLine) - 1 * sizeof(pszLine[0]) ); // TODO: please double check this --rabid
*(pChar - 1) = '\0'; // since we moved everything back one character, we need to change this ref as well
return PL_CATEGORY;
}
else
return PL_NOHIT; // an open bracket with no closing returns nohit
case '@':
switch( djbHash(pszLine)) // the hash check is case sensitive, and includes the @ symbol
{
case CHK_PROFILEVERSION20:
lstrcpyA( pszLine, "2.0" );
return PL_VERSIONSTRING;
case CHK_PROFILEVERSION21:
lstrcpyA( pszLine, "2.1" );
return PL_VERSIONSTRING;
case CHK_PROFILEVERSION22:
lstrcpyA( pszLine, "2.2" );
return PL_VERSIONSTRING;
default:
DebugWriteA("Unknown version string found with hash %u: %s\n", djbHash(pszLine), pszLine);
return PL_NOHIT;
} // end switch (dbjHash(pszLine))
// default: keep running
}
pChar = strchr(pszLine, '=');
if( !pChar ) // no = sign
{
return PL_NOHIT;
}
else // there is an '=' sign
{
// We hash the string. If the hash matches the hash of one of our targets, we compare strings to verify.
// If we don't use hashes, we have to compare vs a LOT of strings.
*pChar = '\0'; // truncate at the '=' for now
for (char *pIter = pszLine; *pIter; pIter++)
*pIter = (char)toupper(*pIter);
dwReturn = djbHash(pszLine);
pChar++;
MoveMemory( pszLine, pChar, (lstrlenA(pChar) + 1) * sizeof(pszLine[0]) ); // change string to match what's to the right of '='
}
return dwReturn;
}
// Called immediately after ParseLine to assign values based on whatever the keyname was
// notes: pszFFDevice may be overwritten with whatever is in pszLine; please make sure pszLine is not too big!
bool ProcessKey( DWORD dwKey, DWORD dwSection, LPCSTR pszLine, LPTSTR pszFFDevice, LPBYTE bFFDeviceNr, bool bIsInterface )
{
static TCHAR pszDeviceName[MAX_PATH];
static BYTE bDeviceNr = 0;
static GUID gGUID;
bool bReturn = true;
LPCONTROLLER pController = NULL; // used when we're assigning things in the [Controller X] category
LPSHORTCUTS pShortcuts = NULL;
unsigned int iLength = lstrlenA( pszLine ) / 2; // 2 HEX characters correspond to one BYTE; thus iLength represents the length of pszLine after conversion to BYTEs
switch (dwSection)
{
case CHK_CONTROLLER_1:
if (bIsInterface)
pController = &(g_ivConfig->Controllers[0]);
else
pController = &(g_pcControllers[0]);
break;
case CHK_CONTROLLER_2:
if (bIsInterface)
pController = &(g_ivConfig->Controllers[1]);
else
pController = &(g_pcControllers[1]);
break;
case CHK_CONTROLLER_3:
if (bIsInterface)
pController = &(g_ivConfig->Controllers[2]);
else
pController = &(g_pcControllers[2]);
break;
case CHK_CONTROLLER_4:
if (bIsInterface)
pController = &(g_ivConfig->Controllers[3]);
else
pController = &(g_pcControllers[3]);
break;
case CHK_SHORTCUTS:
if (bIsInterface)
pShortcuts = &(g_ivConfig->Shortcuts);
else
pShortcuts = &g_scShortcuts;
break;
}
switch( dwKey )
{
case PL_RESET:
ZeroMemory( pszDeviceName, sizeof(pszDeviceName) );
gGUID = GUID_NULL;
bDeviceNr = 0;
break;
case CHK_LANGUAGE:
if (dwSection == CHK_GENERAL)
if (bIsInterface)
g_ivConfig->Language = (LANGID)atoi(pszLine);
else
g_strEmuInfo.Language = (LANGID)atoi(pszLine);
break;
case CHK_SHOWMESSAGES:
if (dwSection == CHK_GENERAL)
if (bIsInterface)
g_ivConfig->fDisplayShortPop = (atoi(pszLine) != 0);
else
g_strEmuInfo.fDisplayShortPop = (atoi(pszLine) != 0);
break;
case CHK_MEMPAK:
if (dwSection == CHK_LASTBROWSERDIR)
CHAR_TO_TCHAR(g_aszLastBrowse[BF_MEMPAK], pszLine, MAX_PATH);
else if (dwSection == CHK_FOLDERS)
CHAR_TO_TCHAR(g_aszDefFolders[BF_MEMPAK], pszLine, MAX_PATH);
break;
case CHK_GBXROM:
if (dwSection == CHK_LASTBROWSERDIR)
CHAR_TO_TCHAR(g_aszLastBrowse[BF_GBROM], pszLine, MAX_PATH);
else if (dwSection == CHK_FOLDERS)
CHAR_TO_TCHAR(g_aszDefFolders[BF_GBROM], pszLine, MAX_PATH);
break;
case CHK_GBXSAVE:
if (dwSection == CHK_LASTBROWSERDIR)
CHAR_TO_TCHAR(g_aszLastBrowse[BF_GBSAVE], pszLine, MAX_PATH);
else if (dwSection == CHK_FOLDERS)
CHAR_TO_TCHAR(g_aszDefFolders[BF_GBSAVE], pszLine, MAX_PATH);
break;
case CHK_PROFILE:
if (dwSection == CHK_LASTBROWSERDIR)
CHAR_TO_TCHAR(g_aszLastBrowse[BF_PROFILE], pszLine, MAX_PATH);
break;
case CHK_NOTE:
if (dwSection == CHK_LASTBROWSERDIR)
CHAR_TO_TCHAR(g_aszLastBrowse[BF_NOTE], pszLine, MAX_PATH);
break;
case CHK_SHORTCUTS:
if (dwSection == CHK_LASTBROWSERDIR)
CHAR_TO_TCHAR(g_aszLastBrowse[BF_SHORTCUTS], pszLine, MAX_PATH);
break;
case CHK_PLUGGED:
if (pController)
pController->fPlugged = atoi(pszLine);
break;
case CHK_RAWDATA:
if (pController)
pController->fRawData = atoi(pszLine);
break;
case CHK_XINPUT:
if (pController)
pController->fXInput = atoi(pszLine);
break;
case CHK_PAKTYPE:
if (pController)
pController->PakType = atoi(pszLine);
break;
case CHK_REALN64RANGE:
if (pController)
pController->fRealN64Range = atoi(pszLine);
break;
case CHK_RAPIDFIREENABLED:
if (pController)
pController->bRapidFireEnabled = atoi(pszLine) != 0;
break;
case CHK_RAPIDFIRERATE:
if (pController)
pController->bRapidFireRate = (BYTE)atoi(pszLine);
break;
case CHK_STICKRANGE:
if (pController)
pController->bStickRange = (BYTE)atoi(pszLine);
break;
case CHK_MOUSEMOVEX:
if (pController)
pController->bMouseMoveX = atoi(pszLine);
break;
case CHK_MOUSEMOVEY:
if (pController)
pController->bMouseMoveY = atoi(pszLine);
break;
case CHK_AXISSET:
if (pController)
pController->bAxisSet = atoi(pszLine);
break;
case CHK_KEYABSOLUTEX:
if (pController)
pController->fKeyAbsoluteX = atoi(pszLine);
break;
case CHK_KEYABSOLUTEY:
if (pController)
pController->fKeyAbsoluteY = atoi(pszLine);
break;
case CHK_PADDEADZONE:
if (pController)
pController->bPadDeadZone = (BYTE)atoi(pszLine);
break;
case CHK_MOUSESENSITIVITYX:
if (pController)
pController->wMouseSensitivityX = (WORD)atoi(pszLine);
break;
case CHK_MOUSESENSITIVITYY:
if (pController)
pController->wMouseSensitivityY = (WORD)atoi(pszLine);
break;
case CHK_RUMBLETYPE:
if (pController)
pController->bRumbleTyp = (BYTE)atoi(pszLine);
break;
case CHK_RUMBLESTRENGTH:
if (pController)
pController->bRumbleStrength = (BYTE)atoi(pszLine);
break;
case CHK_VISUALRUMBLE:
if (pController)
pController->fVisualRumble = atoi(pszLine);
break;
case CHK_FFDEVICEGUID:
if (pController)
{
bReturn = StringtoGUIDA(&pController->guidFFDevice, pszLine);
if (bIsInterface && bReturn)
{
// For some reason, we use ONLY device names and numbers inside the interface for FF device selection. So if we don't set those,
// FFDevice won't load properly.
int nDevice = FindDeviceinList(pController->guidFFDevice);
if (nDevice != -1 && pszFFDevice && bFFDeviceNr)
{
_tcsncpy(pszFFDevice, g_devList[nDevice].szProductName, DEFAULT_BUFFER);
*bFFDeviceNr = g_devList[nDevice].bProductCounter;
}
else
{
pController->guidFFDevice = GUID_NULL;
return false;
}
}
else
return bReturn;
}
break;
case CHK_FFDEVICENAME:
if( pController && pszFFDevice )
{
CHAR_TO_TCHAR( pszFFDevice, pszLine, MAX_PATH ); // HACK: pszLine is read from a file; could overflow easily. guessed size of pszFFDevice buffer.
return true;
}
break;
case CHK_FFDEVICENR:
if( pController && bFFDeviceNr && ( iLength >= sizeof(BYTE) ))
{
*bFFDeviceNr = atoi( pszLine );
return true;
}
break;
case CHK_MEMPAKFILE:
if( pController )
{
CHAR_TO_TCHAR( pController->szMempakFile, pszLine, MAX_PATH );
}
break;
case CHK_GBROMFILE:
if( pController )
{
CHAR_TO_TCHAR( pController->szTransferRom, pszLine, MAX_PATH );
}
break;
case CHK_GBROMSAVE:
if( pController )
{
CHAR_TO_TCHAR( pController->szTransferSave, pszLine, MAX_PATH );
}
break;
case CHK_DINPUTNAME:
gGUID = GUID_NULL; // invalidate current GUID
CHAR_TO_TCHAR( pszDeviceName, pszLine, MAX_PATH );
break;
case CHK_DINPUTNR:
gGUID = GUID_NULL; // invalidate current GUID
if( iLength >= sizeof(BYTE) )
{
TexttoHexA( pszLine, &bDeviceNr, sizeof(BYTE) );
}
break;
case CHK_DINPUTGUID:
if (StringtoGUIDA(&gGUID, pszLine))
return true;
else
{
gGUID = GUID_NULL; // invalidate current GUID
return false;
}
break;
case CHK_BUTTON:
if ( dwSection == CHK_CONTROLS || pShortcuts || pController )
{
int controlnum = 0, buttonID = 0;
BUTTON btnWorking;
ZeroMemory(&btnWorking, sizeof(btnWorking));
unsigned int tOffset, tAxisID, tBtnType;
if (sscanf(pszLine, "%d %d %x %u %u", &controlnum, &buttonID, &tOffset, &tAxisID, &tBtnType) != 5)
return false;
// done to overcome issues with sscanf and "small" data blocks
btnWorking.bOffset = (BYTE)tOffset;
btnWorking.bAxisID = (BYTE)tAxisID;
btnWorking.bBtnType = (BYTE)tBtnType;
if (pController)
{
// special case: if we're in one of the categories CHK_CONTROLLER_n, assume we're processing a Profile file.
// Ignore the read controlnum and use our input controller number.
controlnum = (int)(dwSection - CHK_CONTROLLER_1); // HACK: assume our hash reproduces these linearly
}
// Now we need to assign parentdevice. If we have a valid gGUID, we'll use that...
int found = FindDeviceinList(gGUID);
if (found != -1)
btnWorking.parentDevice = &g_devList[found];
else
{
// ... otherwise, we do the following in order:
// 1. If bBtnType is of type DT_MOUSEBUTTON or DT_MOUSEAXE, set gGUID to that of g_sysMouse (ignoring the given name and number)
if ( btnWorking.bBtnType == DT_MOUSEBUTTON || btnWorking.bBtnType == DT_MOUSEAXE )
{
btnWorking.parentDevice = &g_sysMouse;
}
// 2. If bBtnType is of type DT_KEYBUTTON, set gGUID to that of SysKeyboard
else if ( btnWorking.bBtnType == DT_KEYBUTTON )
{
gGUID = GUID_SysKeyboard;
found = FindDeviceinList(gGUID);
if (found != -1)
btnWorking.parentDevice = &g_devList[found];
else
btnWorking.parentDevice = NULL;
}
// 3. otherwise, look up the name and number using FindDeviceinList, and set gGUID to that
else
{
found = FindDeviceinList(pszDeviceName, bDeviceNr, true);
if (found != -1)
{
gGUID = g_devList[found].guidInstance;
btnWorking.parentDevice = &g_devList[found];
}
else
{
DebugWrite(_T("ProcessKey: couldn't find a device in g_devList for %s %d\n"), pszDeviceName, bDeviceNr);
gGUID = GUID_NULL;
btnWorking.parentDevice = NULL;
return false;
}
}
}
if (pShortcuts)
{
// bounds check on controlnum and buttonID
if ( (controlnum == -1 && buttonID != 0) && ((controlnum < 0) || (controlnum > 3) || (buttonID < 0) || (buttonID >= SC_TOTAL)) )
{
gGUID = GUID_NULL; // since we may have cached an invalid GUID, invalidate it
return false;
}
// Copy the completed button to the correct shortcut
if (bIsInterface)
if (controlnum == -1)
g_ivConfig->Shortcuts.bMouseLock = btnWorking;
else
g_ivConfig->Shortcuts.Player[controlnum].aButtons[buttonID] = btnWorking;
else // if (!bIsInterface)
if (controlnum == -1)
g_scShortcuts.bMouseLock = btnWorking;
else
g_scShortcuts.Player[controlnum].aButtons[buttonID] = btnWorking;
}
else // it's a controller button
{
// bounds check on controlnum and buttonID
if ( (controlnum < 0) || (controlnum > 3) || (buttonID < 0) || (buttonID >= ARRAYSIZE(g_pcControllers[0].aButton)) )
{
gGUID = GUID_NULL; // since we may have cached an invalid GUID, invalidate it
return false;
}
// Copy the completed button to the correct controller and buttonID
if (bIsInterface)
g_ivConfig->Controllers[controlnum].aButton[buttonID] = btnWorking;
else
g_pcControllers[controlnum].aButton[buttonID] = btnWorking;
}
}
break;
case CHK_MODIFIER:
// Modifiers format: controlnum bOffset bAxisID bBtnType bModType fToggle fStatus dwSpecific
if ( dwSection == CHK_MODIFIERS || pController )
{
int controlnum = 0;
MODIFIER modWorking;
ZeroMemory(&modWorking, sizeof(modWorking));
unsigned int tOffset, tAxisID, tBtnType, tModType, tToggle, tStatus, tSpecific;
if (sscanf(pszLine, "%u %x %u %u %u %u %u %x", &controlnum, &tOffset, &tAxisID,
&tBtnType, &tModType, &tToggle, &tStatus, &tSpecific) != 8)
return false;
// done to overcome issues with sscanf and "small" data blocks
modWorking.btnButton.bOffset = (BYTE)tOffset;
modWorking.btnButton.bAxisID = (BYTE)tAxisID;
modWorking.btnButton.bBtnType = (BYTE)tBtnType;
modWorking.bModType = (BYTE)tModType;
modWorking.fToggle = tToggle;
modWorking.fStatus = tStatus;
modWorking.dwSpecific = tSpecific; // looks stupid, but unsigned int might not always be DWORD32
// Now we need to assign parentdevice. If we have a valid gGUID, we'll use that...
int found = FindDeviceinList(gGUID);
if (found != -1)
modWorking.btnButton.parentDevice = &g_devList[found];
else
{
// ... otherwise, we do the following in order:
// 1. If bBtnType is of type DT_MOUSEBUTTON or DT_MOUSEAXE, set gGUID to that of g_sysMouse (ignoring the given name and number)
if ( modWorking.btnButton.bBtnType == DT_MOUSEBUTTON || modWorking.btnButton.bBtnType == DT_MOUSEAXE )
{
modWorking.btnButton.parentDevice = &g_sysMouse;
}
// 2. If bBtnType is of type DT_KEYBUTTON, set gGUID to that of SysKeyboard
else if ( modWorking.btnButton.bBtnType == DT_KEYBUTTON )
{
gGUID = GUID_SysKeyboard;
int found = FindDeviceinList(gGUID);
if (found != -1)
modWorking.btnButton.parentDevice = &g_devList[found];
else
modWorking.btnButton.parentDevice = NULL;
}
// 3. otherwise, look up the name and number using FindDeviceinList, and set gGUID to that
else
{
found = FindDeviceinList(pszDeviceName, bDeviceNr, true);
if (found != -1)
{
gGUID = g_devList[found].guidInstance;
modWorking.btnButton.parentDevice = &g_devList[found];
}
else
{
DebugWrite(_T("ProcessKey: couldn't find a device in g_devList for %s %d\n"), pszDeviceName, bDeviceNr);
gGUID = GUID_NULL;
modWorking.btnButton.parentDevice = NULL;
return false;
}
}
}
// bounds check on controlnum and buttonID
if ( (controlnum < 0) || (controlnum > 3) )
{
gGUID = GUID_NULL; // since we may have cached an invalid GUID, invalidate it
return false;
}
// Allocate and add the completed modifier
if (bIsInterface)
{
if (g_ivConfig->Controllers[controlnum].nModifiers > 0)
{
g_ivConfig->Controllers[controlnum].pModifiers = (LPMODIFIER)P_realloc(g_ivConfig->Controllers[controlnum].pModifiers, (g_ivConfig->Controllers[controlnum].nModifiers + 1) * sizeof(MODIFIER));
}
else
{
g_ivConfig->Controllers[controlnum].pModifiers = (LPMODIFIER)P_malloc( sizeof(MODIFIER));
}
g_ivConfig->Controllers[controlnum].pModifiers[g_ivConfig->Controllers[controlnum].nModifiers] = modWorking;
(g_ivConfig->Controllers[controlnum].nModifiers)++;
}
else
{
if (g_pcControllers[controlnum].nModifiers > 0)
{
g_pcControllers[controlnum].pModifiers = (LPMODIFIER)P_realloc(g_pcControllers[controlnum].pModifiers, (g_pcControllers[controlnum].nModifiers + 1) * sizeof(MODIFIER));
}
else
{
g_pcControllers[controlnum].pModifiers = (LPMODIFIER)P_malloc( sizeof(MODIFIER));
}
g_pcControllers[controlnum].pModifiers[g_pcControllers[controlnum].nModifiers] = modWorking;
(g_pcControllers[controlnum].nModifiers)++;
}
}
break;
}
return bReturn;
}
/******************
Load the default profile from the raw "resource" data (i.e. the builtin defaults contained in the dll)
******************/
bool LoadProfileFromResource( LPCTSTR pszResource, int iController, bool bIsInterface )
{
const DWORD dwControllerSect[] = { CHK_CONTROLLER_1 , CHK_CONTROLLER_2, CHK_CONTROLLER_3, CHK_CONTROLLER_4 };
if( iController > 3 || iController < 0 )
return false;
HRSRC res = FindResource( g_strEmuInfo.hinst, pszResource, _T("PROFILE") );
if( res == NULL )
return false;
char *profile = (char*)LockResource( LoadResource( g_strEmuInfo.hinst, res ));
char *profileend = profile + SizeofResource( g_strEmuInfo.hinst, res );
ProcessKey( PL_RESET, 0, 0, 0, 0, bIsInterface );
DWORD dwCommand = PL_NOHIT;
char szLine[4096];
while( profile < profileend )
{
while( profile < profileend && (CHECK_WHITESPACES( *profile ) || *profile == ' ' ))
++profile;
int i = 0;
while( profile < profileend && i < sizeof(szLine)-1 && !(CHECK_WHITESPACES( *profile )) )
szLine[i++] = *profile++;
szLine[i] = '\0';
dwCommand = ParseLine( szLine );
ProcessKey( dwCommand, dwControllerSect[iController], szLine, 0, 0, bIsInterface ); // resource will not contain a FF device
}
return true;
}
/******************
See overloaded function above
******************/
bool LoadProfileFromResource( int indexController, bool bIsInterface )
{
const int resIds[] = { IDR_PROFILE_DEFAULT1, IDR_PROFILE_DEFAULT2, IDR_PROFILE_DEFAULT3, IDR_PROFILE_DEFAULT4 };
TCHAR szId[20];
wsprintf( szId, _T("#%i"), resIds[indexController] );
return LoadProfileFromResource( szId, indexController, bIsInterface );
}
// Load a controller profile from a saved configuration file
// need to incorporate type (keyb/mouse/joy), GUID for joy, and bOffset
bool LoadProfileFile( const TCHAR *pszFileName, int iController, TCHAR *pszFFDevice, BYTE *bFFDeviceNr )
{
const DWORD dwControllerSect[] = { CHK_CONTROLLER_1 , CHK_CONTROLLER_2, CHK_CONTROLLER_3, CHK_CONTROLLER_4 };
FILE *proFile = NULL;
char szLine[4096];
int iVersion = 0;
if ( (proFile = _tfopen(pszFileName, _T("rS")) ) == NULL)
return false;
// Test if right Version
while( !iVersion && ( fgets(szLine, sizeof(szLine) - 1, proFile) ) )
{
szLine[strlen(szLine) - 1] = '\0'; // remove newline
if( ParseLine( szLine ) == PL_VERSIONSTRING )
iVersion = (int)(atof( szLine ) * 100);
}
if( iVersion != 220 ) // HACK: this should probably not be a hardcoded value
{
fclose(proFile);
return false;
}
SetControllerDefaults( &(g_ivConfig->Controllers[iController]) );
pszFFDevice[0] = pszFFDevice[1] = '\0';
*bFFDeviceNr = 0;
ProcessKey( PL_RESET, 0, 0, 0, 0, true );
DWORD dwCommand = PL_NOHIT;
while( fgets(szLine, sizeof(szLine) - 1, proFile) )
{
szLine[strlen(szLine) - 1] = '\0'; // remove newline
dwCommand = ParseLine( szLine );
ProcessKey( dwCommand, dwControllerSect[iController], szLine, pszFFDevice, bFFDeviceNr, true );
}
fclose(proFile);
return true;
}
// Load a controller profile from a saved configuration file
// need to incorporate type (keyb/mouse/joy), GUID for joy, and bOffset
bool LoadShortcutsFile( const TCHAR *pszFileName )
{
FILE *fShortsFile = NULL;
char szLine[4096];
int iVersion = 0;
if ( (fShortsFile = _tfopen(pszFileName, _T("rS")) ) == NULL)
return false;
// Test if right Version
while( !iVersion && ( fgets(szLine, sizeof(szLine) - 1, fShortsFile) ) )
{
szLine[strlen(szLine) - 1] = '\0'; // remove newline
if( ParseLine( szLine ) == PL_VERSIONSTRING )
iVersion = (int)(atof( szLine ) * 100);
}
if( iVersion != 220 ) // HACK: this should probably not be a hardcoded value
{
fclose(fShortsFile);
return false;
}
ZeroMemory( &(g_ivConfig->Shortcuts), sizeof(SHORTCUTS) );
ProcessKey( PL_RESET, 0, 0, 0, 0, true );
DWORD dwCommand = PL_NOHIT;
while( fgets(szLine, sizeof(szLine) - 1, fShortsFile) )
{
szLine[strlen(szLine) - 1] = '\0'; // remove newline
dwCommand = ParseLine( szLine );
ProcessKey( dwCommand, CHK_SHORTCUTS, szLine, 0, 0, true );
}
fclose(fShortsFile);
return true;
}
// Serializes the profile for the CURRENT controller for saving to a file
// called in one place, from within Interface.cpp, ControllerTabProc (when you click "Save Profile")
void FormatProfileBlock( FILE * fFile, const int i )
{
DumpControllerSettings(fFile, i, false);
string strMouse;
string strDevs[MAX_DEVICES];
string strNull;
FormatControlsBlock(&strMouse, strDevs, &strNull, i);
DumpStreams(fFile, strMouse, strDevs, strNull, false);
strMouse.clear();
for (int j = 0; j < g_nDevices; j++)
strDevs[j].clear();
strNull.clear();
FormatModifiersBlock(&strMouse, strDevs, &strNull, i);
DumpStreams(fFile, strMouse, strDevs, strNull, false);
}
// same as FormatProfileBlock, but saves shortcuts instead
void FormatShortcutsBlock(FILE * fFile, bool bIsINI)
{
// I'm going to use STL strings here because I don't want to screw with buffer management
string strMouse;
string strDevs[MAX_DEVICES];
string strNull;
for ( int i = 0; i < 4; i++ ) // Player for
{
for ( int j = 0; j < SC_TOTAL; j++ ) // aButtons for
{
if (g_ivConfig->Shortcuts.Player[i].aButtons[j].parentDevice) // possibly unbound
{
if ( IsEqualGUID(g_sysMouse.guidInstance, g_ivConfig->Shortcuts.Player[i].aButtons[j].parentDevice->guidInstance) )
{
char szBuf[DEFAULT_BUFFER];
// add to the mouse stream
sprintf(szBuf, STRING_INI_BUTTON "=%d %d %02X %d %d\n", i, j, g_ivConfig->Shortcuts.Player[i].aButtons[j].bOffset, g_ivConfig->Shortcuts.Player[i].aButtons[j].bAxisID, g_ivConfig->Shortcuts.Player[i].aButtons[j].bBtnType);
strMouse.append(szBuf);
}
else
for (int match = 0; match < g_nDevices; match++)
if ( IsEqualGUID(g_devList[match].guidInstance, g_ivConfig->Shortcuts.Player[i].aButtons[j].parentDevice->guidInstance) )
{
char szBuf[DEFAULT_BUFFER];
// add to the appropriate device stream
sprintf(szBuf, STRING_INI_BUTTON "=%d %d %02X %d %d\n", i, j, g_ivConfig->Shortcuts.Player[i].aButtons[j].bOffset, g_ivConfig->Shortcuts.Player[i].aButtons[j].bAxisID, g_ivConfig->Shortcuts.Player[i].aButtons[j].bBtnType);
strDevs[match].append(szBuf);
break;
}
}
} // end buttons for
} // end Player for
// gotta do it again for that one pesky mouselock button
if (g_ivConfig->Shortcuts.bMouseLock.parentDevice) // possibly unbound
{
if ( IsEqualGUID(g_sysMouse.guidInstance, g_ivConfig->Shortcuts.bMouseLock.parentDevice->guidInstance) )
{
char szBuf[DEFAULT_BUFFER];
// add to the mouse stream
sprintf(szBuf, STRING_INI_BUTTON "=%d %d %02X %d %d\n", -1, 0, g_ivConfig->Shortcuts.bMouseLock.bOffset, g_ivConfig->Shortcuts.bMouseLock.bAxisID, g_ivConfig->Shortcuts.bMouseLock.bBtnType);
strMouse.append(szBuf);
}
else
for (int match = 0; match < g_nDevices; match++)
if ( IsEqualGUID(g_devList[match].guidInstance, g_ivConfig->Shortcuts.bMouseLock.parentDevice->guidInstance) )
{
char szBuf[DEFAULT_BUFFER];
// add to the appropriate device stream
sprintf(szBuf, STRING_INI_BUTTON "=%d %d %02X %d %d\n", -1, 0, g_ivConfig->Shortcuts.bMouseLock.bOffset, g_ivConfig->Shortcuts.bMouseLock.bAxisID, g_ivConfig->Shortcuts.bMouseLock.bBtnType);
strDevs[match].append(szBuf);
break;
}
} // end shortcuts edge case
DumpStreams(fFile, strMouse, strDevs, strNull, bIsINI);
}
// load shortcuts from "resources", i.e. builtin defaults
bool LoadShortcutsFromResource(bool bIsInterface)
{
if (bIsInterface)
ZeroMemory( &(g_ivConfig->Shortcuts), sizeof(SHORTCUTS) );
TCHAR szId[20];
wsprintf( szId, _T("#%i"), IDR_SHORTCUTS_DEFAULT );
HRSRC res = FindResource( g_strEmuInfo.hinst, szId, _T("SHORTCUT") );
if( res == NULL )
return false;
char *profile = (char*)LockResource( LoadResource( g_strEmuInfo.hinst, res ));
char *profileend = profile + SizeofResource( g_strEmuInfo.hinst, res );
ProcessKey( PL_RESET, 0, 0, 0, 0, bIsInterface );
DWORD dwCommand = PL_NOHIT;
char szLine[4096];
while( profile < profileend )
{
while( profile < profileend && (CHECK_WHITESPACES( *profile ) || *profile == ' ' ))
++profile;
int i = 0;
while( profile < profileend && i < sizeof(szLine)-1 && !(CHECK_WHITESPACES( *profile )) )
szLine[i++] = *profile++;
szLine[i] = '\0';
dwCommand = ParseLine( szLine );
ProcessKey( dwCommand, CHK_SHORTCUTS, szLine, 0, 0, bIsInterface );
}
return true;
}
// returns the user-chosen default directory (path) for each of the following:
// application dir, mempak dir, gameboy rom dir, gameboyrom save dir
// Tries to query user settings; if blank or invalid, returns their defaults
// Massages the output directory a bit
bool GetDirectory( LPTSTR pszDirectory, WORD wDirID )
{
bool bReturn = true;
TCHAR szBuffer[MAX_PATH + 1];
const TCHAR szDefaultStrings[3][DEFAULT_BUFFER] = { STRING_DEF_MEMPAKFILE, STRING_DEF_GBROMFILE, STRING_DEF_GBROMSAVE };
TCHAR *pSlash;
pszDirectory[0] = pszDirectory[1] = '\0';
switch( wDirID )
{
case DIRECTORY_MEMPAK:
case DIRECTORY_GBROMS:
case DIRECTORY_GBSAVES:
if (g_aszDefFolders[wDirID][0] == 0)
lstrcpyn( pszDirectory, szDefaultStrings[wDirID], MAX_PATH);
else
lstrcpyn( pszDirectory, g_aszDefFolders[wDirID], MAX_PATH);
break;
case DIRECTORY_DLL:
if (GetModuleFileName(g_strEmuInfo.hinst, szBuffer, MAX_PATH))
{
GetFullPathName( szBuffer, MAX_PATH, pszDirectory, &pSlash );
*pSlash = 0;
}
break;
case DIRECTORY_APPLICATION:
break;
default:
// we don't know what the hell you're talking about, set pszFileName to current .exe directory
// and return false
bReturn = false;
}
if( pszDirectory[1] == ':' || ( pszDirectory[1] == '\\' && pszDirectory[0] == '\\' )) // Absolute Path( x: or \\ )
lstrcpyn( szBuffer, pszDirectory, MAX_PATH );
else
{
GetModuleFileName( NULL, szBuffer, MAX_PATH );
pSlash = _tcsrchr( szBuffer, '\\' );
++pSlash;
lstrcpyn( pSlash, pszDirectory, MAX_PATH );
}
GetFullPathName( szBuffer, MAX_PATH, pszDirectory, &pSlash );
pSlash = &pszDirectory[lstrlen( pszDirectory ) - 1];
if( *pSlash != '\\' )
{
pSlash[1] = '\\';
pSlash[2] = '\0';
}
return bReturn;
}
// Attempts to store the "absolute" filename for a file;
// if szFileName is an absolute filename (starting with a letter and colon or two backslashes) it is simply copied
// otherwise, it is concatenated with the known directory, such as mempak directory (type given by wDirID)
void GetAbsoluteFileName( TCHAR *szAbsolute, const TCHAR *szFileName, const WORD wDirID )
{
if( szFileName[1] == ':' || (szFileName[1] == '\\' && szFileName[0] == '\\'))
lstrcpyn( szAbsolute, szFileName, MAX_PATH );
else
{
GetDirectory( szAbsolute, wDirID );
lstrcat( szAbsolute, szFileName); // HACK: possible buffer overflow
}
}
// Populates the list of mempak/transfer pak files from the config'd directory
BOOL SendFilestoList( HWND hDlgItem, WORD wType )
{
HANDLE hFindFile;
WIN32_FIND_DATA FindFile;
TCHAR szPattern[MAX_PATH + 10];
TCHAR *pszExtensions;
BOOL Success;
switch( wType )
{
case FILIST_MEM:
GetDirectory( szPattern, DIRECTORY_MEMPAK );
lstrcat( szPattern, _T("*.*") );
pszExtensions = _T(".mpk\0.n64\0");
break;
case FILIST_TRANSFER:
GetDirectory( szPattern, DIRECTORY_GBROMS );
lstrcat( szPattern, _T("*.gb?") );
pszExtensions = _T(".gb\0.gbc\0");
break;
default:
return FALSE;
}
TCHAR *pcPoint;
TCHAR *pszExt;
bool bValidFile;
hFindFile = FindFirstFile( szPattern, &FindFile );
if( hFindFile != INVALID_HANDLE_VALUE )
{
do
{
pszExt = pszExtensions;
pcPoint = _tcsrchr( FindFile.cFileName, _T('.') );
bValidFile = false;
do
{
if( !lstrcmpi( pcPoint, pszExt ))
bValidFile = true;
pszExt += lstrlen( pszExt ) + 1;
}
while( *pszExt && !bValidFile );
if( bValidFile )
SendMessage( hDlgItem, LB_ADDSTRING, 0, (LPARAM)FindFile.cFileName );
}
while( FindNextFile( hFindFile, &FindFile ));
FindClose( hFindFile );
Success = TRUE;
}
else
Success = FALSE;
return Success;
}
bool BrowseFolders( HWND hwndParent, TCHAR *pszHeader, TCHAR *pszDirectory )
{
ITEMIDLIST *piStart = NULL;
if( pszDirectory[0] != '\0')
{
IShellFolder* pDesktopFolder;
if( SUCCEEDED( SHGetDesktopFolder( &pDesktopFolder )))
{
OLECHAR olePath[MAX_PATH];
ULONG chEaten;