System: Move overlay checking to common

This commit is contained in:
Connor McLaughlin
2022-08-27 16:52:24 +10:00
parent ea8d779962
commit 3a5bf6d29b
10 changed files with 119 additions and 80 deletions

View File

@ -1,5 +1,6 @@
#include "cd_image.h"
#include "common/assert.h"
#include "common/error.h"
#include "common/file_system.h"
#include "common/log.h"
#include "common/path.h"
@ -17,7 +18,7 @@ u32 CDImage::GetBytesPerSector(TrackMode mode)
return sizes[static_cast<u32>(mode)];
}
std::unique_ptr<CDImage> CDImage::Open(const char* filename, Common::Error* error)
std::unique_ptr<CDImage> CDImage::Open(const char* filename, bool allow_patches, Common::Error* error)
{
const char* extension;
@ -37,43 +38,67 @@ std::unique_ptr<CDImage> CDImage::Open(const char* filename, Common::Error* erro
return nullptr;
}
std::unique_ptr<CDImage> image;
if (StringUtil::Strcasecmp(extension, ".cue") == 0)
{
return OpenCueSheetImage(filename, error);
image = OpenCueSheetImage(filename, error);
}
else if (StringUtil::Strcasecmp(extension, ".bin") == 0 || StringUtil::Strcasecmp(extension, ".img") == 0 ||
StringUtil::Strcasecmp(extension, ".iso") == 0)
{
return OpenBinImage(filename, error);
image = OpenBinImage(filename, error);
}
else if (StringUtil::Strcasecmp(extension, ".chd") == 0)
{
return OpenCHDImage(filename, error);
image = OpenCHDImage(filename, error);
}
else if (StringUtil::Strcasecmp(extension, ".ecm") == 0)
{
return OpenEcmImage(filename, error);
image = OpenEcmImage(filename, error);
}
else if (StringUtil::Strcasecmp(extension, ".mds") == 0)
{
return OpenMdsImage(filename, error);
image = OpenMdsImage(filename, error);
}
else if (StringUtil::Strcasecmp(extension, ".pbp") == 0)
{
return OpenPBPImage(filename, error);
image = OpenPBPImage(filename, error);
}
else if (StringUtil::Strcasecmp(extension, ".m3u") == 0)
{
return OpenM3uImage(filename, error);
image = OpenM3uImage(filename, allow_patches, error);
}
else if (IsDeviceName(filename))
{
image = OpenDeviceImage(filename, error);
}
else
{
Log_ErrorPrintf("Unknown extension '%s' from filename '%s'", extension, filename);
return nullptr;
}
if (IsDeviceName(filename))
return OpenDeviceImage(filename, error);
if (allow_patches)
{
#ifdef __ANDROID__
const std::string ppf_filename(
Path::BuildRelativePath(filename, Path::ReplaceExtension(filename_display_name, "ppf")));
#else
const std::string ppf_filename(
Path::BuildRelativePath(filename, Path::ReplaceExtension(Path::GetFileName(filename), "ppf")));
#endif
if (FileSystem::FileExists(ppf_filename.c_str()))
{
image = CDImage::OverlayPPFPatch(ppf_filename.c_str(), std::move(image));
if (!image)
{
if (error)
error->SetFormattedMessage("Failed to apply ppf patch from '%s'.", ppf_filename.c_str());
}
}
}
#undef CASE_COMPARE
Log_ErrorPrintf("Unknown extension '%s' from filename '%s'", extension, filename);
return nullptr;
return image;
}
CDImage::LBA CDImage::GetTrackStartPosition(u8 track) const

View File

