Libretro: Added support for GG/PAR cheats

This commit is contained in:
Sour 2019-10-16 20:55:09 -04:00
parent 207282ed1e
commit 388f62f57b
3 changed files with 63 additions and 2 deletions

View file

@ -2,6 +2,7 @@
#include "CheatManager.h"
#include "MessageManager.h"
#include "Console.h"
#include "../Utilities/HexUtilities.h"
CheatManager::CheatManager(Console* console)
{
@ -60,6 +61,58 @@ void CheatManager::ClearCheats(bool showMessage)
memset(_bankHasCheats, 0, sizeof(_bankHasCheats));
}
void CheatManager::AddStringCheat(string code)
{
static string _convertTable = "DF4709156BC8A23E";
auto lock = _console->AcquireLock();
std::transform(code.begin(), code.end(), code.begin(), ::toupper);
if(code.size() == 9 && code[4] == '-') {
uint32_t rawValue = 0;
for(int i = 0; i < code.size(); i++) {
if(code[i] != '-') {
rawValue <<= 4;
size_t pos = _convertTable.find_first_of(code[i]);
if(pos == string::npos) {
//Invalid code
return;
}
rawValue |= (uint32_t)pos;
}
}
CheatCode cheat;
cheat.Address = (
((rawValue & 0x3C00) << 10) |
((rawValue & 0x3C) << 14) |
((rawValue & 0xF00000) >> 8) |
((rawValue & 0x03) << 10) |
((rawValue & 0xC000) >> 6) |
((rawValue & 0xF0000) >> 12) |
((rawValue & 0x3C0) >> 6)
);
cheat.Value = rawValue >> 24;
AddCheat(cheat);
} else if(code.size() == 8) {
for(int i = 0; i < code.size(); i++) {
if((code[i] < 'A' || code[i] > 'F') && (code[i] < '0' && code[i] > '9')) {
//Invalid code
return;
}
}
uint32_t rawValue = HexUtilities::FromHex(code);
CheatCode cheat;
cheat.Address = rawValue >> 8;
cheat.Value = rawValue & 0xFF;
AddCheat(cheat);
}
}
vector<CheatCode> CheatManager::GetCheats()
{
return _cheats;

View file

@ -16,12 +16,14 @@ private:
bool _hasCheats = false;
bool _bankHasCheats[0x100] = {};
vector<CheatCode> _cheats;
void AddCheat(CheatCode code);
public:
CheatManager(Console* console);
void AddStringCheat(string code);
void SetCheats(vector<CheatCode> codes);
void SetCheats(uint32_t codes[], uint32_t length);
void ClearCheats(bool showMessage = true);

View file

@ -13,6 +13,7 @@
#include "../Core/VideoRenderer.h"
#include "../Core/EmuSettings.h"
#include "../Core/SaveStateManager.h"
#include "../Core/CheatManager.h"
#include "../Utilities/snes_ntsc.h"
#include "../Utilities/FolderUtilities.h"
#include "../Utilities/HexUtilities.h"
@ -71,7 +72,8 @@ extern "C" {
_console.reset(new Console());
_console->Initialize();
KeyManager::SetSettings(_console->GetSettings().get());
_renderer.reset(new LibretroRenderer(_console, retroEnv));
_soundManager.reset(new LibretroSoundManager(_console));
_keyManager.reset(new LibretroKeyManager(_console));
@ -457,10 +459,14 @@ extern "C" {
RETRO_API void retro_cheat_reset()
{
_console->GetCheatManager()->ClearCheats();
}
RETRO_API void retro_cheat_set(unsigned index, bool enabled, const char *codeStr)
{
if(codeStr) {
_console->GetCheatManager()->AddStringCheat(codeStr);
}
}
void update_input_descriptors()