settings.cpp 7.6 KB

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