@ -217,14 +217,14 @@ public:
static bool IsDeviceName(const char* filename);
// Opening disc image.
static std::unique_ptr<CDImage> Open(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> Open(const char* filename, bool allow_patches, Common::Error* error);
static std::unique_ptr<CDImage> OpenBinImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> OpenCueSheetImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> OpenCHDImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> OpenEcmImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> OpenMdsImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> OpenPBPImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> OpenM3uImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage> OpenM3uImage(const char* filename, bool apply_patches, Common::Error* error);
static std::unique_ptr<CDImage> OpenDeviceImage(const char* filename, Common::Error* error);
static std::unique_ptr<CDImage>
CreateMemoryImage(CDImage* image, ProgressCallback* progress = ProgressCallback::NullProgressCallback);
@ -232,15 +232,42 @@ public:
ProgressCallback* progress = ProgressCallback::NullProgressCallback);
// Accessors.
const std::string& GetFileName() const { return m_filename; }
LBA GetPositionOnDisc() const { return m_position_on_disc; }
Position GetMSFPositionOnDisc() const { return Position::FromLBA(m_position_on_disc); }
LBA GetPositionInTrack() const { return m_position_in_track; }
Position GetMSFPositionInTrack() const { return Position::FromLBA(m_position_in_track); }
LBA GetLBACount() const { return m_lba_count; }
u32 GetIndexNumber() const { return m_current_index->index_number; }
u32 GetTrackNumber() const { return m_current_index->track_number; }
u32 GetTrackCount() const { return static_cast<u32>(m_tracks.size()); }
const std::string& GetFileName() const
{
return m_filename;
}
LBA GetPositionOnDisc() const
{
return m_position_on_disc;
}
Position GetMSFPositionOnDisc() const
{
return Position::FromLBA(m_position_on_disc);
}
LBA GetPositionInTrack() const
{
return m_position_in_track;
}
Position GetMSFPositionInTrack() const
{
return Position::FromLBA(m_position_in_track);
}
LBA GetLBACount() const
{
return m_lba_count;
}
u32 GetIndexNumber() const
{
return m_current_index->index_number;
}
u32 GetTrackNumber() const
{
return m_current_index->track_number;
}
u32 GetTrackCount() const
{
return static_cast<u32>(m_tracks.size());
}
LBA GetTrackStartPosition(u8 track) const;
Position GetTrackStartMSFPosition(u8 track) const;
LBA GetTrackLength(u8 track) const;
@ -248,11 +275,26 @@ public:
TrackMode GetTrackMode(u8 track) const;
LBA GetTrackIndexPosition(u8 track, u8 index) const;
LBA GetTrackIndexLength(u8 track, u8 index) const;
u32 GetFirstTrackNumber() const { return m_tracks.front().track_number; }
u32 GetLastTrackNumber() const { return m_tracks.back().track_number; }
u32 GetIndexCount() const { return static_cast<u32>(m_indices.size()); }
const std::vector<Track>& GetTracks() const { return m_tracks; }
const std::vector<Index>& GetIndices() const { return m_indices; }
u32 GetFirstTrackNumber() const
{
return m_tracks.front().track_number;
}
u32 GetLastTrackNumber() const
{
return m_tracks.back().track_number;
}
u32 GetIndexCount() const
{
return static_cast<u32>(m_indices.size());
}
const std::vector<Track>& GetTracks() const
{
return m_tracks;
}
const std::vector<Index>& GetIndices() const
{
return m_indices;
}
const Track& GetTrack(u32 track) const;
const Index& GetIndex(u32 i) const;

View File

@ -17,7 +17,7 @@ public:
CDImageM3u();
~CDImageM3u() override;
bool Open(const char* path, Common::Error* Error);
bool Open(const char* path, bool apply_patches, Common::Error* Error);
bool ReadSubChannelQ(SubChannelQ* subq, const Index& index, LBA lba_in_index) override;
bool HasNonStandardSubchannel() const override;
@ -42,13 +42,14 @@ private:
std::vector<Entry> m_entries;
std::unique_ptr<CDImage> m_current_image;
u32 m_current_image_index = UINT32_C(0xFFFFFFFF);
bool m_apply_patches = false;
};
CDImageM3u::CDImageM3u() = default;
CDImageM3u::~CDImageM3u() = default;
bool CDImageM3u::Open(const char* path, Common::Error* error)
bool CDImageM3u::Open(const char* path, bool apply_patches, Common::Error* error)
{
std::FILE* fp = FileSystem::OpenCFile(path, "rb");
if (!fp)
@ -65,6 +66,7 @@ bool CDImageM3u::Open(const char* path, Common::Error* error)
std::istringstream ifs(m3u_file.value());
m_filename = path;
m_apply_patches = apply_patches;
std::vector<std::string> entries;
std::string line;
@ -131,7 +133,7 @@ bool CDImageM3u::SwitchSubImage(u32 index, Common::Error* error)
return true;
const Entry& entry = m_entries[index];
std::unique_ptr<CDImage> new_image = CDImage::Open(entry.filename.c_str(), error);
std::unique_ptr<CDImage> new_image = CDImage::Open(entry.filename.c_str(), m_apply_patches, error);
if (!new_image)
{
Log_ErrorPrintf("Failed to load subimage %u (%s)", index, entry.filename.c_str());
@ -170,10 +172,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, Common::Error* error)
std::unique_ptr<CDImage> CDImage::OpenM3uImage(const char* filename, bool apply_patches, Common::Error* error)
{
std::unique_ptr<CDImageM3u> image = std::make_unique<CDImageM3u>();
if (!image->Open(filename, error))
if (!image->Open(filename, apply_patches, error))
return {};
return image;