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

@ -3788,356 +3788,141 @@ static int unzFindExtension (unzFile &file, const char *ext, bool restart, bool
} }
#endif #endif
void CMemory::CheckForAnyPatch (const char *rom_filename, bool8 header, int32 &rom_size) void CMemory::CheckForAnyPatch(const char *rom_filename, bool8 header, int32 &rom_size)
{ {
Settings.IsPatched = false; Settings.IsPatched = false;
if (Settings.NoPatch) if (Settings.NoPatch)
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 = false;
bool flag;
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;
if (rom_filename_length < 4) if (rom_filename_length < 4)
return; return;
if (rom_filename[rom_filename_length - 4] == '.') if (rom_filename[rom_filename_length - 4] == '.')
ext = &rom_filename[rom_filename_length - 3]; ext = &rom_filename[rom_filename_length - 3];
#ifdef UNZIP_SUPPORT #ifdef UNZIP_SUPPORT
if (!strcasecmp(ext, "zip") || !strcasecmp(ext, ".zip")) if (!strcasecmp(ext, "zip") || !strcasecmp(ext, ".zip"))
{ {
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);
ret = ReadBPSPatch(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;
if (ret)
{
printf("!\n");
flag = true;
}
else
printf(" failed!\n");
port = unzFindExtension(file, "ips", false);
}
if (!flag)
{
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); Stream *s = new unzStream(file);
ret = ReadIPSPatch(s, offset, rom_size); ret = read_patch_func(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");
break;
}
if (unzFindExtension(file, ips, false, false) == UNZ_OK) printf(" failed!\n");
printf("WARNING: Ignoring extra .%s files!\n", ips); }
} while (++i < 1000); return false;
} };
if (!flag) auto try_zip_ips_sequence = [&](const char *pattern) {
{ for (int i = 0; i < 1000; i++)
i = 0; {
char ips[8];
snprintf(ips, 8, pattern, i);
if (!try_zip_patch(ips, ReadIPSPatch))
break;
}
};
do if (!flag)
{ try_zip_patch("bps", ReadBPSPatch);
snprintf(ips, 8, "ips%d", i); if (!flag)
try_zip_patch("ups", ReadUPSPatch);
if (!flag)
try_zip_patch("ips", ReadIPSPatch);
if (!flag)
try_zip_ips_sequence("%03d.ips");
if (!flag)
try_zip_ips_sequence("ips%d");
if (!flag)
try_zip_ips_sequence("ip%d");
if (unzFindExtension(file, ips) != UNZ_OK) assert(unzClose(file) == UNZ_OK);
break;
printf(" in %s", rom_filename); if (flag)
return;
}
}
Stream *s = new unzStream(file); // Mercurial Magic (MSU-1 distribution pack)
ret = ReadIPSPatch(s, offset, rom_size); if (strcasecmp(ext, "msu1") && strcasecmp(ext, ".msu1")) // ROM was *NOT* loaded from a .msu1 pack
delete s; {
Stream *s = S9xMSU1OpenFile("patch.bps", TRUE);
if (s)
{
printf("Using BPS patch from msu1");
ret = ReadBPSPatch(s, offset, rom_size);
s->closeStream();
if (ret) if (ret)
{ printf("!\n");
printf("!\n"); else
flag = true; printf(" failed!\n");
} }
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)
{
i = 0;
do
{
snprintf(ips, 4, "ip%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 < 10);
}
assert(unzClose(file) == UNZ_OK);
if (flag)
return;
}
}
// Mercurial Magic (MSU-1 distribution pack)
if (strcasecmp(ext, "msu1") && strcasecmp(ext, ".msu1")) // ROM was *NOT* loaded from a .msu1 pack
{
Stream *s = S9xMSU1OpenFile("patch.bps", TRUE);
if (s)
{
printf("Using BPS patch from msu1");
ret = ReadBPSPatch(s, offset, rom_size);
s->closeStream();
if (ret)
printf("!\n");
else
printf(" failed!\n");
}
}
#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)
{
printf("Using %s patch %s", type, filename.c_str());
if ((patch_file = OPEN_FSTREAM(filename.c_str(), "rb")) != NULL) Stream *s = new fStream(patch_file);
{ ret = read_patch_func(s, 0, rom_size);
printf("Using BPS patch %s", filename.c_str()); s->closeStream();
Stream *s = new fStream(patch_file); if (ret)
ret = ReadBPSPatch(s, 0, rom_size); {
s->closeStream(); printf("!\n");
return true;
}
else
printf(" failed!\n");
}
return false;
};
if (ret) auto try_ips_sequence = [&](const char *pattern) -> bool {
{ for (int i = 0; i < 1000; i++)
printf("!\n"); {
return; char ips[8];
} snprintf(ips, 8, pattern, i);
else if (!try_patch("IPS", ips, ReadIPSPatch))
printf(" failed!\n"); break;
} }
return flag;
};
// UPS if (try_patch("BPS", S9xGetFilename(".bps", PATCH_DIR), ReadBPSPatch))
filename = S9xGetFilename(".ups", PATCH_DIR); return;
if (try_patch("UPS", S9xGetFilename(".ups", PATCH_DIR), ReadUPSPatch))
if ((patch_file = OPEN_FSTREAM(filename.c_str(), "rb")) != NULL) return;
{ if (try_patch("IPS", S9xGetFilename(".ips", PATCH_DIR), ReadIPSPatch))
printf("Using UPS patch %s", filename.c_str());; return;
if (try_ips_sequence("%03d.ips"))
Stream *s = new fStream(patch_file); return;
ret = ReadUPSPatch(s, 0, rom_size); if (try_ips_sequence("ips%d"))
s->closeStream(); return;
if (try_ips_sequence("ip%d"))
if (ret) return;
{
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);
} }