summaryrefslogtreecommitdiffstats
path: root/src/yuzu/configuration/configure_system.cpp
blob: 4872a475b62a3d99703b0cdf99e075f12bfeb3a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-FileCopyrightText: 2016 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <chrono>
#include <optional>

#include <QFileDialog>
#include <QGraphicsItem>
#include <QMessageBox>
#include "common/settings.h"
#include "core/core.h"
#include "core/hle/service/time/time_manager.h"
#include "ui_configure_system.h"
#include "yuzu/configuration/configuration_shared.h"
#include "yuzu/configuration/configure_system.h"

constexpr std::array<u32, 7> LOCALE_BLOCKLIST{
    // pzzefezrpnkzeidfej
    // thhsrnhutlohsternp
    // BHH4CG          U
    // Raa1AB          S
    //  nn9
    //  ts
    0b0100011100001100000, // Japan
    0b0000001101001100100, // Americas
    0b0100110100001000010, // Europe
    0b0100110100001000010, // Australia
    0b0000000000000000000, // China
    0b0100111100001000000, // Korea
    0b0100111100001000000, // Taiwan
};

static bool IsValidLocale(u32 region_index, u32 language_index) {
    if (region_index >= LOCALE_BLOCKLIST.size()) {
        return false;
    }
    return ((LOCALE_BLOCKLIST.at(region_index) >> language_index) & 1) == 0;
}

ConfigureSystem::ConfigureSystem(
    Core::System& system_, std::shared_ptr<std::forward_list<ConfigurationShared::Tab*>> group,
    QWidget* parent)
    : Tab(group, parent), ui{std::make_unique<Ui::ConfigureSystem>()}, system{system_} {
    ui->setupUi(this);

    connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
        ui->rng_seed_edit->setEnabled(state == Qt::Checked);
        if (state != Qt::Checked) {
            ui->rng_seed_edit->setText(QStringLiteral("00000000"));
        }
    });

    connect(ui->custom_rtc_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
        ui->custom_rtc_edit->setEnabled(state == Qt::Checked);
        if (state != Qt::Checked) {
            ui->custom_rtc_edit->setDateTime(QDateTime::currentDateTime());
        }
    });

    const auto locale_check = [this](int index) {
        const auto region_index = ConfigurationShared::GetComboboxIndex(
            Settings::values.region_index.GetValue(true), ui->combo_region);
        const auto language_index = ConfigurationShared::GetComboboxIndex(
            Settings::values.language_index.GetValue(true), ui->combo_language);
        const bool valid_locale = IsValidLocale(region_index, language_index);
        ui->label_warn_invalid_locale->setVisible(!valid_locale);
        if (!valid_locale) {
            ui->label_warn_invalid_locale->setText(
                tr("Warning: \"%1\" is not a valid language for region \"%2\"")
                    .arg(ui->combo_language->currentText())
                    .arg(ui->combo_region->currentText()));
        }
    };

    connect(ui->combo_language, qOverload<int>(&QComboBox::currentIndexChanged), this,
            locale_check);
    connect(ui->combo_region, qOverload<int>(&QComboBox::currentIndexChanged), this, locale_check);

    SetupPerGameUI();

    SetConfiguration();
}

ConfigureSystem::~ConfigureSystem() = default;

void ConfigureSystem::changeEvent(QEvent* event) {
    if (event->type() == QEvent::LanguageChange) {
        RetranslateUI();
    }

    QWidget::changeEvent(event);
}

void ConfigureSystem::RetranslateUI() {
    ui->retranslateUi(this);
}

void ConfigureSystem::SetConfiguration() {
    enabled = !system.IsPoweredOn();
    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_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_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()));
    ui->use_unsafe_extended_memory_layout->setEnabled(enabled);
    ui->use_unsafe_extended_memory_layout->setChecked(
        Settings::values.use_unsafe_extended_memory_layout.GetValue());

    if (Settings::IsConfiguringGlobal()) {
        ui->combo_language->setCurrentIndex(Settings::values.language_index.GetValue());
        ui->combo_region->setCurrentIndex(Settings::values.region_index.GetValue());
        ui->combo_time_zone->setCurrentIndex(Settings::values.time_zone_index.GetValue());
    } else {
        ConfigurationShared::SetPerGameSetting(ui->combo_language,
                                               &Settings::values.language_index);
        ConfigurationShared::SetPerGameSetting(ui->combo_region, &Settings::values.region_index);
        ConfigurationShared::SetPerGameSetting(ui->combo_time_zone,
                                               &Settings::values.time_zone_index);

        ConfigurationShared::SetHighlight(ui->label_language,
                                          !Settings::values.language_index.UsingGlobal());
        ConfigurationShared::SetHighlight(ui->label_region,
                                          !Settings::values.region_index.UsingGlobal());
        ConfigurationShared::SetHighlight(ui->label_timezone,
                                          !Settings::values.time_zone_index.UsingGlobal());
    }
}

void ConfigureSystem::ReadSystemSettings() {}

void ConfigureSystem::ApplyConfiguration() {
    // Allow setting custom RTC even if system is powered on,
    // 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.GetValue() +
                                     Service::Time::TimeManager::GetExternalTimeZoneOffset()};
                system.GetTimeManager().UpdateLocalSystemClockTime(posix_time);
            }
        } else {
            Settings::values.custom_rtc_enabled = false;
        }
    }

    Settings::values.device_name = ui->device_name_edit->text().toStdString();

    if (!enabled) {
        return;
    }

    ConfigurationShared::ApplyPerGameSetting(&Settings::values.language_index, ui->combo_language);
    ConfigurationShared::ApplyPerGameSetting(&Settings::values.region_index, ui->combo_region);
    ConfigurationShared::ApplyPerGameSetting(&Settings::values.time_zone_index,
                                             ui->combo_time_zone);
    ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_unsafe_extended_memory_layout,
                                             ui->use_unsafe_extended_memory_layout,
                                             use_unsafe_extended_memory_layout);

    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 {
        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));
            }
            break;
        case ConfigurationShared::CheckState::Global:
            Settings::values.rng_seed_enabled.SetGlobal(true);
            Settings::values.rng_seed.SetGlobal(true);
            break;
        case ConfigurationShared::CheckState::Count:
            break;
        }
    }
}

void ConfigureSystem::SetupPerGameUI() {
    if (Settings::IsConfiguringGlobal()) {
        ui->combo_language->setEnabled(Settings::values.language_index.UsingGlobal());
        ui->combo_region->setEnabled(Settings::values.region_index.UsingGlobal());
        ui->combo_time_zone->setEnabled(Settings::values.time_zone_index.UsingGlobal());
        ui->rng_seed_checkbox->setEnabled(Settings::values.rng_seed.UsingGlobal());
        ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.UsingGlobal());

        return;
    }

    ConfigurationShared::SetColoredComboBox(ui->combo_language, ui->label_language,
                                            Settings::values.language_index.GetValue(true));
    ConfigurationShared::SetColoredComboBox(ui->combo_region, ui->label_region,
                                            Settings::values.region_index.GetValue(true));
    ConfigurationShared::SetColoredComboBox(ui->combo_time_zone, ui->label_timezone,
                                            Settings::values.time_zone_index.GetValue(true));

    ConfigurationShared::SetColoredTristate(
        ui->rng_seed_checkbox, Settings::values.rng_seed.UsingGlobal(),
        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,
                                            use_unsafe_extended_memory_layout);

    ui->custom_rtc_checkbox->setVisible(false);
    ui->custom_rtc_edit->setVisible(false);
}