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:
@@ -42,14 +42,14 @@ static Preference<bool> g_bMemoryCards( "MemoryCards", false );
|
|||||||
static Preference<bool> g_bMemoryCardProfiles( "MemoryCardProfiles", true );
|
static Preference<bool> g_bMemoryCardProfiles( "MemoryCardProfiles", true );
|
||||||
|
|
||||||
// if set, always use the device that mounts to this point
|
// 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
|
// Look for this level, bus, port when assigning cards. -1 = match any
|
||||||
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbBus( MemoryCardUsbBusInit, NUM_PLAYERS );
|
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbBus( MemoryCardUsbBusInit, NUM_PLAYERS, PreferenceType::Immutable );
|
||||||
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbPort( MemoryCardUsbPortInit, NUM_PLAYERS );
|
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbPort( MemoryCardUsbPortInit, NUM_PLAYERS, PreferenceType::Immutable );
|
||||||
Preference1D<int> MemoryCardManager::m_iMemoryCardUsbLevel( MemoryCardUsbLevelInit, NUM_PLAYERS );
|
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] =
|
const RString MEM_CARD_MOUNT_POINT[NUM_PLAYERS] =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,8 +17,8 @@
|
|||||||
|
|
||||||
NetworkManager* NETWORK = nullptr; // global and accessible from anywhere in our program
|
NetworkManager* NETWORK = nullptr; // global and accessible from anywhere in our program
|
||||||
|
|
||||||
Preference<bool> NetworkManager::httpEnabled("HttpEnabled", false);
|
Preference<bool> NetworkManager::httpEnabled("HttpEnabled", false, nullptr, PreferenceType::Immutable);
|
||||||
Preference<RString> NetworkManager::httpAllowHosts("HttpAllowHosts", "");
|
Preference<RString> NetworkManager::httpAllowHosts("HttpAllowHosts", "", nullptr, PreferenceType::Immutable);
|
||||||
|
|
||||||
static const char *HttpErrorCodeNames[] = {
|
static const char *HttpErrorCodeNames[] = {
|
||||||
"Blocked",
|
"Blocked",
|
||||||
|
|||||||
+8
-4
@@ -9,9 +9,10 @@
|
|||||||
|
|
||||||
static SubscriptionManager<IPreference> m_Subscribers;
|
static SubscriptionManager<IPreference> m_Subscribers;
|
||||||
|
|
||||||
IPreference::IPreference( const RString& sName ):
|
IPreference::IPreference( const RString& sName, PreferenceType type ):
|
||||||
m_sName( sName ),
|
m_sName( sName ),
|
||||||
m_bIsStatic( false )
|
m_bDoNotWrite( type == PreferenceType::Deprecated ),
|
||||||
|
m_bImmutable( type == PreferenceType::Immutable )
|
||||||
{
|
{
|
||||||
m_Subscribers.Subscribe( this );
|
m_Subscribers.Subscribe( this );
|
||||||
}
|
}
|
||||||
@@ -81,13 +82,16 @@ void IPreference::ReadFrom( const XNode* pNode, bool bIsStatic )
|
|||||||
if( pNode->GetAttrValue(m_sName, sVal) )
|
if( pNode->GetAttrValue(m_sName, sVal) )
|
||||||
{
|
{
|
||||||
FromString( sVal );
|
FromString( sVal );
|
||||||
m_bIsStatic = bIsStatic;
|
if (bIsStatic)
|
||||||
|
m_bDoNotWrite = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IPreference::WriteTo( XNode* pNode ) const
|
void IPreference::WriteTo( XNode* pNode ) const
|
||||||
{
|
{
|
||||||
if( !m_bIsStatic )
|
if (m_bDoNotWrite)
|
||||||
|
return;
|
||||||
|
|
||||||
pNode->AppendAttr( m_sName, ToString() );
|
pNode->AppendAttr( m_sName, ToString() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+25
-7
@@ -8,11 +8,28 @@
|
|||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
class XNode;
|
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;
|
struct lua_State;
|
||||||
class IPreference
|
class IPreference
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
IPreference( const RString& sName );
|
IPreference( const RString& sName, PreferenceType type );
|
||||||
virtual ~IPreference();
|
virtual ~IPreference();
|
||||||
void ReadFrom( const XNode* pNode, bool bIsStatic );
|
void ReadFrom( const XNode* pNode, bool bIsStatic );
|
||||||
void WriteTo( XNode* pNode ) const;
|
void WriteTo( XNode* pNode ) const;
|
||||||
@@ -36,10 +53,11 @@ public:
|
|||||||
static void ReadAllDefaultsFromNode( const XNode* pNode );
|
static void ReadAllDefaultsFromNode( const XNode* pNode );
|
||||||
|
|
||||||
RString GetName() { return m_sName; }
|
RString GetName() { return m_sName; }
|
||||||
void SetStatic( bool b ) { m_bIsStatic = b; }
|
bool IsImmutable() { return m_bImmutable; }
|
||||||
private:
|
private:
|
||||||
RString m_sName;
|
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 );
|
void BroadcastPreferenceChanged( const RString& sPreferenceName );
|
||||||
@@ -48,8 +66,8 @@ template <class T>
|
|||||||
class Preference : public IPreference
|
class Preference : public IPreference
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Preference( const RString& sName, const T& defaultValue, void (pfnValidate)(T& val) = nullptr ):
|
Preference( const RString& sName, const T& defaultValue, void (pfnValidate)(T& val) = nullptr, PreferenceType type = PreferenceType::Mutable ):
|
||||||
IPreference( sName ),
|
IPreference( sName, type ),
|
||||||
m_currentValue( defaultValue ),
|
m_currentValue( defaultValue ),
|
||||||
m_defaultValue( defaultValue ),
|
m_defaultValue( defaultValue ),
|
||||||
m_pfnValidate( pfnValidate )
|
m_pfnValidate( pfnValidate )
|
||||||
@@ -131,14 +149,14 @@ public:
|
|||||||
typedef Preference<T> PreferenceT;
|
typedef Preference<T> PreferenceT;
|
||||||
vector<PreferenceT*> m_v;
|
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 )
|
for( size_t i=0; i<N; ++i )
|
||||||
{
|
{
|
||||||
RString sName;
|
RString sName;
|
||||||
T defaultValue;
|
T defaultValue;
|
||||||
pfn( i, sName, 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
@@ -264,9 +264,9 @@ PrefsManager::PrefsManager() :
|
|||||||
m_CourseSortOrder ( "CourseSortOrder", COURSE_SORT_SONGS ),
|
m_CourseSortOrder ( "CourseSortOrder", COURSE_SORT_SONGS ),
|
||||||
m_bSubSortByNumSteps ( "SubSortByNumSteps", false ),
|
m_bSubSortByNumSteps ( "SubSortByNumSteps", false ),
|
||||||
m_GetRankingName ( "GetRankingName", RANKING_ON ),
|
m_GetRankingName ( "GetRankingName", RANKING_ON ),
|
||||||
m_sAdditionalSongFolders ( "AdditionalSongFolders", "" ),
|
m_sAdditionalSongFolders ( "AdditionalSongFolders", "", nullptr, PreferenceType::Immutable ),
|
||||||
m_sAdditionalCourseFolders ( "AdditionalCourseFolders", "" ),
|
m_sAdditionalCourseFolders ( "AdditionalCourseFolders", "", nullptr, PreferenceType::Immutable ),
|
||||||
m_sAdditionalFolders ( "AdditionalFolders", "" ),
|
m_sAdditionalFolders ( "AdditionalFolders", "", nullptr, PreferenceType::Immutable ),
|
||||||
m_sDefaultTheme ( "DefaultTheme", "default" ),
|
m_sDefaultTheme ( "DefaultTheme", "default" ),
|
||||||
m_sLastSeenVideoDriver ( "LastSeenVideoDriver", "" ),
|
m_sLastSeenVideoDriver ( "LastSeenVideoDriver", "" ),
|
||||||
m_sVideoRenderers ( "VideoRenderers", "" ), // StepMania.cpp sets these on first run:
|
m_sVideoRenderers ( "VideoRenderers", "" ), // StepMania.cpp sets these on first run:
|
||||||
@@ -574,6 +574,11 @@ public:
|
|||||||
LuaHelpers::ReportScriptErrorFmt( "SetPreference: unknown preference \"%s\"", sName.c_str() );
|
LuaHelpers::ReportScriptErrorFmt( "SetPreference: unknown preference \"%s\"", sName.c_str() );
|
||||||
COMMON_RETURN_SELF;
|
COMMON_RETURN_SELF;
|
||||||
}
|
}
|
||||||
|
else if (pPref->IsImmutable())
|
||||||
|
{
|
||||||
|
LuaHelpers::ReportScriptErrorFmt( "SetPreference: preference \"%s\" is immutable", sName.c_str() );
|
||||||
|
COMMON_RETURN_SELF;
|
||||||
|
}
|
||||||
|
|
||||||
lua_pushvalue( L, 2 );
|
lua_pushvalue( L, 2 );
|
||||||
pPref->SetFromStack( L );
|
pPref->SetFromStack( L );
|
||||||
@@ -589,6 +594,11 @@ public:
|
|||||||
LuaHelpers::ReportScriptErrorFmt( "SetPreferenceToDefault: unknown preference \"%s\"", sName.c_str() );
|
LuaHelpers::ReportScriptErrorFmt( "SetPreferenceToDefault: unknown preference \"%s\"", sName.c_str() );
|
||||||
COMMON_RETURN_SELF;
|
COMMON_RETURN_SELF;
|
||||||
}
|
}
|
||||||
|
else if (pPref->IsImmutable())
|
||||||
|
{
|
||||||
|
LuaHelpers::ReportScriptErrorFmt( "SetPreference: preference \"%s\" is immutable", sName.c_str() );
|
||||||
|
COMMON_RETURN_SELF;
|
||||||
|
}
|
||||||
|
|
||||||
pPref->LoadDefault();
|
pPref->LoadDefault();
|
||||||
LOG->Trace( "Restored preference \"%s\" to default \"%s\"", sName.c_str(), pPref->ToString().c_str() );
|
LOG->Trace( "Restored preference \"%s\" to default \"%s\"", sName.c_str(), pPref->ToString().c_str() );
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ XToString(MemoryCardDriverType);
|
|||||||
StringToX(MemoryCardDriverType);
|
StringToX(MemoryCardDriverType);
|
||||||
LuaXType(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
|
bool UsbStorageDevice::operator==(const UsbStorageDevice& other) const
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user