From 5ca3aee00ac3ac1a906de68c216907a7b41825fe Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 29 Jun 2017 11:20:38 +0200 Subject: [PATCH] FileUtil: Add a class for Exists/IsDirectory/GetSize Some code was calling more than one of these functions in a row (in particular, FileUtil.cpp itself did it a lot...), which is a waste since it's possible to call stat a single time and then read all three values from the stat struct. This commit adds a File::FileInfo class that calls stat once on construction and then lets Exists/IsDirectory/GetSize be executed very quickly. The performance improvement mostly matters for functions that can be handling a lot of files, such as File::ScanDirectoryTree. I've also done some cleanup in code that uses these functions. For instance, some code had checks like !Exists() || !IsDirectory(), which is functionally equivalent to !IsDirectory(), and some code was using File::GetSize even though there was an IOFile object that the code could call GetSize on. --- Source/Core/Common/FileUtil.cpp | 144 +++++++++--------- Source/Core/Common/FileUtil.h | 42 ++++- Source/Core/Common/SysConf.cpp | 3 +- Source/Core/Core/Boot/Boot.cpp | 2 +- Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp | 3 - Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp | 36 ++--- .../Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp | 5 +- .../Core/HW/GCMemcard/GCMemcardDirectory.cpp | 4 +- Source/Core/Core/HW/WiiSaveCrypted.cpp | 12 +- Source/Core/Core/IOS/FS/FS.cpp | 6 +- Source/Core/Core/IOS/FS/FileIO.cpp | 2 +- .../Core/Core/IOS/Network/KD/NWC24Config.cpp | 17 +-- .../Core/IOS/Network/NCD/WiiNetConfig.cpp | 9 +- Source/Core/Core/IOS/USB/Bluetooth/BTBase.cpp | 4 +- Source/Core/Core/Movie.cpp | 17 +-- Source/Core/DiscIO/NANDContentLoader.cpp | 2 +- Source/Core/DiscIO/WiiWad.cpp | 2 +- .../DolphinWX/Config/GameCubeConfigPane.cpp | 17 +-- .../DolphinWX/ISOProperties/ISOProperties.cpp | 17 +-- .../Core/DolphinWX/Input/InputConfigDiag.cpp | 6 +- Source/Core/VideoCommon/TextureCacheBase.cpp | 2 +- 21 files changed, 175 insertions(+), 177 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 597908f740..6f8e3a8b24 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -51,38 +51,67 @@ // REMEMBER: strdup considered harmful! namespace File { -// Returns true if file filename exists -bool Exists(const std::string& filename) -{ - struct stat file_info; - #ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(filename).c_str(), &file_info); -#else - int result = stat(filename.c_str(), &file_info); -#endif - - return (result == 0); +FileInfo::FileInfo(const std::string& path) +{ + m_exists = _tstat64(UTF8ToTStr(path).c_str(), &m_stat) == 0; } -// Returns true if filename is a directory -bool IsDirectory(const std::string& filename) +FileInfo::FileInfo(const char* path) : FileInfo(std::string(path)) { - struct stat file_info; - -#ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(filename).c_str(), &file_info); +} #else - int result = stat(filename.c_str(), &file_info); +FileInfo::FileInfo(const std::string& path) : FileInfo(path.c_str()) +{ +} + +FileInfo::FileInfo(const char* path) +{ + m_exists = stat(path, &m_stat) == 0; +} #endif - if (result < 0) - { - WARN_LOG(COMMON, "IsDirectory: stat failed on %s: %s", filename.c_str(), strerror(errno)); - return false; - } +FileInfo::FileInfo(int fd) +{ + m_exists = fstat(fd, &m_stat); +} - return S_ISDIR(file_info.st_mode); +bool FileInfo::Exists() const +{ + return m_exists; +} + +bool FileInfo::IsDirectory() const +{ + return m_exists ? S_ISDIR(m_stat.st_mode) : false; +} + +bool FileInfo::IsFile() const +{ + return m_exists ? !S_ISDIR(m_stat.st_mode) : false; +} + +u64 FileInfo::GetSize() const +{ + return IsFile() ? m_stat.st_size : 0; +} + +// Returns true if the path exists +bool Exists(const std::string& path) +{ + return FileInfo(path).Exists(); +} + +// Returns true if the path exists and is a directory +bool IsDirectory(const std::string& path) +{ + return FileInfo(path).IsDirectory(); +} + +// Returns true if the path exists and is a file +bool IsFile(const std::string& path) +{ + return FileInfo(path).IsFile(); } // Deletes a given filename, return true on success @@ -91,16 +120,18 @@ bool Delete(const std::string& filename) { INFO_LOG(COMMON, "Delete: file %s", filename.c_str()); + const FileInfo file_info(filename); + // Return true because we care about the file no // being there, not the actual delete. - if (!Exists(filename)) + if (!file_info.Exists()) { WARN_LOG(COMMON, "Delete: %s does not exist", filename.c_str()); return true; } // We can't delete a directory - if (IsDirectory(filename)) + if (file_info.IsDirectory()) { WARN_LOG(COMMON, "Delete failed: %s is a directory", filename.c_str()); return false; @@ -163,7 +194,7 @@ bool CreateFullPath(const std::string& fullPath) int panicCounter = 100; INFO_LOG(COMMON, "CreateFullPath: path %s", fullPath.c_str()); - if (File::Exists(fullPath)) + if (Exists(fullPath)) { INFO_LOG(COMMON, "CreateFullPath: path exists %s", fullPath.c_str()); return true; @@ -181,7 +212,7 @@ bool CreateFullPath(const std::string& fullPath) // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") std::string const subPath(fullPath.substr(0, position + 1)); - if (!File::IsDirectory(subPath)) + if (!IsDirectory(subPath)) File::CreateDir(subPath); // A safety check @@ -201,7 +232,7 @@ bool DeleteDir(const std::string& filename) INFO_LOG(COMMON, "DeleteDir: directory %s", filename.c_str()); // check if a directory - if (!File::IsDirectory(filename)) + if (!IsDirectory(filename)) { ERROR_LOG(COMMON, "DeleteDir: Not a directory %s", filename.c_str()); return false; @@ -344,46 +375,16 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename) #endif } -// Returns the size of filename (64bit) -u64 GetSize(const std::string& filename) +// Returns the size of a file (or returns 0 if the path isn't a file that exists) +u64 GetSize(const std::string& path) { - if (!Exists(filename)) - { - WARN_LOG(COMMON, "GetSize: failed %s: No such file", filename.c_str()); - return 0; - } - - if (IsDirectory(filename)) - { - WARN_LOG(COMMON, "GetSize: failed %s: is a directory", filename.c_str()); - return 0; - } - - struct stat buf; -#ifdef _WIN32 - if (_tstat64(UTF8ToTStr(filename).c_str(), &buf) == 0) -#else - if (stat(filename.c_str(), &buf) == 0) -#endif - { - DEBUG_LOG(COMMON, "GetSize: %s: %lld", filename.c_str(), (long long)buf.st_size); - return buf.st_size; - } - - ERROR_LOG(COMMON, "GetSize: Stat failed %s: %s", filename.c_str(), GetLastErrorMsg().c_str()); - return 0; + return FileInfo(path).GetSize(); } // Overloaded GetSize, accepts file descriptor u64 GetSize(const int fd) { - struct stat buf; - if (fstat(fd, &buf) != 0) - { - ERROR_LOG(COMMON, "GetSize: stat failed %i: %s", fd, GetLastErrorMsg().c_str()); - return 0; - } - return buf.st_size; + return FileInfo(fd).GetSize(); } // Overloaded GetSize, accepts FILE* @@ -458,7 +459,8 @@ FSTEntry ScanDirectoryTree(const std::string& directory, bool recursive) continue; auto physical_name = directory + DIR_SEP + virtual_name; FSTEntry entry; - entry.isDirectory = IsDirectory(physical_name); + const FileInfo file_info(physical_name); + entry.isDirectory = file_info.IsDirectory(); if (entry.isDirectory) { if (recursive) @@ -469,7 +471,7 @@ FSTEntry ScanDirectoryTree(const std::string& directory, bool recursive) } else { - entry.size = GetSize(physical_name); + entry.size = file_info.GetSize(); } entry.virtualName = virtual_name; entry.physicalName = physical_name; @@ -561,9 +563,9 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) { if (source_path == dest_path) return; - if (!File::Exists(source_path)) + if (!Exists(source_path)) return; - if (!File::Exists(dest_path)) + if (!Exists(dest_path)) File::CreateFullPath(dest_path); #ifdef _WIN32 @@ -596,11 +598,11 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) std::string dest = dest_path + DIR_SEP + virtualName; if (IsDirectory(source)) { - if (!File::Exists(dest)) + if (!Exists(dest)) File::CreateFullPath(dest + DIR_SEP); CopyDir(source, dest); } - else if (!File::Exists(dest)) + else if (!Exists(dest)) File::Copy(source, dest); #ifdef _WIN32 } while (FindNextFile(hFind, &ffd) != 0); @@ -852,12 +854,12 @@ void SetUserPath(unsigned int dir_index, const std::string& path) std::string GetThemeDir(const std::string& theme_name) { std::string dir = File::GetUserPath(D_THEMES_IDX) + theme_name + "/"; - if (File::Exists(dir)) + if (Exists(dir)) return dir; // If the theme doesn't exist in the user dir, load from shared directory dir = GetSysDirectory() + THEMES_DIR "/" + theme_name + "/"; - if (File::Exists(dir)) + if (Exists(dir)) return dir; // If the theme doesn't exist at all, load the default theme diff --git a/Source/Core/Common/FileUtil.h b/Source/Core/Common/FileUtil.h index 5eec5382ce..d8f1c660eb 100644 --- a/Source/Core/Common/FileUtil.h +++ b/Source/Core/Common/FileUtil.h @@ -9,6 +9,8 @@ #include #include +#include + #include "Common/CommonTypes.h" #include "Common/NonCopyable.h" @@ -78,14 +80,42 @@ struct FSTEntry std::vector children; }; -// Returns true if file filename exists -bool Exists(const std::string& filename); +// The functions in this class are functionally identical to the standalone functions +// below, but if you are going to be calling more than one of the functions using the +// same path, creating a single FileInfo object and calling its functions multiple +// times is faster than calling standalone functions multiple times. +class FileInfo final +{ +public: + explicit FileInfo(const std::string& path); + explicit FileInfo(const char* path); + explicit FileInfo(int fd); -// Returns true if filename is a directory -bool IsDirectory(const std::string& filename); + // Returns true if the path exists + bool Exists() const; + // Returns true if the path exists and is a directory + bool IsDirectory() const; + // Returns true if the path exists and is a file + bool IsFile() const; + // Returns the size of a file (or returns 0 if the path doesn't refer to a file) + u64 GetSize() const; -// Returns the size of filename (64bit) -u64 GetSize(const std::string& filename); +private: + struct stat m_stat; + bool m_exists; +}; + +// Returns true if the path exists +bool Exists(const std::string& path); + +// Returns true if the path exists and is a directory +bool IsDirectory(const std::string& path); + +// Returns true if the path exists and is a file +bool IsFile(const std::string& path); + +// Returns the size of a file (or returns 0 if the path isn't a file that exists) +u64 GetSize(const std::string& path); // Overloaded GetSize, accepts file descriptor u64 GetSize(const int fd); diff --git a/Source/Core/Common/SysConf.cpp b/Source/Core/Common/SysConf.cpp index 2a96feeea0..4e5e88fb83 100644 --- a/Source/Core/Common/SysConf.cpp +++ b/Source/Core/Common/SysConf.cpp @@ -56,8 +56,7 @@ void SysConf::Load() { Clear(); - if (!File::Exists(m_file_name) || File::GetSize(m_file_name) != SYSCONF_SIZE || - !LoadFromFile(m_file_name)) + if (File::GetSize(m_file_name) != SYSCONF_SIZE || !LoadFromFile(m_file_name)) { WARN_LOG(CORE, "No valid SYSCONF detected. Creating a new one."); InsertDefaultEntries(); diff --git a/Source/Core/Core/Boot/Boot.cpp b/Source/Core/Core/Boot/Boot.cpp index 2b695073e5..7d83cc7660 100644 --- a/Source/Core/Core/Boot/Boot.cpp +++ b/Source/Core/Core/Boot/Boot.cpp @@ -466,8 +466,8 @@ bool CBoot::BootUp(std::unique_ptr boot) BootExecutableReader::BootExecutableReader(const std::string& file_name) { - m_bytes.resize(File::GetSize(file_name)); File::IOFile file{file_name, "rb"}; + m_bytes.resize(file.GetSize()); file.ReadBytes(m_bytes.data(), m_bytes.size()); } diff --git a/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp b/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp index b9fbb6c5b4..74a61073ef 100644 --- a/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp +++ b/Source/Core/Core/HW/DSPHLE/UCodes/AX.cpp @@ -52,9 +52,6 @@ void AXUCode::LoadResamplingCoefficients() for (fidx = 0; fidx < ArraySize(filenames); ++fidx) { filename = filenames[fidx]; - if (!File::Exists(filename)) - continue; - if (File::GetSize(filename) != 0x1000) continue; diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp index a5df52b53f..69f2b6fc91 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp @@ -204,30 +204,26 @@ void CEXIIPL::LoadFontFile(const std::string& filename, u32 offset) if (ipl_rom_path.empty()) ipl_rom_path = FindIPLDump(File::GetSysDirectory() + GC_SYS_DIR); - if (File::Exists(ipl_rom_path)) - { - // The user has an IPL dump, load the font from it - File::IOFile stream(ipl_rom_path, "rb"); - if (!stream) - return; - - // Official Windows-1252 and Shift JIS fonts present on the IPL dumps are 0x2575 and 0x4a24d - // bytes long respectively, so, determine the size of the font being loaded based on the offset - u64 fontsize = (offset == 0x1aff00) ? 0x4a24d : 0x2575; - - INFO_LOG(BOOT, "Found IPL dump, loading %s font from %s", - ((offset == 0x1aff00) ? "Shift JIS" : "Windows-1252"), (ipl_rom_path).c_str()); - - stream.Seek(offset, 0); - stream.ReadBytes(m_pIPL + offset, fontsize); - - m_FontsLoaded = true; - } - else + // If the user has an IPL dump, load the font from it + File::IOFile stream(ipl_rom_path, "rb"); + if (!stream) { // No IPL dump available, load bundled font instead LoadFileToIPL(filename, offset); + return; } + + // Official Windows-1252 and Shift JIS fonts present on the IPL dumps are 0x2575 and 0x4a24d + // bytes long respectively, so, determine the size of the font being loaded based on the offset + u64 fontsize = (offset == 0x1aff00) ? 0x4a24d : 0x2575; + + INFO_LOG(BOOT, "Found IPL dump, loading %s font from %s", + ((offset == 0x1aff00) ? "Shift JIS" : "Windows-1252"), (ipl_rom_path).c_str()); + + stream.Seek(offset, 0); + stream.ReadBytes(m_pIPL + offset, fontsize); + + m_FontsLoaded = true; } void CEXIIPL::SetCS(int _iCS) diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp index 5b2e1f8ea9..8c819eeb2f 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp @@ -176,11 +176,12 @@ void CEXIMemoryCard::SetupGciFolder(u16 sizeMb) strDirectoryName = strDirectoryName + SConfig::GetDirectoryForRegion(region) + DIR_SEP + StringFromFormat("Card %c", 'A' + card_index); - if (!File::Exists(strDirectoryName)) // first use of memcard folder, migrate automatically + const File::FileInfo file_info(strDirectoryName); + if (!file_info.Exists()) // first use of memcard folder, migrate automatically { MigrateFromMemcardFile(strDirectoryName + DIR_SEP, card_index); } - else if (!File::IsDirectory(strDirectoryName)) + else if (!file_info.IsDirectory()) { if (File::Rename(strDirectoryName, strDirectoryName + ".original")) { diff --git a/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp b/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp index 3dcdb64d11..23c0e4b467 100644 --- a/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp +++ b/Source/Core/Core/HW/GCMemcard/GCMemcardDirectory.cpp @@ -142,10 +142,8 @@ GCMemcardDirectory::GCMemcardDirectory(const std::string& directory, int slot, u m_save_directory(directory), m_exiting(false) { // Use existing header data if available - if (File::Exists(m_save_directory + MC_HDR)) { - File::IOFile hdr_file((m_save_directory + MC_HDR), "rb"); - hdr_file.ReadBytes(&m_hdr, BLOCK_SIZE); + File::IOFile((m_save_directory + MC_HDR), "rb").ReadBytes(&m_hdr, BLOCK_SIZE); } std::vector filenames = Common::DoFileSearch({m_save_directory}, {".gci"}); diff --git a/Source/Core/Core/HW/WiiSaveCrypted.cpp b/Source/Core/Core/HW/WiiSaveCrypted.cpp index 004a58ba33..48fcc8f765 100644 --- a/Source/Core/Core/HW/WiiSaveCrypted.cpp +++ b/Source/Core/Core/HW/WiiSaveCrypted.cpp @@ -343,6 +343,7 @@ void CWiiSaveCrypted::ImportWiiSaveFiles() std::string file_path_full = m_wii_title_path + file_path; File::CreateFullPath(file_path_full); + const File::FileInfo file_info(file_path_full); if (file_hdr_tmp.type == 1) { file_size = Common::swap32(file_hdr_tmp.size); @@ -361,7 +362,7 @@ void CWiiSaveCrypted::ImportWiiSaveFiles() mbedtls_aes_crypt_cbc(&m_aes_ctx, MBEDTLS_AES_DECRYPT, file_size_rounded, m_iv, static_cast(file_data_enc.data()), file_data.data()); - if (!File::Exists(file_path_full) || + if (!file_info.Exists() || AskYesNoT("%s already exists, overwrite?", file_path_full.c_str())) { INFO_LOG(CONSOLE, "Creating file %s", file_path_full.c_str()); @@ -372,12 +373,12 @@ void CWiiSaveCrypted::ImportWiiSaveFiles() } else if (file_hdr_tmp.type == 2) { - if (!File::Exists(file_path_full)) + if (!file_info.Exists()) { if (!File::CreateDir(file_path_full)) ERROR_LOG(CONSOLE, "Failed to create directory %s", file_path_full.c_str()); } - else if (!File::IsDirectory(file_path_full)) + else if (!file_info.IsDirectory()) { ERROR_LOG(CONSOLE, "Failed to create directory %s because a file with the same name exists", @@ -399,13 +400,14 @@ void CWiiSaveCrypted::ExportWiiSaveFiles() memset(&file_hdr_tmp, 0, FILE_HDR_SZ); u32 file_size = 0; - if (File::IsDirectory(m_files_list[i])) + const File::FileInfo file_info(m_files_list[i]); + if (file_info.IsDirectory()) { file_hdr_tmp.type = 2; } else { - file_size = static_cast(File::GetSize(m_files_list[i])); + file_size = static_cast(file_info.GetSize()); file_hdr_tmp.type = 1; } diff --git a/Source/Core/Core/IOS/FS/FS.cpp b/Source/Core/Core/IOS/FS/FS.cpp index 0dffca2e9d..c27c5c908c 100644 --- a/Source/Core/Core/IOS/FS/FS.cpp +++ b/Source/Core/Core/IOS/FS/FS.cpp @@ -524,13 +524,15 @@ IPCCommandResult FS::ReadDirectory(const IOCtlVRequest& request) INFO_LOG(IOS_FILEIO, "FS: IOCTL_READ_DIR %s", DirName.c_str()); - if (!File::Exists(DirName)) + const File::FileInfo file_info(DirName); + + if (!file_info.Exists()) { WARN_LOG(IOS_FILEIO, "FS: Search not found: %s", DirName.c_str()); return GetFSReply(FS_ENOENT); } - if (!File::IsDirectory(DirName)) + if (!file_info.IsDirectory()) { // It's not a directory, so error. // Games don't usually seem to care WHICH error they get, as long as it's < diff --git a/Source/Core/Core/IOS/FS/FileIO.cpp b/Source/Core/Core/IOS/FS/FileIO.cpp index 7ee063e7c6..ed70f88e59 100644 --- a/Source/Core/Core/IOS/FS/FileIO.cpp +++ b/Source/Core/Core/IOS/FS/FileIO.cpp @@ -102,7 +102,7 @@ ReturnCode FileIO::Open(const OpenRequest& request) // The file must exist before we can open it // It should be created by ISFS_CreateFile, not here - if (!File::Exists(m_filepath) || File::IsDirectory(m_filepath)) + if (!File::IsFile(m_filepath)) { WARN_LOG(IOS_FILEIO, "FileIO: Open (%s) failed - File doesn't exist %s", Modes[m_Mode], m_filepath.c_str()); diff --git a/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp b/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp index 8609e4b6ab..b6003477f4 100644 --- a/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp +++ b/Source/Core/Core/IOS/Network/KD/NWC24Config.cpp @@ -27,22 +27,15 @@ NWC24Config::NWC24Config() void NWC24Config::ReadConfig() { - if (File::Exists(m_path)) + if (!File::IOFile(m_path, "rb").ReadBytes(&m_data, sizeof(m_data))) { - if (!File::IOFile(m_path, "rb").ReadBytes(&m_data, sizeof(m_data))) - { - ResetConfig(); - } - else - { - const s32 config_error = CheckNwc24Config(); - if (config_error) - ERROR_LOG(IOS_WC24, "There is an error in the config for for WC24: %d", config_error); - } + ResetConfig(); } else { - ResetConfig(); + const s32 config_error = CheckNwc24Config(); + if (config_error) + ERROR_LOG(IOS_WC24, "There is an error in the config for for WC24: %d", config_error); } } diff --git a/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp b/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp index 3c18ace58a..97a04a3b13 100644 --- a/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp +++ b/Source/Core/Core/IOS/Network/NCD/WiiNetConfig.cpp @@ -27,15 +27,8 @@ WiiNetConfig::WiiNetConfig() void WiiNetConfig::ReadConfig() { - if (File::Exists(m_path)) - { - if (!File::IOFile(m_path, "rb").ReadBytes(&m_data, sizeof(m_data))) - ResetConfig(); - } - else - { + if (!File::IOFile(m_path, "rb").ReadBytes(&m_data, sizeof(m_data))) ResetConfig(); - } } void WiiNetConfig::WriteConfig() const diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTBase.cpp b/Source/Core/Core/IOS/USB/Bluetooth/BTBase.cpp index c90dfa3e14..ada80f2f17 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTBase.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTBase.cpp @@ -40,9 +40,9 @@ void BackUpBTInfoSection(const SysConf* sysconf) void RestoreBTInfoSection(SysConf* sysconf) { const std::string filename = File::GetUserPath(D_SESSION_WIIROOT_IDX) + DIR_SEP WII_BTDINF_BACKUP; - if (!File::Exists(filename)) - return; File::IOFile backup(filename, "rb"); + if (!backup) + return; std::vector section(BT_INFO_SECTION_LENGTH); if (!backup.ReadBytes(section.data(), section.size())) { diff --git a/Source/Core/Core/Movie.cpp b/Source/Core/Core/Movie.cpp index cade8d20f0..009a3d0760 100644 --- a/Source/Core/Core/Movie.cpp +++ b/Source/Core/Core/Movie.cpp @@ -960,20 +960,13 @@ bool PlayInput(const std::string& filename) if (s_playMode != MODE_NONE) return false; - if (!File::Exists(filename)) + File::IOFile recording_file(filename, "rb"); + if (!recording_file.ReadArray(&tmpHeader, 1)) return false; - File::IOFile g_recordfd; - - if (!g_recordfd.Open(filename, "rb")) - return false; - - g_recordfd.ReadArray(&tmpHeader, 1); - if (!IsMovieHeader(tmpHeader.filetype)) { PanicAlertT("Invalid recording file"); - g_recordfd.Close(); return false; } @@ -993,11 +986,11 @@ bool PlayInput(const std::string& filename) Core::UpdateWantDeterminism(); - s_totalBytes = g_recordfd.GetSize() - 256; + s_totalBytes = recording_file.GetSize() - 256; EnsureTmpInputSize((size_t)s_totalBytes); - g_recordfd.ReadArray(tmpInput, (size_t)s_totalBytes); + recording_file.ReadArray(tmpInput, (size_t)s_totalBytes); s_currentByte = 0; - g_recordfd.Close(); + recording_file.Close(); // Load savestate (and skip to frame data) if (tmpHeader.bFromSaveState) diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index 0178c3c14e..47ea26c81a 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -163,7 +163,7 @@ bool NANDContentLoader::Initialize(const std::string& name) return false; } - std::vector bytes(File::GetSize(tmd_filename)); + std::vector bytes(tmd_file.GetSize()); tmd_file.ReadBytes(bytes.data(), bytes.size()); m_tmd.SetBytes(std::move(bytes)); diff --git a/Source/Core/DiscIO/WiiWad.cpp b/Source/Core/DiscIO/WiiWad.cpp index 8bd0164cab..1b04f3e68d 100644 --- a/Source/Core/DiscIO/WiiWad.cpp +++ b/Source/Core/DiscIO/WiiWad.cpp @@ -46,7 +46,7 @@ bool IsWiiWAD(BlobReader& reader) WiiWAD::WiiWAD(const std::string& name) : m_reader(CreateBlobReader(name)) { - if (m_reader == nullptr || File::IsDirectory(name)) + if (m_reader == nullptr) { m_valid = false; return; diff --git a/Source/Core/DolphinWX/Config/GameCubeConfigPane.cpp b/Source/Core/DolphinWX/Config/GameCubeConfigPane.cpp index 2b904a684c..43d1fa11ac 100644 --- a/Source/Core/DolphinWX/Config/GameCubeConfigPane.cpp +++ b/Source/Core/DolphinWX/Config/GameCubeConfigPane.cpp @@ -359,18 +359,15 @@ void GameCubeConfigPane::ChooseSlotPath(bool is_slot_a, ExpansionInterface::TEXI if (!filename.empty()) { - if (File::Exists(filename)) + if (memcard && File::Exists(filename)) { - if (memcard) + GCMemcard memorycard(filename); + if (!memorycard.IsValid()) { - GCMemcard memorycard(filename); - if (!memorycard.IsValid()) - { - WxUtils::ShowErrorDialog(wxString::Format(_("Cannot use that file as a memory card.\n%s\n" - "is not a valid GameCube memory card file"), - filename.c_str())); - return; - } + WxUtils::ShowErrorDialog(wxString::Format(_("Cannot use that file as a memory card.\n%s\n" + "is not a valid GameCube memory card file"), + filename.c_str())); + return; } } diff --git a/Source/Core/DolphinWX/ISOProperties/ISOProperties.cpp b/Source/Core/DolphinWX/ISOProperties/ISOProperties.cpp index b05baa7250..7dcff7c0b2 100644 --- a/Source/Core/DolphinWX/ISOProperties/ISOProperties.cpp +++ b/Source/Core/DolphinWX/ISOProperties/ISOProperties.cpp @@ -4,6 +4,7 @@ #include "DolphinWX/ISOProperties/ISOProperties.h" +#include #include #include #include @@ -442,16 +443,12 @@ void CISOProperties::CreateGUIControls() sButtons->GetAffirmativeButton()->SetLabel(_("Close")); // If there is no default gameini, disable the button. - bool game_ini_exists = false; - for (const std::string& ini_filename : - SConfig::GetGameIniFilenames(game_id, m_open_iso->GetRevision())) - { - if (File::Exists(File::GetSysDirectory() + GAMESETTINGS_DIR DIR_SEP + ini_filename)) - { - game_ini_exists = true; - break; - } - } + const std::vector ini_names = + SConfig::GetGameIniFilenames(game_id, m_open_iso->GetRevision()); + const bool game_ini_exists = + std::any_of(ini_names.cbegin(), ini_names.cend(), [](const std::string& name) { + return File::Exists(File::GetSysDirectory() + GAMESETTINGS_DIR DIR_SEP + name); + }); if (!game_ini_exists) EditConfigDefault->Disable(); diff --git a/Source/Core/DolphinWX/Input/InputConfigDiag.cpp b/Source/Core/DolphinWX/Input/InputConfigDiag.cpp index 8aaa51ccb0..b0e5c96371 100644 --- a/Source/Core/DolphinWX/Input/InputConfigDiag.cpp +++ b/Source/Core/DolphinWX/Input/InputConfigDiag.cpp @@ -856,11 +856,9 @@ void InputConfigDialog::LoadProfile(wxCommandEvent&) std::string fname; InputConfigDialog::GetProfilePath(fname); - if (!File::Exists(fname)) - return; - IniFile inifile; - inifile.Load(fname); + if (!inifile.Load(fname)) + return; controller->LoadConfig(inifile.GetOrCreateSection("Profile")); controller->UpdateReferences(g_controller_interface); diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index c61fa1fe5e..5c7c61695a 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -424,7 +424,7 @@ void TextureCacheBase::DumpTexture(TCacheEntry* entry, std::string basename, uns std::string szDir = File::GetUserPath(D_DUMPTEXTURES_IDX) + SConfig::GetInstance().GetGameID(); // make sure that the directory exists - if (!File::Exists(szDir) || !File::IsDirectory(szDir)) + if (!File::IsDirectory(szDir)) File::CreateDir(szDir); if (level > 0)