config.h 8.4 KB

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