configure_system.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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(Core::System& system_, QWidget* parent)
  18. : QWidget(parent), ui{std::make_unique<Ui::ConfigureSystem>()}, system{system_} {
  19. ui->setupUi(this);
  20. connect(ui->button_regenerate_console_id, &QPushButton::clicked, this,
  21. &ConfigureSystem::RefreshConsoleID);
  22. connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  23. ui->rng_seed_edit->setEnabled(state == Qt::Checked);
  24. if (state != Qt::Checked) {
  25. ui->rng_seed_edit->setText(QStringLiteral("00000000"));
  26. }
  27. });
  28. connect(ui->custom_rtc_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
  29. ui->custom_rtc_edit->setEnabled(state == Qt::Checked);
  30. if (state != Qt::Checked) {
  31. ui->custom_rtc_edit->setDateTime(QDateTime::currentDateTime());
  32. }
  33. });
  34. ui->label_console_id->setVisible(Settings::IsConfiguringGlobal());
  35. ui->button_regenerate_console_id->setVisible(Settings::IsConfiguringGlobal());
  36. SetupPerGameUI();
  37. SetConfiguration();
  38. }
  39. ConfigureSystem::~ConfigureSystem() = default;
  40. void ConfigureSystem::changeEvent(QEvent* event) {
  41. if (event->type() == QEvent::LanguageChange) {
  42. RetranslateUI();
  43. }
  44. QWidget::changeEvent(event);
  45. }
  46. void ConfigureSystem::RetranslateUI() {
  47. ui->retranslateUi(this);
  48. }
  49. void ConfigureSystem::SetConfiguration() {
  50. enabled = !system.IsPoweredOn();
  51. const auto rng_seed =
  52. QStringLiteral("%1")
  53. .arg(Settings::values.rng_seed.GetValue().value_or(0), 8, 16, QLatin1Char{'0'})
  54. .toUpper();
  55. const auto rtc_time = Settings::values.custom_rtc.value_or(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));
  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. // Allow setting custom RTC even if system is powered on,
  88. // to allow in-game time to be fast forwarded
  89. if (Settings::IsConfiguringGlobal()) {
  90. if (ui->custom_rtc_checkbox->isChecked()) {
  91. Settings::values.custom_rtc = ui->custom_rtc_edit->dateTime().toSecsSinceEpoch();
  92. if (system.IsPoweredOn()) {
  93. const s64 posix_time{*Settings::values.custom_rtc +
  94. Service::Time::TimeManager::GetExternalTimeZoneOffset()};
  95. system.GetTimeManager().UpdateLocalSystemClockTime(posix_time);
  96. }
  97. } else {
  98. Settings::values.custom_rtc = std::nullopt;
  99. }
  100. }
  101. if (!enabled) {
  102. return;
  103. }
  104. ConfigurationShared::ApplyPerGameSetting(&Settings::values.language_index, ui->combo_language);
  105. ConfigurationShared::ApplyPerGameSetting(&Settings::values.region_index, ui->combo_region);
  106. ConfigurationShared::ApplyPerGameSetting(&Settings::values.time_zone_index,
  107. ui->combo_time_zone);
  108. ConfigurationShared::ApplyPerGameSetting(&Settings::values.sound_index, ui->combo_sound);
  109. if (Settings::IsConfiguringGlobal()) {
  110. // Guard if during game and set to game-specific value
  111. if (Settings::values.rng_seed.UsingGlobal()) {
  112. if (ui->rng_seed_checkbox->isChecked()) {
  113. Settings::values.rng_seed.SetValue(
  114. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  115. } else {
  116. Settings::values.rng_seed.SetValue(std::nullopt);
  117. }
  118. }
  119. } else {
  120. switch (use_rng_seed) {
  121. case ConfigurationShared::CheckState::On:
  122. case ConfigurationShared::CheckState::Off:
  123. Settings::values.rng_seed.SetGlobal(false);
  124. if (ui->rng_seed_checkbox->isChecked()) {
  125. Settings::values.rng_seed.SetValue(
  126. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  127. } else {
  128. Settings::values.rng_seed.SetValue(std::nullopt);
  129. }
  130. break;
  131. case ConfigurationShared::CheckState::Global:
  132. Settings::values.rng_seed.SetGlobal(false);
  133. Settings::values.rng_seed.SetValue(std::nullopt);
  134. Settings::values.rng_seed.SetGlobal(true);
  135. break;
  136. case ConfigurationShared::CheckState::Count:
  137. break;
  138. }
  139. }
  140. }
  141. void ConfigureSystem::RefreshConsoleID() {
  142. QMessageBox::StandardButton reply;
  143. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  144. "Your current virtual Switch will not be recoverable. "
  145. "This might have unexpected effects in games. This might fail, "
  146. "if you use an outdated config savegame. Continue?");
  147. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  148. QMessageBox::No | QMessageBox::Yes);
  149. if (reply == QMessageBox::No) {
  150. return;
  151. }
  152. u64 console_id{};
  153. ui->label_console_id->setText(
  154. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  155. }
  156. void ConfigureSystem::SetupPerGameUI() {
  157. if (Settings::IsConfiguringGlobal()) {
  158. ui->combo_language->setEnabled(Settings::values.language_index.UsingGlobal());
  159. ui->combo_region->setEnabled(Settings::values.region_index.UsingGlobal());
  160. ui->combo_time_zone->setEnabled(Settings::values.time_zone_index.UsingGlobal());
  161. ui->combo_sound->setEnabled(Settings::values.sound_index.UsingGlobal());
  162. ui->rng_seed_checkbox->setEnabled(Settings::values.rng_seed.UsingGlobal());
  163. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.UsingGlobal());
  164. return;
  165. }
  166. ConfigurationShared::SetColoredComboBox(ui->combo_language, ui->label_language,
  167. Settings::values.language_index.GetValue(true));
  168. ConfigurationShared::SetColoredComboBox(ui->combo_region, ui->label_region,
  169. Settings::values.region_index.GetValue(true));
  170. ConfigurationShared::SetColoredComboBox(ui->combo_time_zone, ui->label_timezone,
  171. Settings::values.time_zone_index.GetValue(true));
  172. ConfigurationShared::SetColoredComboBox(ui->combo_sound, ui->label_sound,
  173. Settings::values.sound_index.GetValue(true));
  174. ConfigurationShared::SetColoredTristate(
  175. ui->rng_seed_checkbox, Settings::values.rng_seed.UsingGlobal(),
  176. Settings::values.rng_seed.GetValue().has_value(),
  177. Settings::values.rng_seed.GetValue(true).has_value(), use_rng_seed);
  178. ui->custom_rtc_checkbox->setVisible(false);
  179. ui->custom_rtc_edit->setVisible(false);
  180. }