settings_setting.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <limits>
  5. #include <map>
  6. #include <optional>
  7. #include <stdexcept>
  8. #include <string>
  9. #include <typeindex>
  10. #include <typeinfo>
  11. #include <fmt/core.h>
  12. #include "common/common_types.h"
  13. #include "common/settings_common.h"
  14. #include "common/settings_enums.h"
  15. namespace Settings {
  16. /** The Setting class is a simple resource manager. It defines a label and default value
  17. * alongside the actual value of the setting for simpler and less-error prone use with frontend
  18. * configurations. Specifying a default value and label is required. A minimum and maximum range
  19. * can be specified for sanitization.
  20. */
  21. template <typename Type, bool ranged = false>
  22. class Setting : public BasicSetting {
  23. protected:
  24. Setting() = default;
  25. public:
  26. /**
  27. * Sets a default value, label, and setting value.
  28. *
  29. * @param linkage Setting registry
  30. * @param default_val Initial value of the setting, and default value of the setting
  31. * @param name Label for the setting
  32. * @param category_ Category of the setting AKA INI group
  33. * @param specialization_ Suggestion for how frontend implementations represent this in a config
  34. * @param save_ Suggests that this should or should not be saved to a frontend config file
  35. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  36. * @param other_setting_ A second Setting to associate to this one in metadata
  37. */
  38. explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name,
  39. Category category_, u32 specialization_ = Specialization::Default,
  40. bool save_ = true, bool runtime_modifiable_ = false,
  41. BasicSetting* other_setting_ = nullptr)
  42. requires(!ranged)
  43. : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_,
  44. other_setting_),
  45. value{default_val}, default_value{default_val} {}
  46. virtual ~Setting() = default;
  47. /**
  48. * Sets a default value, minimum value, maximum value, and label.
  49. *
  50. * @param linkage Setting registry
  51. * @param default_val Initial value of the setting, and default value of the setting
  52. * @param min_val Sets the minimum allowed value of the setting
  53. * @param max_val Sets the maximum allowed value of the setting
  54. * @param name Label for the setting
  55. * @param category_ Category of the setting AKA INI group
  56. * @param specialization_ Suggestion for how frontend implementations represent this in a config
  57. * @param save_ Suggests that this should or should not be saved to a frontend config file
  58. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  59. * @param other_setting_ A second Setting to associate to this one in metadata
  60. */
  61. explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val,
  62. const Type& max_val, const std::string& name, Category category_,
  63. u32 specialization_ = Specialization::Default, bool save_ = true,
  64. bool runtime_modifiable_ = false, BasicSetting* other_setting_ = nullptr)
  65. requires(ranged)
  66. : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_,
  67. other_setting_),
  68. value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val} {}
  69. /**
  70. * Returns a reference to the setting's value.
  71. *
  72. * @returns A reference to the setting
  73. */
  74. [[nodiscard]] virtual const Type& GetValue() const {
  75. return value;
  76. }
  77. [[nodiscard]] virtual const Type& GetValue(bool need_global) const {
  78. return value;
  79. }
  80. /**
  81. * Sets the setting to the given value.
  82. *
  83. * @param val The desired value
  84. */
  85. virtual void SetValue(const Type& val) {
  86. Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
  87. std::swap(value, temp);
  88. }
  89. /**
  90. * Returns the value that this setting was created with.
  91. *
  92. * @returns A reference to the default value
  93. */
  94. [[nodiscard]] const Type& GetDefault() const {
  95. return default_value;
  96. }
  97. [[nodiscard]] constexpr bool IsEnum() const override {
  98. return std::is_enum_v<Type>;
  99. }
  100. protected:
  101. [[nodiscard]] std::string ToString(const Type& value_) const {
  102. if constexpr (std::is_same_v<Type, std::string>) {
  103. return value_;
  104. } else if constexpr (std::is_same_v<Type, std::optional<u32>>) {
  105. return value_.has_value() ? std::to_string(*value_) : "none";
  106. } else if constexpr (std::is_same_v<Type, bool>) {
  107. return value_ ? "true" : "false";
  108. } else if constexpr (std::is_same_v<Type, AudioEngine>) {
  109. // Compatibility with old AudioEngine setting being a string
  110. return CanonicalizeEnum(value_);
  111. } else if constexpr (std::is_floating_point_v<Type>) {
  112. return fmt::format("{:f}", value_);
  113. } else if constexpr (std::is_enum_v<Type>) {
  114. return std::to_string(static_cast<u32>(value_));
  115. } else {
  116. return std::to_string(value_);
  117. }
  118. }
  119. public:
  120. /**
  121. * Converts the value of the setting to a std::string. Respects the global state if the setting
  122. * has one.
  123. *
  124. * @returns The current setting as a std::string
  125. */
  126. [[nodiscard]] std::string ToString() const override {
  127. return ToString(this->GetValue());
  128. }
  129. /**
  130. * Returns the default value of the setting as a std::string.
  131. *
  132. * @returns The default value as a string.
  133. */
  134. [[nodiscard]] std::string DefaultToString() const override {
  135. return ToString(default_value);
  136. }
  137. /**
  138. * Assigns a value to the setting.
  139. *
  140. * @param val The desired setting value
  141. *
  142. * @returns A reference to the setting
  143. */
  144. virtual const Type& operator=(const Type& val) {
  145. Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
  146. std::swap(value, temp);
  147. return value;
  148. }
  149. /**
  150. * Returns a reference to the setting.
  151. *
  152. * @returns A reference to the setting
  153. */
  154. explicit virtual operator const Type&() const {
  155. return value;
  156. }
  157. /**
  158. * Converts the given value to the Setting's type of value. Uses SetValue to enter the setting,
  159. * thus respecting its constraints.
  160. *
  161. * @param input The desired value
  162. */
  163. void LoadString(const std::string& input) override final {
  164. if (input.empty()) {
  165. this->SetValue(this->GetDefault());
  166. return;
  167. }
  168. try {
  169. if constexpr (std::is_same_v<Type, std::string>) {
  170. this->SetValue(input);
  171. } else if constexpr (std::is_same_v<Type, std::optional<u32>>) {
  172. this->SetValue(static_cast<u32>(std::stoul(input)));
  173. } else if constexpr (std::is_same_v<Type, bool>) {
  174. this->SetValue(input == "true");
  175. } else if constexpr (std::is_same_v<Type, float>) {
  176. this->SetValue(std::stof(input));
  177. } else if constexpr (std::is_same_v<Type, AudioEngine>) {
  178. this->SetValue(ToEnum<AudioEngine>(input));
  179. } else {
  180. this->SetValue(static_cast<Type>(std::stoll(input)));
  181. }
  182. } catch (std::invalid_argument&) {
  183. this->SetValue(this->GetDefault());
  184. } catch (std::out_of_range&) {
  185. this->SetValue(this->GetDefault());
  186. }
  187. }
  188. [[nodiscard]] std::string Canonicalize() const override final {
  189. if constexpr (std::is_enum_v<Type>) {
  190. return CanonicalizeEnum(this->GetValue());
  191. } else {
  192. return ToString(this->GetValue());
  193. }
  194. }
  195. /**
  196. * Gives us another way to identify the setting without having to go through a string.
  197. *
  198. * @returns the type_index of the setting's type
  199. */
  200. [[nodiscard]] std::type_index TypeId() const override final {
  201. return std::type_index(typeid(Type));
  202. }
  203. [[nodiscard]] constexpr u32 EnumIndex() const override final {
  204. if constexpr (std::is_enum_v<Type>) {
  205. return EnumMetadata<Type>::Index();
  206. } else {
  207. return std::numeric_limits<u32>::max();
  208. }
  209. }
  210. [[nodiscard]] constexpr bool IsFloatingPoint() const final {
  211. return std::is_floating_point_v<Type>;
  212. }
  213. [[nodiscard]] constexpr bool IsIntegral() const final {
  214. return std::is_integral_v<Type>;
  215. }
  216. [[nodiscard]] std::string MinVal() const override final {
  217. if constexpr (std::is_arithmetic_v<Type> && !ranged) {
  218. return this->ToString(std::numeric_limits<Type>::min());
  219. } else {
  220. return this->ToString(minimum);
  221. }
  222. }
  223. [[nodiscard]] std::string MaxVal() const override final {
  224. if constexpr (std::is_arithmetic_v<Type> && !ranged) {
  225. return this->ToString(std::numeric_limits<Type>::max());
  226. } else {
  227. return this->ToString(maximum);
  228. }
  229. }
  230. [[nodiscard]] constexpr bool Ranged() const override {
  231. return ranged;
  232. }
  233. protected:
  234. Type value{}; ///< The setting
  235. const Type default_value{}; ///< The default value
  236. const Type maximum{}; ///< Maximum allowed value of the setting
  237. const Type minimum{}; ///< Minimum allowed value of the setting
  238. };
  239. /**
  240. * The SwitchableSetting class is a slightly more complex version of the Setting class. This adds a
  241. * custom setting to switch to when a guest application specifically requires it. The effect is that
  242. * other components of the emulator can access the setting's intended value without any need for the
  243. * component to ask whether the custom or global setting is needed at the moment.
  244. *
  245. * By default, the global setting is used.
  246. */
  247. template <typename Type, bool ranged = false>
  248. class SwitchableSetting : virtual public Setting<Type, ranged> {
  249. public:
  250. /**
  251. * Sets a default value, label, and setting value.
  252. *
  253. * @param linkage Setting registry
  254. * @param default_val Initial value of the setting, and default value of the setting
  255. * @param name Label for the setting
  256. * @param category_ Category of the setting AKA INI group
  257. * @param specialization_ Suggestion for how frontend implementations represent this in a config
  258. * @param save_ Suggests that this should or should not be saved to a frontend config file
  259. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  260. * @param other_setting_ A second Setting to associate to this one in metadata
  261. */
  262. template <typename T = BasicSetting>
  263. explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name,
  264. Category category_, u32 specialization_ = Specialization::Default,
  265. bool save_ = true, bool runtime_modifiable_ = false,
  266. typename std::enable_if<!ranged, T*>::type other_setting_ = nullptr)
  267. : Setting<Type, false>{
  268. linkage, default_val, name, category_, specialization_,
  269. save_, runtime_modifiable_, other_setting_} {
  270. linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
  271. }
  272. virtual ~SwitchableSetting() = default;
  273. /**
  274. * Sets a default value, minimum value, maximum value, and label.
  275. *
  276. * @param linkage Setting registry
  277. * @param default_val Initial value of the setting, and default value of the setting
  278. * @param min_val Sets the minimum allowed value of the setting
  279. * @param max_val Sets the maximum allowed value of the setting
  280. * @param name Label for the setting
  281. * @param category_ Category of the setting AKA INI group
  282. * @param specialization_ Suggestion for how frontend implementations represent this in a config
  283. * @param save_ Suggests that this should or should not be saved to a frontend config file
  284. * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
  285. * @param other_setting_ A second Setting to associate to this one in metadata
  286. */
  287. template <typename T = BasicSetting>
  288. explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val,
  289. const Type& max_val, const std::string& name, Category category_,
  290. u32 specialization_ = Specialization::Default, bool save_ = true,
  291. bool runtime_modifiable_ = false,
  292. typename std::enable_if<ranged, T*>::type other_setting_ = nullptr)
  293. : Setting<Type, true>{linkage, default_val, min_val,
  294. max_val, name, category_,
  295. specialization_, save_, runtime_modifiable_,
  296. other_setting_} {
  297. linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); });
  298. }
  299. /**
  300. * Tells this setting to represent either the global or custom setting when other member
  301. * functions are used.
  302. *
  303. * @param to_global Whether to use the global or custom setting.
  304. */
  305. void SetGlobal(bool to_global) override final {
  306. use_global = to_global;
  307. }
  308. /**
  309. * Returns whether this setting is using the global setting or not.
  310. *
  311. * @returns The global state
  312. */
  313. [[nodiscard]] bool UsingGlobal() const override final {
  314. return use_global;
  315. }
  316. /**
  317. * Returns either the global or custom setting depending on the values of this setting's global
  318. * state or if the global value was specifically requested.
  319. *
  320. * @param need_global Request global value regardless of setting's state; defaults to false
  321. *
  322. * @returns The required value of the setting
  323. */
  324. [[nodiscard]] const Type& GetValue() const override final {
  325. if (use_global) {
  326. return this->value;
  327. }
  328. return custom;
  329. }
  330. [[nodiscard]] const Type& GetValue(bool need_global) const override final {
  331. if (use_global || need_global) {
  332. return this->value;
  333. }
  334. return custom;
  335. }
  336. /**
  337. * Sets the current setting value depending on the global state.
  338. *
  339. * @param val The new value
  340. */
  341. void SetValue(const Type& val) override final {
  342. Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
  343. if (use_global) {
  344. std::swap(this->value, temp);
  345. } else {
  346. std::swap(custom, temp);
  347. }
  348. }
  349. [[nodiscard]] constexpr bool Switchable() const override final {
  350. return true;
  351. }
  352. [[nodiscard]] std::string ToStringGlobal() const override final {
  353. return this->ToString(this->value);
  354. }
  355. /**
  356. * Assigns the current setting value depending on the global state.
  357. *
  358. * @param val The new value
  359. *
  360. * @returns A reference to the current setting value
  361. */
  362. const Type& operator=(const Type& val) override final {
  363. Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
  364. if (use_global) {
  365. std::swap(this->value, temp);
  366. return this->value;
  367. }
  368. std::swap(custom, temp);
  369. return custom;
  370. }
  371. /**
  372. * Returns the current setting value depending on the global state.
  373. *
  374. * @returns A reference to the current setting value
  375. */
  376. explicit operator const Type&() const override final {
  377. if (use_global) {
  378. return this->value;
  379. }
  380. return custom;
  381. }
  382. protected:
  383. bool use_global{true}; ///< The setting's global state
  384. Type custom{}; ///< The custom value of the setting
  385. };
  386. } // namespace Settings