cubos::engine::Assets class final

Resource which manages all assets. Responsible for loading and unloading assets, storing them in memory, and providing access to them.

Assets are all identified through Asset handles.

Public types

enum class Status { Unknown, Unloaded, Loading, Loaded, Failed }
Possible statuses for an asset.

Constructors, destructors, conversion operators

Assets()
Constructs an empty manager without any bridges or metadata.

Public functions

void registerBridge(const std::string& extension, std::shared_ptr<AssetBridge> bridge)
Registers a new bridge for the given extension.
void cleanup()
Cleans up all assets that are not in use. Should be called periodically to free up memory.
void importAll(std::string_view path)
Similar to loadMeta, but also creates new .meta files with random UUIDs for files without one.
auto find(std::string_view path) -> AnyAsset
Obtains an asset handle with an UUID from a path.
void loadMeta(std::string_view path)
Loads all metadata from the virtual filesystem, in the given path. If the path points to a directory, it will be recursively searched for metadata files.
void unloadMeta(std::string_view path)
Unloads all asset metadata from assets within the given path.
auto load(AnyAsset handle) const -> AnyAsset
Loads the asset with the given handle, upgrading the handle to a strong one.
auto saveMeta(const AnyAsset& handle) const -> bool
Saves changes made to an asset's metadata.
auto save(const AnyAsset& handle) const -> bool
Saves changes made to the asset with the given handle.
auto readMeta(const AnyAsset& handle) const -> AssetMetaRead
Gets read-only access to the metadata associated with the given handle.
auto writeMeta(const AnyAsset& handle) -> AssetMetaWrite
Gets read-write access to the metadata associated with the given handle.
template<typename T>
auto read(Asset<T> handle) const -> AssetRead<T>
Gets read-only access to the asset data associated with the given handle.
template<typename T>
auto tryRead(Asset<T> handle) const -> core::memory::Opt<AssetRead<T>>
Gets read-only access to the asset data associated with the given handle.
template<typename T>
auto write(Asset<T> handle) -> AssetWrite<T>
Gets read-write access to the asset data associated with the given handle.
auto status(const AnyAsset& handle) const -> Status
Gets the status of the asset with the given handle.
auto update(AnyAsset& handle) const -> bool
Updates the given handle to the latest version of the asset.
template<typename T>
auto update(Asset<T>& handle) const -> bool
Updates the given handle to the latest version of the asset.
void invalidate(const AnyAsset& handle)
Unloads the given asset. Can be used to force assets to be reloaded.
template<typename T>
auto create(T data) -> Asset<T>
Creates a new asset with a random UUID with the given data (and empty metadata).
template<typename T>
auto store(AnyAsset handle, T data) -> AnyAsset
Stores the given asset data in memory, associated with the given handle.
auto listAll() const -> std::vector<AnyAsset>
Gets all assets that have been registered.
auto type(const AnyAsset& handle) const -> const core::reflection::Type&
Gets the type of an asset.
auto hasKnownType(const AnyAsset& handle) const -> bool
Checks if the asset has a known type.

Enum documentation

enum class cubos::engine::Assets::Status

Possible statuses for an asset.

Enumerators
Unknown

No metadata is associated with the asset.

Unloaded

The asset is not loaded.

Loading

The asset is being loaded.

Loaded

The asset is loaded.

Failed

The asset failed to load.

Function documentation

void cubos::engine::Assets::registerBridge(const std::string& extension, std::shared_ptr<AssetBridge> bridge)

Registers a new bridge for the given extension.

Parameters
extension Extension to register the bridge for.
bridge Bridge to register.

If more than one extension match a given asset's name, the longest extension is picked.

void cubos::engine::Assets::importAll(std::string_view path)

Similar to loadMeta, but also creates new .meta files with random UUIDs for files without one.

Parameters
path Path to load metadata from.

AnyAsset cubos::engine::Assets::find(std::string_view path)

Obtains an asset handle with an UUID from a path.

Parameters
path Path to get handle from.
Returns Asset handle with the found UUID, or null if not found.

void cubos::engine::Assets::loadMeta(std::string_view path)

Loads all metadata from the virtual filesystem, in the given path. If the path points to a directory, it will be recursively searched for metadata files.

Parameters
path Path to load metadata from.

void cubos::engine::Assets::unloadMeta(std::string_view path)

Unloads all asset metadata from assets within the given path.

Parameters
path Path to unload metadata from.

If an asset from the given path is currently loaded, it will not be unloaded, but it will be dissociated from its original metadata path.

AnyAsset cubos::engine::Assets::load(AnyAsset handle) const

Loads the asset with the given handle, upgrading the handle to a strong one.

