Unravel Engine C++ Reference
Loading...
Searching...
No Matches
unravel::asset_manager Class Reference

Manages assets, including loading, unloading, and storage. More...

#include <asset_manager.h>

Public Member Functions

 asset_manager (rtti::context &ctx)
 Constructs an asset manager with the given context.
 
 ~asset_manager ()
 Destructs the asset manager.
 
void set_parent (asset_manager *parent)
 Sets the parent asset manager.
 
auto init (rtti::context &ctx) -> bool
 Initializes the asset manager with the given context.
 
auto deinit (rtti::context &ctx) -> bool
 Deinitializes the asset manager with the given context.
 
void unload_all ()
 Unloads all assets.
 
void unload_group (const std::string &group)
 Unloads all assets in a specified group.
 
auto load_database (const std::string &protocol) -> bool
 Loads an asset database from a protocol.
 
void save_database (const std::string &protocol, const fs::path &path)
 Saves the asset database to a specified path.
 
void remove_asset_info_for_path (const fs::path &path)
 Removes asset information for a specified path.
 
void remove_asset_info_for_key (const std::string &key)
 Removes asset information for a specified key.
 
void register_asset_info_for_path (const fs::path &path, bool override)
 
auto add_asset_info_for_path (const fs::path &path, const asset_meta &meta, bool override) -> hpp::uuid
 Adds asset information for a specified path.
 
auto add_asset_info_for_key (const std::string &key, const asset_meta &meta, bool override) -> hpp::uuid
 Adds asset information for a specified key.
 
auto get_metadata (const hpp::uuid &uid) const -> asset_database::meta
 Gets metadata for a resource uid.
 
auto get_metadata_for_path (const fs::path &path) const -> asset_database::meta
 
auto get_metadata_for_key (const std::string &key) const -> asset_database::meta
 
auto generate_metadata (const fs::path &p) const -> asset_meta
 
template<typename HeaderT >
auto get_header (const hpp::uuid &uid) const -> std::shared_ptr< HeaderT >
 Gets typed header info for an asset without loading it.
 
template<typename S , typename... Args>
auto add_storage (Args &&... args) -> asset_storage< S > &
 Adds a storage for a specific type.
 
template<typename T >
auto get_asset (const std::string &key, load_flags flags=load_flags::standard, load_mode mode=load_mode::immediate) -> asset_handle< T >
 Gets an asset by its key.
 
template<typename T >
auto get_asset (const hpp::uuid &uid, load_flags flags=load_flags::standard, load_mode mode=load_mode::immediate) -> asset_handle< T >
 Gets an asset by its UUID.
 
template<typename T >
auto register_asset (const std::string &key) -> asset_handle< T >
 Registers an asset by key without loading it. The actual load is triggered on the first handle.get() call.
 
template<typename T >
auto register_asset (const hpp::uuid &uid) -> asset_handle< T >
 Registers an asset by UUID without loading it. The actual load is triggered on the first handle.get() call.
 
template<typename T >
void preload_assets ()
 Triggers loading on all deferred handles of a given type. Non-blocking: starts background loads but does not wait.
 
void preload_all_assets ()
 Triggers loading on all deferred handles across every registered storage. Non-blocking: starts background loads but does not wait. Useful for game runtime preloading after scene deserialization.
 
template<typename T >
auto find_asset (const std::string &key) const -> const asset_handle< T > &
 Finds an asset by its key.
 
template<typename T >
auto get_asset_from_instance (const std::string &key, std::shared_ptr< T > entry) -> asset_handle< T >
 Gets an asset handle from an instance.
 
template<typename T >
void rename_asset (const std::string &key, const std::string &new_key)
 Renames an asset.
 
template<typename T >
void unload_asset (const std::string &key)
 Unloads an asset by its key.
 
template<typename T >
auto get_assets (const std::string &group={}) const -> std::vector< asset_handle< T > >
 Gets all assets in a specified group.
 
template<typename T , typename F >
auto get_assets_with_predicate (F &&predicate) const -> std::vector< asset_handle< T > >
 Gets all assets that satisfy a predicate.
 
