From 413316560784348b8ea2684d272b974fd0428267 Mon Sep 17 00:00:00 2001
From: lat9nq <22451773+lat9nq@users.noreply.github.com>
Date: Mon, 5 Jun 2023 20:41:50 -0400
Subject: [PATCH] settings,core,config_sys: Remove optional type from
 custom_rtc, rng_seed

core: Fix MSVC errors
---
 src/common/settings.cpp                     |  3 +-
 src/common/settings.h                       |  6 ++--
 src/core/core.cpp                           |  4 ++-
 src/core/hle/kernel/k_process.cpp           |  3 +-
 src/core/hle/service/spl/spl_module.cpp     |  3 +-
 src/yuzu/configuration/configure_system.cpp | 38 ++++++++++-----------
 6 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/src/common/settings.cpp b/src/common/settings.cpp
index e3f30f7e3..696929479 100644
--- a/src/common/settings.cpp
+++ b/src/common/settings.cpp
@@ -62,7 +62,8 @@ void LogSettings() {
 
     LOG_INFO(Config, "yuzu Configuration:");
     log_setting("Controls_UseDockedMode", values.use_docked_mode.GetValue());
-    log_setting("System_RngSeed", values.rng_seed.GetValue().value_or(0));
+    log_setting("System_RngSeedEnabled", values.rng_seed_enabled.GetValue());
+    log_setting("System_RngSeed", values.rng_seed.GetValue());
     log_setting("System_DeviceName", values.device_name.GetValue());
     log_setting("System_CurrentUser", values.current_user.GetValue());
     log_setting("System_LanguageIndex", values.language_index.GetValue());
diff --git a/src/common/settings.h b/src/common/settings.h
index 61d15467d..999f8b5be 100644
--- a/src/common/settings.h
+++ b/src/common/settings.h
@@ -505,10 +505,12 @@ struct Values {
     SwitchableSetting<u8> bg_blue{0, "bg_blue"};
 
     // System
-    SwitchableSetting<std::optional<u32>> rng_seed{std::optional<u32>(), "rng_seed"};
+    SwitchableSetting<bool> rng_seed_enabled{false, "rng_seed_enabled"};
+    SwitchableSetting<u32> rng_seed{0, "rng_seed"};
     Setting<std::string> device_name{"Yuzu", "device_name"};
     // Measured in seconds since epoch
-    std::optional<s64> custom_rtc;
+    SwitchableSetting<bool> custom_rtc_enabled{false, "custom_rtc_enabled"};
+    SwitchableSetting<s64> custom_rtc{0, "custom_rtc"};
     // Set on game boot, reset on stop. Seconds difference between current time and `custom_rtc`
     s64 custom_rtc_differential;
 
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 9e3eb3795..da1baa892 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -149,7 +149,9 @@ struct System::Impl {
         const auto current_time =
             std::chrono::duration_cast<std::chrono::seconds>(posix_time).count();
         Settings::values.custom_rtc_differential =
-            Settings::values.custom_rtc.value_or(current_time) - current_time;
+            (Settings::values.custom_rtc_enabled ? Settings::values.custom_rtc.GetValue()
+                                                 : current_time) -
+            current_time;
 
         // Create a default fs if one doesn't already exist.
         if (virtual_filesystem == nullptr) {
diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp
index efe86ad27..ae064ee04 100644
--- a/src/core/hle/kernel/k_process.cpp
+++ b/src/core/hle/kernel/k_process.cpp
@@ -81,7 +81,8 @@ Result KProcess::Initialize(KProcess* process, Core::System& system, std::string
     process->m_capabilities.InitializeForMetadatalessProcess();
     process->m_is_initialized = true;
 
-    std::mt19937 rng(Settings::values.rng_seed.GetValue().value_or(std::time(nullptr)));
+    std::mt19937 rng(Settings::values.rng_seed_enabled ? Settings::values.rng_seed.GetValue()
+                                                       : static_cast<u32>(std::time(nullptr)));
     std::uniform_int_distribution<u64> distribution;
     std::generate(process->m_random_entropy.begin(), process->m_random_entropy.end(),
                   [&] { return distribution(rng); });
diff --git a/src/core/hle/service/spl/spl_module.cpp b/src/core/hle/service/spl/spl_module.cpp
index 0227d4393..cd631b2ea 100644
--- a/src/core/hle/service/spl/spl_module.cpp
+++ b/src/core/hle/service/spl/spl_module.cpp
@@ -19,7 +19,8 @@ namespace Service::SPL {
 Module::Interface::Interface(Core::System& system_, std::shared_ptr<Module> module_,
                              const char* name)
     : ServiceFramework{system_, name}, module{std::move(module_)},
-      rng(Settings::values.rng_seed.GetValue().value_or(std::time(nullptr))) {}
+      rng(Settings::values.rng_seed_enabled ? Settings::values.rng_seed.GetValue()
+                                            : static_cast<u32>(std::time(nullptr))) {}
 
 Module::Interface::~Interface() = default;
 
diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp
index f1ae312c6..c892635b8 100644
--- a/src/yuzu/configuration/configure_system.cpp
+++ b/src/yuzu/configuration/configure_system.cpp
@@ -95,19 +95,20 @@ void ConfigureSystem::RetranslateUI() {
 
 void ConfigureSystem::SetConfiguration() {
     enabled = !system.IsPoweredOn();
-    const auto rng_seed =
-        QStringLiteral("%1")
-            .arg(Settings::values.rng_seed.GetValue().value_or(0), 8, 16, QLatin1Char{'0'})
-            .toUpper();
-    const auto rtc_time = Settings::values.custom_rtc.value_or(QDateTime::currentSecsSinceEpoch());
+    const auto rng_seed = QStringLiteral("%1")
+                              .arg(Settings::values.rng_seed.GetValue(), 8, 16, QLatin1Char{'0'})
+                              .toUpper();
+    const auto rtc_time = Settings::values.custom_rtc_enabled
+                              ? Settings::values.custom_rtc.GetValue()
+                              : QDateTime::currentSecsSinceEpoch();
 
-    ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.GetValue().has_value());
-    ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.GetValue().has_value() &&
+    ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed_enabled.GetValue());
+    ui->rng_seed_edit->setEnabled(Settings::values.rng_seed_enabled.GetValue() &&
                                   Settings::values.rng_seed.UsingGlobal());
     ui->rng_seed_edit->setText(rng_seed);
 
-    ui->custom_rtc_checkbox->setChecked(Settings::values.custom_rtc.has_value());
-    ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc.has_value());
+    ui->custom_rtc_checkbox->setChecked(Settings::values.custom_rtc_enabled.GetValue());
+    ui->custom_rtc_edit->setEnabled(Settings::values.custom_rtc_enabled.GetValue());
     ui->custom_rtc_edit->setDateTime(QDateTime::fromSecsSinceEpoch(rtc_time));
     ui->device_name_edit->setText(
         QString::fromUtf8(Settings::values.device_name.GetValue().c_str()));
@@ -142,13 +143,15 @@ void ConfigureSystem::ApplyConfiguration() {
     // to allow in-game time to be fast forwarded
     if (Settings::IsConfiguringGlobal()) {
         if (ui->custom_rtc_checkbox->isChecked()) {
+            Settings::values.custom_rtc_enabled = true;
             Settings::values.custom_rtc = ui->custom_rtc_edit->dateTime().toSecsSinceEpoch();
             if (system.IsPoweredOn()) {
-                const s64 posix_time{*Settings::values.custom_rtc};
+                const s64 posix_time{Settings::values.custom_rtc.GetValue() +
+                                     Service::Time::TimeManager::GetExternalTimeZoneOffset()};
                 system.GetTimeManager().UpdateLocalSystemClockTime(posix_time);
             }
         } else {
-            Settings::values.custom_rtc = std::nullopt;
+            Settings::values.custom_rtc_enabled = false;
         }
     }
 
@@ -169,26 +172,23 @@ void ConfigureSystem::ApplyConfiguration() {
     if (Settings::IsConfiguringGlobal()) {
         // Guard if during game and set to game-specific value
         if (Settings::values.rng_seed.UsingGlobal()) {
+            Settings::values.rng_seed_enabled = ui->rng_seed_checkbox->isChecked();
             if (ui->rng_seed_checkbox->isChecked()) {
                 Settings::values.rng_seed.SetValue(ui->rng_seed_edit->text().toUInt(nullptr, 16));
-            } else {
-                Settings::values.rng_seed.SetValue(std::nullopt);
             }
         }
     } else {
         switch (use_rng_seed) {
         case ConfigurationShared::CheckState::On:
         case ConfigurationShared::CheckState::Off:
+            Settings::values.rng_seed_enabled.SetGlobal(false);
             Settings::values.rng_seed.SetGlobal(false);
             if (ui->rng_seed_checkbox->isChecked()) {
                 Settings::values.rng_seed.SetValue(ui->rng_seed_edit->text().toUInt(nullptr, 16));
-            } else {
-                Settings::values.rng_seed.SetValue(std::nullopt);
             }
             break;
         case ConfigurationShared::CheckState::Global:
-            Settings::values.rng_seed.SetGlobal(false);
-            Settings::values.rng_seed.SetValue(std::nullopt);
+            Settings::values.rng_seed_enabled.SetGlobal(true);
             Settings::values.rng_seed.SetGlobal(true);
             break;
         case ConfigurationShared::CheckState::Count:
@@ -217,8 +217,8 @@ void ConfigureSystem::SetupPerGameUI() {
 
     ConfigurationShared::SetColoredTristate(
         ui->rng_seed_checkbox, Settings::values.rng_seed.UsingGlobal(),
-        Settings::values.rng_seed.GetValue().has_value(),
-        Settings::values.rng_seed.GetValue(true).has_value(), use_rng_seed);
+        Settings::values.rng_seed_enabled.GetValue(),
+        Settings::values.rng_seed_enabled.GetValue(true), use_rng_seed);
 
     ConfigurationShared::SetColoredTristate(ui->use_unsafe_extended_memory_layout,
                                             Settings::values.use_unsafe_extended_memory_layout,