2003-09-08 03:26:58 +00:00
|
|
|
#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"
|
2003-09-08 03:26:58 +00:00
|
|
|
|
|
|
|
|
ProfileManager* PROFILEMAN = NULL; // global and accessable from anywhere in our program
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define PROFILE_FILE "Profile.ini"
|
|
|
|
|
|
2003-10-14 17:06:30 +00:00
|
|
|
#define STEPS_MEM_CARD_DATA_FILE "StepsMemCardData.dat"
|
|
|
|
|
#define COURSE_MEM_CARD_DATA_FILE "CourseMemCardData.dat"
|
2003-09-08 03:26:58 +00:00
|
|
|
|
|
|
|
|
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-09-08 03:26:58 +00:00
|
|
|
{
|
2003-11-01 19:36:52 +00:00
|
|
|
GetDirListing( PROFILES_DIR "*", asProfileIDsOut, true, false );
|
2003-09-08 03:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
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 19:36:52 +00:00
|
|
|
bool ProfileManager::LoadDefaultProfileFromMachine( PlayerNumber pn )
|
2003-09-08 03:26:58 +00:00
|
|
|
{
|
2003-11-01 19:36:52 +00:00
|
|
|
m_bUsingMemoryCard[pn] = false;
|
|
|
|
|
CString sProfileID = PREFSMAN->m_sDefaultProfile[pn];
|
|
|
|
|
if( sProfileID.empty() )
|
2003-09-09 09:15:02 +00:00
|
|
|
{
|
|
|
|
|
m_sProfileDir[pn] = "";
|
2003-11-01 19:36:52 +00:00
|
|
|
return false;
|
2003-09-09 09:15:02 +00:00
|
|
|
}
|
2003-09-08 03:26:58 +00:00
|
|
|
|
2003-11-01 19:36:52 +00:00
|
|
|
m_sProfileDir[pn] = PROFILES_DIR + sProfileID + SLASH;
|
|
|
|
|
|
|
|
|
|
bool bResult = m_Profile[pn].LoadFromIni( m_sProfileDir[pn]+PROFILE_FILE );
|
|
|
|
|
if( !bResult )
|
2003-09-08 03:26:58 +00:00
|
|
|
{
|
2003-11-01 19:36:52 +00:00
|
|
|
LOG->Warn( "Default profile '%s' does not exist", sProfileID.c_str() );
|
|
|
|
|
UnloadProfile( pn );
|
|
|
|
|
return false;
|
2003-09-08 03:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
2003-09-08 07:21:41 +00:00
|
|
|
//
|
|
|
|
|
// Load scores into SONGMAN
|
|
|
|
|
//
|
2003-10-14 17:06:30 +00:00
|
|
|
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-09-08 03:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
2003-11-01 19:36:52 +00:00
|
|
|
bool ProfileManager::SaveProfile( PlayerNumber pn )
|
2003-09-08 03:26:58 +00:00
|
|
|
{
|
|
|
|
|
if( m_sProfileDir[pn].empty() )
|
2003-11-01 19:36:52 +00:00
|
|
|
return false;
|
2003-09-08 03:26:58 +00:00
|
|
|
|
2003-11-01 19:36:52 +00:00
|
|
|
m_Profile[pn].SaveToIni( m_sProfileDir[pn]+PROFILE_FILE );
|
2003-09-08 03:26:58 +00:00
|
|
|
|
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
|
2003-10-14 17:06:30 +00:00
|
|
|
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-09-14 20:34:33 +00:00
|
|
|
|
2003-11-01 19:36:52 +00:00
|
|
|
void ProfileManager::UnloadProfile( PlayerNumber pn )
|
|
|
|
|
{
|
|
|
|
|
m_sProfileDir[pn] = "";
|
|
|
|
|
m_bUsingMemoryCard[pn] = false;
|
2003-09-14 20:34:33 +00:00
|
|
|
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 03:26:58 +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 19:36:52 +00:00
|
|
|
m_sName = "NewProfile";
|
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 );
|
2003-09-08 03:26:58 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// 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;
|
|
|
|
|
for( int 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-09-08 07:21:41 +00:00
|
|
|
|
2003-11-01 19:36:52 +00:00
|
|
|
CreateDirectories( sProfileDir );
|
2003-09-08 07:21:41 +00:00
|
|
|
|
2003-11-01 19:36:52 +00:00
|
|
|
sProfileDir += SLASH;
|
2003-09-08 07:21:41 +00:00
|
|
|
|
|
|
|
|
Profile pro;
|
2003-11-01 19:36:52 +00:00
|
|
|
pro.m_sName = sName;
|
|
|
|
|
pro.SaveToIni( sProfileDir + PROFILE_FILE );
|
2003-09-08 07:21:41 +00:00
|
|
|
|
|
|
|
|
FlushDirCache();
|
2003-11-01 19:36:52 +00:00
|
|
|
return true;
|
|
|
|
|
// TODO: Handle error cases
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-09-08 03:26:58 +00:00
|
|
|
}
|
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;
|
|
|
|
|
}
|