Allow to mark preferences as immutable or deprecated

- Deprecated: The option is read from the settings, but not written back
  when saving. This can be used for migrating options to a new name or
  format. (Not used yet.)
- Immutable: The option can not be changed from Lua. Used for
  security-relevant options that should not be changable by the theme or
  mod charts. Currently that's networking and some file system access
  options.
This commit is contained in:
Martin Natano
2022-02-20 17:24:43 +01:00
parent eb0ab83d89
commit d8b6d36a78
6 changed files with 55 additions and 23 deletions
+5 -5
View File
@@ -42,14 +42,14 @@ static Preference<bool> g_bMemoryCards( "MemoryCards", false );
static Preference<bool> g_bMemoryCardProfiles( "MemoryCardProfiles", true );
// if set, always use the device that mounts to this point
Preference1D<RString> MemoryCardManager::m_sMemoryCardOsMountPoint( MemoryCardOsMountPointInit, NUM_PLAYERS );
Preference1D<RString> MemoryCardManager::m_sMemoryCardOsMountPoint( MemoryCardOsMountPointInit, NUM_PLAYERS, PreferenceType::Immutable );
// Look for this level, bus, port when assigning cards. -1 = match any
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbBus( MemoryCardUsbBusInit, NUM_PLAYERS );
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbPort( MemoryCardUsbPortInit, NUM_PLAYERS );
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbLevel( MemoryCardUsbLevelInit, NUM_PLAYERS );
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbBus( MemoryCardUsbBusInit, NUM_PLAYERS, PreferenceType::Immutable );
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbPort( MemoryCardUsbPortInit, NUM_PLAYERS, PreferenceType::Immutable );
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbLevel( MemoryCardUsbLevelInit, NUM_PLAYERS, PreferenceType::Immutable );
Preference<RString> MemoryCardManager::m_sEditorMemoryCardOsMountPoint( "EditorMemoryCardOsMountPoint", "" );
Preference<RString> MemoryCardManager::m_sEditorMemoryCardOsMountPoint( "EditorMemoryCardOsMountPoint", "", nullptr, PreferenceType::Immutable );
const RString MEM_CARD_MOUNT_POINT[NUM_PLAYERS] =
{
+2 -2
View File
@@ -17,8 +17,8 @@
NetworkManager* NETWORK = nullptr; // global and accessible from anywhere in our program
Preference<bool> NetworkManager::httpEnabled("HttpEnabled", false);
Preference<RString> NetworkManager::httpAllowHosts("HttpAllowHosts", "");
Preference<bool> NetworkManager::httpEnabled("HttpEnabled", false, nullptr, PreferenceType::Immutable);
Preference<RString> NetworkManager::httpAllowHosts("HttpAllowHosts", "", nullptr, PreferenceType::Immutable);
static const char *HttpErrorCodeNames[] = {
"Blocked",
+9 -5
View File
@@ -9,9 +9,10 @@
static SubscriptionManager<IPreference> m_Subscribers;
IPreference::IPreference( const RString& sName ):
IPreference::IPreference( const RString& sName, PreferenceType type ):
m_sName( sName ),
m_bIsStatic( false )
m_bDoNotWrite( type == PreferenceType::Deprecated ),
m_bImmutable( type == PreferenceType::Immutable )
{
m_Subscribers.Subscribe( this );
}
@@ -81,14 +82,17 @@ void IPreference::ReadFrom( const XNode* pNode, bool bIsStatic )
if( pNode->GetAttrValue(m_sName, sVal) )
{
FromString( sVal );
m_bIsStatic = bIsStatic;
if (bIsStatic)
m_bDoNotWrite = true;
}
}
void IPreference::WriteTo( XNode* pNode ) const
{
if( !m_bIsStatic )
pNode->AppendAttr( m_sName, ToString() );
if (m_bDoNotWrite)
return;
pNode->AppendAttr( m_sName, ToString() );
}
/* Load our value from the node, and make it the new default. */
+25 -7
View File
@@ -8,11 +8,28 @@
#include "RageUtil.h"
class XNode;
enum class PreferenceType
{
// Allow reading and writing of the preference through Lua.
// This is the default behavior.
Mutable,
// Mark the preference as read-only i.e. don't allow setting of the
// preference through Lua.
Immutable,
// The preference is deprecated.
// This deprecated flag will not be written to the Preferences file.
// If this preference was deprecated in favor of a new one, see
// TranslateDeprecatedFlags() to see the what it was deprecated in favor of.
Deprecated,
};
struct lua_State;
class IPreference
{
public:
IPreference( const RString& sName );
IPreference( const RString& sName, PreferenceType type );
virtual ~IPreference();
void ReadFrom( const XNode* pNode, bool bIsStatic );
void WriteTo( XNode* pNode ) const;
@@ -36,10 +53,11 @@ public:
static void ReadAllDefaultsFromNode( const XNode* pNode );
RString GetName() { return m_sName; }
void SetStatic( bool b ) { m_bIsStatic = b; }
bool IsImmutable() { return m_bImmutable; }
private:
RString m_sName;
bool m_bIsStatic; // loaded from Static.ini? If so, don't write to Preferences.ini
bool m_bDoNotWrite;
bool m_bImmutable;
};
void BroadcastPreferenceChanged( const RString& sPreferenceName );
@@ -48,8 +66,8 @@ template <class T>
class Preference : public IPreference
{
public:
Preference( const RString& sName, const T& defaultValue, void (pfnValidate)(T& val) = nullptr ):
IPreference( sName ),
Preference( const RString& sName, const T& defaultValue, void (pfnValidate)(T& val) = nullptr, PreferenceType type = PreferenceType::Mutable ):
IPreference( sName, type ),
m_currentValue( defaultValue ),
m_defaultValue( defaultValue ),
m_pfnValidate( pfnValidate )
@@ -131,14 +149,14 @@ public:
typedef Preference<T> PreferenceT;
vector<PreferenceT*> m_v;
Preference1D( void pfn(size_t i, RString &sNameOut, T &defaultValueOut ), size_t N )
Preference1D( void pfn(size_t i, RString &sNameOut, T &defaultValueOut ), size_t N, PreferenceType type = PreferenceType::Mutable )
{
for( size_t i=0; i<N; ++i )
{
RString sName;
T defaultValue;
pfn( i, sName, defaultValue );
m_v.push_back( new Preference<T>(sName, defaultValue) );
m_v.push_back( new Preference<T>(sName, defaultValue, nullptr, type) );
}
}
+13 -3
View File
@@ -264,9 +264,9 @@ PrefsManager::PrefsManager() :
m_CourseSortOrder ( "CourseSortOrder", COURSE_SORT_SONGS ),
m_bSubSortByNumSteps ( "SubSortByNumSteps", false ),
m_GetRankingName ( "GetRankingName", RANKING_ON ),
m_sAdditionalSongFolders ( "AdditionalSongFolders", "" ),
m_sAdditionalCourseFolders ( "AdditionalCourseFolders", "" ),
m_sAdditionalFolders ( "AdditionalFolders", "" ),
m_sAdditionalSongFolders ( "AdditionalSongFolders", "", nullptr, PreferenceType::Immutable ),
m_sAdditionalCourseFolders ( "AdditionalCourseFolders", "", nullptr, PreferenceType::Immutable ),
m_sAdditionalFolders ( "AdditionalFolders", "", nullptr, PreferenceType::Immutable ),
m_sDefaultTheme ( "DefaultTheme", "default" ),
m_sLastSeenVideoDriver ( "LastSeenVideoDriver", "" ),
m_sVideoRenderers ( "VideoRenderers", "" ), // StepMania.cpp sets these on first run:
@@ -574,6 +574,11 @@ public:
LuaHelpers::ReportScriptErrorFmt( "SetPreference: unknown preference \"%s\"", sName.c_str() );
COMMON_RETURN_SELF;
}
else if (pPref->IsImmutable())
{
LuaHelpers::ReportScriptErrorFmt( "SetPreference: preference \"%s\" is immutable", sName.c_str() );
COMMON_RETURN_SELF;
}
lua_pushvalue( L, 2 );
pPref->SetFromStack( L );
@@ -589,6 +594,11 @@ public:
LuaHelpers::ReportScriptErrorFmt( "SetPreferenceToDefault: unknown preference \"%s\"", sName.c_str() );
COMMON_RETURN_SELF;
}
else if (pPref->IsImmutable())
{
LuaHelpers::ReportScriptErrorFmt( "SetPreference: preference \"%s\" is immutable", sName.c_str() );
COMMON_RETURN_SELF;
}
pPref->LoadDefault();
LOG->Trace( "Restored preference \"%s\" to default \"%s\"", sName.c_str(), pPref->ToString().c_str() );
+1 -1
View File
@@ -22,7 +22,7 @@ XToString(MemoryCardDriverType);
StringToX(MemoryCardDriverType);
LuaXType(MemoryCardDriverType);
Preference<MemoryCardDriverType> g_MemoryCardDriver("MemoryCardDriver", MemoryCardDriverType_Usb);
Preference<MemoryCardDriverType> g_MemoryCardDriver("MemoryCardDriver", MemoryCardDriverType_Usb, nullptr, PreferenceType::Immutable);
bool UsbStorageDevice::operator==(const UsbStorageDevice& other) const
{