config.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <SDL.h>
  6. #include <inih/cpp/INIReader.h>
  7. #include "common/file_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/param_package.h"
  10. #include "core/hle/service/acc/profile_manager.h"
  11. #include "core/settings.h"
  12. #include "input_common/main.h"
  13. #include "yuzu_cmd/config.h"
  14. #include "yuzu_cmd/default_ini.h"
  15. Config::Config() {
  16. // TODO: Don't hardcode the path; let the frontend decide where to put the config files.
  17. sdl2_config_loc = FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "sdl2-config.ini";
  18. sdl2_config = std::make_unique<INIReader>(sdl2_config_loc);
  19. Reload();
  20. }
  21. Config::~Config() = default;
  22. bool Config::LoadINI(const std::string& default_contents, bool retry) {
  23. const char* location = this->sdl2_config_loc.c_str();
  24. if (sdl2_config->ParseError() < 0) {
  25. if (retry) {
  26. LOG_WARNING(Config, "Failed to load {}. Creating file from defaults...", location);
  27. FileUtil::CreateFullPath(location);
  28. FileUtil::WriteStringToFile(true, default_contents, location);
  29. sdl2_config = std::make_unique<INIReader>(location); // Reopen file
  30. return LoadINI(default_contents, false);
  31. }
  32. LOG_ERROR(Config, "Failed.");
  33. return false;
  34. }
  35. LOG_INFO(Config, "Successfully loaded {}", location);
  36. return true;
  37. }
  38. static const std::array<int, Settings::NativeButton::NumButtons> default_buttons = {
  39. SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_T,
  40. SDL_SCANCODE_G, SDL_SCANCODE_F, SDL_SCANCODE_H, SDL_SCANCODE_Q, SDL_SCANCODE_W,
  41. SDL_SCANCODE_M, SDL_SCANCODE_N, SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_B,
  42. };
  43. static const std::array<std::array<int, 5>, Settings::NativeAnalog::NumAnalogs> default_analogs{{
  44. {
  45. SDL_SCANCODE_UP,
  46. SDL_SCANCODE_DOWN,
  47. SDL_SCANCODE_LEFT,
  48. SDL_SCANCODE_RIGHT,
  49. SDL_SCANCODE_D,
  50. },
  51. {
  52. SDL_SCANCODE_I,
  53. SDL_SCANCODE_K,
  54. SDL_SCANCODE_J,
  55. SDL_SCANCODE_L,
  56. SDL_SCANCODE_D,
  57. },
  58. }};
  59. void Config::ReadValues() {
  60. // Controls
  61. for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
  62. std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]);
  63. Settings::values.buttons[i] =
  64. sdl2_config->Get("Controls", Settings::NativeButton::mapping[i], default_param);
  65. if (Settings::values.buttons[i].empty())
  66. Settings::values.buttons[i] = default_param;
  67. }
  68. for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
  69. std::string default_param = InputCommon::GenerateAnalogParamFromKeys(
  70. default_analogs[i][0], default_analogs[i][1], default_analogs[i][2],
  71. default_analogs[i][3], default_analogs[i][4], 0.5f);
  72. Settings::values.analogs[i] =
  73. sdl2_config->Get("Controls", Settings::NativeAnalog::mapping[i], default_param);
  74. if (Settings::values.analogs[i].empty())
  75. Settings::values.analogs[i] = default_param;
  76. }
  77. Settings::values.motion_device = sdl2_config->Get(
  78. "Controls", "motion_device", "engine:motion_emu,update_period:100,sensitivity:0.01");
  79. Settings::values.touch_device =
  80. sdl2_config->Get("Controls", "touch_device", "engine:emu_window");
  81. // Core
  82. Settings::values.use_cpu_jit = sdl2_config->GetBoolean("Core", "use_cpu_jit", true);
  83. Settings::values.use_multi_core = sdl2_config->GetBoolean("Core", "use_multi_core", false);
  84. // Renderer
  85. Settings::values.resolution_factor =
  86. (float)sdl2_config->GetReal("Renderer", "resolution_factor", 1.0);
  87. Settings::values.use_frame_limit = sdl2_config->GetBoolean("Renderer", "use_frame_limit", true);
  88. Settings::values.frame_limit =
  89. static_cast<u16>(sdl2_config->GetInteger("Renderer", "frame_limit", 100));
  90. Settings::values.use_accurate_gpu_emulation =
  91. sdl2_config->GetBoolean("Renderer", "use_accurate_gpu_emulation", false);
  92. Settings::values.bg_red = (float)sdl2_config->GetReal("Renderer", "bg_red", 0.0);
  93. Settings::values.bg_green = (float)sdl2_config->GetReal("Renderer", "bg_green", 0.0);
  94. Settings::values.bg_blue = (float)sdl2_config->GetReal("Renderer", "bg_blue", 0.0);
  95. // Audio
  96. Settings::values.sink_id = sdl2_config->Get("Audio", "output_engine", "auto");
  97. Settings::values.enable_audio_stretching =
  98. sdl2_config->GetBoolean("Audio", "enable_audio_stretching", true);
  99. Settings::values.audio_device_id = sdl2_config->Get("Audio", "output_device", "auto");
  100. Settings::values.volume = sdl2_config->GetReal("Audio", "volume", 1);
  101. // Data Storage
  102. Settings::values.use_virtual_sd =
  103. sdl2_config->GetBoolean("Data Storage", "use_virtual_sd", true);
  104. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir,
  105. sdl2_config->Get("Data Storage", "nand_directory",
  106. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir)));
  107. FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir,
  108. sdl2_config->Get("Data Storage", "sdmc_directory",
  109. FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir)));
  110. // System
  111. Settings::values.use_docked_mode = sdl2_config->GetBoolean("System", "use_docked_mode", false);
  112. Settings::values.enable_nfc = sdl2_config->GetBoolean("System", "enable_nfc", true);
  113. const auto size = sdl2_config->GetInteger("System", "users_size", 0);
  114. Settings::values.current_user = std::clamp<int>(
  115. sdl2_config->GetInteger("System", "current_user", 0), 0, Service::Account::MAX_USERS - 1);
  116. const auto enabled = sdl2_config->GetBoolean("System", "rng_seed_enabled", false);
  117. if (enabled) {
  118. Settings::values.rng_seed = sdl2_config->GetInteger("System", "rng_seed", 0);
  119. } else {
  120. Settings::values.rng_seed = std::nullopt;
  121. }
  122. // Miscellaneous
  123. Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace");
  124. Settings::values.use_dev_keys = sdl2_config->GetBoolean("Miscellaneous", "use_dev_keys", false);
  125. // Debugging
  126. Settings::values.use_gdbstub = sdl2_config->GetBoolean("Debugging", "use_gdbstub", false);
  127. Settings::values.gdbstub_port =
  128. static_cast<u16>(sdl2_config->GetInteger("Debugging", "gdbstub_port", 24689));
  129. Settings::values.program_args = sdl2_config->Get("Debugging", "program_args", "");
  130. // Web Service
  131. Settings::values.enable_telemetry =
  132. sdl2_config->GetBoolean("WebService", "enable_telemetry", true);
  133. Settings::values.web_api_url =
  134. sdl2_config->Get("WebService", "web_api_url", "https://api.yuzu-emu.org");
  135. Settings::values.yuzu_username = sdl2_config->Get("WebService", "yuzu_username", "");
  136. Settings::values.yuzu_token = sdl2_config->Get("WebService", "yuzu_token", "");
  137. }
  138. void Config::Reload() {
  139. LoadINI(DefaultINI::sdl2_config_file);
  140. ReadValues();
  141. }