From 4fef62aa3b5587c47631160727ba86ca66637bc3 Mon Sep 17 00:00:00 2001 From: hrydgard Date: Tue, 24 Feb 2009 19:57:29 +0000 Subject: [PATCH] Be friendlier when trying to but from empty DVD drives. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2422 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/DiscIO/Src/Blob.cpp | 2 +- Source/Core/DiscIO/Src/DriveBlob.cpp | 36 ++++++++++++++++++------ Source/Core/DiscIO/Src/DriveBlob.h | 4 ++- Source/Core/DiscIO/Src/VolumeCreator.cpp | 22 +++++++-------- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Source/Core/DiscIO/Src/Blob.cpp b/Source/Core/DiscIO/Src/Blob.cpp index a2c04feec8..7055f3df2a 100644 --- a/Source/Core/DiscIO/Src/Blob.cpp +++ b/Source/Core/DiscIO/Src/Blob.cpp @@ -131,7 +131,7 @@ IBlobReader* CreateBlobReader(const char* filename) if (IsCompressedBlob(filename)) return CompressedBlobReader::Create(filename); - // Still here? Assume plain file. + // Still here? Assume plain file - since we know it exists due to the File::Exists check above. return PlainFileReader::Create(filename); } diff --git a/Source/Core/DiscIO/Src/DriveBlob.cpp b/Source/Core/DiscIO/Src/DriveBlob.cpp index 7e798e337f..785ef11a17 100644 --- a/Source/Core/DiscIO/Src/DriveBlob.cpp +++ b/Source/Core/DiscIO/Src/DriveBlob.cpp @@ -21,23 +21,34 @@ namespace DiscIO { DriveReader::DriveReader(const char *drive) + : hDisc(INVALID_HANDLE_VALUE) { #ifdef _WIN32 char path[MAX_PATH]; strncpy(path, drive, 3); path[2] = 0; sprintf(path, "\\\\.\\%s", drive); - + SectorReader::SetSectorSize(2048); hDisc = CreateFile(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL); if (hDisc != INVALID_HANDLE_VALUE) { + // Do a test read to make sure everything is OK, since it seems you can get + // handles to empty drives. + DWORD not_used; + if (!ReadFile(hDisc, 0, m_blocksize, (LPDWORD)¬_used, NULL)) + { + // OK, something is wrong. + CloseHandle(hDisc); + hDisc = INVALID_HANDLE_VALUE; + return; + } #ifdef _LOCKDRIVE // Do we want to lock the drive? // Lock the compact disc in the CD-ROM drive to prevent accidental // removal while reading from it. pmrLockCDROM.PreventMediaRemoval = TRUE; - DeviceIoControl (hDisc, IOCTL_CDROM_MEDIA_REMOVAL, + DeviceIoControl(hDisc, IOCTL_CDROM_MEDIA_REMOVAL, &pmrLockCDROM, sizeof(pmrLockCDROM), NULL, 0, &dwNotUsed, NULL); #endif @@ -46,11 +57,10 @@ namespace DiscIO if (file_) { #endif - SectorReader::SetSectorSize(2048); } else - { - PanicAlert("Load from DVD backup failed or no disc in drive %s",drive); + { + PanicAlert("Load from DVD backup failed or no disc in drive %s", drive); } } // DriveReader::DriveReader @@ -67,15 +77,23 @@ namespace DiscIO if (hDisc != INVALID_HANDLE_VALUE) { CloseHandle(hDisc); + hDisc = INVALID_HANDLE_VALUE; } #else fclose(file_); + file_ = 0; #endif } - DriveReader * DriveReader::Create(const char *drive) + DriveReader *DriveReader::Create(const char *drive) { - return new DriveReader(drive); + DriveReader *reader = new DriveReader(drive); + if (!reader->IsOK()) + { + delete reader; + return 0; + } + return reader; } void DriveReader::GetBlock(u64 block_num, u8 *out_ptr) @@ -88,7 +106,7 @@ namespace DiscIO LONG off_high = (LONG)(offset >> 32); SetFilePointer(hDisc, off_low, &off_high, FILE_BEGIN); if (!ReadFile(hDisc, lpSector, m_blocksize, (LPDWORD)&NotUsed, NULL)) - PanicAlert("DRE"); + PanicAlert("Disc Read Error"); #else fseek(file_, m_blocksize*block_num, SEEK_SET); fread(lpSector, 1, m_blocksize, file_); @@ -107,7 +125,7 @@ namespace DiscIO SetFilePointer(hDisc, off_low, &off_high, FILE_BEGIN); if (!ReadFile(hDisc, out_ptr, (DWORD)(m_blocksize * num_blocks), (LPDWORD)&NotUsed, NULL)) { - PanicAlert("DRE"); + PanicAlert("Disc Read Error"); return false; } #else diff --git a/Source/Core/DiscIO/Src/DriveBlob.h b/Source/Core/DiscIO/Src/DriveBlob.h index 80c7ec093c..d1da218e91 100644 --- a/Source/Core/DiscIO/Src/DriveBlob.h +++ b/Source/Core/DiscIO/Src/DriveBlob.h @@ -36,9 +36,11 @@ private: #ifdef _WIN32 HANDLE hDisc; - PREVENT_MEDIA_REMOVAL pmrLockCDROM; + PREVENT_MEDIA_REMOVAL pmrLockCDROM; + bool IsOK() {return hDisc != INVALID_HANDLE_VALUE;} #else FILE* file_; + bool IsOK() {return file_ != 0;} #endif s64 size; u64 *block_pointers; diff --git a/Source/Core/DiscIO/Src/VolumeCreator.cpp b/Source/Core/DiscIO/Src/VolumeCreator.cpp index bb1d785abe..b3c84b85c6 100644 --- a/Source/Core/DiscIO/Src/VolumeCreator.cpp +++ b/Source/Core/DiscIO/Src/VolumeCreator.cpp @@ -93,7 +93,7 @@ IVolume* CreateVolumeFromFilename(const std::string& _rFilename) delete pReader; } - return(pVolume); + return pVolume; } break; @@ -133,17 +133,17 @@ IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _VolumeType, FILE* pT = fopen(MasterKeyFile, "rb"); if (pT == NULL) { - if(PanicYesNo("Can't open '%s'.\n If you know the key, now it's the time to paste it into " + if (PanicYesNo("Can't open '%s'.\n If you know the key, now it's the time to paste it into " "'%s' before pressing [YES].", MasterKeyFile, MasterKeyFileHex)) { pT = fopen(MasterKeyFileHex, "r"); - if(pT==NULL){ + if (!pT) { PanicAlert("could not open %s", MasterKeyFileHex); return NULL; } static char hexkey[32]; - if(fread(hexkey,1,32,pT)<32) + if (fread(hexkey, 1, 32, pT)<32) { PanicAlert("%s is not the right size", MasterKeyFileHex); fclose(pT); @@ -152,24 +152,24 @@ IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _VolumeType, fclose(pT); static char binkey[16]; - char *t=hexkey; - for(int i=0;i<16;i++) + char *t = hexkey; + for (int i = 0; i < 16; i++) { - char h[3]={*(t++),*(t++),0}; - binkey[i] = (char) strtol(h,NULL,16); + char h[3] = {*(t++), *(t++), 0}; + binkey[i] = (char)strtol(h, NULL, 16); } pT = fopen(MasterKeyFile, "wb"); - if(pT==NULL){ + if (!pT) { PanicAlert("could not open/make '%s' for writing!", MasterKeyFile); return NULL; } - fwrite(binkey,16,1,pT); + fwrite(binkey, 16, 1, pT); fclose(pT); pT = fopen(MasterKeyFileHex, "rb"); - if(pT==NULL){ + if (!pT) { PanicAlert("could not open '%s' for reading!\n did the file get deleted or locked after converting?", MasterKeyFileHex); return NULL; }