Skip to content

Commit

Permalink
CDImage: Tidy up Open() method
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jan 19, 2025
1 parent 7905793 commit 21b167d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 69 deletions.
68 changes: 32 additions & 36 deletions src/util/cd_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,83 +50,79 @@ void CDImage::DeinterleaveSubcode(const u8* subcode_in, u8* subcode_out)
}
}

std::unique_ptr<CDImage> CDImage::Open(const char* filename, bool allow_patches, Error* error)
std::unique_ptr<CDImage> CDImage::Open(const char* path, bool allow_patches, Error* error)
{
const char* extension;

// Annoying handling because of storage access framework.
#ifdef __ANDROID__
std::string filename_display_name(FileSystem::GetDisplayNameFromPath(filename));
if (filename_display_name.empty())
filename_display_name = filename;

extension = std::strrchr(filename_display_name.c_str(), '.');
const std::string path_display_name = FileSystem::GetDisplayNameFromPath(path);
const std::string_view extension = Path::GetExtension(path_display_name);
#else
extension = std::strrchr(filename, '.');
const std::string_view extension = Path::GetExtension(path);
#endif

std::unique_ptr<CDImage> image;
if (!extension)
if (extension.empty())
{
// Device filenames on Linux don't have extensions.
if (IsDeviceName(filename))
if (IsDeviceName(path))
{
image = OpenDeviceImage(filename, error);
image = OpenDeviceImage(path, error);
}
else
{
Error::SetStringFmt(error, "Invalid filename: '{}'", Path::GetFileName(filename));
Error::SetStringFmt(error, "Invalid filename: '{}'", Path::GetFileName(path));
return nullptr;
}
}
else if (StringUtil::Strcasecmp(extension, ".cue") == 0)
else if (StringUtil::EqualNoCase(extension, "cue"))
{
image = OpenCueSheetImage(filename, error);
image = OpenCueSheetImage(path, error);
}
else if (StringUtil::Strcasecmp(extension, ".bin") == 0 || StringUtil::Strcasecmp(extension, ".img") == 0 ||
StringUtil::Strcasecmp(extension, ".iso") == 0 || StringUtil::Strcasecmp(extension, ".ecm") == 0)
else if (StringUtil::EqualNoCase(extension, "bin") || StringUtil::EqualNoCase(extension, "img") ||
StringUtil::EqualNoCase(extension, "iso") || StringUtil::EqualNoCase(extension, "ecm"))
{
image = OpenBinImage(filename, error);
image = OpenBinImage(path, error);
}
else if (StringUtil::Strcasecmp(extension, ".chd") == 0)
else if (StringUtil::EqualNoCase(extension, "chd"))
{
image = OpenCHDImage(filename, error);
image = OpenCHDImage(path, error);
}
else if (StringUtil::Strcasecmp(extension, ".mds") == 0)
else if (StringUtil::EqualNoCase(extension, "mds"))
{
image = OpenMdsImage(filename, error);
image = OpenMdsImage(path, error);
}
else if (StringUtil::Strcasecmp(extension, ".pbp") == 0)
else if (StringUtil::EqualNoCase(extension, "pbp"))
{
image = OpenPBPImage(filename, error);
image = OpenPBPImage(path, error);
}
else if (StringUtil::Strcasecmp(extension, ".m3u") == 0)
else if (StringUtil::EqualNoCase(extension, "m3u"))
{
image = OpenM3uImage(filename, allow_patches, error);
// skip applying patches to the main path, which isn't a real disc
image = OpenM3uImage(path, allow_patches, error);
allow_patches = false;
}
else if (IsDeviceName(filename))
else if (IsDeviceName(path))
{
image = OpenDeviceImage(filename, error);
image = OpenDeviceImage(path, error);
}
else
{
Error::SetStringFmt(error, "Unknown extension '{}' from filename '{}'", extension, Path::GetFileName(filename));
Error::SetStringFmt(error, "Unknown extension '{}' from filename '{}'", extension, Path::GetFileName(path));
return nullptr;
}

if (allow_patches)
{
#ifdef __ANDROID__
const std::string ppf_filename(
Path::BuildRelativePath(filename, Path::ReplaceExtension(filename_display_name, "ppf")));
const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(path_display_name, "ppf"));
#else
const std::string ppf_filename(
Path::BuildRelativePath(filename, Path::ReplaceExtension(Path::GetFileName(filename), "ppf")));
const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(Path::GetFileName(path), "ppf"));
#endif
if (FileSystem::FileExists(ppf_filename.c_str()))
if (FileSystem::FileExists(ppf_path.c_str()))
{
image = CDImage::OverlayPPFPatch(ppf_filename.c_str(), std::move(image));
image = CDImage::OverlayPPFPatch(ppf_path.c_str(), std::move(image));
if (!image)
Error::SetStringFmt(error, "Failed to apply ppf patch from '{}'.", ppf_filename);
Error::SetStringFmt(error, "Failed to apply ppf patch from '{}'.", ppf_path);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/util/cd_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CDImage
MODE1_HEADER_SIZE = 4,
MODE2_HEADER_SIZE = 12,
MODE2_DATA_SECTOR_SIZE = 2336, // header + edc
FRAMES_PER_SECOND = 75, // "sectors", or "timecode frames" (not "channel frames")
FRAMES_PER_SECOND = 75, // "sectors", or "timecode frames" (not "channel frames")
SECONDS_PER_MINUTE = 60,
FRAMES_PER_MINUTE = FRAMES_PER_SECOND * SECONDS_PER_MINUTE,
SUBCHANNEL_BYTES_PER_FRAME = 12,
Expand Down Expand Up @@ -240,17 +240,17 @@ class CDImage
static bool IsDeviceName(const char* filename);

// Opening disc image.
static std::unique_ptr<CDImage> Open(const char* filename, bool allow_patches, Error* error);
static std::unique_ptr<CDImage> Open(const char* path, bool allow_patches, Error* error);
static std::unique_ptr<CDImage> OpenBinImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenCueSheetImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenCHDImage(const char* filename, Error* error);
static std::unique_ptr<CDImage> OpenMdsImage(const char* filename, Error* error);
static std::unique_ptr<CDImage> OpenPBPImage(const char* filename, Error* error);
static std::unique_ptr<CDImage> OpenM3uImage(const char* filename, bool apply_patches, Error* error);
static std::unique_ptr<CDImage> OpenDeviceImage(const char* filename, Error* error);
static std::unique_ptr<CDImage> OpenCHDImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenMdsImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenPBPImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenM3uImage(const char* path, bool apply_patches, Error* error);
static std::unique_ptr<CDImage> OpenDeviceImage(const char* path, Error* error);
static std::unique_ptr<CDImage>
CreateMemoryImage(CDImage* image, ProgressCallback* progress = ProgressCallback::NullProgressCallback);
static std::unique_ptr<CDImage> OverlayPPFPatch(const char* filename, std::unique_ptr<CDImage> parent_image,
static std::unique_ptr<CDImage> OverlayPPFPatch(const char* path, std::unique_ptr<CDImage> parent_image,
ProgressCallback* progress = ProgressCallback::NullProgressCallback);

// Accessors.
Expand Down
4 changes: 2 additions & 2 deletions src/util/cd_image_chd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,10 @@ s64 CDImageCHD::GetSizeOnDisk() const
return static_cast<s64>(chd_get_compressed_size(m_chd));
}

std::unique_ptr<CDImage> CDImage::OpenCHDImage(const char* filename, Error* error)
std::unique_ptr<CDImage> CDImage::OpenCHDImage(const char* path, Error* error)
{
std::unique_ptr<CDImageCHD> image = std::make_unique<CDImageCHD>();
if (!image->Open(filename, error))
if (!image->Open(path, error))
return {};

return image;
Expand Down
26 changes: 13 additions & 13 deletions src/util/cd_image_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,10 @@ bool CDImageDeviceWin32::DetermineReadMode(bool try_sptd)
return false;
}

std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* filename, Error* error)
std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* path, Error* error)
{
std::unique_ptr<CDImageDeviceWin32> image = std::make_unique<CDImageDeviceWin32>();
if (!image->Open(filename, error))
if (!image->Open(path, error))
return {};

return image;
Expand Down Expand Up @@ -679,9 +679,9 @@ std::vector<std::pair<std::string, std::string>> CDImage::GetDeviceList()
return ret;
}

