// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com) // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Config.hpp #pragma once #ifndef NAZARA_CORE_VIRTUALDIRECTORY_HPP #define NAZARA_CORE_VIRTUALDIRECTORY_HPP #include #include #include #include #include #include #include #include namespace Nz { class VirtualDirectory; using VirtualDirectoryPtr = std::shared_ptr; class VirtualDirectory : public std::enable_shared_from_this { public: struct DataPointerEntry; struct DirectoryEntry; struct FileContentEntry; struct PhysicalDirectoryEntry; struct PhysicalFileEntry; using Entry = std::variant; inline VirtualDirectory(std::weak_ptr parentDirectory = {}); inline VirtualDirectory(std::filesystem::path physicalPath, std::weak_ptr parentDirectory = {}); VirtualDirectory(const VirtualDirectory&) = delete; VirtualDirectory(VirtualDirectory&&) = delete; ~VirtualDirectory() = default; bool Exists(std::string_view path); template void Foreach(F&& callback, bool includeDots = false); template bool GetEntry(std::string_view path, F&& callback); template bool GetFileContent(std::string_view path, F&& callback); inline DirectoryEntry& StoreDirectory(std::string_view path, VirtualDirectoryPtr directory); inline PhysicalDirectoryEntry& StoreDirectory(std::string_view path, std::filesystem::path directoryPath); inline FileContentEntry& StoreFile(std::string_view path, std::vector file); inline PhysicalFileEntry& StoreFile(std::string_view path, std::filesystem::path filePath); inline DataPointerEntry& StoreFile(std::string_view path, const void* data, std::size_t size); VirtualDirectory& operator=(const VirtualDirectory&) = delete; VirtualDirectory& operator=(VirtualDirectory&&) = delete; struct DataPointerEntry { const void* data; std::size_t size; }; struct DirectoryEntry { VirtualDirectoryPtr directory; }; struct FileContentEntry { std::vector data; }; struct PhysicalDirectoryEntry { std::filesystem::path filePath; }; struct PhysicalFileEntry { std::filesystem::path filePath; }; private: template bool GetEntryInternal(std::string_view name, F&& callback); inline bool CreateOrRetrieveDirectory(std::string_view path, std::shared_ptr& directory, std::string_view& entryName); template T& StoreInternal(std::string name, T value); template static bool CallbackReturn(F&& callback, Args&&... args); template static bool SplitPath(std::string_view path, F1&& dirCB, F2&& fileCB); struct ContentEntry { std::string name; Entry entry; }; std::optional m_physicalPath; std::vector m_content; std::weak_ptr m_parent; }; } #include #endif // NAZARA_CORE_VIRTUALDIRECTORY_HPP