diff --git a/common/FileSystem.cpp b/common/FileSystem.cpp index 44351aa9b4..31b6a28707 100644 --- a/common/FileSystem.cpp +++ b/common/FileSystem.cpp @@ -1196,7 +1196,7 @@ size_t FileSystem::ReadFileWithProgress(std::FILE* fp, void* dst, size_t length, break; const size_t read_size = std::min(length - done, chunk_size); - if (std::fread(static_cast(dst)+ done, read_size, 1, fp) != 1) + if (std::fread(static_cast(dst) + done, read_size, 1, fp) != 1) { Error::SetErrno(error, "fread() failed: ", errno); break; @@ -1460,7 +1460,24 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin } // enter the recursive function - return (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) > 0); + if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) == 0) + return false; + + if (flags & FILESYSTEM_FIND_SORT_BY_NAME) + { + std::sort(results->begin(), results->end(), [](const FILESYSTEM_FIND_DATA& lhs, const FILESYSTEM_FIND_DATA& rhs) { + // directories first + if ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) != + (rhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY)) + { + return ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) != 0); + } + + return (StringUtil::Strcasecmp(lhs.FileName.c_str(), rhs.FileName.c_str()) < 0); + }); + } + + return true; } static void TranslateStat64(struct stat* st, const struct _stat64& st64) @@ -2042,7 +2059,24 @@ bool FileSystem::FindFiles(const char* path, const char* pattern, u32 flags, Fin } // enter the recursive function - return (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) > 0); + if (RecursiveFindFiles(path, nullptr, nullptr, pattern, flags, results, visited) == 0) + return false; + + if (flags & FILESYSTEM_FIND_SORT_BY_NAME) + { + std::sort(results->begin(), results->end(), [](const FILESYSTEM_FIND_DATA& lhs, const FILESYSTEM_FIND_DATA& rhs) { + // directories first + if ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) != + (rhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY)) + { + return ((lhs.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY) != 0); + } + + return (StringUtil::Strcasecmp(lhs.FileName.c_str(), rhs.FileName.c_str()) < 0); + }); + } + + return true; } bool FileSystem::StatFile(const char* path, struct stat* st) diff --git a/common/FileSystem.h b/common/FileSystem.h index aec2b08eb7..1b6c89ab08 100644 --- a/common/FileSystem.h +++ b/common/FileSystem.h @@ -39,6 +39,7 @@ enum FILESYSTEM_FIND_FLAGS FILESYSTEM_FIND_FOLDERS = (1 << 3), FILESYSTEM_FIND_FILES = (1 << 4), FILESYSTEM_FIND_KEEP_ARRAY = (1 << 5), + FILESYSTEM_FIND_SORT_BY_NAME = (1 << 6), }; struct FILESYSTEM_STAT_DATA diff --git a/pcsx2/ImGui/ImGuiFullscreen.cpp b/pcsx2/ImGui/ImGuiFullscreen.cpp index ed566fa833..8d9a8a07e0 100644 --- a/pcsx2/ImGui/ImGuiFullscreen.cpp +++ b/pcsx2/ImGui/ImGuiFullscreen.cpp @@ -1863,7 +1863,9 @@ void ImGuiFullscreen::PopulateFileSelectorItems() { FileSystem::FindResultsArray results; FileSystem::FindFiles(s_file_selector_current_directory.c_str(), "*", - FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_FOLDERS | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RELATIVE_PATHS, &results); + FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_FOLDERS | FILESYSTEM_FIND_HIDDEN_FILES | + FILESYSTEM_FIND_RELATIVE_PATHS | FILESYSTEM_FIND_SORT_BY_NAME, + &results); std::string parent_path; std::string::size_type sep_pos = s_file_selector_current_directory.rfind(FS_OSPATH_SEPARATOR_CHARACTER); diff --git a/pcsx2/SIO/Pad/Pad.cpp b/pcsx2/SIO/Pad/Pad.cpp index 5c1e64029b..3a70bdf387 100644 --- a/pcsx2/SIO/Pad/Pad.cpp +++ b/pcsx2/SIO/Pad/Pad.cpp @@ -473,7 +473,8 @@ std::vector Pad::GetInputProfileNames() { FileSystem::FindResultsArray results; FileSystem::FindFiles(EmuFolders::InputProfiles.c_str(), "*.ini", - FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RELATIVE_PATHS, + FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_HIDDEN_FILES | FILESYSTEM_FIND_RELATIVE_PATHS | + FILESYSTEM_FIND_SORT_BY_NAME, &results); std::vector ret;