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.
This commit is contained in:
GovanifY 2019-01-23 02:01:30 +01:00 committed by lightningterror
parent 2aadf0be89
commit 628f5abaac
3 changed files with 46 additions and 53 deletions

View File

@ -22,6 +22,7 @@
#include "GameDatabase.h" #include "GameDatabase.h"
#include <memory> #include <memory>
#include <vector>
#include <wx/textfile.h> #include <wx/textfile.h>
#include <wx/dir.h> #include <wx/dir.h>
#include <wx/txtstrm.h> #include <wx/txtstrm.h>
@ -33,9 +34,7 @@
extern void _ApplyPatch(IniPatch *p); extern void _ApplyPatch(IniPatch *p);
IniPatch Patch[ MAX_PATCH ]; std::vector<IniPatch> Patch;
int patchnumber = 0;
wxString strgametitle; 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 // Is this really what we want to be doing here? Seems like just leaving it empty/blank
// would make more sense... --air // 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); /*int code = */PatchTableExecute(set, commands_patch);
} }
@ -126,7 +125,7 @@ void TrimPatches(wxString& s)
{ {
wxStringTokenizer tkn( s, L"\n" ); wxStringTokenizer tkn( s, L"\n" );
while(tkn.HasMoreTokens()) { while(tkn.HasMoreTokens()) {
inifile_command(tkn.GetNextToken()); inifile_command(tkn.GetNextToken());
} }
} }
@ -152,29 +151,29 @@ int LoadPatchesFromGamesDB(const wxString& crc, const Game_Data& game)
if (patchFound) TrimPatches(patch); if (patchFound) TrimPatches(patch);
return patchnumber; return Patch.size();
} }
void inifile_processString(const wxString& inStr) void inifile_processString(const wxString& inStr)
{ {
wxString str(inStr); wxString str(inStr);
inifile_trim(str); inifile_trim(str);
if (!str.IsEmpty()) inifile_command(str); if (!str.IsEmpty()) inifile_command(str);
} }
// This routine receives a file from inifile_read, trims it, // This routine receives a file from inifile_read, trims it,
// Then sends the command to be parsed. // Then sends the command to be parsed.
void inifile_process(wxTextFile &f1 ) void inifile_process(wxTextFile &f1 )
{ {
for (uint i = 0; i < f1.GetLineCount(); i++) for (uint i = 0; i < f1.GetLineCount(); i++)
{ {
inifile_processString(f1[i]); inifile_processString(f1[i]);
} }
} }
void ForgetLoadedPatches() void ForgetLoadedPatches()
{ {
patchnumber = 0; Patch.clear();
} }
static int _LoadPatchFiles(const wxDirName& folderName, wxString& fileSpec, const wxString& friendlyName, int& numberFoundPatchFiles) 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()); wxDir dir(folderName.ToString());
int before = patchnumber; int before = Patch.size();
wxString buffer; wxString buffer;
wxTextFile f; wxTextFile f;
bool found = dir.GetFirst(&buffer, L"*", wxDIR_FILES); bool found = dir.GetFirst(&buffer, L"*", wxDIR_FILES);
while (found) { while (found) {
if (buffer.Upper().Matches(fileSpec.Upper())) { if (buffer.Upper().Matches(fileSpec.Upper())) {
PatchesCon->WriteLn(Color_Green, L"Found %s file: '%s'", WX_STR(friendlyName), WX_STR(buffer)); 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)); f.Open(Path::Combine(dir.GetName(), buffer));
inifile_process(f); inifile_process(f);
f.Close(); 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'", 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++; numberFoundPatchFiles++;
} }
found = dir.GetNext(&buffer); found = dir.GetNext(&buffer);
} }
return patchnumber - before; return Patch.size() - before;
} }
// This routine loads patches from a zip file // 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) { int LoadPatchesFromZip(wxString gameCRC, const wxString& patchesArchiveFilename) {
gameCRC.MakeUpper(); gameCRC.MakeUpper();
int before = patchnumber; int before = Patch.size();
std::unique_ptr<wxZipEntry> entry; std::unique_ptr<wxZipEntry> entry;
wxFFileInputStream in(patchesArchiveFilename); wxFFileInputStream in(patchesArchiveFilename);
@ -227,14 +226,14 @@ int LoadPatchesFromZip(wxString gameCRC, const wxString& patchesArchiveFilename)
name.MakeUpper(); name.MakeUpper();
if (name.Find(gameCRC) == 0 && name.Find(L".PNACH")+6u == name.Length()) { if (name.Find(gameCRC) == 0 && name.Find(L".PNACH")+6u == name.Length()) {
PatchesCon->WriteLn(Color_Green, L"Loading patch '%s' from archive '%s'", 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); wxTextInputStream pnach(zip);
while (!zip.Eof()) { while (!zip.Eof()) {
inifile_processString(pnach.ReadLine()); 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) static u32 StrToU32(const wxString& str, int base = 10)
{ {
unsigned long l; unsigned long l;
str.ToULong(&l, base); str.ToULong(&l, base);
return l; return l;
} }
static u64 StrToU64(const wxString& str, int base = 10) static u64 StrToU64(const wxString& str, int base = 10)
{ {
wxULongLong_t l; wxULongLong_t l;
str.ToULongLong(&l, base); str.ToULongLong(&l, base);
return l; return l;
} }
// PatchFunc Functions. // PatchFunc Functions.
namespace PatchFunc namespace PatchFunc
{ {
void comment( const wxString& text1, const wxString& text2 ) void comment( const wxString& text1, const wxString& text2 )
{ {
PatchesCon->WriteLn(L"comment: " + text2); PatchesCon->WriteLn(L"comment: " + text2);
} }
struct PatchPieces struct PatchPieces
{ {
wxArrayString m_pieces; wxArrayString m_pieces;
PatchPieces( const wxString& param ) PatchPieces( const wxString& param )
@ -299,7 +298,7 @@ namespace PatchFunc
const wxString& MemAddr() const { return m_pieces[2]; } const wxString& MemAddr() const { return m_pieces[2]; }
const wxString& OperandSize() const { return m_pieces[3]; } const wxString& OperandSize() const { return m_pieces[3]; }
const wxString& WriteValue() const { return m_pieces[4]; } const wxString& WriteValue() const { return m_pieces[4]; }
}; };
void patchHelper(const wxString& cmd, const wxString& param) { void patchHelper(const wxString& cmd, const wxString& param) {
// Error Handling Note: I just throw simple wxStrings here, and then catch them below and // Error Handling Note: I just throw simple wxStrings here, and then catch them below and
@ -313,17 +312,15 @@ namespace PatchFunc
try try
{ {
if(patchnumber >= MAX_PATCH)
throw wxString( L"Maximum number of patches reached" );
IniPatch& iPatch = Patch[patchnumber];
PatchPieces pieces(param); PatchPieces pieces(param);
IniPatch iPatch = { 0 };
iPatch.enabled = 0; iPatch.enabled = 0;
iPatch.placetopatch = StrToU32(pieces.PlaceToPatch(), 10); iPatch.placetopatch = StrToU32(pieces.PlaceToPatch(), 10);
if (iPatch.placetopatch >= _PPT_END_MARKER) if (iPatch.placetopatch >= _PPT_END_MARKER)
throw wxsFormat(L"Invalid 'place' value '%s' (0 - once on startup, 1: continuously)", WX_STR(pieces.PlaceToPatch())); 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.cpu = (patch_cpu_type)PatchTableExecute(pieces.CpuType(), cpuCore);
iPatch.addr = StrToU32(pieces.MemAddr(), 16); iPatch.addr = StrToU32(pieces.MemAddr(), 16);
iPatch.type = (patch_data_type)PatchTableExecute(pieces.OperandSize(), dataType); 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())); throw wxsFormat(L"Unrecognized Operand Size: '%s'", WX_STR(pieces.OperandSize()));
iPatch.enabled = 1; // omg success!! iPatch.enabled = 1; // omg success!!
Patch.push_back(iPatch);
patchnumber++;
} }
catch( wxString& exmsg ) catch( wxString& exmsg )
{ {
@ -351,9 +348,9 @@ namespace PatchFunc
// This is for applying patches directly to memory // This is for applying patches directly to memory
void ApplyLoadedPatches(patch_place_type place) void ApplyLoadedPatches(patch_place_type place)
{ {
for (int i = 0; i < patchnumber; i++) for (auto& i : Patch)
{ {
if (Patch[i].placetopatch == place) if (i.placetopatch == place)
_ApplyPatch(&Patch[i]); _ApplyPatch(&i);
} }
} }

View File

@ -38,8 +38,6 @@
#include "Pcsx2Defs.h" #include "Pcsx2Defs.h"
#include "SysForwardDefs.h" #include "SysForwardDefs.h"
#define MAX_PATCH 2048
enum patch_cpu_type { enum patch_cpu_type {
NO_CPU, NO_CPU,
CPU_EE, CPU_EE,
@ -85,7 +83,6 @@ typedef void PATCHTABLEFUNC( const wxString& text1, const wxString& text2 );
struct IniPatch struct IniPatch
{ {
int enabled; int enabled;
int group;
patch_data_type type; patch_data_type type;
patch_cpu_type cpu; patch_cpu_type cpu;
int placetopatch; int placetopatch;

View File

@ -75,7 +75,7 @@ void RefreshListBox(HWND hWnd)
char cpucore[2][10]={"EE", "IOP"}; char cpucore[2][10]={"EE", "IOP"};
// Adding items // 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(Address, "0x%.8x", patch[i].addr);
sprintf(CPU, "%s", cpucore[patch[i].cpu-1]); sprintf(CPU, "%s", cpucore[patch[i].cpu-1]);
@ -697,8 +697,7 @@ BOOL CALLBACK AddPatchProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
break; break;
// Add new patch, refresh and exit // Add new patch, refresh and exit
memcpy((void *)&patch[patchnumber], (void *)&temp, sizeof(IniPatch)); Patch.push_back(temp);
patchnumber++;
RefreshListBox(hParent); RefreshListBox(hParent);
EndDialog(hWnd,1); EndDialog(hWnd,1);
break; break;
@ -952,10 +951,10 @@ BOOL CALLBACK pnachWriterProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
} }
// write patches // write patches
for(int i=0;i<patchnumber;i++) for (const auto& i : Patch)
{ {
char cpucore[10], type[10]; char cpucore[10], type[10];
switch(patch[i].cpu) switch(i.cpu)
{ {
case 1: case 1:
strcpy(cpucore, "EE"); strcpy(cpucore, "EE");
@ -965,7 +964,7 @@ BOOL CALLBACK pnachWriterProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
break; break;
} }
switch(patch[i].type) switch(i.type)
{ {
case 1: case 1:
strcpy(type, "byte"); strcpy(type, "byte");
@ -981,7 +980,7 @@ BOOL CALLBACK pnachWriterProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
break; break;
} }
//patch=placetopatch,cpucore,address,type,data //patch=placetopatch,cpucore,address,type,data
fprintf(fp, "patch=%d,%s,%.8x,%s,%.8x\n", patch[i].placetopatch, cpucore, patch[i].addr, type, patch[i].data); fprintf(fp, "patch=%d,%s,%.8x,%s,%.8x\n", i.placetopatch, cpucore, i.addr, type, i.data);
} }
fclose(fp); fclose(fp);