Skip to content

Commit

Permalink
Cheats: Simplify code merging on import
Browse files Browse the repository at this point in the history
The save file already does replacement.
  • Loading branch information
stenzek committed Nov 3, 2024
1 parent a8bc364 commit 0fc719c
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 18 deletions.
14 changes: 1 addition & 13 deletions src/core/cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ std::string Cheats::FormatCodeForFile(const CodeInfo& code)
fmt::format_to(appender, "OptionRange = {}:{}\n", code.option_range_start, code.option_range_end);
}

fmt::format_to(appender, "{}\n", code.body);
fmt::format_to(appender, "{}\n\n", code.body, code.body.ends_with('\n') ? "\n" : "");
return std::string(buf.begin(), buf.end());
}

Expand Down Expand Up @@ -700,18 +700,6 @@ bool Cheats::SaveCodesToFile(const char* path, const CodeInfoList& codes, Error*
return true;
}

void Cheats::MergeCheatList(CodeInfoList* dst, CodeInfoList src)
{
for (CodeInfo& code : src)
{
CodeInfo* existing_code = FindCodeInInfoList(*dst, code.name);
if (existing_code)
*existing_code = std::move(code);
else
dst->push_back(std::move(code));
}
}

std::string Cheats::GetChtFilename(const std::string_view serial, std::optional<GameHash> hash, bool cheats)
{
return Path::Combine(cheats ? EmuFolders::Cheats : EmuFolders::Patches, GetChtTemplate(serial, hash, false));
Expand Down
3 changes: 0 additions & 3 deletions src/core/cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ extern bool UpdateCodeInFile(const char* path, const std::string_view name, cons
/// Updates or adds multiple codes to the file, rewriting it.
extern bool SaveCodesToFile(const char* path, const CodeInfoList& codes, Error* error);

/// Merges two cheat lists, with any duplicates in the new list taking precedence.
extern void MergeCheatList(CodeInfoList* dst, CodeInfoList src);

/// Returns the path to a new cheat/patch cht for the specified serial and hash.
extern std::string GetChtFilename(const std::string_view serial, std::optional<GameHash> hash, bool cheats);

Expand Down
3 changes: 1 addition & 2 deletions src/duckstation-qt/gamecheatsettingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,7 @@ void GameCheatSettingsWidget::importCodes(const std::string& file_contents)
return;
}

Cheats::MergeCheatList(&m_codes, std::move(new_codes));
if (!Cheats::SaveCodesToFile(getPathForSavingCheats().c_str(), m_codes, &error))
if (!Cheats::SaveCodesToFile(getPathForSavingCheats().c_str(), new_codes, &error))
{
QMessageBox::critical(this, tr("Error"),
tr("Failed to save file:\n%1").arg(QString::fromStdString(error.GetDescription())));
Expand Down

0 comments on commit 0fc719c

Please sign in to comment.