From 3441574be613c1751e55dd9e99819c85ea1bf828 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 3 Jul 2026 00:25:28 +1000 Subject: [PATCH] CDImage: Add some more helper functions --- src/util/cd_image.cpp | 48 ++++++++++++++++++++++++++++--------------- src/util/cd_image.h | 7 +++++++ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/util/cd_image.cpp b/src/util/cd_image.cpp index de751ee4b..e457c0957 100644 --- a/src/util/cd_image.cpp +++ b/src/util/cd_image.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: CC-BY-NC-ND-4.0 #include "cd_image.h" +#include "translation.h" #include "common/assert.h" #include "common/bcdutils.h" @@ -12,6 +13,8 @@ #include "common/path.h" #include "common/string_util.h" +#include + #include LOG_CHANNEL(CDImage); @@ -53,13 +56,7 @@ void CDImage::DeinterleaveSubcode(const u8* subcode_in, u8* subcode_out) std::unique_ptr CDImage::Open(const char* path, bool allow_patches, Error* error) { - // Annoying handling because of storage access framework. -#ifdef __ANDROID__ - const std::string path_display_name = FileSystem::GetDisplayNameFromPath(path); - const std::string_view extension = Path::GetExtension(path_display_name); -#else const std::string_view extension = Path::GetExtension(path); -#endif std::unique_ptr image; if (extension.empty()) @@ -118,11 +115,7 @@ std::unique_ptr CDImage::Open(const char* path, bool allow_patches, Err if (allow_patches) { -#ifdef __ANDROID__ - const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(path_display_name, "ppf")); -#else const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(Path::GetFileName(path), "ppf")); -#endif if (FileSystem::FileExists(ppf_path.c_str())) { image = CDImage::OverlayPPFPatch(ppf_path.c_str(), std::move(image), error); @@ -136,17 +129,25 @@ std::unique_ptr CDImage::Open(const char* path, bool allow_patches, Err bool CDImage::HasOverlayablePatch(const char* path) { - // Annoying handling because of storage access framework. -#ifdef __ANDROID__ - const std::string ppf_path = - Path::BuildRelativePath(path, Path::ReplaceExtension(FileSystem::GetDisplayNameFromPath(path), "ppf")); -#else const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(Path::GetFileName(path), "ppf")); -#endif - return FileSystem::FileExists(ppf_path.c_str()); } +const char* CDImage::GetTrackModeDisplayName(TrackMode mode) +{ + static constexpr std::array track_mode_strings = {{ + TRANSLATE_DISAMBIG_NOOP("CDImage", "Audio", "TrackMode"), + TRANSLATE_DISAMBIG_NOOP("CDImage", "Mode 1", "TrackMode"), + TRANSLATE_DISAMBIG_NOOP("CDImage", "Mode 1/Raw", "TrackMode"), + TRANSLATE_DISAMBIG_NOOP("CDImage", "Mode 2", "TrackMode"), + TRANSLATE_DISAMBIG_NOOP("CDImage", "Mode 2/Form 1", "TrackMode"), + TRANSLATE_DISAMBIG_NOOP("CDImage", "Mode 2/Form 2", "TrackMode"), + TRANSLATE_DISAMBIG_NOOP("CDImage", "Mode 2/Mix", "TrackMode"), + TRANSLATE_DISAMBIG_NOOP("CDImage", "Mode 2/Raw", "TrackMode"), + }}; + return Host::TranslateToCString("CDImage", track_mode_strings[static_cast(mode)], "TrackMode"); +} + CDImage::LBA CDImage::GetTrackStartPosition(u8 track) const { Assert(track > 0 && track <= m_tracks.size()); @@ -362,6 +363,14 @@ s64 CDImage::GetSizeOnDisk() const return -1; } +std::string CDImage::GetSummary() const +{ + return fmt::format(TRANSLATE_PLURAL_FS("CDImage", "%n tracks covering {0} MB ({1} MB on disk)", "Number of tracks", + static_cast(m_tracks.size())), + ((m_lba_count * CDImage::RAW_SECTOR_SIZE) + 1048575) / 1048576, + (GetSizeOnDisk() + 1048575) / 1048576); +} + void CDImage::ClearTOC() { m_lba_count = 0; @@ -546,6 +555,11 @@ std::tuple CDImage::Position::ToBCD() const return std::make_tuple(BinaryToBCD(minute), BinaryToBCD(second), BinaryToBCD(frame)); } +std::string CDImage::Position::ToString() const +{ + return fmt::format("{:02}:{:02}:{:02}", minute, second, frame); +} + CDImage::Position CDImage::Position::operator+(const Position& rhs) { return FromLBA(ToLBA() + rhs.ToLBA()); diff --git a/src/util/cd_image.h b/src/util/cd_image.h index 77db4f2e8..f9a5d78fa 100644 --- a/src/util/cd_image.h +++ b/src/util/cd_image.h @@ -92,6 +92,7 @@ public: LBA ToLBA() const; std::tuple ToBCD() const; + std::string ToString() const; Position operator+(const Position& rhs); Position& operator+=(const Position& pos); @@ -189,6 +190,9 @@ public: /// Returns true if an overlayable patch file exists for the specified image path. static bool HasOverlayablePatch(const char* path); + /// Returns a readable string for the given track mode. + static const char* GetTrackModeDisplayName(TrackMode mode); + // Opening disc image. static std::unique_ptr Open(const char* path, bool allow_patches, Error* error); static std::unique_ptr OpenBinImage(const char* path, Error* error); @@ -281,6 +285,9 @@ public: // If this function returns -1, it means the size could not be computed. virtual s64 GetSizeOnDisk() const; + // Returns readable track summary of the image. + std::string GetSummary() const; + protected: void ClearTOC(); void CopyTOC(const CDImage* image);