CDImage: Tidy up Open() method

This commit is contained in:
Stenzek 2025-01-19 20:52:18 +10:00
parent 7905793ae0
commit 21b167d382
No known key found for this signature in database
8 changed files with 64 additions and 69 deletions

View File

@ -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__ #ifdef __ANDROID__
std::string filename_display_name(FileSystem::GetDisplayNameFromPath(filename)); const std::string path_display_name = FileSystem::GetDisplayNameFromPath(path);
if (filename_display_name.empty()) const std::string_view extension = Path::GetExtension(path_display_name);
filename_display_name = filename;
extension = std::strrchr(filename_display_name.c_str(), '.');
#else #else
extension = std::strrchr(filename, '.'); const std::string_view extension = Path::GetExtension(path);
#endif #endif
std::unique_ptr<CDImage> image; std::unique_ptr<CDImage> image;
if (!extension) if (extension.empty())
{ {
// Device filenames on Linux don't have extensions. // Device filenames on Linux don't have extensions.
if (IsDeviceName(filename)) if (IsDeviceName(path))
{ {
image = OpenDeviceImage(filename, error); image = OpenDeviceImage(path, error);
} }
else else
{ {
Error::SetStringFmt(error, "Invalid filename: '{}'", Path::GetFileName(filename)); Error::SetStringFmt(error, "Invalid filename: '{}'", Path::GetFileName(path));
return nullptr; 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 || else if (StringUtil::EqualNoCase(extension, "bin") || StringUtil::EqualNoCase(extension, "img") ||
StringUtil::Strcasecmp(extension, ".iso") == 0 || StringUtil::Strcasecmp(extension, ".ecm") == 0) 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 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; return nullptr;
} }
if (allow_patches) if (allow_patches)
{ {
#ifdef __ANDROID__ #ifdef __ANDROID__
const std::string ppf_filename( const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(path_display_name, "ppf"));
Path::BuildRelativePath(filename, Path::ReplaceExtension(filename_display_name, "ppf")));
#else #else
const std::string ppf_filename( const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(Path::GetFileName(path), "ppf"));
Path::BuildRelativePath(filename, Path::ReplaceExtension(Path::GetFileName(filename), "ppf")));
#endif #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) if (!image)
Error::SetStringFmt(error, "Failed to apply ppf patch from '{}'.", ppf_filename); Error::SetStringFmt(error, "Failed to apply ppf patch from '{}'.", ppf_path);
} }
} }

View File

@ -240,17 +240,17 @@ public:
static bool IsDeviceName(const char* filename); static bool IsDeviceName(const char* filename);
// Opening disc image. // 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> OpenBinImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenCueSheetImage(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> OpenCHDImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenMdsImage(const char* filename, Error* error); static std::unique_ptr<CDImage> OpenMdsImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenPBPImage(const char* filename, Error* error); static std::unique_ptr<CDImage> OpenPBPImage(const char* path, Error* error);
static std::unique_ptr<CDImage> OpenM3uImage(const char* filename, bool apply_patches, Error* error); static std::unique_ptr<CDImage> OpenM3uImage(const char* path, bool apply_patches, Error* error);
static std::unique_ptr<CDImage> OpenDeviceImage(const char* filename, Error* error); static std::unique_ptr<CDImage> OpenDeviceImage(const char* path, Error* error);
static std::unique_ptr<CDImage> static std::unique_ptr<CDImage>
CreateMemoryImage(CDImage* image, ProgressCallback* progress = ProgressCallback::NullProgressCallback); 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); ProgressCallback* progress = ProgressCallback::NullProgressCallback);
// Accessors. // Accessors.

View File

@ -533,10 +533,10 @@ s64 CDImageCHD::GetSizeOnDisk() const
return static_cast<s64>(chd_get_compressed_size(m_chd)); 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>(); std::unique_ptr<CDImageCHD> image = std::make_unique<CDImageCHD>();
if (!image->Open(filename, error)) if (!image->Open(path, error))
return {}; return {};
return image; return image;

View File

