From 5bfd4c7bf89c9a14488d6963eeca9cb2dd8bdce3 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 14 Apr 2015 19:18:34 -0700 Subject: [PATCH] Refactor ISO creation to avoid duplicate code. --- pcsx2/CDVD/CompressedFileReader.cpp | 16 +----------- pcsx2/CDVD/CompressedFileReader.h | 7 ++---- pcsx2/CDVD/InputIsoFile.cpp | 38 ++++++++++++++++++----------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/pcsx2/CDVD/CompressedFileReader.cpp b/pcsx2/CDVD/CompressedFileReader.cpp index 5e6588f6b9..4f41c997d0 100644 --- a/pcsx2/CDVD/CompressedFileReader.cpp +++ b/pcsx2/CDVD/CompressedFileReader.cpp @@ -20,20 +20,6 @@ #include "GzippedFileReader.h" // CompressedFileReader factory. - -// Go through available compressed readers -bool CompressedFileReader::DetectCompressed(AsyncFileReader* pReader) { - const wxString& filename = pReader->GetFilename(); - if (GzippedFileReader::CanHandle(filename)) { - return true; - } - if (CsoFileReader::CanHandle(filename)) { - return true; - } - return false; -} - -// Return a new reader which can handle, or any reader otherwise (which will fail on open) AsyncFileReader* CompressedFileReader::GetNewReader(const wxString& fileName) { if (GzippedFileReader::CanHandle(fileName)) { return new GzippedFileReader(); @@ -42,5 +28,5 @@ AsyncFileReader* CompressedFileReader::GetNewReader(const wxString& fileName) { return new CsoFileReader(); } // This is the one which will fail on open. - return new GzippedFileReader(); + return NULL; } diff --git a/pcsx2/CDVD/CompressedFileReader.h b/pcsx2/CDVD/CompressedFileReader.h index fbb11edc20..2267bf2c96 100644 --- a/pcsx2/CDVD/CompressedFileReader.h +++ b/pcsx2/CDVD/CompressedFileReader.h @@ -18,11 +18,8 @@ // Factory - creates an AsyncFileReader derived instance which can read a compressed file class CompressedFileReader { public: - // Checks if any of the available compressed file handlers can open this - static bool DetectCompressed(AsyncFileReader* pReader); - - // fileName is only used to choose the compressed reader. - // If no matching handler is found then an arbitrary handlers will be returned. + // fileName and its contents may be used to choose the compressed reader. + // If no matching handler is found, NULL is returned. // The returned instance still needs ->Open(filename) before usage. // Open(filename) may still fail. static AsyncFileReader* GetNewReader(const wxString& fileName); diff --git a/pcsx2/CDVD/InputIsoFile.cpp b/pcsx2/CDVD/InputIsoFile.cpp index ab1602f853..2bf6bef0bc 100644 --- a/pcsx2/CDVD/InputIsoFile.cpp +++ b/pcsx2/CDVD/InputIsoFile.cpp @@ -200,20 +200,34 @@ bool InputIsoFile::Open( const wxString& srcfile, bool testOnly ) { Close(); m_filename = srcfile; - - // Allow write sharing of the iso based on the ini settings. - // Mostly useful for romhacking, where the disc is frequently - // changed and the emulator would block modifications - m_reader = new FlatFileReader(EmuConfig.CdvdShareWrite); + m_reader = NULL; + + bool isBlockdump = false; + bool isCompressed = false; + + // First try using a compressed reader. If it works, go with it. + m_reader = CompressedFileReader::GetNewReader(m_filename); + isCompressed = m_reader != NULL; + + // If it wasn't compressed, let's open it has a FlatFileReader. + if (!isCompressed) + { + // Allow write sharing of the iso based on the ini settings. + // Mostly useful for romhacking, where the disc is frequently + // changed and the emulator would block modifications + m_reader = new FlatFileReader(EmuConfig.CdvdShareWrite); + } + m_reader->Open(m_filename); - bool isBlockdump, isCompressed = false; - if(isBlockdump = BlockdumpFileReader::DetectBlockdump(m_reader)) + // It might actually be a blockdump file. + // Check that before continuing with the FlatFileReader. + isBlockdump = BlockdumpFileReader::DetectBlockdump(m_reader); + if (isBlockdump) { delete m_reader; - BlockdumpFileReader *bdr = new BlockdumpFileReader();; - + BlockdumpFileReader *bdr = new BlockdumpFileReader(); bdr->Open(m_filename); m_blockofs = bdr->GetBlockOffset(); @@ -221,11 +235,7 @@ bool InputIsoFile::Open( const wxString& srcfile, bool testOnly ) m_reader = bdr; - ReadUnit = 1; - } else if (isCompressed = CompressedFileReader::DetectCompressed(m_reader)) { - delete m_reader; - m_reader = CompressedFileReader::GetNewReader(m_filename); - m_reader->Open(m_filename); + ReadUnit = 1; } bool detected = Detect();