snes9xgx/source/ngc/memcardop.cpp

368 lines
8.1 KiB
C++
Raw Normal View History

/****************************************************************************
* Snes9x 1.51 Nintendo Wii/Gamecube Port
*
* softdev July 2006
* crunchy2 May-June 2007
2009-03-11 18:28:37 +01:00
* Tantric 2008-2009
*
2008-08-12 05:25:16 +02:00
* memcardop.cpp
*
* Memory Card routines
***************************************************************************/
#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "snes9xGX.h"
#include "video.h"
#include "menu.h"
#include "preferences.h"
2009-03-11 18:28:37 +01:00
#include "filebrowser.h"
2008-08-12 05:25:16 +02:00
#include "fileop.h"
2009-03-11 18:28:37 +01:00
#include "dvd.h"
2009-03-11 18:28:37 +01:00
static u8 * SysArea = NULL;
/****************************************************************************
2009-03-11 18:28:37 +01:00
* MountMC
*
* Mounts the memory card in the given slot.
* Returns the result of the last attempted CARD_Mount command.
***************************************************************************/
2009-03-11 18:28:37 +01:00
static int MountMC(int slot, bool silent)
{
int ret = -1;
int tries = 0;
2009-03-11 18:28:37 +01:00
// Initialize Card System
SysArea = (u8 *)memalign(32, CARD_WORKAREA);
memset (SysArea, 0, CARD_WORKAREA);
CARD_Init ("SNES", "00");
// Mount the card
2009-03-11 18:28:37 +01:00
while(tries < 10 && ret != 0)
{
2009-03-11 18:28:37 +01:00
EXI_ProbeReset();
ret = CARD_Mount (slot, SysArea, NULL);
VIDEO_WaitVSync();
tries++;
}
2009-03-11 18:28:37 +01:00
if(ret != 0 && !silent)
{
if (slot == CARD_SLOTA)
ErrorPrompt("Unable to mount Slot A Memory Card!");
else
ErrorPrompt("Unable to mount Slot B Memory Card!");
}
return ret;
}
/****************************************************************************
2009-03-11 18:28:37 +01:00
* TestMC
*
* Checks to see if a card is in the card slot specified
***************************************************************************/
2009-03-11 18:28:37 +01:00
bool TestMC(int slot, bool silent)
{
2008-08-16 02:02:08 +02:00
// Memory Cards do not work in Wii mode - disable
#ifdef HW_RVL
return false;
#endif
2009-03-11 18:28:37 +01:00
bool ret = false;
2009-03-11 18:28:37 +01:00
// Try to mount the card
if (MountMC(slot, silent) == 0)
{
// Mount successful!
CARD_Unmount (slot);
2009-03-11 18:28:37 +01:00
ret = true;
}
2009-03-11 18:28:37 +01:00
free(SysArea);
return ret;
}
/****************************************************************************
* ParseMCDirectory
*
* Parses a list of all files on the specified memory card
***************************************************************************/
int
ParseMCDirectory (int slot)
{
card_dir CardDir;
int CardError;
int entryNum = 0;
// Try to mount the card
CardError = MountMC(slot, NOTSILENT);
if (CardError == 0)
{
2009-03-11 18:28:37 +01:00
CardError = CARD_FindFirst (slot, &CardDir, TRUE);
while (CardError != CARD_ERROR_NOFILE)
{
2009-03-11 18:28:37 +01:00
BROWSERENTRY * newBrowserList = (BROWSERENTRY *)realloc(browserList, (entryNum+1) * sizeof(BROWSERENTRY));
if(!newBrowserList) // failed to allocate required memory
{
ResetBrowser();
ErrorPrompt("Out of memory: too many files!");
entryNum = -1;
break;
}
else
2009-03-11 18:28:37 +01:00
{
browserList = newBrowserList;
}
memset(&(browserList[entryNum]), 0, sizeof(BROWSERENTRY)); // clear the new entry
strncpy(browserList[entryNum].filename, (char *)CardDir.filename, MAXJOLIET);
browserList[entryNum].length = CardDir.filelen;
entryNum++;
2009-03-11 18:28:37 +01:00
CardError = CARD_FindNext (&CardDir);
}
CARD_Unmount(slot);
}
2009-03-11 18:28:37 +01:00
return entryNum;
}
/****************************************************************************
* Verify Memory Card file against buffer
***************************************************************************/
static int
2008-11-12 08:50:39 +01:00
VerifyMCFile (char *buf, int slot, char *filename, int datasize)
{
card_file CardFile;
2009-03-11 18:28:37 +01:00
unsigned char verifybuffer[65536] ATTRIBUTE_ALIGN (32);
int CardError;
unsigned int blocks;
unsigned int SectorSize;
int bytesleft = 0;
int bytesread = 0;
2009-03-11 18:28:37 +01:00
int ret = 0;
2009-03-11 18:28:37 +01:00
memset (verifybuffer, 0, 65536);
2009-03-11 18:28:37 +01:00
// Get Sector Size
CARD_GetSectorSize (slot, &SectorSize);
2009-03-11 18:28:37 +01:00
memset (&CardFile, 0, sizeof (CardFile));
CardError = CARD_Open (slot, filename, &CardFile);
2009-03-11 18:28:37 +01:00
if(CardError)
{
ErrorPrompt("Unable to open file!");
}
else
{
blocks = CardFile.len;
if (blocks < SectorSize)
blocks = SectorSize;
if (blocks % SectorSize)
blocks += SectorSize;
2009-03-11 18:28:37 +01:00
if (blocks > (unsigned int)datasize)
blocks = datasize;
bytesleft = blocks;
bytesread = 0;
while (bytesleft > 0)
{
2009-03-11 18:28:37 +01:00
CARD_Read (&CardFile, verifybuffer, SectorSize, bytesread);
if ( memcmp (buf + bytesread, verifybuffer, (unsigned int)bytesleft < SectorSize ? bytesleft : SectorSize) )
{
ErrorPrompt("File integrity could not be verified!");
break;
}
bytesleft -= SectorSize;
bytesread += SectorSize;
2009-03-11 18:28:37 +01:00
ShowProgress ("Verifying...", bytesread, blocks);
}
CARD_Close (&CardFile);
2009-03-11 18:28:37 +01:00
CancelAction();
}
2009-03-11 18:28:37 +01:00
return ret;
}
/****************************************************************************
2008-11-12 08:50:39 +01:00
* LoadMCFile
* Load savebuffer from Memory Card file
***************************************************************************/
int
2008-11-12 08:50:39 +01:00
LoadMCFile (char *buf, int slot, char *filename, bool silent)
{
card_file CardFile;
int CardError;
unsigned int blocks;
unsigned int SectorSize;
int bytesleft = 0;
int bytesread = 0;
2009-03-11 18:28:37 +01:00
// Try to mount the card
CardError = MountMC(slot, NOTSILENT);
if (CardError == 0)
{
2009-03-11 18:28:37 +01:00
// Get Sector Size
CARD_GetSectorSize (slot, &SectorSize);
memset (&CardFile, 0, sizeof (CardFile));
CardError = CARD_Open (slot, filename, &CardFile);
2009-03-11 18:28:37 +01:00
if(CardError)
{
2009-03-12 08:20:16 +01:00
if(!silent)
ErrorPrompt("Unable to open file!");
2009-03-11 18:28:37 +01:00
}
else
{
blocks = CardFile.len;
2009-03-11 18:28:37 +01:00
if (blocks < SectorSize)
blocks = SectorSize;
2009-03-11 18:28:37 +01:00
if (blocks % SectorSize)
blocks += SectorSize;
bytesleft = blocks;
bytesread = 0;
while (bytesleft > 0)
{
CardError = CARD_Read (&CardFile, buf + bytesread, SectorSize, bytesread);
2009-03-11 18:28:37 +01:00
if(CardError)
{
ErrorPrompt("Error loading file!");
bytesread = 0;
break;
}
2009-03-11 18:28:37 +01:00
bytesleft -= SectorSize;
bytesread += SectorSize;
ShowProgress ("Loading...", bytesread, blocks);
}
CARD_Close (&CardFile);
CancelAction();
}
2009-03-11 18:28:37 +01:00
CARD_Unmount(slot);
}
2009-03-11 18:28:37 +01:00
free(SysArea);
return bytesread;
}
/****************************************************************************
2008-11-12 08:50:39 +01:00
* SaveMCFile
* Write savebuffer to Memory Card file
***************************************************************************/
int
2008-11-12 08:50:39 +01:00
SaveMCFile (char *buf, int slot, char *filename, int datasize, bool silent)
{
card_file CardFile;
card_stat CardStatus;
int CardError;
unsigned int blocks;
unsigned int SectorSize;
2009-03-11 18:28:37 +01:00
int byteswritten = 0;
int bytesleft = 0;
if(datasize <= 0)
return 0;
2009-03-11 18:28:37 +01:00
// Try to mount the card
CardError = MountMC(slot, NOTSILENT);
if (CardError == 0)
{
2009-03-11 18:28:37 +01:00
// Get Sector Size
CARD_GetSectorSize (slot, &SectorSize);
2009-03-11 18:28:37 +01:00
// Calculate number of blocks required
blocks = (datasize / SectorSize) * SectorSize;
if (datasize % SectorSize)
blocks += SectorSize;
2009-03-11 18:28:37 +01:00
// Delete existing file (if present)
memset(&CardStatus, 0, sizeof(card_stat));
CardError = CARD_Open (slot, filename, &CardFile);
if(CardError == 0)
{
2009-03-11 18:28:37 +01:00
CARD_Close (&CardFile);
CardError = CARD_Delete(slot, filename);
if (CardError)
{
2009-03-11 18:28:37 +01:00
ErrorPrompt("Unable to delete existing file!");
goto done;
}
}
2009-03-11 18:28:37 +01:00
// Create new file
memset(&CardStatus, 0, sizeof(card_stat));
CardError = CARD_Create (slot, filename, blocks, &CardFile);
if (CardError)
{
2009-03-11 18:28:37 +01:00
if (CardError == CARD_ERROR_INSSPACE)
ErrorPrompt("Insufficient space to create file!");
else
ErrorPrompt("Unable to create card file!");
goto done;
}
2009-03-11 18:28:37 +01:00
// Now, have an open file handle, ready to send out the data
CARD_GetStatus (slot, CardFile.filenum, &CardStatus);
CardStatus.icon_addr = 0x0;
CardStatus.icon_fmt = 2;
CardStatus.icon_speed = 1;
CardStatus.comment_addr = 2048;
CARD_SetStatus (slot, CardFile.filenum, &CardStatus);
2009-03-11 18:28:37 +01:00
bytesleft = blocks;
while (bytesleft > 0)
{
CardError =
2009-03-16 07:58:50 +01:00
CARD_Write (&CardFile, buf + byteswritten, SectorSize, byteswritten);
2009-03-11 18:28:37 +01:00
if(CardError)
{
ErrorPrompt("Error writing file!");
byteswritten = 0;
break;
}
bytesleft -= SectorSize;
byteswritten += SectorSize;
2009-03-11 18:28:37 +01:00
ShowProgress ("Saving...", byteswritten, blocks);
}
CARD_Close (&CardFile);
2009-03-11 18:28:37 +01:00
CancelAction();
2009-03-11 18:28:37 +01:00
if (byteswritten > 0 && GCSettings.VerifySaves)
{
2009-03-11 18:28:37 +01:00
// Verify the written file
if (!VerifyMCFile (buf, slot, filename, byteswritten) )
byteswritten = 0;
}
2009-03-11 18:28:37 +01:00
done:
CARD_Unmount (slot);
}
2009-03-11 18:28:37 +01:00
free(SysArea);
return byteswritten;
}