@ -634,10 +634,10 @@ bool CDImageDeviceWin32::DetermineReadMode(bool try_sptd)
return false; 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>(); std::unique_ptr<CDImageDeviceWin32> image = std::make_unique<CDImageDeviceWin32>();
if (!image->Open(filename, error)) if (!image->Open(path, error))
return {}; return {};
return image; return image;
@ -679,9 +679,9 @@ std::vector<std::pair<std::string, std::string>> CDImage::GetDeviceList()
return ret; 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__) #elif defined(__linux__) && !defined(__ANDROID__)
@ -1083,10 +1083,10 @@ bool CDImageDeviceLinux::DetermineReadMode(Error* error)
return false; 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>(); std::unique_ptr<CDImageDeviceLinux> image = std::make_unique<CDImageDeviceLinux>();
if (!image->Open(filename, error)) if (!image->Open(path, error))
return {}; return {};
return image; return image;
@ -1550,10 +1550,10 @@ bool CDImageDeviceMacOS::DetermineReadMode(Error* error)
return false; 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>(); std::unique_ptr<CDImageDeviceMacOS> image = std::make_unique<CDImageDeviceMacOS>();
if (!image->Open(filename, error)) if (!image->Open(path, error))
return {}; return {};
return image; return image;
@ -1605,12 +1605,12 @@ std::vector<std::pair<std::string, std::string>> CDImage::GetDeviceList()
return ret; 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; return false;
io_service_t service = GetDeviceMediaService(filename); io_service_t service = GetDeviceMediaService(path);
const bool valid = (service != 0); const bool valid = (service != 0);
if (valid) if (valid)
IOObjectRelease(service); IOObjectRelease(service);
@ -1620,7 +1620,7 @@ bool CDImage::IsDeviceName(const char* filename)
#else #else
std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* filename, Error* error) std::unique_ptr<CDImage> CDImage::OpenDeviceImage(const char* path, Error* error)
{ {
return {}; return {};
} }
@ -1630,7 +1630,7 @@ std::vector<std::pair<std::string, std::string>> CDImage::GetDeviceList()
return {}; return {};
} }
bool CDImage::IsDeviceName(const char* filename) bool CDImage::IsDeviceName(const char* path)
{ {
return false; return false;
} }

View File

@ -181,10 +181,10 @@ bool CDImageM3u::ReadSubChannelQ(SubChannelQ* subq, const Index& index, LBA lba_
return m_current_image->ReadSubChannelQ(subq, index, lba_in_index); 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>(); 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 {};
return image; return image;

View File

@ -270,10 +270,10 @@ s64 CDImageMds::GetSizeOnDisk() const
return FileSystem::FSize64(m_mdf_file); 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>(); std::unique_ptr<CDImageMds> image = std::make_unique<CDImageMds>();
if (!image->OpenAndParse(filename, error)) if (!image->OpenAndParse(path, error))
return {}; return {};
return image; return image;

View File

@ -935,10 +935,10 @@ s64 CDImagePBP::GetSizeOnDisk() const
return FileSystem::FSize64(m_file); 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>(); std::unique_ptr<CDImagePBP> image = std::make_unique<CDImagePBP>();
if (!image->Open(filename, error)) if (!image->Open(path, error))
return {}; return {};
return image; return image;

View File

@ -458,12 +458,11 @@ s64 CDImagePPF::GetSizeOnDisk() const
return m_patch_size + m_parent_image->GetSizeOnDisk(); return m_patch_size + m_parent_image->GetSizeOnDisk();
} }
std::unique_ptr<CDImage> std::unique_ptr<CDImage> CDImage::OverlayPPFPatch(const char* path, std::unique_ptr<CDImage> parent_image,
CDImage::OverlayPPFPatch(const char* filename, std::unique_ptr<CDImage> parent_image, ProgressCallback* progress)
ProgressCallback* progress /* = ProgressCallback::NullProgressCallback */)
{ {
std::unique_ptr<CDImagePPF> ppf_image = std::make_unique<CDImagePPF>(); 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 {};
return ppf_image; return ppf_image;