config.cpp 36 KB

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