Cheats: find the cheat file by CRC32.
This commit is contained in:
63
cheats.cpp
63
cheats.cpp
@@ -49,8 +49,49 @@ struct CheatComp
|
||||
|
||||
static char cheat_zip[1024] = {};
|
||||
|
||||
void cheats_init(char *rom_path)
|
||||
{
|
||||
int find_by_crc(uint32_t romcrc)
|
||||
{
|
||||
sprintf(cheat_zip, "%s/cheats/%s", getRootDir(), HomeDir);
|
||||
DIR *d = opendir(cheat_zip);
|
||||
if (!d)
|
||||
{
|
||||
printf("Couldn't open dir: %s\n", cheat_zip);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct dirent *de = 0;
|
||||
while((de = readdir(d)))
|
||||
{
|
||||
if (de->d_type == DT_REG)
|
||||
{
|
||||
char *ext = strcasestr(de->d_name, ".zip");
|
||||
if (ext && (ext - de->d_name) > 10)
|
||||
{
|
||||
ext -= 10;
|
||||
if (ext[0] == '[' && ext[9] == ']')
|
||||
{
|
||||
uint32_t crc = 0;
|
||||
if (sscanf(ext, "[%X].zip", &crc) == 1)
|
||||
{
|
||||
if (crc == romcrc)
|
||||
{
|
||||
strcat(cheat_zip, "/");
|
||||
strcat(cheat_zip, de->d_name);
|
||||
closedir(d);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(d);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cheats_init(char *rom_path, uint32_t romcrc)
|
||||
{
|
||||
cheats.clear();
|
||||
loaded = 0;
|
||||
cheat_zip[0] = 0;
|
||||
@@ -68,9 +109,9 @@ void cheats_init(char *rom_path)
|
||||
{
|
||||
memset(&_z, 0, sizeof(_z));
|
||||
|
||||
char *rom_name = strrchr(rom_path, '/');
|
||||
if (!rom_name) return;
|
||||
|
||||
char *rom_name = strrchr(rom_path, '/');
|
||||
if (!rom_name) return;
|
||||
|
||||
sprintf(cheat_zip, "%s/cheats/%s%s", getRootDir(), HomeDir, rom_name);
|
||||
char *p = strrchr(cheat_zip, '.');
|
||||
if (p) *p = 0;
|
||||
@@ -78,11 +119,17 @@ void cheats_init(char *rom_path)
|
||||
|
||||
if (!mz_zip_reader_init_file(&_z, cheat_zip, 0))
|
||||
{
|
||||
printf("no cheat file %s\n", cheat_zip);
|
||||
return;
|
||||
memset(&_z, 0, sizeof(_z));
|
||||
if (!find_by_crc(romcrc) || !mz_zip_reader_init_file(&_z, cheat_zip, 0))
|
||||
{
|
||||
printf("no cheat file found\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Using cheat file: %s\n", cheat_zip);
|
||||
|
||||
mz_zip_archive *z = new mz_zip_archive(_z);
|
||||
for (size_t i = 0; i < mz_zip_reader_get_num_files(z); i++)
|
||||
{
|
||||
@@ -325,6 +372,6 @@ void cheats_toggle()
|
||||
}
|
||||
|
||||
int cheats_loaded()
|
||||
{
|
||||
{
|
||||
return loaded;
|
||||
}
|
||||
Reference in New Issue
Block a user