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