From 6f20b98ce1e6be29ec4efd9b205a9ad0f52c09ac Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 20 Oct 2022 18:43:09 +0800 Subject: [PATCH] Filesystem rework to go through asset folder, build directory and populate folders with files --- .../Filesystem/SHFileSystemDefines.h | 31 ++++++++ .../Filesystem/SHFileSystemLibrary.cpp} | 76 +++++++++---------- .../Filesystem/SHFileSystemLibrary.h | 56 ++++++++++++++ SHADE_Engine/src/Assets/SHAsset.h | 2 - SHADE_Engine/src/Assets/SHAssetManager.cpp | 21 ++--- SHADE_Engine/src/Filesystem/SHFileSystem.h | 70 ----------------- 6 files changed, 135 insertions(+), 121 deletions(-) create mode 100644 SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemDefines.h rename SHADE_Engine/src/{Filesystem/SHFileSystem.cpp => Assets/Libraries/Filesystem/SHFileSystemLibrary.cpp} (59%) create mode 100644 SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemLibrary.h delete mode 100644 SHADE_Engine/src/Filesystem/SHFileSystem.h diff --git a/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemDefines.h b/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemDefines.h new file mode 100644 index 00000000..1d730a3f --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemDefines.h @@ -0,0 +1,31 @@ +/*************************************************************************//** + * \file SHFileSystemDefines.h + * \author Loh Xiao Qi + * \date October 2022 + * \brief + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ +#pragma once +#include + +namespace SHADE +{ + class SHFolder; + + typedef unsigned char FolderCounter; + typedef unsigned char FileCounter; + typedef uint64_t FolderLocation; + typedef uint64_t FolderHandle; + typedef std::string FolderName; + typedef std::string FileName; + typedef std::string FolderPath; + typedef std::string FilePath; + typedef std::string FileExt; + typedef SHFolder* FolderPointer; + + constexpr char FOLDER_BIT_ALLOCATE{ 4 }; + constexpr char FOLDER_MAX_DEPTH{ 16 }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp b/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemLibrary.cpp similarity index 59% rename from SHADE_Engine/src/Filesystem/SHFileSystem.cpp rename to SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemLibrary.cpp index bd34ed71..08f13288 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp +++ b/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemLibrary.cpp @@ -1,5 +1,5 @@ #include "SHpch.h" -#include "SHFileSystem.h" +#include "SHFileSystemLibrary.h" #include "fileapi.h" #include #include @@ -8,15 +8,16 @@ namespace SHADE { char const FOLDER_MAX_COUNT {15}; - std::unordered_map> SHFileSystem::folders; - FolderPointer SHFileSystem::root {nullptr}; + std::unordered_map> SHFileSystemLibrary::folders; + std::vector SHFileSystemLibrary::files; + FolderPointer SHFileSystemLibrary::root {nullptr}; SHFolder::SHFolder(FolderHandle id, FolderName name) :id{ id }, name{ name }, subFolders(0), folded{ false }, path{""} { } - FolderLocation SHFileSystem::CreateNewFolderHere(FolderName name, FolderLocation here) noexcept + FolderLocation SHFileSystemLibrary::CreateNewFolderHere(FolderName name, FolderLocation here) noexcept { if (here == 0) { @@ -60,7 +61,7 @@ namespace SHADE return location; } - bool SHFileSystem::DeleteFolder(FolderPointer location) noexcept + bool SHFileSystemLibrary::DeleteFolder(FolderPointer location) noexcept { if (!folders.contains(location->id)) { @@ -76,11 +77,12 @@ namespace SHADE return true; } - void SHFileSystem::StartupFillDirectories(FolderPath path) noexcept + void SHFileSystemLibrary::BuildDirectory(FolderPath path) noexcept { std::queue folderQueue; - folderQueue.push(RegisterFolder(path, 0, 0, "Root")); + folderQueue.push(RegisterFolder(path, 0, "Root")); + root = folderQueue.front(); while (!folderQueue.empty()) { @@ -92,41 +94,36 @@ namespace SHADE { if (!dirEntry.is_directory()) { - folder->files.emplace_back( - dirEntry.path().filename().string(), - dirEntry.path().string(), - dirEntry.path().extension().string() - ); + SHFile newFile { + dirEntry.path().stem().string(), + dirEntry.path().extension().string(), + dirEntry.path().string() + }; - continue; + files.push_back(newFile); + folder->files.push_back(&(files.back())); } + else + { + FolderLocation location = folder->id; + location <<= FOLDER_BIT_ALLOCATE; + location |= ++count; - FolderLocation location = folder->id; - location <<= FOLDER_BIT_ALLOCATE; - location |= ++count; - - std::string name = dirEntry.path().string(); - name = name.substr(name.find_last_of('/') + 1, name.length() - name.find_last_of('/')); - - FolderPointer newFolder{ RegisterFolder( - dirEntry.path().string(), - folder->id, - location, - name) - }; - - folderQueue.push(newFolder); - folder->subFolders.push_back(newFolder); + std::string name = dirEntry.path().stem().string(); + FolderPointer newFolder{ RegisterFolder( + dirEntry.path().string(), + location, + name) + }; + + folderQueue.push(newFolder); + folder->subFolders.push_back(newFolder); + } } } } - FolderPointer SHFileSystem::GetRoot() noexcept - { - return root; - } - - FolderPointer SHFileSystem::CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept + FolderPointer SHFileSystemLibrary::CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept { if (!CreateDirectoryA(path.c_str(), nullptr)) @@ -138,21 +135,20 @@ namespace SHADE folders[location]->path = path; folders[parent]->subFolders.push_back(folders[location].get()); - return FolderMakeHelper(path, parent, location, name); + return FolderMakeHelper(path, location, name); } - FolderPointer SHFileSystem::RegisterFolder(FolderPath path, FolderLocation parent, FolderHandle location, + FolderPointer SHFileSystemLibrary::RegisterFolder(FolderPath path, FolderHandle location, FolderName name) noexcept { - return FolderMakeHelper(path, parent, location, name); + return FolderMakeHelper(path, location, name); } - FolderPointer SHFileSystem::FolderMakeHelper(FolderPath path, FolderLocation parent, FolderHandle location, + FolderPointer SHFileSystemLibrary::FolderMakeHelper(FolderPath path, FolderHandle location, FolderName name) noexcept { folders[location] = std::make_unique(location, name); folders[location]->path = path; - folders[parent]->subFolders.push_back(folders[location].get()); return folders[location].get(); } diff --git a/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemLibrary.h b/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemLibrary.h new file mode 100644 index 00000000..281e0984 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/Filesystem/SHFileSystemLibrary.h @@ -0,0 +1,56 @@ +#pragma once + +#include "SHFileSystemDefines.h" +#include "Assets/SHAssetMacros.h" +#include +#include +#include + +namespace SHADE +{ + struct SHFile + { + FileName name; + FileExt ext; + FilePath path; + + AssetID assetRef; + }; + + class SHFolder + { + public: + SHFolder(FolderHandle id, FolderName name); + + FolderHandle id; + FolderName name; + std::vector subFolders; + std::vector files; + + bool folded; + + private: + FolderPath path; + friend class SHFileSystemLibrary; + }; + + class SHFileSystemLibrary + { + public: + static FolderLocation CreateNewFolderHere(FolderName name, FolderLocation here = 0) noexcept; + + static bool DeleteFolder(FolderPointer location) noexcept; + + static void BuildDirectory(FolderPath path) noexcept; + + static FolderPointer root; + static std::unordered_map> folders; + static std::vector files; + + private: + + static FolderPointer CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; + static FolderPointer RegisterFolder(FolderPath path, FolderHandle location, FolderName name) noexcept; + static FolderPointer FolderMakeHelper(FolderPath path, FolderHandle location, FolderName name) noexcept; + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/SHAsset.h b/SHADE_Engine/src/Assets/SHAsset.h index 8d7b55d1..73b8ef9b 100644 --- a/SHADE_Engine/src/Assets/SHAsset.h +++ b/SHADE_Engine/src/Assets/SHAsset.h @@ -11,7 +11,6 @@ *****************************************************************************/ #pragma once -#include "Filesystem/SHFileSystem.h" #include "SHAssetMacros.h" namespace SHADE @@ -22,6 +21,5 @@ namespace SHADE AssetID id; AssetType type; AssetPath path; - FolderLocation location; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index aa9772dd..c3eec668 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -12,7 +12,7 @@ #include #include "SHAssetManager.h" #include "SHAssetMetaHandler.h" -#include "Filesystem/SHFileSystem.h" +#include "Libraries/Filesystem/SHFileSystemLibrary.h" #include "Libraries/SHMeshLoader.h" #include "Libraries/SHTextureLoader.h" @@ -213,8 +213,7 @@ namespace SHADE .name {path.filename().string()}, .id {0}, .type {AssetType::MESH}, - .path {path}, - .location {0} + .path {path} } ); } @@ -226,8 +225,7 @@ namespace SHADE .name {path.filename().string()}, .id {0}, .type {AssetType::DDS}, - .path {path}, - .location {0} + .path {path} } ); } @@ -340,8 +338,7 @@ namespace SHADE mesh.header.meshName, id, AssetType::MESH, - path, - 0 + path ); } } @@ -363,8 +360,7 @@ namespace SHADE image.name, id, AssetType::TEXTURE, - path, - 0 + path ); } } @@ -376,6 +372,13 @@ namespace SHADE { RetrieveAssets(); LoadAllData(); + SHFileSystemLibrary::BuildDirectory(ASSET_ROOT); + auto const& root {SHFileSystemLibrary::root}; + + for (auto const& file : root->files) + { + SHLOG_INFO("{}", file->name); + } } /**************************************************************************** diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h deleted file mode 100644 index 8df794fd..00000000 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.h +++ /dev/null @@ -1,70 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace SHADE -{ - class SHFolder; - - typedef unsigned char FolderCounter; - typedef unsigned char FileCounter; - typedef uint64_t FolderLocation; - typedef uint64_t FolderHandle; - typedef std::string FolderName; - typedef std::string FileName; - typedef std::string FolderPath; - typedef std::string FilePath; - typedef std::string FileExt; - typedef SHFolder* FolderPointer; - - constexpr char FOLDER_BIT_ALLOCATE{ 4 }; - constexpr char FOLDER_MAX_DEPTH{ 16 }; - - struct SHFile - { - FileName name; - FilePath path; - FileExt ext; - }; - - class SHFolder - { - public: - SHFolder(FolderHandle id, FolderName name); - - FolderHandle id; - FolderName name; - std::vector subFolders; - std::vector files; - - bool folded; - - private: - FolderPath path; - friend class SHFileSystem; - }; - - class SHFileSystem - { - public: - static FolderLocation CreateNewFolderHere(FolderName name, FolderLocation here = 0) noexcept; - - static bool DeleteFolder(FolderPointer location) noexcept; - - static void StartupFillDirectories(FolderPath path) noexcept; - - static FolderPointer GetRoot() noexcept; - - private: - static FolderPointer root; - - static std::unordered_map> folders; - - static FolderPointer CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; - static FolderPointer RegisterFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; - static FolderPointer FolderMakeHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; - }; -} \ No newline at end of file