settings.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <forward_list>
  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. #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. settings.sort([](const BasicSetting* a, const BasicSetting* b) {
  99. return a->GetLabel() < b->GetLabel();
  100. });
  101. for (const auto& setting : settings) {
  102. if (setting->Id() == values.yuzu_token.Id()) {
  103. // Hide the token secret, which could be used to share patreon builds
  104. continue;
  105. }
  106. std::string name = fmt::format(
  107. "{:c}{:c} {}.{}", setting->ToString() == setting->DefaultToString() ? '-' : 'M',
  108. setting->UsingGlobal() ? '-' : 'C', TranslateCategory(category),
  109. setting->GetLabel());
  110. log_setting(name, setting->Canonicalize());
  111. }
  112. }
  113. log_path("DataStorage_CacheDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::CacheDir));
  114. log_path("DataStorage_ConfigDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::ConfigDir));
  115. log_path("DataStorage_LoadDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::LoadDir));
  116. log_path("DataStorage_NANDDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir));
  117. log_path("DataStorage_SDMCDir", Common::FS::GetYuzuPath(Common::FS::YuzuPath::SDMCDir));
  118. }
  119. bool IsGPULevelExtreme() {
  120. return values.gpu_accuracy.GetValue() == GpuAccuracy::Extreme;
  121. }
  122. bool IsGPULevelHigh() {
  123. return values.gpu_accuracy.GetValue() == GpuAccuracy::Extreme ||
  124. values.gpu_accuracy.GetValue() == GpuAccuracy::High;
  125. }
  126. bool IsFastmemEnabled() {
  127. if (values.cpu_debug_mode) {
  128. return static_cast<bool>(values.cpuopt_fastmem);
  129. }
  130. return true;
  131. }
  132. float Volume() {
  133. if (values.audio_muted) {
  134. return 0.0f;
  135. }
  136. return values.volume.GetValue() / static_cast<f32>(values.volume.GetDefault());
  137. }
  138. Linkage::Linkage(u32 initial_count) : count{initial_count} {}
  139. Linkage::~Linkage() = default;
  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 UpdateRescalingInfo() {
  195. const auto setup = values.resolution_setup.GetValue();
  196. auto& info = values.resolution_info;
  197. info.downscale = false;
  198. switch (setup) {
  199. case ResolutionSetup::Res1_2X:
  200. info.up_scale = 1;
  201. info.down_shift = 1;
  202. info.downscale = true;
  203. break;
  204. case ResolutionSetup::Res3_4X:
  205. info.up_scale = 3;
  206. info.down_shift = 2;
  207. info.downscale = true;
  208. break;
  209. case ResolutionSetup::Res1X:
  210. info.up_scale = 1;
  211. info.down_shift = 0;
  212. break;
  213. case ResolutionSetup::Res3_2X:
  214. info.up_scale = 3;
  215. info.down_shift = 1;
  216. break;
  217. case ResolutionSetup::Res2X:
  218. info.up_scale = 2;
  219. info.down_shift = 0;
  220. break;
  221. case ResolutionSetup::Res3X:
  222. info.up_scale = 3;
  223. info.down_shift = 0;
  224. break;
  225. case ResolutionSetup::Res4X:
  226. info.up_scale = 4;
  227. info.down_shift = 0;
  228. break;
  229. case ResolutionSetup::Res5X:
  230. info.up_scale = 5;
  231. info.down_shift = 0;
  232. break;
  233. case ResolutionSetup::Res6X:
  234. info.up_scale = 6;
  235. info.down_shift = 0;
  236. break;
  237. case ResolutionSetup::Res7X:
  238. info.up_scale = 7;
  239. info.down_shift = 0;
  240. break;
  241. case ResolutionSetup::Res8X:
  242. info.up_scale = 8;
  243. info.down_shift = 0;
  244. break;
  245. default:
  246. ASSERT(false);
  247. info.up_scale = 1;
  248. info.down_shift = 0;
  249. break;
  250. }
  251. info.up_factor = static_cast<f32>(info.up_scale) / (1U << info.down_shift);
  252. info.down_factor = static_cast<f32>(1U << info.down_shift) / info.up_scale;
  253. info.active = info.up_scale != 1 || info.down_shift != 0;
  254. }
  255. void RestoreGlobalState(bool is_powered_on) {
  256. // If a game is running, DO NOT restore the global settings state
  257. if (is_powered_on) {
  258. return;
  259. }
  260. for (const auto& reset : values.linkage.restore_functions) {
  261. reset();
  262. }
  263. }
  264. } // namespace Settings