settings.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project & 2024 suyu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <version>
  4. #include "common/settings_enums.h"
  5. #if __cpp_lib_chrono >= 201907L
  6. #include <chrono>
  7. #include <exception>
  8. #include <stdexcept>
  9. #endif
  10. #include <compare>
  11. #include <cstddef>
  12. #include <filesystem>
  13. #include <functional>
  14. #include <string_view>
  15. #include <type_traits>
  16. #include <fmt/core.h>
  17. #include "common/assert.h"
  18. #include "common/fs/fs_util.h"
  19. #include "common/fs/path_util.h"
  20. #include "common/logging/log.h"
  21. #include "common/settings.h"
  22. #include "common/time_zone.h"
  23. namespace Settings {
  24. // Clang 14 and earlier have errors when explicitly instantiating these classes
  25. #ifndef CANNOT_EXPLICITLY_INSTANTIATE
  26. #define SETTING(TYPE, RANGED) template class Setting<TYPE, RANGED>
  27. #define SWITCHABLE(TYPE, RANGED) template class SwitchableSetting<TYPE, RANGED>
  28. SETTING(AppletMode, false);
  29. SETTING(AudioEngine, false);
  30. SETTING(bool, false);
  31. SETTING(int, false);
  32. SETTING(std::string, false);
  33. SETTING(u16, false);
  34. SWITCHABLE(AnisotropyMode, true);
  35. SWITCHABLE(AntiAliasing, false);
  36. SWITCHABLE(AspectRatio, true);
  37. SWITCHABLE(AstcDecodeMode, true);
  38. SWITCHABLE(AstcRecompression, true);
  39. SWITCHABLE(AudioMode, true);
  40. SWITCHABLE(CpuBackend, true);
  41. SWITCHABLE(CpuAccuracy, true);
  42. SWITCHABLE(FullscreenMode, true);
  43. SWITCHABLE(GpuAccuracy, true);
  44. SWITCHABLE(Language, true);
  45. SWITCHABLE(MemoryLayout, true);
  46. SWITCHABLE(NvdecEmulation, false);
  47. SWITCHABLE(Region, true);
  48. SWITCHABLE(RendererBackend, true);
  49. SWITCHABLE(ScalingFilter, false);
  50. SWITCHABLE(ShaderBackend, true);
  51. SWITCHABLE(TimeZone, true);
  52. SETTING(VSyncMode, true);
  53. SWITCHABLE(bool, false);
  54. SWITCHABLE(int, false);
  55. SWITCHABLE(int, true);
  56. SWITCHABLE(s64, false);
  57. SWITCHABLE(u16, true);
  58. SWITCHABLE(u32, false);
  59. SWITCHABLE(u8, false);
  60. SWITCHABLE(u8, true);
  61. // Used in UISettings
  62. // TODO see if we can move this to uisettings.cpp
  63. SWITCHABLE(ConfirmStop, true);
  64. #undef SETTING
  65. #undef SWITCHABLE
  66. #endif
  67. Values values;
  68. std::string GetTimeZoneString(TimeZone time_zone) {
  69. const auto time_zone_index = static_cast<std::size_t>(time_zone);
  70. ASSERT(time_zone_index < Common::TimeZone::GetTimeZoneStrings().size());
  71. std::string location_name;
  72. if (time_zone_index == 0) { // Auto
  73. #if __cpp_lib_chrono >= 201907L && !defined(MINGW)
  74. // Disabled for MinGW -- tzdb always returns Etc/UTC
  75. try {
  76. const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb();
  77. const std::chrono::time_zone* current_zone = time_zone_data.current_zone();
  78. std::string_view current_zone_name = current_zone->name();
  79. location_name = current_zone_name;
  80. } catch (std::runtime_error& runtime_error) {
  81. // VCRUNTIME will throw a runtime_error if the operating system's selected time zone
  82. // cannot be found
  83. location_name = Common::TimeZone::FindSystemTimeZone();
  84. LOG_WARNING(Common,
  85. "Error occurred when trying to determine system time zone:\n{}\nFalling "
  86. "back to hour offset \"{}\"",
  87. runtime_error.what(), location_name);
  88. }
  89. #else
  90. location_name = Common::TimeZone::FindSystemTimeZone();
  91. #endif
  92. } else {
  93. location_name = Common::TimeZone::GetTimeZoneStrings()[time_zone_index];
  94. }
  95. return location_name;
  96. }
  97. void LogSettings() {
  98. const auto log_setting = [](std::string_view name, const auto& value) {
  99. LOG_INFO(Config, "{}: {}", name, value);
  100. };
  101. const auto log_path = [](std::string_view name, const std::filesystem::path& path) {
  102. LOG_INFO(Config, "{}: {}", name, Common::FS::PathToUTF8String(path));
  103. };
  104. LOG_INFO(Config, "yuzu Configuration:");
  105. for (auto& [category, settings] : values.linkage.by_category) {
  106. for (const auto& setting : settings) {
  107. if (setting->Id() == values.yuzu_token.Id()) {
  108. // Hide the token secret, for security reasons.
  109. continue;
  110. }
  111. const auto name = fmt::format(
  112. "{:c}{:c} {}.{}", setting->ToString() == setting->DefaultToString() ? '-' : 'M',
  113. setting->UsingGlobal() ? '-' : 'C', TranslateCategory(category),
  114. setting->GetLabel());
  115. log_setting(name, setting->Canonicalize());
  116. }
  117. }
  118. log_path("DataStorage_CacheDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::CacheDir));
  119. log_path("DataStorage_ConfigDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::ConfigDir));
  120. log_path("DataStorage_LoadDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::LoadDir));
  121. log_path("DataStorage_NANDDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::NANDDir));
  122. log_path("DataStorage_SDMCDir", Common::FS::GetSuyuPath(Common::FS::SuyuPath::SDMCDir));
  123. }
  124. void UpdateGPUAccuracy() {
  125. values.current_gpu_accuracy = values.gpu_accuracy.GetValue();
  126. }
  127. bool IsGPULevelExtreme() {
  128. return values.current_gpu_accuracy == GpuAccuracy::Extreme;
  129. }
  130. bool IsGPULevelHigh() {
  131. return values.current_gpu_accuracy == GpuAccuracy::Extreme ||
  132. values.current_gpu_accuracy == GpuAccuracy::High;
  133. }
  134. bool IsFastmemEnabled() {
  135. if (values.cpu_debug_mode) {
  136. return static_cast<bool>(values.cpuopt_fastmem);
  137. }
  138. return true;
  139. }
  140. static bool is_nce_enabled = false;
  141. void SetNceEnabled(bool is_39bit) {
  142. const bool is_nce_selected = values.cpu_backend.GetValue() == CpuBackend::Nce;
  143. if (is_nce_selected && !IsFastmemEnabled()) {
  144. LOG_WARNING(Common, "Fastmem is required to natively execute code in a performant manner, "
  145. "falling back to Dynarmic");
  146. }
  147. if (is_nce_selected && !is_39bit) {
  148. LOG_WARNING(
  149. Common,
  150. "Program does not utilize 39-bit address space, unable to natively execute code");
  151. }
  152. is_nce_enabled = IsFastmemEnabled() && is_nce_selected && is_39bit;
  153. }
  154. bool IsNceEnabled() {
  155. return is_nce_enabled;
  156. }
  157. bool IsDockedMode() {
  158. return values.use_docked_mode.GetValue() == Settings::ConsoleMode::Docked;
  159. }
  160. float Volume() {
  161. if (values.audio_muted) {
  162. return 0.0f;
  163. }
  164. return values.volume.GetValue() / static_cast<f32>(values.volume.GetDefault());
  165. }
  166. const char* TranslateCategory(Category category) {
  167. switch (category) {
  168. case Category::Android:
  169. return "Android";
  170. case Category::Audio:
  171. return "Audio";
  172. case Category::Core:
  173. return "Core";
  174. case Category::Cpu:
  175. case Category::CpuDebug:
  176. case Category::CpuUnsafe:
  177. return "Cpu";
  178. case Category::Overlay:
  179. return "Overlay";
  180. case Category::Renderer:
  181. case Category::RendererAdvanced:
  182. case Category::RendererDebug:
  183. return "Renderer";
  184. case Category::System:
  185. case Category::SystemAudio:
  186. return "System";
  187. case Category::DataStorage:
  188. return "Data Storage";
  189. case Category::Debugging:
  190. case Category::DebuggingGraphics:
  191. return "Debugging";
  192. case Category::GpuDriver:
  193. return "GpuDriver";
  194. case Category::LibraryApplet:
  195. return "LibraryApplet";
  196. case Category::Miscellaneous:
  197. return "Miscellaneous";
  198. case Category::Network:
  199. return "Network";
  200. case Category::WebService:
  201. return "WebService";
  202. case Category::AddOns:
  203. return "DisabledAddOns";
  204. case Category::Controls:
  205. return "Controls";
  206. case Category::Ui:
  207. case Category::UiGeneral:
  208. return "UI";
  209. case Category::UiAudio:
  210. return "UiAudio";
  211. case Category::UiLayout:
  212. return "UILayout";
  213. case Category::UiGameList:
  214. return "UIGameList";
  215. case Category::Screenshots:
  216. return "Screenshots";
  217. case Category::Shortcuts:
  218. return "Shortcuts";
  219. case Category::Multiplayer:
  220. return "Multiplayer";
  221. case Category::Services:
  222. return "Services";
  223. case Category::Paths:
  224. return "Paths";
  225. case Category::Linux:
  226. return "Linux";
  227. case Category::MaxEnum:
  228. break;
  229. }
  230. return "Miscellaneous";
  231. }
  232. void TranslateResolutionInfo(ResolutionSetup setup, ResolutionScalingInfo& info) {
  233. info.downscale = false;
  234. switch (setup) {
  235. case ResolutionSetup::Res1_2X:
  236. info.up_scale = 1;
  237. info.down_shift = 1;
  238. info.downscale = true;
  239. break;
  240. case ResolutionSetup::Res3_4X:
  241. info.up_scale = 3;
  242. info.down_shift = 2;
  243. info.downscale = true;
  244. break;
  245. case ResolutionSetup::Res1X:
  246. info.up_scale = 1;
  247. info.down_shift = 0;
  248. break;
  249. case ResolutionSetup::Res3_2X:
  250. info.up_scale = 3;
  251. info.down_shift = 1;
  252. break;
  253. case ResolutionSetup::Res2X:
  254. info.up_scale = 2;
  255. info.down_shift = 0;
  256. break;
  257. case ResolutionSetup::Res3X:
  258. info.up_scale = 3;
  259. info.down_shift = 0;
  260. break;
  261. case ResolutionSetup::Res4X:
  262. info.up_scale = 4;
  263. info.down_shift = 0;
  264. break;
  265. case ResolutionSetup::Res5X:
  266. info.up_scale = 5;
  267. info.down_shift = 0;
  268. break;
  269. case ResolutionSetup::Res6X:
  270. info.up_scale = 6;
  271. info.down_shift = 0;
  272. break;
  273. case ResolutionSetup::Res7X:
  274. info.up_scale = 7;
  275. info.down_shift = 0;
  276. break;
  277. case ResolutionSetup::Res8X:
  278. info.up_scale = 8;
  279. info.down_shift = 0;
  280. break;
  281. default:
  282. ASSERT(false);
  283. info.up_scale = 1;
  284. info.down_shift = 0;
  285. break;
  286. }
  287. info.up_factor = static_cast<f32>(info.up_scale) / (1U << info.down_shift);
  288. info.down_factor = static_cast<f32>(1U << info.down_shift) / info.up_scale;
  289. info.active = info.up_scale != 1 || info.down_shift != 0;
  290. }
  291. void UpdateRescalingInfo() {
  292. const auto setup = values.resolution_setup.GetValue();
  293. auto& info = values.resolution_info;
  294. TranslateResolutionInfo(setup, info);
  295. }
  296. void RestoreGlobalState(bool is_powered_on) {
  297. // If a game is running, DO NOT restore the global settings state
  298. if (is_powered_on) {
  299. return;
  300. }
  301. for (const auto& reset : values.linkage.restore_functions) {
  302. reset();
  303. }
  304. }
  305. static bool configuring_global = true;
  306. bool IsConfiguringGlobal() {
  307. return configuring_global;
  308. }
  309. void SetConfiguringGlobal(bool is_global) {
  310. configuring_global = is_global;
  311. }
  312. } // namespace Settings