bool CDImage::IsDeviceName(const char* filename)
bool CDImage::IsDeviceName(const char* path)
{
return std::string_view(filename).starts_with("\\\\.\\");
return std::string_view(path).starts_with("\\\\.\\");
}

#elif defined(__linux__) && !defined(__ANDROID__)
Expand Down Expand Up @@ -1083,10 +1083,10 @@ bool CDImageDeviceLinux::DetermineReadMode(Error* error)
return false;
}

std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* filename, Error* error)
std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* path, Error* error)
{
std::unique_ptr<CDImageDeviceLinux> image = std::make_unique<CDImageDeviceLinux>();
if (!image->Open(filename, error))
if (!image->Open(path, error))
return {};

return image;
Expand Down Expand Up @@ -1550,10 +1550,10 @@ bool CDImageDeviceMacOS::DetermineReadMode(Error* error)
return false;
}

std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* filename, Error* error)
std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* path, Error* error)
{
std::unique_ptr<CDImageDeviceMacOS> image = std::make_unique<CDImageDeviceMacOS>();
if (!image->Open(filename, error))
if (!image->Open(path, error))
return {};

return image;
Expand Down Expand Up @@ -1605,12 +1605,12 @@ std::vector<std::pair<std::string, std::string>> CDImage::GetDeviceList()
return ret;
}

