add FixedProfiles metric. If on, profiles shouldn't be added or deleted - only cleared.

This commit is contained in:
Chris Danford
2005-08-14 01:59:46 +00:00
parent 055201ac76
commit c5fa8517dd
2 changed files with 108 additions and 65 deletions
+104 -60
View File
@@ -40,7 +40,17 @@ static Preference<CString> g_sMemoryCardProfileImportSubdirs( "MemoryCardProfile
static CString LocalProfileIdToDir( const CString &sProfileID ) { return USER_PROFILES_DIR + sProfileID + "/"; } static CString LocalProfileIdToDir( const CString &sProfileID ) { return USER_PROFILES_DIR + sProfileID + "/"; }
static CString LocalProfileDirToId( const CString &sDir ) { return Basename( sDir ); } static CString LocalProfileDirToId( const CString &sDir ) { return Basename( sDir ); }
static map<CString,Profile*> g_mapLocalProfileIdToProfile; struct DirAndProfile
{
CString sDir;
Profile profile;
};
static vector<DirAndProfile> g_vLocalProfile;
static ThemeMetric<bool> FIXED_PROFILES( "ProfileManager", "FixedProfiles" );
static ThemeMetric<int> NUM_FIXED_PROFILES( "ProfileManager", "NumFixedProfiles" );
#define FIXED_PROFILE_CHARACTER_ID( i ) THEME->GetMetric( "ProfileManager", ssprintf("FixedProfileCharacterID%d",int(i+1)) )
ProfileManager::ProfileManager() ProfileManager::ProfileManager()
@@ -69,6 +79,33 @@ void ProfileManager::Init()
LoadMachineProfile(); LoadMachineProfile();
RefreshLocalProfilesFromDisk(); RefreshLocalProfilesFromDisk();
if( FIXED_PROFILES )
{
// resize to the fixed number
if( (int)g_vLocalProfile.size() > NUM_FIXED_PROFILES )
g_vLocalProfile.erase( g_vLocalProfile.begin()+NUM_FIXED_PROFILES, g_vLocalProfile.end() );
for( int i=g_vLocalProfile.size(); i<NUM_FIXED_PROFILES; i++ )
{
CString sCharacterID = FIXED_PROFILE_CHARACTER_ID( i );
Character *pCharacter = GAMESTATE->GetCharacterFromID( sCharacterID );
CString sThrowAway;
CreateLocalProfile( pCharacter->GetDisplayName(), sThrowAway );
}
// apply fixed characters
for( int i=0; i<NUM_FIXED_PROFILES; i++ )
{
CString sCharacterID = THEME->GetMetric( "ProfileManager", ssprintf("FixedProfileCharacterID%d",int(i+1)) );
g_vLocalProfile[i].profile.m_sCharacterID = sCharacterID;
}
}
}
bool ProfileManager::FixedProfiles() const
{
return FIXED_PROFILES;
} }
ProfileLoadResult ProfileManager::LoadProfile( PlayerNumber pn, CString sProfileDir, bool bIsMemCard ) ProfileLoadResult ProfileManager::LoadProfile( PlayerNumber pn, CString sProfileDir, bool bIsMemCard )
@@ -134,8 +171,7 @@ bool ProfileManager::LoadLocalProfileFromMachine( PlayerNumber pn )
m_bWasLoadedFromMemoryCard[pn] = false; m_bWasLoadedFromMemoryCard[pn] = false;
m_bLastLoadWasFromLastGood[pn] = false; m_bLastLoadWasFromLastGood[pn] = false;
map<CString,Profile*>::iterator iter = g_mapLocalProfileIdToProfile.find( sProfileID ); if( GetLocalProfile(sProfileID) == NULL )
if( iter == g_mapLocalProfileIdToProfile.end() )
{ {
m_sProfileDir[pn] = ""; m_sProfileDir[pn] = "";
return false; return false;
@@ -268,9 +304,10 @@ bool ProfileManager::SaveProfile( PlayerNumber pn ) const
bool ProfileManager::SaveLocalProfile( CString sProfileID ) bool ProfileManager::SaveLocalProfile( CString sProfileID )
{ {
Profile &profile = GetLocalProfile( sProfileID ); Profile *pProfile = GetLocalProfile( sProfileID );
ASSERT( pProfile != NULL );
CString sDir = LocalProfileIdToDir( sProfileID ); CString sDir = LocalProfileIdToDir( sProfileID );
bool b = profile.SaveAllToDir( sDir, PREFSMAN->m_bSignProfileData ); bool b = pProfile->SaveAllToDir( sDir, PREFSMAN->m_bSignProfileData );
return b; return b;
} }
@@ -301,7 +338,7 @@ const Profile* ProfileManager::GetProfile( PlayerNumber pn ) const
else else
{ {
CString sProfileID = LocalProfileDirToId( m_sProfileDir[pn] ); CString sProfileID = LocalProfileDirToId( m_sProfileDir[pn] );
return GetLocalProfileByID( sProfileID ); return GetLocalProfile( sProfileID );
} }
} }
@@ -314,9 +351,7 @@ CString ProfileManager::GetPlayerName( PlayerNumber pn ) const
void ProfileManager::UnloadAllLocalProfiles() void ProfileManager::UnloadAllLocalProfiles()
{ {
FOREACHM( CString, Profile*, g_mapLocalProfileIdToProfile, iter ) g_vLocalProfile.clear();
SAFE_DELETE( iter->second );
g_mapLocalProfileIdToProfile.clear();
} }
void ProfileManager::RefreshLocalProfilesFromDisk() void ProfileManager::RefreshLocalProfilesFromDisk()
@@ -324,29 +359,26 @@ void ProfileManager::RefreshLocalProfilesFromDisk()
UnloadAllLocalProfiles(); UnloadAllLocalProfiles();
vector<CString> vsProfileID; vector<CString> vsProfileID;
GetDirListing( USER_PROFILES_DIR "*", vsProfileID, true, false ); GetDirListing( USER_PROFILES_DIR "*", vsProfileID, true, true );
FOREACH_CONST( CString, vsProfileID, s ) FOREACH_CONST( CString, vsProfileID, p )
{ {
Profile *&pProfile = g_mapLocalProfileIdToProfile[*s]; g_vLocalProfile.push_back( DirAndProfile() );
pProfile = new Profile; DirAndProfile &dap = g_vLocalProfile.back();
CString sProfileDir = LocalProfileIdToDir( *s ); dap.sDir = *p;
LOG->Trace(" '%s'", pProfile->GetDisplayNameOrHighScoreName().c_str()); dap.profile.LoadAllFromDir( *p, PREFSMAN->m_bSignProfileData );
pProfile->LoadAllFromDir( sProfileDir, PREFSMAN->m_bSignProfileData );
} }
} }
Profile &ProfileManager::GetLocalProfile( const CString &sProfileID ) const Profile *ProfileManager::GetLocalProfile( const CString &sProfileID ) const
{ {
map<CString,Profile*>::iterator iter = g_mapLocalProfileIdToProfile.find( sProfileID ); CString sDir = LocalProfileIdToDir( sProfileID );
if( iter == g_mapLocalProfileIdToProfile.end() ) FOREACH_CONST( DirAndProfile, g_vLocalProfile, dap )
{ {
LOG->Warn( "ProfileID '%s' doesn't exist", sProfileID.c_str() ); if( dap->sDir == sDir )
static Profile s_pro; return &dap->profile;
s_pro.InitAll();
return s_pro;
} }
return *iter->second; return NULL;
} }
bool ProfileManager::CreateLocalProfile( CString sName, CString &sProfileIDOut ) bool ProfileManager::CreateLocalProfile( CString sName, CString &sProfileIDOut )
@@ -371,7 +403,7 @@ bool ProfileManager::CreateLocalProfile( CString sName, CString &sProfileIDOut )
// //
Profile *pProfile = new Profile; Profile *pProfile = new Profile;
pProfile->m_sDisplayName = sName; pProfile->m_sDisplayName = sName;
pProfile->m_sCharacter = GAMESTATE->GetRandomCharacter()->m_sName; pProfile->m_sCharacterID = GAMESTATE->GetRandomCharacter()->m_sCharacterID;
// //
// Save it to disk. // Save it to disk.
@@ -392,48 +424,54 @@ bool ProfileManager::CreateLocalProfile( CString sName, CString &sProfileIDOut )
void ProfileManager::AddLocalProfileByID( Profile *pProfile, CString sProfileID ) void ProfileManager::AddLocalProfileByID( Profile *pProfile, CString sProfileID )
{ {
ASSERT_M( g_mapLocalProfileIdToProfile.find(sProfileID) == g_mapLocalProfileIdToProfile.end(), // make sure this id doesn't already exist
ASSERT_M( GetLocalProfile(sProfileID) == NULL,
ssprintf("creating \"%s\" \"%s\" that already exists", ssprintf("creating \"%s\" \"%s\" that already exists",
pProfile->m_sDisplayName.c_str(), sProfileID.c_str()) ); pProfile->m_sDisplayName.c_str(), sProfileID.c_str()) );
g_mapLocalProfileIdToProfile[sProfileID] = pProfile; // insert
} g_vLocalProfile.push_back( DirAndProfile() );
DirAndProfile &dap = g_vLocalProfile.back();
const Profile* ProfileManager::GetLocalProfileByID( const CString &sProfileID ) const dap.sDir = LocalProfileIdToDir( sProfileID );
{ dap.profile = *pProfile;
map<CString,Profile*>::const_iterator iter = g_mapLocalProfileIdToProfile.find( sProfileID );
ASSERT( iter != g_mapLocalProfileIdToProfile.end() );
return iter->second;
} }
bool ProfileManager::RenameLocalProfile( CString sProfileID, CString sNewName ) bool ProfileManager::RenameLocalProfile( CString sProfileID, CString sNewName )
{ {
ASSERT( !sProfileID.empty() ); ASSERT( !sProfileID.empty() );
Profile &pro = ProfileManager::GetLocalProfile( sProfileID ); Profile *pProfile = ProfileManager::GetLocalProfile( sProfileID );
pro.m_sDisplayName = sNewName; ASSERT( pProfile );
pProfile->m_sDisplayName = sNewName;
CString sProfileDir = LocalProfileIdToDir( sProfileID ); CString sProfileDir = LocalProfileIdToDir( sProfileID );
return pro.SaveAllToDir( sProfileDir, PREFSMAN->m_bSignProfileData ); return pProfile->SaveAllToDir( sProfileDir, PREFSMAN->m_bSignProfileData );
} }
bool ProfileManager::DeleteLocalProfile( CString sProfileID ) bool ProfileManager::DeleteLocalProfile( CString sProfileID )
{ {
map<CString,Profile*>::iterator it = g_mapLocalProfileIdToProfile.find( sProfileID ); Profile *pProfile = ProfileManager::GetLocalProfile( sProfileID );
if( it == g_mapLocalProfileIdToProfile.end() ) ASSERT( pProfile );
CString sProfileDir = LocalProfileIdToDir( sProfileID );
FOREACH( DirAndProfile, g_vLocalProfile, i )
{ {
LOG->Warn( "DeleteLocalProfile: ProfileID '%s' doesn't exist", sProfileID.c_str() ); if( i->sDir == sProfileDir )
return false; {
if( DeleteRecursive(sProfileDir) )
{
g_vLocalProfile.erase( i );
return true;
}
else
{
return false;
}
}
} }
CString sProfileDir = LocalProfileIdToDir( sProfileID ); LOG->Warn( "DeleteLocalProfile: ProfileID '%s' doesn't exist", sProfileID.c_str() );
if( !DeleteRecursive(sProfileDir) ) return false;
return false;
delete it->second;
g_mapLocalProfileIdToProfile.erase( it );
return true;
} }
void ProfileManager::SaveMachineProfile() const void ProfileManager::SaveMachineProfile() const
@@ -700,9 +738,9 @@ bool ProfileManager::IsPersistentProfile( ProfileSlot slot ) const
void ProfileManager::GetLocalProfileIDs( vector<CString> &vsProfileIDsOut ) const void ProfileManager::GetLocalProfileIDs( vector<CString> &vsProfileIDsOut ) const
{ {
vsProfileIDsOut.clear(); vsProfileIDsOut.clear();
FOREACHM_CONST( CString, Profile*, g_mapLocalProfileIdToProfile, iter ) FOREACH_CONST( DirAndProfile, g_vLocalProfile, i)
{ {
CString sID = LocalProfileDirToId( iter->first ); CString sID = LocalProfileDirToId( i->sDir );
vsProfileIDsOut.push_back( sID ); vsProfileIDsOut.push_back( sID );
} }
} }
@@ -710,26 +748,24 @@ void ProfileManager::GetLocalProfileIDs( vector<CString> &vsProfileIDsOut ) cons
void ProfileManager::GetLocalProfileDisplayNames( vector<CString> &vsProfileDisplayNamesOut ) const void ProfileManager::GetLocalProfileDisplayNames( vector<CString> &vsProfileDisplayNamesOut ) const
{ {
vsProfileDisplayNamesOut.clear(); vsProfileDisplayNamesOut.clear();
FOREACHM_CONST( CString, Profile*, g_mapLocalProfileIdToProfile, iter ) FOREACH_CONST( DirAndProfile, g_vLocalProfile, i)
vsProfileDisplayNamesOut.push_back( iter->second->m_sDisplayName ); vsProfileDisplayNamesOut.push_back( i->profile.m_sDisplayName );
} }
int ProfileManager::GetLocalProfileIndex( CString sProfileID ) const int ProfileManager::GetLocalProfileIndex( CString sProfileID ) const
{ {
int iIndex = 0;
CString sDir = LocalProfileIdToDir( sProfileID ); CString sDir = LocalProfileIdToDir( sProfileID );
FOREACHM_CONST( CString, Profile*, g_mapLocalProfileIdToProfile, iter ) FOREACH_CONST( DirAndProfile, g_vLocalProfile, i )
{ {
if( iter->first == sProfileID ) if( i->sDir == sProfileID )
return iIndex; return i - g_vLocalProfile.begin();
iIndex++;
} }
return -1; return -1;
} }
int ProfileManager::GetNumLocalProfiles() const int ProfileManager::GetNumLocalProfiles() const
{ {
return g_mapLocalProfileIdToProfile.size(); return g_vLocalProfile.size();
} }
@@ -745,7 +781,15 @@ public:
static int GetProfile( T* p, lua_State *L ) { PlayerNumber pn = (PlayerNumber)IArg(1); Profile* pP = p->GetProfile(pn); ASSERT(pP); pP->PushSelf(L); return 1; } static int GetProfile( T* p, lua_State *L ) { PlayerNumber pn = (PlayerNumber)IArg(1); Profile* pP = p->GetProfile(pn); ASSERT(pP); pP->PushSelf(L); return 1; }
static int GetMachineProfile( T* p, lua_State *L ) { p->GetMachineProfile()->PushSelf(L); return 1; } static int GetMachineProfile( T* p, lua_State *L ) { p->GetMachineProfile()->PushSelf(L); return 1; }
static int SaveMachineProfile( T* p, lua_State *L ) { p->SaveMachineProfile(); return 1; } static int SaveMachineProfile( T* p, lua_State *L ) { p->SaveMachineProfile(); return 1; }
static int GetLocalProfile( T* p, lua_State *L ) { Profile &pro = p->GetLocalProfile(SArg(1)); pro.PushSelf(L); return 1; } static int GetLocalProfile( T* p, lua_State *L )
{
Profile *pProfile = p->GetLocalProfile(SArg(1));
if( pProfile )
pProfile->PushSelf(L);
else
lua_pushnil(L );
return 1;
}
static int GetLocalProfileIndex( T* p, lua_State *L ) { lua_pushnumber(L, p->GetLocalProfileIndex(SArg(1)) ); return 1; } static int GetLocalProfileIndex( T* p, lua_State *L ) { lua_pushnumber(L, p->GetLocalProfileIndex(SArg(1)) ); return 1; }
static int GetNumLocalProfiles( T* p, lua_State *L ) { lua_pushnumber(L, p->GetNumLocalProfiles() ); return 1; } static int GetNumLocalProfiles( T* p, lua_State *L ) { lua_pushnumber(L, p->GetNumLocalProfiles() ); return 1; }
+4 -5
View File
@@ -16,8 +16,6 @@ class Trail;
struct HighScore; struct HighScore;
struct lua_State; struct lua_State;
const int MAX_NUM_LOCAL_PROFILES = 8;
class ProfileManager class ProfileManager
{ {
public: public:
@@ -26,10 +24,13 @@ public:
void Init(); void Init();
bool FixedProfiles() const; // If true, profiles shouldn't be added/deleted
// local profiles // local profiles
void UnloadAllLocalProfiles(); void UnloadAllLocalProfiles();
void RefreshLocalProfilesFromDisk(); void RefreshLocalProfilesFromDisk();
Profile &GetLocalProfile( const CString &sProfileID ); const Profile *GetLocalProfile( const CString &sProfileID ) const;
Profile *GetLocalProfile( const CString &sProfileID ) { return (Profile*) ((const ProfileManager *) this)->GetLocalProfile(sProfileID); }
bool CreateLocalProfile( CString sName, CString &sProfileIDOut ); bool CreateLocalProfile( CString sName, CString &sProfileIDOut );
void AddLocalProfileByID( Profile *pProfile, CString sProfileID ); // transfers ownership of pProfile void AddLocalProfileByID( Profile *pProfile, CString sProfileID ); // transfers ownership of pProfile
@@ -71,8 +72,6 @@ public:
Profile* GetProfile( PlayerNumber pn ) { return (Profile*) ((const ProfileManager *) this)->GetProfile(pn); } Profile* GetProfile( PlayerNumber pn ) { return (Profile*) ((const ProfileManager *) this)->GetProfile(pn); }
const Profile* GetProfile( ProfileSlot slot ) const; const Profile* GetProfile( ProfileSlot slot ) const;
Profile* GetProfile( ProfileSlot slot ) { return (Profile*) ((const ProfileManager *) this)->GetProfile(slot); } Profile* GetProfile( ProfileSlot slot ) { return (Profile*) ((const ProfileManager *) this)->GetProfile(slot); }
const Profile* GetLocalProfileByID( const CString &sProfileID ) const;
Profile* GetLocalProfileByID( const CString &sProfileID ) { return (Profile*) ((const ProfileManager *) this)->GetLocalProfileByID(sProfileID); }
CString GetProfileDir( ProfileSlot slot ) const; CString GetProfileDir( ProfileSlot slot ) const;
CString GetProfileDirImportedFrom( ProfileSlot slot ) const; CString GetProfileDirImportedFrom( ProfileSlot slot ) const;