memory card cleanup
This commit is contained in:
+148
-156
@@ -7,6 +7,7 @@
|
||||
#include "RageLog.h"
|
||||
#include "ScreenManager.h"
|
||||
#include "ProfileManager.h"
|
||||
#include "Foreach.h"
|
||||
|
||||
MemoryCardManager* MEMCARDMAN = NULL; // global and accessable from anywhere in our program
|
||||
|
||||
@@ -18,18 +19,12 @@ MemoryCardManager::MemoryCardManager()
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
m_bTooLate[p] = false;
|
||||
m_bWriteError[p] = false;
|
||||
}
|
||||
m_pDriver->GetStorageDevices( m_vStorageDevices );
|
||||
|
||||
m_bDontPlaySoundsOnce = false;
|
||||
|
||||
m_soundReady.Load( THEME->GetPathToS("MemoryCardManager ready") );
|
||||
m_soundError.Load( THEME->GetPathToS("MemoryCardManager error") );
|
||||
m_soundTooLate.Load( THEME->GetPathToS("MemoryCardManager too late") );
|
||||
m_soundDisconnect.Load( THEME->GetPathToS("MemoryCardManager disconnect") );
|
||||
|
||||
UpdateUnassignedCards();
|
||||
}
|
||||
|
||||
MemoryCardManager::~MemoryCardManager()
|
||||
@@ -49,64 +44,168 @@ void MemoryCardManager::Update( float fDelta )
|
||||
|
||||
|
||||
// check for disconnects
|
||||
{
|
||||
for( unsigned i=0; i<vOld.size(); i++ )
|
||||
FOREACH( UsbStorageDevice, vOld, old )
|
||||
{
|
||||
bool bFoundMatch = false;
|
||||
FOREACH( UsbStorageDevice, vNew, newd )
|
||||
{
|
||||
if( old->IdsMatch(*newd) )
|
||||
{
|
||||
const UsbStorageDevice &old = vOld[i];
|
||||
if( find(vNew.begin(),vNew.end(),old) == vNew.end() ) // didn't find
|
||||
{
|
||||
LOG->Trace( ssprintf("Disconnected bus %d port %d device %d path %s", old.iBus, old.iPort, old.iLevel, old.sOsMountDir.c_str()) );
|
||||
vDisconnects.push_back( old );
|
||||
}
|
||||
bFoundMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( !bFoundMatch )
|
||||
{
|
||||
LOG->Trace( "Disconnected bus %d port %d device %d path %s", old->iBus, old->iPort, old->iLevel, old->sOsMountDir.c_str() );
|
||||
vDisconnects.push_back( *old );
|
||||
}
|
||||
}
|
||||
|
||||
// check for connects
|
||||
{
|
||||
for( unsigned i=0; i<vNew.size(); i++ )
|
||||
{
|
||||
const UsbStorageDevice &newd = vNew[i];
|
||||
if( find(vOld.begin(),vOld.end(),newd) == vOld.end() ) // didn't find
|
||||
{
|
||||
LOG->Trace( ssprintf("Connected bus %d port %d device %d path %s", newd.iBus, newd.iPort, newd.iLevel, newd.sOsMountDir.c_str()) );
|
||||
vConnects.push_back( newd );
|
||||
}
|
||||
}
|
||||
}
|
||||
FOREACH( UsbStorageDevice, vNew, newd )
|
||||
{
|
||||
bool bFoundMatch = false;
|
||||
FOREACH( UsbStorageDevice, vOld, old )
|
||||
{
|
||||
if( old->IdsMatch(*newd) )
|
||||
{
|
||||
bFoundMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( !bFoundMatch )
|
||||
{
|
||||
LOG->Trace( "Connected bus %d port %d device %d path %s", newd->iBus, newd->iPort, newd->iLevel, newd->sOsMountDir.c_str() );
|
||||
vDisconnects.push_back( *newd );
|
||||
}
|
||||
}
|
||||
|
||||
// unassign cards that were disconnected
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
if( m_Device[p].IsBlank() ) // not assigned a card
|
||||
continue;
|
||||
|
||||
if( find(vDisconnects.begin(),vDisconnects.end(),m_Device[p]) != vDisconnects.end() ) // assigned card was disconnected
|
||||
{
|
||||
m_pDriver->Unmount(&m_Device[p], MEM_CARD_MOUNT_POINT[p]);
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( assigned_device.IsBlank() ) // not assigned a card
|
||||
continue;
|
||||
|
||||
FOREACH( UsbStorageDevice, vDisconnects, disconnect )
|
||||
{
|
||||
if( disconnect->IdsMatch(assigned_device) )
|
||||
{
|
||||
m_pDriver->Unmount(&assigned_device, MEM_CARD_MOUNT_POINT[p]);
|
||||
|
||||
assigned_device.MakeBlank();
|
||||
m_soundDisconnect.Play();
|
||||
|
||||
if( PROFILEMAN->ProfileWasLoadedFromMemoryCard(p) )
|
||||
PROFILEMAN->UnloadProfile( p );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_Device[p].MakeBlank();
|
||||
m_soundDisconnect.Play();
|
||||
// Update the status of already-assigned cards. It may contain updated info
|
||||
// like WriteTest results and a new sName.
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( assigned_device.IsBlank() ) // no card assigned to this player
|
||||
continue;
|
||||
|
||||
if( PROFILEMAN->ProfileWasLoadedFromMemoryCard((PlayerNumber)p) )
|
||||
PROFILEMAN->UnloadProfile( (PlayerNumber)p );
|
||||
}
|
||||
}
|
||||
}
|
||||
FOREACH( UsbStorageDevice, m_vStorageDevices, d )
|
||||
{
|
||||
if( d->IdsMatch(assigned_device) )
|
||||
{
|
||||
// play write test error sound if write test failed since we last checked
|
||||
if( assigned_device.bNeedsWriteTest && !d->bNeedsWriteTest && !d->bWriteTestSucceeded )
|
||||
m_soundError.Play();
|
||||
assigned_device = *d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UpdateUnassignedCards();
|
||||
// make a list of unassigned
|
||||
vector<UsbStorageDevice> vUnassignedDevices = m_vStorageDevices; // copy
|
||||
|
||||
// remove cards that are already assigned
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( assigned_device.IsBlank() ) // no card assigned to this player
|
||||
continue;
|
||||
|
||||
FOREACH( UsbStorageDevice, vUnassignedDevices, d )
|
||||
{
|
||||
if( d->IdsMatch(assigned_device) )
|
||||
{
|
||||
vUnassignedDevices.erase( d );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to assign each device to a player
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
LOG->Trace( "Looking for a card for Player %d", p+1 );
|
||||
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( !assigned_device.IsBlank() ) // they already have an assigned card
|
||||
{
|
||||
LOG->Trace( "Player %d already has a card: '%s'", p+1, assigned_device.sOsMountDir.c_str() );
|
||||
continue; // skip
|
||||
}
|
||||
|
||||
FOREACH( UsbStorageDevice, vUnassignedDevices, d )
|
||||
{
|
||||
// search for card dir match
|
||||
if( !PREFSMAN->m_sMemoryCardOsMountPoint[p].empty() &&
|
||||
d->sOsMountDir.CompareNoCase(PREFSMAN->m_sMemoryCardOsMountPoint[p]) )
|
||||
continue; // not a match
|
||||
|
||||
// search for USB bus match
|
||||
if( PREFSMAN->m_iMemoryCardUsbBus[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbBus[p] != d->iBus )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbPort[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbPort[p] != d->iPort )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbLevel[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbLevel[p] != d->iLevel )
|
||||
continue; // not a match
|
||||
|
||||
LOG->Trace( "device match: iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d, sOsMountDir: %s",
|
||||
d->iScsiIndex, d->iBus, d->iLevel, d->iPort, d->sOsMountDir.c_str() );
|
||||
|
||||
assigned_device = *d; // save a copy
|
||||
vUnassignedDevices.erase( d ); // remove the device so we don't match it for another player
|
||||
m_bTooLate[p] = m_bCardsLocked; // the device is too late if inserted when cards were locked
|
||||
|
||||
// play sound
|
||||
if( m_bTooLate[p] )
|
||||
m_soundTooLate.Play();
|
||||
else if( !d->bNeedsWriteTest && !d->bWriteTestSucceeded )
|
||||
m_soundError.Play();
|
||||
else
|
||||
m_soundReady.Play();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SCREENMAN->RefreshCreditsMessages();
|
||||
}
|
||||
}
|
||||
|
||||
MemoryCardState MemoryCardManager::GetCardState( PlayerNumber pn )
|
||||
{
|
||||
if( m_Device[pn].IsBlank() )
|
||||
UsbStorageDevice &d = m_Device[pn];
|
||||
if( d.IsBlank() )
|
||||
return MEMORY_CARD_STATE_NO_CARD;
|
||||
else if( m_bTooLate[pn] )
|
||||
return MEMORY_CARD_STATE_TOO_LATE;
|
||||
else if( m_bWriteError[pn] )
|
||||
else if( !d.bNeedsWriteTest && !d.bWriteTestSucceeded )
|
||||
return MEMORY_CARD_STATE_WRITE_ERROR;
|
||||
else
|
||||
return MEMORY_CARD_STATE_READY;
|
||||
@@ -133,10 +232,7 @@ void MemoryCardManager::LockCards( bool bLock )
|
||||
if( m_Device[p].IsBlank() ) // they don't have an assigned card
|
||||
continue;
|
||||
|
||||
if( !m_pDriver->MountAndTestWrite(&m_Device[p], MEM_CARD_MOUNT_POINT[p]) )
|
||||
{
|
||||
m_bWriteError[p] = true;
|
||||
}
|
||||
m_pDriver->MountAndTestWrite(&m_Device[p], MEM_CARD_MOUNT_POINT[p]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,123 +243,19 @@ void MemoryCardManager::LockCards( bool bLock )
|
||||
m_pDriver->SetMountThreadState( MemoryCardDriver::detect_and_mount );
|
||||
}
|
||||
|
||||
void MemoryCardManager::UpdateUnassignedCards()
|
||||
{
|
||||
LOG->Trace( "UpdateUnassignedCards" );
|
||||
|
||||
// make a list of unassigned
|
||||
vector<UsbStorageDevice> vUnassignedDevices = m_vStorageDevices; // copy
|
||||
|
||||
// remove cards that are already assigned
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
if( m_Device[p].IsBlank() ) // no card assigned to this player
|
||||
continue;
|
||||
|
||||
vector<UsbStorageDevice>::iterator it = find(vUnassignedDevices.begin(),vUnassignedDevices.end(),m_Device[p]);
|
||||
if( it != vUnassignedDevices.end() )
|
||||
{
|
||||
// TRICKY: Update m_Device with the latest info we received from the
|
||||
// driver. It may contain updated info like WriteTest results and a
|
||||
// sName.
|
||||
m_Device[p] = *it;
|
||||
|
||||
vUnassignedDevices.erase( it );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to assign each device to a player
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
LOG->Trace( "Looking for a card for Player %d", p+1 );
|
||||
|
||||
if( !m_Device[p].IsBlank() ) // they already have an assigned card
|
||||
{
|
||||
LOG->Trace( "Player %d already has a card: '%s'", p+1, m_Device[p].sOsMountDir.c_str() );
|
||||
continue; // skip
|
||||
}
|
||||
|
||||
unsigned i;
|
||||
|
||||
// search for card dir match
|
||||
if( !PREFSMAN->m_sMemoryCardOsMountPoint[p].empty() )
|
||||
{
|
||||
for( i=0; i<vUnassignedDevices.size(); i++ )
|
||||
{
|
||||
UsbStorageDevice &usbd = vUnassignedDevices[i];
|
||||
if( usbd.sOsMountDir.CompareNoCase(PREFSMAN->m_sMemoryCardOsMountPoint[p]) == 0 ) // match
|
||||
{
|
||||
LOG->Trace( "dir match: iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d",
|
||||
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort );
|
||||
goto match;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// search for USB bus match
|
||||
for( i=0; i<vUnassignedDevices.size(); i++ )
|
||||
{
|
||||
UsbStorageDevice &usbd = vUnassignedDevices[i];
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbBus[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbBus[p] != usbd.iBus )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbPort[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbPort[p] != usbd.iPort )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbLevel[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbLevel[p] != usbd.iLevel )
|
||||
continue; // not a match
|
||||
|
||||
LOG->Trace( "bus/port match: iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d",
|
||||
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort );
|
||||
goto match;
|
||||
}
|
||||
|
||||
LOG->Trace( "Player %d memory card: none", p+1 );
|
||||
continue; // no matches
|
||||
match:
|
||||
m_Device[p] = vUnassignedDevices[i]; // save a copy
|
||||
LOG->Trace( "Player %d memory card: '%s'", p+1, m_Device[p].sOsMountDir.c_str() );
|
||||
vUnassignedDevices.erase( vUnassignedDevices.begin()+i ); // remove the device so we don't match it for another player
|
||||
m_bTooLate[p] = m_bCardsLocked;
|
||||
m_bWriteError[p] = false;
|
||||
|
||||
if( !m_bDontPlaySoundsOnce )
|
||||
{
|
||||
// play sound
|
||||
if( m_bWriteError[p] )
|
||||
m_soundError.Play();
|
||||
else if( m_bTooLate[p] )
|
||||
m_soundTooLate.Play();
|
||||
else
|
||||
m_soundReady.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_bDontPlaySoundsOnce = false;
|
||||
}
|
||||
|
||||
void MemoryCardManager::FlushAndReset()
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
if( m_Device[p].IsBlank() ) // no card assigned
|
||||
UsbStorageDevice &d = m_Device[p];
|
||||
if( d.IsBlank() ) // no card assigned
|
||||
continue; // skip
|
||||
if( m_bWriteError[p] || m_bTooLate[p] )
|
||||
if( (!d.bNeedsWriteTest && !d.bWriteTestSucceeded) || m_bTooLate[p] )
|
||||
continue; // skip
|
||||
m_pDriver->Flush(&m_Device[p]);
|
||||
}
|
||||
|
||||
m_pDriver->ResetUsbStorage(); // forces cards to be re-detected
|
||||
|
||||
m_bDontPlaySoundsOnce = true;
|
||||
}
|
||||
|
||||
bool MemoryCardManager::PathIsMemCard( CString sDir ) const
|
||||
|
||||
Reference in New Issue
Block a user