2015-07-05 23:06:06 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
#include "CheatManager.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "MessageManager.h"
|
2018-07-02 18:49:19 +00:00
|
|
|
#include "NotificationManager.h"
|
2015-07-05 23:06:06 +00:00
|
|
|
|
2018-07-01 19:21:05 +00:00
|
|
|
CheatManager::CheatManager(shared_ptr<Console> console)
|
2015-07-05 23:06:06 +00:00
|
|
|
{
|
2018-07-01 19:21:05 +00:00
|
|
|
_console = console;
|
|
|
|
|
2015-07-05 23:06:06 +00:00
|
|
|
for(int i = 0; i <= 0xFFFF; i++) {
|
|
|
|
_relativeCheatCodes.push_back(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CheatManager::DecodeValue(uint32_t code, uint32_t* bitIndexes, uint32_t bitCount)
|
|
|
|
{
|
|
|
|
uint32_t result = 0;
|
|
|
|
for(uint32_t i = 0; i < bitCount; i++) {
|
|
|
|
result <<= 1;
|
|
|
|
result |= (code >> bitIndexes[i]) & 0x01;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeInfo CheatManager::GetGGCodeInfo(string ggCode)
|
|
|
|
{
|
|
|
|
string ggLetters = "APZLGITYEOXUKSVN";
|
|
|
|
|
|
|
|
uint32_t rawCode = 0;
|
2015-07-07 01:31:32 +00:00
|
|
|
for(size_t i = 0, len = ggCode.size(); i < len; i++) {
|
2015-07-05 23:06:06 +00:00
|
|
|
rawCode |= ggLetters.find(ggCode[i]) << (i * 4);
|
|
|
|
}
|
|
|
|
|
2018-06-25 20:21:15 +00:00
|
|
|
CodeInfo code = { };
|
2015-07-05 23:06:06 +00:00
|
|
|
code.IsRelativeAddress = true;
|
|
|
|
code.CompareValue = -1;
|
|
|
|
uint32_t addressBits[15] = { 14, 13, 12, 19, 22, 21, 20, 7, 10, 9, 8, 15, 18, 17, 16 };
|
|
|
|
uint32_t valueBits[8] = { 3, 6, 5, 4, 23, 2, 1, 0 };
|
|
|
|
if(ggCode.size() == 8) {
|
|
|
|
//Bit 5 of the value is stored in a different location for 8-character codes
|
2017-05-18 03:19:06 +00:00
|
|
|
valueBits[4] = 31;
|
2015-07-05 23:06:06 +00:00
|
|
|
|
|
|
|
uint32_t compareValueBits[8] = { 27, 30, 29, 28, 23, 26, 25, 24 };
|
|
|
|
code.CompareValue = DecodeValue(rawCode, compareValueBits, 8);
|
|
|
|
}
|
|
|
|
code.Address = DecodeValue(rawCode, addressBits, 15) + 0x8000;
|
|
|
|
code.Value = DecodeValue(rawCode, valueBits, 8);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeInfo CheatManager::GetPARCodeInfo(uint32_t parCode)
|
|
|
|
{
|
|
|
|
uint32_t shiftValues[31] = {
|
|
|
|
3, 13, 14, 1, 6, 9, 5, 0, 12, 7, 2, 8, 10, 11, 4, //address
|
|
|
|
19, 21, 23, 22, 20, 17, 16, 18, //compare
|
|
|
|
29, 31, 24, 26, 25, 30, 27, 28 //value
|
|
|
|
};
|
|
|
|
uint32_t key = 0x7E5EE93A;
|
|
|
|
uint32_t xorValue = 0x5C184B91;
|
|
|
|
|
|
|
|
//Throw away bit 0, not used.
|
|
|
|
parCode >>= 1;
|
|
|
|
|
|
|
|
uint32_t result = 0;
|
|
|
|
for(int32_t i = 30; i >= 0; i--) {
|
|
|
|
if(((key ^ parCode) >> 30) & 0x01) {
|
|
|
|
result |= 0x01 << shiftValues[i];
|
|
|
|
key ^= xorValue;
|
|
|
|
}
|
|
|
|
parCode <<= 1;
|
|
|
|
key <<= 1;
|
|
|
|
}
|
|
|
|
|
2018-06-25 20:21:15 +00:00
|
|
|
CodeInfo code = { };
|
2015-07-05 23:06:06 +00:00
|
|
|
code.IsRelativeAddress = true;
|
|
|
|
code.Address = (result & 0x7fff) + 0x8000;
|
|
|
|
code.Value = (result >> 24) & 0xFF;
|
|
|
|
code.CompareValue = (result >> 16) & 0xFF;
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheatManager::AddCode(CodeInfo &code)
|
|
|
|
{
|
|
|
|
if(code.IsRelativeAddress) {
|
2017-08-08 17:01:55 +00:00
|
|
|
if(code.Address > 0xFFFF) {
|
|
|
|
//Invalid cheat, ignore it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-05 23:06:06 +00:00
|
|
|
if(_relativeCheatCodes[code.Address] == nullptr) {
|
|
|
|
_relativeCheatCodes[code.Address].reset(new vector<CodeInfo>());
|
|
|
|
}
|
|
|
|
_relativeCheatCodes[code.Address]->push_back(code);
|
|
|
|
} else {
|
|
|
|
_absoluteCheatCodes.push_back(code);
|
|
|
|
}
|
2018-07-02 18:49:19 +00:00
|
|
|
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::CheatAdded);
|
2015-07-05 23:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheatManager::AddGameGenieCode(string code)
|
|
|
|
{
|
2016-08-28 22:58:22 +00:00
|
|
|
CodeInfo info = GetGGCodeInfo(code);
|
|
|
|
AddCode(info);
|
2015-07-05 23:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheatManager::AddProActionRockyCode(uint32_t code)
|
|
|
|
{
|
2016-08-28 22:58:22 +00:00
|
|
|
CodeInfo info = GetPARCodeInfo(code);
|
|
|
|
AddCode(info);
|
2015-07-05 23:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheatManager::AddCustomCode(uint32_t address, uint8_t value, int32_t compareValue, bool isRelativeAddress)
|
|
|
|
{
|
|
|
|
CodeInfo code;
|
|
|
|
code.Address = address;
|
|
|
|
code.Value = value;
|
|
|
|
code.CompareValue = compareValue;
|
|
|
|
code.IsRelativeAddress = isRelativeAddress;
|
|
|
|
|
2016-08-28 22:58:22 +00:00
|
|
|
AddCode(code);
|
2015-07-05 23:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheatManager::ClearCodes()
|
|
|
|
{
|
2016-01-23 05:52:06 +00:00
|
|
|
bool cheatRemoved = false;
|
|
|
|
|
2015-07-05 23:06:06 +00:00
|
|
|
for(int i = 0; i <= 0xFFFF; i++) {
|
2016-08-28 22:58:22 +00:00
|
|
|
if(!_relativeCheatCodes[i]) {
|
2016-01-23 05:52:06 +00:00
|
|
|
cheatRemoved = true;
|
|
|
|
}
|
2016-08-28 22:58:22 +00:00
|
|
|
_relativeCheatCodes[i].reset();
|
2015-07-05 23:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 22:58:22 +00:00
|
|
|
cheatRemoved |= _absoluteCheatCodes.size() > 0;
|
|
|
|
_absoluteCheatCodes.clear();
|
2016-01-23 05:52:06 +00:00
|
|
|
|
|
|
|
if(cheatRemoved) {
|
2018-07-02 18:49:19 +00:00
|
|
|
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::CheatRemoved);
|
2015-07-05 23:06:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheatManager::ApplyRamCodes(uint16_t addr, uint8_t &value)
|
|
|
|
{
|
2018-07-01 19:21:05 +00:00
|
|
|
if(_relativeCheatCodes[addr] != nullptr) {
|
|
|
|
for(uint32_t i = 0, len = i < _relativeCheatCodes[addr]->size(); i < len; i++) {
|
|
|
|
CodeInfo code = _relativeCheatCodes[addr]->at(i);
|
2015-07-05 23:06:06 +00:00
|
|
|
if(code.CompareValue == -1 || code.CompareValue == value) {
|
|
|
|
value = code.Value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheatManager::ApplyPrgCodes(uint8_t *prgRam, uint32_t prgSize)
|
|
|
|
{
|
2018-07-01 19:21:05 +00:00
|
|
|
for(uint32_t i = 0, len = i < _absoluteCheatCodes.size(); i < len; i++) {
|
|
|
|
CodeInfo code = _absoluteCheatCodes[i];
|
2015-07-05 23:06:06 +00:00
|
|
|
if(code.Address < prgSize) {
|
|
|
|
if(code.CompareValue == -1 || code.CompareValue == prgRam[code.Address]) {
|
|
|
|
prgRam[code.Address] = code.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 01:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<CodeInfo> CheatManager::GetCheats()
|
|
|
|
{
|
|
|
|
//Used by NetPlay
|
|
|
|
vector<CodeInfo> cheats;
|
2018-07-01 19:21:05 +00:00
|
|
|
for(unique_ptr<vector<CodeInfo>> &codes : _relativeCheatCodes) {
|
2016-01-22 01:30:00 +00:00
|
|
|
if(codes) {
|
|
|
|
std::copy(codes.get()->begin(), codes.get()->end(), std::back_inserter(cheats));
|
|
|
|
}
|
|
|
|
}
|
2018-07-01 19:21:05 +00:00
|
|
|
std::copy(_absoluteCheatCodes.begin(), _absoluteCheatCodes.end(), std::back_inserter(cheats));
|
2016-01-22 01:30:00 +00:00
|
|
|
return cheats;
|
|
|
|
}
|
|
|
|
|
2016-08-28 22:58:22 +00:00
|
|
|
void CheatManager::SetCheats(CheatInfo cheats[], uint32_t length)
|
|
|
|
{
|
2018-07-01 19:21:05 +00:00
|
|
|
_console->Pause();
|
2016-08-28 22:58:22 +00:00
|
|
|
|
2018-07-01 19:21:05 +00:00
|
|
|
ClearCodes();
|
2016-08-28 22:58:22 +00:00
|
|
|
|
|
|
|
for(uint32_t i = 0; i < length; i++) {
|
|
|
|
CheatInfo &cheat = cheats[i];
|
2016-12-11 15:56:23 +00:00
|
|
|
switch(cheat.Type) {
|
2018-07-01 19:21:05 +00:00
|
|
|
case CheatType::Custom: AddCustomCode(cheat.Address, cheat.Value, cheat.UseCompareValue ? cheat.CompareValue : -1, cheat.IsRelativeAddress); break;
|
|
|
|
case CheatType::GameGenie: AddGameGenieCode(cheat.GameGenieCode); break;
|
|
|
|
case CheatType::ProActionRocky: AddProActionRockyCode(cheat.ProActionRockyCode); break;
|
2016-08-28 22:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 19:21:05 +00:00
|
|
|
_console->Resume();
|
2016-08-28 22:58:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 01:30:00 +00:00
|
|
|
void CheatManager::SetCheats(vector<CodeInfo> &cheats)
|
|
|
|
{
|
|
|
|
//Used by NetPlay
|
2018-07-01 19:21:05 +00:00
|
|
|
ClearCodes();
|
2016-01-22 01:30:00 +00:00
|
|
|
|
|
|
|
if(cheats.size() > 0) {
|
2016-02-19 18:05:04 +00:00
|
|
|
MessageManager::DisplayMessage("Cheats", cheats.size() > 1 ? "CheatsApplied" : "CheatApplied", std::to_string(cheats.size()));
|
2016-01-22 01:30:00 +00:00
|
|
|
for(CodeInfo &cheat : cheats) {
|
2018-07-01 19:21:05 +00:00
|
|
|
AddCode(cheat);
|
2016-01-22 01:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-05 23:06:06 +00:00
|
|
|
}
|