Class
AssetServer
View sourceWorld resource for typed asset pools and path-based asset I/O.
AssetServerPlugin provides this resource. Register each asset
family with a typed Assets pool and register matching
Importer and Exporter converters. The server selects a
converter by asset type and exact path extension, records load metadata, and
queues load and save requests for later processing.
New load metadata starts in LoadState.Loading. Later processing
stores a successfully imported value and changes its state to
LoadState.Loaded, or changes it to LoadState.Failed when
loading or importing fails.
Examples
import { AssetServer } from '@wimaengine/asset'
function queueTextureLoad(world, Texture) {
const server = world.getResource(AssetServer)
const handle = server.load(Texture, '/textures/player.png')
return handle
}
// The queued request is processed after this call returns.Constructor
Methods
staticdefault(): AssetServer
View sourceregisterAsset<T>(assets: Assets<T>): void
View sourceRegisters a typed Assets pool with this server.
Registers the pool using its type constructor as the runtime type ID. The pool is available for loading and saving that asset type after registration. Registering another pool for the same type replaces the previously selected pool.
Examples
import { AssetServer, Assets } from '@wimaengine/asset'
class Texture {}
const server = new AssetServer()
const textures = new Assets(Texture)
server.registerAsset(textures)
// `textures` is now the pool used for `Texture` loads and saves.registerImporter<T>(type: Constructor<T>, importer: Importer<T>): void
View sourceRegisters an Importer for its asset type and supported file
extensions.
The type argument does not determine selection. The server uses the
constructor in Importer#asset and each exact label returned by
Importer#getExtensions. Keep type aligned with that
constructor for a consistent registration API. Labels must match the
extension extracted from a load path exactly; no normalization is applied.
Registering an existing type-and-extension pair warns and selects the new
importer.
Examples
import { AssetServer, Importer } from '@wimaengine/asset'
class Texture {}
class TextureImporter extends Importer {
constructor() { super(Texture) }
getExtensions() { return ['png'] }
}
const server = new AssetServer()
server.registerImporter(Texture, new TextureImporter())
// A load path ending in `.png` can now select this importer for `Texture`.registerExporter<T>(type: Constructor<T>, exporter: Exporter<T>): void
View sourceRegisters an Exporter for its asset type and supported file
extensions.
The type argument does not determine selection. The server uses the
constructor in Exporter#asset and each exact label returned by
Exporter#getExtensions. Keep type aligned with that constructor
for a consistent registration API. Labels must match the extension
extracted from a save path exactly; no normalization is applied.
Registering an existing type-and-extension pair warns and selects the new
exporter.
Examples
import { AssetServer, Exporter } from '@wimaengine/asset'
class Texture {}
class TextureExporter extends Exporter {
constructor() { super(Texture) }
getExtensions() { return ['png'] }
}
const server = new AssetServer()
server.registerExporter(Texture, new TextureExporter())
// A save path ending in `.png` can now select this exporter for `Texture`.load<T>(type: Constructor<T>, path: string): Handle<T>
View sourceReserves a typed asset handle and queues a load request for a path.
The server requires an Assets pool registered for type and
throws if that pool is missing. If a type-specific base path is present,
it is prefixed to path by direct string concatenation. The resulting
complete path is used for metadata lookup and the queued request; path
separators are not normalized.
For a new complete path, the pool reserves an asset ID and the returned
Handle owns its first reference. A later call for the same
complete path retains the recorded ID through Assets#upgrade and
returns another handle without queuing another request. If that ID is
stale because its pool slot was recycled, the upgrade returns undefined,
which this method also returns.
Fetching and importing happen during later asset processing. Register a
matching Importer before processing begins.
Path metadata is matched by complete path rather than by type. The normal plugin setup therefore expects a path to identify one asset family.
Examples
import { AssetServer, Assets } from '@wimaengine/asset'
class Texture {}
const server = new AssetServer()
const textures = new Assets(Texture)
server.registerAsset(textures)
const path = '/textures/player.png'
const handle = server.load(Texture, path)
const assetId = handle.id()loadUntyped(typeId: TypeId, path: string, typeName?: string): UntypedHandle
View sourceQueues a load request using an already computed asset type ID.
AssetServer#load uses this form after deriving the type ID. The
optional typeName affects only the missing-pool error message. The path
is combined with the server’s type-specific base path before metadata
lookup. If metadata already exists for that complete path, the registered
pool upgrades its asset ID; otherwise this method reserves an ID, records
metadata in the initial LoadState.Loading state, and queues a
load request.
save<T>(handle: Handle<T>, path?: string): void
View sourceQueues an asset for saving to a path.
The request stores the handle’s type ID and packed Handle#id.
When path is supplied, that exact non-empty string is used. The server
does not prepend a base path or normalize it. When path is omitted, the
server uses the path recorded for that asset ID by AssetServer#load.
If no target path is available, the server queues a failed save request
using '<unknown>' as its path and returns without throwing.
This method does not verify that the handle still resolves in its typed
Assets pool, or look up an Exporter, serialize, or
send the asset immediately. Saving is queued for later asset processing.
During that processing, a stale or missing asset is reported as a saving
AssetLoadFail event.
Examples
import { AssetServer, Assets } from '@wimaengine/asset'
class Texture {}
const server = new AssetServer()
const textures = new Assets(Texture)
server.registerAsset(textures)
const handle = textures.add(new Texture())
const path = '/exports/player.json'
server.save(handle, path)
// A registered exporter processes the queued request later.getImporter<T>(typeId: TypeId, path: string): Importer<T>
View sourceReturns the registered Importer for a type ID and path-derived
extension.
The extension is the text after the final ., or the entire path string
when no . is present; a path ending in . produces an empty string. The
result is not lowercased, trimmed, or otherwise normalized. The server
matches it exactly against the extensions returned by
Importer#getExtensions and the supplied typeId.
Examples
import { AssetServer, Importer } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'
class Texture {}
class TextureImporter extends Importer {
constructor() { super(Texture) }
getExtensions() { return ['png'] }
}
const server = new AssetServer()
server.registerImporter(Texture, new TextureImporter())
const importer = server.getImporter(typeid(Texture), '/textures/player.png')getExporter<T>(typeId: TypeId, path: string): Exporter<T>
View sourceReturns the registered Exporter for a type ID and path-derived
extension.
The extension is the text after the final ., or the entire path string
when no . is present; a path ending in . produces an empty string. The
result is not lowercased, trimmed, or otherwise normalized. The server
matches it exactly against the extensions returned by
Exporter#getExtensions and the supplied typeId.
Examples
import { AssetServer, Exporter } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'
class Texture {}
class TextureExporter extends Exporter {
constructor() { super(Texture) }
getExtensions() { return ['png'] }
}
const server = new AssetServer()
server.registerExporter(Texture, new TextureExporter())
const exporter = server.getExporter(typeid(Texture), '/textures/player.png')getAssets(typeId: TypeId): Assets<unknown>
View sourceReturns the Assets pool registered under an exact runtime type
ID.
Use typeid with the same constructor used by
AssetServer#registerAsset when starting from an
asset constructor. No pool is created or inferred here; an unregistered
type ID returns undefined.
Examples
import { AssetServer, Assets } from '@wimaengine/asset'
import { typeid } from '@wimaengine/type'
class Texture {}
const server = new AssetServer()
const textures = new Assets(Texture)
server.registerAsset(textures)
const registered = server.getAssets(typeid(Texture))getAssetInfo<T>(handle: Handle<T>): AssetInfo
View sourceReturns metadata associated with a handle’s packed
Handle#id.
The lookup does not query the handle’s Assets pool or validate
its type, slot, or generation. A stale handle can therefore still return
metadata until AssetServer#dropAssetInfo removes
that ID. If the ID has no metadata, the method returns undefined.
Examples
import { AssetServer, Assets } from '@wimaengine/asset'
class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
const handle = server.load(Texture, '/textures/player.png')
const info = server.getAssetInfo(handle)getAssetInfoByAssetId(assetId: AssetId): AssetInfo
View sourceReturns metadata associated with an exact packed AssetId.
The lookup uses only assetId; it does not accept or validate an asset
type and does not query a typed Assets pool. Use
AssetServer#getAssetInfo when a
Handle is available. If the ID has no metadata, the method
returns undefined.
getAssetInfoByPath(path: string): AssetInfo
View sourceReturns the metadata indexed by an exact recorded path.
The key is the complete path stored when
AssetServer#load queued the request. This method does not
derive a path from a handle,
normalize the string, prepend a base path, or search by asset type. Pass
the same complete path used by the request; an unindexed path returns
undefined.
dropAssetInfo(id: AssetId): void
View sourceRemoves the metadata associated with an asset ID.
This deletes the ID and path metadata for the asset. The handle, typed
Assets pool, queued requests, and pool reference count remain
unchanged. A later load can therefore create fresh metadata for the same
path. If id has no metadata, the method does nothing.
Examples
import { AssetServer, Assets } from '@wimaengine/asset'
class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
const handle = server.load(Texture, '/textures/player.png')
server.dropAssetInfo(handle.id())
// The handle and asset pool are unchanged; only server metadata is gone.flushLoadRequests(): AssetLoadRequest[]
View sourceTakes the pending load requests as one FIFO batch and starts a fresh queue.
The returned array is the exact queue instance that was pending when this
method was called, so its entries retain the order in which
AssetServer#load queued them. The server replaces its
pending batch before returning; requests queued afterwards are therefore
collected separately. This method does not fetch, import, retry, or
otherwise process the returned requests.
Examples
import { AssetServer, Assets } from '@wimaengine/asset'
class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
server.load(Texture, '/textures/player.png')
const firstBatch = server.flushLoadRequests()
server.load(Texture, '/textures/enemy.png')
const secondBatch = server.flushLoadRequests()
// Each batch preserves enqueue order and belongs to its own queue.
console.log(firstBatch[0].path, secondBatch[0].path)flushSaveRequests(): AssetSaveRequest[]
View sourceTakes the pending save requests as one FIFO batch and starts a fresh queue.
The returned array is the exact queue instance that was pending when this
method was called, so its entries retain the order in which
AssetServer#save queued them. The server replaces its
pending batch before returning; requests queued afterwards are therefore
collected separately. This method does not serialize, send, retry, or
otherwise process the returned requests.
setLoadState(info: AssetInfo, state: number): void
View sourceReplaces the load state stored in an asset metadata entry.
This method only assigns state to info.loadstate. Changing the
metadata here does not fetch, enqueue, import, serialize, retry, or
otherwise process the asset. The value is stored without validation, so
use one of the constants from LoadState.
Examples
import { AssetServer, Assets, LoadState } from '@wimaengine/asset'
class Texture {}
const server = new AssetServer()
server.registerAsset(new Assets(Texture))
const handle = server.load(Texture, '/textures/player.png')
const info = server.getAssetInfo(handle)
if (info) server.setLoadState(info, LoadState.Failed)
// Only the metadata changes; no fetch or retry is started.