configure_system.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/file_util.h"
  12. #include "core/core.h"
  13. #include "core/settings.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::configuring_global);
  34. ui->button_regenerate_console_id->setVisible(Settings::configuring_global);
  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.GetValue().value_or(
  55. std::chrono::seconds(QDateTime::currentSecsSinceEpoch()));
  56. if (Settings::configuring_global) {
  57. ui->combo_language->setCurrentIndex(Settings::values.language_index.GetValue());
  58. ui->combo_region->setCurrentIndex(Settings::values.region_index.GetValue());
  59. ui->combo_time_zone->setCurrentIndex(Settings::values.time_zone_index.GetValue());
  60. ui->combo_sound->setCurrentIndex(Settings::values.sound_index.GetValue());
  61. ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.GetValue().has_value());
  62. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.GetValue().has_value() &&
  63. Settings::values.rng_seed.UsingGlobal());
  64. ui->rng_seed_edit->setText(rng_seed);
  65. ui->custom_rtc_checkbox->setChecked(Settings::values.custom_rtc.GetValue().has_value());
  66. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.GetValue().has_value() &&
  67. Settings::values.rng_seed.UsingGlobal());
  68. ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time.count()));
  69. } else {
  70. ConfigurationShared::SetPerGameSetting(ui->combo_language,
  71. &Settings::values.language_index);
  72. ConfigurationShared::SetPerGameSetting(ui->combo_region, &Settings::values.region_index);
  73. ConfigurationShared::SetPerGameSetting(ui->combo_time_zone,
  74. &Settings::values.time_zone_index);
  75. ConfigurationShared::SetPerGameSetting(ui->combo_sound, &Settings::values.sound_index);
  76. if (Settings::values.rng_seed.UsingGlobal()) {
  77. ui->rng_seed_checkbox->setCheckState(Qt::PartiallyChecked);
  78. } else {
  79. ui->rng_seed_checkbox->setCheckState(
  80. Settings::values.rng_seed.GetValue().has_value() ? Qt::Checked : Qt::Unchecked);
  81. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.GetValue().has_value());
  82. if (Settings::values.rng_seed.GetValue().has_value()) {
  83. ui->rng_seed_edit->setText(rng_seed);
  84. }
  85. }
  86. if (Settings::values.custom_rtc.UsingGlobal()) {
  87. ui->custom_rtc_checkbox->setCheckState(Qt::PartiallyChecked);
  88. } else {
  89. ui->custom_rtc_checkbox->setCheckState(
  90. Settings::values.custom_rtc.GetValue().has_value() ? Qt::Checked : Qt::Unchecked);
  91. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.GetValue().has_value());
  92. if (Settings::values.custom_rtc.GetValue().has_value()) {
  93. ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time.count()));
  94. }
  95. }
  96. }
  97. }
  98. void ConfigureSystem::ReadSystemSettings() {}
  99. void ConfigureSystem::ApplyConfiguration() {
  100. if (!enabled) {
  101. return;
  102. }
  103. if (Settings::configuring_global) {
  104. // Guard if during game and set to game-specific value
  105. if (Settings::values.language_index.UsingGlobal()) {
  106. Settings::values.language_index.SetValue(ui->combo_language->currentIndex());
  107. }
  108. if (Settings::values.region_index.UsingGlobal()) {
  109. Settings::values.region_index.SetValue(ui->combo_region->currentIndex());
  110. }
  111. if (Settings::values.time_zone_index.UsingGlobal()) {
  112. Settings::values.time_zone_index.SetValue(ui->combo_time_zone->currentIndex());
  113. }
  114. if (Settings::values.sound_index.UsingGlobal()) {
  115. Settings::values.sound_index.SetValue(ui->combo_sound->currentIndex());
  116. }
  117. if (Settings::values.rng_seed.UsingGlobal()) {
  118. if (ui->rng_seed_checkbox->isChecked()) {
  119. Settings::values.rng_seed.SetValue(
  120. ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  121. } else {
  122. Settings::values.rng_seed.SetValue(std::nullopt);
  123. }
  124. }
  125. if (Settings::values.custom_rtc.UsingGlobal()) {
  126. if (ui->custom_rtc_checkbox->isChecked()) {
  127. Settings::values.custom_rtc.SetValue(
  128. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  129. } else {
  130. Settings::values.custom_rtc.SetValue(std::nullopt);
  131. }
  132. }
  133. } else {
  134. ConfigurationShared::ApplyPerGameSetting(&Settings::values.language_index,
  135. ui->combo_language);
  136. ConfigurationShared::ApplyPerGameSetting(&Settings::values.region_index, ui->combo_region);
  137. ConfigurationShared::ApplyPerGameSetting(&Settings::values.time_zone_index,
  138. ui->combo_time_zone);
  139. ConfigurationShared::ApplyPerGameSetting(&Settings::values.sound_index, ui->combo_sound);
  140. switch (ui->rng_seed_checkbox->checkState()) {
  141. case Qt::Checked:
  142. Settings::values.rng_seed.SetGlobal(false);
  143. Settings::values.rng_seed.SetValue(ui->rng_seed_edit->text().toULongLong(nullptr, 16));
  144. break;
  145. case Qt::Unchecked:
  146. Settings::values.rng_seed.SetGlobal(false);
  147. Settings::values.rng_seed.SetValue(std::nullopt);
  148. break;
  149. case Qt::PartiallyChecked:
  150. Settings::values.rng_seed.SetGlobal(false);
  151. Settings::values.rng_seed.SetValue(std::nullopt);
  152. Settings::values.rng_seed.SetGlobal(true);
  153. break;
  154. }
  155. switch (ui->custom_rtc_checkbox->checkState()) {
  156. case Qt::Checked:
  157. Settings::values.custom_rtc.SetGlobal(false);
  158. Settings::values.custom_rtc.SetValue(
  159. std::chrono::seconds(ui->custom_rtc_edit->dateTime().toSecsSinceEpoch()));
  160. break;
  161. case Qt::Unchecked:
  162. Settings::values.custom_rtc.SetGlobal(false);
  163. Settings::values.custom_rtc.SetValue(std::nullopt);
  164. break;
  165. case Qt::PartiallyChecked:
  166. Settings::values.custom_rtc.SetGlobal(false);
  167. Settings::values.custom_rtc.SetValue(std::nullopt);
  168. Settings::values.custom_rtc.SetGlobal(true);
  169. break;
  170. }
  171. }
  172. Settings::Apply();
  173. }
  174. void ConfigureSystem::RefreshConsoleID() {
  175. QMessageBox::StandardButton reply;
  176. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  177. "Your current virtual Switch will not be recoverable. "
  178. "This might have unexpected effects in games. This might fail, "
  179. "if you use an outdated config savegame. Continue?");
  180. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  181. QMessageBox::No | QMessageBox::Yes);
  182. if (reply == QMessageBox::No) {
  183. return;
  184. }
  185. u64 console_id{};
  186. ui->label_console_id->setText(
  187. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  188. }
  189. void ConfigureSystem::SetupPerGameUI() {
  190. if (Settings::configuring_global) {
  191. ui->combo_language->setEnabled(Settings::values.language_index.UsingGlobal());
  192. ui->combo_region->setEnabled(Settings::values.region_index.UsingGlobal());
  193. ui->combo_time_zone->setEnabled(Settings::values.time_zone_index.UsingGlobal());
  194. ui->combo_sound->setEnabled(Settings::values.sound_index.UsingGlobal());
  195. ui->rng_seed_checkbox->setEnabled(Settings::values.rng_seed.UsingGlobal());
  196. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.UsingGlobal());
  197. ui->custom_rtc_checkbox->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  198. ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.UsingGlobal());
  199. return;
  200. }
  201. ConfigurationShared::InsertGlobalItem(ui->combo_language);
  202. ConfigurationShared::InsertGlobalItem(ui->combo_region);
  203. ConfigurationShared::InsertGlobalItem(ui->combo_time_zone);
  204. ConfigurationShared::InsertGlobalItem(ui->combo_sound);
  205. ui->rng_seed_checkbox->setTristate(true);
  206. ui->custom_rtc_checkbox->setTristate(true);
  207. }