diff --git a/CHANGES b/CHANGES index faaf2c15d..2ce4cd1ea 100644 --- a/CHANGES +++ b/CHANGES @@ -75,6 +75,7 @@ Misc: - Qt: Show warning if XQ audio is toggled while loaded (fixes mgba.io/i/2295) - Qt: Add e-Card passing to the command line (closes mgba.io/i/2474) - Qt: Boot both a multiboot image and ROM with CLI args (closes mgba.io/i/1941) + - Qt: Improve cheat parsing (fixes mgba.io/i/2297) - Windows: Attach to console if present - Vita: Add bilinear filtering option (closes mgba.io/i/344) diff --git a/src/platform/qt/CheatsView.cpp b/src/platform/qt/CheatsView.cpp index d26e63bcd..e73b52955 100644 --- a/src/platform/qt/CheatsView.cpp +++ b/src/platform/qt/CheatsView.cpp @@ -7,11 +7,13 @@ #include "GBAApp.h" #include "CoreController.h" +#include "LogController.h" #include #include #include #include +#include #include #ifdef M_CORE_GBA @@ -110,7 +112,7 @@ void CheatsView::removeSet() { return; } CoreController::Interrupter interrupter(m_controller); - for (const QModelIndex& index : selection) { + for (const QModelIndex& index ATTRIBUTE_UNUSED : selection) { m_model.removeAt(selection[0]); } } @@ -149,18 +151,40 @@ void CheatsView::enterCheat() { index = m_model.index(m_model.rowCount() - 1, 0, QModelIndex()); m_ui.cheatList->selectionModel()->select(index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); } + // TODO: Update API to handle this splitting in the core + QRegularExpression regexp("\\s"); #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', Qt::SkipEmptyParts); + QStringList cheats = m_ui.codeEntry->toPlainText().split(regexp, Qt::SkipEmptyParts); #else - QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts); + QStringList cheats = m_ui.codeEntry->toPlainText().split(regexp, QString::SkipEmptyParts); #endif + int failure = 0; + QString buffer; for (const QString& string : cheats) { m_model.beginAppendRow(index); - mCheatAddLine(set, string.toUtf8().constData(), m_codeType); + if (!buffer.isEmpty()) { + buffer += " " + string; + if (mCheatAddLine(set, buffer.toUtf8().constData(), m_codeType)) { + buffer.clear(); + } else if (mCheatAddLine(set, string.toUtf8().constData(), m_codeType)) { + buffer.clear(); + } else { + buffer = string; + ++failure; + } + } else if (!mCheatAddLine(set, string.toUtf8().constData(), m_codeType)) { + buffer = string; + } m_model.endAppendRow(); } + if (!buffer.isEmpty()) { + ++failure; + } if (set->refresh) { set->refresh(set, m_controller->cheatDevice()); } + if (failure) { + LOG(QT, ERROR) << tr("Some cheats could not be added. Please ensure they're formatted correctly and/or try other cheat types."); + } m_ui.codeEntry->clear(); }