Files
itgmania212121/stepmania/src/ScreenServiceAction.cpp
T

422 lines
14 KiB
C++
Raw Normal View History

#include "global.h"
#include "ScreenServiceAction.h"
#include "ThemeManager.h"
#include "Bookkeeper.h"
#include "Profile.h"
#include "ProfileManager.h"
#include "ScreenManager.h"
#include "RageFileManager.h"
2005-12-21 21:11:49 +00:00
#include "RageLog.h"
#include "PrefsManager.h"
#include "SongManager.h"
#include "song.h"
#include "MemoryCardManager.h"
#include "GameState.h"
#include "PlayerState.h"
2005-12-22 03:10:04 +00:00
#include "LocalizedString.h"
2005-12-21 09:56:16 +00:00
static LocalizedString BOOKKEEPING_DATA_CLEARED( "ScreenServiceAction", "Bookkeeping data cleared." );
2006-01-22 01:00:06 +00:00
static RString ClearBookkeepingData()
{
BOOKKEEPER->ClearAll();
BOOKKEEPER->WriteToDisk();
return BOOKKEEPING_DATA_CLEARED.GetValue();
}
2005-12-21 09:56:16 +00:00
static LocalizedString MACHINE_STATS_CLEARED( "ScreenServiceAction", "Machine stats cleared." );
2006-01-22 01:00:06 +00:00
static RString ClearMachineStats()
{
Profile* pProfile = PROFILEMAN->GetMachineProfile();
// don't reset the Guid
2006-01-22 01:00:06 +00:00
RString sGuid = pProfile->m_sGuid;
pProfile->InitAll();
pProfile->m_sGuid = sGuid;
PROFILEMAN->SaveMachineProfile();
return MACHINE_STATS_CLEARED.GetValue();
}
2005-12-21 09:56:16 +00:00
static LocalizedString MACHINE_EDITS_CLEARED( "ScreenServiceAction", "%d edits cleared, %d errors." );
2006-01-22 01:00:06 +00:00
static RString ClearMachineEdits()
{
int iNumAttempted = 0;
int iNumSuccessful = 0;
2006-01-22 01:00:06 +00:00
vector<RString> vsEditFiles;
2005-12-01 03:20:25 +00:00
GetDirListing( PROFILEMAN->GetProfileDir(ProfileSlot_Machine)+EDIT_STEPS_SUBDIR+"*.edit", vsEditFiles, false, true );
GetDirListing( PROFILEMAN->GetProfileDir(ProfileSlot_Machine)+EDIT_COURSES_SUBDIR+"*.crs", vsEditFiles, false, true );
2006-01-22 01:00:06 +00:00
FOREACH_CONST( RString, vsEditFiles, i )
{
iNumAttempted++;
bool bSuccess = FILEMAN->Remove( *i );
if( bSuccess )
iNumSuccessful++;
}
// reload the machine profile
PROFILEMAN->SaveMachineProfile();
PROFILEMAN->LoadMachineProfile();
2005-12-21 09:56:16 +00:00
int iNumErrors = iNumAttempted-iNumSuccessful;
return ssprintf(MACHINE_EDITS_CLEARED.GetValue(),iNumSuccessful,iNumErrors);
}
2005-12-21 03:56:20 +00:00
static PlayerNumber GetFirstReadyMemoryCard()
{
FOREACH_PlayerNumber( pn )
{
2005-12-01 03:20:25 +00:00
if( MEMCARDMAN->GetCardState(pn) != MemoryCardState_Ready )
continue; // skip
2005-11-30 05:33:01 +00:00
if( !MEMCARDMAN->IsMounted(pn) )
MEMCARDMAN->MountCard(pn);
2005-12-21 03:56:20 +00:00
return pn;
}
2005-12-21 03:56:20 +00:00
return PLAYER_INVALID;
}
2005-12-21 09:56:16 +00:00
static LocalizedString MEMORY_CARD_EDITS_NOT_CLEARED( "ScreenServiceAction", "Edits not cleared - No memory cards ready." );
static LocalizedString EDITS_CLEARED ( "ScreenServiceAction", "%d edits cleared, %d errors." );
2006-01-22 01:00:06 +00:00
static RString ClearMemoryCardEdits()
2005-12-21 03:56:20 +00:00
{
PlayerNumber pn = GetFirstReadyMemoryCard();
if( pn == PLAYER_INVALID )
return MEMORY_CARD_EDITS_NOT_CLEARED.GetValue();
2005-12-21 03:56:20 +00:00
int iNumAttempted = 0;
int iNumSuccessful = 0;
2005-12-21 03:58:07 +00:00
if( !MEMCARDMAN->IsMounted(pn) )
MEMCARDMAN->MountCard(pn);
2006-01-22 01:00:06 +00:00
RString sDir = MEM_CARD_MOUNT_POINT[pn] + (RString)PREFSMAN->m_sMemoryCardProfileSubdir + "/";
vector<RString> vsEditFiles;
2005-12-21 03:58:07 +00:00
GetDirListing( sDir+EDIT_STEPS_SUBDIR+"*.edit", vsEditFiles, false, true );
GetDirListing( sDir+EDIT_COURSES_SUBDIR+"*.crs", vsEditFiles, false, true );
2006-01-22 01:00:06 +00:00
FOREACH_CONST( RString, vsEditFiles, i )
2005-12-21 03:58:07 +00:00
{
iNumAttempted++;
bool bSuccess = FILEMAN->Remove( *i );
if( bSuccess )
iNumSuccessful++;
}
2005-12-21 03:58:07 +00:00
MEMCARDMAN->UnmountCard(pn);
2005-12-21 09:56:16 +00:00
return ssprintf(EDITS_CLEARED.GetValue(),iNumSuccessful,iNumAttempted-iNumSuccessful);
}
2005-12-21 09:56:16 +00:00
static LocalizedString STATS_NOT_SAVED ( "ScreenServiceAction", "Stats not saved - No memory cards ready." );
static LocalizedString MACHINE_STATS_SAVED ( "ScreenServiceAction", "Machine stats saved to P%d card." );
static LocalizedString ERROR_SAVING_MACHINE_STATS ( "ScreenServiceAction", "Error saving machine stats to P%d card." );
2006-01-22 01:00:06 +00:00
static RString TransferStatsMachineToMemoryCard()
{
2005-12-21 03:56:20 +00:00
PlayerNumber pn = GetFirstReadyMemoryCard();
if( pn == PLAYER_INVALID )
return STATS_NOT_SAVED.GetValue();
2005-12-21 03:58:07 +00:00
if( !MEMCARDMAN->IsMounted(pn) )
MEMCARDMAN->MountCard(pn);
2006-01-22 01:00:06 +00:00
RString sDir = MEM_CARD_MOUNT_POINT[pn];
2005-12-21 03:58:07 +00:00
sDir += "MachineProfile/";
2005-12-21 03:58:07 +00:00
bool bSaved = PROFILEMAN->GetMachineProfile()->SaveAllToDir( sDir, PREFSMAN->m_bSignProfileData );
2005-12-21 03:58:07 +00:00
MEMCARDMAN->UnmountCard(pn);
2005-12-21 03:58:07 +00:00
if( bSaved )
2005-12-21 09:56:16 +00:00
return ssprintf(MACHINE_STATS_SAVED.GetValue(),pn+1);
2005-12-21 03:58:07 +00:00
else
2005-12-21 09:56:16 +00:00
return ssprintf(ERROR_SAVING_MACHINE_STATS.GetValue(),pn+1);
}
2005-12-21 09:56:16 +00:00
static LocalizedString STATS_NOT_LOADED ( "ScreenServiceAction", "Stats not loaded - No memory cards ready." );
static LocalizedString MACHINE_STATS_LOADED ( "ScreenServiceAction", "Machine stats loaded from P%d card." );
static LocalizedString THERE_IS_NO_PROFILE ( "ScreenServiceAction", "There is no machine profile on P%d card." );
static LocalizedString PROFILE_CORRUPT ( "ScreenServiceAction", "The profile on P%d card contains corrupt or tampered data." );
2006-01-22 01:00:06 +00:00
static RString TransferStatsMemoryCardToMachine()
{
2005-12-21 03:56:20 +00:00
PlayerNumber pn = GetFirstReadyMemoryCard();
if( pn == PLAYER_INVALID )
return STATS_NOT_LOADED.GetValue();
2005-12-21 03:58:07 +00:00
if( !MEMCARDMAN->IsMounted(pn) )
MEMCARDMAN->MountCard(pn);
2006-01-22 01:00:06 +00:00
RString sDir = MEM_CARD_MOUNT_POINT[pn];
2005-12-21 03:58:07 +00:00
sDir += "MachineProfile/";
2005-12-21 03:58:07 +00:00
Profile backup = *PROFILEMAN->GetMachineProfile();
2005-12-21 03:58:07 +00:00
ProfileLoadResult lr = PROFILEMAN->GetMachineProfile()->LoadAllFromDir( sDir, PREFSMAN->m_bSignProfileData );
2006-01-22 01:00:06 +00:00
RString s;
2005-12-21 03:58:07 +00:00
switch( lr )
{
case ProfileLoadResult_Success:
2005-12-21 09:56:16 +00:00
s = ssprintf(MACHINE_STATS_LOADED.GetValue(),pn+1);
2005-12-21 03:58:07 +00:00
break;
case ProfileLoadResult_FailedNoProfile:
*PROFILEMAN->GetMachineProfile() = backup;
2005-12-21 09:56:16 +00:00
s = ssprintf(THERE_IS_NO_PROFILE.GetValue(),pn+1);
2005-12-21 03:58:07 +00:00
break;
case ProfileLoadResult_FailedTampered:
*PROFILEMAN->GetMachineProfile() = backup;
2005-12-21 09:56:16 +00:00
s = ssprintf(PROFILE_CORRUPT.GetValue(),pn+1);
2005-12-21 03:58:07 +00:00
break;
default:
ASSERT(0);
}
2005-12-21 03:58:07 +00:00
MEMCARDMAN->UnmountCard(pn);
2005-12-21 09:56:16 +00:00
return s;
}
2006-01-22 01:00:06 +00:00
static void CopyEdits( const RString &sFrom, const RString &sTo, int &iNumAttempted, int &iNumSuccessful, int &iNumOverwritten )
2005-12-21 03:49:34 +00:00
{
2005-12-21 04:05:08 +00:00
iNumAttempted = 0;
iNumSuccessful = 0;
iNumOverwritten = 0;
2005-12-21 03:49:34 +00:00
{
2006-01-22 01:00:06 +00:00
RString sFromDir = sFrom + EDIT_STEPS_SUBDIR;
RString sToDir = sTo + EDIT_STEPS_SUBDIR;
2006-01-22 01:00:06 +00:00
vector<RString> vsFiles;
2005-12-21 03:58:07 +00:00
GetDirListing( sFromDir+"*.edit", vsFiles, false, false );
2006-01-22 01:00:06 +00:00
FOREACH_CONST( RString, vsFiles, i )
{
2005-12-21 03:58:07 +00:00
iNumAttempted++;
if( DoesFileExist(sToDir+*i) )
iNumOverwritten++;
bool bSuccess = FileCopy( sFromDir+*i, sToDir+*i );
if( bSuccess )
iNumSuccessful++;
}
2005-12-21 03:58:07 +00:00
}
2005-12-21 04:05:08 +00:00
2005-12-21 03:58:07 +00:00
{
2006-01-22 01:00:06 +00:00
RString sFromDir = sFrom + EDIT_COURSES_SUBDIR;
RString sToDir = sTo + EDIT_COURSES_SUBDIR;
2006-01-22 01:00:06 +00:00
vector<RString> vsFiles;
2005-12-21 03:58:07 +00:00
GetDirListing( sFromDir+"*.crs", vsFiles, false, false );
2006-01-22 01:00:06 +00:00
FOREACH_CONST( RString, vsFiles, i )
2005-12-21 03:58:07 +00:00
{
iNumAttempted++;
if( DoesFileExist(sToDir+*i) )
iNumOverwritten++;
bool bSuccess = FileCopy( sFromDir+*i, sToDir+*i );
if( bSuccess )
iNumSuccessful++;
}
}
2005-12-21 04:05:08 +00:00
}
2006-01-22 01:00:06 +00:00
static void SyncFiles( const RString &sFromDir, const RString &sToDir, const RString &sMask, int &iNumAdded, int &iNumDeleted, int &iNumOverwritten, int &iNumFailed )
2005-12-21 21:11:49 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> vsFilesSource;
2005-12-21 21:11:49 +00:00
GetDirListing( sFromDir+sMask, vsFilesSource, false, false );
2006-01-22 01:00:06 +00:00
vector<RString> vsFilesDest;
2005-12-21 21:11:49 +00:00
GetDirListing( sToDir+sMask, vsFilesDest, false, false );
2006-01-22 01:00:06 +00:00
vector<RString> vsToDelete;
2005-12-21 21:11:49 +00:00
GetAsNotInBs( vsFilesDest, vsFilesSource, vsToDelete );
for( unsigned i = 0; i < vsToDelete.size(); ++i )
{
2006-01-22 01:00:06 +00:00
RString sFile = sToDir + vsToDelete[i];
2005-12-21 21:11:49 +00:00
LOG->Trace( "Delete \"%s\"", sFile.c_str() );
if( FILEMAN->Remove(sFile) )
++iNumDeleted;
else
++iNumFailed;
}
for( unsigned i = 0; i < vsFilesSource.size(); ++i )
{
2006-01-22 01:00:06 +00:00
RString sFileFrom = sFromDir + vsFilesSource[i];
RString sFileTo = sToDir + vsFilesSource[i];
2005-12-21 21:11:49 +00:00
LOG->Trace( "Copy \"%s\"", sFileFrom.c_str() );
bool bOverwrite = DoesFileExist( sFileTo );
bool bSuccess = FileCopy( sFileFrom, sFileTo );
if( bSuccess )
{
if( bOverwrite )
++iNumOverwritten;
else
++iNumAdded;
}
else
++iNumFailed;
}
}
2006-01-22 01:00:06 +00:00
static void SyncEdits( const RString &sFromDir, const RString &sToDir, int &iNumAdded, int &iNumDeleted, int &iNumOverwritten, int &iNumFailed )
2005-12-21 21:11:49 +00:00
{
iNumAdded = 0;
iNumDeleted = 0;
iNumOverwritten = 0;
iNumFailed = 0;
SyncFiles( sFromDir + EDIT_STEPS_SUBDIR, sToDir + EDIT_STEPS_SUBDIR, "*.edit", iNumAdded, iNumDeleted, iNumOverwritten, iNumFailed );
SyncFiles( sFromDir + EDIT_COURSES_SUBDIR, sToDir + EDIT_COURSES_SUBDIR, "*.crs", iNumAdded, iNumDeleted, iNumOverwritten, iNumFailed );
}
2005-12-21 09:56:16 +00:00
static LocalizedString EDITS_NOT_COPIED( "ScreenServiceAction", "Edits not copied - No memory cards ready." );
2005-12-21 20:07:48 +00:00
static LocalizedString COPIED_TO_CARD( "ScreenServiceAction", "Copied to P%d card:" );
2005-12-21 21:11:49 +00:00
static LocalizedString COPIED_AND_OVERWRITTEN( "ScreenServiceAction", "%d copied, %d overwritten" );
static LocalizedString ADDED_AND_OVERWRITTEN( "ScreenServiceAction", "%d added, %d overwritten" );
static LocalizedString FAILED( "ScreenServiceAction", "%d failed" );
static LocalizedString DELETED( "ScreenServiceAction", "%d deleted" );
2006-01-22 01:00:06 +00:00
static RString CopyEditsMachineToMemoryCard()
2005-12-21 04:05:08 +00:00
{
PlayerNumber pn = GetFirstReadyMemoryCard();
if( pn == PLAYER_INVALID )
return EDITS_NOT_COPIED.GetValue();
2005-12-21 04:05:08 +00:00
if( !MEMCARDMAN->IsMounted(pn) )
MEMCARDMAN->MountCard(pn);
int iNumAttempted = 0;
int iNumSuccessful = 0;
int iNumOverwritten = 0;
2006-01-22 01:00:06 +00:00
RString sFromDir = PROFILEMAN->GetProfileDir(ProfileSlot_Machine);
RString sToDir = MEM_CARD_MOUNT_POINT[pn] + (RString)PREFSMAN->m_sMemoryCardProfileSubdir + "/";
2005-12-21 04:05:08 +00:00
CopyEdits( sFromDir, sToDir, iNumAttempted, iNumSuccessful, iNumOverwritten );
2005-12-21 03:58:07 +00:00
MEMCARDMAN->UnmountCard(pn);
2006-01-22 01:00:06 +00:00
RString sRet = ssprintf( COPIED_TO_CARD.GetValue(), pn+1 ) + " ";
2005-12-21 20:07:48 +00:00
sRet += ssprintf( COPIED_AND_OVERWRITTEN.GetValue(), iNumSuccessful, iNumOverwritten );
if( iNumSuccessful < iNumAttempted )
2006-01-22 01:00:06 +00:00
sRet += RString("; ") + ssprintf( FAILED.GetValue(), iNumAttempted-iNumSuccessful );
2005-12-21 21:11:49 +00:00
return sRet;
}
2006-01-22 01:00:06 +00:00
static RString SyncEditsMachineToMemoryCard()
2005-12-21 21:11:49 +00:00
{
PlayerNumber pn = GetFirstReadyMemoryCard();
if( pn == PLAYER_INVALID )
return EDITS_NOT_COPIED.GetValue();
2005-12-21 21:11:49 +00:00
if( !MEMCARDMAN->IsMounted(pn) )
MEMCARDMAN->MountCard(pn);
int iNumAdded = 0;
int iNumDeleted = 0;
int iNumOverwritten = 0;
int iNumFailed = 0;
2006-01-22 01:00:06 +00:00
RString sFromDir = PROFILEMAN->GetProfileDir(ProfileSlot_Machine);
RString sToDir = MEM_CARD_MOUNT_POINT[pn] + (RString)PREFSMAN->m_sMemoryCardProfileSubdir + "/";
2005-12-21 21:11:49 +00:00
SyncEdits( sFromDir, sToDir, iNumAdded, iNumDeleted, iNumOverwritten, iNumFailed );
MEMCARDMAN->UnmountCard(pn);
2006-01-22 01:00:06 +00:00
RString sRet = ssprintf( COPIED_TO_CARD.GetValue(), pn+1 ) + " ";
2005-12-21 21:11:49 +00:00
sRet += ssprintf( ADDED_AND_OVERWRITTEN.GetValue(), iNumAdded, iNumOverwritten );
if( iNumDeleted )
2006-01-22 01:00:06 +00:00
sRet += RString(" ") + ssprintf( DELETED.GetValue(), iNumDeleted );
2005-12-21 21:11:49 +00:00
if( iNumFailed )
2006-01-22 01:00:06 +00:00
sRet += RString("; ") + ssprintf( FAILED.GetValue(), iNumFailed );
2005-12-21 20:07:48 +00:00
return sRet;
}
2005-12-21 20:07:48 +00:00
static LocalizedString COPIED_FROM_CARD( "ScreenServiceAction", "Copied from P%d card:" );
2006-01-22 01:00:06 +00:00
static RString CopyEditsMemoryCardToMachine()
{
2005-12-21 03:49:34 +00:00
PlayerNumber pn = GetFirstReadyMemoryCard();
if( pn == PLAYER_INVALID )
return EDITS_NOT_COPIED.GetValue();
2005-12-21 03:58:07 +00:00
if( !MEMCARDMAN->IsMounted(pn) )
MEMCARDMAN->MountCard(pn);
int iNumAttempted = 0;
int iNumSuccessful = 0;
int iNumOverwritten = 0;
2006-01-22 01:00:06 +00:00
RString sFromDir = MEM_CARD_MOUNT_POINT[pn] + (RString)PREFSMAN->m_sMemoryCardProfileSubdir + "/";
RString sToDir = PROFILEMAN->GetProfileDir(ProfileSlot_Machine);
2005-12-21 03:58:07 +00:00
2005-12-21 04:05:08 +00:00
CopyEdits( sFromDir, sToDir, iNumAttempted, iNumSuccessful, iNumOverwritten );
2005-12-21 03:58:07 +00:00
MEMCARDMAN->UnmountCard(pn);
2005-12-21 03:58:07 +00:00
// reload the machine profile
PROFILEMAN->SaveMachineProfile();
PROFILEMAN->LoadMachineProfile();
2006-01-22 01:00:06 +00:00
RString sRet = ssprintf( COPIED_FROM_CARD.GetValue(), pn+1 ) + " ";
2005-12-21 20:07:48 +00:00
sRet += ssprintf( COPIED_AND_OVERWRITTEN.GetValue(), iNumSuccessful, iNumOverwritten );
if( iNumSuccessful < iNumAttempted )
2006-01-22 01:00:06 +00:00
sRet += RString("; ") + ssprintf( FAILED.GetValue(), iNumAttempted-iNumSuccessful );
2005-12-21 20:07:48 +00:00
return sRet;
}
2005-12-21 09:56:16 +00:00
static LocalizedString PREFERENCES_RESET( "ScreenServiceAction", "Preferences reset." );
2006-01-22 01:00:06 +00:00
static RString ResetPreferences()
{
PREFSMAN->ResetToFactoryDefaults();
return PREFERENCES_RESET.GetValue();
}
2006-01-15 20:46:15 +00:00
REGISTER_SCREEN_CLASS( ScreenServiceAction );
2006-01-15 19:04:34 +00:00
void ScreenServiceAction::Init()
{
2006-01-15 19:04:34 +00:00
ScreenPrompt::Init();
2006-01-22 01:00:06 +00:00
RString sAction = THEME->GetMetric(m_sName,"Action");
2006-01-22 01:00:06 +00:00
RString (*pfn)() = NULL;
2006-01-18 10:09:34 +00:00
if( sAction == "ClearBookkeepingData" ) pfn = ClearBookkeepingData;
else if( sAction == "ClearMachineStats" ) pfn = ClearMachineStats;
else if( sAction == "ClearMachineEdits" ) pfn = ClearMachineEdits;
else if( sAction == "ClearMemoryCardEdits" ) pfn = ClearMemoryCardEdits;
else if( sAction == "TransferStatsMachineToMemoryCard" ) pfn = TransferStatsMachineToMemoryCard;
else if( sAction == "TransferStatsMemoryCardToMachine" ) pfn = TransferStatsMemoryCardToMachine;
else if( sAction == "CopyEditsMachineToMemoryCard" ) pfn = CopyEditsMachineToMemoryCard;
else if( sAction == "CopyEditsMemoryCardToMachine" ) pfn = CopyEditsMemoryCardToMachine;
2005-12-21 21:11:49 +00:00
else if( sAction == "SyncEditsMachineToMemoryCard" ) pfn = SyncEditsMachineToMemoryCard;
2006-01-18 10:09:34 +00:00
else if( sAction == "ResetPreferences" ) pfn = ResetPreferences;
ASSERT_M( pfn, sAction );
2006-01-22 01:00:06 +00:00
RString s = pfn();
ScreenPrompt::SetPromptSettings( s, PROMPT_OK );
}
/*
* (c) 2001-2005 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/