settings.cpp 8.7 KB

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