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"
|
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-17 10:20:53 +00:00
|
|
|
m_bTooLate[NUM_PLAYERS] = false;
|
|
|
|
|
m_bWriteError[NUM_PLAYERS] = false;
|
|
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
m_pDriver->GetStorageDevices( m_vStorageDevices );
|
|
|
|
|
ReassignCards();
|
2003-12-18 04:24:25 +00:00
|
|
|
|
|
|
|
|
m_soundConnect.Load( THEME->GetPathToS("MemoryCardManager connect") );
|
|
|
|
|
m_soundDisconnect.Load( THEME->GetPathToS("MemoryCardManager disconnect") );
|
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()
|
|
|
|
|
{
|
2003-12-17 03:43:31 +00:00
|
|
|
|
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 10:20:53 +00:00
|
|
|
unsigned i;
|
2003-12-17 08:43:14 +00:00
|
|
|
|
2003-12-17 10:20:53 +00:00
|
|
|
// check for disconnects
|
|
|
|
|
for( i=0; i<vOld.size(); i++ )
|
2003-12-17 08:43:14 +00:00
|
|
|
{
|
2003-12-17 10:20:53 +00:00
|
|
|
const UsbStorageDevice old = vOld[i];
|
2003-12-18 07:46:21 +00:00
|
|
|
if( find(vNew.begin(),vNew.end(),old) == vNew.end() ) // didn't find
|
2003-12-18 04:24:25 +00:00
|
|
|
{
|
2003-12-18 07:46:21 +00:00
|
|
|
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
|
|
|
|
|
for( i=0; i<vNew.size(); i++ )
|
2003-12-17 06:59:37 +00:00
|
|
|
{
|
2003-12-17 10:20:53 +00:00
|
|
|
const UsbStorageDevice newd = vNew[i];
|
2003-12-18 07:46:21 +00:00
|
|
|
if( find(vOld.begin(),vOld.end(),newd) == vOld.end() ) // didn't find
|
2003-12-18 04:24:25 +00:00
|
|
|
{
|
2003-12-18 07:46:21 +00:00
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( m_bCardsLocked ) // Cards are locked and play has begun.
|
|
|
|
|
{
|
|
|
|
|
// Don't re-assign devices
|
|
|
|
|
|
|
|
|
|
// play disconnect sound if locked card was removed
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
|
|
|
|
{
|
|
|
|
|
if( !m_Device[p].IsBlank() ) // card is in use
|
|
|
|
|
{
|
|
|
|
|
if( find(vOld.begin(),vOld.end(),m_Device[p]) != vOld.end() ) // match
|
|
|
|
|
{
|
|
|
|
|
m_Device[p].MakeBlank();
|
|
|
|
|
m_soundDisconnect.Play();
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-12-18 04:24:25 +00:00
|
|
|
}
|
2003-12-17 06:59:37 +00:00
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
else // cards aren't locked.
|
|
|
|
|
{
|
|
|
|
|
// play sound for connects or disconnects
|
|
|
|
|
if( !vConnects.empty() )
|
|
|
|
|
m_soundConnect.Play();
|
|
|
|
|
|
|
|
|
|
if( !vDisconnects.empty() )
|
|
|
|
|
m_soundDisconnect.Play();
|
|
|
|
|
|
|
|
|
|
ReassignCards();
|
|
|
|
|
}
|
2003-12-15 08:34:06 +00:00
|
|
|
}
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemoryCardManager::CardState MemoryCardManager::GetCardState( PlayerNumber pn )
|
|
|
|
|
{
|
2003-12-18 07:46:21 +00:00
|
|
|
if( m_Device[pn].IsBlank() )
|
2003-12-17 10:20:53 +00:00
|
|
|
return no_card;
|
|
|
|
|
else if( m_bTooLate[pn] )
|
|
|
|
|
return too_late;
|
|
|
|
|
else if( m_bWriteError[pn] )
|
|
|
|
|
return write_error;
|
|
|
|
|
else
|
|
|
|
|
return ready;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2003-12-18 07:46:21 +00:00
|
|
|
return m_Device[pn].sOsMountDir;
|
2003-12-17 10:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryCardManager::LockCards( bool bLock )
|
|
|
|
|
{
|
|
|
|
|
if( bLock == m_bCardsLocked )
|
|
|
|
|
return; // redundant
|
|
|
|
|
|
|
|
|
|
m_bCardsLocked = bLock;
|
|
|
|
|
|
|
|
|
|
if( bLock )
|
2003-12-15 08:34:06 +00:00
|
|
|
{
|
2003-12-17 10:20:53 +00:00
|
|
|
// try mounting
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
2003-12-16 05:10:27 +00:00
|
|
|
{
|
2003-12-18 07:46:21 +00:00
|
|
|
if( !m_Device[p].IsBlank() )
|
|
|
|
|
m_bWriteError[p] = m_pDriver->MountAndTestWrite( &m_Device[p] );
|
2003-12-16 05:10:27 +00:00
|
|
|
}
|
2003-12-17 03:43:31 +00:00
|
|
|
}
|
2003-12-17 10:20:53 +00:00
|
|
|
else
|
2003-12-17 03:43:31 +00:00
|
|
|
{
|
2003-12-17 10:20:53 +00:00
|
|
|
// clear error flags
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
2003-12-16 05:10:27 +00:00
|
|
|
{
|
2003-12-17 10:20:53 +00:00
|
|
|
m_bTooLate[p] = false;
|
|
|
|
|
m_bWriteError[p] = false;
|
2003-12-16 05:10:27 +00:00
|
|
|
}
|
2003-12-18 07:46:21 +00:00
|
|
|
|
|
|
|
|
ReassignCards();
|
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-18 07:46:21 +00:00
|
|
|
void MemoryCardManager::ReassignCards()
|
|
|
|
|
{
|
|
|
|
|
// Do re-assigning: choose a storage device for each player
|
|
|
|
|
vector<UsbStorageDevice> vDevices = m_vStorageDevices; // copy
|
|
|
|
|
for( int p=0; p<NUM_PLAYERS; p++ )
|
|
|
|
|
{
|
|
|
|
|
m_Device[p].MakeBlank();
|
|
|
|
|
|
2003-12-18 21:18:59 +00:00
|
|
|
unsigned i;
|
2003-12-18 07:46:21 +00:00
|
|
|
|
|
|
|
|
// search for card dir match
|
|
|
|
|
if( !PREFSMAN->m_sMemoryCardDir[p].empty() )
|
|
|
|
|
{
|
|
|
|
|
for( i=0; i<vDevices.size(); i++ )
|
|
|
|
|
{
|
|
|
|
|
UsbStorageDevice &usd = vDevices[i];
|
|
|
|
|
if( usd.sOsMountDir.CompareNoCase(PREFSMAN->m_sMemoryCardDir[p]) == 0 ) // match
|
|
|
|
|
goto match;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// search for USB bus match
|
|
|
|
|
for( i=0; i<vDevices.size(); i++ )
|
|
|
|
|
{
|
|
|
|
|
UsbStorageDevice &usd = vDevices[i];
|
|
|
|
|
if( PREFSMAN->m_iMemoryCardUsbBus[p] != -1 &&
|
|
|
|
|
PREFSMAN->m_iMemoryCardUsbBus[p] != usd.iBus )
|
|
|
|
|
continue; // not a match
|
|
|
|
|
|
|
|
|
|
if( PREFSMAN->m_iMemoryCardUsbPort[p] != -1 &&
|
|
|
|
|
PREFSMAN->m_iMemoryCardUsbPort[p] != usd.iPortOnHub )
|
|
|
|
|
continue; // not a match
|
|
|
|
|
|
|
|
|
|
goto match;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG->Trace( "Player %d memory card: none", p+1 );
|
|
|
|
|
continue; // no matches
|
|
|
|
|
match:
|
|
|
|
|
m_Device[p] = vDevices[i]; // save a copy
|
|
|
|
|
LOG->Trace( "Player %d memory card: '%s'", p+1, m_Device[p].sOsMountDir.c_str() );
|
|
|
|
|
vDevices.erase( vDevices.begin()+i ); // remove the device so we don't match it for another player
|
|
|
|
|
}
|
|
|
|
|
}
|