diff --git a/src/core/cheats.cpp b/src/core/cheats.cpp index 4cc757ede..6bb66ac53 100644 --- a/src/core/cheats.cpp +++ b/src/core/cheats.cpp @@ -529,6 +529,37 @@ void CheatList::ApplyCode(u32 index) m_codes[index].Apply(); } +const CheatCode* CheatList::FindCode(const char* name) const +{ + for (const CheatCode& cc : m_codes) + { + if (cc.description == name) + return &cc; + } + + return nullptr; +} + +const CheatCode* CheatList::FindCode(const char* group, const char* name) const +{ + for (const CheatCode& cc : m_codes) + { + if (cc.group == group && cc.description == name) + return &cc; + } + + return nullptr; +} + +void CheatList::MergeList(const CheatList& cl) +{ + for (const CheatCode& cc : cl.m_codes) + { + if (!FindCode(cc.group.c_str(), cc.description.c_str())) + AddCode(cc); + } +} + std::string CheatCode::GetInstructionsAsString() const { std::stringstream ss; diff --git a/src/core/cheats.h b/src/core/cheats.h index 44caf54b7..526707d79 100644 --- a/src/core/cheats.h +++ b/src/core/cheats.h @@ -102,6 +102,9 @@ public: ALWAYS_INLINE u32 GetCodeCount() const { return static_cast(m_codes.size()); } ALWAYS_INLINE bool IsCodeEnabled(u32 index) const { return m_codes[index].enabled; } + const CheatCode* FindCode(const char* name) const; + const CheatCode* FindCode(const char* group, const char* name) const; + void AddCode(CheatCode cc); void SetCode(u32 index, CheatCode cc); void RemoveCode(u32 i); @@ -125,6 +128,8 @@ public: void ApplyCode(u32 index); + void MergeList(const CheatList& cl); + private: std::vector m_codes; }; diff --git a/src/duckstation-qt/cheatmanagerdialog.cpp b/src/duckstation-qt/cheatmanagerdialog.cpp index e558fc773..ce20a4b3c 100644 --- a/src/duckstation-qt/cheatmanagerdialog.cpp +++ b/src/duckstation-qt/cheatmanagerdialog.cpp @@ -558,10 +558,7 @@ void CheatManagerDialog::importClicked() QtHostInterface::GetInstance()->executeOnEmulationThread( [&new_cheats]() { DebugAssert(System::HasCheatList()); - CheatList* list = System::GetCheatList(); - for (u32 i = 0; i < new_cheats.GetCodeCount(); i++) - list->AddCode(new_cheats.GetCode(i)); - + System::GetCheatList()->MergeList(new_cheats); QtHostInterface::GetInstance()->SaveCheatList(); }, true);