config.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include <SimpleIni.h>
  8. #include <boost/algorithm/string/replace.hpp>
  9. // Workaround for conflicting definition in libloaderapi.h caused by SimpleIni
  10. #undef LoadString
  11. #undef CreateFile
  12. #undef DeleteFile
  13. #undef CopyFile
  14. #undef CreateDirectory
  15. #undef MoveFile
  16. namespace Core {
  17. class System;
  18. }
  19. class Config {
  20. public:
  21. enum class ConfigType {
  22. GlobalConfig,
  23. PerGameConfig,
  24. InputProfile,
  25. };
  26. virtual ~Config() = default;
  27. void ClearControlPlayerValues() const;
  28. [[nodiscard]] const std::string& GetConfigFilePath() const;
  29. [[nodiscard]] bool Exists(const std::string& section, const std::string& key) const;
  30. protected:
  31. explicit Config(ConfigType config_type = ConfigType::GlobalConfig);
  32. void Initialize(const std::string& config_name = "config");
  33. void Initialize(std::optional<std::string> config_path);
  34. void WriteToIni() const;
  35. void SetUpIni();
  36. [[nodiscard]] bool IsCustomConfig() const;
  37. void Reload();
  38. void Save();
  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. void ReadServiceValues();
  60. void ReadDisabledAddOnValues();
  61. void ReadMiscellaneousValues();
  62. void ReadCpuValues();
  63. void ReadRendererValues();
  64. void ReadScreenshotValues();
  65. void ReadSystemValues();
  66. void ReadWebServiceValues();
  67. void ReadNetworkValues();
  68. // Read platform specific sections
  69. virtual void ReadHidbusValues() = 0;
  70. virtual void ReadDebugControlValues() = 0;
  71. virtual void ReadPathValues() = 0;
  72. virtual void ReadShortcutValues() = 0;
  73. virtual void ReadUIValues() = 0;
  74. virtual void ReadUIGamelistValues() = 0;
  75. virtual void ReadUILayoutValues() = 0;
  76. virtual void ReadMultiplayerValues() = 0;
  77. void SaveValues();
  78. void SavePlayerValues(std::size_t player_index);
  79. void SaveTouchscreenValues();
  80. void SaveMotionTouchValues();
  81. // Save functions based off the respective config section names.
  82. void SaveAudioValues();
  83. void SaveControlValues();
  84. void SaveCoreValues();
  85. void SaveDataStorageValues();
  86. void SaveDebuggingValues();
  87. void SaveNetworkValues();
  88. void SaveDisabledAddOnValues();
  89. void SaveMiscellaneousValues();
  90. void SaveCpuValues();
  91. void SaveRendererValues();
  92. void SaveScreenshotValues();
  93. void SaveSystemValues();
  94. void SaveWebServiceValues();
  95. // Save platform specific sections
  96. virtual void SaveHidbusValues() = 0;
  97. virtual void SaveDebugControlValues() = 0;
  98. virtual void SavePathValues() = 0;
  99. virtual void SaveShortcutValues() = 0;
  100. virtual void SaveUIValues() = 0;
  101. virtual void SaveUIGamelistValues() = 0;
  102. virtual void SaveUILayoutValues() = 0;
  103. virtual void SaveMultiplayerValues() = 0;
  104. virtual std::vector<Settings::BasicSetting*>& FindRelevantList(Settings::Category category) = 0;
  105. /**
  106. * Reads a setting from the qt_config.
  107. *
  108. * @param key The setting's identifier
  109. * @param default_value The value to use when the setting is not already present in the config
  110. */
  111. bool ReadBooleanSetting(const std::string& key,
  112. std::optional<bool> default_value = std::nullopt);
  113. s64 ReadIntegerSetting(const std::string& key, std::optional<s64> default_value = std::nullopt);
  114. u64 ReadUnsignedIntegerSetting(const std::string& key,
  115. std::optional<u64> default_value = std::nullopt);
  116. double ReadDoubleSetting(const std::string& key,
  117. std::optional<double> default_value = std::nullopt);
  118. std::string ReadStringSetting(const std::string& key,
  119. std::optional<std::string> default_value = std::nullopt);
  120. /**
  121. * Writes a setting to the qt_config.
  122. *
  123. * @param key The setting's idetentifier
  124. * @param value Value of the setting
  125. * @param default_value Default of the setting if not present in config
  126. * @param use_global Specifies if the custom or global config should be in use, for custom
  127. * configs
  128. */
  129. template <typename Type = int>
  130. void WriteSetting(const std::string& key, const Type& value,
  131. const std::optional<Type>& default_value = std::nullopt,
  132. const std::optional<bool>& use_global = std::nullopt);
  133. void WriteSettingInternal(const std::string& key, const std::string& value);
  134. void ReadCategory(Settings::Category category);
  135. void WriteCategory(Settings::Category category);
  136. void ReadSettingGeneric(Settings::BasicSetting* setting);
  137. void WriteSettingGeneric(const Settings::BasicSetting* setting);
  138. template <typename T>
  139. [[nodiscard]] std::string ToString(const T& value_) {
  140. if constexpr (std::is_same_v<T, std::string>) {
  141. return value_;
  142. } else if constexpr (std::is_same_v<T, std::optional<u32>>) {
  143. return value_.has_value() ? std::to_string(*value_) : "none";
  144. } else if constexpr (std::is_same_v<T, bool>) {
  145. return value_ ? "true" : "false";
  146. } else if constexpr (std::is_same_v<T, u64>) {
  147. return std::to_string(static_cast<u64>(value_));
  148. } else {
  149. return std::to_string(static_cast<s64>(value_));
  150. }
  151. }
  152. void BeginGroup(const std::string& group);
  153. void EndGroup();
  154. std::string GetSection();
  155. [[nodiscard]] std::string GetGroup() const;
  156. static std::string AdjustKey(const std::string& key);
  157. static std::string AdjustOutputString(const std::string& string);
  158. std::string GetFullKey(const std::string& key, bool skipArrayIndex);
  159. int BeginArray(const std::string& array);
  160. void EndArray();
  161. void SetArrayIndex(int index);
  162. const ConfigType type;
  163. std::unique_ptr<CSimpleIniA> config;
  164. std::string config_loc;
  165. const bool global;
  166. private:
  167. inline static std::array<char, 19> special_characters = {'!', '#', '$', '%', '^', '&', '*',
  168. '|', ';', '\'', '\"', ',', '<', '.',
  169. '>', '?', '`', '~', '='};
  170. struct ConfigArray {
  171. std::string name;
  172. int size;
  173. int index;
  174. };
  175. std::vector<ConfigArray> array_stack;
  176. std::vector<std::string> key_stack;
  177. };