From 8f9f35194011accefba5dc707a93471f53bd5650 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 4 Jul 2023 21:03:52 +1000 Subject: [PATCH] Qt: Don't allow creation of memory cards with invalid names --- pcsx2-qt/Settings/MemoryCardCreateDialog.cpp | 25 +++++++++----------- pcsx2/ImGui/FullscreenUI.cpp | 15 ++++++++---- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pcsx2-qt/Settings/MemoryCardCreateDialog.cpp b/pcsx2-qt/Settings/MemoryCardCreateDialog.cpp index 939805cf43..48d74689ff 100644 --- a/pcsx2-qt/Settings/MemoryCardCreateDialog.cpp +++ b/pcsx2-qt/Settings/MemoryCardCreateDialog.cpp @@ -106,28 +106,25 @@ void MemoryCardCreateDialog::updateState() void MemoryCardCreateDialog::createCard() { - QString name(m_ui.name->text()); - std::string nameStr; - - if (m_fileType == MemoryCardFileType::PS1) + const QString name = m_ui.name->text(); + const std::string name_str = QStringLiteral("%1.%2").arg(name) + .arg((m_fileType == MemoryCardFileType::PS1) ? QStringLiteral("mcr") : QStringLiteral("ps2")) + .toStdString(); + if (!Path::IsValidFileName(name_str, false)) { - name += QStringLiteral(".mcr"); - } - else - { - name += QStringLiteral(".ps2"); + QMessageBox::critical(this, tr("Create Memory Card"), + tr("Failed to create the Memory Card, because the name '%1' contains one or more invalid characters.").arg(name)); + return; } - nameStr = name.toStdString(); - - if (FileMcd_GetCardInfo(nameStr).has_value()) + if (FileMcd_GetCardInfo(name_str).has_value()) { QMessageBox::critical(this, tr("Create Memory Card"), tr("Failed to create the Memory Card, because another card with the name '%1' already exists.").arg(name)); return; } - if (!FileMcd_CreateNewCard(nameStr, m_type, m_fileType)) + if (!FileMcd_CreateNewCard(name_str, m_type, m_fileType)) { QMessageBox::critical(this, tr("Create Memory Card"), tr("Failed to create the Memory Card, the log may contain more information.")); @@ -137,7 +134,7 @@ void MemoryCardCreateDialog::createCard() #ifdef _WIN32 if (m_ui.ntfsCompression->isChecked() && m_type == MemoryCardType::File) { - const std::string fullPath(Path::Combine(EmuFolders::MemoryCards, nameStr)); + const std::string fullPath(Path::Combine(EmuFolders::MemoryCards, name_str)); FileSystem::SetPathCompression(fullPath.c_str(), true); } #endif diff --git a/pcsx2/ImGui/FullscreenUI.cpp b/pcsx2/ImGui/FullscreenUI.cpp index 18c6b4cec0..ff09f9a8ca 100644 --- a/pcsx2/ImGui/FullscreenUI.cpp +++ b/pcsx2/ImGui/FullscreenUI.cpp @@ -3579,9 +3579,9 @@ void FullscreenUI::DrawCreateMemoryCardWindow() static constexpr std::tuple memcard_types[] = { {"8 MB [Most Compatible]", MemoryCardType::File, MemoryCardFileType::PS2_8MB}, - {"16 MB", MemoryCardType::File, MemoryCardFileType::PS2_8MB}, - {"32 MB", MemoryCardType::File, MemoryCardFileType::PS2_8MB}, - {"64 MB", MemoryCardType::File, MemoryCardFileType::PS2_8MB}, + {"16 MB", MemoryCardType::File, MemoryCardFileType::PS2_16MB}, + {"32 MB", MemoryCardType::File, MemoryCardFileType::PS2_32MB}, + {"64 MB", MemoryCardType::File, MemoryCardFileType::PS2_64MB}, {"Folder [Recommended]", MemoryCardType::Folder, MemoryCardFileType::PS2_8MB}, {"128 KB [PS1]", MemoryCardType::File, MemoryCardFileType::PS1}, }; @@ -3598,8 +3598,13 @@ void FullscreenUI::DrawCreateMemoryCardWindow() if (ActiveButton(ICON_FA_FOLDER_OPEN " Create", false, create_enabled) && std::strlen(memcard_name) > 0) { - const std::string real_card_name(fmt::format("{}.ps2", memcard_name)); - if (!FileMcd_GetCardInfo(real_card_name).has_value()) + const std::string real_card_name = fmt::format("{}.{}", memcard_name, + (std::get<2>(memcard_types[memcard_type]) == MemoryCardFileType::PS1 ? "mcr" : "ps2")); + if (!Path::IsValidFileName(real_card_name, false)) + { + ShowToast(std::string(), fmt::format("Memory card name '{}' is not valid.", real_card_name)); + } + else if (!FileMcd_GetCardInfo(real_card_name).has_value()) { const auto& [type_title, type, file_type] = memcard_types[memcard_type]; if (FileMcd_CreateNewCard(real_card_name, type, file_type))