summaryrefslogtreecommitdiff
path: root/src/frontend_common/config.h
diff options
context:
space:
mode:
authorGravatar Charles Lombardo2023-11-24 22:59:55 -0500
committerGravatar GitHub2023-11-24 22:59:55 -0500
commit5a182f4e7ccfac696cd54542089c880d002f5cc9 (patch)
treee1bcd72f38f4f57ff6b915022268b0feb86a7937 /src/frontend_common/config.h
parentMerge pull request #12140 from liamwhite/qcr-unreachable (diff)
parentfrontend_common: Don't specify default value for screenshot_path (diff)
downloadyuzu-5a182f4e7ccfac696cd54542089c880d002f5cc9.tar.gz
yuzu-5a182f4e7ccfac696cd54542089c880d002f5cc9.tar.xz
yuzu-5a182f4e7ccfac696cd54542089c880d002f5cc9.zip
Merge pull request #11889 from t895/ini-lib
configuration: Unify config handling across frontends
Diffstat (limited to '')
-rw-r--r--src/frontend_common/config.h210
1 files changed, 210 insertions, 0 deletions
diff --git a/src/frontend_common/config.h b/src/frontend_common/config.h
new file mode 100644
index 000000000..20a1a8056
--- /dev/null
+++ b/src/frontend_common/config.h
@@ -0,0 +1,210 @@
1// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <memory>
7#include <string>
8#include "common/settings.h"
9
10#include <SimpleIni.h>
11#include <boost/algorithm/string/replace.hpp>
12
13// Workaround for conflicting definition in libloaderapi.h caused by SimpleIni
14#undef LoadString
15#undef CreateFile
16#undef DeleteFile
17#undef CopyFile
18#undef CreateDirectory
19#undef MoveFile
20
21namespace Core {
22class System;
23}
24
25class Config {
26public:
27 enum class ConfigType {
28 GlobalConfig,
29 PerGameConfig,
30 InputProfile,
31 };
32
33 virtual ~Config() = default;
34
35 void ClearControlPlayerValues() const;
36
37 [[nodiscard]] const std::string& GetConfigFilePath() const;
38
39 [[nodiscard]] bool Exists(const std::string& section, const std::string& key) const;
40
41protected:
42 explicit Config(ConfigType config_type = ConfigType::GlobalConfig);
43
44 void Initialize(const std::string& config_name = "config");
45 void Initialize(std::optional<std::string> config_path);
46
47 void WriteToIni() const;
48
49 void SetUpIni();
50 [[nodiscard]] bool IsCustomConfig() const;
51
52 void Reload();
53 void Save();
54
55 /**
56 * Derived config classes must implement this so they can reload all platform-specific
57 * values and global ones.
58 */
59 virtual void ReloadAllValues() = 0;
60
61 /**
62 * Derived config classes must implement this so they can save all platform-specific
63 * and global values.
64 */
65 virtual void SaveAllValues() = 0;
66
67 void ReadValues();
68 void ReadPlayerValues(std::size_t player_index);
69
70 void ReadTouchscreenValues();
71 void ReadMotionTouchValues();
72
73 // Read functions bases off the respective config section names.
74 void ReadAudioValues();
75 void ReadControlValues();
76 void ReadCoreValues();
77 void ReadDataStorageValues();
78 void ReadDebuggingValues();
79 void ReadServiceValues();
80 void ReadDisabledAddOnValues();
81 void ReadMiscellaneousValues();
82 void ReadCpuValues();
83 void ReadRendererValues();
84 void ReadScreenshotValues();
85 void ReadSystemValues();
86 void ReadWebServiceValues();
87 void ReadNetworkValues();
88
89 // Read platform specific sections
90 virtual void ReadHidbusValues() = 0;
91 virtual void ReadDebugControlValues() = 0;
92 virtual void ReadPathValues() = 0;
93 virtual void ReadShortcutValues() = 0;
94 virtual void ReadUIValues() = 0;
95 virtual void ReadUIGamelistValues() = 0;
96 virtual void ReadUILayoutValues() = 0;
97 virtual void ReadMultiplayerValues() = 0;
98
99 void SaveValues();
100 void SavePlayerValues(std::size_t player_index);
101 void SaveTouchscreenValues();
102 void SaveMotionTouchValues();
103
104 // Save functions based off the respective config section names.
105 void SaveAudioValues();
106 void SaveControlValues();
107 void SaveCoreValues();
108 void SaveDataStorageValues();
109 void SaveDebuggingValues();
110 void SaveNetworkValues();
111 void SaveDisabledAddOnValues();
112 void SaveMiscellaneousValues();
113 void SaveCpuValues();
114 void SaveRendererValues();
115 void SaveScreenshotValues();
116 void SaveSystemValues();
117 void SaveWebServiceValues();
118
119 // Save platform specific sections
120 virtual void SaveHidbusValues() = 0;
121 virtual void SaveDebugControlValues() = 0;
122 virtual void SavePathValues() = 0;
123 virtual void SaveShortcutValues() = 0;
124 virtual void SaveUIValues() = 0;
125 virtual void SaveUIGamelistValues() = 0;
126 virtual void SaveUILayoutValues() = 0;
127 virtual void SaveMultiplayerValues() = 0;
128
129 virtual std::vector<Settings::BasicSetting*>& FindRelevantList(Settings::Category category) = 0;
130
131 /**
132 * Reads a setting from the qt_config.
133 *
134 * @param key The setting's identifier
135 * @param default_value The value to use when the setting is not already present in the config
136 */
137 bool ReadBooleanSetting(const std::string& key,
138 std::optional<bool> default_value = std::nullopt);
139 s64 ReadIntegerSetting(const std::string& key, std::optional<s64> default_value = std::nullopt);
140 u64 ReadUnsignedIntegerSetting(const std::string& key,
141 std::optional<u64> default_value = std::nullopt);
142 double ReadDoubleSetting(const std::string& key,
143 std::optional<double> default_value = std::nullopt);
144 std::string ReadStringSetting(const std::string& key,
145 std::optional<std::string> default_value = std::nullopt);
146
147 /**
148 * Writes a setting to the qt_config.
149 *
150 * @param key The setting's idetentifier
151 * @param value Value of the setting
152 * @param default_value Default of the setting if not present in config
153 * @param use_global Specifies if the custom or global config should be in use, for custom
154 * configs
155 */
156 template <typename Type = int>
157 void WriteSetting(const std::string& key, const Type& value,
158 const std::optional<Type>& default_value = std::nullopt,
159 const std::optional<bool>& use_global = std::nullopt);
160 void WriteSettingInternal(const std::string& key, const std::string& value);
161
162 void ReadCategory(Settings::Category category);
163 void WriteCategory(Settings::Category category);
164 void ReadSettingGeneric(Settings::BasicSetting* setting);
165 void WriteSettingGeneric(const Settings::BasicSetting* setting);
166
167 template <typename T>
168 [[nodiscard]] std::string ToString(const T& value_) {
169 if constexpr (std::is_same_v<T, std::string>) {
170 return value_;
171 } else if constexpr (std::is_same_v<T, std::optional<u32>>) {
172 return value_.has_value() ? std::to_string(*value_) : "none";
173 } else if constexpr (std::is_same_v<T, bool>) {
174 return value_ ? "true" : "false";
175 } else if constexpr (std::is_same_v<T, u64>) {
176 return std::to_string(static_cast<u64>(value_));
177 } else {
178 return std::to_string(static_cast<s64>(value_));
179 }
180 }
181
182 void BeginGroup(const std::string& group);
183 void EndGroup();
184 std::string GetSection();
185 [[nodiscard]] std::string GetGroup() const;
186 static std::string AdjustKey(const std::string& key);
187 static std::string AdjustOutputString(const std::string& string);
188 std::string GetFullKey(const std::string& key, bool skipArrayIndex);
189 int BeginArray(const std::string& array);
190 void EndArray();
191 void SetArrayIndex(int index);
192
193 const ConfigType type;
194 std::unique_ptr<CSimpleIniA> config;
195 std::string config_loc;
196 const bool global;
197
198private:
199 inline static std::array<char, 19> special_characters = {'!', '#', '$', '%', '^', '&', '*',
200 '|', ';', '\'', '\"', ',', '<', '.',
201 '>', '?', '`', '~', '='};
202
203 struct ConfigArray {
204 std::string name;
205 int size;
206 int index;
207 };
208 std::vector<ConfigArray> array_stack;
209 std::vector<std::string> key_stack;
210};