small update, adds check to importFile so only one save per game allowed, adds check to remove file so removing last file on memcard will not corrupt it

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1120 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
LPFaint99 2008-11-11 07:01:47 +00:00
parent 70837bb034
commit 8373b94fd2
3 changed files with 40 additions and 15 deletions

View File

@ -207,7 +207,8 @@ void CMemcardManager::CopyDeleteClick(wxCommandEvent& event)
int slot = 1; int slot = 1;
int index2 = index1; int index2 = index1;
std::string fileName2(""); std::string fileName2("");
int freeblocks = 0; int freeBlocks = 0;
wxString blocksOpen;
switch (event.GetId()) switch (event.GetId())
{ {
@ -296,12 +297,9 @@ void CMemcardManager::CopyDeleteClick(wxCommandEvent& event)
wxT("Success"),wxOK); wxT("Success"),wxOK);
break; break;
case OUTOFBLOCKS: case OUTOFBLOCKS:
freeblocks = BE16(memoryCard[slot]->bat.FreeBlocks); freeBlocks = BE16(memoryCard[slot]->bat.FreeBlocks);
{ blocksOpen.Printf(wxT("Only %d blocks available"), freeBlocks);
wxString Foobar; wxMessageBox(blocksOpen,wxT("Failure"),wxOK);
Foobar.Printf(wxT("Only %d blocks available"), freeblocks);
wxMessageBox(Foobar,wxT("Failure"),wxOK);
}
break; break;
case OUTOFDIRENTRIES: case OUTOFDIRENTRIES:
wxMessageBox(wxT("No free dir index entries"), wxMessageBox(wxT("No free dir index entries"),
@ -311,6 +309,10 @@ void CMemcardManager::CopyDeleteClick(wxCommandEvent& event)
wxMessageBox(wxT("File is not recognized as a memcard"), wxMessageBox(wxT("File is not recognized as a memcard"),
wxT("Failure"),wxOK); wxT("Failure"),wxOK);
break; break;
case TITLEPRESENT:
wxMessageBox(wxT("Memcard already has a save for this title"),
wxT("Failure"),wxOK);
break;
default: default:
memoryCard[slot]->Save(); memoryCard[slot]->Save();
slot == 1 ? ReloadMemcard(m_Memcard2Path->GetPath().mb_str(), 1) slot == 1 ? ReloadMemcard(m_Memcard2Path->GetPath().mb_str(), 1)

View File

@ -83,6 +83,18 @@ u32 GCMemcard::GetNumFiles()
return j; return j;
} }
bool GCMemcard::titlePresent(u32 gameCode)
{
if (!mcdFile) return 0;
for (int i = 0; i < 127; i++)
{
if (BE32(dir.Dir[i].Gamecode) == gameCode)
return true;
}
return false;
}
bool GCMemcard::RemoveFile(u32 index) //index in the directory array bool GCMemcard::RemoveFile(u32 index) //index in the directory array
{ {
if (!mcdFile) return false; if (!mcdFile) return false;
@ -93,11 +105,13 @@ bool GCMemcard::RemoveFile(u32 index) //index in the directory array
//free the blocks //free the blocks
int blocks_left = BE16(dir.Dir[index].BlockCount); int blocks_left = BE16(dir.Dir[index].BlockCount);
int block = BE16(dir.Dir[index].FirstBlock); int block = BE16(dir.Dir[index].FirstBlock) - 1;
bat.LastAllocated[0] = (u8)((block - 1) >> 8); bat.LastAllocated[0] = (u8)(block >> 8);
bat.LastAllocated[1] = (u8)(block - 1); bat.LastAllocated[1] = (u8)block;
int i = index + 1; int i = index + 1;
memset(&(dir.Dir[index]), 0xFF, 0x40); memset(&(dir.Dir[index]), 0xFF, 0x40);
while (i < 127) while (i < 127)
{ {
DEntry * d = new DEntry; DEntry * d = new DEntry;
@ -126,7 +140,14 @@ bool GCMemcard::RemoveFile(u32 index) //index in the directory array
i++; i++;
} }
//Added to clean up if removing last file
if (BE16(bat.LastAllocated) == (u16)4)
{
for (int j = 0; j < blocks_left; j++)
{
bat.Map[j] = 0x0000;
}
}
// increment update counter // increment update counter
int updateCtr = BE16(dir.UpdateCounter) + 1; int updateCtr = BE16(dir.UpdateCounter) + 1;
dir.UpdateCounter[0] = u8(updateCtr >> 8); dir.UpdateCounter[0] = u8(updateCtr >> 8);
@ -141,9 +162,6 @@ bool GCMemcard::RemoveFile(u32 index) //index in the directory array
u32 GCMemcard::ImportFile(DEntry& direntry, u8* contents, int remove) u32 GCMemcard::ImportFile(DEntry& direntry, u8* contents, int remove)
{ {
// TODO: add a check for existing game id
// so that only one save per title is allowed
// until then any particular title will always use the first save
if (!mcdFile) return NOMEMCARD; if (!mcdFile) return NOMEMCARD;
if (GetNumFiles() >= 127) if (GetNumFiles() >= 127)
@ -154,6 +172,7 @@ u32 GCMemcard::ImportFile(DEntry& direntry, u8* contents, int remove)
{ {
return OUTOFBLOCKS; return OUTOFBLOCKS;
} }
if (!remove && titlePresent(BE32(direntry.Gamecode))) return TITLEPRESENT;
// find first free data block -- assume no freespace fragmentation // find first free data block -- assume no freespace fragmentation
int totalspace = (((u32)BE16(hdr.Size) * 16) - 5); int totalspace = (((u32)BE16(hdr.Size) * 16) - 5);

View File

@ -31,7 +31,8 @@ enum
GCS = 0x110, GCS = 0x110,
OUTOFBLOCKS, OUTOFBLOCKS,
OUTOFDIRENTRIES, OUTOFDIRENTRIES,
NOMEMCARD NOMEMCARD,
TITLEPRESENT
}; };
class GCMemcard class GCMemcard
@ -141,6 +142,9 @@ public:
// get number of file entries in the directory // get number of file entries in the directory
u32 GetNumFiles(); u32 GetNumFiles();
// Returns true if title already on memcard
bool titlePresent(u32 gameCode);
// read directory entry // read directory entry
bool GetFileInfo(u32 index, DEntry& data); bool GetFileInfo(u32 index, DEntry& data);