Qt: Don't allow creation of memory cards with invalid names

This commit is contained in:
Stenzek 2023-07-04 21:03:52 +10:00 committed by Connor McLaughlin
parent a9a1af7307
commit 8f9f351940
2 changed files with 21 additions and 19 deletions

View File

@ -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

View File

@ -3579,9 +3579,9 @@ void FullscreenUI::DrawCreateMemoryCardWindow()
static constexpr std::tuple<const char*, MemoryCardType, MemoryCardFileType> 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))