AssetStorage

class AssetStorage(val asyncContext: CoroutineContext = newSingleThreadAsyncContext(threadName = "AssetStorage-Thread"), val fileResolver: FileHandleResolver = InternalFileHandleResolver(), useDefaultLoaders: Boolean = true, var silenceAssetManagerWarnings: Boolean = false) : Disposable

Asynchronous asset loader based on coroutines API. An AssetManager alternative.

Note that KtxAsync.initiate must be called on the rendering thread before creating an AssetStorage.

asyncContext is used to perform asynchronous file loading. Defaults to a single-threaded context using an AsyncExecutor. See newSingleThreadAsyncContext or ktx.async.newAsyncContext functions to create a custom loading context. Multithreaded contexts are fully supported and might boost loading performance if the assets are loaded asynchronously in parallel.

fileResolver determines how file paths are interpreted. Defaults to InternalFileHandleResolver, which loads internal files.

If useDefaultLoaders is true (which is the default), all default libGDX AssetLoader implementations will be registered. If silenceAssetManagerWarnings is false (which is the default), all non-fatal asset loading issues caused by asset loaders will be logged as errors. It is encouraged not to change this setting to true during the development to avoid potential errors.

Constructors

Link copied to clipboard
constructor(asyncContext: CoroutineContext = newSingleThreadAsyncContext(threadName = "AssetStorage-Thread"), fileResolver: FileHandleResolver = InternalFileHandleResolver(), useDefaultLoaders: Boolean = true, silenceAssetManagerWarnings: Boolean = false)

Properties

Link copied to clipboard
Link copied to clipboard
val fileResolver: FileHandleResolver
Link copied to clipboard
var logger: Logger

libGDX Logger used internally, usually to report issues.

Link copied to clipboard

Allows tracking progress of the loaded assets.

Functions

Link copied to clipboard
suspend fun <T> add(descriptor: AssetDescriptor<T>, asset: T)
inline suspend fun <T> add(path: String, asset: T)
suspend fun <T> add(identifier: Identifier<T>, asset: T)

Adds a fully loaded asset to the storage. Allows to avoid loading the asset with the AssetStorage and to manually add it to storage context.

Link copied to clipboard
operator fun contains(descriptor: AssetDescriptor<*>): Boolean

Checks whether an asset described by descriptor is currently managed by the storage. This will return true for assets that are currently being loaded or

inline operator fun <T> contains(path: String): Boolean

Checks whether an asset in the selected path and T type is currently managed by the storage. This will return true for assets that are currently being loaded or

operator fun contains(identifier: Identifier<*>): Boolean

Checks whether an asset identified by identifier is currently managed by the storage. This will return true for assets that are currently being loaded or

Link copied to clipboard
open override fun dispose()

Unloads all assets. Blocks current thread until are assets are unloaded. Logs all disposing exceptions.

suspend fun dispose(onError: (identifier: Identifier<*>, cause: Throwable) -> Unit)

Unloads all assets. Cancels loading of all scheduled assets. onError will be invoked on every caught disposing exception.

Link copied to clipboard
operator fun <T> get(descriptor: AssetDescriptor<T>): T

Returns a loaded asset of type T described by descriptor or throws MissingAssetException if the asset is not loaded yet or was never scheduled for loading. Rethrows any exceptions encountered during asset loading.

inline operator fun <T> get(path: String): T

Returns a loaded asset of type T loaded from selected path or throws MissingAssetException if the asset is not loaded yet or was never scheduled for loading. Rethrows any exceptions encountered during asset loading.

operator fun <T> get(identifier: Identifier<T>): T

Returns a loaded asset of type T identified by identifier or throws MissingAssetException if the asset is not loaded yet or was never scheduled for loading. Rethrows any exceptions encountered during asset loading.

Link copied to clipboard
inline fun <T> getAssetDescriptor(path: String, parameters: AssetLoaderParameters<T>? = null, fileHandle: FileHandle? = null): AssetDescriptor<T>

Creates a new AssetDescriptor for the selected asset.

Link copied to clipboard

Internal utility method. Returns a list of asset Identifiers associated with the asset path. Do not attempt to modify the returned list. Changing the list might have an unpredictable effect on the asset loaders.

Link copied to clipboard
fun <T> getAsync(descriptor: AssetDescriptor<T>): Deferred<T>

Returns the reference to the asset wrapped with Deferred. Use Deferred.await to obtain the instance. Throws AssetStorageException if the asset was unloaded or never scheduled to begin with.

inline fun <T> getAsync(path: String): Deferred<T>
fun <T> getAsync(identifier: Identifier<T>): Deferred<T>

Returns the reference to the asset wrapped with Deferred. Use Deferred.await to obtain the instance.

Link copied to clipboard

Returns a copy of the list of dependencies of the asset described by descriptor. If the asset is not loaded or has no dependencies, an empty list is returned.

inline fun <T> getDependencies(path: String): List<Identifier<*>>

Returns a copy of the list of dependencies of the asset under path with T type. If the asset is not loaded or has no dependencies, an empty list is returned.

fun getDependencies(identifier: Identifier<*>): List<Identifier<*>>

Returns a copy of the list of dependencies of the asset identified by identifier. If the asset is not loaded or has no dependencies, an empty list is returned.

Link copied to clipboard
inline fun <T> getIdentifier(path: String): Identifier<T>

Creates a new Identifier that allows to uniquely describe an asset by path and class. Uses reified T type to obtain the asset class.

Link copied to clipboard
inline fun <Asset> getLoader(path: String? = null): Loader<Asset>?

Returns the AssetLoader associated with the file. Asset is used to determine the type of the loaded file. path might be necessary to choose the correct loader, as some loaders might be assigned to specific file suffixes or extensions.

