diff --git a/src/duckstation-qt/mainwindow.cpp b/src/duckstation-qt/mainwindow.cpp index 7d3bdc5d3..54ceb246a 100644 --- a/src/duckstation-qt/mainwindow.cpp +++ b/src/duckstation-qt/mainwindow.cpp @@ -1505,6 +1505,22 @@ void MainWindow::onCheckForUpdatesActionTriggered() void MainWindow::openMemoryCardEditor(const QString& card_a_path, const QString& card_b_path) { + for (const QString& card_path : {card_a_path, card_b_path}) + { + if (!card_path.isEmpty() && !QFile::exists(card_path)) + { + if (QMessageBox::question( + this, tr("Memory Card Not Found"), + tr("Memory card '%1' does not exist. Do you want to create an empty memory card?").arg(card_path), + QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) + { + if (!MemoryCardEditorDialog::createMemoryCard(card_path)) + QMessageBox::critical(this, tr("Memory Card Not Found"), + tr("Failed to create memory card '%1'").arg(card_path)); + } + } + } + if (!m_memory_card_editor_dialog) { m_memory_card_editor_dialog = new MemoryCardEditorDialog(this); diff --git a/src/duckstation-qt/memorycardeditordialog.cpp b/src/duckstation-qt/memorycardeditordialog.cpp index 5855cd2ed..a09cf23cd 100644 --- a/src/duckstation-qt/memorycardeditordialog.cpp +++ b/src/duckstation-qt/memorycardeditordialog.cpp @@ -44,9 +44,17 @@ MemoryCardEditorDialog::~MemoryCardEditorDialog() = default; bool MemoryCardEditorDialog::setCardA(const QString& path) { - const int index = m_ui.cardAPath->findData(QVariant(QDir::toNativeSeparators(path))); + int index = m_ui.cardAPath->findData(QVariant(QDir::toNativeSeparators(path))); if (index < 0) - return false; + { + QFileInfo file(path); + if (!file.exists()) + return false; + + QSignalBlocker sb(m_card_a.path_cb); + m_card_a.path_cb->addItem(file.baseName(), QVariant(path)); + index = m_card_a.path_cb->count() - 1; + } m_ui.cardAPath->setCurrentIndex(index); return true; @@ -54,14 +62,30 @@ bool MemoryCardEditorDialog::setCardA(const QString& path) bool MemoryCardEditorDialog::setCardB(const QString& path) { - const int index = m_ui.cardBPath->findData(QVariant(QDir::toNativeSeparators(path))); + int index = m_ui.cardBPath->findData(QVariant(QDir::toNativeSeparators(path))); if (index < 0) - return false; + { + QFileInfo file(path); + if (!file.exists()) + return false; + + QSignalBlocker sb(m_card_b.path_cb); + m_card_b.path_cb->addItem(file.baseName(), QVariant(path)); + index = m_card_b.path_cb->count() - 1; + } m_ui.cardBPath->setCurrentIndex(index); return true; } +bool MemoryCardEditorDialog::createMemoryCard(const QString& path) +{ + MemoryCardImage::DataArray data; + MemoryCardImage::Format(&data); + + return MemoryCardImage::SaveToFile(data, path.toUtf8().constData()); +} + void MemoryCardEditorDialog::resizeEvent(QResizeEvent* ev) { QtUtils::ResizeColumnsForTableView(m_card_a.table, {32, -1, 155, 45}); diff --git a/src/duckstation-qt/memorycardeditordialog.h b/src/duckstation-qt/memorycardeditordialog.h index 1cb0fdb84..e18f69d9d 100644 --- a/src/duckstation-qt/memorycardeditordialog.h +++ b/src/duckstation-qt/memorycardeditordialog.h @@ -18,6 +18,8 @@ public: bool setCardA(const QString& path); bool setCardB(const QString& path); + static bool createMemoryCard(const QString& path); + protected: void resizeEvent(QResizeEvent* ev); void closeEvent(QCloseEvent* ev);