Make cheats struct more versatile

This commit is contained in:
offtkp 2023-10-24 16:52:08 +03:00
parent d4a84c337e
commit 7e7d4f7f16
4 changed files with 82 additions and 5 deletions
src/core

View file

@ -7,9 +7,45 @@ void Cheats::reset() {
ar.reset(); // Reset ActionReplay
}
void Cheats::addCheat(const Cheat& cheat) {
cheats.push_back(cheat);
uint32_t Cheats::addCheat(const Cheat& cheat) {
cheatsLoaded = true;
// Find an empty slot if a cheat was previously removed
for (size_t i = 0; i < cheats.size(); i++) {
if (cheats[i].type == CheatType::None) {
cheats[i] = cheat;
return i;
}
}
// Otherwise, just add a new slot
cheats.push_back(cheat);
return cheats.size() - 1;
}
void Cheats::removeCheat(uint32_t id) {
if (id >= cheats.size()) return;
// Not using std::erase because we don't want to invalidate cheat IDs
cheats[id].type = CheatType::None;
cheats[id].instructions.clear();
// Check if no cheats are loaded
for (const auto& cheat : cheats) {
if (cheat.type != CheatType::None) return;
}
cheatsLoaded = false;
}
void Cheats::enableCheat(uint32_t id) {
if (id >= cheats.size()) return;
cheats[id].enabled = true;
}
void Cheats::disableCheat(uint32_t id) {
if (id >= cheats.size()) return;
cheats[id].enabled = false;
}
void Cheats::clear() {
@ -19,6 +55,8 @@ void Cheats::clear() {
void Cheats::run() {
for (const Cheat& cheat : cheats) {
if (!cheat.enabled) continue;
switch (cheat.type) {
case CheatType::ActionReplay: {
ar.runCheat(cheat.instructions);