fun <Asset> getLoader(type: Class<Asset>, path: String?): Loader<Asset>?

Internal API exposed for inlined method. See inlined getLoader with generics. type is the class of the loaded asset, while path is used to determine if a loader specifically assigned to a file suffix or extension is necessary.

Link copied to clipboard
fun <T> getOrNull(descriptor: AssetDescriptor<T>): T?

Returns a loaded asset of type T described by descriptor or null if the asset is not loaded yet or was never scheduled for loading. Rethrows any exceptions encountered during asset loading.

inline fun <T> getOrNull(path: String): T?

Returns a loaded asset of type T loaded from selected path or null if the asset is not loaded yet or was never scheduled for loading. Rethrows any exceptions encountered during asset loading.

fun <T> getOrNull(identifier: Identifier<T>): T?

Returns a loaded asset of type T identified by identifier or null if the asset is not loaded yet or was never scheduled for loading. Rethrows any exceptions encountered during asset loading.

Link copied to clipboard

Returns the amount of references to the asset described by descriptor.

inline fun <T> getReferenceCount(path: String): Int

Returns the amount of references to the asset under the given path of T type.

fun getReferenceCount(identifier: Identifier<*>): Int

Returns the amount of references to the asset identified by identifier.

Link copied to clipboard
fun isLoaded(descriptor: AssetDescriptor<*>): Boolean

Checks whether an asset described with descriptor is already loaded. Returns false if the asset is not loaded yet, is unloaded or was never loaded to begin with.

inline fun <T> isLoaded(path: String): Boolean

Checks whether an asset in the selected path with T type is already loaded. Returns false if the asset is not loaded yet, is unloaded or was never loaded to begin with.

fun isLoaded(identifier: Identifier<*>): Boolean

Checks whether an asset identified with identifier is already loaded. Returns false if the asset is not loaded yet, is unloaded or was never loaded to begin with.

Link copied to clipboard
suspend fun <T> load(descriptor: AssetDescriptor<T>): T

Schedules loading of an asset of T type described by the descriptor. Suspends the coroutine until an asset is loaded and returns a fully loaded instance of T.

inline suspend fun <T> load(path: String, parameters: AssetLoaderParameters<T>? = null): T

Schedules loading of an asset of T type located at path. Suspends the coroutine until an asset is loaded and returns a fully loaded instance of T.

suspend fun <T> load(identifier: Identifier<T>, parameters: AssetLoaderParameters<T>? = null): T

Schedules loading of an asset with path and type specified by identifier. Suspends the coroutine until an asset is loaded and returns a fully loaded instance of T.

Link copied to clipboard
fun <T> loadAsync(descriptor: AssetDescriptor<T>): Deferred<T>

Schedules loading of an asset of T type described by the descriptor. Suspends the coroutine until an asset is loaded and returns a fully loaded instance of T.

inline fun <T> loadAsync(path: String, parameters: AssetLoaderParameters<T>? = null): Deferred<T>

Schedules asynchronous loading of an asset of T type located at path. Return a Deferred reference which will eventually point to a fully loaded instance of T.

fun <T> loadAsync(identifier: Identifier<T>, parameters: AssetLoaderParameters<T>? = null): Deferred<T>

Schedules loading of an asset with path and type specified by identifier. Suspends the coroutine until an asset is loaded and returns a fully loaded instance of T.

Link copied to clipboard
fun <T> loadSync(descriptor: AssetDescriptor<T>): T

Blocks the current thread until the asset with T type is loaded using the asset descriptor.

inline fun <T> loadSync(path: String, parameters: AssetLoaderParameters<T>? = null): T

Blocks the current thread until the asset with T type is loaded from the given path.

fun <T> loadSync(identifier: Identifier<T>, parameters: AssetLoaderParameters<T>? = null): T

Blocks the current thread until the asset with T type is loaded with data specified by the identifier and optional loading parameters.

Link copied to clipboard

Matches AssetDescriptor pre-processing. Return this String with normalized file separators.

Link copied to clipboard
inline fun <T> setLoader(suffix: String? = null, noinline loaderProvider: () -> Loader<T>)

Associates the AssetLoader with specific asset type determined by T. loaderProvider should create a new instance of loader of the selected types. Optional suffix can be passed if the loader should handle only the files with a specific file name suffix or extension.

fun <T> setLoader(type: Class<T>, suffix: String? = null, loaderProvider: () -> Loader<T>)

Internal API exposed for inlined method. See inlined setLoader with reified generics. Associates the AssetLoader with type under the specified optional suffix.

Link copied to clipboard

Creates a deep copy of the internal asset storage. Returns an AssetStorageSnapshot with the current storage state. Blocks the current thread until the snapshot is complete. If assets are currently being loaded, avoid calling this method from within the rendering thread. For debugging purposes.

Link copied to clipboard

Creates a deep copy of the internal asset storage. Returns an AssetStorageSnapshot with the current storage state. For debugging purposes.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
suspend fun unload(descriptor: AssetDescriptor<*>): Boolean

Removes asset described by the descriptor and all of its dependencies. Does nothing if asset was not loaded in the first place. Will not dispose of the asset if it still is referenced by any other assets. Any removed assets that implement Disposable will be disposed.

inline suspend fun <T> unload(path: String): Boolean

Removes asset loaded with the given path and T type and all of its dependencies. Does nothing if asset was not loaded in the first place. Will not dispose of the asset if it still is referenced by any other assets. Any removed assets that implement Disposable will be disposed.

suspend fun unload(identifier: Identifier<*>): Boolean

Removes asset loaded with the given identifier and all of its dependencies. Does nothing if asset was not loaded in the first place. Will not dispose of the asset if it still is referenced by any other assets. Any removed assets that implement Disposable will be disposed.