summaryrefslogtreecommitdiff
path: root/src/frontend_common/config.h
diff options
context:
space:
mode:
authorGravatar t8952023-11-12 02:03:01 -0500
committerGravatar t8952023-11-21 01:58:13 -0500
commitda14c7b8e47fcd5456d88a033a1fb154a0dcfa39 (patch)
tree4f9da84775b2b25aaa7eb519bccb1d5e620608a0 /src/frontend_common/config.h
parentMerge pull request #12011 from Macj0rdan/controller-applet (diff)
downloadyuzu-da14c7b8e47fcd5456d88a033a1fb154a0dcfa39.tar.gz
yuzu-da14c7b8e47fcd5456d88a033a1fb154a0dcfa39.tar.xz
yuzu-da14c7b8e47fcd5456d88a033a1fb154a0dcfa39.zip
config: Unify config handling under frontend_common
Replaces every way of handling config for each frontend with SimpleIni. frontend_common's Config class is at the center where it saves and loads all of the cross-platform settings and provides a set of pure virtual functions for platform specific settings. As a result of making config handling platform specific, several parts had to be moved to each platform's own config class or to other parts. Default keys were put in platform specific config classes and translatable strings for Qt were moved to shared_translation. Default hotkeys, default_theme, window geometry, and qt metatypes were moved to uisettings. Additionally, to reduce dependence on Qt, QStrings were converted to std::strings where applicable.
Diffstat (limited to 'src/frontend_common/config.h')
-rw-r--r--src/frontend_common/config.h206
1 files changed, 206 insertions, 0 deletions
diff --git a/src/frontend_common/config.h b/src/frontend_common/config.h
new file mode 100644
index 000000000..f741aa8bb
--- /dev/null
+++ b/src/frontend_common/config.h
@@ -0,0 +1,206 @@
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 double ReadDoubleSetting(const std::string& key,
141 std::optional<double> default_value = std::nullopt);
142 std::string ReadStringSetting(const std::string& key,
143 std::optional<std::string> default_value = std::nullopt);
144
145 /**
146 * Writes a setting to the qt_config.
147 *
148 * @param key The setting's idetentifier
149 * @param value Value of the setting
150 * @param default_value Default of the setting if not present in config
151 * @param use_global Specifies if the custom or global config should be in use, for custom
152 * configs
153 */
154 template <typename Type = int>
155 void WriteSetting(const std::string& key, const Type& value,
156 const std::optional<Type>& default_value = std::nullopt,
157 const std::optional<bool>& use_global = std::nullopt);
158 void WriteSettingInternal(const std::string& key, const std::string& value);
159
160 void ReadCategory(Settings::Category category);
161 void WriteCategory(Settings::Category category);
162 void ReadSettingGeneric(Settings::BasicSetting* setting);
163 void WriteSettingGeneric(const Settings::BasicSetting* setting);
164
165 template <typename T>
166 [[nodiscard]] std::string ToString(const T& value_) {
167 if constexpr (std::is_same_v<T, std::string>) {
168 return value_;
169 } else if constexpr (std::is_same_v<T, std::optional<u32>>) {
170 return value_.has_value() ? std::to_string(*value_) : "none";
171 } else if constexpr (std::is_same_v<T, bool>) {
172 return value_ ? "true" : "false";
173 } else {
174 return std::to_string(static_cast<s64>(value_));
175 }
176 }
177
178 void BeginGroup(const std::string& group);
179 void EndGroup();
180 std::string GetSection();
181 [[nodiscard]] std::string GetGroup() const;
182 static std::string AdjustKey(const std::string& key);
183 static std::string AdjustOutputString(const std::string& string);
184 std::string GetFullKey(const std::string& key, bool skipArrayIndex);
185 int BeginArray(const std::string& array);
186 void EndArray();
187 void SetArrayIndex(int index);
188
189 const ConfigType type;
190 std::unique_ptr<CSimpleIniA> config;
191 std::string config_loc;
192 const bool global;
193
194private:
195 inline static std::array<char, 19> special_characters = {'!', '#', '$', '%', '^', '&', '*',
196 '|', ';', '\'', '\"', ',', '<', '.',
197 '>', '?', '`', '~', '='};
198
199 struct ConfigArray {
200 std::string name;
201 int size;
202 int index;
203 };
204 std::vector<ConfigArray> array_stack;
205 std::vector<std::string> key_stack;
206};