Try next archive type if file not found, not just on open error

This commit is contained in:
Vas Crabb 2016-03-25 16:09:14 +11:00
parent 54279dbf11
commit db10880ee0

View File

@ -626,41 +626,39 @@ bool emu_file::part_of_mediapath(std::string path)
osd_file::error emu_file::attempt_zipped() osd_file::error emu_file::attempt_zipped()
{ {
std::string filename; typedef util::archive_file::error (*open_func)(const std::string &filename, util::archive_file::ptr &result);
char const *const suffixes[] = { ".zip", ".7z" };
open_func const open_funcs[ARRAY_LENGTH(suffixes)] = { &util::archive_file::open_zip, &util::archive_file::open_7z };
// loop over archive types
std::string const savepath(m_fullpath);
std::string filename;
for (unsigned i = 0; i < ARRAY_LENGTH(suffixes); i++, m_fullpath = savepath, filename.clear())
{
// loop over directory parts up to the start of filename // loop over directory parts up to the start of filename
while (1) while (1)
{ {
// find the final path separator // find the final path separator
auto const dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]); auto const dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]);
if (dirsep == std::string::npos) if (dirsep == std::string::npos)
return osd_file::error::NOT_FOUND; break;
if (restrict_to_mediapath() && !part_of_mediapath(m_fullpath)) if (restrict_to_mediapath() && !part_of_mediapath(m_fullpath))
return osd_file::error::NOT_FOUND; break;
// insert the part from the right of the separator into the head of the filename // insert the part from the right of the separator into the head of the filename
if (filename.length() > 0) if (!filename.empty()) filename.insert(0, 1, '/');
filename.insert(0, "/"); filename.insert(0, m_fullpath.substr(dirsep + 1, std::string::npos));
filename.insert(0, m_fullpath.substr(dirsep + 1, -1));
// remove this part of the filename and append a .zip extension // remove this part of the filename and append an archive extension
m_fullpath = m_fullpath.substr(0, dirsep).append(".zip"); m_fullpath.resize(dirsep);
m_fullpath.append(suffixes[i]);
// attempt to open the ZIP file // attempt to open the archive file
util::archive_file::ptr zip; util::archive_file::ptr zip;
util::archive_file::error ziperr = util::archive_file::open_zip(m_fullpath, zip); util::archive_file::error ziperr = open_funcs[i](m_fullpath, zip);
if (ziperr != util::archive_file::error::NONE) // chop the archive suffix back off the filename before continuing
{
// remove this part of the filename and append a .7z extension
m_fullpath = m_fullpath.substr(0, dirsep).append(".7z");
// attempt to open the 7zip file
ziperr = util::archive_file::open_7z(m_fullpath, zip);
}
// chop the .zip/.7z back off the filename before continuing
m_fullpath = m_fullpath.substr(0, dirsep); m_fullpath = m_fullpath.substr(0, dirsep);
// if we failed to open this file, continue scanning // if we failed to open this file, continue scanning
@ -669,15 +667,15 @@ osd_file::error emu_file::attempt_zipped()
int header = -1; int header = -1;
// see if we can find a file with the right name and (if available) crc // see if we can find a file with the right name and (if available) CRC
if (m_openflags & OPEN_FLAG_HAS_CRC) header = zip->search(m_crc, filename, false); if (m_openflags & OPEN_FLAG_HAS_CRC) header = zip->search(m_crc, filename, false);
if (header < 0 && (m_openflags & OPEN_FLAG_HAS_CRC)) header = zip->search(m_crc, filename, true); if (header < 0 && (m_openflags & OPEN_FLAG_HAS_CRC)) header = zip->search(m_crc, filename, true);
// if that failed, look for a file with the right crc, but the wrong filename // if that failed, look for a file with the right CRC, but the wrong filename
if (header < 0 && (m_openflags & OPEN_FLAG_HAS_CRC)) header = zip->search(m_crc); if (header < 0 && (m_openflags & OPEN_FLAG_HAS_CRC)) header = zip->search(m_crc);
// if that failed, look for a file with the right name; reporting a bad checksum // if that failed, look for a file with the right name;
// is more helpful and less confusing than reporting "rom not found" // reporting a bad checksum is more helpful and less confusing than reporting "ROM not found"
if (header < 0) header = zip->search(filename, false); if (header < 0) header = zip->search(filename, false);
if (header < 0) header = zip->search(filename, true); if (header < 0) header = zip->search(filename, true);
@ -693,9 +691,11 @@ osd_file::error emu_file::attempt_zipped()
return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load_zipped_file(); return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load_zipped_file();
} }
// close up the ZIP file and try the next level // close up the archive file and try the next level
zip.reset(); zip.reset();
} }
}
return osd_file::error::NOT_FOUND;
} }