2003-12-15 08:34:06 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
Class: MemoryCardManager
|
|
|
|
|
|
|
|
|
|
Desc: See Header.
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
|
|
|
|
|
Chris Danford
|
|
|
|
|
Chris Gomez
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MemoryCardManager.h"
|
2003-12-17 10:20:53 +00:00
|
|
|
#include "arch/MemoryCard/MemoryCardDriver.h" // for UsbStorageDevice
|
|
|
|
|
#include "arch/arch.h"
|
|
|
|
|
#include "ScreenManager.h"
|
2003-12-18 04:24:25 +00:00
|
|
|
#include "ThemeManager.h"
|
2003-12-18 07:46:21 +00:00
|
|
|
#include "PrefsManager.h"
|
|
|
|
|
#include "RageLog.h"
|
2004-01-03 03:42:40 +00:00
|
|
|
#include "ScreenManager.h"
|
|
|
|
|
#include "ProfileManager.h"
|
2003-12-15 08:34:06 +00:00
|
|
|
|
|
|
|
|
MemoryCardManager* MEMCARDMAN = NULL; // global and accessable from anywhere in our program
|
|
|
|
|
|
|
|
|
|
|
2003-12-17 10:20:53 +00:00
|
|
|
MemoryCardManager::MemoryCardManager()
|
|
|
|
|
{
|
|
|
|
|
m_pDriver = MakeMemoryCardDriver();
|
|
|
|
|
m_bCardsLocked = false;
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
2003-12-15 08:34:06 +00:00
|
|
|
{
|
2003-12-19 04:52:07 +00:00
|
|
|
m_bTooLate[p] = false;
|
|
|
|
|
m_bWriteError[p] = false;
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
m_pDriver->GetStorageDevices( m_vStorageDevices );
|
2003-12-18 04:24:25 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
m_soundReady.Load( THEME->GetPathToS("MemoryCardManager ready") );
|
|
|
|
|
m_soundError.Load( THEME->GetPathToS("MemoryCardManager error") );
|
|
|
|
|
m_soundTooLate.Load( THEME->GetPathToS("MemoryCardManager too late") );
|
2003-12-18 04:24:25 +00:00
|
|
|
m_soundDisconnect.Load( THEME->GetPathToS("MemoryCardManager disconnect") );
|
2003-12-19 04:52:07 +00:00
|
|
|
|
|
|
|
|
AssignUnassignedCards();
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
2003-12-17 03:43:31 +00:00
|
|
|
|
2003-12-17 10:20:53 +00:00
|
|
|
MemoryCardManager::~MemoryCardManager()
|
|
|
|
|
{
|
2004-01-21 03:09:59 +00:00
|
|
|
delete m_pDriver;
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
2003-12-17 03:43:31 +00:00
|
|
|
|
2003-12-17 10:20:53 +00:00
|
|
|
void MemoryCardManager::Update( float fDelta )
|
|
|
|
|
{
|
|
|
|
|
if( m_pDriver->StorageDevicesChanged() )
|
2003-12-17 03:43:31 +00:00
|
|
|
{
|
2003-12-18 07:46:21 +00:00
|
|
|
vector<UsbStorageDevice> vOld = m_vStorageDevices; // copy
|
|
|
|
|
m_pDriver->GetStorageDevices( m_vStorageDevices );
|
|
|
|
|
vector<UsbStorageDevice> &vNew = m_vStorageDevices;
|
|
|
|
|
vector<UsbStorageDevice> vConnects; // fill these in below
|
|
|
|
|
vector<UsbStorageDevice> vDisconnects; // fill these in below
|
2003-12-15 08:34:06 +00:00
|
|
|
|
2003-12-17 08:43:14 +00:00
|
|
|
|
2003-12-17 10:20:53 +00:00
|
|
|
// check for disconnects
|
2003-12-17 08:43:14 +00:00
|
|
|
{
|
2004-01-03 04:29:42 +00:00
|
|
|
for( unsigned i=0; i<vOld.size(); i++ )
|
2003-12-18 04:24:25 +00:00
|
|
|
{
|
2004-01-03 03:42:40 +00:00
|
|
|
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.iPortOnHub, old.iDeviceOnBus, old.sOsMountDir.c_str()) );
|
|
|
|
|
vDisconnects.push_back( old );
|
|
|
|
|
}
|
2003-12-18 04:24:25 +00:00
|
|
|
}
|
2003-12-17 08:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-17 10:20:53 +00:00
|
|
|
// check for connects
|
2003-12-17 06:59:37 +00:00
|
|
|
{
|
2004-01-03 04:29:42 +00:00
|
|
|
for( unsigned i=0; i<vNew.size(); i++ )
|
2003-12-18 04:24:25 +00:00
|
|
|
{
|
2004-01-03 03:42:40 +00:00
|
|
|
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.iPortOnHub, newd.iDeviceOnBus, newd.sOsMountDir.c_str()) );
|
|
|
|
|
vConnects.push_back( newd );
|
|
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
// unassign cards that were disconnected
|
2003-12-18 07:46:21 +00:00
|
|
|
{
|
2004-01-03 03:42:40 +00:00
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
2003-12-18 07:46:21 +00:00
|
|
|
{
|
2004-01-03 03:42:40 +00:00
|
|
|
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_Device[p].MakeBlank();
|
|
|
|
|
m_soundDisconnect.Play();
|
|
|
|
|
|
|
|
|
|
if( PROFILEMAN->ProfileWasLoadedFromMemoryCard((PlayerNumber)p) )
|
|
|
|
|
PROFILEMAN->UnloadProfile( (PlayerNumber)p );
|
2004-03-20 22:16:57 +00:00
|
|
|
|
|
|
|
|
m_pDriver->Unmount(&m_Device[p], MEM_CARD_MOUNT_POINT[p]);
|
2004-01-03 03:42:40 +00:00
|
|
|
}
|
2003-12-18 04:24:25 +00:00
|
|
|
}
|
2003-12-17 06:59:37 +00:00
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
AssignUnassignedCards();
|
2004-01-03 03:42:40 +00:00
|
|
|
|
|
|
|
|
if( !m_bCardsLocked )
|
|
|
|
|
{
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
2004-02-22 04:53:51 +00:00
|
|
|
PROFILEMAN->LoadFirstAvailableProfile( (PlayerNumber)p );
|
2004-01-03 03:42:40 +00:00
|
|
|
}
|
|
|
|
|
SCREENMAN->RefreshCreditsMessages();
|
2003-12-15 08:34:06 +00:00
|
|
|
}
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
MemoryCardState MemoryCardManager::GetCardState( PlayerNumber pn )
|
2003-12-17 10:20:53 +00:00
|
|
|
{
|
2003-12-18 07:46:21 +00:00
|
|
|
if( m_Device[pn].IsBlank() )
|
2003-12-19 04:52:07 +00:00
|
|
|
return MEMORY_CARD_STATE_NO_CARD;
|
2003-12-17 10:20:53 +00:00
|
|
|
else if( m_bTooLate[pn] )
|
2003-12-19 04:52:07 +00:00
|
|
|
return MEMORY_CARD_STATE_TOO_LATE;
|
2003-12-17 10:20:53 +00:00
|
|
|
else if( m_bWriteError[pn] )
|
2003-12-19 04:52:07 +00:00
|
|
|
return MEMORY_CARD_STATE_WRITE_ERROR;
|
2003-12-17 10:20:53 +00:00
|
|
|
else
|
2003-12-19 04:52:07 +00:00
|
|
|
return MEMORY_CARD_STATE_READY;
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-14 01:40:24 +00:00
|
|
|
/*
|
2003-12-17 10:20:53 +00:00
|
|
|
CString MemoryCardManager::GetOsMountDir( PlayerNumber pn )
|
|
|
|
|
{
|
2003-12-18 07:46:21 +00:00
|
|
|
if( m_Device[pn].IsBlank() )
|
2003-12-17 10:20:53 +00:00
|
|
|
return "";
|
|
|
|
|
else
|
2004-02-27 03:39:34 +00:00
|
|
|
{
|
|
|
|
|
CString sDir = m_Device[pn].sOsMountDir;
|
2004-03-07 04:34:49 +00:00
|
|
|
FixSlashesInPlace( sDir );
|
2004-02-27 03:39:34 +00:00
|
|
|
if( !sDir.empty() && sDir.Right(1)!="/" )
|
|
|
|
|
sDir += '/';
|
|
|
|
|
return sDir;
|
|
|
|
|
}
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
2004-03-14 01:40:24 +00:00
|
|
|
*/
|
2003-12-17 10:20:53 +00:00
|
|
|
|
|
|
|
|
void MemoryCardManager::LockCards( bool bLock )
|
|
|
|
|
{
|
|
|
|
|
m_bCardsLocked = bLock;
|
|
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
if( !bLock )
|
2003-12-17 03:43:31 +00:00
|
|
|
{
|
2003-12-19 04:52:07 +00:00
|
|
|
// clear too late flag
|
2003-12-17 10:20:53 +00:00
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
|
|
|
|
m_bTooLate[p] = false;
|
2003-12-18 07:46:21 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
AssignUnassignedCards();
|
2003-12-15 08:34:06 +00:00
|
|
|
}
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
2003-12-17 03:43:31 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
void MemoryCardManager::AssignUnassignedCards()
|
2003-12-18 07:46:21 +00:00
|
|
|
{
|
2004-02-26 17:05:27 +00:00
|
|
|
LOG->Trace( "AssignUnassignedCards" );
|
|
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
// make a list of unassigned
|
|
|
|
|
vector<UsbStorageDevice> vUnassignedDevices = m_vStorageDevices; // copy
|
|
|
|
|
|
|
|
|
|
// remove cards that are already assigned
|
2003-12-18 07:46:21 +00:00
|
|
|
{
|
2003-12-19 04:52:07 +00:00
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
|
|
|
|
{
|
2004-02-27 02:59:20 +00:00
|
|
|
if( m_Device[p].IsBlank() ) // no card assigned to this player
|
2003-12-19 04:52:07 +00:00
|
|
|
continue;
|
2003-12-18 07:46:21 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
vector<UsbStorageDevice>::iterator it = find(vUnassignedDevices.begin(),vUnassignedDevices.end(),m_Device[p]);
|
|
|
|
|
ASSERT( it != vUnassignedDevices.end() ) // the assigned card better be connected!
|
|
|
|
|
vUnassignedDevices.erase( it );
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
// try to assign each device to a player
|
|
|
|
|
{
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
2003-12-18 07:46:21 +00:00
|
|
|
{
|
2004-02-26 17:05:27 +00:00
|
|
|
LOG->Trace( "Looking for a card for Player %d", p+1 );
|
|
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
if( !m_Device[p].IsBlank() ) // they already have an assigned card
|
2004-02-26 17:05:27 +00:00
|
|
|
{
|
2004-02-27 02:59:20 +00:00
|
|
|
LOG->Trace( "Player %d already has a card: '%s'", p+1, m_Device[p].sOsMountDir.c_str() );
|
2003-12-19 04:52:07 +00:00
|
|
|
continue; // skip
|
2004-02-26 17:05:27 +00:00
|
|
|
}
|
2003-12-19 04:52:07 +00:00
|
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
// search for card dir match
|
2003-12-19 08:51:47 +00:00
|
|
|
if( !PREFSMAN->m_sMemoryCardOsMountPoint[p].empty() )
|
2003-12-18 07:46:21 +00:00
|
|
|
{
|
2003-12-19 04:52:07 +00:00
|
|
|
for( i=0; i<vUnassignedDevices.size(); i++ )
|
|
|
|
|
{
|
2004-02-27 00:31:06 +00:00
|
|
|
UsbStorageDevice &usbd = vUnassignedDevices[i];
|
|
|
|
|
if( usbd.sOsMountDir.CompareNoCase(PREFSMAN->m_sMemoryCardOsMountPoint[p]) == 0 ) // match
|
2004-02-26 17:00:15 +00:00
|
|
|
{
|
|
|
|
|
LOG->Trace( "dir match: iUsbStorageIndex: %d, iBus: %d, iDeviceOnBus: %d, iPortOnHub: %d",
|
|
|
|
|
usbd.iUsbStorageIndex, usbd.iBus, usbd.iDeviceOnBus, usbd.iPortOnHub );
|
2003-12-19 04:52:07 +00:00
|
|
|
goto match;
|
2004-02-26 17:00:15 +00:00
|
|
|
}
|
2003-12-19 04:52:07 +00:00
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
// search for USB bus match
|
|
|
|
|
for( i=0; i<vUnassignedDevices.size(); i++ )
|
|
|
|
|
{
|
2004-02-27 00:31:06 +00:00
|
|
|
UsbStorageDevice &usbd = vUnassignedDevices[i];
|
2003-12-18 07:46:21 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
if( PREFSMAN->m_iMemoryCardUsbBus[p] != -1 &&
|
2004-02-27 00:31:06 +00:00
|
|
|
PREFSMAN->m_iMemoryCardUsbBus[p] != usbd.iBus )
|
2003-12-19 04:52:07 +00:00
|
|
|
continue; // not a match
|
2003-12-18 07:46:21 +00:00
|
|
|
|
2003-12-19 04:52:07 +00:00
|
|
|
if( PREFSMAN->m_iMemoryCardUsbPort[p] != -1 &&
|
2004-02-27 00:31:06 +00:00
|
|
|
PREFSMAN->m_iMemoryCardUsbPort[p] != usbd.iPortOnHub )
|
2003-12-19 04:52:07 +00:00
|
|
|
continue; // not a match
|
|
|
|
|
|
2004-02-26 17:00:15 +00:00
|
|
|
LOG->Trace( "bus/port match: iUsbStorageIndex: %d, iBus: %d, iDeviceOnBus: %d, iPortOnHub: %d",
|
|
|
|
|
usbd.iUsbStorageIndex, usbd.iBus, usbd.iDeviceOnBus, usbd.iPortOnHub );
|
2003-12-19 04:52:07 +00:00
|
|
|
goto match;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG->Trace( "Player %d memory card: none", p+1 );
|
|
|
|
|
continue; // no matches
|
2003-12-18 07:46:21 +00:00
|
|
|
match:
|
2003-12-19 04:52:07 +00:00
|
|
|
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_bCardsLocked )
|
2004-03-14 01:40:24 +00:00
|
|
|
{
|
|
|
|
|
if( !m_pDriver->MountAndTestWrite(&m_Device[p], MEM_CARD_MOUNT_POINT[p]) )
|
|
|
|
|
{
|
2003-12-19 04:52:07 +00:00
|
|
|
m_bWriteError[p] = true;
|
2004-03-14 01:40:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2003-12-19 04:52:07 +00:00
|
|
|
|
|
|
|
|
// play sound
|
|
|
|
|
if( m_bWriteError[p] )
|
|
|
|
|
m_soundError.Play();
|
|
|
|
|
else if( m_bTooLate[p] )
|
|
|
|
|
m_soundTooLate.Play();
|
|
|
|
|
else
|
|
|
|
|
m_soundReady.Play();
|
|
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
}
|
2003-12-19 08:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryCardManager::FlushAllDisks()
|
|
|
|
|
{
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
|
|
|
|
{
|
|
|
|
|
if( !m_Device[p].IsBlank() ) // they already have an assigned card
|
|
|
|
|
continue; // skip
|
|
|
|
|
if( m_bWriteError[p] || m_bTooLate[p] )
|
|
|
|
|
continue; // skip
|
|
|
|
|
m_pDriver->Flush(&m_Device[p]);
|
|
|
|
|
}
|
2004-02-26 07:28:59 +00:00
|
|
|
|
2004-02-26 07:36:30 +00:00
|
|
|
m_pDriver->ResetUsbStorage();
|
2003-12-19 08:17:44 +00:00
|
|
|
}
|