diff --git a/stepmania/src/MemoryCardManager.cpp b/stepmania/src/MemoryCardManager.cpp index e696bf6131..205a993860 100644 --- a/stepmania/src/MemoryCardManager.cpp +++ b/stepmania/src/MemoryCardManager.cpp @@ -16,10 +16,11 @@ #include "arch/arch.h" #include "ScreenManager.h" #include "ThemeManager.h" +#include "PrefsManager.h" +#include "RageLog.h" MemoryCardManager* MEMCARDMAN = NULL; // global and accessable from anywhere in our program -vector g_StorageDevices; MemoryCardManager::MemoryCardManager() { @@ -30,7 +31,8 @@ MemoryCardManager::MemoryCardManager() m_bTooLate[NUM_PLAYERS] = false; m_bWriteError[NUM_PLAYERS] = false; } - m_pDriver->GetStorageDevices( g_StorageDevices ); + m_pDriver->GetStorageDevices( m_vStorageDevices ); + ReassignCards(); m_soundConnect.Load( THEME->GetPathToS("MemoryCardManager connect") ); m_soundDisconnect.Load( THEME->GetPathToS("MemoryCardManager disconnect") ); @@ -45,9 +47,11 @@ void MemoryCardManager::Update( float fDelta ) { if( m_pDriver->StorageDevicesChanged() ) { - vector vOld = g_StorageDevices; // make a copy - m_pDriver->GetStorageDevices( g_StorageDevices ); - vector &vNew = g_StorageDevices; + vector vOld = m_vStorageDevices; // copy + m_pDriver->GetStorageDevices( m_vStorageDevices ); + vector &vNew = m_vStorageDevices; + vector vConnects; // fill these in below + vector vDisconnects; // fill these in below unsigned i; @@ -55,10 +59,10 @@ void MemoryCardManager::Update( float fDelta ) for( i=0; iSystemMessage( ssprintf("Disconnected bus %d port %d device %d path %s", old.iBus, old.iPortOnHub, old.iDeviceOnBus, old.sOsMountDir.c_str()) ); - m_soundDisconnect.Play(); + 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 ); } } @@ -66,18 +70,47 @@ void MemoryCardManager::Update( float fDelta ) for( i=0; iSystemMessage( ssprintf("Connected bus %d port %d device %d path %s", newd.iBus, newd.iPortOnHub, newd.iDeviceOnBus, newd.sOsMountDir.c_str()) ); - m_soundConnect.Play(); + 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; psOsMountDir; + return m_Device[pn].sOsMountDir; } void MemoryCardManager::LockCards( bool bLock ) @@ -107,8 +140,8 @@ void MemoryCardManager::LockCards( bool bLock ) // try mounting for( int p=0; pMountAndTestWrite( m_pDevice[p] ); + if( !m_Device[p].IsBlank() ) + m_bWriteError[p] = m_pDriver->MountAndTestWrite( &m_Device[p] ); } } else @@ -119,6 +152,52 @@ void MemoryCardManager::LockCards( bool bLock ) m_bTooLate[p] = false; m_bWriteError[p] = false; } + + ReassignCards(); } } +void MemoryCardManager::ReassignCards() +{ + // Do re-assigning: choose a storage device for each player + vector vDevices = m_vStorageDevices; // copy + for( int p=0; pm_sMemoryCardDir[p].empty() ) + { + for( i=0; im_sMemoryCardDir[p]) == 0 ) // match + goto match; + } + } + + // search for USB bus match + for( i=0; im_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 + } +} \ No newline at end of file diff --git a/stepmania/src/MemoryCardManager.h b/stepmania/src/MemoryCardManager.h index babef7f855..242d24a72e 100644 --- a/stepmania/src/MemoryCardManager.h +++ b/stepmania/src/MemoryCardManager.h @@ -12,9 +12,7 @@ */ #include "PlayerNumber.h" #include "RageSound.h" - -struct UsbStorageDevice; -class MemoryCardDriver; +#include "arch/MemoryCard/MemoryCardDriver.h" class MemoryCardManager { @@ -33,12 +31,16 @@ public: void LockCards( bool bLock ); // prevent removing or changing of memory cards protected: + void ReassignCards(); // do our best to assign a Device to each player + MemoryCardDriver *m_pDriver; + vector m_vStorageDevices; // all currently connected + bool m_bCardsLocked; bool m_bTooLate[NUM_PLAYERS]; // card was inserted after lock bool m_bWriteError[NUM_PLAYERS]; // couldn't write to the card - UsbStorageDevice* m_pDevice[NUM_PLAYERS]; // device in the memory card slot, NULL if none + UsbStorageDevice m_Device[NUM_PLAYERS]; // device in the memory card slot, NULL if none RageSound m_soundConnect; RageSound m_soundDisconnect; diff --git a/stepmania/src/PrefsManager.h b/stepmania/src/PrefsManager.h index 61eecdf99a..685c3001f3 100644 --- a/stepmania/src/PrefsManager.h +++ b/stepmania/src/PrefsManager.h @@ -121,7 +121,7 @@ public: int m_iEndlessBreakLength; CString m_sLanguage; CString m_sDefaultLocalProfileID[NUM_PLAYERS]; - CString m_sMemoryCardDir[NUM_PLAYERS]; // always use this dir for the memory card + CString m_sMemoryCardDir[NUM_PLAYERS]; // if set, always use this dir for the memory card int m_iMemoryCardUsbBus[NUM_PLAYERS]; // take the first storage device on this usb bus int m_iMemoryCardUsbPort[NUM_PLAYERS]; // take the first storage device on this usb bus int m_iCenterImageTranslateX; diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriver.h b/stepmania/src/arch/MemoryCard/MemoryCardDriver.h index e4ea410840..2560a13301 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriver.h +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriver.h @@ -4,7 +4,9 @@ struct UsbStorageDevice { - UsbStorageDevice() + UsbStorageDevice() { MakeBlank(); } + + void MakeBlank() { iBus = -1; iDeviceOnBus = -1; @@ -18,11 +20,18 @@ struct UsbStorageDevice int iUsbStorageIndex; CString sOsMountDir; // WITHOUT trailing slash + bool IsBlank() { return sOsMountDir.empty(); } + bool operator==(const UsbStorageDevice& other) { - return - iBus==other.iBus && - iDeviceOnBus==other.iDeviceOnBus; // every time a device is plugged in, it gets a unique device number + // ugly... +#if _WINDOWS + // we don't have hub/port number info on Windows + return sOsMountDir==other.sOsMountDir; // every time a device is plugged in, it gets a unique device number +#else // LINUX or other + return iBus==other.iBus && + iDeviceOnBus==other.iDeviceOnBus; // every time a device is plugged in, it gets a unique device number +#endif } };