template<typename T , typename F >
void for_each_asset (F &&callback)
 Applies a callback function to each asset.
 
auto get_all_assets (const std::string &group) const -> std::vector< std::string >
 Gets all assets.
 
void evict_unused_assets (const std::string &group, std::chrono::steady_clock::duration max_idle)
 Evicts full-loaded assets not accessed within the given duration. Demotes them back to deferred state so they can be re-loaded on next access.
 
auto add_asset (const std::string &key, bool override=false) -> hpp::uuid
 Adds an asset with a specified key.
 
auto add_asset_for_path (const fs::path &path, bool override=false) -> hpp::uuid
 Adds an asset with a specified path.
 

Detailed Description

Manages assets, including loading, unloading, and storage.

Definition at line 18 of file asset_manager.h.

Constructor & Destructor Documentation

◆ asset_manager()

unravel::asset_manager::asset_manager ( rtti::context & ctx)

Constructs an asset manager with the given context.

Parameters
ctxThe context for the asset manager.

Definition at line 24 of file asset_manager.cpp.

◆ ~asset_manager()

unravel::asset_manager::~asset_manager ( )
default

Destructs the asset manager.

Member Function Documentation

◆ add_asset()

auto unravel::asset_manager::add_asset ( const std::string & key,
bool override = false ) -> hpp::uuid

Adds an asset with a specified key.

Parameters
keyThe key of the asset.
Returns
The UUID of the added asset.

Definition at line 210 of file asset_manager.cpp.

◆ add_asset_for_path()

auto unravel::asset_manager::add_asset_for_path ( const fs::path & path,
bool override = false ) -> hpp::uuid

Adds an asset with a specified path.

Parameters
pathThe path of the asset.
overrideWhether to override the asset if it already exists.
Returns
The UUID of the added asset.

Definition at line 216 of file asset_manager.cpp.

◆ add_asset_info_for_key()

auto unravel::asset_manager::add_asset_info_for_key ( const std::string & key,
const asset_meta & meta,
bool override ) -> hpp::uuid

Adds asset information for a specified key.

Parameters
keyThe key of the asset.
metaThe metadata of the asset.
Returns
The UUID of the added asset.

Definition at line 228 of file asset_manager.cpp.

◆ add_asset_info_for_path()

auto unravel::asset_manager::add_asset_info_for_path ( const fs::path & path,
const asset_meta & meta,
bool override ) -> hpp::uuid

Adds asset information for a specified path.

Parameters
pathThe path of the asset.
metaThe metadata of the asset.
Returns
The UUID of the added asset.

Definition at line 222 of file asset_manager.cpp.

◆ add_storage()

template<typename S , typename... Args>
auto unravel::asset_manager::add_storage ( Args &&... args) -> asset_storage<S>&
inline

Adds a storage for a specific type.

Template Parameters
SThe type of storage.
ArgsThe arguments for constructing the storage.
Parameters
argsThe arguments for constructing the storage.
Returns
A reference to the added storage.

Definition at line 142 of file asset_manager.h.

◆ deinit()

auto unravel::asset_manager::deinit ( rtti::context & ctx) -> bool

Deinitializes the asset manager with the given context.

Parameters
ctxThe context to deinitialize.
Returns
True if deinitialization was successful, false otherwise.

Definition at line 121 of file asset_manager.cpp.

◆ evict_unused_assets()

void unravel::asset_manager::evict_unused_assets ( const std::string & group,
std::chrono::steady_clock::duration max_idle )

Evicts full-loaded assets not accessed within the given duration. Demotes them back to deferred state so they can be re-loaded on next access.

Parameters
groupThe group to evict assets from.
max_idleMaximum idle duration before eviction.

Definition at line 304 of file asset_manager.cpp.

◆ find_asset()

template<typename T >
auto unravel::asset_manager::find_asset ( const std::string & key) const -> const asset_handle<T>&
inline

Finds an asset by its key.

Template Parameters
TThe type of the asset.
Parameters
keyThe key of the asset.
Returns
A constant reference to the handle of the asset.

Definition at line 264 of file asset_manager.h.

◆ for_each_asset()

