configure_system.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <chrono>
  6. #include <optional>
  7. #include <QFileDialog>
  8. #include <QGraphicsItem>
  9. #include <QMessageBox>
  10. #include "common/assert.h"
  11. #include "common/settings.h"
  12. #include "core/core.h"
  13. #include "core/hle/service/time/time_manager.h"
  14. #include "ui_configure_system.h"
  15. #include "yuzu/configuration/configuration_shared.h"
  16. #include "yuzu/configuration/configure_system.h"
  17. ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureSystem) {
  18. ui->setupUi(this);
  19. connect(ui->button_regenerate_console_id, &QPushButton::clicked, this,
  20. &ConfigureSystem::RefreshConsoleID);
  21. connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  22. ui->rng_seed_edit->setEnabled(state == Qt::Checked);
  23. if (state != Qt::Checked) {
  24. ui->rng_seed_edit->setText(QStringLiteral("00000000"));
  25. }
  26. });
  27. connect(ui->custom_rtc_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  28. ui->custom_rtc_edit->setEnabled(state == Qt::Checked);
  29. if (state != Qt::Checked) {
  30. ui->custom_rtc_edit->setDateTime(QDateTime::currentDateTime());
  31. }
  32. });
  33. ui->label_console_id->setVisible(Settings::IsConfiguringGlobal());
  34. ui->button_regenerate_console_id->setVisible(Settings::IsConfiguringGlobal());
  35. SetupPerGameUI();
  36. SetConfiguration();
  37. }
  38. ConfigureSystem::~ConfigureSystem() = default;
  39. void ConfigureSystem::changeEvent(QEvent* event) {
  40. if (event->type() == QEvent::LanguageChange) {
  41. RetranslateUI();
  42. }
  43. QWidget::changeEvent(event);
  44. }
  45. void ConfigureSystem::RetranslateUI() {
  46. ui->retranslateUi(this);
  47. }
  48. void ConfigureSystem::SetConfiguration() {
  49. enabled = !Core::System::GetInstance().IsPoweredOn();
  50. const auto rng_seed =
  51. QStringLiteral("%1")
  52. .arg(Settings::values.rng_seed.GetValue().value_or(0), 8, 16, QLatin1Char{'0'})
  53. .toUpper();
  54. const auto rtc_time = Settings::values.custom_rtc.value_or(
  55. std::chrono::seconds(QDateTime::currentSecsSinceEpoch()));
  56. ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.GetValue().has_value());
  57. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.GetValue().has_value() &&
  58. Settings::values.rng_seed.UsingGlobal());
  59. ui->rng_seed_edit->setText(rng_seed);
  60. ui->custom_rtc_checkbox->setChecked(Settings::values.custom_rtc.has_value());
  61. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.has_value());
  62. ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time.count()));
  63. if (Settings::IsConfiguringGlobal()) {
  64. ui->combo_language->setCurrentIndex(Settings::values.language_index.GetValue());
  65. ui->combo_region->setCurrentIndex(Settings::values.region_index.GetValue());
  66. ui->combo_time_zone->setCurrentIndex(Settings::values.time_zone_index.GetValue());
  67. ui->combo_sound->setCurrentIndex(Settings::values.sound_index.GetValue());
  68. } else {
  69. ConfigurationShared::SetPerGameSetting(ui->combo_language,
  70. &Settings::values.language_index);
  71. ConfigurationShared::SetPerGameSetting(ui->combo_region, &Settings::values.region_index);
  72. ConfigurationShared::SetPerGameSetting(ui->combo_time_zone,
  73. &Settings::values.time_zone_index);
  74. ConfigurationShared::SetPerGameSetting(ui->combo_sound, &Settings::values.sound_index);
  75. ConfigurationShared::SetHighlight(ui->label_language,
  76. !Settings::values.language_index.UsingGlobal());
  77. ConfigurationShared::SetHighlight(ui->label_region,
  78. !Settings::values.region_index.UsingGlobal());
  79. ConfigurationShared::SetHighlight(ui->label_timezone,
  80. !Settings::values.time_zone_index.UsingGlobal());
  81. ConfigurationShared::SetHighlight(ui->label_sound,
  82. !Settings::values.sound_index.UsingGlobal());
  83. }
  84. }
  85. void ConfigureSystem::ReadSystemSettings() {}
  86. void ConfigureSystem::ApplyConfiguration() {
  87. auto& system = Core::System::GetInstance();
  88. // Allow setting custom RTC even if system is powered on,
  89. // to allow in-game time to be fast forwarded
  90. if (Settings::IsConfiguringGlobal()) {
  91. if (ui->custom_rtc_checkbox->isChecked()) {
  92. Settings::values.custom_rtc =
  93. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch());
  94. if (system.IsPoweredOn()) {
  95. const s64 posix_time{Settings::values.custom_rtc->count() +
  96. Service::Time::TimeManager::GetExternalTimeZoneOffset()};
  97. system.GetTimeManager().UpdateLocalSystemClockTime(posix_time);
  98. }
  99. } else {
  100. Settings::values.custom_rtc = std::nullopt;
  101. }
  102. }
  103. if (!enabled) {
  104. return;
  105. }
  106. ConfigurationShared::ApplyPerGameSetting(&Settings::values.language_index, ui->combo_language);
  107. ConfigurationShared::ApplyPerGameSetting(&Settings::values.region_index, ui->combo_region);
  108. ConfigurationShared::ApplyPerGameSetting(&Settings::values.time_zone_index,
  109. ui->combo_time_zone);
  110. ConfigurationShared::ApplyPerGameSetting(&Settings::values.sound_index, ui->combo_sound);
  111. if (Settings::IsConfiguringGlobal()) {
  112. // Guard if during game and set to game-specific value
  113. if (Settings::values.rng_seed.UsingGlobal()) {
  114. if (ui->rng_seed_checkbox->isChecked()) {
  115. Settings::values.rng_seed.SetValue(
  116. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  117. } else {
  118. Settings::values.rng_seed.SetValue(std::nullopt);
  119. }
  120. }
  121. } else {
  122. switch (use_rng_seed) {
  123. case ConfigurationShared::CheckState::On:
  124. case ConfigurationShared::CheckState::Off:
  125. Settings::values.rng_seed.SetGlobal(false);
  126. if (ui->rng_seed_checkbox->isChecked()) {
  127. Settings::values.rng_seed.SetValue(
  128. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  129. } else {
  130. Settings::values.rng_seed.SetValue(std::nullopt);
  131. }
  132. break;
  133. case ConfigurationShared::CheckState::Global:
  134. Settings::values.rng_seed.SetGlobal(false);
  135. Settings::values.rng_seed.SetValue(std::nullopt);
  136. Settings::values.rng_seed.SetGlobal(true);
  137. break;
  138. case ConfigurationShared::CheckState::Count:
  139. break;
  140. }
  141. }
  142. system.ApplySettings();
  143. }
  144. void ConfigureSystem::RefreshConsoleID() {
  145. QMessageBox::StandardButton reply;
  146. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  147. "Your current virtual Switch will not be recoverable. "
  148. "This might have unexpected effects in games. This might fail, "
  149. "if you use an outdated config savegame. Continue?");
  150. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  151. QMessageBox::No | QMessageBox::Yes);
  152. if (reply == QMessageBox::No) {
  153. return;
  154. }
  155. u64 console_id{};
  156. ui->label_console_id->setText(
  157. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  158. }
  159. void ConfigureSystem::SetupPerGameUI() {
  160. if (Settings::IsConfiguringGlobal()) {
  161. ui->combo_language->setEnabled(Settings::values.language_index.UsingGlobal());
  162. ui->combo_region->setEnabled(Settings::values.region_index.UsingGlobal());
  163. ui->combo_time_zone->setEnabled(Settings::values.time_zone_index.UsingGlobal());
  164. ui->combo_sound->setEnabled(Settings::values.sound_index.UsingGlobal());
  165. ui->rng_seed_checkbox->setEnabled(Settings::values.rng_seed.UsingGlobal());
  166. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.UsingGlobal());
  167. return;
  168. }
  169. ConfigurationShared::SetColoredComboBox(ui->combo_language, ui->label_language,
  170. Settings::values.language_index.GetValue(true));
  171. ConfigurationShared::SetColoredComboBox(ui->combo_region, ui->label_region,
  172. Settings::values.region_index.GetValue(true));
  173. ConfigurationShared::SetColoredComboBox(ui->combo_time_zone, ui->label_timezone,
  174. Settings::values.time_zone_index.GetValue(true));
  175. ConfigurationShared::SetColoredComboBox(ui->combo_sound, ui->label_sound,
  176. Settings::values.sound_index.GetValue(true));
  177. ConfigurationShared::SetColoredTristate(
  178. ui->rng_seed_checkbox, Settings::values.rng_seed.UsingGlobal(),
  179. Settings::values.rng_seed.GetValue().has_value(),
  180. Settings::values.rng_seed.GetValue(true).has_value(), use_rng_seed);
  181. ui->custom_rtc_checkbox->setVisible(false);
  182. ui->custom_rtc_edit->setVisible(false);
  183. }