settings.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu 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(AudioEngine, false);
  29. SETTING(bool, false);
  30. SETTING(int, false);
  31. SETTING(std::string, false);
  32. SETTING(u16, false);
  33. SWITCHABLE(AnisotropyMode, true);
  34. SWITCHABLE(AntiAliasing, false);
  35. SWITCHABLE(AspectRatio, true);
  36. SWITCHABLE(AstcDecodeMode, true);
  37. SWITCHABLE(AstcRecompression, true);
  38. SWITCHABLE(AudioMode, true);
  39. SWITCHABLE(CpuAccuracy, true);
  40. SWITCHABLE(FullscreenMode, true);
  41. SWITCHABLE(GpuAccuracy, true);
  42. SWITCHABLE(Language, true);
  43. SWITCHABLE(NvdecEmulation, false);
  44. SWITCHABLE(Region, true);
  45. SWITCHABLE(RendererBackend, true);
  46. SWITCHABLE(ScalingFilter, false);
  47. SWITCHABLE(ShaderBackend, true);
  48. SWITCHABLE(TimeZone, true);
  49. SETTING(VSyncMode, true);
  50. SWITCHABLE(bool, false);
  51. SWITCHABLE(int, false);
  52. SWITCHABLE(int, true);
  53. SWITCHABLE(s64, false);
  54. SWITCHABLE(u16, true);
  55. SWITCHABLE(u32, false);
  56. SWITCHABLE(u8, false);
  57. SWITCHABLE(u8, true);
  58. #undef SETTING
  59. #undef SWITCHABLE
  60. #endif
  61. Values values;
  62. std::string GetTimeZoneString(TimeZone time_zone) {
  63. const auto time_zone_index = static_cast<std::size_t>(time_zone);
  64. ASSERT(time_zone_index < Common::TimeZone::GetTimeZoneStrings().size());
  65. std::string location_name;
  66. if (time_zone_index == 0) { // Auto
  67. #if __cpp_lib_chrono >= 201907L && !defined(MINGW)
  68. // Disabled for MinGW -- tzdb always returns Etc/UTC
  69. try {
  70. const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb();
  71. const std::chrono::time_zone* current_zone = time_zone_data.current_zone();
  72. std::string_view current_zone_name = current_zone->name();
  73. location_name = current_zone_name;
  74. } catch (std::runtime_error& runtime_error) {
  75. // VCRUNTIME will throw a runtime_error if the operating system's selected time zone
  76. // cannot be found
  77. location_name = Common::TimeZone::FindSystemTimeZone();
  78. LOG_WARNING(Common,
  79. "Error occurred when trying to determine system time zone:\n{}\nFalling "
  80. "back to hour offset \"{}\"",
  81. runtime_error.what(), location_name);
  82. }
  83. #else
  84. location_name = Common::TimeZone::FindSystemTimeZone();
  85. #endif
  86. } else {
  87. location_name = Common::TimeZone::GetTimeZoneStrings()[time_zone_index];
  88. }
  89. return location_name;
  90. }
  91. void LogSettings() {
  92. const auto log_setting = [](std::string_view name, const auto& value) {
  93. LOG_INFO(Config, "{}: {}", name, value);
  94. };
  95. const auto log_path = [](std::string_view name, const std::filesystem::path& path) {
  96. LOG_INFO(Config, "{}: {}", name, Common::FS::PathToUTF8String(path));
  97. };
  98. LOG_INFO(Config, "yuzu Configuration:");
  99. for (auto& [category, settings] : values.linkage.by_category) {
  100. for (const auto& setting : settings) {
  101. if (setting->Id() == values.yuzu_token.Id()) {
  102. // Hide the token secret, for security reasons.
  103. continue;
  104. }
  105. const auto name = fmt::format(
  106. "{:c}{:c} {}.{}", setting->ToString() == setting->DefaultToString() ? '-' : 'M',
  107. setting->UsingGlobal() ? '-' : 'C', TranslateCategory(category),
  108. setting->GetLabel());
  109. log_setting(name, setting->Canonicalize());
  110. }
  111. }
  112. log_path("DataStorage_CacheDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::CacheDir));
  113. log_path("DataStorage_ConfigDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::ConfigDir));
  114. log_path("DataStorage_LoadDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::LoadDir));
  115. log_path("DataStorage_NANDDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir));
  116. log_path("DataStorage_SDMCDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::SDMCDir));
  117. }
  118. bool IsGPULevelExtreme() {
  119. return values.gpu_accuracy.GetValue() == GpuAccuracy::Extreme;
  120. }
  121. bool IsGPULevelHigh() {
  122. return values.gpu_accuracy.GetValue() == GpuAccuracy::Extreme ||
  123. values.gpu_accuracy.GetValue() == GpuAccuracy::High;
  124. }
  125. bool IsFastmemEnabled() {
  126. if (values.cpu_debug_mode) {
  127. return static_cast<bool>(values.cpuopt_fastmem);
  128. }
  129. return true;
  130. }
  131. bool IsDockedMode() {
  132. return values.use_docked_mode.GetValue() == Settings::ConsoleMode::Docked;
  133. }
  134. float Volume() {
  135. if (values.audio_muted) {
  136. return 0.0f;
  137. }
  138. return values.volume.GetValue() / static_cast<f32>(values.volume.GetDefault());
  139. }
  140. const char* TranslateCategory(Category category) {
  141. switch (category) {
  142. case Category::Audio:
  143. return "Audio";
  144. case Category::Core:
  145. return "Core";
  146. case Category::Cpu:
  147. case Category::CpuDebug:
  148. case Category::CpuUnsafe:
  149. return "Cpu";
  150. case Category::Renderer:
  151. case Category::RendererAdvanced:
  152. case Category::RendererDebug:
  153. return "Renderer";
  154. case Category::System:
  155. case Category::SystemAudio:
  156. return "System";
  157. case Category::DataStorage:
  158. return "Data Storage";
  159. case Category::Debugging:
  160. case Category::DebuggingGraphics:
  161. return "Debugging";
  162. case Category::Miscellaneous:
  163. return "Miscellaneous";
  164. case Category::Network:
  165. return "Network";
  166. case Category::WebService:
  167. return "WebService";
  168. case Category::AddOns:
  169. return "DisabledAddOns";
  170. case Category::Controls:
  171. return "Controls";
  172. case Category::Ui:
  173. case Category::UiGeneral:
  174. return "UI";
  175. case Category::UiLayout:
  176. return "UiLayout";
  177. case Category::UiGameList:
  178. return "UiGameList";
  179. case Category::Screenshots:
  180. return "Screenshots";
  181. case Category::Shortcuts:
  182. return "Shortcuts";
  183. case Category::Multiplayer:
  184. return "Multiplayer";
  185. case Category::Services:
  186. return "Services";
  187. case Category::Paths:
  188. return "Paths";
  189. case Category::MaxEnum:
  190. break;
  191. }
  192. return "Miscellaneous";
  193. }
  194. void TranslateResolutionInfo(ResolutionSetup setup, ResolutionScalingInfo& info) {
  195. info.downscale = false;
  196. switch (setup) {
  197. case ResolutionSetup::Res1_2X:
  198. info.up_scale = 1;
  199. info.down_shift = 1;
  200. info.downscale = true;
  201. break;
  202. case ResolutionSetup::Res3_4X:
  203. info.up_scale = 3;
  204. info.down_shift = 2;
  205. info.downscale = true;
  206. break;
  207. case ResolutionSetup::Res1X:
  208. info.up_scale = 1;
  209. info.down_shift = 0;
  210. break;
  211. case ResolutionSetup::Res3_2X:
  212. info.up_scale = 3;
  213. info.down_shift = 1;
  214. break;
  215. case ResolutionSetup::Res2X:
  216. info.up_scale = 2;
  217. info.down_shift = 0;
  218. break;
  219. case ResolutionSetup::Res3X:
  220. info.up_scale = 3;
  221. info.down_shift = 0;
  222. break;
  223. case ResolutionSetup::Res4X:
  224. info.up_scale = 4;
  225. info.down_shift = 0;
  226. break;
  227. case ResolutionSetup::Res5X:
  228. info.up_scale = 5;
  229. info.down_shift = 0;
  230. break;
  231. case ResolutionSetup::Res6X:
  232. info.up_scale = 6;
  233. info.down_shift = 0;
  234. break;
  235. case ResolutionSetup::Res7X:
  236. info.up_scale = 7;
  237. info.down_shift = 0;
  238. break;
  239. case ResolutionSetup::Res8X:
  240. info.up_scale = 8;
  241. info.down_shift = 0;
  242. break;
  243. default:
  244. ASSERT(false);
  245. info.up_scale = 1;
  246. info.down_shift = 0;
  247. break;
  248. }
  249. info.up_factor = static_cast<f32>(info.up_scale) / (1U << info.down_shift);
  250. info.down_factor = static_cast<f32>(1U << info.down_shift) / info.up_scale;
  251. info.active = info.up_scale != 1 || info.down_shift != 0;
  252. }
  253. void UpdateRescalingInfo() {
  254. const auto setup = values.resolution_setup.GetValue();
  255. auto& info = values.resolution_info;
  256. TranslateResolutionInfo(setup, info);
  257. }
  258. void RestoreGlobalState(bool is_powered_on) {
  259. // If a game is running, DO NOT restore the global settings state
  260. if (is_powered_on) {
  261. return;
  262. }
  263. for (const auto& reset : values.linkage.restore_functions) {
  264. reset();
  265. }
  266. }
  267. static bool configuring_global = true;
  268. bool IsConfiguringGlobal() {
  269. return configuring_global;
  270. }
  271. void SetConfiguringGlobal(bool is_global) {
  272. configuring_global = is_global;
  273. }
  274. } // namespace Settings