Skip to content

Commit

Permalink
Merge pull request #97 from retro-wertz/fix_cheat
Browse files Browse the repository at this point in the history
Backport cheat support fixes
  • Loading branch information
inactive123 authored May 30, 2018
2 parents fbb708c + 1c03729 commit d69855a
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <string>
#include <vector>

#include "libretro.h"

Expand Down Expand Up @@ -580,7 +582,7 @@ void retro_cheat_reset(void)

void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
const char *begin, *c;
/*const char *begin, *c;
begin = c = code;
Expand Down Expand Up @@ -617,7 +619,68 @@ void retro_cheat_set(unsigned index, bool enabled, const char *code)
log_cb(RETRO_LOG_ERROR, "[VBA] Invalid cheat code '%s'\n", buf);
}
} while (*c++);
} while (*c++);*/

#define ISHEXDEC \
((codeLine[cursor] >= '0') && (codeLine[cursor] <= '9')) || \
((codeLine[cursor] >= 'a') && (codeLine[cursor] <= 'f')) || \
((codeLine[cursor] >= 'A') && (codeLine[cursor] <= 'F')) \

std::string codeLine = code;
std::string name = "cheat_" + index;
int matchLength = 0;
std::vector<std::string> codeParts;
int cursor;

//Break the code into Parts
for (cursor=0;;cursor++)
{
if (ISHEXDEC)
matchLength++;
else
{
if (matchLength)
{
if (matchLength > 8)
{
codeParts.push_back(codeLine.substr(cursor - matchLength, 8));
codeParts.push_back(codeLine.substr(cursor - matchLength + 8, matchLength - 8));
}
else
codeParts.push_back(codeLine.substr(cursor - matchLength, matchLength));

matchLength = 0;
}
}
if (!codeLine[cursor])
break;
}

//Add to core
for (cursor = 0; cursor<codeParts.size(); cursor += 2)
{
std::string codeString;
codeString += codeParts[cursor];

if (codeParts[cursor + 1].length() == 8)
{
codeString += codeParts[cursor + 1];
cheatsAddGSACode(codeString.c_str(), name.c_str(), true);
}
else if (codeParts[cursor + 1].length() == 4)
{
codeString += " ";
codeString += codeParts[cursor + 1];
cheatsAddCBACode(codeString.c_str(), name.c_str());
}
else
{
codeString += " ";
codeString += codeParts[cursor + 1];
log_cb(RETRO_LOG_ERROR, "Invalid cheat code '%s'\n", codeString.c_str());
}
log_cb(RETRO_LOG_INFO, "Cheat code added: '%s'\n", codeString.c_str());
}
}

bool retro_load_game(const struct retro_game_info *game)
Expand Down

0 comments on commit d69855a

Please sign in to comment.