template<typename T , typename F >
void unravel::asset_manager::for_each_asset ( F && callback)
inline

Applies a callback function to each asset.

Template Parameters
TThe type of the assets.
FThe callback function.
Parameters
callbackThe callback function.

Definition at line 380 of file asset_manager.h.

◆ generate_metadata()

auto unravel::asset_manager::generate_metadata ( const fs::path & p) const -> asset_meta

Definition at line 272 of file asset_manager.cpp.

◆ get_all_assets()

auto unravel::asset_manager::get_all_assets ( const std::string & group) const -> std::vector<std::string>

Gets all assets.

Returns
A vector of handles to the assets.

Definition at line 159 of file asset_manager.cpp.

◆ get_asset() [1/2]

template<typename T >
auto unravel::asset_manager::get_asset ( const hpp::uuid & uid,
load_flags flags = load_flags::standard,
load_mode mode = load_mode::immediate ) -> asset_handle<T>
inline

Gets an asset by its UUID.

Template Parameters
TThe type of the asset.
Parameters
uidThe UUID of the asset.
flagsThe load flags for the asset.
modeWhether to load immediately or defer until first get().
Returns
The handle to the asset.

Definition at line 181 of file asset_manager.h.

◆ get_asset() [2/2]

template<typename T >
auto unravel::asset_manager::get_asset ( const std::string & key,
load_flags flags = load_flags::standard,
load_mode mode = load_mode::immediate ) -> asset_handle<T>
inline

Gets an asset by its key.

Template Parameters
TThe type of the asset.
Parameters
keyThe key of the asset.
flagsThe load flags for the asset.
modeWhether to load immediately or defer until first get().
Returns
The handle to the asset.

Definition at line 159 of file asset_manager.h.

◆ get_asset_from_instance()

template<typename T >
auto unravel::asset_manager::get_asset_from_instance ( const std::string & key,
std::shared_ptr< T > entry ) -> asset_handle<T>
inline

Gets an asset handle from an instance.

Template Parameters
TThe type of the asset.
Parameters
keyThe key of the asset.
entryThe shared pointer to the asset instance.
Returns
The handle to the asset.

Definition at line 278 of file asset_manager.h.

◆ get_assets()

template<typename T >
auto unravel::asset_manager::get_assets ( const std::string & group = {}) const -> std::vector<asset_handle<T>>
inline

Gets all assets in a specified group.

Template Parameters
TThe type of the assets.
Parameters
groupThe group to get assets from.
Returns
A vector of handles to the assets.

Definition at line 345 of file asset_manager.h.

◆ get_assets_with_predicate()

template<typename T , typename F >
auto unravel::asset_manager::get_assets_with_predicate ( F && predicate) const -> std::vector<asset_handle<T>>
inline

Gets all assets that satisfy a predicate.

Template Parameters
TThe type of the assets.
FThe predicate function.
Parameters
predicateThe predicate function.
Returns
A vector of handles to the assets that satisfy the predicate.

Definition at line 367 of file asset_manager.h.

◆ get_header()

template<typename HeaderT >
auto unravel::asset_manager::get_header ( const hpp::uuid & uid) const -> std::shared_ptr<HeaderT>
inline

Gets typed header info for an asset without loading it.

Template Parameters
HeaderTThe header info type (e.g. texture_header_info).
Parameters
uidThe UUID of the asset.
Returns
Shared pointer to the header, or nullptr if not available.

Definition at line 124 of file asset_manager.h.

◆ get_metadata()

auto unravel::asset_manager::get_metadata ( const hpp::uuid & uid) const -> asset_database::meta

Gets metadata for a resource uid.

Parameters
uidThe the uuid of the resource.
Returns
Meta object containing information.

Definition at line 235 of file asset_manager.cpp.

◆ get_metadata_for_key()

auto unravel::asset_manager::get_metadata_for_key ( const std::string & key) const -> asset_database::meta

Definition at line 256 of file asset_manager.cpp.

◆ get_metadata_for_path()

auto unravel::asset_manager::get_metadata_for_path ( const fs::path & path) const -> asset_database::meta

Definition at line 250 of file asset_manager.cpp.

