diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index b5ddb4c60f..cdcc236d0a 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -816,24 +816,19 @@ static void window_loadsave_populate_list(rct_window *w, sint32 includeNewItem, w->disabled_widgets &= ~(1 << WIDX_NEW_FOLDER); // List all directories - char subDir[MAX_PATH]; - sint32 fileEnumHandle = platform_enumerate_directories_begin(directory); - while (platform_enumerate_directories_next(fileEnumHandle, subDir)) + auto subDirectories = Path::GetDirectories(directory); + for (const auto &sdName : subDirectories) { + auto subDir = sdName + PATH_SEPARATOR; + LoadSaveListItem newListItem; - - char path[MAX_PATH]; - safe_strcpy(path, directory, sizeof(path)); - safe_strcat_path(path, subDir, sizeof(path)); - - newListItem.path = path; + newListItem.path = Path::Combine(directory, subDir); newListItem.name = subDir; newListItem.type = TYPE_DIRECTORY; newListItem.loaded = false; _listItems.push_back(newListItem); } - platform_enumerate_files_end(fileEnumHandle); // List all files with the wanted extensions char filter[MAX_PATH]; diff --git a/src/openrct2/core/FileScanner.cpp b/src/openrct2/core/FileScanner.cpp index b6624b8dd4..c12b565e11 100644 --- a/src/openrct2/core/FileScanner.cpp +++ b/src/openrct2/core/FileScanner.cpp @@ -31,6 +31,7 @@ #include "../localisation/Language.h" #endif +#include #include #include #include @@ -170,6 +171,8 @@ public: return false; } + virtual void GetDirectoryChildren(std::vector &children, const std::string &path) abstract; + private: void PushState(const std::string &directory) { @@ -219,10 +222,6 @@ private: patterns.shrink_to_fit(); return patterns; } - -protected: - virtual void GetDirectoryChildren(std::vector &children, const std::string &path) abstract; - }; #ifdef _WIN32 @@ -235,7 +234,6 @@ public: { } -protected: void GetDirectoryChildren(std::vector &children, const std::string &path) override { std::string pattern = path + "\\*"; @@ -296,7 +294,6 @@ public: { } -protected: void GetDirectoryChildren(std::vector &children, const std::string &path) override { struct dirent * * namelist; @@ -391,6 +388,25 @@ void Path::QueryDirectory(QueryDirectoryResult * result, const std::string &patt delete scanner; } +std::vector Path::GetDirectories(const std::string &path) +{ + auto scanner = std::unique_ptr(ScanDirectory(path, false)); + auto baseScanner = static_cast(scanner.get()); + + std::vector children; + baseScanner->GetDirectoryChildren(children, path); + + std::vector subDirectories; + for (const auto &c : children) + { + if (c.Type == DIRECTORY_CHILD_TYPE::DC_DIRECTORY) + { + subDirectories.push_back(c.Name); + } + } + return subDirectories; +} + static uint32 GetPathChecksum(const utf8 * path) { uint32 hash = 0xD8430DED; diff --git a/src/openrct2/core/FileScanner.h b/src/openrct2/core/FileScanner.h index 52e7c432da..f3424b0f6a 100644 --- a/src/openrct2/core/FileScanner.h +++ b/src/openrct2/core/FileScanner.h @@ -19,6 +19,7 @@ #ifdef __cplusplus #include +#include #include "../common.h" struct FileInfo @@ -66,6 +67,8 @@ namespace Path * @returns An aggregated result of all scanned files. */ void QueryDirectory(QueryDirectoryResult * result, const std::string &pattern); + + std::vector GetDirectories(const std::string &path); } #endif