From c5fa8517dd8f3ee0ada16189881b4536d8058724 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Sun, 14 Aug 2005 01:59:46 +0000 Subject: [PATCH] add FixedProfiles metric. If on, profiles shouldn't be added or deleted - only cleared. --- stepmania/src/ProfileManager.cpp | 164 ++++++++++++++++++++----------- stepmania/src/ProfileManager.h | 9 +- 2 files changed, 108 insertions(+), 65 deletions(-) diff --git a/stepmania/src/ProfileManager.cpp b/stepmania/src/ProfileManager.cpp index 46b58ab98c..4ce2081205 100644 --- a/stepmania/src/ProfileManager.cpp +++ b/stepmania/src/ProfileManager.cpp @@ -40,7 +40,17 @@ static Preference g_sMemoryCardProfileImportSubdirs( "MemoryCardProfile static CString LocalProfileIdToDir( const CString &sProfileID ) { return USER_PROFILES_DIR + sProfileID + "/"; } static CString LocalProfileDirToId( const CString &sDir ) { return Basename( sDir ); } -static map g_mapLocalProfileIdToProfile; +struct DirAndProfile +{ + CString sDir; + Profile profile; +}; +static vector g_vLocalProfile; + + +static ThemeMetric FIXED_PROFILES( "ProfileManager", "FixedProfiles" ); +static ThemeMetric NUM_FIXED_PROFILES( "ProfileManager", "NumFixedProfiles" ); +#define FIXED_PROFILE_CHARACTER_ID( i ) THEME->GetMetric( "ProfileManager", ssprintf("FixedProfileCharacterID%d",int(i+1)) ) ProfileManager::ProfileManager() @@ -69,6 +79,33 @@ void ProfileManager::Init() LoadMachineProfile(); 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(); iGetCharacterFromID( sCharacterID ); + CString sThrowAway; + CreateLocalProfile( pCharacter->GetDisplayName(), sThrowAway ); + } + + // apply fixed characters + for( int i=0; iGetMetric( "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 ) @@ -134,8 +171,7 @@ bool ProfileManager::LoadLocalProfileFromMachine( PlayerNumber pn ) m_bWasLoadedFromMemoryCard[pn] = false; m_bLastLoadWasFromLastGood[pn] = false; - map::iterator iter = g_mapLocalProfileIdToProfile.find( sProfileID ); - if( iter == g_mapLocalProfileIdToProfile.end() ) + if( GetLocalProfile(sProfileID) == NULL ) { m_sProfileDir[pn] = ""; return false; @@ -268,9 +304,10 @@ bool ProfileManager::SaveProfile( PlayerNumber pn ) const bool ProfileManager::SaveLocalProfile( CString sProfileID ) { - Profile &profile = GetLocalProfile( sProfileID ); + Profile *pProfile = GetLocalProfile( sProfileID ); + ASSERT( pProfile != NULL ); CString sDir = LocalProfileIdToDir( sProfileID ); - bool b = profile.SaveAllToDir( sDir, PREFSMAN->m_bSignProfileData ); + bool b = pProfile->SaveAllToDir( sDir, PREFSMAN->m_bSignProfileData ); return b; } @@ -301,7 +338,7 @@ const Profile* ProfileManager::GetProfile( PlayerNumber pn ) const else { 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() { - FOREACHM( CString, Profile*, g_mapLocalProfileIdToProfile, iter ) - SAFE_DELETE( iter->second ); - g_mapLocalProfileIdToProfile.clear(); + g_vLocalProfile.clear(); } void ProfileManager::RefreshLocalProfilesFromDisk() @@ -324,29 +359,26 @@ void ProfileManager::RefreshLocalProfilesFromDisk() UnloadAllLocalProfiles(); vector vsProfileID; - GetDirListing( USER_PROFILES_DIR "*", vsProfileID, true, false ); - FOREACH_CONST( CString, vsProfileID, s ) + GetDirListing( USER_PROFILES_DIR "*", vsProfileID, true, true ); + FOREACH_CONST( CString, vsProfileID, p ) { - Profile *&pProfile = g_mapLocalProfileIdToProfile[*s]; - pProfile = new Profile; - CString sProfileDir = LocalProfileIdToDir( *s ); - LOG->Trace(" '%s'", pProfile->GetDisplayNameOrHighScoreName().c_str()); - pProfile->LoadAllFromDir( sProfileDir, PREFSMAN->m_bSignProfileData ); + g_vLocalProfile.push_back( DirAndProfile() ); + DirAndProfile &dap = g_vLocalProfile.back(); + dap.sDir = *p; + dap.profile.LoadAllFromDir( *p, PREFSMAN->m_bSignProfileData ); } } -Profile &ProfileManager::GetLocalProfile( const CString &sProfileID ) +const Profile *ProfileManager::GetLocalProfile( const CString &sProfileID ) const { - map::iterator iter = g_mapLocalProfileIdToProfile.find( sProfileID ); - if( iter == g_mapLocalProfileIdToProfile.end() ) + CString sDir = LocalProfileIdToDir( sProfileID ); + FOREACH_CONST( DirAndProfile, g_vLocalProfile, dap ) { - LOG->Warn( "ProfileID '%s' doesn't exist", sProfileID.c_str() ); - static Profile s_pro; - s_pro.InitAll(); - return s_pro; + if( dap->sDir == sDir ) + return &dap->profile; } - return *iter->second; + return NULL; } bool ProfileManager::CreateLocalProfile( CString sName, CString &sProfileIDOut ) @@ -371,7 +403,7 @@ bool ProfileManager::CreateLocalProfile( CString sName, CString &sProfileIDOut ) // Profile *pProfile = new Profile; pProfile->m_sDisplayName = sName; - pProfile->m_sCharacter = GAMESTATE->GetRandomCharacter()->m_sName; + pProfile->m_sCharacterID = GAMESTATE->GetRandomCharacter()->m_sCharacterID; // // Save it to disk. @@ -392,48 +424,54 @@ bool ProfileManager::CreateLocalProfile( CString sName, CString &sProfileIDOut ) 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", pProfile->m_sDisplayName.c_str(), sProfileID.c_str()) ); - g_mapLocalProfileIdToProfile[sProfileID] = pProfile; -} - -const Profile* ProfileManager::GetLocalProfileByID( const CString &sProfileID ) const -{ - map::const_iterator iter = g_mapLocalProfileIdToProfile.find( sProfileID ); - ASSERT( iter != g_mapLocalProfileIdToProfile.end() ); - return iter->second; + // insert + g_vLocalProfile.push_back( DirAndProfile() ); + DirAndProfile &dap = g_vLocalProfile.back(); + dap.sDir = LocalProfileIdToDir( sProfileID ); + dap.profile = *pProfile; } bool ProfileManager::RenameLocalProfile( CString sProfileID, CString sNewName ) { ASSERT( !sProfileID.empty() ); - Profile &pro = ProfileManager::GetLocalProfile( sProfileID ); - pro.m_sDisplayName = sNewName; + Profile *pProfile = ProfileManager::GetLocalProfile( sProfileID ); + ASSERT( pProfile ); + pProfile->m_sDisplayName = sNewName; CString sProfileDir = LocalProfileIdToDir( sProfileID ); - return pro.SaveAllToDir( sProfileDir, PREFSMAN->m_bSignProfileData ); + return pProfile->SaveAllToDir( sProfileDir, PREFSMAN->m_bSignProfileData ); } bool ProfileManager::DeleteLocalProfile( CString sProfileID ) { - map::iterator it = g_mapLocalProfileIdToProfile.find( sProfileID ); - if( it == g_mapLocalProfileIdToProfile.end() ) + Profile *pProfile = ProfileManager::GetLocalProfile( sProfileID ); + ASSERT( pProfile ); + CString sProfileDir = LocalProfileIdToDir( sProfileID ); + + FOREACH( DirAndProfile, g_vLocalProfile, i ) { - LOG->Warn( "DeleteLocalProfile: ProfileID '%s' doesn't exist", sProfileID.c_str() ); - return false; + if( i->sDir == sProfileDir ) + { + if( DeleteRecursive(sProfileDir) ) + { + g_vLocalProfile.erase( i ); + return true; + } + else + { + return false; + } + } } - CString sProfileDir = LocalProfileIdToDir( sProfileID ); - if( !DeleteRecursive(sProfileDir) ) - return false; - - delete it->second; - g_mapLocalProfileIdToProfile.erase( it ); - - return true; + LOG->Warn( "DeleteLocalProfile: ProfileID '%s' doesn't exist", sProfileID.c_str() ); + return false; } void ProfileManager::SaveMachineProfile() const @@ -700,9 +738,9 @@ bool ProfileManager::IsPersistentProfile( ProfileSlot slot ) const void ProfileManager::GetLocalProfileIDs( vector &vsProfileIDsOut ) const { 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 ); } } @@ -710,26 +748,24 @@ void ProfileManager::GetLocalProfileIDs( vector &vsProfileIDsOut ) cons void ProfileManager::GetLocalProfileDisplayNames( vector &vsProfileDisplayNamesOut ) const { vsProfileDisplayNamesOut.clear(); - FOREACHM_CONST( CString, Profile*, g_mapLocalProfileIdToProfile, iter ) - vsProfileDisplayNamesOut.push_back( iter->second->m_sDisplayName ); + FOREACH_CONST( DirAndProfile, g_vLocalProfile, i) + vsProfileDisplayNamesOut.push_back( i->profile.m_sDisplayName ); } int ProfileManager::GetLocalProfileIndex( CString sProfileID ) const { - int iIndex = 0; CString sDir = LocalProfileIdToDir( sProfileID ); - FOREACHM_CONST( CString, Profile*, g_mapLocalProfileIdToProfile, iter ) + FOREACH_CONST( DirAndProfile, g_vLocalProfile, i ) { - if( iter->first == sProfileID ) - return iIndex; - iIndex++; + if( i->sDir == sProfileID ) + return i - g_vLocalProfile.begin(); } return -1; } 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 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 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 GetNumLocalProfiles( T* p, lua_State *L ) { lua_pushnumber(L, p->GetNumLocalProfiles() ); return 1; } diff --git a/stepmania/src/ProfileManager.h b/stepmania/src/ProfileManager.h index 3946c39813..0cc6d4d990 100644 --- a/stepmania/src/ProfileManager.h +++ b/stepmania/src/ProfileManager.h @@ -16,8 +16,6 @@ class Trail; struct HighScore; struct lua_State; -const int MAX_NUM_LOCAL_PROFILES = 8; - class ProfileManager { public: @@ -26,10 +24,13 @@ public: void Init(); + bool FixedProfiles() const; // If true, profiles shouldn't be added/deleted + // local profiles void UnloadAllLocalProfiles(); 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 ); 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); } const Profile* GetProfile( ProfileSlot slot ) const; 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 GetProfileDirImportedFrom( ProfileSlot slot ) const;