bool CDImage::IsDeviceName(const char* filename)
bool CDImage::IsDeviceName(const char* path)
{
if (!std::string_view(filename).starts_with("/dev"))
if (!std::string_view(path).starts_with("/dev"))
return false;

io_service_t service = GetDeviceMediaService(filename);
io_service_t service = GetDeviceMediaService(path);
const bool valid = (service != 0);
if (valid)
IOObjectRelease(service);
Expand All @@ -1620,7 +1620,7 @@ bool CDImage::IsDeviceName(const char* filename)

#else

std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* filename, Error* error)
std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* path, Error* error)
{
return {};
}
Expand All @@ -1630,7 +1630,7 @@ std::vector<std::pair<std::string, std::string>> CDImage::GetDeviceList()
return {};
}

bool CDImage::IsDeviceName(const char* filename)
bool CDImage::IsDeviceName(const char* path)
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/cd_image_m3u.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ bool CDImageM3u::ReadSubChannelQ(SubChannelQ* subq, const Index& index, LBA lba_
return m_current_image->ReadSubChannelQ(subq, index, lba_in_index);
}

std::unique_ptr<CDImage> CDImage::OpenM3uImage(const char* filename, bool apply_patches, Error* error)
std::unique_ptr<CDImage> CDImage::OpenM3uImage(const char* path, bool apply_patches, Error* error)
{
std::unique_ptr<CDImageM3u> image = std::make_unique<CDImageM3u>();
if (!image->Open(filename, apply_patches, error))
if (!image->Open(path, apply_patches, error))
return {};

return image;
Expand Down
4 changes: 2 additions & 2 deletions src/util/cd_image_mds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ s64 CDImageMds::GetSizeOnDisk() const
return FileSystem::FSize64(m_mdf_file);
}

std::unique_ptr<CDImage> CDImage::OpenMdsImage(const char* filename, Error* error)
std::unique_ptr<CDImage> CDImage::OpenMdsImage(const char* path, Error* error)
{
std::unique_ptr<CDImageMds> image = std::make_unique<CDImageMds>();
if (!image->OpenAndParse(filename, error))
if (!image->OpenAndParse(path, error))
return {};

return image;
Expand Down
4 changes: 2 additions & 2 deletions src/util/cd_image_pbp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,10 @@ s64 CDImagePBP::GetSizeOnDisk() const
return FileSystem::FSize64(m_file);
}

std::unique_ptr<CDImage> CDImage::OpenPBPImage(const char* filename, Error* error)
std::unique_ptr<CDImage> CDImage::OpenPBPImage(const char* path, Error* error)
{
std::unique_ptr<CDImagePBP> image = std::make_unique<CDImagePBP>();
if (!image->Open(filename, error))
if (!image->Open(path, error))
return {};

return image;
Expand Down
7 changes: 3 additions & 4 deletions src/util/cd_image_ppf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,11 @@ s64 CDImagePPF::GetSizeOnDisk() const
return m_patch_size + m_parent_image->GetSizeOnDisk();
}

std::unique_ptr<CDImage>
CDImage::OverlayPPFPatch(const char* filename, std::unique_ptr<CDImage> parent_image,
ProgressCallback* progress /* = ProgressCallback::NullProgressCallback */)
std::unique_ptr<CDImage> CDImage::OverlayPPFPatch(const char* path, std::unique_ptr<CDImage> parent_image,
ProgressCallback* progress)
{
std::unique_ptr<CDImagePPF> ppf_image = std::make_unique<CDImagePPF>();
if (!ppf_image->Open(filename, std::move(parent_image)))
if (!ppf_image->Open(path, std::move(parent_image)))
return {};

return ppf_image;
Expand Down

0 comments on commit 21b167d

Please sign in to comment.