Qt: Removing cheats

This commit is contained in:
Jeffrey Pfau 2015-02-14 18:39:18 -08:00
parent dc5fb14fa1
commit 8741a374a5
6 changed files with 56 additions and 5 deletions

View File

@ -443,8 +443,23 @@ void GBACheatAttachDevice(struct GBA* gba, struct GBACheatDevice* device) {
void GBACheatAddSet(struct GBACheatDevice* device, struct GBACheatSet* cheats) { void GBACheatAddSet(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
*GBACheatSetsAppend(&device->cheats) = cheats; *GBACheatSetsAppend(&device->cheats) = cheats;
_patchROM(device, cheats);
_addBreakpoint(device, cheats); _addBreakpoint(device, cheats);
_patchROM(device, cheats);
}
void GBACheatRemoveSet(struct GBACheatDevice* device, struct GBACheatSet* cheats) {
size_t i;
for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
if (*GBACheatSetsGetPointer(&device->cheats, i) == cheats) {
break;
}
}
if (i == GBACheatSetsSize(&device->cheats)) {
return;
}
GBACheatSetsShift(&device->cheats, i, 1);
_unpatchROM(device, cheats);
_removeBreakpoint(device, cheats);
} }
bool GBACheatAddCodeBreaker(struct GBACheatSet* cheats, uint32_t op1, uint16_t op2) { bool GBACheatAddCodeBreaker(struct GBACheatSet* cheats, uint32_t op1, uint16_t op2) {

View File

@ -93,6 +93,20 @@ GBACheatSet* CheatsModel::itemAt(const QModelIndex& index) {
return static_cast<GBACheatSet*>(index.internalPointer()); return static_cast<GBACheatSet*>(index.internalPointer());
} }
void CheatsModel::removeAt(const QModelIndex& index) {
if (!index.isValid()) {
return;
}
int row = index.row();
GBACheatSet* set = static_cast<GBACheatSet*>(index.internalPointer());
beginRemoveRows(QModelIndex(), row, row);
GBACheatRemoveSet(m_device, set);
GBACheatSetDeinit(set);
delete set;
endInsertRows();
}
void CheatsModel::loadFile(const QString& path) { void CheatsModel::loadFile(const QString& path) {
VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY); VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY);
if (!vf) { if (!vf) {
@ -106,7 +120,7 @@ void CheatsModel::loadFile(const QString& path) {
void CheatsModel::addSet(GBACheatSet* set) { void CheatsModel::addSet(GBACheatSet* set) {
beginInsertRows(QModelIndex(), GBACheatSetsSize(&m_device->cheats), GBACheatSetsSize(&m_device->cheats) + 1); beginInsertRows(QModelIndex(), GBACheatSetsSize(&m_device->cheats), GBACheatSetsSize(&m_device->cheats) + 1);
*GBACheatSetsAppend(&m_device->cheats) = set; GBACheatAddSet(m_device, set);
endInsertRows(); endInsertRows();
} }

View File

@ -30,6 +30,7 @@ public:
virtual Qt::ItemFlags flags(const QModelIndex &index) const override; virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
GBACheatSet* itemAt(const QModelIndex& index); GBACheatSet* itemAt(const QModelIndex& index);
void removeAt(const QModelIndex& index);
void loadFile(const QString& path); void loadFile(const QString& path);
void addSet(GBACheatSet* set); void addSet(GBACheatSet* set);

View File

@ -17,6 +17,7 @@ using namespace QGBA;
CheatsView::CheatsView(GameController* controller, QWidget* parent) CheatsView::CheatsView(GameController* controller, QWidget* parent)
: QWidget(parent) : QWidget(parent)
, m_controller(controller)
, m_model(controller->cheatDevice()) , m_model(controller->cheatDevice())
{ {
m_ui.setupUi(this); m_ui.setupUi(this);
@ -25,6 +26,7 @@ CheatsView::CheatsView(GameController* controller, QWidget* parent)
connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load())); connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));
connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet())); connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet()));
connect(m_ui.remove, SIGNAL(clicked()), this, SLOT(removeSet()));
connect(controller, SIGNAL(gameStopped(GBAThread*)), &m_model, SLOT(invalidated())); connect(controller, SIGNAL(gameStopped(GBAThread*)), &m_model, SLOT(invalidated()));
connect(m_ui.add, &QPushButton::clicked, [this]() { connect(m_ui.add, &QPushButton::clicked, [this]() {
@ -50,7 +52,20 @@ void CheatsView::load() {
void CheatsView::addSet() { void CheatsView::addSet() {
GBACheatSet* set = new GBACheatSet; GBACheatSet* set = new GBACheatSet;
GBACheatSetInit(set, nullptr); GBACheatSetInit(set, nullptr);
m_controller->threadInterrupt();
m_model.addSet(set); m_model.addSet(set);
m_controller->threadContinue();
}
void CheatsView::removeSet() {
GBACheatSet* set;
QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes();
if (selection.count() != 1) {
return;
}
m_controller->threadInterrupt();
m_model.removeAt(selection[0]);
m_controller->threadContinue();
} }
void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callback) { void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callback) {
@ -60,9 +75,14 @@ void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callb
return; return;
} }
set = m_model.itemAt(selection[0]); set = m_model.itemAt(selection[0]);
if (!set) {
return;
}
m_controller->threadInterrupt();
QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts); QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts);
for (const QString& string : cheats) { for (const QString& string : cheats) {
callback(set, string.toLocal8Bit().constData()); callback(set, string.toLocal8Bit().constData());
} }
m_controller->threadContinue();
m_ui.codeEntry->clear(); m_ui.codeEntry->clear();
} }

View File

@ -29,11 +29,13 @@ public:
private slots: private slots:
void load(); void load();
void addSet(); void addSet();
void removeSet();
private: private:
void enterCheat(std::function<bool(GBACheatSet*, const char*)> callback); void enterCheat(std::function<bool(GBACheatSet*, const char*)> callback);
Ui::CheatsView m_ui; Ui::CheatsView m_ui;
GameController* m_controller;
CheatsModel m_model; CheatsModel m_model;
}; };

View File

@ -16,9 +16,6 @@
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="2" column="1" colspan="2"> <item row="2" column="1" colspan="2">
<widget class="QPushButton" name="remove"> <widget class="QPushButton" name="remove">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>Remove</string> <string>Remove</string>
</property> </property>
@ -129,6 +126,8 @@
<tabstop>cheatList</tabstop> <tabstop>cheatList</tabstop>
<tabstop>addSet</tabstop> <tabstop>addSet</tabstop>
<tabstop>load</tabstop> <tabstop>load</tabstop>
<tabstop>save</tabstop>
<tabstop>remove</tabstop>
<tabstop>codeEntry</tabstop> <tabstop>codeEntry</tabstop>
<tabstop>add</tabstop> <tabstop>add</tabstop>
<tabstop>addGSA</tabstop> <tabstop>addGSA</tabstop>