From 9a223098f658cebd2a7abc173e2392d6d3a4a991 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Thu, 8 Sep 2022 11:17:25 +0800 Subject: [PATCH 1/3] Initial Commit --- SHADE_Engine/SHADE_Engine.vcxproj | 2 + SHADE_Engine/SHADE_Engine.vcxproj.filters | 2 + SHADE_Engine/src/Filesystem/SHFileSystem.cpp | 75 ++++++++++++++++++++ SHADE_Engine/src/Filesystem/SHFileSystem.h | 44 ++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 SHADE_Engine/src/Filesystem/SHFileSystem.cpp create mode 100644 SHADE_Engine/src/Filesystem/SHFileSystem.h diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index d89ec12a..ba1be338 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -103,10 +103,12 @@ + + Create diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index b16c713b..6ca01248 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -10,11 +10,13 @@ Engine + Engine + \ No newline at end of file diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp new file mode 100644 index 00000000..81ff6a8b --- /dev/null +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp @@ -0,0 +1,75 @@ +#include "SHFileSystem.h" +#include "fileapi.h" +#include + +namespace SHADE +{ + char const FOLDER_MAX_COUNT {15}; + + std::unordered_map> SHFileSystem::folders; + + SHFolder::SHFolder(FolderHandle id, FolderName name) + :id{id}, name{name}, subFolders(0) + { + } + + FolderLocation SHFileSystem::CreateNewFolderHere(FolderName name, FolderLocation here) noexcept + { + if (here == 0) + { + if (!folders.contains(0)) + { + folders[0] = std::make_unique(0, "root"); + } + + FolderCounter const count = static_cast(folders[here]->subFolders.size()); + + assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); + + FolderHandle const location = static_cast(count); + + CreateFolderHelper(folders[0]->path, here, location, name); + + return location; + } + + assert(folders.contains(here), "Folder creation location does not exist/invalid\n"); + + FolderCounter const count = static_cast(folders[here]->subFolders.size()); + + FolderHandle location = here; + location <<= 4; + location |= count; + + assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); + CreateFolderHelper(folders[0]->path, here, location, name); + + return location; + } + + bool SHFileSystem::DeleteFolder(FolderLocation location) noexcept + { + assert(folders.contains(location), "Delete target does not exist/invalid.\n"); + + for (auto const& subFolder : folders[location]->subFolders) + { + DeleteFolder(subFolder); + } + + RemoveDirectoryA(folders[location]->path.string().c_str()); + return true; + } + + bool SHFileSystem::CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept + { + assert( + CreateDirectoryA(path.string().c_str(), nullptr), + "Failed to create folder\n" + ); + + folders[location] = std::make_unique(location, name); + folders[parent]->subFolders.push_back(location); + + return true; + } +} diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h new file mode 100644 index 00000000..c58eb81e --- /dev/null +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace SHADE +{ + typedef unsigned char FolderCounter; + typedef unsigned char FileCounter; + typedef uint32_t FolderLocation; + typedef uint32_t FolderHandle; + typedef std::string FolderName; + typedef std::filesystem::path FolderPath; + + class SHFolder + { + public: + SHFolder(FolderHandle id, FolderName name); + + FolderHandle id; + FolderName name; + std::vector subFolders; + + private: + FolderPath path; + friend class SHFileSystem; + }; + + class SHFileSystem + { + public: + static FolderLocation CreateNewFolderHere(FolderName name, FolderLocation here = 0) noexcept; + + static bool DeleteFolder(FolderLocation location) noexcept; + + private: + static std::unordered_map> folders; + + static bool CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; + }; +} \ No newline at end of file From 47f716f72b16a85c7c42e817016739de470ce644 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Tue, 13 Sep 2022 11:27:51 +0800 Subject: [PATCH 2/3] SP3-105 SP3-106 Folder tree building done Location bit shift done --- SHADE_Engine/src/Filesystem/SHFileSystem.cpp | 90 ++++++++++++++++---- SHADE_Engine/src/Filesystem/SHFileSystem.h | 26 ++++-- 2 files changed, 94 insertions(+), 22 deletions(-) diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp index 81ff6a8b..5663dadd 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.cpp +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.cpp @@ -1,15 +1,19 @@ +#include "SHpch.h" #include "SHFileSystem.h" #include "fileapi.h" +#include #include +#include namespace SHADE { char const FOLDER_MAX_COUNT {15}; std::unordered_map> SHFileSystem::folders; + FolderPointer SHFileSystem::root {nullptr}; SHFolder::SHFolder(FolderHandle id, FolderName name) - :id{id}, name{name}, subFolders(0) + :id{ id }, name{ name }, subFolders(0), folded{ false }, path{""} { } @@ -22,54 +26,110 @@ namespace SHADE folders[0] = std::make_unique(0, "root"); } - FolderCounter const count = static_cast(folders[here]->subFolders.size()); + auto const count = static_cast(folders[here]->subFolders.size()); assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); - FolderHandle const location = static_cast(count); + auto const location = static_cast(count); - CreateFolderHelper(folders[0]->path, here, location, name); + CreateFolder(folders[0]->path, here, location, name); return location; } assert(folders.contains(here), "Folder creation location does not exist/invalid\n"); - FolderCounter const count = static_cast(folders[here]->subFolders.size()); + auto const count = static_cast(folders[here]->subFolders.size()); FolderHandle location = here; - location <<= 4; + location <<= FOLDER_BIT_ALLOCATE; location |= count; assert(count < FOLDER_MAX_COUNT, "Max subfolders reached\n"); - CreateFolderHelper(folders[0]->path, here, location, name); + CreateFolder(folders[0]->path, here, location, name); return location; } - bool SHFileSystem::DeleteFolder(FolderLocation location) noexcept + bool SHFileSystem::DeleteFolder(FolderPointer location) noexcept { - assert(folders.contains(location), "Delete target does not exist/invalid.\n"); + assert(folders.contains(location->id), "Delete target does not exist/invalid.\n"); - for (auto const& subFolder : folders[location]->subFolders) + for (auto const& subFolder : folders[location->id]->subFolders) { DeleteFolder(subFolder); } - RemoveDirectoryA(folders[location]->path.string().c_str()); + RemoveDirectoryA(folders[location->id]->path.c_str()); return true; } - bool SHFileSystem::CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept + void SHFileSystem::StartupFillDirectories(FolderPath path) noexcept + { + std::queue folderQueue; + + folderQueue.push(RegisterFolder(path, 0, 0, "Root")); + + while (!folderQueue.empty()) + { + auto folder = folderQueue.front(); + folderQueue.pop(); + FolderCounter count = 0; + + for (auto const& dirEntry : std::filesystem::directory_iterator(folder->path)) + { + if (!dirEntry.is_directory()) + { + continue; + } + + 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); + } + } + } + + FolderPointer SHFileSystem::CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept { assert( - CreateDirectoryA(path.string().c_str(), nullptr), + CreateDirectoryA(path.c_str(), nullptr), "Failed to create folder\n" ); folders[location] = std::make_unique(location, name); - folders[parent]->subFolders.push_back(location); + folders[location]->path = path; + folders[parent]->subFolders.push_back(folders[location].get()); - return true; + return FolderMakeHelper(path, parent, location, name); + } + + FolderPointer SHFileSystem::RegisterFolder(FolderPath path, FolderLocation parent, FolderHandle location, + FolderName name) noexcept + { + return FolderMakeHelper(path, parent, location, name); + } + + FolderPointer SHFileSystem::FolderMakeHelper(FolderPath path, FolderLocation parent, 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/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h index c58eb81e..2eff32bd 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.h +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.h @@ -3,17 +3,21 @@ #include #include #include -#include #include namespace SHADE { + class SHFolder; typedef unsigned char FolderCounter; typedef unsigned char FileCounter; - typedef uint32_t FolderLocation; - typedef uint32_t FolderHandle; + typedef uint64_t FolderLocation; + typedef uint64_t FolderHandle; typedef std::string FolderName; - typedef std::filesystem::path FolderPath; + typedef std::string FolderPath; + typedef SHFolder* FolderPointer; + + constexpr char FOLDER_BIT_ALLOCATE{ 4 }; + constexpr char FOLDER_MAX_DEPTH{ 26 }; class SHFolder { @@ -22,7 +26,9 @@ namespace SHADE FolderHandle id; FolderName name; - std::vector subFolders; + std::vector subFolders; + + bool folded; private: FolderPath path; @@ -34,11 +40,17 @@ namespace SHADE public: static FolderLocation CreateNewFolderHere(FolderName name, FolderLocation here = 0) noexcept; - static bool DeleteFolder(FolderLocation location) noexcept; + static bool DeleteFolder(FolderPointer location) noexcept; + + static void StartupFillDirectories(FolderPath path) noexcept; private: + static FolderPointer root; + static std::unordered_map> folders; - static bool CreateFolderHelper(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept; + 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 From b6a6e9b3e5ca4d802f9d45e5b99ff1b81cb94ba1 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Tue, 13 Sep 2022 11:28:16 +0800 Subject: [PATCH 3/3] Updated some const --- SHADE_Engine/src/Filesystem/SHFileSystem.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Filesystem/SHFileSystem.h b/SHADE_Engine/src/Filesystem/SHFileSystem.h index 2eff32bd..9b8b94a2 100644 --- a/SHADE_Engine/src/Filesystem/SHFileSystem.h +++ b/SHADE_Engine/src/Filesystem/SHFileSystem.h @@ -8,6 +8,7 @@ namespace SHADE { class SHFolder; + typedef unsigned char FolderCounter; typedef unsigned char FileCounter; typedef uint64_t FolderLocation; @@ -17,7 +18,7 @@ namespace SHADE typedef SHFolder* FolderPointer; constexpr char FOLDER_BIT_ALLOCATE{ 4 }; - constexpr char FOLDER_MAX_DEPTH{ 26 }; + constexpr char FOLDER_MAX_DEPTH{ 16 }; class SHFolder {