Etterna 0.74.4
Loading...
Searching...
No Matches
Preference.h
1/* Preference - Holds user-chosen preferences that are saved between sessions.
2 */
3
4#ifndef PREFERENCE_H
5#define PREFERENCE_H
6
7#include "EnumHelper.h"
8#include "Etterna/Singletons/LuaManager.h"
9#include "RageUtil/Utils/RageUtil.h"
10class XNode;
11
12struct lua_State;
14{
15 public:
16 IPreference(const std::string& sName);
17 virtual ~IPreference();
18 void ReadFrom(const XNode* pNode, bool bIsStatic);
19 void WriteTo(XNode* pNode) const;
20 void ReadDefaultFrom(const XNode* pNode);
21
22 virtual void LoadDefault() = 0;
23 virtual void SetDefaultFromString(const std::string& s) = 0;
24
25 [[nodiscard]] virtual auto ToString() const -> std::string = 0;
26 virtual void FromString(const std::string& s) = 0;
27
28 virtual void SetFromStack(lua_State* L);
29 virtual void PushValue(lua_State* L) const;
30
31 [[nodiscard]] auto GetName() const -> const std::string& { return m_sName; }
32
33 static auto GetPreferenceByName(const std::string& sName) -> IPreference*;
34 static void LoadAllDefaults();
35 static void ReadAllPrefsFromNode(const XNode* pNode, bool bIsStatic);
36 static void SavePrefsToNode(XNode* pNode);
37 static void ReadAllDefaultsFromNode(const XNode* pNode);
38
39 auto GetName() -> std::string { return m_sName; }
40 void SetStatic(bool b) { m_bIsStatic = b; }
41
42 private:
43 std::string m_sName;
44 bool m_bIsStatic; // loaded from Static.ini? If so, don't write to
45 // Preferences.ini
46};
47
48void
49BroadcastPreferenceChanged(const std::string& sPreferenceName);
50
51template<class T>
52class Preference : public IPreference
53{
54 public:
55 Preference(const std::string& sName,
56 const T& defaultValue,
57 void(pfnValidate)(T& val) = nullptr)
58 : IPreference(sName)
59 , m_currentValue(defaultValue)
60 , m_defaultValue(defaultValue)
61 , m_pfnValidate(pfnValidate)
62 {
63 LoadDefault();
64 }
65
66 [[nodiscard]] auto ToString() const -> std::string override
67 {
68 return StringConversion::ToString<T>(m_currentValue);
69 }
70 void FromString(const std::string& s) override
71 {
72 if (!StringConversion::FromString<T>(s, m_currentValue)) {
73 m_currentValue = m_defaultValue;
74 }
75 if (m_pfnValidate) {
76 m_pfnValidate(m_currentValue);
77 }
78 }
79 void SetFromStack(lua_State* L) override
80 {
81 LuaHelpers::Pop<T>(L, m_currentValue);
82 if (m_pfnValidate) {
83 m_pfnValidate(m_currentValue);
84 }
85 }
86 void PushValue(lua_State* L) const override
87 {
88 LuaHelpers::Push<T>(L, m_currentValue);
89 }
90
91 void LoadDefault() override { m_currentValue = m_defaultValue; }
92 void SetDefaultFromString(const std::string& s) override
93 {
94 T def = m_defaultValue;
95 if (!StringConversion::FromString<T>(s, m_defaultValue)) {
96 m_defaultValue = def;
97 }
98 }
99
100 [[nodiscard]] auto Get() const -> const T& { return m_currentValue; }
101
102 [[nodiscard]] auto GetDefault() const -> const T& { return m_defaultValue; }
103
104 operator const T() const { return Get(); }
105
106 void Set(const T& other)
107 {
108 m_currentValue = other;
109 BroadcastPreferenceChanged(GetName());
110 }
111
112 static auto GetPreferenceByName(const std::string& sName) -> Preference<T>*
113 {
114 auto pPreference = IPreference::GetPreferenceByName(sName);
115 Preference<T>* pRet = dynamic_cast<Preference<T>*>(pPreference);
116 return pRet;
117 }
118
119 private:
120 T m_currentValue;
121 T m_defaultValue;
122 void (*m_pfnValidate)(T& val);
123};
124
126namespace LuaHelpers {
127template<typename T>
128void
129Push(lua_State* L, const Preference<T>& Object)
130{
131 LuaHelpers::Push<T>(L, Object.Get());
132}
133}
134
135template<class T>
137{
138 public:
139 using PreferenceT = Preference<T>;
140 std::vector<PreferenceT*> m_v;
141
142 Preference1D(void pfn(size_t i, std::string& sNameOut, T& defaultValueOut),
143 size_t N)
144 {
145 for (size_t i = 0; i < N; ++i) {
146 std::string sName;
147 T defaultValue;
148 pfn(i, sName, defaultValue);
149 m_v.push_back(new Preference<T>(sName, defaultValue));
150 }
151 }
152
154 {
155 for (auto& v : m_v) {
156 SAFE_DELETE(v);
157 }
158 }
159 auto operator[](size_t i) const -> const Preference<T>& { return *m_v[i]; }
160 auto operator[](size_t i) -> Preference<T>& { return *m_v[i]; }
161};
162
163#endif
Definition Preference.h:14
Definition Preference.h:137
Definition Preference.h:53
Definition XmlFile.h:95
Utilities for working with Lua.
Definition LuaReference.cpp:172