config.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // SPDX-FileCopyrightText: 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include "common/fs/fs.h"
  6. #include "common/fs/path_util.h"
  7. #include "common/settings.h"
  8. #include "common/settings_common.h"
  9. #include "common/settings_enums.h"
  10. #include "config.h"
  11. #include "core/core.h"
  12. #include "core/hle/service/acc/profile_manager.h"
  13. #include "core/hle/service/hid/controllers/npad.h"
  14. #include "network/network.h"
  15. #include <boost/algorithm/string/replace.hpp>
  16. namespace FS = Common::FS;
  17. Config::Config(const ConfigType config_type)
  18. : type(config_type), global{config_type == ConfigType::GlobalConfig} {}
  19. void Config::Initialize(const std::string& config_name) {
  20. const std::filesystem::path fs_config_loc = FS::GetYuzuPath(FS::YuzuPath::ConfigDir);
  21. const auto config_file = fmt::format("{}.ini", config_name);
  22. switch (type) {
  23. case ConfigType::GlobalConfig:
  24. config_loc = FS::PathToUTF8String(fs_config_loc / config_file);
  25. void(FS::CreateParentDir(config_loc));
  26. SetUpIni();
  27. Reload();
  28. break;
  29. case ConfigType::PerGameConfig:
  30. config_loc = FS::PathToUTF8String(fs_config_loc / "custom" / FS::ToU8String(config_file));
  31. void(FS::CreateParentDir(config_loc));
  32. SetUpIni();
  33. Reload();
  34. break;
  35. case ConfigType::InputProfile:
  36. config_loc = FS::PathToUTF8String(fs_config_loc / "input" / config_file);
  37. void(FS::CreateParentDir(config_loc));
  38. SetUpIni();
  39. break;
  40. }
  41. }
  42. void Config::Initialize(const std::optional<std::string> config_path) {
  43. const std::filesystem::path default_sdl_config_path =
  44. FS::GetYuzuPath(FS::YuzuPath::ConfigDir) / "sdl2-config.ini";
  45. config_loc = config_path.value_or(FS::PathToUTF8String(default_sdl_config_path));
  46. void(FS::CreateParentDir(config_loc));
  47. SetUpIni();
  48. Reload();
  49. }
  50. void Config::WriteToIni() const {
  51. if (const SI_Error rc = config->SaveFile(config_loc.c_str(), false); rc < 0) {
  52. LOG_ERROR(Frontend, "Config file could not be saved!");
  53. }
  54. }
  55. void Config::SetUpIni() {
  56. config = std::make_unique<CSimpleIniA>();
  57. config->SetUnicode(true);
  58. config->SetSpaces(false);
  59. config->LoadFile(config_loc.c_str());
  60. }
  61. bool Config::IsCustomConfig() const {
  62. return type == ConfigType::PerGameConfig;
  63. }
  64. void Config::ReadPlayerValues(const std::size_t player_index) {
  65. std::string player_prefix;
  66. if (type != ConfigType::InputProfile) {
  67. player_prefix.append("player_").append(ToString(player_index)).append("_");
  68. }
  69. auto& player = Settings::values.players.GetValue()[player_index];
  70. if (IsCustomConfig()) {
  71. const auto profile_name =
  72. ReadStringSetting(std::string(player_prefix).append("profile_name"));
  73. if (profile_name.empty()) {
  74. // Use the global input config
  75. player = Settings::values.players.GetValue(true)[player_index];
  76. return;
  77. }
  78. player.profile_name = profile_name;
  79. }
  80. if (player_prefix.empty() && Settings::IsConfiguringGlobal()) {
  81. const auto controller = static_cast<Settings::ControllerType>(
  82. ReadIntegerSetting(std::string(player_prefix).append("type"),
  83. static_cast<u8>(Settings::ControllerType::ProController)));
  84. if (controller == Settings::ControllerType::LeftJoycon ||
  85. controller == Settings::ControllerType::RightJoycon) {
  86. player.controller_type = controller;
  87. }
  88. } else {
  89. std::string connected_key = player_prefix;
  90. player.connected = ReadBooleanSetting(connected_key.append("connected"),
  91. std::make_optional(player_index == 0));
  92. player.controller_type = static_cast<Settings::ControllerType>(
  93. ReadIntegerSetting(std::string(player_prefix).append("type"),
  94. static_cast<u8>(Settings::ControllerType::ProController)));
  95. player.vibration_enabled = ReadBooleanSetting(
  96. std::string(player_prefix).append("vibration_enabled"), std::make_optional(true));
  97. player.vibration_strength = static_cast<int>(
  98. ReadIntegerSetting(std::string(player_prefix).append("vibration_strength"), 100));
  99. player.body_color_left = static_cast<u32>(ReadIntegerSetting(
  100. std::string(player_prefix).append("body_color_left"), Settings::JOYCON_BODY_NEON_BLUE));
  101. player.body_color_right = static_cast<u32>(ReadIntegerSetting(
  102. std::string(player_prefix).append("body_color_right"), Settings::JOYCON_BODY_NEON_RED));
  103. player.button_color_left = static_cast<u32>(
  104. ReadIntegerSetting(std::string(player_prefix).append("button_color_left"),
  105. Settings::JOYCON_BUTTONS_NEON_BLUE));
  106. player.button_color_right = static_cast<u32>(
  107. ReadIntegerSetting(std::string(player_prefix).append("button_color_right"),
  108. Settings::JOYCON_BUTTONS_NEON_RED));
  109. }
  110. }
  111. void Config::ReadTouchscreenValues() {
  112. Settings::values.touchscreen.enabled =
  113. ReadBooleanSetting(std::string("touchscreen_enabled"), std::make_optional(true));
  114. Settings::values.touchscreen.rotation_angle =
  115. static_cast<u32>(ReadIntegerSetting(std::string("touchscreen_angle"), 0));
  116. Settings::values.touchscreen.diameter_x =
  117. static_cast<u32>(ReadIntegerSetting(std::string("touchscreen_diameter_x"), 15));
  118. Settings::values.touchscreen.diameter_y =
  119. static_cast<u32>(ReadIntegerSetting(std::string("touchscreen_diameter_y"), 15));
  120. }
  121. void Config::ReadAudioValues() {
  122. BeginGroup(Settings::TranslateCategory(Settings::Category::Audio));
  123. ReadCategory(Settings::Category::Audio);
  124. ReadCategory(Settings::Category::UiAudio);
  125. EndGroup();
  126. }
  127. void Config::ReadControlValues() {
  128. BeginGroup(Settings::TranslateCategory(Settings::Category::Controls));
  129. ReadCategory(Settings::Category::Controls);
  130. Settings::values.players.SetGlobal(!IsCustomConfig());
  131. for (std::size_t p = 0; p < Settings::values.players.GetValue().size(); ++p) {
  132. ReadPlayerValues(p);
  133. }
  134. // Disable docked mode if handheld is selected
  135. const auto controller_type = Settings::values.players.GetValue()[0].controller_type;
  136. if (controller_type == Settings::ControllerType::Handheld) {
  137. Settings::values.use_docked_mode.SetGlobal(!IsCustomConfig());
  138. Settings::values.use_docked_mode.SetValue(Settings::ConsoleMode::Handheld);
  139. }
  140. if (IsCustomConfig()) {
  141. EndGroup();
  142. return;
  143. }
  144. ReadTouchscreenValues();
  145. ReadMotionTouchValues();
  146. EndGroup();
  147. }
  148. void Config::ReadMotionTouchValues() {
  149. int num_touch_from_button_maps = BeginArray(std::string("touch_from_button_maps"));
  150. if (num_touch_from_button_maps > 0) {
  151. for (int i = 0; i < num_touch_from_button_maps; ++i) {
  152. SetArrayIndex(i);
  153. Settings::TouchFromButtonMap map;
  154. map.name = ReadStringSetting(std::string("name"), std::string("default"));
  155. const int num_touch_maps = BeginArray(std::string("entries"));
  156. map.buttons.reserve(num_touch_maps);
  157. for (int j = 0; j < num_touch_maps; j++) {
  158. SetArrayIndex(j);
  159. std::string touch_mapping = ReadStringSetting(std::string("bind"));
  160. map.buttons.emplace_back(std::move(touch_mapping));
  161. }
  162. EndArray(); // entries
  163. Settings::values.touch_from_button_maps.emplace_back(std::move(map));
  164. }
  165. } else {
  166. Settings::values.touch_from_button_maps.emplace_back(
  167. Settings::TouchFromButtonMap{"default", {}});
  168. num_touch_from_button_maps = 1;
  169. }
  170. EndArray(); // touch_from_button_maps
  171. Settings::values.touch_from_button_map_index = std::clamp(
  172. Settings::values.touch_from_button_map_index.GetValue(), 0, num_touch_from_button_maps - 1);
  173. }
  174. void Config::ReadCoreValues() {
  175. BeginGroup(Settings::TranslateCategory(Settings::Category::Core));
  176. ReadCategory(Settings::Category::Core);
  177. EndGroup();
  178. }
  179. void Config::ReadDataStorageValues() {
  180. BeginGroup(Settings::TranslateCategory(Settings::Category::DataStorage));
  181. FS::SetYuzuPath(FS::YuzuPath::NANDDir, ReadStringSetting(std::string("nand_directory")));
  182. FS::SetYuzuPath(FS::YuzuPath::SDMCDir, ReadStringSetting(std::string("sdmc_directory")));
  183. FS::SetYuzuPath(FS::YuzuPath::LoadDir, ReadStringSetting(std::string("load_directory")));
  184. FS::SetYuzuPath(FS::YuzuPath::DumpDir, ReadStringSetting(std::string("dump_directory")));
  185. FS::SetYuzuPath(FS::YuzuPath::TASDir, ReadStringSetting(std::string("tas_directory")));
  186. ReadCategory(Settings::Category::DataStorage);
  187. EndGroup();
  188. }
  189. void Config::ReadDebuggingValues() {
  190. BeginGroup(Settings::TranslateCategory(Settings::Category::Debugging));
  191. // Intentionally not using the QT default setting as this is intended to be changed in the ini
  192. Settings::values.record_frame_times =
  193. ReadBooleanSetting(std::string("record_frame_times"), std::make_optional(false));
  194. ReadCategory(Settings::Category::Debugging);
  195. ReadCategory(Settings::Category::DebuggingGraphics);
  196. EndGroup();
  197. }
  198. void Config::ReadServiceValues() {
  199. BeginGroup(Settings::TranslateCategory(Settings::Category::Services));
  200. ReadCategory(Settings::Category::Services);
  201. EndGroup();
  202. }
  203. void Config::ReadDisabledAddOnValues() {
  204. // Custom config section
  205. BeginGroup(std::string("DisabledAddOns"));
  206. const int size = BeginArray(std::string(""));
  207. for (int i = 0; i < size; ++i) {
  208. SetArrayIndex(i);
  209. const auto title_id = ReadIntegerSetting(std::string("title_id"), 0);
  210. std::vector<std::string> out;
  211. const int d_size = BeginArray("disabled");
  212. for (int j = 0; j < d_size; ++j) {
  213. SetArrayIndex(j);
  214. out.push_back(ReadStringSetting(std::string("d"), std::string("")));
  215. }
  216. EndArray(); // d
  217. Settings::values.disabled_addons.insert_or_assign(title_id, out);
  218. }
  219. EndArray(); // Base disabled addons array - Has no base key
  220. EndGroup();
  221. }
  222. void Config::ReadMiscellaneousValues() {
  223. BeginGroup(Settings::TranslateCategory(Settings::Category::Miscellaneous));
  224. ReadCategory(Settings::Category::Miscellaneous);
  225. EndGroup();
  226. }
  227. void Config::ReadCpuValues() {
  228. BeginGroup(Settings::TranslateCategory(Settings::Category::Cpu));
  229. ReadCategory(Settings::Category::Cpu);
  230. ReadCategory(Settings::Category::CpuDebug);
  231. ReadCategory(Settings::Category::CpuUnsafe);
  232. EndGroup();
  233. }
  234. void Config::ReadRendererValues() {
  235. BeginGroup(Settings::TranslateCategory(Settings::Category::Renderer));
  236. ReadCategory(Settings::Category::Renderer);
  237. ReadCategory(Settings::Category::RendererAdvanced);
  238. ReadCategory(Settings::Category::RendererDebug);
  239. EndGroup();
  240. }
  241. void Config::ReadScreenshotValues() {
  242. BeginGroup(Settings::TranslateCategory(Settings::Category::Screenshots));
  243. ReadCategory(Settings::Category::Screenshots);
  244. FS::SetYuzuPath(FS::YuzuPath::ScreenshotsDir,
  245. ReadStringSetting(std::string("screenshot_path"),
  246. FS::GetYuzuPathString(FS::YuzuPath::ScreenshotsDir)));
  247. EndGroup();
  248. }
  249. void Config::ReadSystemValues() {
  250. BeginGroup(Settings::TranslateCategory(Settings::Category::System));
  251. ReadCategory(Settings::Category::System);
  252. ReadCategory(Settings::Category::SystemAudio);
  253. EndGroup();
  254. }
  255. void Config::ReadWebServiceValues() {
  256. BeginGroup(Settings::TranslateCategory(Settings::Category::WebService));
  257. ReadCategory(Settings::Category::WebService);
  258. EndGroup();
  259. }
  260. void Config::ReadNetworkValues() {
  261. BeginGroup(Settings::TranslateCategory(Settings::Category::Services));
  262. ReadCategory(Settings::Category::Network);
  263. EndGroup();
  264. }
  265. void Config::ReadValues() {
  266. if (global) {
  267. ReadDataStorageValues();
  268. ReadDebuggingValues();
  269. ReadDisabledAddOnValues();
  270. ReadNetworkValues();
  271. ReadServiceValues();
  272. ReadWebServiceValues();
  273. ReadMiscellaneousValues();
  274. }
  275. ReadControlValues();
  276. ReadCoreValues();
  277. ReadCpuValues();
  278. ReadRendererValues();
  279. ReadAudioValues();
  280. ReadSystemValues();
  281. }
  282. void Config::SavePlayerValues(const std::size_t player_index) {
  283. std::string player_prefix;
  284. if (type != ConfigType::InputProfile) {
  285. player_prefix = std::string("player_").append(ToString(player_index)).append("_");
  286. }
  287. const auto& player = Settings::values.players.GetValue()[player_index];
  288. if (IsCustomConfig()) {
  289. if (player.profile_name.empty()) {
  290. // No custom profile selected
  291. return;
  292. }
  293. WriteSetting(std::string(player_prefix).append("profile_name"), player.profile_name,
  294. std::make_optional(std::string("")));
  295. }
  296. WriteSetting(std::string(player_prefix).append("type"), static_cast<u8>(player.controller_type),
  297. std::make_optional(static_cast<u8>(Settings::ControllerType::ProController)));
  298. if (!player_prefix.empty() || !Settings::IsConfiguringGlobal()) {
  299. WriteSetting(std::string(player_prefix).append("connected"), player.connected,
  300. std::make_optional(player_index == 0));
  301. WriteSetting(std::string(player_prefix).append("vibration_enabled"),
  302. player.vibration_enabled, std::make_optional(true));
  303. WriteSetting(std::string(player_prefix).append("vibration_strength"),
  304. player.vibration_strength, std::make_optional(100));
  305. WriteSetting(std::string(player_prefix).append("body_color_left"), player.body_color_left,
  306. std::make_optional(Settings::JOYCON_BODY_NEON_BLUE));
  307. WriteSetting(std::string(player_prefix).append("body_color_right"), player.body_color_right,
  308. std::make_optional(Settings::JOYCON_BODY_NEON_RED));
  309. WriteSetting(std::string(player_prefix).append("button_color_left"),
  310. player.button_color_left,
  311. std::make_optional(Settings::JOYCON_BUTTONS_NEON_BLUE));
  312. WriteSetting(std::string(player_prefix).append("button_color_right"),
  313. player.button_color_right,
  314. std::make_optional(Settings::JOYCON_BUTTONS_NEON_RED));
  315. }
  316. }
  317. void Config::SaveTouchscreenValues() {
  318. const auto& touchscreen = Settings::values.touchscreen;
  319. WriteSetting(std::string("touchscreen_enabled"), touchscreen.enabled, std::make_optional(true));
  320. WriteSetting(std::string("touchscreen_angle"), touchscreen.rotation_angle,
  321. std::make_optional(static_cast<u32>(0)));
  322. WriteSetting(std::string("touchscreen_diameter_x"), touchscreen.diameter_x,
  323. std::make_optional(static_cast<u32>(15)));
  324. WriteSetting(std::string("touchscreen_diameter_y"), touchscreen.diameter_y,
  325. std::make_optional(static_cast<u32>(15)));
  326. }
  327. void Config::SaveMotionTouchValues() {
  328. BeginArray(std::string("touch_from_button_maps"));
  329. for (std::size_t p = 0; p < Settings::values.touch_from_button_maps.size(); ++p) {
  330. SetArrayIndex(static_cast<int>(p));
  331. WriteSetting(std::string("name"), Settings::values.touch_from_button_maps[p].name,
  332. std::make_optional(std::string("default")));
  333. BeginArray(std::string("entries"));
  334. for (std::size_t q = 0; q < Settings::values.touch_from_button_maps[p].buttons.size();
  335. ++q) {
  336. SetArrayIndex(static_cast<int>(q));
  337. WriteSetting(std::string("bind"),
  338. Settings::values.touch_from_button_maps[p].buttons[q]);
  339. }
  340. EndArray(); // entries
  341. }
  342. EndArray(); // touch_from_button_maps
  343. }
  344. void Config::SaveValues() {
  345. if (global) {
  346. SaveDataStorageValues();
  347. SaveDebuggingValues();
  348. SaveDisabledAddOnValues();
  349. SaveNetworkValues();
  350. SaveWebServiceValues();
  351. SaveMiscellaneousValues();
  352. }
  353. SaveControlValues();
  354. SaveCoreValues();
  355. SaveCpuValues();
  356. SaveRendererValues();
  357. SaveAudioValues();
  358. SaveSystemValues();
  359. WriteToIni();
  360. }
  361. void Config::SaveAudioValues() {
  362. BeginGroup(Settings::TranslateCategory(Settings::Category::Audio));
  363. WriteCategory(Settings::Category::Audio);
  364. WriteCategory(Settings::Category::UiAudio);
  365. EndGroup();
  366. }
  367. void Config::SaveControlValues() {
  368. BeginGroup(Settings::TranslateCategory(Settings::Category::Controls));
  369. WriteCategory(Settings::Category::Controls);
  370. Settings::values.players.SetGlobal(!IsCustomConfig());
  371. for (std::size_t p = 0; p < Settings::values.players.GetValue().size(); ++p) {
  372. SavePlayerValues(p);
  373. }
  374. if (IsCustomConfig()) {
  375. EndGroup();
  376. return;
  377. }
  378. SaveTouchscreenValues();
  379. SaveMotionTouchValues();
  380. EndGroup();
  381. }
  382. void Config::SaveCoreValues() {
  383. BeginGroup(Settings::TranslateCategory(Settings::Category::Core));
  384. WriteCategory(Settings::Category::Core);
  385. EndGroup();
  386. }
  387. void Config::SaveDataStorageValues() {
  388. BeginGroup(Settings::TranslateCategory(Settings::Category::DataStorage));
  389. WriteSetting(std::string("nand_directory"), FS::GetYuzuPathString(FS::YuzuPath::NANDDir),
  390. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::NANDDir)));
  391. WriteSetting(std::string("sdmc_directory"), FS::GetYuzuPathString(FS::YuzuPath::SDMCDir),
  392. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::SDMCDir)));
  393. WriteSetting(std::string("load_directory"), FS::GetYuzuPathString(FS::YuzuPath::LoadDir),
  394. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::LoadDir)));
  395. WriteSetting(std::string("dump_directory"), FS::GetYuzuPathString(FS::YuzuPath::DumpDir),
  396. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::DumpDir)));
  397. WriteSetting(std::string("tas_directory"), FS::GetYuzuPathString(FS::YuzuPath::TASDir),
  398. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::TASDir)));
  399. WriteCategory(Settings::Category::DataStorage);
  400. EndGroup();
  401. }
  402. void Config::SaveDebuggingValues() {
  403. BeginGroup(Settings::TranslateCategory(Settings::Category::Debugging));
  404. // Intentionally not using the QT default setting as this is intended to be changed in the ini
  405. WriteSetting(std::string("record_frame_times"), Settings::values.record_frame_times);
  406. WriteCategory(Settings::Category::Debugging);
  407. WriteCategory(Settings::Category::DebuggingGraphics);
  408. EndGroup();
  409. }
  410. void Config::SaveNetworkValues() {
  411. BeginGroup(Settings::TranslateCategory(Settings::Category::Services));
  412. WriteCategory(Settings::Category::Network);
  413. EndGroup();
  414. }
  415. void Config::SaveDisabledAddOnValues() {
  416. // Custom config section
  417. BeginGroup(std::string("DisabledAddOns"));
  418. int i = 0;
  419. BeginArray(std::string(""));
  420. for (const auto& elem : Settings::values.disabled_addons) {
  421. SetArrayIndex(i);
  422. WriteSetting(std::string("title_id"), elem.first, std::make_optional(static_cast<u64>(0)));
  423. BeginArray(std::string("disabled"));
  424. for (std::size_t j = 0; j < elem.second.size(); ++j) {
  425. SetArrayIndex(static_cast<int>(j));
  426. WriteSetting(std::string("d"), elem.second[j], std::make_optional(std::string("")));
  427. }
  428. EndArray(); // disabled
  429. ++i;
  430. }
  431. EndArray(); // Base disabled addons array - Has no base key
  432. EndGroup();
  433. }
  434. void Config::SaveMiscellaneousValues() {
  435. BeginGroup(Settings::TranslateCategory(Settings::Category::Miscellaneous));
  436. WriteCategory(Settings::Category::Miscellaneous);
  437. EndGroup();
  438. }
  439. void Config::SaveCpuValues() {
  440. BeginGroup(Settings::TranslateCategory(Settings::Category::Cpu));
  441. WriteCategory(Settings::Category::Cpu);
  442. WriteCategory(Settings::Category::CpuDebug);
  443. WriteCategory(Settings::Category::CpuUnsafe);
  444. EndGroup();
  445. }
  446. void Config::SaveRendererValues() {
  447. BeginGroup(Settings::TranslateCategory(Settings::Category::Renderer));
  448. WriteCategory(Settings::Category::Renderer);
  449. WriteCategory(Settings::Category::RendererAdvanced);
  450. WriteCategory(Settings::Category::RendererDebug);
  451. EndGroup();
  452. }
  453. void Config::SaveScreenshotValues() {
  454. BeginGroup(Settings::TranslateCategory(Settings::Category::Screenshots));
  455. WriteSetting(std::string("screenshot_path"),
  456. FS::GetYuzuPathString(FS::YuzuPath::ScreenshotsDir));
  457. WriteCategory(Settings::Category::Screenshots);
  458. EndGroup();
  459. }
  460. void Config::SaveSystemValues() {
  461. BeginGroup(Settings::TranslateCategory(Settings::Category::System));
  462. WriteCategory(Settings::Category::System);
  463. WriteCategory(Settings::Category::SystemAudio);
  464. EndGroup();
  465. }
  466. void Config::SaveWebServiceValues() {
  467. BeginGroup(Settings::TranslateCategory(Settings::Category::WebService));
  468. WriteCategory(Settings::Category::WebService);
  469. EndGroup();
  470. }
  471. bool Config::ReadBooleanSetting(const std::string& key, const std::optional<bool> default_value) {
  472. std::string full_key = GetFullKey(key, false);
  473. if (!default_value.has_value()) {
  474. return config->GetBoolValue(GetSection().c_str(), full_key.c_str(), false);
  475. }
  476. if (config->GetBoolValue(GetSection().c_str(),
  477. std::string(full_key).append("\\default").c_str(), false)) {
  478. return static_cast<bool>(default_value.value());
  479. } else {
  480. return config->GetBoolValue(GetSection().c_str(), full_key.c_str(),
  481. static_cast<bool>(default_value.value()));
  482. }
  483. }
  484. s64 Config::ReadIntegerSetting(const std::string& key, const std::optional<s64> default_value) {
  485. std::string full_key = GetFullKey(key, false);
  486. if (!default_value.has_value()) {
  487. try {
  488. return std::stoll(
  489. std::string(config->GetValue(GetSection().c_str(), full_key.c_str(), "0")));
  490. } catch (...) {
  491. return 0;
  492. }
  493. }
  494. s64 result = 0;
  495. if (config->GetBoolValue(GetSection().c_str(),
  496. std::string(full_key).append("\\default").c_str(), true)) {
  497. result = default_value.value();
  498. } else {
  499. try {
  500. result = std::stoll(std::string(config->GetValue(
  501. GetSection().c_str(), full_key.c_str(), ToString(default_value.value()).c_str())));
  502. } catch (...) {
  503. result = default_value.value();
  504. }
  505. }
  506. return result;
  507. }
  508. double Config::ReadDoubleSetting(const std::string& key,
  509. const std::optional<double> default_value) {
  510. std::string full_key = GetFullKey(key, false);
  511. if (!default_value.has_value()) {
  512. return config->GetDoubleValue(GetSection().c_str(), full_key.c_str(), 0);
  513. }
  514. double result;
  515. if (config->GetBoolValue(GetSection().c_str(),
  516. std::string(full_key).append("\\default").c_str(), true)) {
  517. result = default_value.value();
  518. } else {
  519. result =
  520. config->GetDoubleValue(GetSection().c_str(), full_key.c_str(), default_value.value());
  521. }
  522. return result;
  523. }
  524. std::string Config::ReadStringSetting(const std::string& key,
  525. const std::optional<std::string> default_value) {
  526. std::string result;
  527. std::string full_key = GetFullKey(key, false);
  528. if (!default_value.has_value()) {
  529. result = config->GetValue(GetSection().c_str(), full_key.c_str(), "");
  530. boost::replace_all(result, "\"", "");
  531. return result;
  532. }
  533. if (config->GetBoolValue(GetSection().c_str(),
  534. std::string(full_key).append("\\default").c_str(), true)) {
  535. result = default_value.value();
  536. } else {
  537. result =
  538. config->GetValue(GetSection().c_str(), full_key.c_str(), default_value.value().c_str());
  539. }
  540. boost::replace_all(result, "\"", "");
  541. boost::replace_all(result, "//", "/");
  542. return result;
  543. }
  544. bool Config::Exists(const std::string& section, const std::string& key) const {
  545. const std::string value = config->GetValue(section.c_str(), key.c_str(), "");
  546. return !value.empty();
  547. }
  548. template <typename Type>
  549. void Config::WriteSetting(const std::string& key, const Type& value,
  550. const std::optional<Type>& default_value,
  551. const std::optional<bool>& use_global) {
  552. std::string full_key = GetFullKey(key, false);
  553. std::string saved_value;
  554. std::string string_default;
  555. if constexpr (std::is_same_v<Type, std::string>) {
  556. saved_value.append(AdjustOutputString(value));
  557. if (default_value.has_value()) {
  558. string_default.append(AdjustOutputString(default_value.value()));
  559. }
  560. } else {
  561. saved_value.append(AdjustOutputString(ToString(value)));
  562. if (default_value.has_value()) {
  563. string_default.append(ToString(default_value.value()));
  564. }
  565. }
  566. if (default_value.has_value() && use_global.has_value()) {
  567. if (!global) {
  568. WriteSettingInternal(std::string(full_key).append("\\global"),
  569. ToString(use_global.value()));
  570. }
  571. if (global || use_global.value() == false) {
  572. WriteSettingInternal(std::string(full_key).append("\\default"),
  573. ToString(string_default == saved_value));
  574. WriteSettingInternal(full_key, saved_value);
  575. }
  576. } else if (default_value.has_value() && !use_global.has_value()) {
  577. WriteSettingInternal(std::string(full_key).append("\\default"),
  578. ToString(string_default == saved_value));
  579. WriteSettingInternal(full_key, saved_value);
  580. } else {
  581. WriteSettingInternal(full_key, saved_value);
  582. }
  583. }
  584. void Config::WriteSettingInternal(const std::string& key, const std::string& value) {
  585. config->SetValue(GetSection().c_str(), key.c_str(), value.c_str());
  586. }
  587. void Config::Reload() {
  588. ReadValues();
  589. // To apply default value changes
  590. SaveValues();
  591. }
  592. void Config::Save() {
  593. SaveValues();
  594. }
  595. void Config::ClearControlPlayerValues() const {
  596. // If key is an empty string, all keys in the current group() are removed.
  597. const char* section = Settings::TranslateCategory(Settings::Category::Controls);
  598. CSimpleIniA::TNamesDepend keys;
  599. config->GetAllKeys(section, keys);
  600. for (const auto& key : keys) {
  601. if (std::string(config->GetValue(section, key.pItem)).empty()) {
  602. config->Delete(section, key.pItem);
  603. }
  604. }
  605. }
  606. const std::string& Config::GetConfigFilePath() const {
  607. return config_loc;
  608. }
  609. void Config::ReadCategory(const Settings::Category category) {
  610. const auto& settings = FindRelevantList(category);
  611. std::ranges::for_each(settings, [&](const auto& setting) { ReadSettingGeneric(setting); });
  612. }
  613. void Config::WriteCategory(const Settings::Category category) {
  614. const auto& settings = FindRelevantList(category);
  615. std::ranges::for_each(settings, [&](const auto& setting) { WriteSettingGeneric(setting); });
  616. }
  617. void Config::ReadSettingGeneric(Settings::BasicSetting* const setting) {
  618. if (!setting->Save() || (!setting->Switchable() && !global)) {
  619. return;
  620. }
  621. const std::string key = AdjustKey(setting->GetLabel());
  622. const std::string default_value(setting->DefaultToString());
  623. bool use_global = true;
  624. if (setting->Switchable() && !global) {
  625. use_global =
  626. ReadBooleanSetting(std::string(key).append("\\use_global"), std::make_optional(true));
  627. setting->SetGlobal(use_global);
  628. }
  629. if (global || !use_global) {
  630. const bool is_default =
  631. ReadBooleanSetting(std::string(key).append("\\default"), std::make_optional(true));
  632. if (!is_default) {
  633. const std::string setting_string = ReadStringSetting(key, default_value);
  634. setting->LoadString(setting_string);
  635. } else {
  636. // Empty string resets the Setting to default
  637. setting->LoadString("");
  638. }
  639. }
  640. }
  641. void Config::WriteSettingGeneric(const Settings::BasicSetting* const setting) {
  642. if (!setting->Save()) {
  643. return;
  644. }
  645. std::string key = AdjustKey(setting->GetLabel());
  646. if (setting->Switchable()) {
  647. if (!global) {
  648. WriteSetting(std::string(key).append("\\use_global"), setting->UsingGlobal());
  649. }
  650. if (global || !setting->UsingGlobal()) {
  651. WriteSetting(std::string(key).append("\\default"),
  652. setting->ToString() == setting->DefaultToString());
  653. WriteSetting(key, setting->ToString());
  654. }
  655. } else if (global) {
  656. WriteSetting(std::string(key).append("\\default"),
  657. setting->ToString() == setting->DefaultToString());
  658. WriteSetting(key, setting->ToString());
  659. }
  660. }
  661. void Config::BeginGroup(const std::string& group) {
  662. // You can't begin a group while reading/writing from a config array
  663. ASSERT(array_stack.empty());
  664. key_stack.push_back(AdjustKey(group));
  665. }
  666. void Config::EndGroup() {
  667. // You can't end a group if you haven't started one yet
  668. ASSERT(!key_stack.empty());
  669. // You can't end a group when reading/writing from a config array
  670. ASSERT(array_stack.empty());
  671. key_stack.pop_back();
  672. }
  673. std::string Config::GetSection() {
  674. if (key_stack.empty()) {
  675. return std::string{""};
  676. }
  677. return key_stack.front();
  678. }
  679. std::string Config::GetGroup() const {
  680. if (key_stack.size() <= 1) {
  681. return std::string{""};
  682. }
  683. std::string key;
  684. for (size_t i = 1; i < key_stack.size(); ++i) {
  685. key.append(key_stack[i]).append("\\");
  686. }
  687. return key;
  688. }
  689. std::string Config::AdjustKey(const std::string& key) {
  690. std::string adjusted_key(key);
  691. boost::replace_all(adjusted_key, "/", "\\");
  692. boost::replace_all(adjusted_key, " ", "%20");
  693. return adjusted_key;
  694. }
  695. std::string Config::AdjustOutputString(const std::string& string) {
  696. std::string adjusted_string(string);
  697. boost::replace_all(adjusted_string, "\\", "/");
  698. boost::replace_all(adjusted_string, "//", "/");
  699. // Needed for backwards compatibility with QSettings deserialization
  700. for (const auto& special_character : special_characters) {
  701. if (adjusted_string.find(special_character) != std::string::npos) {
  702. adjusted_string.insert(0, "\"");
  703. adjusted_string.append("\"");
  704. break;
  705. }
  706. }
  707. return adjusted_string;
  708. }
  709. std::string Config::GetFullKey(const std::string& key, bool skipArrayIndex) {
  710. if (array_stack.empty()) {
  711. return std::string(GetGroup()).append(AdjustKey(key));
  712. }
  713. std::string array_key;
  714. for (size_t i = 0; i < array_stack.size(); ++i) {
  715. if (!array_stack[i].name.empty()) {
  716. array_key.append(array_stack[i].name).append("\\");
  717. }
  718. if (!skipArrayIndex || (array_stack.size() - 1 != i && array_stack.size() > 1)) {
  719. array_key.append(ToString(array_stack[i].index)).append("\\");
  720. }
  721. }
  722. std::string final_key = std::string(GetGroup()).append(array_key).append(AdjustKey(key));
  723. return final_key;
  724. }
  725. int Config::BeginArray(const std::string& array) {
  726. array_stack.push_back(ConfigArray{AdjustKey(array), 0, 0});
  727. const int size = config->GetLongValue(GetSection().c_str(),
  728. GetFullKey(std::string("size"), true).c_str(), 0);
  729. array_stack.back().size = size;
  730. return size;
  731. }
  732. void Config::EndArray() {
  733. // You can't end a config array before starting one
  734. ASSERT(!array_stack.empty());
  735. // Write out the size to config
  736. if (key_stack.size() == 1 && array_stack.back().name.empty()) {
  737. // Edge-case where the first array created doesn't have a name
  738. config->SetValue(GetSection().c_str(), std::string("size").c_str(),
  739. ToString(array_stack.back().size).c_str());
  740. } else {
  741. const auto key = GetFullKey(std::string("size"), true);
  742. config->SetValue(GetSection().c_str(), key.c_str(),
  743. ToString(array_stack.back().size).c_str());
  744. }
  745. array_stack.pop_back();
  746. }
  747. void Config::SetArrayIndex(const int index) {
  748. // You can't set the array index if you haven't started one yet
  749. ASSERT(!array_stack.empty());
  750. const int array_index = index + 1;
  751. // You can't exceed the known max size of the array by more than 1
  752. ASSERT(array_stack.front().size + 1 >= array_index);
  753. // Change the config array size to the current index since you may want
  754. // to reduce the number of elements that you read back from the config
  755. // in the future.
  756. array_stack.back().size = array_index;
  757. array_stack.back().index = array_index;
  758. }