Debian Codegraph @

Source/Core/DolphinQt/ConvertDialog.cpp

raw symbols SCIP
1 // Copyright 2020 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/ConvertDialog.h"
5
6 #include <algorithm>
7 #include <filesystem>
8 #include <future>
9 #include <memory>
10 #include <utility>
11
12 #include <QCheckBox>
13 #include <QComboBox>
14 #include <QFormLayout>
15 #include <QGroupBox>
16 #include <QLabel>
17 #include <QList>
18 #include <QMessageBox>
19 #include <QPushButton>
20 #include <QString>
21 #include <QVBoxLayout>
22
23 #include "Common/Assert.h"
24 #include "Common/Logging/Log.h"
25 #include "Common/StringUtil.h"
26 #include "DiscIO/Blob.h"
27 #include "DiscIO/DiscUtils.h"
28 #include "DiscIO/ScrubbedBlob.h"
29 #include "DiscIO/WIABlob.h"
30 #include "DolphinQt/QtUtils/DolphinFileDialog.h"
31 #include "DolphinQt/QtUtils/ModalMessageBox.h"
32 #include "DolphinQt/QtUtils/ParallelProgressDialog.h"
33 #include "UICommon/GameFile.h"
34 #include "UICommon/UICommon.h"
35
36 ConvertDialog::ConvertDialog(QList<std::shared_ptr<const UICommon::GameFile>> files,
37 QWidget* parent)
38 : QDialog(parent), m_files(std::move(files))
39 {
40 ASSERT(!m_files.empty());
41
42 setWindowTitle(tr("Convert"));
43
44 auto* const form_layout = new QFormLayout;
45 form_layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
46
47 m_format = new QComboBox;
48 m_format->addItem(QStringLiteral("ISO"), static_cast<int>(DiscIO::BlobType::PLAIN));
49 m_format->addItem(QStringLiteral("GCZ"), static_cast<int>(DiscIO::BlobType::GCZ));
50 m_format->addItem(QStringLiteral("WIA"), static_cast<int>(DiscIO::BlobType::WIA));
51 m_format->addItem(QStringLiteral("RVZ"), static_cast<int>(DiscIO::BlobType::RVZ));
52 if (std::ranges::all_of(
53 m_files, [](const auto& file) { return file->GetBlobType() == DiscIO::BlobType::PLAIN; }))
54 {
55 m_format->setCurrentIndex(m_format->count() - 1);
56 }
57 form_layout->addRow(tr("Format:"), m_format);
58
59 m_block_size = new QComboBox;
60 form_layout->addRow(tr("Block Size:"), m_block_size);
61
62 m_compression = new QComboBox;
63 form_layout->addRow(tr("Compression:"), m_compression);
64
65 m_compression_level = new QComboBox;
66 form_layout->addRow(tr("Compression Level:"), m_compression_level);
67
68 m_scrub = new QCheckBox;
69 form_layout->addRow(tr("Remove Junk Data (Irreversible):"), m_scrub);
70
71 auto* const convert_button = new QPushButton(tr("Convert..."));
72
73 auto* const info_text = new QLabel(
74 tr("ISO: A simple and robust format which is supported by many programs. It takes up more "
75 "space than any other format.\n\n"
76 "GCZ: A basic compressed format which is compatible with most versions of Dolphin and "
77 "some other programs. It can't efficiently compress junk data (unless removed) or "
78 "encrypted Wii data.\n\n"
79 "WIA: An advanced compressed format which is compatible with Dolphin 5.0-12188 and later, "
80 "and a few other programs. It can efficiently compress encrypted Wii data, but not junk "
81 "data (unless removed).\n\n"
82 "RVZ: An advanced compressed format which is compatible with Dolphin 5.0-12188 and later. "
83 "It can efficiently compress both junk data and encrypted Wii data."));
84 info_text->setWordWrap(true);
85 info_text->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
86
87 auto* const options_group = new QGroupBox(tr("Options"));
88 auto* const options_layout = new QVBoxLayout{options_group};
89 options_layout->addLayout(form_layout);
90 options_layout->addWidget(convert_button);
91
92 auto* const info_group = new QGroupBox(tr("Info"));
93 auto* const info_layout = new QVBoxLayout{info_group};
94 info_layout->addWidget(info_text);
95
96 auto* const main_layout = new QVBoxLayout{this};
97 main_layout->addWidget(options_group);
98 main_layout->addWidget(info_group, 1);
99
100 connect(m_format, &QComboBox::currentIndexChanged, this, &ConvertDialog::OnFormatChanged);
101 connect(m_compression, &QComboBox::currentIndexChanged, this,
102 &ConvertDialog::OnCompressionChanged);
103 connect(convert_button, &QPushButton::clicked, this, &ConvertDialog::Convert);
104
105 OnFormatChanged();
106 OnCompressionChanged();
107 }
108
109 void ConvertDialog::AddToBlockSizeComboBox(int size)
110 {
111 m_block_size->addItem(QString::fromStdString(UICommon::FormatSize(size, 0)), size);
112
113 // Select the default, or if it is not available, the size closest to it.
114 // This code assumes that sizes get added to the combo box in increasing order.
115 if (size <= DiscIO::GCZ_RVZ_PREFERRED_BLOCK_SIZE)
116 m_block_size->setCurrentIndex(m_block_size->count() - 1);
117 }
118
119 void ConvertDialog::AddToCompressionComboBox(const QString& name,
120 DiscIO::WIARVZCompressionType type)
121 {
122 m_compression->addItem(name, static_cast<int>(type));
123 }
124
125 void ConvertDialog::AddToCompressionLevelComboBox(int level)
126 {
127 m_compression_level->addItem(QString::number(level), level);
128 }
129
130 void ConvertDialog::OnFormatChanged()
131 {
132 const DiscIO::BlobType format = static_cast<DiscIO::BlobType>(m_format->currentData().toInt());
133
134 m_block_size->clear();
135 m_compression->clear();
136
137 // Populate m_block_size
138 switch (format)
139 {
140 case DiscIO::BlobType::GCZ:
141 {
142 // To support legacy versions of dolphin, we have to check the GCZ block size
143 // See DiscIO::IsGCZBlockSizeLegacyCompatible() for details
144 const auto block_size_ok = [this](int block_size) {
145 return std::ranges::all_of(m_files, [block_size](const auto& file) {
146 return DiscIO::IsGCZBlockSizeLegacyCompatible(block_size, file->GetVolumeSize());
147 });
148 };
149
150 // Add all block sizes in the normal range that do not cause problems
151 for (int block_size = DiscIO::PREFERRED_MIN_BLOCK_SIZE;
152 block_size <= DiscIO::PREFERRED_MAX_BLOCK_SIZE; block_size *= 2)
153 {
154 if (block_size_ok(block_size))
155 AddToBlockSizeComboBox(block_size);
156 }
157
158 // If we didn't find a good block size, pick the block size which was hardcoded
159 // in older versions of Dolphin. That way, at least we're not worse than older versions.
160 if (m_block_size->count() == 0)
161 {
162 if (!block_size_ok(DiscIO::GCZ_FALLBACK_BLOCK_SIZE))
163 {
164 ERROR_LOG_FMT(MASTER_LOG, "Failed to find a block size which does not cause problems "
165 "when decompressing using an old version of Dolphin");
166 }
167 AddToBlockSizeComboBox(DiscIO::GCZ_FALLBACK_BLOCK_SIZE);
168 }
169
170 break;
171 }
172 case DiscIO::BlobType::WIA:
173 m_block_size->setEnabled(true);
174
175 // This is the smallest block size supported by WIA. For performance, larger sizes are avoided.
176 AddToBlockSizeComboBox(DiscIO::WIA_MIN_BLOCK_SIZE);
177
178 break;
179 case DiscIO::BlobType::RVZ:
180 m_block_size->setEnabled(true);
181
182 for (int block_size = DiscIO::PREFERRED_MIN_BLOCK_SIZE;
183 block_size <= DiscIO::PREFERRED_MAX_BLOCK_SIZE; block_size *= 2)
184 AddToBlockSizeComboBox(block_size);
185
186 break;
187 default:
188 break;
189 }
190
191 // Populate m_compression
192 switch (format)
193 {
194 case DiscIO::BlobType::GCZ:
195 m_compression->setEnabled(true);
196 AddToCompressionComboBox(QStringLiteral("Deflate"), DiscIO::WIARVZCompressionType::None);
197 break;
198 case DiscIO::BlobType::WIA:
199 case DiscIO::BlobType::RVZ:
200 {
201 m_compression->setEnabled(true);
202
203 // i18n: %1 is the name of a compression method (e.g. LZMA)
204 const QString slow = tr("%1 (slow)");
205
206 AddToCompressionComboBox(tr("No Compression"), DiscIO::WIARVZCompressionType::None);
207
208 if (format == DiscIO::BlobType::WIA)
209 AddToCompressionComboBox(QStringLiteral("Purge"), DiscIO::WIARVZCompressionType::Purge);
210
211 AddToCompressionComboBox(slow.arg(QStringLiteral("bzip2")),
212 DiscIO::WIARVZCompressionType::Bzip2);
213
214 AddToCompressionComboBox(slow.arg(QStringLiteral("LZMA")), DiscIO::WIARVZCompressionType::LZMA);
215
216 AddToCompressionComboBox(slow.arg(QStringLiteral("LZMA2")),
217 DiscIO::WIARVZCompressionType::LZMA2);
218
219 if (format == DiscIO::BlobType::RVZ)
220 {
221 // i18n: %1 is the name of a compression method (e.g. Zstandard)
222 const QString recommended = tr("%1 (recommended)");
223
224 AddToCompressionComboBox(recommended.arg(QStringLiteral("Zstandard")),
225 DiscIO::WIARVZCompressionType::Zstd);
226 m_compression->setCurrentIndex(m_compression->count() - 1);
227 }
228
229 break;
230 }
231 default:
232 m_compression->setEnabled(false);
233 break;
234 }
235
236 m_block_size->setEnabled(m_block_size->count() > 1);
237 m_compression->setEnabled(m_compression->count() > 1);
238
239 // Block scrubbing of RVZ containers and Datel discs
240 const bool scrubbing_allowed = format != DiscIO::BlobType::RVZ &&
241 std::ranges::none_of(m_files, &UICommon::GameFile::IsDatelDisc);
242
243 m_scrub->setEnabled(scrubbing_allowed);
244 if (!scrubbing_allowed)
245 m_scrub->setChecked(false);
246 }
247
248 void ConvertDialog::OnCompressionChanged()
249 {
250 m_compression_level->clear();
251
252 const auto compression_type =
253 static_cast<DiscIO::WIARVZCompressionType>(m_compression->currentData().toInt());
254
255 const std::pair<int, int> range = DiscIO::GetAllowedCompressionLevels(compression_type, true);
256
257 for (int i = range.first; i <= range.second; ++i)
258 {
259 AddToCompressionLevelComboBox(i);
260 if (i == 5)
261 m_compression_level->setCurrentIndex(m_compression_level->count() - 1);
262 }
263
264 m_compression_level->setEnabled(m_compression_level->count() > 1);
265 }
266
267 bool ConvertDialog::ShowAreYouSureDialog(const QString& text)
268 {
269 ModalMessageBox warning(this);
270 warning.setIcon(QMessageBox::Warning);
271 warning.setWindowTitle(tr("Confirm"));
272 warning.setText(tr("Are you sure?"));
273 warning.setInformativeText(text);
274 warning.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
275
276 return warning.exec() == QMessageBox::Yes;
277 }
278
279 void ConvertDialog::Convert()
280 {
281 const DiscIO::BlobType format = static_cast<DiscIO::BlobType>(m_format->currentData().toInt());
282 const int block_size = m_block_size->currentData().toInt();
283 const DiscIO::WIARVZCompressionType compression =
284 static_cast<DiscIO::WIARVZCompressionType>(m_compression->currentData().toInt());
285 const int compression_level = m_compression_level->currentData().toInt();
286 const bool scrub = m_scrub->isChecked();
287
288 if (scrub && format == DiscIO::BlobType::PLAIN)
289 {
290 if (!ShowAreYouSureDialog(tr("Removing junk data does not save any space when converting to "
291 "ISO (unless you package the ISO file in a compressed file format "
292 "such as ZIP afterwards). Do you want to continue anyway?")))
293 {
294 return;
295 }
296 }
297
298 if (!scrub && format == DiscIO::BlobType::GCZ &&
299 std::ranges::any_of(m_files, [](const auto& file) {
300 return file->GetPlatform() == DiscIO::Platform::WiiDisc && !file->IsDatelDisc();
301 }))
302 {
303 if (!ShowAreYouSureDialog(tr("Converting Wii disc images to GCZ without removing junk data "
304 "does not save any noticeable amount of space compared to "
305 "converting to ISO. Do you want to continue anyway?")))
306 {
307 return;
308 }
309 }
310
311 if (std::ranges::any_of(m_files, &UICommon::GameFile::IsNKit))
312 {
313 if (!ShowAreYouSureDialog(
314 tr("Dolphin can't convert NKit files to non-NKit files. Converting an NKit file in "
315 "Dolphin will result in another NKit file.\n"
316 "\n"
317 "If you want to convert an NKit file to a non-NKit file, you can use the same "
318 "program as you originally used when converting the file to the NKit format.\n"
319 "\n"
320 "Do you want to continue anyway?")))
321 {
322 return;
323 }
324 }
325
326 QString extension;
327 QString filter;
328 switch (format)
329 {
330 case DiscIO::BlobType::PLAIN:
331 extension = QStringLiteral(".iso");
332 filter = tr("Uncompressed GC/Wii images (*.iso *.gcm)");
333 break;
334 case DiscIO::BlobType::GCZ:
335 extension = QStringLiteral(".gcz");
336 filter = tr("GCZ GC/Wii images (*.gcz)");
337 break;
338 case DiscIO::BlobType::WIA:
339 extension = QStringLiteral(".wia");
340 filter = tr("WIA GC/Wii images (*.wia)");
341 break;
342 case DiscIO::BlobType::RVZ:
343 extension = QStringLiteral(".rvz");
344 filter = tr("RVZ GC/Wii images (*.rvz)");
345 break;
346 default:
347 ASSERT(false);
348 return;
349 }
350
351 QString dst_dir;
352 QString dst_path;
353
354 if (m_files.size() > 1)
355 {
356 dst_dir = DolphinFileDialog::getExistingDirectory(
357 this, tr("Save Converted Images"),
358 QFileInfo(QString::fromStdString(m_files[0]->GetFilePath())).dir().absolutePath());
359
360 if (dst_dir.isEmpty())
361 return;
362 }
363 else
364 {
365 dst_path = DolphinFileDialog::getSaveFileName(
366 this, tr("Save Converted Image"),
367 QFileInfo(QString::fromStdString(m_files[0]->GetFilePath()))
368 .dir()
369 .absoluteFilePath(
370 QFileInfo(QString::fromStdString(m_files[0]->GetFilePath())).completeBaseName())
371 .append(extension),
372 filter);
373
374 if (dst_path.isEmpty())
375 return;
376 }
377
378 int success_count = 0;
379
380 for (const auto& file : m_files)
381 {
382 const auto original_path = file->GetFilePath();
383 if (m_files.size() > 1)
384 {
385 dst_path =
386 QDir(dst_dir)
387 .absoluteFilePath(QFileInfo(QString::fromStdString(original_path)).completeBaseName())
388 .append(extension);
389 QFileInfo dst_info = QFileInfo(dst_path);
390 if (dst_info.exists())
391 {
392 ModalMessageBox confirm_replace(this);
393 confirm_replace.setIcon(QMessageBox::Warning);
394 confirm_replace.setWindowTitle(tr("Confirm"));
395 confirm_replace.setText(tr("The file %1 already exists.\n"
396 "Do you wish to replace it?")
397 .arg(dst_info.fileName()));
398 confirm_replace.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
399
400 if (confirm_replace.exec() == QMessageBox::No)
401 continue;
402 }
403 }
404
405 if (std::filesystem::exists(StringToPath(dst_path.toStdString())))
406 {
407 std::error_code ec;
408 if (std::filesystem::equivalent(StringToPath(dst_path.toStdString()),
409 StringToPath(original_path), ec))
410 {
411 ModalMessageBox::critical(
412 this, tr("Error"),
413 tr("The destination file cannot be the same as the source file\n\n"
414 "Please select another destination path for \"%1\"")
415 .arg(QString::fromStdString(original_path)));
416 continue;
417 }
418 }
419
420 ParallelProgressDialog progress_dialog(tr("Converting..."), tr("Abort"), 0, 100, this);
421 progress_dialog.GetRaw()->setWindowModality(Qt::WindowModal);
422 progress_dialog.GetRaw()->setWindowTitle(tr("Progress"));
423
424 if (m_files.size() > 1)
425 {
426 // i18n: %1 is a filename.
427 progress_dialog.GetRaw()->setLabelText(
428 tr("Converting...\n%1").arg(QFileInfo(QString::fromStdString(original_path)).fileName()));
429 }
430
431 std::unique_ptr<DiscIO::BlobReader> blob_reader;
432 bool scrub_current_file = scrub;
433
434 if (scrub_current_file)
435 {
436 blob_reader = DiscIO::ScrubbedBlob::Create(original_path);
437 if (!blob_reader)
438 {
439 const int result =
440 ModalMessageBox::warning(this, tr("Question"),
441 tr("Failed to remove junk data from file \"%1\".\n\n"
442 "Would you like to convert it without removing junk data?")
443 .arg(QString::fromStdString(original_path)),
444 QMessageBox::Ok | QMessageBox::Abort);
445
446 if (result == QMessageBox::Ok)
447 scrub_current_file = false;
448 else
449 return;
450 }
451 }
452
453 if (!scrub_current_file)
454 blob_reader = DiscIO::CreateBlobReader(original_path);
455
456 if (!blob_reader)
457 {
458 ModalMessageBox::critical(
459 this, tr("Error"),
460 tr("Failed to open the input file \"%1\".").arg(QString::fromStdString(original_path)));
461 return;
462 }
463 else
464 {
465 const auto callback = [&progress_dialog](const std::string& text, float percent) {
466 progress_dialog.SetValue(percent * 100);
467 return !progress_dialog.WasCanceled();
468 };
469
470 std::future<bool> success;
471
472 switch (format)
473 {
474 case DiscIO::BlobType::PLAIN:
475 success = std::async(std::launch::async, [&] {
476 const bool good = DiscIO::ConvertToPlain(blob_reader.get(), original_path,
477 dst_path.toStdString(), callback);
478 progress_dialog.Reset();
479 return good;
480 });
481 break;
482
483 case DiscIO::BlobType::GCZ:
484 success = std::async(std::launch::async, [&] {
485 const bool good = DiscIO::ConvertToGCZ(
486 blob_reader.get(), original_path, dst_path.toStdString(),
487 file->GetPlatform() == DiscIO::Platform::WiiDisc ? 1 : 0, block_size, callback);
488 progress_dialog.Reset();
489 return good;
490 });
491 break;
492
493 case DiscIO::BlobType::WIA:
494 case DiscIO::BlobType::RVZ:
495 success = std::async(std::launch::async, [&] {
496 const bool good =
497 DiscIO::ConvertToWIAOrRVZ(blob_reader.get(), original_path, dst_path.toStdString(),
498 format == DiscIO::BlobType::RVZ, compression,
499 compression_level, block_size, callback);
500 progress_dialog.Reset();
501 return good;
502 });
503 break;
504
505 default:
506 ASSERT(false);
507 break;
508 }
509
510 progress_dialog.GetRaw()->exec();
511 if (!success.get())
512 {
513 ModalMessageBox::critical(this, tr("Error"),
514 tr("Dolphin failed to complete the requested action."));
515 return;
516 }
517
518 success_count++;
519 }
520 }
521
522 ModalMessageBox::information(this, tr("Success"),
523 tr("Successfully converted %n image(s).", "", success_count));
524
525 close();
526 }