-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXBoxMemoryCard.cpp
More file actions
1408 lines (1126 loc) · 33 KB
/
Copy pathXBoxMemoryCard.cpp
File metadata and controls
1408 lines (1126 loc) · 33 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
// XBox memory card access
#include "Common.h"
#if TARGET==XBOX
#include "MemoryCard.h"
#include "resourceaccumulator.h"
#include "d3d8.h"
#include "texture.h"
#include "xboxdx.h"
#include "xgraphics.h"
#include "xbox.h"
#include "frontend.h"
#define SAVE_FILE_NAME "SAVE.DAT"
#define MAX_NAME_LEN 40
#define CLAMP_SIZE(n) {if ((n) > 50000 * XBOX_BLOCK_SIZE) (n) = 50000 * XBOX_BLOCK_SIZE;}
//---------------------------------------------------------------------------
class CXBoxSaveHeader
{
public:
DWORD mMagic;
DWORD mVersion;
XCALCSIG_SIGNATURE mSig;
void CreateHeader(BYTE *data,DWORD datasize);
BOOL CheckHeader(BYTE *data,DWORD datasize);
};
//---------------------------------------------------------------------------
void CXBoxSaveHeader::CreateHeader(BYTE *data,DWORD datasize)
{
HANDLE sigh=XCalculateSignatureBegin(0);
XCalculateSignatureUpdate(sigh,data,datasize);
XCalculateSignatureEnd(sigh,&mSig);
mMagic=MKID("NEKO");
mVersion=100;
}
//---------------------------------------------------------------------------
BOOL CXBoxSaveHeader::CheckHeader(BYTE *data,DWORD datasize)
{
if (mMagic!=MKID("NEKO"))
return(FALSE);
if (mVersion!=100)
return(FALSE);
XCALCSIG_SIGNATURE sig;
HANDLE sigh=XCalculateSignatureBegin(0);
XCalculateSignatureUpdate(sigh,data,datasize);
XCalculateSignatureEnd(sigh,&sig);
if (memcmp(&sig,&mSig,sizeof(mSig))!=0)
return(FALSE);
return(TRUE);
}
//---------------------------------------------------------------------------
void CXBoxMemoryCard::InitialiseOnce()
{
// this state has to persist across levels so isn't reset.
memset(mLastAskedHash, 0, sizeof(mLastAskedHash));
for (int i=0;i<TOTAL_XBOX_CARDS;i++)
{
mCardNeedsUpdating[i]=MODULE_INIT;
for (int j=0;j<MAX_MEMORY_CARD_SAVES;j++)
{
mCardSaveName[i][j]=NULL;
}
}
mRememberedInsertedCards = mRememberedRemovedCards = 0;
mCurrentDeviceBitmap=0;
}
//---------------------------------------------------------------------------
void CXBoxMemoryCard::Initialise()
{
mSaveImage=CTEXTURE::GetTextureByName("FrontEnd\\v2\\FE_XB_SaveGame.tga",TEXFMT_UNKNOWN,TEX_NORMAL,1);
}
//---------------------------------------------------------------------------
void CXBoxMemoryCard::Shutdown()
{
SAFE_RELEASE(mSaveImage);
for (int i=0;i<TOTAL_XBOX_CARDS;i++)
{
for (int j=0;j<MAX_MEMORY_CARD_SAVES;j++)
{
SAFE_DELETE_ARRAY(mCardSaveName[i][j]);
}
}
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetNumCards(int *num)
{
*num=8;
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
BOOL CXBoxMemoryCard::IsHDDAvailable()
{
return(TRUE);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetCardType(int card,int *cardtype)
{
if (card==-1)
*cardtype=MCT_HDD;
else
*cardtype=MCT_CARD;
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
DWORD CXBoxMemoryCard::GetPort(int card)
{
if ((card==0) || (card==1))
return(XDEVICE_PORT0);
else if ((card==2) || (card==3))
return(XDEVICE_PORT1);
else if ((card==4) || (card==5))
return(XDEVICE_PORT2);
else
return(XDEVICE_PORT3);
}
//---------------------------------------------------------------------------
DWORD CXBoxMemoryCard::GetSlot(int card)
{
if ((card==0) || (card==2) || (card==4) || (card==6))
return(XDEVICE_TOP_SLOT);
else
return(XDEVICE_BOTTOM_SLOT);
}
//---------------------------------------------------------------------------
void CXBoxMemoryCard::UpdateCardSaveInfo(int card,char drive)
{
char path[16];
sprintf(path,"%c:\\",drive);
XGAME_FIND_DATA data;
mCardSaves[card]=0;
HANDLE h=XFindFirstSaveGame(path,&data);
if (h==INVALID_HANDLE_VALUE)
{
// Could not enumerate for some reason, or no games found
return;
}
else
{
do
{
if (!mCardSaveName[card][mCardSaves[card]])
mCardSaveName[card][mCardSaves[card]]=new (MEMTYPE_MEMORYCARD) WCHAR[MAX_NAME_LEN];
wcsncpy(mCardSaveName[card][mCardSaves[card]],data.szSaveGameName, MAX_NAME_LEN - 1);
mCardSaves[card]++;
CONSOLE.PingLoadingScreen();
}
while (XFindNextSaveGame(h,&data));
XFindClose(h);
}
}
//---------------------------------------------------------------------------
void CXBoxMemoryCard::GetMountedCardFreeAndTotalBytes(int card, char drive)
{
if (card == -1) card = TOTAL_XBOX_CARDS-1;
ULARGE_INTEGER freebytes,totalbytes;
char dirname[16];
sprintf(dirname,"%c:\\",drive);
GetDiskFreeSpaceEx(dirname,&freebytes,&totalbytes,NULL);
int sfreebytes,stotalbytes;
if (freebytes.HighPart!=0 || freebytes.LowPart > 0x7ffffff)
sfreebytes=0x7FFFFFFF;
else
sfreebytes=freebytes.LowPart;
if (totalbytes.HighPart!=0 || totalbytes.LowPart > 0x7ffffff)
stotalbytes=0x7FFFFFFF;
else
stotalbytes=totalbytes.LowPart;
CLAMP_SIZE(stotalbytes);
CLAMP_SIZE(sfreebytes );
mCardFormatted[card]=TRUE;
mCardBytesTotal[card]=stotalbytes;
mCardBytesFree[card]=sfreebytes;
}
//---------------------------------------------------------------------------
void CXBoxMemoryCard::UpdateCardInfo(int card)
{
if (card==-1)
card=(TOTAL_XBOX_CARDS-1);
if (card==(TOTAL_XBOX_CARDS-1))
{
// Hard drive
mCardPresent[card]=TRUE;
mCardFormatted[card]=TRUE;
mCardNeedsUpdating[card]=NO;
wcscpy(mCardName[card],ToWCHAR(""));
ULARGE_INTEGER freebytes,totalbytes;
char dirname[16];
sprintf(dirname,"U:\\");
GetDiskFreeSpaceEx(dirname,&freebytes,&totalbytes,NULL);
int sfreebytes,stotalbytes;
if (freebytes.HighPart!=0 || freebytes.LowPart > 0x7fffffff)
sfreebytes=0x7FFFFFFF;
else
sfreebytes=freebytes.LowPart;
if (totalbytes.HighPart!=0 || totalbytes.LowPart > 0x7fffffff)
stotalbytes=0x7FFFFFFF;
else
stotalbytes=totalbytes.LowPart;
CLAMP_SIZE(stotalbytes);
CLAMP_SIZE(sfreebytes );
mCardBytesTotal[card]=stotalbytes;
mCardBytesFree[card]=sfreebytes;
UpdateCardSaveInfo(card,'U');
}
else
{
// Memory card
mCardNeedsUpdating[card]=NO;
DWORD connectedcards=mCurrentDeviceBitmap;
if (card==0)
connectedcards&=XDEVICE_PORT0_TOP_MASK;
else if (card==1)
connectedcards&=XDEVICE_PORT0_BOTTOM_MASK;
else if (card==2)
connectedcards&=XDEVICE_PORT1_TOP_MASK;
else if (card==3)
connectedcards&=XDEVICE_PORT1_BOTTOM_MASK;
else if (card==4)
connectedcards&=XDEVICE_PORT2_TOP_MASK;
else if (card==5)
connectedcards&=XDEVICE_PORT2_BOTTOM_MASK;
else if (card==6)
connectedcards&=XDEVICE_PORT3_TOP_MASK;
else if (card==7)
connectedcards&=XDEVICE_PORT3_BOTTOM_MASK;
if (connectedcards==0)
{
// Not present
mCardPresent[card]=FALSE;
mCardFormatted[card]=FALSE;
mCardBytesTotal[card]=0;
mCardBytesFree[card]=0;
mCardSaves[card]=0;
// but we leave the old name in, in case someone wants it
}
else
{
// Present
mCardPresent[card]=TRUE;
char drive;
DWORD res=XMountMU(GetPort(card),GetSlot(card),&drive);
if (res!=ERROR_SUCCESS)
{
// Mount failed
if (res==ERROR_DISK_FULL)
{
// MU full. Because of TCR 1.7-2-22 let's just pretend it's crap. Even though we've written all the code to allow
// the user to go to the Dash and sort it out.
// The TCR sentence is:
// Xbox MUs that fail to mount must be visually distinct in storage selection UI and
// must not be player selectable as a read/write target.
mCardFormatted[card]=FALSE;
mCardBytesTotal[card]=0;
mCardBytesFree[card]=0;
mCardSaves[card]=0;
wcscpy(mCardName[card],ToWCHAR(""));
}
else if (res==ERROR_DEVICE_NOT_CONNECTED)
{
// Removed
mCardPresent[card]=FALSE;
mCardFormatted[card]=FALSE;
mCardBytesTotal[card]=0;
mCardBytesFree[card]=0;
mCardSaves[card]=0;
// but we leave the old name in, in case someone wants it
}
else if (res==ERROR_ALREADY_ASSIGNED)
{
// Eh? Oh dear...
TRACE("Warning : XMount() retured ERROR_ALREADY_ASSIGNED!\n");
mCardPresent[card]=FALSE;
mCardFormatted[card]=FALSE;
mCardBytesTotal[card]=0;
mCardBytesFree[card]=0;
mCardSaves[card]=0;
// but we leave the old name in, in case someone wants it
}
else
{
char buf[256];
sprintf(buf,"Error %d\n",res);
TRACE(buf);
// Unformatted or otherwise unusable
mCardPresent[card]=TRUE;
mCardFormatted[card]=FALSE;
mCardBytesTotal[card]=0;
mCardBytesFree[card]=0;
mCardSaves[card]=0;
wcscpy(mCardName[card],ToWCHAR(""));
}
}
else
{
// Mount succeeded
GetMountedCardFreeAndTotalBytes(card, drive);
// Get the user name for this MU
if (XMUNameFromDriveLetter(drive,mCardName[card],MAX_MUNAME)!=ERROR_SUCCESS)
{
wcscpy(mCardName[card],ToWCHAR(""));
}
// Some of the characters in the memory card names aren't valid in our font because we've nicked them for special characters such
// as joypad buttons.
for (WCHAR *walker = mCardName[card]; *walker; walker++)
{
for (const WCHAR *lookup_walker = CBitmapFont::HIJACKED_FONT_ENTRIES; *lookup_walker; lookup_walker++)
{
if (*walker == *lookup_walker)
{
// this one here is an example.
// Let's turn it into a null character.
*walker = 0xdead; // pick any old value, as long as it's not in our font so the font draw will come up with the null character
// and don't keep looking
break;
}
}
}
UpdateCardSaveInfo(card,drive);
if (XUnmountMU(GetPort(card),GetSlot(card))!=ERROR_SUCCESS)
{
TRACE("Warning : XUnmountMU failed!\n");
}
}
}
}
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetCardInfo(int card,BOOL *present,BOOL *formatted,int *bytesfree,int *bytestotal)
{
int cardindex=card;
if (card==-1)
cardindex=TOTAL_XBOX_CARDS-1;
if (present)
*present=mCardPresent[cardindex];
if (formatted)
*formatted=mCardFormatted[cardindex];
if (bytesfree)
*bytesfree=mCardBytesFree[cardindex];
if (bytestotal)
*bytestotal=mCardBytesTotal[cardindex];
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetCardName(int card,WCHAR *buffer)
{
if (card==-1)
wcscpy(buffer,FET.GetText(EFETextHack(FEX_XBOX_HARD_DISK)));
else
wcscpy(buffer,FET.GetText(EFETextHack(FEX_MU_1A + card)));
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetCardName2(int card,WCHAR *buffer)
{
buffer[0] = 0;
if (card != -1)
{
// mCardName may have something in it even if the card isn't present.
// this will be the name of the previous
// memory card, you can get at that with GetCardOrPreviousCardName2
if (mCardPresent[card] && mCardName[card][0])
{
wcscpy(buffer,ToWCHAR(" ("));
wcscat(buffer,mCardName[card]);
wcscat(buffer,ToWCHAR(")"));
}
else
{
}
}
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetCardOrPreviousCardName2(int card,WCHAR *buffer)
{
buffer[0] = 0;
if (card==-1)
wcscpy(buffer,FET.GetText(EFETextHack(FEX_XBOX_HARD_DISK)));
else
{
if (mCardName[card][0])
{
wcscpy(buffer,mCardName[card]);
}
else
{
wcscpy(buffer,FET.GetText(EFETextHack(FEX_MU_1A + card)));
}
}
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::Format(int card)
{
SASSERT(0,"CXBoxMemoryCard::Format() - Not appropriate on XBOX, you shouldn't get here.");
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::Unformat(int card)
{
SASSERT(0,"CXBoxMemoryCard::Unformat() - Not appropriate on XBOX, you shouldn't get here.");
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetNumSaves(int card,int *num)
{
int cardindex=card;
if (card==-1)
cardindex=TOTAL_XBOX_CARDS-1;
*num=mCardSaves[cardindex];
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::GetSaveName(int card,int save,WCHAR *name)
{
int cardindex=card;
if (card==-1)
cardindex=TOTAL_XBOX_CARDS-1;
wcscpy(name,mCardSaveName[cardindex][save]);
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::WriteSaveImage(char *directory,CDXTexture *image)
{
// Determine size of incoming texture
D3DSURFACE_DESC SrcDesc;
HRESULT hr = image->GetTexture()->GetLevelDesc( 0, &SrcDesc );
if (FAILED(hr))
return(MCE_FAILURE);
// Generate a 64x64 DXT1 surface
LPDIRECT3DSURFACE8 pDxt1GameImageSurface = NULL;
hr = LT.GetDevice()->CreateImageSurface( 64, 64, D3DFMT_DXT1,&pDxt1GameImageSurface );
if (FAILED(hr))
return(MCE_FAILURE);
// Copy the source into the DXT1 surface
LPDIRECT3DSURFACE8 pSrcSurface = NULL;
hr = image->GetTexture()->GetSurfaceLevel( 0, &pSrcSurface );
hr = D3DXLoadSurfaceFromSurface( pDxt1GameImageSurface, NULL, NULL, pSrcSurface, NULL, NULL, D3DX_DEFAULT, D3DCOLOR( 0 ) );
SAFE_RELEASE(pSrcSurface);
if (FAILED(hr))
{
SAFE_RELEASE(pDxt1GameImageSurface);
return(MCE_FAILURE);
}
// Generate the save game image name
CHAR strMetaDataFile[MAX_PATH];
lstrcpyA(strMetaDataFile,directory);
lstrcatA(strMetaDataFile,"saveimage.xbx");
// Write the compressed texture out to an .XPR file
hr=XGWriteSurfaceOrTextureToXPR( pDxt1GameImageSurface, strMetaDataFile, TRUE );
SAFE_RELEASE(pDxt1GameImageSurface);
if (FAILED(hr))
return(MCE_FAILURE);
return(MCE_SUCCESS);
}
//---------------------------------------------------------------------------
static int count_num_files(char drive)
{
char path[16];
sprintf(path, "%c:\\*.*", drive);
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(path, &data);
int num_files = 0;
while (h != INVALID_HANDLE_VALUE)
{
num_files++;
if (!FindNextFile(h, &data)) break;
}
FindClose(h);
return num_files;
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::CountNumHardDriveFiles()
{
return count_num_files('U');
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::CreateSave(int card,WCHAR *name,int *save,BOOL allowed_overwrite)
{
if (save)
*save=-1;
// Check to see if a save with that name already exists
int cardindex=card;
if (card==-1)
cardindex=TOTAL_XBOX_CARDS-1;
if (!allowed_overwrite)
{
for (int i=0;i<mCardSaves[cardindex];i++)
{
if (wcsicmp(mCardSaveName[cardindex][i],name)==0)
{
if (save)
*save=i; // Return number of existing save
return(MCE_FILEEXISTS);
}
}
}
char drive;
DWORD res;
if (card!=-1)
{
res=XMountMU(GetPort(card),GetSlot(card),&drive);
}
else
{
// Hard drive
res=ERROR_SUCCESS;
drive='U';
}
BOOL ok=TRUE;
if (res!=ERROR_SUCCESS)
{
// Generic failure
return(MCE_FAILURE);
}
else
{
char path[16];
sprintf(path,"%c:\\",drive);
char gamedir[256];
switch(XCreateSaveGame(path,name,allowed_overwrite? OPEN_ALWAYS: CREATE_NEW,0,gamedir,256))
{
case ERROR_DISK_FULL:
if (card!=-1)
XUnmountMU(GetPort(card), GetSlot(card));
return MCE_CARDFULL;
case ERROR_SUCCESS:
// that's fine.
break;
case ERROR_ALREADY_EXISTS:
// Oh no can't overwrite!
ASSERT(!allowed_overwrite); // otherwise why would we get this error?
if (card!=-1)
XUnmountMU(GetPort(card), GetSlot(card));
return MCE_FILEEXISTS;
case ERROR_CANNOT_MAKE:
// couldn't even begin to make the game. Why's that?
{
if (card!=-1)
XUnmountMU(GetPort(card), GetSlot(card));
// we have to check for too many files, strangely.
int num_saves;
GetNumSaves(card, &num_saves);
if (num_saves == MAX_MEMORY_CARD_SAVES)
{
// Well, that's a simple and quick check.
return MCE_CARDFULL;
}
// But of course, the cert tools don't do it that way, they don't create
// valid savegames. So we'd better just enumerate
if (count_num_files(drive) >= MAX_MEMORY_CARD_SAVES)
{
// That's why.
return MCE_TOO_MANY_SAVES;
}
// not a clue.
return MCE_FAILURE;
}
break;
default:
if (card!=-1)
XUnmountMU(GetPort(card), GetSlot(card));
return MCE_FAILURE;
}
// Write savegame image to new save
if (mSaveImage)
WriteSaveImage(gamedir,mSaveImage);
else
{
TRACE("Warning : Save game icon was not available!\n");
}
// and we'd better update the free space display on the card.
GetMountedCardFreeAndTotalBytes(card, drive);
if (card!=-1)
{
if (XUnmountMU(GetPort(card),GetSlot(card))!=ERROR_SUCCESS)
{
TRACE("Warning : XUnmountMU failed!\n");
}
}
// Update this cardindex. Actually we don't really care about the cardindex info,
// we just wanted a new save game number. So let's just invent one.
// Actually, this is a lie. First we want to check whether our savegame is in fact a duplicate.
for (int ii=0;ii<mCardSaves[cardindex];ii++)
{
if (wcsicmp(mCardSaveName[cardindex][ii],name)==0)
{
// Aha! The save already exists.
if (save)
*save=ii; // Return number of existing save
goto save_found;
}
}
// If we've got here, the save is new.
if (mCardSaves[cardindex] == MAX_MEMORY_CARD_SAVES - 1)
{
// We've got no space.
// But we'll have stored memory.
// Let's just replace one in our list.
*save = MAX_MEMORY_CARD_SAVES - 1;
}
else
{
// Stick it on the end.
*save = mCardSaves[cardindex]++;
// we might not have the memory allocated.
if (!mCardSaveName[cardindex][*save])
mCardSaveName[cardindex][*save]=new (MEMTYPE_MEMORYCARD) WCHAR[MAX_NAME_LEN];
}
// copy it in.
wcsncpy(mCardSaveName [cardindex][*save], name , MAX_NAME_LEN - 1);
save_found:;
}
if (ok)
return(MCE_SUCCESS);
else
return(MCE_FAILURE);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::DeleteSave(int card,int save,WCHAR *name)
{
int cardindex=card;
if (card==-1)
cardindex=TOTAL_XBOX_CARDS-1;
char drive;
DWORD res;
if (mCardSaves[cardindex]<=save)
{
return(MCE_NOFILE);
}
if (card!=-1)
{
res=XMountMU(GetPort(card),GetSlot(card),&drive);
}
else
{
// Hard drive
res=ERROR_SUCCESS;
drive='U';
}
BOOL ok=TRUE;
if (res!=ERROR_SUCCESS)
{
// Generic failure
return(MCE_FAILURE);
}
else
{
char path[16];
sprintf(path,"%c:\\",drive);
// Let's verify the shape of the data structures.
ASSERT(!wcscmp(mCardSaveName[cardindex][save], name));
if (XDeleteSaveGame(path,mCardSaveName[cardindex][save])!=ERROR_SUCCESS)
{
ok=FALSE;
}
if (card!=-1)
{
if (XUnmountMU(GetPort(card),GetSlot(card))!=ERROR_SUCCESS)
{
TRACE("Warning : XUnmountMU failed!\n");
}
}
// Update this card
UpdateCardInfo(card);
}
if (ok)
return(MCE_SUCCESS);
else
return(MCE_FAILURE);
}
//---------------------------------------------------------------------------
static FILETIME last_creation_time;
int CXBoxMemoryCard::WriteSave(int card,int save,WCHAR *name,void *data,int datasize)
{
int cardindex=card;
if (card==-1)
cardindex=TOTAL_XBOX_CARDS-1;
char drive;
DWORD res;
if (mCardSaves[cardindex]<=save)
{
return(MCE_NOFILE);
}
if (card!=-1)
{
res=XMountMU(GetPort(card),GetSlot(card),&drive);
}
else
{
// Hard drive
res=ERROR_SUCCESS;
drive='U';
}
if (res!=ERROR_SUCCESS)
{
// Generic failure
return(MCE_FAILURE);
}
else
{
res=MCE_SUCCESS;
// we don't save the path any more, because it takes lots of memory.
// We just get the directory of the save from the save game name
char root_path[4] = "x:\\";
char path[MAX_PATH];
root_path[0] = drive;
if (XCreateSaveGame(root_path, name, OPEN_ALWAYS, 0, path, MAX_PATH) != ERROR_SUCCESS)
{
// Something strange has happened.
ASSERT(0);
res = MCE_FAILURE;
}
else
{
strcat(path,SAVE_FILE_NAME);
// Sometimes we may overwrite an existing save, so use OPEN_ALWAYS
// We assume that code deciding whether this is a good idea or not has happened elsewhere
HANDLE h=CreateFile(path,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (h==INVALID_HANDLE_VALUE)
{
// Opening file failed
if (GetLastError()==ERROR_ALREADY_EXISTS) // Check for "file exists" error
res=MCE_FILEEXISTS;
else
res=MCE_FAILURE;
}
else
{
CXBoxSaveHeader header;
header.CreateHeader((BYTE *) data,datasize);
DWORD written;
if (!WriteFile(h,&header,sizeof(header),&written,NULL))
{
// Write failed. But why?
if (GetLastError() == ERROR_DISK_FULL) res = MCE_CARDFULL;
else res = MCE_FAILURE ;
}
else
{
if (written!=sizeof(header))
{
// Out of space
res=MCE_CARDFULL;
}
else
{
if (!WriteFile(h,data,datasize,&written,NULL))
{
// Write failed
res=MCE_FAILURE;
}
else
{
if (written!=datasize)
{
// Out of space
res=MCE_CARDFULL;
}
}
}
}
GetFileTime(h, &last_creation_time, NULL, NULL);
CloseHandle(h);
}
}
// and we'd better update the free space display on the card.
GetMountedCardFreeAndTotalBytes(card, drive);
if (card!=-1)
{
if (XUnmountMU(GetPort(card),GetSlot(card))!=ERROR_SUCCESS)
{
TRACE("Warning : XUnmountMU failed!\n");
}
}
// Don't update this card
}
return(res);
}
//---------------------------------------------------------------------------
int CXBoxMemoryCard::ReadSave(int card,int save,WCHAR *name,void *data,int datasize,int *bytesread)
{
int cardindex=card;
if (card==-1)
cardindex=TOTAL_XBOX_CARDS-1;
char drive;
DWORD res;
if (mCardSaves[cardindex]<=save)
{
return(MCE_NOFILE);
}
if (card!=-1)
{
res=XMountMU(GetPort(card),GetSlot(card),&drive);
}
else
{
// Hard drive