diff --git a/source/mijin/platform/folders.cpp b/source/mijin/platform/folders.cpp index e80c84f..aafd838 100644 --- a/source/mijin/platform/folders.cpp +++ b/source/mijin/platform/folders.cpp @@ -1,12 +1,12 @@ #include "./folders.hpp" +#include +#include #include "../detect.hpp" #include "../debug/assert.hpp" -#include - #if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS # include #endif @@ -103,4 +103,48 @@ fs::path getKnownFolder(KnownFolder folder) MIJIN_NOEXCEPT return {}; // don't know :/ #endif } + +std::vector getAllConfigFolders(IncludeUser includeUser, IncludeSystem includeSystem) MIJIN_NOEXCEPT +{ + std::vector result; + if (includeUser) + { + result.push_back(getKnownFolder(KnownFolder::USER_CONFIG_ROOT)); + } +#if MIJIN_TARGET_OS == MIJIN_OS_LINUX + if (includeSystem) + { + if (const char* val = std::getenv("XDG_CONFIG_DIRS"); val != nullptr) + { + for (auto part : std::ranges::split_view(std::string_view(val), ':')) + { + result.emplace_back(part.begin(), part.end()); + } + } + } +#endif + return result; +} + +std::vector getAllDataFolders(IncludeUser includeUser, IncludeSystem includeSystem) MIJIN_NOEXCEPT +{ + std::vector result; + if (includeUser) + { + result.push_back(getKnownFolder(KnownFolder::USER_DATA_ROOT)); + } +#if MIJIN_TARGET_OS == MIJIN_OS_LINUX + if (includeSystem) + { + if (const char* val = std::getenv("XDG_DATA_DIRS"); val != nullptr) + { + for (auto part : std::ranges::split_view(std::string_view(val), ':')) + { + result.emplace_back(part.begin(), part.end()); + } + } + } +#endif + return result; +} } \ No newline at end of file diff --git a/source/mijin/platform/folders.hpp b/source/mijin/platform/folders.hpp index 1e189dc..233120c 100644 --- a/source/mijin/platform/folders.hpp +++ b/source/mijin/platform/folders.hpp @@ -4,8 +4,10 @@ #define MIJIN_PLATFORM_FOLDERS_HPP_INCLUDED 1 #include "../internal/common.hpp" +#include "../util/flag.hpp" #include +#include namespace fs = std::filesystem; @@ -20,8 +22,19 @@ enum class KnownFolder USER_DATA_ROOT }; +MIJIN_DEFINE_FLAG(IncludeUser); +MIJIN_DEFINE_FLAG(IncludeSystem); + [[nodiscard]] fs::path getKnownFolder(KnownFolder folder) MIJIN_NOEXCEPT; + +[[nodiscard]] +std::vector getAllConfigFolders(IncludeUser includeUser = IncludeUser::YES, + IncludeSystem includeSystem = IncludeSystem::YES) MIJIN_NOEXCEPT; + +[[nodiscard]] +std::vector getAllDataFolders(IncludeUser includeUser = IncludeUser::YES, + IncludeSystem includeSystem = IncludeSystem::YES) MIJIN_NOEXCEPT; } #endif // defined(MIJIN_PLATFORM_FOLDERS_HPP_INCLUDED) diff --git a/source/mijin/virtual_filesystem/filesystem.hpp b/source/mijin/virtual_filesystem/filesystem.hpp index 47aa2b1..b0c7122 100644 --- a/source/mijin/virtual_filesystem/filesystem.hpp +++ b/source/mijin/virtual_filesystem/filesystem.hpp @@ -84,8 +84,15 @@ public: [[nodiscard]] virtual FileInfo getFileInfo(const fs::path& file) = 0; [[nodiscard]] virtual Optional getNativePath(const fs::path& /* file */) { return NULL_OPTIONAL; } [[nodiscard]] virtual StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) = 0; + virtual void getAllPaths(const fs::path& path, std::vector& outPaths) { outPaths.push_back(getPath(path)); } [[nodiscard]] PathReference getPath(fs::path path) MIJIN_NOEXCEPT { return PathReference(this, std::move(path)); } + [[nodiscard]] std::vector getAllPaths(const fs::path& path) + { + std::vector paths; + getAllPaths(path, paths); + return paths; + } }; class OSFileSystemAdapter : public FileSystemAdapter diff --git a/source/mijin/virtual_filesystem/stacked.cpp b/source/mijin/virtual_filesystem/stacked.cpp index 7f61624..e42151d 100644 --- a/source/mijin/virtual_filesystem/stacked.cpp +++ b/source/mijin/virtual_filesystem/stacked.cpp @@ -112,4 +112,12 @@ StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mo return StreamError::IO_ERROR; } +void StackedFileSystemAdapter::getAllPaths(const fs::path& path, std::vector& outPaths) +{ + for (auto& adapter : adapters_) + { + adapter->getAllPaths(path, outPaths); + } +} + } // namespace mijin diff --git a/source/mijin/virtual_filesystem/stacked.hpp b/source/mijin/virtual_filesystem/stacked.hpp index 725c5dc..4305179 100644 --- a/source/mijin/virtual_filesystem/stacked.hpp +++ b/source/mijin/virtual_filesystem/stacked.hpp @@ -33,6 +33,7 @@ public: FileInfo getFileInfo(const fs::path& file) override; Optional getNativePath(const fs::path& file) override; StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr& outStream) override; + void getAllPaths(const fs::path &path, std::vector &outPaths) override; inline void addAdapter(std::unique_ptr&& adapter) { adapters_.push_back(std::move(adapter));