Unravel Engine C++ Reference
Loading...
Searching...
No Matches
asset_storage.h
Go to the documentation of this file.
1#pragma once
2
3#include "asset_handle.h"
4
5#include <context/context.hpp>
6#include <hpp/event.hpp>
7
8#include <cassert>
9#include <functional>
10#include <map>
11#include <mutex>
12#include <unordered_map>
13
15
16namespace unravel
17{
18
19struct asset_importer_meta : crtp_meta_type<asset_importer_meta>
20{
21 virtual ~asset_importer_meta() = default;
22};
23
70
71struct mesh_importer_meta : crtp_meta_type<mesh_importer_meta, asset_importer_meta>
72{
74 {
75 bool import_meshes{true};
76 bool weld_vertices{true};
77 bool optimize_meshes{true};
79 bool find_degenerates{true};
82
83 struct rig_meta
84 {
85
86 } rig;
87
89 {
91
93
100};
101
102struct animation_importer_meta : crtp_meta_type<animation_importer_meta, asset_importer_meta>
103{
104
114};
115
116struct audio_importer_meta : crtp_meta_type<audio_importer_meta, asset_importer_meta>
117{
118 bool force_to_mono{false};
119};
120
126{
128 hpp::uuid uid{};
130 std::string type{};
132 std::shared_ptr<asset_importer_meta> importer;
133};
134
140{
141public:
146 struct meta
147 {
149 std::string location{};
152 };
153
155 using database_t = std::map<hpp::uuid, meta>;
156
162 static auto generate_id(const fs::path& p) -> hpp::uuid
163 {
164 return generate_uuid();//p.generic_string());
165 }
166
171 auto get_database() const -> const database_t&
172 {
173 return asset_meta_;
174 }
175
180 void set_database(const database_t& rhs)
181 {
182 std::lock_guard<std::mutex> lock(asset_mutex_);
183 asset_meta_ = rhs;
184 }
185
190 {
191 std::lock_guard<std::mutex> lock(asset_mutex_);
192 asset_meta_.clear();
193 }
194
201 auto add_asset(const std::string& location, const asset_meta& meta, bool override) -> hpp::uuid
202 {
203 const auto& uid = get_uuid(location);
204 if(!override && !uid.is_nil())
205 {
206 return uid;
207 }
208
209 std::lock_guard<std::mutex> lock(asset_mutex_);
210
211 auto& metainfo = asset_meta_[meta.uid];
212 metainfo.location = location;
213 metainfo.meta = meta;
214 // Keep original uid so that we dont break any links
215 if(!uid.is_nil())
216 {
217 metainfo.meta.uid = uid;
218 }
219 else
220 {
221 APPLOG_TRACE("{} - {} -> {}", __func__, hpp::to_string(metainfo.meta.uid), location);
222 }
223
224 return metainfo.meta.uid;
225 }
226
232 auto get_uuid(const std::string& location) const -> const hpp::uuid&
233 {
234 std::lock_guard<std::mutex> lock(asset_mutex_);
235
236 for(const auto& kvp : asset_meta_)
237 {
238 const auto& uid = kvp.first;
239 const auto& metainfo = kvp.second;
240 if(metainfo.location == location)
241 {
242 return uid;
243 }
244 }
245
246 static const hpp::uuid uid;
247 return uid;
248 }
249
255 auto get_metadata(const hpp::uuid& id) const -> const meta&
256 {
257 std::lock_guard<std::mutex> lock(asset_mutex_);
258
259 auto it = asset_meta_.find(id);
260 if(it == asset_meta_.end())
261 {
262 static const meta empty;
263 return empty;
264 }
265
266 return it->second;
267 }
268
274 void rename_asset(const std::string& key, const std::string& new_key)
275 {
276 std::lock_guard<std::mutex> lock(asset_mutex_);
277 for(auto& kvp : asset_meta_)
278 {
279 auto& uid = kvp.first;
280 auto& metainfo = kvp.second;
281 if(metainfo.location == key)
282 {
283 APPLOG_TRACE("{}::{} - {} -> {}", __func__, hpp::to_string(uid), key, new_key);
284
285 metainfo.location = new_key;
286 }
287 }
288 }
289
294 void remove_asset(const std::string& key)
295 {
296 std::lock_guard<std::mutex> lock(asset_mutex_);
297 for(auto& kvp : asset_meta_)
298 {
299 auto& uid = kvp.first;
300 auto& metainfo = kvp.second;
301 if(metainfo.location == key)
302 {
303 APPLOG_TRACE("{}::{} - {}", __func__, hpp::to_string(uid), key);
304
305 asset_meta_.erase(uid);
306 return;
307 }
308 }
309 }
310
311private:
313 mutable std::mutex asset_mutex_{};
315 database_t asset_meta_{};
316};
317
323{
324 virtual ~basic_storage() = default;
325
330 virtual void unload_all(tpp::thread_pool& pool) = 0;
331
337 virtual void unload_single(tpp::thread_pool& pool, const std::string& key) = 0;
338
344 virtual void unload_group(tpp::thread_pool& pool, const std::string& group) = 0;
345};
346
352template<typename T>
354{
356 using request_container_t = std::unordered_map<std::string, asset_handle<T>>;
358 template<typename F>
359 using callable = std::function<F>;
360
362 using load_from_file_t = callable<bool(tpp::thread_pool& pool, asset_handle<T>&, const std::string&)>;
363
365 using predicate_t = callable<bool(const asset_handle<T>&)>;
366 using load_from_instance_t = callable<bool(tpp::thread_pool& pool, asset_handle<T>&, std::shared_ptr<T>)>;
367
368 ~asset_storage() override = default;
369
375 void unload_handle(tpp::thread_pool& pool, asset_handle<T>& handle)
376 {
377 pool.stop(handle.task_id());
378 handle.invalidate();
379 }
380
386 void unload_with_condition(tpp::thread_pool& pool, const predicate_t& predicate)
387 {
388 std::lock_guard<std::recursive_mutex> lock(container_mutex);
389 for(auto it = container.begin(); it != container.end();)
390 {
391 if(predicate(it->second))
392 {
393 auto& handle = it->second;
394 unload_handle(pool, handle);
395 it = container.erase(it);
396 }
397 else
398 {
399 ++it;
400 }
401 }
402 }
403
408 void unload_all(tpp::thread_pool& pool) final
409 {
411 [](const auto& it)
412 {
413 return true;
414 });
415 }
416
422 void unload_group(tpp::thread_pool& pool, const std::string& group) final
423 {
425 [&](const auto& it)
426 {
427 const auto& id = it.id();
428 hpp::string_view id_view(id);
429 return id_view.starts_with(group);
430 });
431 }
432
438 void unload_single(tpp::thread_pool& pool, const std::string& key) final
439 {
441 [&](const auto& it)
442 {
443 const auto& id = it.id();
444 return id == key;
445 });
446 }
447
453 auto get_with_condition(const predicate_t& predicate) const -> std::vector<asset_handle<T>>
454 {
455 std::lock_guard<std::recursive_mutex> lock(container_mutex);
456 std::vector<asset_handle<T>> result;
457 result.reserve(container.size() + 1);
458 result.emplace_back(asset_handle<T>::get_empty());
459
460 for(const auto& kvp : container)
461 {
462 if(predicate(kvp.second))
463 {
464 result.emplace_back(kvp.second);
465 }
466 }
467
468 return result;
469 }
470
476 auto get_group(const std::string& group) const -> std::vector<asset_handle<T>>
477 {
478 return get_with_condition(
479 [&](const auto& it)
480 {
481 const auto& id = it.id();
482 hpp::string_view id_view(id);
483 return id_view.starts_with(group);
484 });
485 }
486
494 mutable std::recursive_mutex container_mutex;
495};
496
497} // namespace unravel
Manages asset metadata and provides functionality for adding, removing, and querying assets.
void remove_asset(const std::string &key)
Removes an asset from the database.
void rename_asset(const std::string &key, const std::string &new_key)
Renames an asset.
void set_database(const database_t &rhs)
Sets the asset database.
auto get_metadata(const hpp::uuid &id) const -> const meta &
Gets the metadata of an asset based on its UUID.
void remove_all()
Removes all assets from the database.
static auto generate_id(const fs::path &p) -> hpp::uuid
Generates a UUID based on the file path.
auto get_database() const -> const database_t &
Gets the entire asset database.
std::map< hpp::uuid, meta > database_t
Type definition for the asset database.
auto get_uuid(const std::string &location) const -> const hpp::uuid &
Gets the UUID of an asset based on its location.
auto add_asset(const std::string &location, const asset_meta &meta, bool override) -> hpp::uuid
Adds an asset to the database.
#define APPLOG_TRACE(...)
Definition logging.h:17
auto generate_uuid() -> hpp::uuid
Definition uuid.cpp:25
Represents a handle to an asset, providing access and management functions.
struct unravel::animation_importer_meta::root_motion_meta root_motion
Metadata information for an asset including its location.
asset_meta meta
Metadata of the asset.
std::string location
Location of the asset.
virtual ~asset_importer_meta()=default
Metadata for an asset, including its UUID and type.
std::shared_ptr< asset_importer_meta > importer
Importer meta.
std::string type
Type of the asset.
hpp::uuid uid
Unique identifier for the asset.
Manages storage and loading of assets of a specific type.
callable< bool(tpp::thread_pool &pool, asset_handle< T > &, const std::string &)> load_from_file_t
Function type for loading from file.
void unload_with_condition(tpp::thread_pool &pool, const predicate_t &predicate)
Unloads assets that satisfy a condition.
load_from_instance_t load_from_instance
Function for loading assets from instance.
auto get_group(const std::string &group) const -> std::vector< asset_handle< T > >
Gets all assets in a specified group.
void unload_all(tpp::thread_pool &pool) final
Unloads all assets.
callable< bool(tpp::thread_pool &pool, asset_handle< T > &, std::shared_ptr< T >)> load_from_instance_t
callable< bool(const asset_handle< T > &)> predicate_t
Function type for loading from instance. Predicate function type.
void unload_handle(tpp::thread_pool &pool, asset_handle< T > &handle)
Unloads a handle.
std::function< F > callable
Type alias for callable functions.
std::unordered_map< std::string, asset_handle< T > > request_container_t
Container for asset requests.
std::recursive_mutex container_mutex
Mutex for container operations.
~asset_storage() override=default
void unload_group(tpp::thread_pool &pool, const std::string &group) final
Unloads all assets in a specified group.
load_from_file_t load_from_file
Function for loading assets from file.
auto get_with_condition(const predicate_t &predicate) const -> std::vector< asset_handle< T > >
Gets assets that satisfy a condition.
void unload_single(tpp::thread_pool &pool, const std::string &key) final
Unloads a single asset by its key.
request_container_t container
Container for asset requests.
Abstract base class for asset storage.
virtual void unload_single(tpp::thread_pool &pool, const std::string &key)=0
Unloads a single asset by its key.
virtual void unload_all(tpp::thread_pool &pool)=0
Unloads all assets.
virtual ~basic_storage()=default
virtual void unload_group(tpp::thread_pool &pool, const std::string &group)=0
Unloads all assets in a specified group.
struct unravel::mesh_importer_meta::rig_meta rig
struct unravel::mesh_importer_meta::model_meta model
struct unravel::mesh_importer_meta::animations_meta animations
struct unravel::mesh_importer_meta::materials_meta materials
struct unravel::texture_importer_meta::quality_meta quality
gfx::uniform_handle handle
Definition uniform.cpp:9