Condense CheckForAnyPatch with lambdas.

This commit is contained in:
BearOso 2023-02-07 15:58:36 -06:00
parent db10ed33e1
commit 3bbed09867
1 changed files with 115 additions and 330 deletions

View File

@ -3796,11 +3796,9 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r
return; return;
FSTREAM patch_file = NULL; FSTREAM patch_file = NULL;
uint32 i;
long offset = header ? 512 : 0; long offset = header ? 512 : 0;
int ret; int ret;
bool flag; bool flag = false;
char ips[8];
int rom_filename_length = strlen(rom_filename); int rom_filename_length = strlen(rom_filename);
const char *ext = NULL; const char *ext = NULL;
@ -3815,154 +3813,49 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r
unzFile file = unzOpen(rom_filename); unzFile file = unzOpen(rom_filename);
if (file) if (file)
{ {
int port = unzFindExtension(file, "bps"); auto try_zip_patch = [&](const char *ext, bool8 (*read_patch_func)(Stream * r, long offset, int32 &rom_size)) -> bool {
if (port == UNZ_OK) if (unzFindExtension(file, ext) == UNZ_OK)
{ {
printf(" in %s", rom_filename); printf(" in %s", rom_filename);
Stream *s = new unzStream(file); Stream *s = new unzStream(file);
ret = ReadBPSPatch(s, offset, rom_size); ret = read_patch_func(s, offset, rom_size);
delete s;
if (ret)
printf("!\n");
else
printf(" failed!\n");
}
port = unzFindExtension(file, "ups");
if (port == UNZ_OK)
{
printf(" in %s", rom_filename);
Stream *s = new unzStream(file);
ret = ReadUPSPatch(s, offset, rom_size);
delete s;
if (ret)
printf("!\n");
else
printf(" failed!\n");
}
port = unzFindExtension(file, "ips");
while (port == UNZ_OK)
{
printf(" in %s", rom_filename);
Stream *s = new unzStream(file);
ret = ReadIPSPatch(s, offset, rom_size);
delete s; delete s;
if (ret) if (ret)
{ {
printf("!\n"); printf("!\n");
flag = true; flag = true;
return true;
} }
else
printf(" failed!\n");
port = unzFindExtension(file, "ips", false); printf(" failed!\n");
} }
return false;
};
auto try_zip_ips_sequence = [&](const char *pattern) {
for (int i = 0; i < 1000; i++)
{
char ips[8];
snprintf(ips, 8, pattern, i);
if (!try_zip_patch(ips, ReadIPSPatch))
break;
}
};
if (!flag) if (!flag)
{ try_zip_patch("bps", ReadBPSPatch);
i = 0;
do
{
snprintf(ips, 8, "%03d.ips", i);
if (unzFindExtension(file, ips) != UNZ_OK)
break;
printf(" in %s", rom_filename);
Stream *s = new unzStream(file);
ret = ReadIPSPatch(s, offset, rom_size);
delete s;
if (ret)
{
printf("!\n");
flag = true;
}
else
{
printf(" failed!\n");
break;
}
if (unzFindExtension(file, ips, false, false) == UNZ_OK)
printf("WARNING: Ignoring extra .%s files!\n", ips);
} while (++i < 1000);
}
if (!flag) if (!flag)
{ try_zip_patch("ups", ReadUPSPatch);
i = 0;
do
{
snprintf(ips, 8, "ips%d", i);
if (unzFindExtension(file, ips) != UNZ_OK)
break;
printf(" in %s", rom_filename);
Stream *s = new unzStream(file);
ret = ReadIPSPatch(s, offset, rom_size);
delete s;
if (ret)
{
printf("!\n");
flag = true;
}
else
{
printf(" failed!\n");
break;
}
if (unzFindExtension(file, ips, false, false) == UNZ_OK)
printf("WARNING: Ignoring extra .%s files!\n", ips);
} while (++i < 1000);
}
if (!flag) if (!flag)
{ try_zip_patch("ips", ReadIPSPatch);
i = 0; if (!flag)
try_zip_ips_sequence("%03d.ips");
do if (!flag)
{ try_zip_ips_sequence("ips%d");
snprintf(ips, 4, "ip%d", i); if (!flag)
try_zip_ips_sequence("ip%d");
if (unzFindExtension(file, ips) != UNZ_OK)
break;
printf(" in %s", rom_filename);
Stream *s = new unzStream(file);
ret = ReadIPSPatch(s, offset, rom_size);
delete s;
if (ret)
{
printf("!\n");
flag = true;
}
else
{
printf(" failed!\n");
break;
}
if (unzFindExtension(file, ips, false, false) == UNZ_OK)
printf("WARNING: Ignoring extra .%s files!\n", ips);
} while (++i < 10);
}
assert(unzClose(file) == UNZ_OK); assert(unzClose(file) == UNZ_OK);
@ -3989,155 +3882,47 @@ void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &r
} }
#endif #endif
// BPS auto try_patch = [&](const char *type, std::string filename, bool8 (*read_patch_func)(Stream * r, long offset, int32 &rom_size)) -> bool {
std::string filename = S9xGetFilename(".bps", PATCH_DIR);
if ((patch_file = OPEN_FSTREAM(filename.c_str(), "rb")) != NULL) if ((patch_file = OPEN_FSTREAM(filename.c_str(), "rb")) != NULL)
{ {
printf("Using BPS patch %s", filename.c_str()); printf("Using %s patch %s", type, filename.c_str());
Stream *s = new fStream(patch_file); Stream *s = new fStream(patch_file);
ret = ReadBPSPatch(s, 0, rom_size); ret = read_patch_func(s, 0, rom_size);
s->closeStream(); s->closeStream();
if (ret) if (ret)
{ {
printf("!\n"); printf("!\n");
return true;
}
else
printf(" failed!\n");
}
return false;
};
auto try_ips_sequence = [&](const char *pattern) -> bool {
for (int i = 0; i < 1000; i++)
{
char ips[8];
snprintf(ips, 8, pattern, i);
if (!try_patch("IPS", ips, ReadIPSPatch))
break;
}
return flag;
};
if (try_patch("BPS", S9xGetFilename(".bps", PATCH_DIR), ReadBPSPatch))
return;
if (try_patch("UPS", S9xGetFilename(".ups", PATCH_DIR), ReadUPSPatch))
return;
if (try_patch("IPS", S9xGetFilename(".ips", PATCH_DIR), ReadIPSPatch))
return;
if (try_ips_sequence("%03d.ips"))
return;
if (try_ips_sequence("ips%d"))
return;
if (try_ips_sequence("ip%d"))
return; return;
} }
else
printf(" failed!\n");
}
// UPS
filename = S9xGetFilename(".ups", PATCH_DIR);
if ((patch_file = OPEN_FSTREAM(filename.c_str(), "rb")) != NULL)
{
printf("Using UPS patch %s", filename.c_str());;
Stream *s = new fStream(patch_file);
ret = ReadUPSPatch(s, 0, rom_size);
s->closeStream();
if (ret)
{
printf("!\n");
return;
}
else
printf(" failed!\n");
}
// IPS
filename = S9xGetFilename(".ips", PATCH_DIR);
if ((patch_file = OPEN_FSTREAM(filename.c_str(), "rb")) != NULL)
{
printf("Using IPS patch %s", filename.c_str());
Stream *s = new fStream(patch_file);
ret = ReadIPSPatch(s, offset, rom_size);
s->closeStream();
if (ret)
{
printf("!\n");
return;
}
else
printf(" failed!\n");
}
i = 0;
flag = false;
do
{
snprintf(ips, 8, "%03d.ips", i);
filename = S9xGetFilename(ips, PATCH_DIR);
if (!(patch_file = OPEN_FSTREAM(filename.c_str(), "rb")))
break;
printf("Using IPS patch %s", filename.c_str());
Stream *s = new fStream(patch_file);
ret = ReadIPSPatch(s, offset, rom_size);
s->closeStream();
if (ret)
{
printf("!\n");
flag = true;
}
else
{
printf(" failed!\n");
break;
}
} while (i < 1000);
if (flag)
return;
i = 0;
flag = false;
do
{
snprintf(ips, 8, "ips%d", i);
filename = S9xGetFilename(ips, PATCH_DIR);
if (!(patch_file = OPEN_FSTREAM(filename.c_str(), "rb")))
break;
printf("Using IPS patch %s", filename.c_str());
Stream *s = new fStream(patch_file);
ret = ReadIPSPatch(s, offset, rom_size);
s->closeStream();
if (ret)
{
printf("!\n");
flag = true;
}
else
{
printf(" failed!\n");
break;
}
} while (++i < 1000);
if (flag)
return;
i = 0;
flag = false;
do
{
snprintf(ips, 4, "ip%d", i);
filename = S9xGetFilename(ips, PATCH_DIR);
if (!(patch_file = OPEN_FSTREAM(filename.c_str(), "rb")))
break;
printf("Using IPS patch %s", filename.c_str());;
Stream *s = new fStream(patch_file);
ret = ReadIPSPatch(s, offset, rom_size);
s->closeStream();
if (ret)
{
printf("!\n");
flag = true;
}
else
{
printf(" failed!\n");
break;
}
} while (++i < 10);
}