Merge pull request #16514 from rf2222222/libretro_cheat_support

Libretro: Cheat Support
This commit is contained in:
Henrik Rydgård 2022-12-09 12:16:43 +01:00 committed by GitHub
commit 03433c42ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,7 @@
#include "Common/File/VFS/VFS.h"
#include "Common/File/VFS/AssetReader.h"
#include "Common/Data/Text/I18n.h"
#include "Common/StringUtils.h"
#include "Core/Config.h"
#include "Core/ConfigValues.h"
@ -36,6 +37,8 @@
#include "Core/System.h"
#include "Core/CoreTiming.h"
#include "Core/HW/Display.h"
#include "Core/CwCheat.h"
#include "Core/ELF/ParamSFO.h"
#include "GPU/GPUState.h"
#include "GPU/GPUInterface.h"
@ -1668,9 +1671,86 @@ size_t retro_get_memory_size(unsigned id)
return 0;
}
void retro_cheat_reset(void) {}
void retro_cheat_reset(void) {
// Init Cheat Engine
CWCheatEngine *cheatEngine = new CWCheatEngine(g_paramSFO.GetDiscID());
Path file=cheatEngine->CheatFilename();
void retro_cheat_set(unsigned index, bool enabled, const char *code) { }
// Output cheats to cheat file
std::ofstream outFile;
outFile.open(file.c_str());
outFile << "_S " << g_paramSFO.GetDiscID() << std::endl;
outFile.close();
g_Config.bReloadCheats = true;
// Parse and Run the Cheats
cheatEngine->ParseCheats();
if (cheatEngine->HasCheats()) {
cheatEngine->Run();
}
}
void retro_cheat_set(unsigned index, bool enabled, const char *code) {
// Initialize Cheat Engine
CWCheatEngine *cheatEngine = new CWCheatEngine(g_paramSFO.GetDiscID());
cheatEngine->CreateCheatFile();
Path file=cheatEngine->CheatFilename();
// Read cheats file
std::vector<std::string> cheats;
std::ifstream cheat_content(file.c_str());
std::stringstream buffer;
buffer << cheat_content.rdbuf();
std::string existing_cheats=ReplaceAll(buffer.str(), std::string("\n_C"), std::string("|"));
SplitString(existing_cheats, '|', cheats);
// Generate Cheat String
std::stringstream cheat("");
cheat << (enabled ? "1 " : "0 ") << index << std::endl;
std::string code_str(code);
std::vector<std::string> codes;
code_str=ReplaceAll(code_str, std::string(" "), std::string("+"));
SplitString(code_str, '+', codes);
int part=0;
for (int i=0; i < codes.size(); i++) {
if (codes[i].size() <= 2) {
// _L _M ..etc
// Assume _L
} else if (part == 0) {
cheat << "_L " << codes[i] << " ";
part++;
} else {
cheat << codes[i] << std::endl;
part=0;
}
}
// Add or Replace the Cheat
if (index + 1 < cheats.size()) {
cheats[index + 1]=cheat.str();
} else {
cheats.push_back(cheat.str());
}
// Output cheats to cheat file
std::ofstream outFile;
outFile.open(file.c_str());
outFile << "_S " << g_paramSFO.GetDiscID() << std::endl;
for (int i=1; i < cheats.size(); i++) {
outFile << "_C" << cheats[i] << std::endl;
}
outFile.close();
g_Config.bReloadCheats = true;
// Parse and Run the Cheats
cheatEngine->ParseCheats();
if (cheatEngine->HasCheats()) {
cheatEngine->Run();
}
}
int System_GetPropertyInt(SystemProperty prop)
{