From 628f5abaac0328aea92933dbc8837725c6bd407c Mon Sep 17 00:00:00 2001 From: GovanifY Date: Wed, 23 Jan 2019 02:01:30 +0100 Subject: [PATCH] PCSX2: Remove the arbitrary limit on patches by converting the patch list to a vector. (#2797) Pnach had a limit that, while increased at some point to 2048, is still not enough for everyone. This uses a vector to avoid that limit, as there is no reason to keep it and people loading pnach with over 2048 patches most likely know what they are doing. Inipatch group member was also unused in the whole codebase so I did some cleanup and removed it. --- pcsx2/Patch.cpp | 83 +++++++++++++++----------------- pcsx2/Patch.h | 3 -- pcsx2/windows/cheats/browser.cpp | 13 +++-- 3 files changed, 46 insertions(+), 53 deletions(-) diff --git a/pcsx2/Patch.cpp b/pcsx2/Patch.cpp index aead3227a4..468cd87720 100644 --- a/pcsx2/Patch.cpp +++ b/pcsx2/Patch.cpp @@ -22,6 +22,7 @@ #include "GameDatabase.h" #include +#include #include #include #include @@ -33,9 +34,7 @@ extern void _ApplyPatch(IniPatch *p); -IniPatch Patch[ MAX_PATCH ]; - -int patchnumber = 0; +std::vector Patch; wxString strgametitle; @@ -115,7 +114,7 @@ static void inifile_command(const wxString& cmd) // Is this really what we want to be doing here? Seems like just leaving it empty/blank // would make more sense... --air - if (set.rvalue.IsEmpty()) set.rvalue = set.lvalue; + if (set.rvalue.IsEmpty()) set.rvalue = set.lvalue; /*int code = */PatchTableExecute(set, commands_patch); } @@ -126,7 +125,7 @@ void TrimPatches(wxString& s) { wxStringTokenizer tkn( s, L"\n" ); - while(tkn.HasMoreTokens()) { + while(tkn.HasMoreTokens()) { inifile_command(tkn.GetNextToken()); } } @@ -152,29 +151,29 @@ int LoadPatchesFromGamesDB(const wxString& crc, const Game_Data& game) if (patchFound) TrimPatches(patch); - return patchnumber; + return Patch.size(); } void inifile_processString(const wxString& inStr) { - wxString str(inStr); - inifile_trim(str); - if (!str.IsEmpty()) inifile_command(str); + wxString str(inStr); + inifile_trim(str); + if (!str.IsEmpty()) inifile_command(str); } // This routine receives a file from inifile_read, trims it, // Then sends the command to be parsed. void inifile_process(wxTextFile &f1 ) { - for (uint i = 0; i < f1.GetLineCount(); i++) - { - inifile_processString(f1[i]); - } + for (uint i = 0; i < f1.GetLineCount(); i++) + { + inifile_processString(f1[i]); + } } void ForgetLoadedPatches() { - patchnumber = 0; + Patch.clear(); } static int _LoadPatchFiles(const wxDirName& folderName, wxString& fileSpec, const wxString& friendlyName, int& numberFoundPatchFiles) @@ -187,26 +186,26 @@ static int _LoadPatchFiles(const wxDirName& folderName, wxString& fileSpec, cons } wxDir dir(folderName.ToString()); - int before = patchnumber; + int before = Patch.size(); wxString buffer; wxTextFile f; bool found = dir.GetFirst(&buffer, L"*", wxDIR_FILES); while (found) { if (buffer.Upper().Matches(fileSpec.Upper())) { PatchesCon->WriteLn(Color_Green, L"Found %s file: '%s'", WX_STR(friendlyName), WX_STR(buffer)); - int before = patchnumber; + int before = Patch.size(); f.Open(Path::Combine(dir.GetName(), buffer)); inifile_process(f); f.Close(); - int loaded = patchnumber - before; + int loaded = Patch.size() - before; PatchesCon->WriteLn((loaded ? Color_Green : Color_Gray), L"Loaded %d %s from '%s' at '%s'", - loaded, WX_STR(friendlyName), WX_STR(buffer), WX_STR(folderName.ToString())); + loaded, WX_STR(friendlyName), WX_STR(buffer), WX_STR(folderName.ToString())); numberFoundPatchFiles++; } found = dir.GetNext(&buffer); } - return patchnumber - before; + return Patch.size() - before; } // This routine loads patches from a zip file @@ -216,7 +215,7 @@ static int _LoadPatchFiles(const wxDirName& folderName, wxString& fileSpec, cons int LoadPatchesFromZip(wxString gameCRC, const wxString& patchesArchiveFilename) { gameCRC.MakeUpper(); - int before = patchnumber; + int before = Patch.size(); std::unique_ptr entry; wxFFileInputStream in(patchesArchiveFilename); @@ -227,14 +226,14 @@ int LoadPatchesFromZip(wxString gameCRC, const wxString& patchesArchiveFilename) name.MakeUpper(); if (name.Find(gameCRC) == 0 && name.Find(L".PNACH")+6u == name.Length()) { PatchesCon->WriteLn(Color_Green, L"Loading patch '%s' from archive '%s'", - WX_STR(entry->GetName()), WX_STR(patchesArchiveFilename)); + WX_STR(entry->GetName()), WX_STR(patchesArchiveFilename)); wxTextInputStream pnach(zip); while (!zip.Eof()) { inifile_processString(pnach.ReadLine()); } } } - return patchnumber - before; + return Patch.size() - before; } @@ -263,28 +262,28 @@ int LoadPatchesFromDir(wxString name, const wxDirName& folderName, const wxStrin static u32 StrToU32(const wxString& str, int base = 10) { - unsigned long l; - str.ToULong(&l, base); - return l; + unsigned long l; + str.ToULong(&l, base); + return l; } static u64 StrToU64(const wxString& str, int base = 10) { - wxULongLong_t l; - str.ToULongLong(&l, base); - return l; + wxULongLong_t l; + str.ToULongLong(&l, base); + return l; } // PatchFunc Functions. namespace PatchFunc { - void comment( const wxString& text1, const wxString& text2 ) - { + void comment( const wxString& text1, const wxString& text2 ) + { PatchesCon->WriteLn(L"comment: " + text2); - } + } - struct PatchPieces - { + struct PatchPieces + { wxArrayString m_pieces; PatchPieces( const wxString& param ) @@ -299,7 +298,7 @@ namespace PatchFunc const wxString& MemAddr() const { return m_pieces[2]; } const wxString& OperandSize() const { return m_pieces[3]; } const wxString& WriteValue() const { return m_pieces[4]; } - }; + }; void patchHelper(const wxString& cmd, const wxString& param) { // Error Handling Note: I just throw simple wxStrings here, and then catch them below and @@ -313,17 +312,15 @@ namespace PatchFunc try { - if(patchnumber >= MAX_PATCH) - throw wxString( L"Maximum number of patches reached" ); - - IniPatch& iPatch = Patch[patchnumber]; PatchPieces pieces(param); + IniPatch iPatch = { 0 }; iPatch.enabled = 0; - iPatch.placetopatch = StrToU32(pieces.PlaceToPatch(), 10); + if (iPatch.placetopatch >= _PPT_END_MARKER) throw wxsFormat(L"Invalid 'place' value '%s' (0 - once on startup, 1: continuously)", WX_STR(pieces.PlaceToPatch())); + iPatch.cpu = (patch_cpu_type)PatchTableExecute(pieces.CpuType(), cpuCore); iPatch.addr = StrToU32(pieces.MemAddr(), 16); iPatch.type = (patch_data_type)PatchTableExecute(pieces.OperandSize(), dataType); @@ -336,8 +333,8 @@ namespace PatchFunc throw wxsFormat(L"Unrecognized Operand Size: '%s'", WX_STR(pieces.OperandSize())); iPatch.enabled = 1; // omg success!! + Patch.push_back(iPatch); - patchnumber++; } catch( wxString& exmsg ) { @@ -351,9 +348,9 @@ namespace PatchFunc // This is for applying patches directly to memory void ApplyLoadedPatches(patch_place_type place) { - for (int i = 0; i < patchnumber; i++) + for (auto& i : Patch) { - if (Patch[i].placetopatch == place) - _ApplyPatch(&Patch[i]); + if (i.placetopatch == place) + _ApplyPatch(&i); } } diff --git a/pcsx2/Patch.h b/pcsx2/Patch.h index d763aa25c6..e0d6e9b520 100644 --- a/pcsx2/Patch.h +++ b/pcsx2/Patch.h @@ -38,8 +38,6 @@ #include "Pcsx2Defs.h" #include "SysForwardDefs.h" -#define MAX_PATCH 2048 - enum patch_cpu_type { NO_CPU, CPU_EE, @@ -85,7 +83,6 @@ typedef void PATCHTABLEFUNC( const wxString& text1, const wxString& text2 ); struct IniPatch { int enabled; - int group; patch_data_type type; patch_cpu_type cpu; int placetopatch; diff --git a/pcsx2/windows/cheats/browser.cpp b/pcsx2/windows/cheats/browser.cpp index 3024ec0b13..6f9f085e9c 100644 --- a/pcsx2/windows/cheats/browser.cpp +++ b/pcsx2/windows/cheats/browser.cpp @@ -75,7 +75,7 @@ void RefreshListBox(HWND hWnd) char cpucore[2][10]={"EE", "IOP"}; // Adding items - for (int i = patchnumber-1; i >= 0; i--) + for (int i = Patch.size()-1; i >= 0; i--) { sprintf(Address, "0x%.8x", patch[i].addr); sprintf(CPU, "%s", cpucore[patch[i].cpu-1]); @@ -697,8 +697,7 @@ BOOL CALLBACK AddPatchProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) break; // Add new patch, refresh and exit - memcpy((void *)&patch[patchnumber], (void *)&temp, sizeof(IniPatch)); - patchnumber++; + Patch.push_back(temp); RefreshListBox(hParent); EndDialog(hWnd,1); break; @@ -952,10 +951,10 @@ BOOL CALLBACK pnachWriterProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) } // write patches - for(int i=0;i