From f435983996e57587554f9ad875e3d46848f17408 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 31 Dec 2005 04:59:56 +0000 Subject: [PATCH] IPreference stores preferences, but knows nothing about loading or saving them; that's up to PrefsManager. Treat IPreference as a simple data holder, with simple facilities for looking them up by name, converting to/from strings, and storing to/from an XNode, but with no application-specific code to save to disk. This can be used alone as a way for code to configure things internally, even for uses that have no notion of storing user preferences (eg. unit tests), and other programs can use it to store preferences in entirely different ways (SMPackage could use it to store to the registry). PrefsManager is layered on top, to implement StepMania's particular use of Preference (saving and loading INIs), but isn't needed for Preference to be useful. This also makes Preference only use XNode, not the more specialized IniFile. (One piece is missing: several low-level places, eg. Dialog, want to set a preference and write it to disk immediately. The only way to do that is to have access to PREFSMAN. FIXME.) --- stepmania/src/Preference.cpp | 20 +++++++++++--------- stepmania/src/Preference.h | 10 +++++----- stepmania/src/PrefsManager.cpp | 9 ++++++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/stepmania/src/Preference.cpp b/stepmania/src/Preference.cpp index 2e360a3cd7..8afd31b339 100644 --- a/stepmania/src/Preference.cpp +++ b/stepmania/src/Preference.cpp @@ -1,6 +1,6 @@ #include "global.h" #include "Preference.h" -#include "IniFile.h" +#include "XmlFile.h" #include "RageLog.h" #include "LuaFunctions.h" #include "LuaManager.h" @@ -37,16 +37,18 @@ void IPreference::LoadAllDefaults() (*p)->LoadDefault(); } -void IPreference::ReadAllPrefsFromIni( const IniFile &ini, const CString &sSection ) +void IPreference::ReadAllPrefsFromNode( const XNode* pNode ) { + if( pNode == NULL ) + return; FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p ) - (*p)->ReadFrom( ini, sSection ); + (*p)->ReadFrom( pNode ); } -void IPreference::SavePrefsToIni( IniFile &ini ) +void IPreference::SavePrefsToNode( XNode* pNode ) { FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p ) - (*p)->WriteTo( ini ); + (*p)->WriteTo( pNode ); } void IPreference::PushValue( lua_State *L ) const @@ -87,16 +89,16 @@ READFROM_AND_WRITETO( float ) READFROM_AND_WRITETO( bool ) READFROM_AND_WRITETO( CString ) -void IPreference::ReadFrom( const IniFile &ini, const CString &sSection ) +void IPreference::ReadFrom( const XNode* pNode ) { CString sVal; - if( ini.GetValue( sSection, m_sName, sVal ) ) + if( pNode->GetAttrValue(m_sName, sVal) ) FromString( sVal ); } -void IPreference::WriteTo( IniFile &ini ) const +void IPreference::WriteTo( XNode* pNode ) const { - ini.SetValue( "Options", m_sName, ToString() ); + pNode->AppendAttr( m_sName, ToString() ); } void BroadcastPreferenceChanged( const CString& sPreferenceName ) diff --git a/stepmania/src/Preference.h b/stepmania/src/Preference.h index 3c42a7d309..dfce6b5d16 100644 --- a/stepmania/src/Preference.h +++ b/stepmania/src/Preference.h @@ -5,7 +5,7 @@ #include "EnumHelper.h" #include "RageUtil.h" -class IniFile; +class XNode; struct lua_State; class IPreference @@ -15,8 +15,8 @@ public: virtual ~IPreference(); virtual void LoadDefault() = 0; - virtual void ReadFrom( const IniFile &ini, const CString &sSection ); - virtual void WriteTo( IniFile &ini ) const; + virtual void ReadFrom( const XNode* pNode ); + virtual void WriteTo( XNode* pNode ) const; virtual CString ToString() const = 0; virtual void FromString( const CString &s ) = 0; @@ -28,8 +28,8 @@ public: static IPreference *GetPreferenceByName( const CString &sName ); static void LoadAllDefaults(); - static void ReadAllPrefsFromIni( const IniFile &ini, const CString &sSection ); - static void SavePrefsToIni( IniFile &ini ); + static void ReadAllPrefsFromNode( const XNode* pNode ); + static void SavePrefsToNode( XNode* pNode ); protected: CString m_sName; diff --git a/stepmania/src/PrefsManager.cpp b/stepmania/src/PrefsManager.cpp index df668c7132..d6f0ffef8e 100644 --- a/stepmania/src/PrefsManager.cpp +++ b/stepmania/src/PrefsManager.cpp @@ -419,7 +419,7 @@ void PrefsManager::ReadPrefsFromIni( const IniFile &ini, const CString &sSection CString sFallback; if( ini.GetValue(sSection,"Fallback",sFallback) ) { - ReadPrefsFromIni( ini, sFallback ); + IPreference::ReadAllPrefsFromNode( ini.GetChild(sFallback) ); } //IPreference *pPref = PREFSMAN->GetPreferenceByName( *sName ); @@ -430,7 +430,7 @@ void PrefsManager::ReadPrefsFromIni( const IniFile &ini, const CString &sSection // } // pPref->FromString( sVal ); - IPreference::ReadAllPrefsFromIni( ini, sSection ); + IPreference::ReadAllPrefsFromNode( ini.GetChild(sSection) ); // validate m_iSongsPerPlay.Set( clamp(m_iSongsPerPlay.Get(),0,MAX_SONGS_PER_PLAY) ); @@ -469,7 +469,10 @@ void PrefsManager::SavePrefsToIni( IniFile &ini ) if( !m_sCurrentGame.Get().empty() ) StoreGamePrefs(); - IPreference::SavePrefsToIni( ini ); + XNode* pNode = ini.GetChild( "Options" ); + if( pNode == NULL ) + pNode = ini.AppendChild( "Options" ); + IPreference::SavePrefsToNode( pNode ); FOREACHM_CONST( CString, GamePrefs, m_mapGameNameToGamePrefs, iter ) {