sandbox
Loading...
Searching...
No Matches
asset_registry.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_SCENES_ASSET_REGISTRY_HPP_
3#define LIBSBX_SCENES_ASSET_REGISTRY_HPP_
4
5#include <unordered_map>
6#include <filesystem>
7
8#include <libsbx/utility/hashed_string.hpp>
9#include <libsbx/utility/exception.hpp>
10
11#include <libsbx/math/uuid.hpp>
12
13#include <libsbx/core/engine.hpp>
14
15#include <libsbx/assets/assets_module.hpp>
16
17#include <libsbx/graphics/graphics_module.hpp>
18#include <libsbx/graphics/images/image2d.hpp>
19#include <libsbx/graphics/images/cube_image.hpp>
20
21namespace sbx::scenes {
22
24 std::filesystem::path path;
25 std::string name;
26 std::string type{"unknown"};
27 std::string source{"dynamic"};
28}; // struct asset_metadata
29
30template<typename Handle>
32
33public:
34
35 auto has(const utility::hashed_string& name) const -> bool {
36 return _by_name.contains(name);
37 }
38
39 auto get(const utility::hashed_string& name) const -> const Handle& {
40 if (auto entry = _by_name.find(name); entry != _by_name.end()) {
41 return entry->second;
42 }
43
44 throw utility::runtime_error{"Could not find asset '{}'", name.str()};
45 }
46
47 auto insert(const utility::hashed_string& name, const Handle& handle, asset_metadata metadata) -> bool {
48 if (_by_name.contains(name)) {
49 return false;
50 }
51
52 _by_name.emplace(name, handle);
53 _metadata.emplace(handle, std::move(metadata));
54
55 return true;
56 }
57
58 auto metadata(const Handle& handle) const -> const asset_metadata& {
59 return _metadata.at(handle);
60 }
61
62 auto size() const -> std::size_t {
63 return _by_name.size();
64 }
65
66 auto begin() const {
67 return _metadata.begin();
68 }
69
70 auto end() const {
71 return _metadata.end();
72 }
73
74private:
75
76 std::unordered_map<utility::hashed_string, Handle> _by_name;
77 std::unordered_map<Handle, asset_metadata> _metadata;
78
79}; // class asset_table
80
82
83public:
84
85 template<typename... Args>
86 auto request_image(const utility::hashed_string& name, const std::filesystem::path& path, Args&&... args) -> graphics::image2d_handle {
87 if (_images.has(name)) {
88 return _images.get(name);
89 }
90
91 auto& gfx = core::engine::get_module<graphics::graphics_module>();
92
93 const auto handle = gfx.add_resource<graphics::image2d>(path, std::forward<Args>(args)...);
94
95 _images.insert(name, handle, asset_metadata{path, name.str(), "image", "disk"});
96
97 return handle;
98 }
99
100 auto has_image(const utility::hashed_string& name) const -> bool {
101 return _images.has(name);
102 }
103
104 auto get_image(const utility::hashed_string& name) const -> graphics::image2d_handle {
105 return _images.get(name);
106 }
107
108 auto image_metadata(const graphics::image2d_handle& handle) const -> const asset_metadata& {
109 return _images.metadata(handle);
110 }
111
112 auto images() const -> const asset_table<graphics::image2d_handle>& {
113 return _images;
114 }
115
116 template<typename... Args>
117 auto request_cube_image(const utility::hashed_string& name, const std::filesystem::path& path, Args&&... args) -> graphics::cube_image2d_handle {
118 if (_cube_images.has(name)) {
119 return _cube_images.get(name);
120 }
121
122 auto& gfx = core::engine::get_module<graphics::graphics_module>();
123
124 const auto handle = gfx.add_resource<graphics::cube_image>(path, std::forward<Args>(args)...);
125
126 _cube_images.insert(name, handle, asset_metadata{path, name.str(), "cube_image", "disk"});
127
128 return handle;
129 }
130
131 auto has_cube_image(const utility::hashed_string& name) const -> bool {
132 return _cube_images.has(name);
133 }
134
135 auto get_cube_image(const utility::hashed_string& name) const -> graphics::cube_image2d_handle {
136 return _cube_images.get(name);
137 }
138
139 auto cube_image_metadata(const graphics::cube_image2d_handle& handle) const -> const asset_metadata& {
140 return _cube_images.metadata(handle);
141 }
142
143 auto cube_images() const -> const asset_table<graphics::cube_image2d_handle>& {
144 return _cube_images;
145 }
146
147 template<typename Mesh, typename... Args>
148 auto request_mesh(const utility::hashed_string& name, Args&&... args) -> math::uuid {
149 if (_meshes.has(name)) {
150 return _meshes.get(name);
151 }
152
153 auto& am = core::engine::get_module<assets::assets_module>();
154
155 const auto id = am.add_asset<Mesh>(std::forward<Args>(args)...);
156
157 _meshes.insert(name, id, asset_metadata{"", name.str(), "mesh", "generated"});
158
159 return id;
160 }
161
162 template<typename Mesh, typename Path, typename... Args>
163 requires (std::is_constructible_v<std::filesystem::path, Path>)
164 auto request_mesh(const utility::hashed_string& name, const Path& path, Args&&... args) -> math::uuid {
165 if (_meshes.has(name)) {
166 return _meshes.get(name);
167 }
168
169 auto& am = core::engine::get_module<assets::assets_module>();
170
171 const auto id = am.add_asset<Mesh>(path, std::forward<Args>(args)...);
172
173 _meshes.insert(name, id, asset_metadata{path, name.str(), "mesh", "disk"});
174
175 return id;
176 }
177
178 auto has_mesh(const utility::hashed_string& name) const -> bool {
179 return _meshes.has(name);
180 }
181
182 auto get_mesh(const utility::hashed_string& name) const -> math::uuid {
183 return _meshes.get(name);
184 }
185
186 auto mesh_metadata(const math::uuid& handle) const -> const asset_metadata& {
187 return _meshes.metadata(handle);
188 }
189
190 auto meshes() const -> const asset_table<math::uuid>& {
191 return _meshes;
192 }
193
194 template<typename Animation, typename... Args>
195 auto request_animation(const utility::hashed_string& name, Args&&... args) -> math::uuid {
196 if (_animations.has(name)) {
197 return _animations.get(name);
198 }
199
200 auto& am = core::engine::get_module<assets::assets_module>();
201
202 const auto id = am.add_asset<Animation>(std::forward<Args>(args)...);
203
204 _animations.insert(name, id, asset_metadata{"", name.str(), "animation", "generated"});
205
206 return id;
207 }
208
209 auto has_animation(const utility::hashed_string& name) const -> bool {
210 return _animations.has(name);
211 }
212
213 auto get_animation(const utility::hashed_string& name) const -> math::uuid {
214 return _animations.get(name);
215 }
216
217 auto animations() const -> const asset_table<math::uuid>& {
218 return _animations;
219 }
220
221 template<typename Material, typename... Args>
222 auto request_material(const utility::hashed_string& name, Args&&... args) -> Material& {
223 auto& am = core::engine::get_module<assets::assets_module>();
224
225 if (_materials.has(name)) {
226 return am.get_asset<Material>(_materials.get(name));
227 }
228
229 const auto id = am.add_asset<Material>(std::forward<Args>(args)...);
230
231 _materials.insert(name, id, asset_metadata{"", name.str(), "material", "dynamic"});
232
233 return am.get_asset<Material>(id);
234 }
235
236 auto has_material(const utility::hashed_string& name) const -> bool {
237 return _materials.has(name);
238 }
239
240 auto get_material(const utility::hashed_string& name) const -> math::uuid {
241 return _materials.get(name);
242 }
243
244 auto material_metadata(const math::uuid& handle) const -> const asset_metadata& {
245 return _materials.metadata(handle);
246 }
247
248 auto materials() const -> const asset_table<math::uuid>& {
249 return _materials;
250 }
251
252private:
253
257 asset_table<math::uuid> _animations;
258 asset_table<math::uuid> _materials;
259
260}; // class asset_registry
261
262} // namespace sbx::scenes
263
264#endif // LIBSBX_SCENES_ASSET_REGISTRY_HPP_
Definition: cube_image.hpp:21
Definition: image2d.hpp:52
Definition: resource_storage.hpp:18
Definition: uuid.hpp:160
Definition: asset_registry.hpp:81
Definition: asset_registry.hpp:31
Definition: id.hpp:9
Definition: hashed_string.hpp:17
Definition: asset_registry.hpp:23
Definition: exception.hpp:18