| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/VerifyWidget.h" |
| 5 | |
| 6 | #include <future> |
| 7 | #include <memory> |
| 8 | #include <optional> |
| 9 | #include <span> |
| 10 | #include <tuple> |
| 11 | |
| 12 | #include <QByteArray> |
| 13 | #include <QHBoxLayout> |
| 14 | #include <QHeaderView> |
| 15 | #include <QLabel> |
| 16 | #include <QVBoxLayout> |
| 17 | |
| 18 | #include "Common/CommonTypes.h" |
| 19 | #include "Core/Core.h" |
| 20 | #include "Core/System.h" |
| 21 | #include "DiscIO/Volume.h" |
| 22 | #include "DiscIO/VolumeVerifier.h" |
| 23 | #include "DolphinQt/QtUtils/ParallelProgressDialog.h" |
| 24 | #include "DolphinQt/Settings.h" |
| 25 | |
| 26 | VerifyWidget::VerifyWidget(std::shared_ptr<DiscIO::Volume> volume) : m_volume(std::move(volume)) |
| 27 | { |
| 28 | QVBoxLayout* layout = new QVBoxLayout; |
| 29 | |
| 30 | CreateWidgets(); |
| 31 | ConnectWidgets(); |
| 32 | |
| 33 | layout->addWidget(m_problems); |
| 34 | layout->addWidget(m_summary_text); |
| 35 | layout->addLayout(m_hash_layout); |
| 36 | layout->addLayout(m_redump_layout); |
| 37 | layout->addWidget(m_verify_button); |
| 38 | |
| 39 | layout->setStretchFactor(m_problems, 5); |
| 40 | layout->setStretchFactor(m_summary_text, 2); |
| 41 | |
| 42 | setLayout(layout); |
| 43 | |
| 44 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 45 | &VerifyWidget::OnEmulationStateChanged); |
| 46 | |
| 47 | OnEmulationStateChanged(Core::GetState(Core::System::GetInstance())); |
| 48 | } |
| 49 | |
| 50 | void VerifyWidget::OnEmulationStateChanged(Core::State state) |
| 51 | { |
| 52 | const bool running = state != Core::State::Uninitialized; |
| 53 | |
| 54 | // Verifying a Wii game while emulation is running doesn't work correctly |
| 55 | // due to verification of a Wii game creating an instance of IOS |
| 56 | m_verify_button->setEnabled(!running); |
| 57 | } |
| 58 | |
| 59 | void VerifyWidget::CreateWidgets() |
| 60 | { |
| 61 | m_problems = new QTableWidget(0, 2, this); |
| 62 | m_problems->setTabKeyNavigation(false); |
| 63 | m_problems->setHorizontalHeaderLabels({tr("Problem"), tr("Severity")}); |
| 64 | m_problems->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); |
| 65 | m_problems->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
| 66 | m_problems->horizontalHeader()->setHighlightSections(false); |
| 67 | m_problems->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); |
| 68 | m_problems->verticalHeader()->hide(); |
| 69 | |
| 70 | m_summary_text = new QTextEdit(this); |
| 71 | m_summary_text->setReadOnly(true); |
| 72 | |
| 73 | m_hash_layout = new QFormLayout; |
| 74 | std::tie(m_crc32_checkbox, m_crc32_line_edit) = AddHashLine(m_hash_layout, tr("CRC32:")); |
| 75 | std::tie(m_md5_checkbox, m_md5_line_edit) = AddHashLine(m_hash_layout, tr("MD5:")); |
| 76 | std::tie(m_sha1_checkbox, m_sha1_line_edit) = AddHashLine(m_hash_layout, tr("SHA-1:")); |
| 77 | |
| 78 | const auto default_to_calculate = DiscIO::VolumeVerifier::GetDefaultHashesToCalculate(); |
| 79 | m_crc32_checkbox->setChecked(default_to_calculate.crc32); |
| 80 | m_md5_checkbox->setChecked(default_to_calculate.md5); |
| 81 | m_sha1_checkbox->setChecked(default_to_calculate.sha1); |
| 82 | |
| 83 | m_redump_layout = new QFormLayout; |
| 84 | if (DiscIO::IsDisc(m_volume->GetVolumeType())) |
| 85 | { |
| 86 | std::tie(m_redump_checkbox, m_redump_line_edit) = |
| 87 | AddHashLine(m_redump_layout, tr("Redump.info Status:")); |
| 88 | m_redump_checkbox->setChecked(CanVerifyRedump()); |
| 89 | UpdateRedumpEnabled(); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | m_redump_checkbox = nullptr; |
| 94 | m_redump_line_edit = nullptr; |
| 95 | } |
| 96 | |
| 97 | // Extend line edits to their maximum possible widths (needed on macOS) |
| 98 | m_hash_layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow); |
| 99 | m_redump_layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow); |
| 100 | |
| 101 | m_verify_button = new QPushButton(tr("Verify Integrity"), this); |
| 102 | } |
| 103 | |
| 104 | std::pair<QCheckBox*, QLineEdit*> VerifyWidget::AddHashLine(QFormLayout* layout, |
| 105 | const QString& text) |
| 106 | { |
| 107 | QLineEdit* line_edit = new QLineEdit(this); |
| 108 | line_edit->setReadOnly(true); |
| 109 | QCheckBox* checkbox = new QCheckBox(tr("Calculate"), this); |
| 110 | |
| 111 | QHBoxLayout* hbox_layout = new QHBoxLayout; |
| 112 | hbox_layout->addWidget(line_edit); |
| 113 | hbox_layout->addWidget(checkbox); |
| 114 | |
| 115 | layout->addRow(text, hbox_layout); |
| 116 | |
| 117 | return std::pair(checkbox, line_edit); |
| 118 | } |
| 119 | |
| 120 | void VerifyWidget::ConnectWidgets() |
| 121 | { |
| 122 | connect(m_verify_button, &QPushButton::clicked, this, &VerifyWidget::Verify); |
| 123 | |
| 124 | #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) |
| 125 | connect(m_md5_checkbox, &QCheckBox::checkStateChanged, this, &VerifyWidget::UpdateRedumpEnabled); |
| 126 | connect(m_sha1_checkbox, &QCheckBox::checkStateChanged, this, &VerifyWidget::UpdateRedumpEnabled); |
| 127 | #else |
| 128 | connect(m_md5_checkbox, &QCheckBox::stateChanged, this, &VerifyWidget::UpdateRedumpEnabled); |
| 129 | connect(m_sha1_checkbox, &QCheckBox::stateChanged, this, &VerifyWidget::UpdateRedumpEnabled); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | static void SetHash(QLineEdit* line_edit, std::span<const u8> hash) |
| 134 | { |
| 135 | const QByteArray byte_array = QByteArray::fromRawData(reinterpret_cast<const char*>(hash.data()), |
| 136 | static_cast<int>(hash.size())); |
| 137 | line_edit->setText(QString::fromLatin1(byte_array.toHex())); |
| 138 | } |
| 139 | |
| 140 | bool VerifyWidget::CanVerifyRedump() const |
| 141 | { |
| 142 | // We don't allow Redump verification with CRC32 only since generating a collision is too easy |
| 143 | return m_md5_checkbox->isChecked() || m_sha1_checkbox->isChecked(); |
| 144 | } |
| 145 | |
| 146 | void VerifyWidget::UpdateRedumpEnabled() |
| 147 | { |
| 148 | if (m_redump_checkbox) |
| 149 | m_redump_checkbox->setEnabled(CanVerifyRedump()); |
| 150 | } |
| 151 | |
| 152 | void VerifyWidget::Verify() |
| 153 | { |
| 154 | const bool redump_verification = |
| 155 | CanVerifyRedump() && m_redump_checkbox && m_redump_checkbox->isChecked(); |
| 156 | |
| 157 | DiscIO::VolumeVerifier verifier( |
| 158 | *m_volume, redump_verification, |
| 159 | {m_crc32_checkbox->isChecked(), m_md5_checkbox->isChecked(), m_sha1_checkbox->isChecked()}); |
| 160 | |
| 161 | // We have to divide the number of processed bytes with something so it won't make ints overflow |
| 162 | constexpr int DIVISOR = 0x100; |
| 163 | |
| 164 | ParallelProgressDialog progress(tr("Verifying"), tr("Cancel"), 0, |
| 165 | static_cast<int>(verifier.GetTotalBytes() / DIVISOR), this); |
| 166 | progress.GetRaw()->setWindowTitle(tr("Verifying")); |
| 167 | progress.GetRaw()->setMinimumDuration(500); |
| 168 | progress.GetRaw()->setWindowModality(Qt::WindowModal); |
| 169 | |
| 170 | auto future = |
| 171 | std::async(std::launch::async, |
| 172 | [&verifier, &progress]() -> std::optional<DiscIO::VolumeVerifier::Result> { |
| 173 | progress.SetValue(0); |
| 174 | verifier.Start(); |
| 175 | while (verifier.GetBytesProcessed() != verifier.GetTotalBytes()) |
| 176 | { |
| 177 | progress.SetValue(static_cast<int>(verifier.GetBytesProcessed() / DIVISOR)); |
| 178 | if (progress.WasCanceled()) |
| 179 | return std::nullopt; |
| 180 | |
| 181 | verifier.Process(); |
| 182 | } |
| 183 | verifier.Finish(); |
| 184 | |
| 185 | progress.Reset(); |
| 186 | return verifier.GetResult(); |
| 187 | }); |
| 188 | progress.GetRaw()->exec(); |
| 189 | |
| 190 | std::optional<DiscIO::VolumeVerifier::Result> result = future.get(); |
| 191 | if (!result) |
| 192 | return; |
| 193 | |
| 194 | m_summary_text->setText(QString::fromStdString(result->summary_text)); |
| 195 | |
| 196 | m_problems->setRowCount(static_cast<int>(result->problems.size())); |
| 197 | for (int i = 0; i < m_problems->rowCount(); ++i) |
| 198 | { |
| 199 | const DiscIO::VolumeVerifier::Problem problem = result->problems[i]; |
| 200 | |
| 201 | QString severity; |
| 202 | switch (problem.severity) |
| 203 | { |
| 204 | case DiscIO::VolumeVerifier::Severity::Low: |
| 205 | severity = tr("Low"); |
| 206 | break; |
| 207 | case DiscIO::VolumeVerifier::Severity::Medium: |
| 208 | severity = tr("Medium"); |
| 209 | break; |
| 210 | case DiscIO::VolumeVerifier::Severity::High: |
| 211 | severity = tr("High"); |
| 212 | break; |
| 213 | case DiscIO::VolumeVerifier::Severity::None: |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | SetProblemCellText(i, 0, QString::fromStdString(problem.text)); |
| 218 | SetProblemCellText(i, 1, severity); |
| 219 | } |
| 220 | |
| 221 | SetHash(m_crc32_line_edit, result->hashes.crc32); |
| 222 | SetHash(m_md5_line_edit, result->hashes.md5); |
| 223 | SetHash(m_sha1_line_edit, result->hashes.sha1); |
| 224 | |
| 225 | if (m_redump_line_edit) |
| 226 | m_redump_line_edit->setText(QString::fromStdString(result->redump.message)); |
| 227 | } |
| 228 | |
| 229 | void VerifyWidget::SetProblemCellText(int row, int column, const QString& text) |
| 230 | { |
| 231 | QLabel* label = new QLabel(text); |
| 232 | label->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 233 | label->setWordWrap(true); |
| 234 | label->setMargin(4); |
| 235 | m_problems->setCellWidget(row, column, label); |
| 236 | } |