Add cheats in per game configuration (#6379)

This commit is contained in:
luc-git 2023-04-30 16:36:02 +02:00 committed by GitHub
parent ea649263b7
commit 7327c334ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 86 additions and 122 deletions

View file

@ -0,0 +1,243 @@
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <QCheckBox>
#include <QMessageBox>
#include <QTableWidgetItem>
#include "configure_cheats.h"
#include "core/cheats/cheat_base.h"
#include "core/cheats/cheats.h"
#include "core/cheats/gateway_cheat.h"
#include "core/core.h"
#include "core/hle/kernel/process.h"
#include "ui_configure_cheats.h"
ConfigureCheats::ConfigureCheats(u64 title_id_, QWidget* parent)
: QWidget(parent), ui(std::make_unique<Ui::ConfigureCheats>()), title_id{title_id_} {
// Setup gui control settings
ui->setupUi(this);
ui->tableCheats->setColumnWidth(0, 30);
ui->tableCheats->setColumnWidth(2, 85);
ui->tableCheats->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
ui->tableCheats->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
ui->tableCheats->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
ui->lineName->setEnabled(false);
ui->textCode->setEnabled(false);
ui->textNotes->setEnabled(false);
connect(ui->buttonAddCheat, &QPushButton::clicked, this, &ConfigureCheats::OnAddCheat);
connect(ui->tableCheats, &QTableWidget::cellClicked, this, &ConfigureCheats::OnRowSelected);
connect(ui->lineName, &QLineEdit::textEdited, this, &ConfigureCheats::OnTextEdited);
connect(ui->textNotes, &QPlainTextEdit::textChanged, this, &ConfigureCheats::OnTextEdited);
connect(ui->textCode, &QPlainTextEdit::textChanged, this, &ConfigureCheats::OnTextEdited);
connect(ui->buttonSave, &QPushButton::clicked, this,
[this] { SaveCheat(ui->tableCheats->currentRow()); });
connect(ui->buttonDelete, &QPushButton::clicked, this, &ConfigureCheats::OnDeleteCheat);
cheat_engine = std::make_unique<Cheats::CheatEngine>(title_id, Core::System::GetInstance());
LoadCheats();
}
ConfigureCheats::~ConfigureCheats() = default;
void ConfigureCheats::LoadCheats() {
cheats = cheat_engine->GetCheats();
const int cheats_count = static_cast<int>(cheats.size());
ui->tableCheats->setRowCount(cheats_count);
for (int i = 0; i < cheats_count; i++) {
QCheckBox* enabled = new QCheckBox();
enabled->setChecked(cheats[i]->IsEnabled());
enabled->setStyleSheet(QStringLiteral("margin-left:7px;"));
ui->tableCheats->setItem(i, 0, new QTableWidgetItem());
ui->tableCheats->setCellWidget(i, 0, enabled);
ui->tableCheats->setItem(
i, 1, new QTableWidgetItem(QString::fromStdString(cheats[i]->GetName())));
ui->tableCheats->setItem(
i, 2, new QTableWidgetItem(QString::fromStdString(cheats[i]->GetType())));
enabled->setProperty("row", static_cast<int>(i));
connect(enabled, &QCheckBox::stateChanged, this, &ConfigureCheats::OnCheckChanged);
}
}
bool ConfigureCheats::CheckSaveCheat() {
auto answer = QMessageBox::warning(
this, tr("Cheats"), tr("Would you like to save the current cheat?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Cancel);
if (answer == QMessageBox::Yes) {
return SaveCheat(last_row);
} else {
return answer != QMessageBox::Cancel;
}
}
bool ConfigureCheats::SaveCheat(int row) {
if (ui->lineName->text().isEmpty()) {
QMessageBox::critical(this, tr("Save Cheat"), tr("Please enter a cheat name."));
return false;
}
if (ui->textCode->toPlainText().isEmpty()) {
QMessageBox::critical(this, tr("Save Cheat"), tr("Please enter the cheat code."));
return false;
}
// Check if the cheat lines are valid
auto code_lines = ui->textCode->toPlainText().split(QLatin1Char{'\n'}, Qt::SkipEmptyParts);
for (int i = 0; i < code_lines.size(); ++i) {
Cheats::GatewayCheat::CheatLine cheat_line(code_lines[i].toStdString());
if (cheat_line.valid)
continue;
auto answer = QMessageBox::warning(
this, tr("Save Cheat"),
tr("Cheat code line %1 is not valid.\nWould you like to ignore the error and continue?")
.arg(i + 1),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (answer == QMessageBox::No)
return false;
}
auto cheat = std::make_shared<Cheats::GatewayCheat>(ui->lineName->text().toStdString(),
ui->textCode->toPlainText().toStdString(),
ui->textNotes->toPlainText().toStdString());
if (newly_created) {
cheat_engine->AddCheat(cheat);
newly_created = false;
} else {
cheat_engine->UpdateCheat(row, cheat);
}
cheat_engine->SaveCheatFile();
int previous_row = ui->tableCheats->currentRow();
int previous_col = ui->tableCheats->currentColumn();
LoadCheats();
ui->tableCheats->setCurrentCell(previous_row, previous_col);
edited = false;
ui->buttonSave->setEnabled(false);
ui->buttonAddCheat->setEnabled(true);
return true;
}
void ConfigureCheats::OnRowSelected(int row, int column) {
if (row == last_row) {
return;
}
if (edited && !CheckSaveCheat()) {
ui->tableCheats->setCurrentCell(last_row, last_col);
return;
}
if (static_cast<std::size_t>(row) < cheats.size()) {
if (newly_created) {
// Remove the newly created dummy item
newly_created = false;
ui->tableCheats->setRowCount(ui->tableCheats->rowCount() - 1);
}
const auto& current_cheat = cheats[row];
ui->lineName->setText(QString::fromStdString(current_cheat->GetName()));
ui->textNotes->setPlainText(QString::fromStdString(current_cheat->GetComments()));
ui->textCode->setPlainText(QString::fromStdString(current_cheat->GetCode()));
}
edited = false;
ui->buttonSave->setEnabled(false);
ui->buttonDelete->setEnabled(true);
ui->buttonAddCheat->setEnabled(true);
ui->lineName->setEnabled(true);
ui->textCode->setEnabled(true);
ui->textNotes->setEnabled(true);
last_row = row;
last_col = column;
}
void ConfigureCheats::OnCheckChanged(int state) {
const QCheckBox* checkbox = qobject_cast<QCheckBox*>(sender());
int row = static_cast<int>(checkbox->property("row").toInt());
cheats[row]->SetEnabled(state);
cheat_engine->SaveCheatFile();
}
void ConfigureCheats::OnTextEdited() {
edited = true;
ui->buttonSave->setEnabled(true);
}
void ConfigureCheats::OnDeleteCheat() {
if (newly_created) {
newly_created = false;
} else {
cheat_engine->RemoveCheat(ui->tableCheats->currentRow());
cheat_engine->SaveCheatFile();
}
LoadCheats();
if (cheats.empty()) {
ui->lineName->clear();
ui->textCode->clear();
ui->textNotes->clear();
ui->lineName->setEnabled(false);
ui->textCode->setEnabled(false);
ui->textNotes->setEnabled(false);
ui->buttonDelete->setEnabled(false);
last_row = last_col = -1;
} else {
if (last_row >= ui->tableCheats->rowCount()) {
last_row = ui->tableCheats->rowCount() - 1;
}
ui->tableCheats->setCurrentCell(last_row, last_col);
const auto& current_cheat = cheats[last_row];
ui->lineName->setText(QString::fromStdString(current_cheat->GetName()));
ui->textNotes->setPlainText(QString::fromStdString(current_cheat->GetComments()));
ui->textCode->setPlainText(QString::fromStdString(current_cheat->GetCode()));
}
edited = false;
ui->buttonSave->setEnabled(false);
ui->buttonAddCheat->setEnabled(true);
}
bool ConfigureCheats::ApplyConfiguration() {
if (edited) {
return SaveCheat(ui->tableCheats->currentRow());
}
return true;
}
void ConfigureCheats::OnAddCheat() {
if (edited && !CheckSaveCheat()) {
return;
}
int row = ui->tableCheats->rowCount();
ui->tableCheats->setRowCount(row + 1);
ui->tableCheats->setCurrentCell(row, 1);
// create a dummy item
ui->tableCheats->setItem(row, 1, new QTableWidgetItem(tr("[new cheat]")));
ui->tableCheats->setItem(row, 2, new QTableWidgetItem(QString{}));
ui->lineName->clear();
ui->lineName->setPlaceholderText(tr("[new cheat]"));
ui->textCode->clear();
ui->textNotes->clear();
ui->lineName->setEnabled(true);
ui->textCode->setEnabled(true);
ui->textNotes->setEnabled(true);
ui->buttonSave->setEnabled(true);
ui->buttonDelete->setEnabled(true);
ui->buttonAddCheat->setEnabled(false);
edited = false;
newly_created = true;
last_row = row;
last_col = 1;
}

View file

@ -0,0 +1,61 @@
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include "common/common_types.h"
namespace Cheats {
class CheatBase;
class CheatEngine;
} // namespace Cheats
namespace Ui {
class ConfigureCheats;
} // namespace Ui
class ConfigureCheats : public QWidget {
Q_OBJECT
public:
explicit ConfigureCheats(u64 title_id_, QWidget* parent = nullptr);
~ConfigureCheats();
bool ApplyConfiguration();
private:
/**
* Loads the cheats from the CheatEngine, and populates the table.
*/
void LoadCheats();
/**
* Pops up a message box asking if the user wants to save the current cheat.
* If the user selected Yes, attempts to save the current cheat.
* @return true if the user selected No, or if the cheat was saved successfully
* false if the user selected Cancel, or if the user selected Yes but saving failed
*/
bool CheckSaveCheat();
/**
* Saves the current cheat as the row-th cheat in the cheat list.
* @return true if the cheat is saved successfully, false otherwise
*/
bool SaveCheat(int row);
private slots:
void OnRowSelected(int row, int column);
void OnCheckChanged(int state);
void OnTextEdited();
void OnDeleteCheat();
void OnAddCheat();
private:
std::unique_ptr<Ui::ConfigureCheats> ui;
std::vector<std::shared_ptr<Cheats::CheatBase>> cheats;
bool edited = false, newly_created = false;
int last_row = -1, last_col = -1;
u64 title_id;
std::unique_ptr<Cheats::CheatEngine> cheat_engine;
};

View file

@ -0,0 +1,214 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ConfigureCheats</class>
<widget class="QWidget" name="ConfigureCheats">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>862</width>
<height>612</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Cheats</string>
</property>
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonAddCheat">
<property name="text">
<string>Add Cheat</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="labelAvailableCheats">
<property name="text">
<string>Available Cheats:</string>
</property>
</widget>
</item>
<item>
<widget class="QTableWidget" name="tableCheats">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="columnCount">
<number>3</number>
</property>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string/>
</property>
</column>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Type</string>
</property>
</column>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonSave">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonDelete">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="labelName">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineName"/>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="labelNotes">
<property name="text">
<string>Notes:</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="textNotes"/>
</item>
<item>
<widget class="QLabel" name="labelCode">
<property name="text">
<string>Code:</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="textCode"/>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>buttonAddCheat</tabstop>
<tabstop>tableCheats</tabstop>
<tabstop>lineName</tabstop>
<tabstop>textNotes</tabstop>
<tabstop>textCode</tabstop>
<tabstop>buttonSave</tabstop>
<tabstop>buttonDelete</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>

View file

@ -9,6 +9,7 @@
#include <fmt/format.h>
#include "citra_qt/configuration/config.h"
#include "citra_qt/configuration/configure_audio.h"
#include "citra_qt/configuration/configure_cheats.h"
#include "citra_qt/configuration/configure_debug.h"
#include "citra_qt/configuration/configure_enhancements.h"
#include "citra_qt/configuration/configure_general.h"
@ -35,6 +36,7 @@ ConfigurePerGame::ConfigurePerGame(QWidget* parent, u64 title_id_, const QString
graphics_tab = std::make_unique<ConfigureGraphics>(this);
system_tab = std::make_unique<ConfigureSystem>(this);
debug_tab = std::make_unique<ConfigureDebug>(this);
cheat_tab = std::make_unique<ConfigureCheats>(title_id, this);
ui->setupUi(this);
@ -44,6 +46,7 @@ ConfigurePerGame::ConfigurePerGame(QWidget* parent, u64 title_id_, const QString
ui->tabWidget->addTab(graphics_tab.get(), tr("Graphics"));
ui->tabWidget->addTab(audio_tab.get(), tr("Audio"));
ui->tabWidget->addTab(debug_tab.get(), tr("Debug"));
ui->tabWidget->addTab(cheat_tab.get(), tr("Cheats"));
setFocusPolicy(Qt::ClickFocus);
setWindowTitle(tr("Properties"));
@ -60,6 +63,9 @@ ConfigurePerGame::ConfigurePerGame(QWidget* parent, u64 title_id_, const QString
connect(ui->button_reset_per_game, &QPushButton::clicked, this,
&ConfigurePerGame::ResetDefaults);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
&ConfigurePerGame::HandleAcceptedEvent);
LoadConfiguration();
}
@ -81,6 +87,13 @@ void ConfigurePerGame::ResetDefaults() {
close();
}
void ConfigurePerGame::HandleAcceptedEvent() {
if (ui->tabWidget->currentWidget() == cheat_tab.get()) {
cheat_tab->ApplyConfiguration();
}
accept();
}
void ConfigurePerGame::ApplyConfiguration() {
general_tab->ApplyConfiguration();
system_tab->ApplyConfiguration();
@ -109,6 +122,9 @@ void ConfigurePerGame::RetranslateUI() {
void ConfigurePerGame::HandleApplyButtonClicked() {
ApplyConfiguration();
if (ui->tabWidget->currentWidget() == cheat_tab.get()) {
cheat_tab->ApplyConfiguration();
}
}
static QPixmap GetQPixmapFromSMDH(std::vector<u8>& smdh_data) {

View file

@ -19,6 +19,7 @@ class ConfigureEnhancements;
class ConfigureGraphics;
class ConfigureSystem;
class ConfigureDebug;
class ConfigureCheats;
class QGraphicsScene;
class QStandardItem;
@ -51,6 +52,7 @@ public:
private:
void changeEvent(QEvent* event) override;
void RetranslateUI();
void HandleAcceptedEvent();
void HandleApplyButtonClicked();
@ -70,4 +72,5 @@ private:
std::unique_ptr<ConfigureGraphics> graphics_tab;
std::unique_ptr<ConfigureSystem> system_tab;
std::unique_ptr<ConfigureDebug> debug_tab;
std::unique_ptr<ConfigureCheats> cheat_tab;
};

View file

@ -235,22 +235,6 @@
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ConfigurePerGame</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>20</x>
<y>20</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>20</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>