Hubris Engine Dev
A Project to learn and get into Game Engine developement.
 
Loading...
Searching...
No Matches
ResourceManager.h
Go to the documentation of this file.
1#pragma once
2#include <filesystem>
3
4
5namespace Hubris::IO {
6 class IOBuffer {
7 private:
8 const void* buffer = nullptr;
9 size_t buffersize = 0;
10
11 public:
12 IOBuffer()noexcept = default;
13 IOBuffer(const void* buffer, const size_t buffersize)noexcept {
14 this->buffer = buffer;
15 this->buffersize = buffersize;
16 }
18 delete buffer;
19 buffersize = 0;
20 buffer = nullptr;
21 }
22 inline const size_t& size()const noexcept { return buffersize; };
23 inline const void* get_raw()const noexcept { return buffer; };
24 };
25
27 public:
28 virtual ~ExtensionHandler() = 0;
29
30 virtual IOBuffer& Read(const std::string& Path, bool RelativePath = true) = 0;
31 virtual void Write(const void* Buffer, const size_t Buf_Size) = 0;
32 };
33 struct Extension {
37 const char* extPostfix;
39 };
40
41
42 class Asset {
43
44 };
45
46
47 template<typename T>
48 concept AssetHandlerConcept = requires(const std::string & path, std::shared_ptr<Asset> asset) {
49
50 typename T::AssetType;
51 // Must have Load(path)
52 { T::Load(path) } -> std::same_as<std::shared_ptr<typename T::AssetType>>;
53
54 // Must have Save(asset, path)
55 { T::Save(asset, path) } -> std::same_as<void>;
56
57 // Must have Import(path)
58 { T::Import(path) } -> std::same_as<std::shared_ptr<typename T::AssetType>>;
59
60 // Must have GetHandledType()
61 { T::GetHandledType() } -> std::same_as<std::string>;
62 };
63
65 private:
66 typedef std::unordered_map<std::string, ExtensionHandler*> ExtensionHandlerMap;
67 static inline ExtensionHandlerMap HandlerMap = ExtensionHandlerMap(25);
68
69
70 static std::vector<std::string> ParseExtensions(const std::string& extPostfix) {
71 std::stringstream ss(extPostfix);
72 std::string extension;
73 std::vector<std::string> extensions;
74
75 while (std::getline(ss, extension, '|')) {
76 extension.erase(0, extension.find_first_not_of(" \t\n\r\f\v"));
77 extension.erase(extension.find_last_not_of(" \t\n\r\f\v") + 1);
78 extensions.push_back(extension);
79 }
80 return extensions;
81 }
82 public:
83 static void SetExtensionHandler(const Extension& ext) {
84 //Parse The postfix. | is used to seperate between file extension handled.
85 std::stringstream ss(ext.extPostfix);
86 std::string extension;
87
88 while (std::getline(ss, extension, '|')) {
89 // Remove any leading or trailing whitespace
90 extension.erase(0, extension.find_first_not_of(" \t\n\r\f\v"));
91 extension.erase(extension.find_last_not_of(" \t\n\r\f\v") + 1);
92
93 // Add the extension and its handler to the map
94 HandlerMap[extension] = ext.Handler;
95 }
96 }
97
98 static IOBuffer& ReadFile(const std::string& path) {
99 size_t dotPos = path.find_last_of('.');
100 if (dotPos == std::string::npos) {
101 // No extension found
102 static IOBuffer NULLBUF(nullptr, 0);
103 return NULLBUF;
104 }
105 auto& handler = HandlerMap[path.substr(dotPos + 1)];
106
107 return handler->Read(path);
108 }
109
110
111
112 };
113
119 static std::vector<char> readFile(const std::string& filename) {
120 std::ifstream file(filename, std::ios::ate | std::ios::binary);
121
122 if (!file.is_open()) {
123 throw std::runtime_error("failed to open file!");
124 }
125
126 size_t fileSize = (size_t) file.tellg();
127 std::vector<char> buffer(fileSize);
128
129 file.seekg(0);
130 file.read(buffer.data(), fileSize);
131
132 file.close();
133 return buffer;
134 }
135
136}
virtual void Write(const void *Buffer, const size_t Buf_Size)=0
virtual IOBuffer & Read(const std::string &Path, bool RelativePath=true)=0
IOBuffer() noexcept=default
const void * get_raw() const noexcept
const size_t & size() const noexcept
static IOBuffer & ReadFile(const std::string &path)
static void SetExtensionHandler(const Extension &ext)
ExtensionHandler * Handler
const char * extPostfix
Use | to seperate between handled file extensions.