config.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. int num_touch_from_button_maps = BeginArray(std::string("touch_from_button_maps"));
  183. if (num_touch_from_button_maps > 0) {
  184. for (int i = 0; i < num_touch_from_button_maps; ++i) {
  185. SetArrayIndex(i);
  186. Settings::TouchFromButtonMap map;
  187. map.name = ReadStringSetting(std::string("name"), std::string("default"));
  188. const int num_touch_maps = BeginArray(std::string("entries"));
  189. map.buttons.reserve(num_touch_maps);
  190. for (int j = 0; j < num_touch_maps; j++) {
  191. SetArrayIndex(j);
  192. std::string touch_mapping = ReadStringSetting(std::string("bind"));
  193. map.buttons.emplace_back(std::move(touch_mapping));
  194. }
  195. EndArray(); // entries
  196. Settings::values.touch_from_button_maps.emplace_back(std::move(map));
  197. }
  198. } else {
  199. Settings::values.touch_from_button_maps.emplace_back(
  200. Settings::TouchFromButtonMap{"default", {}});
  201. num_touch_from_button_maps = 1;
  202. }
  203. EndArray(); // touch_from_button_maps
  204. Settings::values.touch_from_button_map_index = std::clamp(
  205. Settings::values.touch_from_button_map_index.GetValue(), 0, num_touch_from_button_maps - 1);
  206. }
  207. void Config::ReadCoreValues() {
  208. BeginGroup(Settings::TranslateCategory(Settings::Category::Core));
  209. ReadCategory(Settings::Category::Core);
  210. EndGroup();
  211. }
  212. void Config::ReadDataStorageValues() {
  213. BeginGroup(Settings::TranslateCategory(Settings::Category::DataStorage));
  214. FS::SetYuzuPath(FS::YuzuPath::NANDDir, ReadStringSetting(std::string("nand_directory")));
  215. FS::SetYuzuPath(FS::YuzuPath::SDMCDir, ReadStringSetting(std::string("sdmc_directory")));
  216. FS::SetYuzuPath(FS::YuzuPath::LoadDir, ReadStringSetting(std::string("load_directory")));
  217. FS::SetYuzuPath(FS::YuzuPath::DumpDir, ReadStringSetting(std::string("dump_directory")));
  218. FS::SetYuzuPath(FS::YuzuPath::TASDir, ReadStringSetting(std::string("tas_directory")));
  219. ReadCategory(Settings::Category::DataStorage);
  220. EndGroup();
  221. }
  222. void Config::ReadDebuggingValues() {
  223. BeginGroup(Settings::TranslateCategory(Settings::Category::Debugging));
  224. // Intentionally not using the QT default setting as this is intended to be changed in the ini
  225. Settings::values.record_frame_times =
  226. ReadBooleanSetting(std::string("record_frame_times"), std::make_optional(false));
  227. ReadCategory(Settings::Category::Debugging);
  228. ReadCategory(Settings::Category::DebuggingGraphics);
  229. EndGroup();
  230. }
  231. void Config::ReadServiceValues() {
  232. BeginGroup(Settings::TranslateCategory(Settings::Category::Services));
  233. ReadCategory(Settings::Category::Services);
  234. EndGroup();
  235. }
  236. void Config::ReadDisabledAddOnValues() {
  237. // Custom config section
  238. BeginGroup(std::string("DisabledAddOns"));
  239. const int size = BeginArray(std::string(""));
  240. for (int i = 0; i < size; ++i) {
  241. SetArrayIndex(i);
  242. const auto title_id = ReadUnsignedIntegerSetting(std::string("title_id"), 0);
  243. std::vector<std::string> out;
  244. const int d_size = BeginArray("disabled");
  245. for (int j = 0; j < d_size; ++j) {
  246. SetArrayIndex(j);
  247. out.push_back(ReadStringSetting(std::string("d"), std::string("")));
  248. }
  249. EndArray(); // d
  250. Settings::values.disabled_addons.insert_or_assign(title_id, out);
  251. }
  252. EndArray(); // Base disabled addons array - Has no base key
  253. EndGroup();
  254. }
  255. void Config::ReadMiscellaneousValues() {
  256. BeginGroup(Settings::TranslateCategory(Settings::Category::Miscellaneous));
  257. ReadCategory(Settings::Category::Miscellaneous);
  258. EndGroup();
  259. }
  260. void Config::ReadCpuValues() {
  261. BeginGroup(Settings::TranslateCategory(Settings::Category::Cpu));
  262. ReadCategory(Settings::Category::Cpu);
  263. ReadCategory(Settings::Category::CpuDebug);
  264. ReadCategory(Settings::Category::CpuUnsafe);
  265. EndGroup();
  266. }
  267. void Config::ReadRendererValues() {
  268. BeginGroup(Settings::TranslateCategory(Settings::Category::Renderer));
  269. ReadCategory(Settings::Category::Renderer);
  270. ReadCategory(Settings::Category::RendererAdvanced);
  271. ReadCategory(Settings::Category::RendererDebug);
  272. EndGroup();
  273. }
  274. void Config::ReadScreenshotValues() {
  275. BeginGroup(Settings::TranslateCategory(Settings::Category::Screenshots));
  276. ReadCategory(Settings::Category::Screenshots);
  277. FS::SetYuzuPath(FS::YuzuPath::ScreenshotsDir,
  278. ReadStringSetting(std::string("screenshot_path")));
  279. EndGroup();
  280. }
  281. void Config::ReadSystemValues() {
  282. BeginGroup(Settings::TranslateCategory(Settings::Category::System));
  283. ReadCategory(Settings::Category::System);
  284. ReadCategory(Settings::Category::SystemAudio);
  285. EndGroup();
  286. }
  287. void Config::ReadWebServiceValues() {
  288. BeginGroup(Settings::TranslateCategory(Settings::Category::WebService));
  289. ReadCategory(Settings::Category::WebService);
  290. EndGroup();
  291. }
  292. void Config::ReadNetworkValues() {
  293. BeginGroup(Settings::TranslateCategory(Settings::Category::Services));
  294. ReadCategory(Settings::Category::Network);
  295. EndGroup();
  296. }
  297. void Config::ReadValues() {
  298. if (global) {
  299. ReadDataStorageValues();
  300. ReadDebuggingValues();
  301. ReadDisabledAddOnValues();
  302. ReadNetworkValues();
  303. ReadServiceValues();
  304. ReadWebServiceValues();
  305. ReadMiscellaneousValues();
  306. }
  307. ReadControlValues();
  308. ReadCoreValues();
  309. ReadCpuValues();
  310. ReadRendererValues();
  311. ReadAudioValues();
  312. ReadSystemValues();
  313. }
  314. void Config::SavePlayerValues(const std::size_t player_index) {
  315. std::string player_prefix;
  316. if (type != ConfigType::InputProfile) {
  317. player_prefix = std::string("player_").append(ToString(player_index)).append("_");
  318. }
  319. const auto& player = Settings::values.players.GetValue()[player_index];
  320. if (IsCustomConfig()) {
  321. if (player.profile_name.empty()) {
  322. // No custom profile selected
  323. return;
  324. }
  325. WriteSetting(std::string(player_prefix).append("profile_name"), player.profile_name,
  326. std::make_optional(std::string("")));
  327. }
  328. WriteSetting(std::string(player_prefix).append("type"), static_cast<u8>(player.controller_type),
  329. std::make_optional(static_cast<u8>(Settings::ControllerType::ProController)));
  330. if (!player_prefix.empty() || !Settings::IsConfiguringGlobal()) {
  331. WriteSetting(std::string(player_prefix).append("connected"), player.connected,
  332. std::make_optional(player_index == 0));
  333. WriteSetting(std::string(player_prefix).append("vibration_enabled"),
  334. player.vibration_enabled, std::make_optional(true));
  335. WriteSetting(std::string(player_prefix).append("vibration_strength"),
  336. player.vibration_strength, std::make_optional(100));
  337. WriteSetting(std::string(player_prefix).append("body_color_left"), player.body_color_left,
  338. std::make_optional(Settings::JOYCON_BODY_NEON_BLUE));
  339. WriteSetting(std::string(player_prefix).append("body_color_right"), player.body_color_right,
  340. std::make_optional(Settings::JOYCON_BODY_NEON_RED));
  341. WriteSetting(std::string(player_prefix).append("button_color_left"),
  342. player.button_color_left,
  343. std::make_optional(Settings::JOYCON_BUTTONS_NEON_BLUE));
  344. WriteSetting(std::string(player_prefix).append("button_color_right"),
  345. player.button_color_right,
  346. std::make_optional(Settings::JOYCON_BUTTONS_NEON_RED));
  347. }
  348. }
  349. void Config::SaveTouchscreenValues() {
  350. const auto& touchscreen = Settings::values.touchscreen;
  351. WriteSetting(std::string("touchscreen_enabled"), touchscreen.enabled, std::make_optional(true));
  352. WriteSetting(std::string("touchscreen_angle"), touchscreen.rotation_angle,
  353. std::make_optional(static_cast<u32>(0)));
  354. WriteSetting(std::string("touchscreen_diameter_x"), touchscreen.diameter_x,
  355. std::make_optional(static_cast<u32>(15)));
  356. WriteSetting(std::string("touchscreen_diameter_y"), touchscreen.diameter_y,
  357. std::make_optional(static_cast<u32>(15)));
  358. }
  359. void Config::SaveMotionTouchValues() {
  360. BeginArray(std::string("touch_from_button_maps"));
  361. for (std::size_t p = 0; p < Settings::values.touch_from_button_maps.size(); ++p) {
  362. SetArrayIndex(static_cast<int>(p));
  363. WriteSetting(std::string("name"), Settings::values.touch_from_button_maps[p].name,
  364. std::make_optional(std::string("default")));
  365. BeginArray(std::string("entries"));
  366. for (std::size_t q = 0; q < Settings::values.touch_from_button_maps[p].buttons.size();
  367. ++q) {
  368. SetArrayIndex(static_cast<int>(q));
  369. WriteSetting(std::string("bind"),
  370. Settings::values.touch_from_button_maps[p].buttons[q]);
  371. }
  372. EndArray(); // entries
  373. }
  374. EndArray(); // touch_from_button_maps
  375. }
  376. void Config::SaveValues() {
  377. if (global) {
  378. SaveDataStorageValues();
  379. SaveDebuggingValues();
  380. SaveDisabledAddOnValues();
  381. SaveNetworkValues();
  382. SaveWebServiceValues();
  383. SaveMiscellaneousValues();
  384. }
  385. SaveControlValues();
  386. SaveCoreValues();
  387. SaveCpuValues();
  388. SaveRendererValues();
  389. SaveAudioValues();
  390. SaveSystemValues();
  391. WriteToIni();
  392. }
  393. void Config::SaveAudioValues() {
  394. BeginGroup(Settings::TranslateCategory(Settings::Category::Audio));
  395. WriteCategory(Settings::Category::Audio);
  396. WriteCategory(Settings::Category::UiAudio);
  397. EndGroup();
  398. }
  399. void Config::SaveControlValues() {
  400. BeginGroup(Settings::TranslateCategory(Settings::Category::Controls));
  401. WriteCategory(Settings::Category::Controls);
  402. Settings::values.players.SetGlobal(!IsCustomConfig());
  403. for (std::size_t p = 0; p < Settings::values.players.GetValue().size(); ++p) {
  404. SavePlayerValues(p);
  405. }
  406. if (IsCustomConfig()) {
  407. EndGroup();
  408. return;
  409. }
  410. SaveTouchscreenValues();
  411. SaveMotionTouchValues();
  412. EndGroup();
  413. }
  414. void Config::SaveCoreValues() {
  415. BeginGroup(Settings::TranslateCategory(Settings::Category::Core));
  416. WriteCategory(Settings::Category::Core);
  417. EndGroup();
  418. }
  419. void Config::SaveDataStorageValues() {
  420. BeginGroup(Settings::TranslateCategory(Settings::Category::DataStorage));
  421. WriteSetting(std::string("nand_directory"), FS::GetYuzuPathString(FS::YuzuPath::NANDDir),
  422. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::NANDDir)));
  423. WriteSetting(std::string("sdmc_directory"), FS::GetYuzuPathString(FS::YuzuPath::SDMCDir),
  424. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::SDMCDir)));
  425. WriteSetting(std::string("load_directory"), FS::GetYuzuPathString(FS::YuzuPath::LoadDir),
  426. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::LoadDir)));
  427. WriteSetting(std::string("dump_directory"), FS::GetYuzuPathString(FS::YuzuPath::DumpDir),
  428. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::DumpDir)));
  429. WriteSetting(std::string("tas_directory"), FS::GetYuzuPathString(FS::YuzuPath::TASDir),
  430. std::make_optional(FS::GetYuzuPathString(FS::YuzuPath::TASDir)));
  431. WriteCategory(Settings::Category::DataStorage);
  432. EndGroup();
  433. }
  434. void Config::SaveDebuggingValues() {
  435. BeginGroup(Settings::TranslateCategory(Settings::Category::Debugging));
  436. // Intentionally not using the QT default setting as this is intended to be changed in the ini
  437. WriteSetting(std::string("record_frame_times"), Settings::values.record_frame_times);
  438. WriteCategory(Settings::Category::Debugging);
  439. WriteCategory(Settings::Category::DebuggingGraphics);
  440. EndGroup();
  441. }
  442. void Config::SaveNetworkValues() {
  443. BeginGroup(Settings::TranslateCategory(Settings::Category::Services));
  444. WriteCategory(Settings::Category::Network);
  445. EndGroup();
  446. }
  447. void Config::SaveDisabledAddOnValues() {
  448. // Custom config section
  449. BeginGroup(std::string("DisabledAddOns"));
  450. int i = 0;
  451. BeginArray(std::string(""));
  452. for (const auto& elem : Settings::values.disabled_addons) {
  453. SetArrayIndex(i);
  454. WriteSetting(std::string("title_id"), elem.first, std::make_optional(static_cast<u64>(0)));
  455. BeginArray(std::string("disabled"));
  456. for (std::size_t j = 0; j < elem.second.size(); ++j) {
  457. SetArrayIndex(static_cast<int>(j));
  458. WriteSetting(std::string("d"), elem.second[j], std::make_optional(std::string("")));
  459. }
  460. EndArray(); // disabled
  461. ++i;
  462. }
  463. EndArray(); // Base disabled addons array - Has no base key
  464. EndGroup();
  465. }
  466. void Config::SaveMiscellaneousValues() {
  467. BeginGroup(Settings::TranslateCategory(Settings::Category::Miscellaneous));
  468. WriteCategory(Settings::Category::Miscellaneous);
  469. EndGroup();
  470. }
  471. void Config::SaveCpuValues() {
  472. BeginGroup(Settings::TranslateCategory(Settings::Category::Cpu));
  473. WriteCategory(Settings::Category::Cpu);
  474. WriteCategory(Settings::Category::CpuDebug);
  475. WriteCategory(Settings::Category::CpuUnsafe);
  476. EndGroup();
  477. }
  478. void Config::SaveRendererValues() {
  479. BeginGroup(Settings::TranslateCategory(Settings::Category::Renderer));
  480. WriteCategory(Settings::Category::Renderer);
  481. WriteCategory(Settings::Category::RendererAdvanced);
  482. WriteCategory(Settings::Category::RendererDebug);
  483. EndGroup();
  484. }
  485. void Config::SaveScreenshotValues() {
  486. BeginGroup(Settings::TranslateCategory(Settings::Category::Screenshots));
  487. WriteSetting(std::string("screenshot_path"),
  488. FS::GetYuzuPathString(FS::YuzuPath::ScreenshotsDir));
  489. WriteCategory(Settings::Category::Screenshots);
  490. EndGroup();
  491. }
  492. void Config::SaveSystemValues() {
  493. BeginGroup(Settings::TranslateCategory(Settings::Category::System));
  494. WriteCategory(Settings::Category::System);
  495. WriteCategory(Settings::Category::SystemAudio);
  496. EndGroup();
  497. }
  498. void Config::SaveWebServiceValues() {
  499. BeginGroup(Settings::TranslateCategory(Settings::Category::WebService));
  500. WriteCategory(Settings::Category::WebService);
  501. EndGroup();
  502. }
  503. bool Config::ReadBooleanSetting(const std::string& key, const std::optional<bool> default_value) {
  504. std::string full_key = GetFullKey(key, false);
  505. if (!default_value.has_value()) {
  506. return config->GetBoolValue(GetSection().c_str(), full_key.c_str(), false);
  507. }
  508. if (config->GetBoolValue(GetSection().c_str(),
  509. std::string(full_key).append("\\default").c_str(), false)) {
  510. return static_cast<bool>(default_value.value());
  511. } else {
  512. return config->GetBoolValue(GetSection().c_str(), full_key.c_str(),
  513. static_cast<bool>(default_value.value()));
  514. }
  515. }
  516. s64 Config::ReadIntegerSetting(const std::string& key, const std::optional<s64> default_value) {
  517. std::string full_key = GetFullKey(key, false);
  518. if (!default_value.has_value()) {
  519. try {
  520. return std::stoll(
  521. std::string(config->GetValue(GetSection().c_str(), full_key.c_str(), "0")));
  522. } catch (...) {
  523. return 0;
  524. }
  525. }
  526. s64 result = 0;
  527. if (config->GetBoolValue(GetSection().c_str(),
  528. std::string(full_key).append("\\default").c_str(), true)) {
  529. result = default_value.value();
  530. } else {
  531. try {
  532. result = std::stoll(std::string(config->GetValue(
  533. GetSection().c_str(), full_key.c_str(), ToString(default_value.value()).c_str())));
  534. } catch (...) {
  535. result = default_value.value();
  536. }
  537. }
  538. return result;
  539. }
  540. u64 Config::ReadUnsignedIntegerSetting(const std::string& key,
  541. const std::optional<u64> default_value) {
  542. std::string full_key = GetFullKey(key, false);
  543. if (!default_value.has_value()) {
  544. try {
  545. return std::stoull(
  546. std::string(config->GetValue(GetSection().c_str(), full_key.c_str(), "0")));
  547. } catch (...) {
  548. return 0;
  549. }
  550. }
  551. u64 result = 0;
  552. if (config->GetBoolValue(GetSection().c_str(),
  553. std::string(full_key).append("\\default").c_str(), true)) {
  554. result = default_value.value();
  555. } else {
  556. try {
  557. result = std::stoull(std::string(config->GetValue(
  558. GetSection().c_str(), full_key.c_str(), ToString(default_value.value()).c_str())));
  559. } catch (...) {
  560. result = default_value.value();
  561. }
  562. }
  563. return result;
  564. }
  565. double Config::ReadDoubleSetting(const std::string& key,
  566. const std::optional<double> default_value) {
  567. std::string full_key = GetFullKey(key, false);
  568. if (!default_value.has_value()) {
  569. return config->GetDoubleValue(GetSection().c_str(), full_key.c_str(), 0);
  570. }
  571. double result;
  572. if (config->GetBoolValue(GetSection().c_str(),
  573. std::string(full_key).append("\\default").c_str(), true)) {
  574. result = default_value.value();
  575. } else {
  576. result =
  577. config->GetDoubleValue(GetSection().c_str(), full_key.c_str(), default_value.value());
  578. }
  579. return result;
  580. }
  581. std::string Config::ReadStringSetting(const std::string& key,
  582. const std::optional<std::string> default_value) {
  583. std::string result;
  584. std::string full_key = GetFullKey(key, false);
  585. if (!default_value.has_value()) {
  586. result = config->GetValue(GetSection().c_str(), full_key.c_str(), "");
  587. boost::replace_all(result, "\"", "");
  588. return result;
  589. }
  590. if (config->GetBoolValue(GetSection().c_str(),
  591. std::string(full_key).append("\\default").c_str(), true)) {
  592. result = default_value.value();
  593. } else {
  594. result =
  595. config->GetValue(GetSection().c_str(), full_key.c_str(), default_value.value().c_str());
  596. }
  597. boost::replace_all(result, "\"", "");
  598. boost::replace_all(result, "//", "/");
  599. return result;
  600. }
  601. bool Config::Exists(const std::string& section, const std::string& key) const {
  602. const std::string value = config->GetValue(section.c_str(), key.c_str(), "");
  603. return !value.empty();
  604. }
  605. template <typename Type>
  606. void Config::WriteSetting(const std::string& key, const Type& value,
  607. const std::optional<Type>& default_value,
  608. const std::optional<bool>& use_global) {
  609. std::string full_key = GetFullKey(key, false);
  610. std::string saved_value;
  611. std::string string_default;
  612. if constexpr (std::is_same_v<Type, std::string>) {
  613. saved_value.append(AdjustOutputString(value));
  614. if (default_value.has_value()) {
  615. string_default.append(AdjustOutputString(default_value.value()));
  616. }
  617. } else {
  618. saved_value.append(AdjustOutputString(ToString(value)));
  619. if (default_value.has_value()) {
  620. string_default.append(ToString(default_value.value()));
  621. }
  622. }
  623. if (default_value.has_value() && use_global.has_value()) {
  624. if (!global) {
  625. WriteSettingInternal(std::string(full_key).append("\\global"),
  626. ToString(use_global.value()));
  627. }
  628. if (global || use_global.value() == false) {
  629. WriteSettingInternal(std::string(full_key).append("\\default"),
  630. ToString(string_default == saved_value));
  631. WriteSettingInternal(full_key, saved_value);
  632. }
  633. } else if (default_value.has_value() && !use_global.has_value()) {
  634. WriteSettingInternal(std::string(full_key).append("\\default"),
  635. ToString(string_default == saved_value));
  636. WriteSettingInternal(full_key, saved_value);
  637. } else {
  638. WriteSettingInternal(full_key, saved_value);
  639. }
  640. }
  641. void Config::WriteSettingInternal(const std::string& key, const std::string& value) {
  642. config->SetValue(GetSection().c_str(), key.c_str(), value.c_str());
  643. }
  644. void Config::Reload() {
  645. ReadValues();
  646. // To apply default value changes
  647. SaveValues();
  648. }
  649. void Config::Save() {
  650. SaveValues();
  651. }
  652. void Config::ClearControlPlayerValues() const {
  653. // If key is an empty string, all keys in the current group() are removed.
  654. const char* section = Settings::TranslateCategory(Settings::Category::Controls);
  655. CSimpleIniA::TNamesDepend keys;
  656. config->GetAllKeys(section, keys);
  657. for (const auto& key : keys) {
  658. if (std::string(config->GetValue(section, key.pItem)).empty()) {
  659. config->Delete(section, key.pItem);
  660. }
  661. }
  662. }
  663. const std::string& Config::GetConfigFilePath() const {
  664. return config_loc;
  665. }
  666. void Config::ReadCategory(const Settings::Category category) {
  667. const auto& settings = FindRelevantList(category);
  668. std::ranges::for_each(settings, [&](const auto& setting) { ReadSettingGeneric(setting); });
  669. }
  670. void Config::WriteCategory(const Settings::Category category) {
  671. const auto& settings = FindRelevantList(category);
  672. std::ranges::for_each(settings, [&](const auto& setting) { WriteSettingGeneric(setting); });
  673. }
  674. void Config::ReadSettingGeneric(Settings::BasicSetting* const setting) {
  675. if (!setting->Save() || (!setting->Switchable() && !global)) {
  676. return;
  677. }
  678. const std::string key = AdjustKey(setting->GetLabel());
  679. const std::string default_value(setting->DefaultToString());
  680. bool use_global = true;
  681. if (setting->Switchable() && !global) {
  682. use_global =
  683. ReadBooleanSetting(std::string(key).append("\\use_global"), std::make_optional(true));
  684. setting->SetGlobal(use_global);
  685. }
  686. if (global || !use_global) {
  687. const bool is_default =
  688. ReadBooleanSetting(std::string(key).append("\\default"), std::make_optional(true));
  689. if (!is_default) {
  690. const std::string setting_string = ReadStringSetting(key, default_value);
  691. setting->LoadString(setting_string);
  692. } else {
  693. // Empty string resets the Setting to default
  694. setting->LoadString("");
  695. }
  696. }
  697. }
  698. void Config::WriteSettingGeneric(const Settings::BasicSetting* const setting) {
  699. if (!setting->Save()) {
  700. return;
  701. }
  702. std::string key = AdjustKey(setting->GetLabel());
  703. if (setting->Switchable()) {
  704. if (!global) {
  705. WriteSetting(std::string(key).append("\\use_global"), setting->UsingGlobal());
  706. }
  707. if (global || !setting->UsingGlobal()) {
  708. WriteSetting(std::string(key).append("\\default"),
  709. setting->ToString() == setting->DefaultToString());
  710. WriteSetting(key, setting->ToString());
  711. }
  712. } else if (global) {
  713. WriteSetting(std::string(key).append("\\default"),
  714. setting->ToString() == setting->DefaultToString());
  715. WriteSetting(key, setting->ToString());
  716. }
  717. }
  718. void Config::BeginGroup(const std::string& group) {
  719. // You can't begin a group while reading/writing from a config array
  720. ASSERT(array_stack.empty());
  721. key_stack.push_back(AdjustKey(group));
  722. }
  723. void Config::EndGroup() {
  724. // You can't end a group if you haven't started one yet
  725. ASSERT(!key_stack.empty());
  726. // You can't end a group when reading/writing from a config array
  727. ASSERT(array_stack.empty());
  728. key_stack.pop_back();
  729. }
  730. std::string Config::GetSection() {
  731. if (key_stack.empty()) {
  732. return std::string{""};
  733. }
  734. return key_stack.front();
  735. }
  736. std::string Config::GetGroup() const {
  737. if (key_stack.size() <= 1) {
  738. return std::string{""};
  739. }
  740. std::string key;
  741. for (size_t i = 1; i < key_stack.size(); ++i) {
  742. key.append(key_stack[i]).append("\\");
  743. }
  744. return key;
  745. }
  746. std::string Config::AdjustKey(const std::string& key) {
  747. std::string adjusted_key(key);
  748. boost::replace_all(adjusted_key, "/", "\\");
  749. boost::replace_all(adjusted_key, " ", "%20");
  750. return adjusted_key;
  751. }
  752. std::string Config::AdjustOutputString(const std::string& string) {
  753. std::string adjusted_string(string);
  754. boost::replace_all(adjusted_string, "\\", "/");
  755. // Windows requires that two forward slashes are used at the start of a path for unmapped
  756. // network drives so we have to watch for that here
  757. #ifndef ANDROID
  758. if (string.substr(0, 2) == "//") {
  759. boost::replace_all(adjusted_string, "//", "/");
  760. adjusted_string.insert(0, "/");
  761. } else {
  762. boost::replace_all(adjusted_string, "//", "/");
  763. }
  764. #endif
  765. // Needed for backwards compatibility with QSettings deserialization
  766. for (const auto& special_character : special_characters) {
  767. if (adjusted_string.find(special_character) != std::string::npos) {
  768. adjusted_string.insert(0, "\"");
  769. adjusted_string.append("\"");
  770. break;
  771. }
  772. }
  773. return adjusted_string;
  774. }
  775. std::string Config::GetFullKey(const std::string& key, bool skipArrayIndex) {
  776. if (array_stack.empty()) {
  777. return std::string(GetGroup()).append(AdjustKey(key));
  778. }
  779. std::string array_key;
  780. for (size_t i = 0; i < array_stack.size(); ++i) {
  781. if (!array_stack[i].name.empty()) {
  782. array_key.append(array_stack[i].name).append("\\");
  783. }
  784. if (!skipArrayIndex || (array_stack.size() - 1 != i && array_stack.size() > 1)) {
  785. array_key.append(ToString(array_stack[i].index)).append("\\");
  786. }
  787. }
  788. std::string final_key = std::string(GetGroup()).append(array_key).append(AdjustKey(key));
  789. return final_key;
  790. }
  791. int Config::BeginArray(const std::string& array) {
  792. array_stack.push_back(ConfigArray{AdjustKey(array), 0, 0});
  793. const int size = config->GetLongValue(GetSection().c_str(),
  794. GetFullKey(std::string("size"), true).c_str(), 0);
  795. array_stack.back().size = size;
  796. return size;
  797. }
  798. void Config::EndArray() {
  799. // You can't end a config array before starting one
  800. ASSERT(!array_stack.empty());
  801. // Set the array size to 0 if the array is ended without changing the index
  802. int size = 0;
  803. if (array_stack.back().index != 0) {
  804. size = array_stack.back().size;
  805. }
  806. // Write out the size to config
  807. if (key_stack.size() == 1 && array_stack.back().name.empty()) {
  808. // Edge-case where the first array created doesn't have a name
  809. config->SetValue(GetSection().c_str(), std::string("size").c_str(), ToString(size).c_str());
  810. } else {
  811. const auto key = GetFullKey(std::string("size"), true);
  812. config->SetValue(GetSection().c_str(), key.c_str(), ToString(size).c_str());
  813. }
  814. array_stack.pop_back();
  815. }
  816. void Config::SetArrayIndex(const int index) {
  817. // You can't set the array index if you haven't started one yet
  818. ASSERT(!array_stack.empty());
  819. const int array_index = index + 1;
  820. // You can't exceed the known max size of the array by more than 1
  821. ASSERT(array_stack.front().size + 1 >= array_index);
  822. // Change the config array size to the current index since you may want
  823. // to reduce the number of elements that you read back from the config
  824. // in the future.
  825. array_stack.back().size = array_index;
  826. array_stack.back().index = array_index;
  827. }