Files
itgmania212121/stepmania/src/ProfileManager.cpp
T

362 lines
8.1 KiB
C++
Raw Normal View History

#include "global.h"
/*
-----------------------------------------------------------------------------
Class: ProfileManager
Desc: See header.
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
#include "ProfileManager.h"
#include "RageUtil.h"
#include "arch/arch.h"
#include "PrefsManager.h"
#include "RageLog.h"
#include "IniFile.h"
2003-09-08 07:21:41 +00:00
#include "GameConstantsAndTypes.h"
#include "SongManager.h"
ProfileManager* PROFILEMAN = NULL; // global and accessable from anywhere in our program
#define PROFILE_FILE "Profile.ini"
#define STEPS_MEM_CARD_DATA_FILE "StepsMemCardData.dat"
#define COURSE_MEM_CARD_DATA_FILE "CourseMemCardData.dat"
2003-11-08 15:49:48 +00:00
#define NEW_MEM_CARD_NAME "NewCard"
2003-11-01 22:04:43 +00:00
#define NEW_PROFILE_NAME "NewProfile"
ProfileManager::ProfileManager()
{
for( int p=0; p<NUM_PLAYERS; p++ )
m_bUsingMemoryCard[p] = false;
}
ProfileManager::~ProfileManager()
{
}
2003-11-01 19:36:52 +00:00
void ProfileManager::GetMachineProfileIDs( vector<CString> &asProfileIDsOut )
{
2003-11-01 19:36:52 +00:00
GetDirListing( PROFILES_DIR "*", asProfileIDsOut, true, false );
}
2003-11-01 19:36:52 +00:00
void ProfileManager::GetMachineProfileNames( vector<CString> &asNamesOut )
2003-09-08 07:21:41 +00:00
{
2003-11-01 19:36:52 +00:00
CStringArray vsProfileIDs;
GetMachineProfileIDs( vsProfileIDs );
for( unsigned i=0; i<vsProfileIDs.size(); i++ )
2003-09-08 07:21:41 +00:00
{
2003-11-01 19:36:52 +00:00
CString sProfileID = vsProfileIDs[i];
2003-09-08 07:21:41 +00:00
Profile pro;
2003-11-01 19:36:52 +00:00
pro.LoadFromIni( PROFILES_DIR + sProfileID + SLASH + PROFILE_FILE );
asNamesOut.push_back( pro.m_sName );
2003-09-08 07:21:41 +00:00
}
}
2003-11-01 22:04:43 +00:00
bool ProfileManager::LoadProfile( PlayerNumber pn, CString sProfileDir, bool bIsMemCard )
{
2003-11-01 22:04:43 +00:00
ASSERT( !sProfileDir.empty() );
ASSERT( sProfileDir.Right(1) == SLASH );
2003-11-01 22:04:43 +00:00
m_sProfileDir[pn] = sProfileDir;
m_bUsingMemoryCard[pn] = bIsMemCard;
2003-11-01 19:36:52 +00:00
bool bResult = m_Profile[pn].LoadFromIni( m_sProfileDir[pn]+PROFILE_FILE );
if( !bResult )
{
2003-11-01 22:04:43 +00:00
LOG->Warn( "Attempting to load profile from '%s' and does not exist", sProfileDir.c_str() );
2003-11-01 19:36:52 +00:00
UnloadProfile( pn );
return false;
}
2003-09-08 07:21:41 +00:00
//
// Load scores into SONGMAN
//
SONGMAN->ReadStepsMemCardDataFromFile( m_sProfileDir[pn]+STEPS_MEM_CARD_DATA_FILE, (MemoryCard)pn );
SONGMAN->ReadCourseMemCardDataFromFile( m_sProfileDir[pn]+COURSE_MEM_CARD_DATA_FILE, (MemoryCard)pn );
2003-11-01 19:36:52 +00:00
return true;
}
2003-11-01 22:04:43 +00:00
bool ProfileManager::CreateProfile( CString sProfileDir, CString sName )
{
bool bResult;
CreateDirectories( sProfileDir );
Profile pro;
pro.m_sName = sName;
bResult = pro.SaveToIni( sProfileDir + PROFILE_FILE );
if( !bResult )
return false;
FlushDirCache();
return true;
}
bool ProfileManager::LoadDefaultProfileFromMachine( PlayerNumber pn )
{
CString sProfileID = PREFSMAN->m_sDefaultMachineProfileID[pn];
if( sProfileID.empty() )
{
m_sProfileDir[pn] = "";
return false;
}
CString sDir = PROFILES_DIR + sProfileID + SLASH;
return LoadProfile( pn, sDir, false );
}
2003-11-08 15:49:48 +00:00
CString GetMemCardDir( PlayerNumber pn )
2003-11-01 22:04:43 +00:00
{
CString sDir = PREFSMAN->m_sMemoryCardDir[pn];
if( sDir.empty() )
2003-11-08 15:49:48 +00:00
return sDir;
2003-11-01 22:04:43 +00:00
if( sDir.Right(1) != SLASH )
sDir += SLASH;
2003-11-08 15:49:48 +00:00
return sDir;
}
bool ProfileManager::IsMemoryCardInserted( PlayerNumber pn )
{
CString sDir = GetMemCardDir( pn );
if( sDir.empty() )
return false;
2003-11-01 22:04:43 +00:00
#ifdef _WINDOWS
// Windows will throw up a message box if we try to write to a
// removable drive with no disk inserted. Find out whether there's a
// disk in the drive w/o writing a file.
// find drive letter
vector<CString> matches;
static Regex parse("^([A-Za-z]+):");
parse.Compare(sDir, matches);
if( matches.size() != 1 )
2003-11-01 22:04:43 +00:00
{
return false;
2003-11-01 22:04:43 +00:00
}
else
{
CString sDrive = matches[0];
TCHAR szVolumeNameBuffer[MAX_PATH];
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD lpFileSystemFlags;
TCHAR szFileSystemNameBuffer[MAX_PATH];
BOOL bResult = GetVolumeInformation(
sDrive + ":",
szVolumeNameBuffer,
sizeof(szVolumeNameBuffer),
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&lpFileSystemFlags,
szFileSystemNameBuffer,
sizeof(szFileSystemNameBuffer) );
return !!bResult;
2003-11-01 22:04:43 +00:00
}
#else
// Try to create directory before writing a temp file.
CreateDirectories( sDir );
// Test whether a memory card is usable by trying to write a file.
CString sFile = sDir + "temp";
FILE* fp = fopen( sFile, "w" );
if( fp )
{
fclose( fp );
remove( sFile );
return true;
}
else
{
return false;
}
#endif
2003-11-01 22:04:43 +00:00
}
bool ProfileManager::LoadProfileFromMemoryCard( PlayerNumber pn )
{
2003-11-08 15:49:48 +00:00
CString sDir = GetMemCardDir( pn );
2003-11-01 22:04:43 +00:00
if( sDir.empty() )
return false;
m_bUsingMemoryCard[pn] = true;
bool bResult;
bResult = LoadProfile( pn, sDir, false );
2003-11-08 15:49:48 +00:00
return bResult;
2003-11-01 22:04:43 +00:00
}
2003-11-09 21:39:54 +00:00
2003-11-01 22:04:43 +00:00
bool ProfileManager::LoadFirstAvailableProfile( PlayerNumber pn )
{
// mount card
if( !PREFSMAN->m_sMemoryCardMountCommand[pn].empty() )
system( PREFSMAN->m_sMemoryCardMountCommand[pn] );
if( IsMemoryCardInserted(pn) )
2003-11-08 15:49:48 +00:00
{
if( LoadProfileFromMemoryCard(pn) )
return true;
CString sDir = GetMemCardDir( pn );
2003-11-08 15:49:48 +00:00
CreateProfile( sDir, NEW_MEM_CARD_NAME );
if( LoadProfileFromMemoryCard(pn) )
return true;
}
if( LoadDefaultProfileFromMachine(pn) )
2003-11-01 22:04:43 +00:00
return true;
2003-11-08 15:49:48 +00:00
return false;
2003-11-01 22:04:43 +00:00
}
2003-11-01 19:36:52 +00:00
bool ProfileManager::SaveProfile( PlayerNumber pn )
{
if( m_sProfileDir[pn].empty() )
2003-11-01 19:36:52 +00:00
return false;
2003-11-01 19:36:52 +00:00
m_Profile[pn].SaveToIni( m_sProfileDir[pn]+PROFILE_FILE );
2003-09-08 07:21:41 +00:00
//
// Save scores into SONGMAN
//
2003-11-01 19:36:52 +00:00
// TODO: move record data to this class
SONGMAN->SaveStepsMemCardDataToFile( m_sProfileDir[pn]+STEPS_MEM_CARD_DATA_FILE, (MemoryCard)pn );
SONGMAN->SaveCourseMemCardDataToFile( m_sProfileDir[pn]+COURSE_MEM_CARD_DATA_FILE, (MemoryCard)pn );
2003-11-01 19:36:52 +00:00
return true;
}
2003-11-01 19:36:52 +00:00
void ProfileManager::UnloadProfile( PlayerNumber pn )
{
m_sProfileDir[pn] = "";
m_bUsingMemoryCard[pn] = false;
m_Profile[pn].Init();
2003-09-08 07:21:41 +00:00
}
2003-11-01 19:36:52 +00:00
Profile* ProfileManager::GetProfile( PlayerNumber pn )
2003-09-08 07:21:41 +00:00
{
2003-11-01 19:36:52 +00:00
if( m_sProfileDir[pn].empty() )
return NULL;
else
return &m_Profile[pn];
2003-09-08 07:21:41 +00:00
}
2003-09-08 07:21:41 +00:00
bool Profile::LoadFromIni( CString sIniPath )
{
Init();
CStringArray asBits;
2003-10-29 20:46:37 +00:00
split( Dirname(sIniPath), SLASH, asBits, true );
2003-09-08 07:21:41 +00:00
CString sLastDir = asBits.back(); // this is a number name, e.g. "0000001"
// Fill in a default value in case ini doesn't have it.
2003-11-01 22:04:43 +00:00
m_sName = NEW_PROFILE_NAME;
2003-09-08 07:21:41 +00:00
//
// read ini
//
IniFile ini( sIniPath );
if( !ini.ReadFile() )
return false;
2003-11-01 19:36:52 +00:00
ini.GetValue( "Profile", "DisplayName", m_sName );
2003-09-08 07:21:41 +00:00
ini.GetValue( "Profile", "LastUsedHighScoreName", m_sLastUsedHighScoreName );
return true;
}
2003-11-01 19:36:52 +00:00
bool Profile::SaveToIni( CString sIniPath )
2003-09-08 07:21:41 +00:00
{
IniFile ini( sIniPath );
2003-11-01 19:36:52 +00:00
ini.SetValue( "Profile", "DisplayName", m_sName );
2003-09-08 07:21:41 +00:00
ini.SetValue( "Profile", "LastUsedHighScoreName", m_sLastUsedHighScoreName );
ini.WriteFile();
2003-09-08 07:21:41 +00:00
return true;
}
2003-11-01 19:36:52 +00:00
bool ProfileManager::CreateMachineProfile( CString sName )
2003-09-08 07:21:41 +00:00
{
2003-11-01 22:04:43 +00:00
if( sName.empty() )
2003-11-08 15:49:48 +00:00
sName = "Machine";
2003-11-01 22:04:43 +00:00
2003-09-08 07:21:41 +00:00
//
// Find a free directory name in the profiles directory
//
2003-11-01 19:36:52 +00:00
CString sProfileID, sProfileDir;
const int MAX_TRIES = 1000;
int i;
for( i=0; i<MAX_TRIES; i++ )
2003-09-08 07:21:41 +00:00
{
2003-11-01 19:36:52 +00:00
sProfileID = ssprintf("%08d",i);
sProfileDir = PROFILES_DIR + sProfileID;
if( !DoesFileExist(sProfileDir) )
2003-09-08 07:21:41 +00:00
break;
}
2003-11-01 19:36:52 +00:00
if( i == MAX_TRIES )
return false;
2003-11-01 22:04:43 +00:00
sProfileDir += SLASH;
2003-09-08 07:21:41 +00:00
2003-11-01 22:04:43 +00:00
bool bResult;
bResult = CreateDirectories( sProfileDir );
if( !bResult )
return false;
2003-09-08 07:21:41 +00:00
Profile pro;
2003-11-01 19:36:52 +00:00
pro.m_sName = sName;
2003-11-01 22:04:43 +00:00
bResult = pro.SaveToIni( sProfileDir + PROFILE_FILE );
if( !bResult )
return false;
2003-09-08 07:21:41 +00:00
FlushDirCache();
2003-11-01 19:36:52 +00:00
return true;
}
bool ProfileManager::RenameMachineProfile( CString sProfileID, CString sNewName )
{
ASSERT( !sProfileID.empty() );
CString sProfileDir = PROFILES_DIR + sProfileID;
CString sProfileFile = sProfileDir + SLASH PROFILE_FILE;
Profile pro;
bool bResult;
bResult = pro.LoadFromIni( sProfileFile );
if( !bResult )
return false;
pro.m_sName = sNewName;
bResult = pro.SaveToIni( sProfileFile );
if( !bResult )
return false;
return true;
}
2003-11-01 19:36:52 +00:00
bool ProfileManager::DeleteMachineProfile( CString sProfileID )
{
// delete all files in profile dir
CString sProfileDir = PROFILES_DIR + sProfileID;
CStringArray asFilesToDelete;
GetDirListing( sProfileDir + SLASH "*", asFilesToDelete, false, true );
for( unsigned i=0; i<asFilesToDelete.size(); i++ )
remove( asFilesToDelete[i] );
// remove profile dir
// FIXME for non Win32 platforms
int ret = rmdir( sProfileDir );
FlushDirCache();
if( ret != 0 )
return false;
else
return true;
}