Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/asset/core/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class HandleSnapshot {

/**
* @readonly
* @type {Constructor<T>}
* @type {import('../../type/index.js').TypeId}
*/
type

Expand All @@ -123,7 +123,7 @@ export class HandleSnapshot {
* @param {AssetId | string} asset
*/
constructor(type, asset) {
this.type = type
this.type = typeid(type)
this.asset = asset
}

Expand All @@ -140,10 +140,10 @@ export class HandleSnapshot {
const server = world.getResource(AssetServer)

if (typeof this.asset === 'string') {
return /** @type {Handle<T>} */ (server.load(this.type, this.asset))
return /** @type {Handle<T>} */ (server.loadUntyped(this.type, this.asset))
}

const assets = /** @type {Assets<T>} */ (server.getAssets(typeid(this.type)))
const assets = /** @type {Assets<T>} */ (server.getAssets(this.type))

// TODO: This is inherently incorrect. When scene resources are added,
// the assetid will point to the wrong asset in the scene due to desync between
Expand All @@ -155,3 +155,7 @@ export class HandleSnapshot {
return /** @type {Handle<T>} */ (assets.upgrade(this.asset))
}
}

/**
* @typedef {Handle<unknown>} UntypedHandle
*/
21 changes: 16 additions & 5 deletions src/asset/resources/assetserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,33 @@ export class AssetServer {
*/
load(type, path) {
const typeId = typeid(type)

return /** @type {Handle<T>} */(this.loadUntyped(typeId, path, type.name))
}

/**
* @param {TypeId} typeId
* @param {string} path
* @param {string} [typeName]
* @returns {import('../core/index.js').UntypedHandle}
*/
loadUntyped(typeId, path, typeName) {
const baseUrl = this.basePaths.get(typeId) || ''
const completePath = baseUrl + path
const assets = this.assets.get(typeId)

assert(assets, `No assets registered for the asset type \`${type.name}\` on \`AssetServer\``)
assert(assets, `No assets registered for the asset type \`${typeName || '<unknown>'}\` on \`AssetServer\``)

const assetInfo = this.assetInfos.getByPath(completePath)

if (assetInfo) {

// SAFETY: handle is generated from `Assets` backing `T`
return /** @type {Handle<T>} */ (assets.upgrade(assetInfo.id))
// SAFETY: handle is generated from `Assets` backing typeId
return assets.upgrade(assetInfo.id)
}

// SAFETY: handle is generated from `Assets` backing `T`
const handle = /** @type {Handle<T>} */ (assets.reserve())
// SAFETY: handle is generated from `Assets` backing typeId
const handle = assets.reserve()
const assetId = handle.id()
const newAssetInfo = new AssetInfo(completePath, assetId)

Expand Down
Loading