Filesystem rework to go through asset folder, build directory and populate folders with files

This commit is contained in:
Xiao Qi 2022-10-20 18:43:09 +08:00
parent f5060ba0b0
commit 6f20b98ce1
6 changed files with 135 additions and 121 deletions

View File

@ -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 <string>
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 };
}

View File

@ -1,5 +1,5 @@
#include "SHpch.h" #include "SHpch.h"
#include "SHFileSystem.h" #include "SHFileSystemLibrary.h"
#include "fileapi.h" #include "fileapi.h"
#include <filesystem> #include <filesystem>
#include <queue> #include <queue>
@ -8,15 +8,16 @@ namespace SHADE
{ {
char const FOLDER_MAX_COUNT {15}; char const FOLDER_MAX_COUNT {15};
std::unordered_map<FolderLocation, std::unique_ptr<SHFolder>> SHFileSystem::folders; std::unordered_map<FolderLocation, std::unique_ptr<SHFolder>> SHFileSystemLibrary::folders;
FolderPointer SHFileSystem::root {nullptr}; std::vector<SHFile> SHFileSystemLibrary::files;
FolderPointer SHFileSystemLibrary::root {nullptr};
SHFolder::SHFolder(FolderHandle id, FolderName name) SHFolder::SHFolder(FolderHandle id, FolderName name)
:id{ id }, name{ name }, subFolders(0), folded{ false }, path{""} :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) if (here == 0)
{ {
@ -60,7 +61,7 @@ namespace SHADE
return location; return location;
} }
bool SHFileSystem::DeleteFolder(FolderPointer location) noexcept bool SHFileSystemLibrary::DeleteFolder(FolderPointer location) noexcept
{ {
if (!folders.contains(location->id)) if (!folders.contains(location->id))
{ {
@ -76,11 +77,12 @@ namespace SHADE
return true; return true;
} }
void SHFileSystem::StartupFillDirectories(FolderPath path) noexcept void SHFileSystemLibrary::BuildDirectory(FolderPath path) noexcept
{ {
std::queue<FolderPointer> folderQueue; std::queue<FolderPointer> folderQueue;
folderQueue.push(RegisterFolder(path, 0, 0, "Root")); folderQueue.push(RegisterFolder(path, 0, "Root"));
root = folderQueue.front();
while (!folderQueue.empty()) while (!folderQueue.empty())
{ {
@ -92,41 +94,36 @@ namespace SHADE
{ {
if (!dirEntry.is_directory()) if (!dirEntry.is_directory())
{ {
folder->files.emplace_back( SHFile newFile {
dirEntry.path().filename().string(), dirEntry.path().stem().string(),
dirEntry.path().string(), dirEntry.path().extension().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; std::string name = dirEntry.path().stem().string();
location <<= FOLDER_BIT_ALLOCATE; FolderPointer newFolder{ RegisterFolder(
location |= ++count; dirEntry.path().string(),
location,
std::string name = dirEntry.path().string(); name)
name = name.substr(name.find_last_of('/') + 1, name.length() - name.find_last_of('/')); };
FolderPointer newFolder{ RegisterFolder( folderQueue.push(newFolder);
dirEntry.path().string(), folder->subFolders.push_back(newFolder);
folder->id, }
location,
name)
};
folderQueue.push(newFolder);
folder->subFolders.push_back(newFolder);
} }
} }
} }
FolderPointer SHFileSystem::GetRoot() noexcept FolderPointer SHFileSystemLibrary::CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept
{
return root;
}
FolderPointer SHFileSystem::CreateFolder(FolderPath path, FolderLocation parent, FolderHandle location, FolderName name) noexcept
{ {
if (!CreateDirectoryA(path.c_str(), nullptr)) if (!CreateDirectoryA(path.c_str(), nullptr))
@ -138,21 +135,20 @@ namespace SHADE
folders[location]->path = path; folders[location]->path = path;
folders[parent]->subFolders.push_back(folders[location].get()); 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 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 FolderName name) noexcept
{ {
folders[location] = std::make_unique<SHFolder>(location, name); folders[location] = std::make_unique<SHFolder>(location, name);
folders[location]->path = path; folders[location]->path = path;
folders[parent]->subFolders.push_back(folders[location].get());
return folders[location].get(); return folders[location].get();
} }

View File

@ -0,0 +1,56 @@
#pragma once
#include "SHFileSystemDefines.h"
#include "Assets/SHAssetMacros.h"
#include <vector>
#include <memory>
#include <unordered_map>
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<FolderPointer> subFolders;
std::vector<SHFile const*> 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<FolderLocation, std::unique_ptr<SHFolder>> folders;
static std::vector<SHFile> 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;
};
}

View File

@ -11,7 +11,6 @@
*****************************************************************************/ *****************************************************************************/
#pragma once #pragma once
#include "Filesystem/SHFileSystem.h"
#include "SHAssetMacros.h" #include "SHAssetMacros.h"
namespace SHADE namespace SHADE
@ -22,6 +21,5 @@ namespace SHADE
AssetID id; AssetID id;
AssetType type; AssetType type;
AssetPath path; AssetPath path;
FolderLocation location;
}; };
} }

View File

@ -12,7 +12,7 @@
#include <chrono> #include <chrono>
#include "SHAssetManager.h" #include "SHAssetManager.h"
#include "SHAssetMetaHandler.h" #include "SHAssetMetaHandler.h"
#include "Filesystem/SHFileSystem.h" #include "Libraries/Filesystem/SHFileSystemLibrary.h"
#include "Libraries/SHMeshLoader.h" #include "Libraries/SHMeshLoader.h"
#include "Libraries/SHTextureLoader.h" #include "Libraries/SHTextureLoader.h"
@ -213,8 +213,7 @@ namespace SHADE
.name {path.filename().string()}, .name {path.filename().string()},
.id {0}, .id {0},
.type {AssetType::MESH}, .type {AssetType::MESH},
.path {path}, .path {path}
.location {0}
} }
); );
} }
@ -226,8 +225,7 @@ namespace SHADE
.name {path.filename().string()}, .name {path.filename().string()},
.id {0}, .id {0},
.type {AssetType::DDS}, .type {AssetType::DDS},
.path {path}, .path {path}
.location {0}
} }
); );
} }
@ -340,8 +338,7 @@ namespace SHADE
mesh.header.meshName, mesh.header.meshName,
id, id,
AssetType::MESH, AssetType::MESH,
path, path
0
); );
} }
} }
@ -363,8 +360,7 @@ namespace SHADE
image.name, image.name,
id, id,
AssetType::TEXTURE, AssetType::TEXTURE,
path, path
0
); );
} }
} }
@ -376,6 +372,13 @@ namespace SHADE
{ {
RetrieveAssets(); RetrieveAssets();
LoadAllData(); LoadAllData();
SHFileSystemLibrary::BuildDirectory(ASSET_ROOT);
auto const& root {SHFileSystemLibrary::root};
for (auto const& file : root->files)
{
SHLOG_INFO("{}", file->name);
}
} }
/**************************************************************************** /****************************************************************************

View File

@ -1,70 +0,0 @@
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <unordered_map>
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<FolderPointer> subFolders;
std::vector<SHFile> 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<FolderLocation, std::unique_ptr<SHFolder>> 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;
};
}