diff --git a/Common/StringUtils.cpp b/Common/StringUtils.cpp index b3b720bad1..992e6ce208 100644 --- a/Common/StringUtils.cpp +++ b/Common/StringUtils.cpp @@ -22,6 +22,27 @@ #include "CommonPaths.h" #include "StringUtils.h" +long parseHexLong(std::string s) { + long value = 0; + + if (s.substr(0,2) == "0x") { + //s = s.substr(2); + } + value = strtoul(s.c_str(),0, 0); + return value; +} +long parseLong(std::string s) { + long value = 0; + if (s.substr(0,2) == "0x") { + s = s.substr(2); + value = strtol(s.c_str(),NULL, 16); + } else { + value = strtol(s.c_str(),NULL, 10); + } + return value; +} + + // faster than sscanf bool AsciiToHex(const char* _szValue, u32& result) { diff --git a/Common/StringUtils.h b/Common/StringUtils.h index 220b70f8c4..0c27481d88 100644 --- a/Common/StringUtils.h +++ b/Common/StringUtils.h @@ -22,7 +22,8 @@ #include #include "Common.h" - +long parseHexLong(std::string s); +long parseLong(std::string s); std::string StringFromFormat(const char* format, ...); // Cheap! bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args); diff --git a/Core/Config.cpp b/Core/Config.cpp index 0cc73bcbc2..c90480c01c 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -63,6 +63,7 @@ void Config::Load(const char *iniFileName) general->Get("ShowDebuggerOnLoad", &bShowDebuggerOnLoad, false); general->Get("Language", &languageIni, "en_US"); general->Get("NumWorkerThreads", &iNumWorkerThreads, cpu_info.num_cores); + general->Get("EnableCheats", &iEnableCheats, 0); general->Get("MaxRecent", &iMaxRecent, 12); // Fix issue from switching from uint (hex in .ini) to int (dec) if (iMaxRecent == 0) @@ -183,6 +184,7 @@ void Config::Save() general->Set("Language", languageIni); general->Set("NumWorkerThreads", iNumWorkerThreads); general->Set("MaxRecent", iMaxRecent); + general->Set("EnableCheats", iEnableCheats); IniFile::Section *cpu = iniFile.GetOrCreateSection("CPU"); cpu->Set("Jit", bJit); diff --git a/Core/Config.h b/Core/Config.h index 090f058a9a..25ee475276 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -80,6 +80,7 @@ public: bool bTexDeposterize; int iFpsLimit; int iMaxRecent; + int iEnableCheats; // Sound bool bEnableSound; diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index ec889d4446..13e4393057 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -109,6 +109,7 @@ Fast USE_FFMPEG;_USE_DSHOW_;_MBCS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS Speed + MultiThreadedDLL true @@ -157,6 +158,7 @@ + @@ -332,6 +334,7 @@ + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index d9be5a4586..f1f807220b 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -427,6 +427,9 @@ MIPS\JitCommon + + Core + @@ -777,6 +780,7 @@ HLE\Libraries +<<<<<<< HEAD HLE\Libraries @@ -791,6 +795,11 @@ MIPS\JitCommon +======= + + + Core +>>>>>>> eaf060015d9dc1ec3caf2f9f576b62eb5c5572f4 @@ -799,4 +808,4 @@ - \ No newline at end of file + diff --git a/Core/CwCheat.cpp b/Core/CwCheat.cpp new file mode 100644 index 0000000000..35bcfe3b77 --- /dev/null +++ b/Core/CwCheat.cpp @@ -0,0 +1,486 @@ +#include "CwCheat.h" +#include "../Core/CoreTiming.h" +#include "../Core/CoreParameter.h" +#include "StringUtils.h" +#include "Common/FileUtil.h" +#include "Config.h" +#include "MIPS/MIPS.h" + +using namespace std; + +string title, title2, title3; +static int CheatEvent = -1; +CWCheatEngine member; +CWCheatEngine cheatsThread; +void hleCheat(u64 userdata, int cyclesLate); + + +void __CheatInit() { + title ="Cheats/"; + title2 = g_paramSFO.GetValueString("DISC_ID").c_str(); + title3 = title + title2 + ".ini"; + CheatEvent = CoreTiming::RegisterEvent("CheatEvent", &hleCheat); + CoreTiming::ScheduleEvent(msToCycles(77), CheatEvent, 0); + File::CreateFullPath("Cheats"); + if (!File::Exists(title3)) { + File::CreateEmptyFile(title3); + } +} +void __CheatShutdown() { + member.Exit(); +} + +void hleCheat(u64 userdata, int cyclesLate) { + CoreTiming::ScheduleEvent(msToCycles(77), CheatEvent, 0); + if (g_Config.iEnableCheats == 1) { + member.Run(); + } +} + +CWCheatEngine::CWCheatEngine() { +} + +void CWCheatEngine::Exit() { + exit2 = true; +} + +string CWCheatEngine::GetNextCode() { + string code; + string modifier = "_L"; + char modifier2 = '0'; + while (true) { + if (currentCode >= codes.size()) { + code.clear(); + break; + } + + code = codes[currentCode++]; + trim2(code); + + if (code.substr(0,2) == modifier) { + code = code.substr(3); + } + else if (code[0] == modifier2) { + break; + } + } + return code; +} + +void CWCheatEngine::skipCodes(int count) { + for (int i = 0; i < count; i ++) { + if (GetNextCode() == "") { + break; + } + } +} + +void CWCheatEngine::skipAllCodes() { + currentCode = codes.size(); +} + +int CWCheatEngine::getAddress(int value) { + // The User space base address has to be added to given value + return (value + 0x08800000) & 0x3FFFFFFF; +} + + + + +void CWCheatEngine::AddCheatLine(string& line) { + //Need GUI text area here + string cheatCodes; + if (cheatCodes.length() <= 0) { + cheatCodes = line; + } else { + cheatCodes += "\n" + line; + } +} + +inline void trim2(string& str) +{ + string::size_type pos = str.find_last_not_of(' '); + if(pos != string::npos) { + str.erase(pos + 1); + pos = str.find_first_not_of(' '); + if(pos != string::npos) str.erase(0, pos); + } + else str.erase(str.begin(), str.end()); +} + +inline vector makeCodeParts(string l) { + vector parts; + char split_char = '\n'; + char empty = ' '; + + for (int i=0; i < l.length(); i++) { + if (l[i] == empty) { + l[i] = '\n'; + } + } + trim2(l); + istringstream iss (l); + std::string each; + while (std::getline(iss, each, split_char)) { + parts.push_back(each); + } + return parts; +} + +vector CWCheatEngine::GetCodesList() { + string line; + string path = __FILE__; + path = path.substr(0,1+path.find_last_of('\\')); + path+= title3; + char* skip = "//"; + vector codesList; //Read from INI here + ifstream list(title3); + for (int i = 0; !list.eof(); i ++) { + getline(list,line, '\n'); + if (line.substr(0,2) == skip) + {line.clear(); + } + else { + codesList.push_back(line); + } + } + for( int i = 0; i < codesList.size(); i++) { + trim2(codesList[i]); + } + return codesList; +} + +void CWCheatEngine::Run() { + CWCheatEngine cheats; + exit2 = false; + while (!exit2) { + codes = cheats.GetCodesList(); //UI Member + currentCode = 0; + + while (true) { + string code = GetNextCode(); + if (code == "") { + Exit(); + break; + } + vectorparts = makeCodeParts(code); + if (parts[0] == "" || parts.size() < 2) { + continue; + } + + int value; + trim2(parts[0]); + trim2(parts[1]); + //cout << parts[0] << endl << parts[1]; + unsigned int comm = (unsigned int)parseHexLong(parts[0]); + int arg = (int)parseHexLong(parts[1]); + int addr = getAddress(comm & 0x0FFFFFFF); + + switch (comm >> 28) { + case 0: // 8-bit write. + if (Memory::IsValidAddress(addr)){ + Memory::Write_U8((u8) arg, addr); + } + break; + case 0x1: // 16-bit write + if (Memory::IsValidAddress(addr)){ + Memory::Write_U16((u16) arg, addr); + } + break; + case 0x2: // 32-bit write + if (Memory::IsValidAddress(addr)){ + Memory::Write_U32((u32) arg, addr); + } + break; + case 0x3: // Increment/Decrement + { + addr = getAddress(arg); + value = 0; + int increment = 0; + // Read value from memory + switch ((comm >> 20) & 0xF) { + case 1: + case 2: // 8-bit + value = Memory::Read_U8(addr); + increment = comm & 0xFF; + break; + case 3: + case 4: // 16-bit + value = Memory::Read_U16(addr); + increment = comm & 0xFFFF; + break; + case 5: + case 6: // 32-bit + value = Memory::Read_U32(addr); + code = GetNextCode(); + parts = makeCodeParts(code); + trim2(parts[0]); + if ( parts[0] != "") { + increment = (int) parseHexLong(parts[0]); + } + break; + } + // Increment/Decrement value + switch ((comm >> 20) & 0xF) { + case 1: + case 3: + case 5: // increment + value += increment; + break; + case 2: + case 4: + case 6: // Decrement + value -= increment; + break; + } + // Write value back to memory + switch ((comm >> 20) & 0xF) { + case 1: + case 2: // 8-bit + Memory::Write_U8((u8) value, addr); + break; + case 3: + case 4: // 16-bit + Memory::Write_U16((u16) value, addr); + break; + case 5: + case 6: // 32-bit + Memory::Write_U32((u32) value, addr); + break; + } + break; } + case 0x4: // 32-bit patch code + code = GetNextCode(); + parts = makeCodeParts(code); + trim2(parts[0]); + trim2(parts[1]); + if (parts[0] != "") { + int data = (int) parseHexLong(parts[0]); + int dataAdd = (int) parseHexLong(parts[1]); + + int maxAddr = (arg >> 16) & 0xFFFF; + int stepAddr = (arg & 0xFFFF) * 4; + for (int a = 0; a < maxAddr; a++) { + if (Memory::IsValidAddress(addr)) { + Memory::Write_U32((u32) data, addr); + } + addr += stepAddr; + data += dataAdd; + } + } + break; + case 0x5: // Memcpy command + code = GetNextCode(); + parts = makeCodeParts(code); + trim2(parts[0]); + trim2(parts[1]); + if (parts[0] != "") { + int destAddr = (int) parseHexLong(parts[0]); + if (Memory::IsValidAddress(addr) && Memory::IsValidAddress(destAddr)) { + Memory::Memcpy(destAddr, Memory::GetPointer(addr), arg); + } + } + break; + case 0x6: // Pointer commands + code = GetNextCode(); + parts = makeCodeParts(code); + trim2(parts[0]); + trim2(parts[1]); + if (parts[0] != "") { + int arg2 = (int) parseHexLong(parts[0]); + int offset = (int) parseHexLong(parts[1]); + int baseOffset = (arg2 >> 20) * 4; + int base = Memory::Read_U32(addr + baseOffset); + int count = arg2 & 0xFFFF; + int type = (arg2 >> 16) & 0xF; + for (int i = 1; i < count; i ++ ) { + if (i+1 < count) { + code = GetNextCode(); + parts = makeCodeParts(code); + trim2(parts[0]); + trim2(parts[1]); + int arg3 = (int) parseHexLong(parts[0]); + int arg4 = (int) parseHexLong(parts[1]); + int comm3 = arg3 >> 28; + switch (comm3) { + case 0x1: // type copy byte + { + int srcAddr = Memory::Read_U32(addr) + offset; + int dstAddr = Memory::Read_U16(addr + baseOffset) + (arg3 & 0x0FFFFFFF); + Memory::Memcpy(dstAddr, Memory::GetPointer(srcAddr), arg); + type = -1; //Done + break; } + case 0x2: + case 0x3: // type pointer walk + { + int walkOffset = arg3 & 0x0FFFFFFF; + if (comm3 == 0x3) { + walkOffset = -walkOffset; + } + base = Memory::Read_U32(base + walkOffset); + int comm4 = arg4 >> 28; + switch (comm4) { + case 0x2: + case 0x3: // type pointer walk + walkOffset = arg4 & 0x0FFFFFFF; + if (comm4 == 0x3) { + walkOffset = -walkOffset; + } + base = Memory::Read_U32(base + walkOffset); + break; + } + break; } + case 0x9: // type multi address write + base += arg3 & 0x0FFFFFFF; + arg += arg4; + break; + } + } + } + + switch (type) { + case 0: // 8 bit write + Memory::Write_U8((u8) arg, base + offset); + break; + case 1: // 16-bit write + Memory::Write_U16((u16) arg, base + offset); + break; + case 2: // 32-bit write + Memory::Write_U32((u32) arg, base + offset); + break; + case 3: // 8 bit inverse write + Memory::Write_U8((u8) arg, base - offset); + break; + case 4: // 16-bit inverse write + Memory::Write_U16((u16) arg, base - offset); + break; + case 5: // 32-bit inverse write + Memory::Write_U32((u32) arg, base - offset); + break; + case -1: // Operation already performed, nothing to do + break; + } + } + break; + case 0x7: // Boolean commands. + switch (arg >> 16) { + case 0x0000: // 8-bit OR. + if (Memory::IsValidAddress(addr)) { + int val1 = (int) (arg & 0xFF); + int val2 = (int) Memory::Read_U8(addr); + Memory::Write_U8((u8) (val1 | val2), addr); + } + break; + case 0x0002: // 8-bit AND. + if (Memory::IsValidAddress(addr)) { + int val1 = (int) (arg & 0xFF); + int val2 = (int) Memory::Read_U8(addr); + Memory::Write_U8((u8) (val1 & val2), addr); + } + break; + case 0x0004: // 8-bit XOR. + if (Memory::IsValidAddress(addr)) { + int val1 = (int) (arg & 0xFF); + int val2 = (int) Memory::Read_U8(addr); + Memory::Write_U8((u8) (val1 ^ val2), addr); + } + break; + case 0x0001: // 16-bit OR. + if (Memory::IsValidAddress(addr)) { + short val1 = (short) (arg & 0xFFFF); + short val2 = (short) Memory::Read_U16(addr); + Memory::Write_U16((u16) (val1 | val2), addr); + } + break; + case 0x0003: // 16-bit AND. + if (Memory::IsValidAddress(addr)) { + short val1 = (short) (arg & 0xFFFF); + short val2 = (short) Memory::Read_U16(addr); + Memory::Write_U16((u16) (val1 & val2), addr); + } + break; + case 0x0005: // 16-bit OR. + if (Memory::IsValidAddress(addr)) { + short val1 = (short) (arg & 0xFFFF); + short val2 = (short) Memory::Read_U16(addr); + Memory::Write_U16((u16) (val1 ^ val2), addr); + } + break; + } + break; + case 0x8: // 8-bit and 16-bit patch code + code = GetNextCode(); + parts = makeCodeParts(code); + trim2(parts[0]); + trim2(parts[1]); + if (parts[0] != "") { + int data = (int) parseHexLong(parts[0]); + int dataAdd = (int) parseHexLong(parts[1]); + + bool is8Bit = (data >> 16) == 0x0000; + int maxAddr = (arg >> 16) & 0xFFFF; + int stepAddr = (arg & 0xFFFF) * (is8Bit ? 1 : 2); + for (int a = 0; a < maxAddr; a++) { + if (Memory::IsValidAddress(addr)) { + if (is8Bit) { + Memory::Write_U8((u8) (data & 0xFF), addr); + } + else { + Memory::Write_U16((u16) (data & 0xFFFF), addr); + } + } + addr += stepAddr; + data += dataAdd; + } + } + break; + case 0xB: // Time command (not sure what to do?) + break; + case 0xC: // Code stopper + if (Memory::IsValidAddress(addr)) { + value = Memory::Read_U32(addr); + if (value != arg) { + skipAllCodes(); + } + } + break; + case 0xD: // Test commands & Jocker codes ( Someone will have to help me with these) + break; + case 0xE: // Test commands, multiple skip + { + bool is8Bit = (comm >> 24) == 0x1; + addr = getAddress(arg & 0x0FFFFFFF); + if (Memory::IsValidAddress(addr)) { + int memoryValue = is8Bit ? Memory::Read_U8(addr) : Memory::Read_U16(addr); + int testValue = comm & (is8Bit ? 0xFF : 0xFFFF); + bool executeNextLines = false; + switch ( arg >> 28) { + case 0x0: // Equal + executeNextLines = memoryValue == testValue; + break; + case 0x1: // Not Equal + executeNextLines = memoryValue != testValue; + break; + case 0x2: // Less Than + executeNextLines = memoryValue < testValue; + break; + case 0x3: // Greater Than + executeNextLines = memoryValue > testValue; + break; + } + if (!executeNextLines) { + int skip = (comm >> 16) & (is8Bit ? 0xFF : 0xFFF); + skipCodes(skip); + } + } + break; + } + } + } + } + // exiting... + Exit(); +} + + + diff --git a/Core/CwCheat.h b/Core/CwCheat.h new file mode 100644 index 0000000000..5454c8d75f --- /dev/null +++ b/Core/CwCheat.h @@ -0,0 +1,44 @@ + +#include +#include +#include +#include +#include "Core\MemMap.h" + +void __CheatInit(); +void __CheatShutdown(); +//void Register_CwCheat(); +using namespace std; + + + +vector makeCodeParts(string l); +void trim2(string& str); + + +class CWCheatEngine { +private: + static long const serialVersionUID = 6791588139795694296L; + static long const int cheatsThreadSleepMillis = 5; + bool cheatsOn; + std::vector codes; + int currentCode; + bool exit2; + void skipCodes(int count); + void skipAllCodes(); + int getAddress(int value); + +public: + CWCheatEngine(); + string String(); + void AddCheatLine(string& line); + vector GetCodesList(); + + void Exit(); + void Run(); + string GetNextCode(); +}; + + + + diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index 42f4aa0774..611c9cfef3 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -16,6 +16,7 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "Core/Config.h" +#include "Core/Cwcheat.h" #include "Core/HLE/HLE.h" #include "Core/MIPS/MIPS.h" #include "Core/MIPS/MIPSCodeUtils.h" @@ -119,6 +120,7 @@ void __KernelInit() __FontInit(); __NetInit(); __VaudioInit(); + __CheatInit(); SaveState::Init(); // Must be after IO, as it may create a directory @@ -160,6 +162,7 @@ void __KernelShutdown() __KernelThreadingShutdown(); __KernelMemoryShutdown(); __InterruptsShutdown(); + __CheatShutdown(); CoreTiming::ClearPendingEvents(); CoreTiming::UnregisterAllEvents(); diff --git a/UI/MenuScreens.cpp b/UI/MenuScreens.cpp index 5606c4c585..bfe7ec1f30 100644 --- a/UI/MenuScreens.cpp +++ b/UI/MenuScreens.cpp @@ -939,7 +939,15 @@ void SystemScreen::render() { if (UICheckBox(GEN_ID, x, y += stride, s->T("12HR Time Format"), ALIGN_TOPLEFT, &tf)) { g_Config.itimeformat = tf ? 1 : 0; } - + bool bEnableCheats = g_Config.iEnableCheats != 0; + UICheckBox(GEN_ID, x, y += stride, s->T("Enable Cheats"), ALIGN_TOPLEFT, &bEnableCheats); + if (bEnableCheats) { + g_Config.iEnableCheats = 1; + } + else { + g_Config.iEnableCheats = 0; + } + bool reportingEnabled = Reporting::IsEnabled(); const static std::string reportHostOfficial = "report.ppsspp.org"; if (UICheckBox(GEN_ID, x, y += stride, s->T("Enable Compatibility Server Reports"), ALIGN_TOPLEFT, &reportingEnabled)) { diff --git a/Windows/WndMainWindow.cpp b/Windows/WndMainWindow.cpp index b57eedb4e2..2ea81402c3 100644 --- a/Windows/WndMainWindow.cpp +++ b/Windows/WndMainWindow.cpp @@ -188,6 +188,9 @@ namespace MainWindow void setFpsLimit(int fps) { g_Config.iFpsLimit = fps; } + void enableCheats(int cheats){ + g_Config.iEnableCheats = cheats; + } BOOL Show(HINSTANCE hInstance, int nCmdShow) {