settings_setting.h 16 KB

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