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-03-12 22:19:19 +01:00
parent eb0ab83d89
commit d8b6d36a78
6 changed files with 55 additions and 23 deletions
+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) );
}
}