Parameters
handle Handle to load the asset for.
Returns Strong handle to the asset, or a null handle if an error occurred.

This method doesn't block, thus the asset may have not yet been loaded when it returns. If the manager is unable to find the asset or a bridge for loading it, a null handle is returned. If an error occurs while loading the asset, it will only fail in read() or be visible through status().

bool cubos::engine::Assets::saveMeta(const AnyAsset& handle) const

Saves changes made to an asset's metadata.

Parameters
handle Handle identifying the asset to save.
Returns Whether the operation was successful.

This method blocks until the asset is saved.

bool cubos::engine::Assets::save(const AnyAsset& handle) const

Saves changes made to the asset with the given handle.

Parameters
handle Handle identifying the asset to save.
Returns Whether the operation was successful.

This method blocks until the asset is saved.

AssetMetaRead cubos::engine::Assets::readMeta(const AnyAsset& handle) const

Gets read-only access to the metadata associated with the given handle.

Parameters
handle Handle of the asset to get the metadata for.
Returns Reference to the metadata.

Aborts if the asset is unknown.

AssetMetaWrite cubos::engine::Assets::writeMeta(const AnyAsset& handle)

Gets read-write access to the metadata associated with the given handle.

Parameters
handle Handle to get the metadata for.
Returns Reference to the metadata.

If the asset is unknown, an empty metadata object is returned.

template<typename T>
AssetRead<T> cubos::engine::Assets::read(Asset<T> handle) const

Gets read-only access to the asset data associated with the given handle.

Template parameters
T Type of the asset data.
Parameters
handle Handle to get the asset data for.
Returns Reference to the asset data.

If the asset is not loaded, this blocks until it is. If the asset cannot be loaded, abort is called.

template<typename T>
core::memory::Opt<AssetRead<T>> cubos::engine::Assets::tryRead(Asset<T> handle) const

Gets read-only access to the asset data associated with the given handle.

Template parameters
T Type of the asset data.
Parameters
handle Handle to get the asset data for.
Returns Reference to the asset data.

If the asset is not loaded, this blocks until it is. If the asset cannot be loaded, returns a null option.

template<typename T>
AssetWrite<T> cubos::engine::Assets::write(Asset<T> handle)

Gets read-write access to the asset data associated with the given handle.

Template parameters
T Type of the asset data.
Parameters
handle Handle to get the asset data for.
Returns Reference to the asset data.

If the asset is not loaded, this blocks until it is. If the asset cannot be loaded, abort is called. This increases the asset's version.

Status cubos::engine::Assets::status(const AnyAsset& handle) const

Gets the status of the asset with the given handle.

Parameters
handle Handle to check the status for.
Returns Status of the asset.

bool cubos::engine::Assets::update(AnyAsset& handle) const

Updates the given handle to the latest version of the asset.

Parameters
handle Handle to update.
Returns Whether the version was updated.

Can be used to implement hot-reloading.

template<typename T>
bool cubos::engine::Assets::update(Asset<T>& handle) const

Updates the given handle to the latest version of the asset.

Parameters
handle Handle to update.
Returns Whether the version was updated.

Can be used to implement hot-reloading.

void cubos::engine::Assets::invalidate(const AnyAsset& handle)

Unloads the given asset. Can be used to force assets to be reloaded.

Parameters
handle Handle to unload.

template<typename T>
Asset<T> cubos::engine::Assets::create(T data)

Creates a new asset with a random UUID with the given data (and empty metadata).

Template parameters
T Type of the asset data.
Parameters
data Asset data to store.
Returns Strong handle to the new asset.

template<typename T>
AnyAsset cubos::engine::Assets::store(AnyAsset handle, T data)

Stores the given asset data in memory, associated with the given handle.

Template parameters
T Type of the asset data.
Parameters
handle Handle to associate the asset with.
data Asset data to store.
Returns Strong handle to the asset.

If an asset with the same handle already exists, it will be replaced. If no metadata is associated with the handle, an empty one will be created. This increases the asset's version.

std::vector<AnyAsset> cubos::engine::Assets::listAll() const

Gets all assets that have been registered.

Returns Vector with all registered assets.

const core::reflection::Type& cubos::engine::Assets::type(const AnyAsset& handle) const

Gets the type of an asset.

Parameters
handle Handle to check the type for.
Returns Asset type.

If the asset is not loaded, its type is deduced from its bridge. If there's also no associated bridge, aborts. If the asset does not exist, aborts.

bool cubos::engine::Assets::hasKnownType(const AnyAsset& handle) const

Checks if the asset has a known type.

Parameters
handle Handle to check the type for.
Returns wheter the asset has a known type