◆ init()

auto unravel::asset_manager::init ( rtti::context & ctx) -> bool

Initializes the asset manager with the given context.

Parameters
ctxThe context to initialize with.
Returns
True if initialization was successful, false otherwise.

Definition at line 35 of file asset_manager.cpp.

◆ load_database()

auto unravel::asset_manager::load_database ( const std::string & protocol) -> bool

Loads an asset database from a protocol.

Parameters
protocolThe protocol to load from.
Returns
True if the database was loaded successfully, false otherwise.

Definition at line 194 of file asset_manager.cpp.

◆ preload_all_assets()

void unravel::asset_manager::preload_all_assets ( )

Triggers loading on all deferred handles across every registered storage. Non-blocking: starts background loads but does not wait. Useful for game runtime preloading after scene deserialization.

Definition at line 312 of file asset_manager.cpp.

◆ preload_assets()

template<typename T >
void unravel::asset_manager::preload_assets ( )
inline

Triggers loading on all deferred handles of a given type. Non-blocking: starts background loads but does not wait.

Template Parameters
TThe type of the assets to preload.

Definition at line 244 of file asset_manager.h.

◆ register_asset() [1/2]

template<typename T >
auto unravel::asset_manager::register_asset ( const hpp::uuid & uid) -> asset_handle<T>
inline

Registers an asset by UUID without loading it. The actual load is triggered on the first handle.get() call.

Template Parameters
TThe type of the asset.
Parameters
uidThe UUID of the asset.
Returns
A deferred handle to the asset.

Definition at line 224 of file asset_manager.h.

◆ register_asset() [2/2]

template<typename T >
auto unravel::asset_manager::register_asset ( const std::string & key) -> asset_handle<T>
inline

Registers an asset by key without loading it. The actual load is triggered on the first handle.get() call.

Template Parameters
TThe type of the asset.
Parameters
keyThe key of the asset.
Returns
A deferred handle to the asset.

Definition at line 207 of file asset_manager.h.

◆ register_asset_info_for_path()

void unravel::asset_manager::register_asset_info_for_path ( const fs::path & path,
bool override )

◆ remove_asset_info_for_key()

void unravel::asset_manager::remove_asset_info_for_key ( const std::string & key)

Removes asset information for a specified key.

Parameters
keyThe key of the asset.

Definition at line 297 of file asset_manager.cpp.

◆ remove_asset_info_for_path()

void unravel::asset_manager::remove_asset_info_for_path ( const fs::path & path)

Removes asset information for a specified path.

Parameters
pathThe path of the asset.

Definition at line 291 of file asset_manager.cpp.

◆ rename_asset()

template<typename T >
void unravel::asset_manager::rename_asset ( const std::string & key,
const std::string & new_key )
inline

Renames an asset.

Template Parameters
TThe type of the asset.
Parameters
keyThe current key of the asset.
new_keyThe new key for the asset.

Definition at line 295 of file asset_manager.h.

◆ save_database()

void unravel::asset_manager::save_database ( const std::string & protocol,
const fs::path & path )

Saves the asset database to a specified path.

Parameters
protocolThe protocol to use for saving.
pathThe path to save the database to.

Definition at line 203 of file asset_manager.cpp.

◆ set_parent()

void unravel::asset_manager::set_parent ( asset_manager * parent)

Sets the parent asset manager.

Parameters
parentThe parent asset manager.

Definition at line 30 of file asset_manager.cpp.

◆ unload_all()

void unravel::asset_manager::unload_all ( )

Unloads all assets.

Definition at line 132 of file asset_manager.cpp.

◆ unload_asset()

template<typename T >
void unravel::asset_manager::unload_asset ( const std::string & key)
inline

Unloads an asset by its key.

Template Parameters
TThe type of the asset.
Parameters
keyThe key of the asset.

Definition at line 327 of file asset_manager.h.

◆ unload_group()

void unravel::asset_manager::unload_group ( const std::string & group)

Unloads all assets in a specified group.

Parameters
groupThe group to unload.

Definition at line 146 of file asset_manager.cpp.


The documentation for this class was generated from the following files: