Only open file once when detecting blob type

This commit is contained in:
JosJuice
2016-12-21 11:30:12 +01:00
parent 3fdcbbdd92
commit d1ea00ed88
9 changed files with 40 additions and 61 deletions

View File

@ -177,23 +177,31 @@ std::unique_ptr<IBlobReader> CreateBlobReader(const std::string& filename)
if (cdio_is_cdrom(filename))
return DriveReader::Create(filename);
if (!File::Exists(filename))
File::IOFile file(filename, "rb");
u32 magic;
if (!file.ReadArray(&magic, 1))
return nullptr;
if (IsWbfsBlob(filename))
return WbfsFileReader::Create(filename);
// Conveniently, every supported file format (except for plain disc images) starts
// with a 4-byte magic number that identifies the format, so we just need a simple
// switch statement to create the right blob type. If the magic number doesn't
// match any known magic number, we assume it's a plain disc image. If that
// assumption is wrong, the volume code that runs later will notice the error
// because the blob won't provide valid data when reading the GC/Wii disc header.
if (IsGCZBlob(filename))
return CompressedBlobReader::Create(filename);
if (IsCISOBlob(filename))
switch (magic)
{
case CISO_MAGIC:
return CISOFileReader::Create(filename);
if (IsTGCBlob(filename))
case GCZ_MAGIC:
return CompressedBlobReader::Create(filename);
case TGC_MAGIC:
return TGCFileReader::Create(filename);
// Still here? Assume plain file - since we know it exists due to the File::Exists check above.
return PlainFileReader::Create(filename);
case WBFS_MAGIC:
return WbfsFileReader::Create(filename);
default:
return PlainFileReader::Create(filename);
}
}
} // namespace