7#include <hpp/event.hpp>
13#include <unordered_map>
186 std::shared_ptr<asset_header_info>
header;
226 std::lock_guard<std::mutex> lock(asset_mutex_);
235 std::lock_guard<std::mutex> lock(asset_mutex_);
265 std::lock_guard<std::mutex> lock(asset_mutex_);
267 auto existing = find_entry_by_location_unlocked(location);
268 if(existing != asset_meta_.end())
272 return existing->first;
278 existing->second.location = location;
279 existing->second.meta =
meta;
280 existing->second.
meta.
uid = existing->first;
281 return existing->first;
284 auto& metainfo = asset_meta_[
meta.uid];
285 metainfo.location = location;
286 metainfo.meta =
meta;
296 auto get_uuid(
const std::string& location)
const ->
const hpp::uuid&
298 std::lock_guard<std::mutex> lock(asset_mutex_);
300 auto it = find_entry_by_location_unlocked(location);
301 if(it != asset_meta_.end())
306 static const hpp::uuid uid;
312 std::lock_guard<std::mutex> lock(asset_mutex_);
314 auto it = find_entry_by_location_unlocked(location);
315 if(it != asset_meta_.end())
320 static const meta empty;
331 std::lock_guard<std::mutex> lock(asset_mutex_);
333 auto it = asset_meta_.find(
id);
334 if(it == asset_meta_.end())
336 static const meta empty;
350 std::lock_guard<std::mutex> lock(asset_mutex_);
351 for(
auto& kvp : asset_meta_)
353 auto& uid = kvp.first;
354 auto& metainfo = kvp.second;
355 if(metainfo.location == key)
357 APPLOG_TRACE(
"{}::{} - {} -> {}", __func__, hpp::to_string(uid), key, new_key);
359 metainfo.location = new_key;
370 std::lock_guard<std::mutex> lock(asset_mutex_);
371 for(
auto& kvp : asset_meta_)
373 auto& uid = kvp.first;
374 auto& metainfo = kvp.second;
375 if(metainfo.location == key)
377 APPLOG_TRACE(
"{}::{} - {}", __func__, hpp::to_string(uid), key);
379 asset_meta_.erase(uid);
393 auto find_entry_by_location_unlocked(
const std::string& location) -> database_t::iterator
395 for(
auto it = asset_meta_.begin(); it != asset_meta_.end(); ++it)
397 if(it->second.location == location)
402 return asset_meta_.end();
405 auto find_entry_by_location_unlocked(
const std::string& location)
const -> database_t::const_iterator
407 for(
auto it = asset_meta_.cbegin(); it != asset_meta_.cend(); ++it)
409 if(it->second.location == location)
414 return asset_meta_.cend();
418 mutable std::mutex asset_mutex_{};
442 virtual void unload_single(tpp::thread_pool& pool,
const std::string& key) = 0;
449 virtual void unload_group(tpp::thread_pool& pool,
const std::string& group) = 0;
457 virtual void evict_unused(tpp::thread_pool& pool,
const std::string& group, std::chrono::steady_clock::duration max_idle) = 0;
496 pool.stop(
handle.task_id());
510 if(predicate(it->second))
512 auto&
handle = it->second;
541 void unload_group(tpp::thread_pool& pool,
const std::string& group)
final
546 const auto&
id = it.id();
547 hpp::string_view id_view(
id);
548 return id_view.starts_with(group);
562 const auto&
id = it.id();
575 std::vector<asset_handle<T>> result;
581 if(predicate(kvp.second))
583 result.emplace_back(kvp.second);
595 auto get_group(
const std::string& group)
const -> std::vector<asset_handle<T>>
600 const auto&
id = it.id();
601 hpp::string_view id_view(
id);
602 return id_view.starts_with(group);
613 void evict_unused(tpp::thread_pool& pool,
const std::string& group, std::chrono::steady_clock::duration max_idle)
final
615 auto now = std::chrono::steady_clock::now();
619 const auto&
id =
handle.id();
620 hpp::string_view id_view(
id);
621 if(!id_view.starts_with(group))
629 bool is_idle = (now -
handle.last_access()) > max_idle;
634 pool.stop(
handle.task_id());
635 handle.demote_to_deferred();
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.
auto get_metadata(const std::string &location) const -> const meta &
void remove_all()
Removes all assets from the database.
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(...)
load_mode
Controls whether an asset is loaded immediately or deferred until first access.
@ deferred
Register handle only; load triggered on first .get() call.
Thread-safe handle to an asset.
Manages storage and loading of assets of a specific type.
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.
callable< bool(tpp::thread_pool &pool, asset_handle< T > &, const std::string &, load_mode)> load_from_file_t
Function type for loading from file.
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.
void evict_unused(tpp::thread_pool &pool, const std::string &group, std::chrono::steady_clock::duration max_idle) final
Evicts loaded assets not accessed within the given duration. Assets whose weak_ptr has expired (no ex...
std::unordered_map< std::string, asset_handle< T > > request_container_t
Container for asset requests.
std::recursive_mutex container_mutex
Mutex for container operations.
void preload_all() final
Triggers loading on all deferred handles in this storage. Non-blocking: starts background loads but d...
~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 void preload_all()=0
Triggers loading on all deferred handles in this storage. Non-blocking: starts background loads but d...
virtual ~basic_storage()=default
virtual void unload_group(tpp::thread_pool &pool, const std::string &group)=0
Unloads all assets in a specified group.
virtual void evict_unused(tpp::thread_pool &pool, const std::string &group, std::chrono::steady_clock::duration max_idle)=0
Evicts loaded assets not accessed within the given duration. Demotes them back to deferred state so m...
compression_quality compression
struct unravel::texture_importer_meta::quality_meta quality
bool invert_normal_y
Bake OpenGL/DirectX normal Y difference into texels at compile (normal_map type only).