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
3 changes: 2 additions & 1 deletion src/animation/components/player.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/** @import { PlaybackSettings } from '../core/index.js'*/
/** @import { AssetId } from '../../asset/index.js'*/
import { Handle } from '../../asset/index.js'
import { Playback } from '../core/index.js'
import { AnimationClip } from '../assets/index.js'

export class AnimationPlayer {

/**
* @type {Map<number,Playback>}
* @type {Map<AssetId,Playback>}
*/
animations = new Map()

Expand Down
7 changes: 3 additions & 4 deletions src/animation/systems/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Handle } from '../../asset/index.js'
import { Entity, Query, World } from '../../ecs/index.js'
import { VirtualClock } from '../../time/index.js'
import { AnimationPlayer, AnimationTarget } from '../components/index.js'
Expand Down Expand Up @@ -33,9 +32,9 @@ export function applyAnimations(world) {

const [player] = play

player.animations.forEach((playback, handleid) => {
const handle = new Handle(clips, handleid)
const clip = clips.get(handle)
player.animations.forEach((playback, assetId) => {

const clip = clips.getByAssetId(assetId)
const tracks = clip.getTracks(target.id)

if (!tracks) return
Expand Down
103 changes: 72 additions & 31 deletions src/asset/core/asset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @import {AssetId} from '../types/index.js' */
/** @import {Constructor} from '../../reflect/index.js'*/
import { packInto64Int, unpackFrom64Int } from '../../algorithms/index.js'
import { DenseList } from '../../datastructures/index.js'
import { AssetAdded, AssetDropped, AssetEvent, AssetModified } from '../events/assets.js'

Expand Down Expand Up @@ -43,12 +44,10 @@ export class Assets {
* @returns {Handle<T>}
*/
add(asset) {
const id = this.assets.reserve()

this.assets.set(id, new AssetEntry(asset))

const handle = new Handle(this, id)
const handle = this.reserve()
const entry = this.getEntry(handle)

entry.asset = asset
this.events.push(new AssetAdded(this.type, handle.id()))

return handle
Expand Down Expand Up @@ -79,7 +78,7 @@ export class Assets {
* @param {T} asset
*/
setUsingAssetId(assetId, asset) {
const entry = this.assets.get(assetId)
const entry = this.getEntryByAssetId(assetId)

if (!entry) return

Expand Down Expand Up @@ -120,9 +119,34 @@ export class Assets {
* @returns {AssetEntry<T> | undefined}
*/
getEntry(handle) {
const { index } = handle
const { index, generation } = handle

return this.getEntryInternal(index, generation)
}

/**
* @param {AssetId} assetId
* @returns {AssetEntry<T> | undefined}
*/
getEntryByAssetId(assetId) {
const [index, generation] = unpackFrom64Int(assetId)

return this.assets.get(index)
return this.getEntryInternal(index, generation)
}

/**
* @private
* @param {number} index
* @param {number} generation
*/
getEntryInternal(index, generation) {
const entry = this.assets.get(index)

if (!entry) return undefined

if (entry.generation !== generation) return undefined

return entry
}

/**
Expand Down Expand Up @@ -154,11 +178,11 @@ export class Assets {
* @returns {T | undefined}
*/
getByAssetId(id) {
const entry = this.assets.get(id)
const entry = this.getEntryByAssetId(id)

if (!entry) return undefined

return this.assets.get(id).asset
return entry.asset
}

/**
Expand Down Expand Up @@ -200,19 +224,31 @@ export class Assets {
* @param {AssetId} assetId
*/
upgrade(assetId) {
return new Handle(this, assetId)
const [index, generation] = unpackFrom64Int(assetId)

return new Handle(this, index, generation)
}

/**
* @returns {Handle<T>}
*
*/
reserve() {
const id = this.assets.reserve()
const index = this.assets.reserve()
const entry = this.assets.get(index)

if (entry) {
entry.generation += 1

return new Handle(this, index, entry.generation)
}

this.assets.set(id, new AssetEntry(undefined))
const newEntry = new AssetEntry(undefined)

return new Handle(this, id)
newEntry.generation += 1
this.assets.set(index, newEntry)

return new Handle(this, index, newEntry.generation)
}
}

Expand Down Expand Up @@ -243,16 +279,24 @@ export class Handle {
index

/**
* @param {Assets<T>} assets
* @param {number} index
* @readonly
* @type {number}
*/
generation = 0

/**
* @param {Assets<T>} assets
* @param {number} index
* @param {number} generation
*/
constructor(assets, index) {
constructor(assets, index, generation) {
this.index = index
this.generation = generation
this.assets = assets

const entry = assets.getEntry(this)

if (entry) {
if (entry && entry.generation === generation) {
entry.refCount += 1
}
}
Expand All @@ -261,13 +305,13 @@ export class Handle {
* @returns {AssetId}
*/
id() {
return /** @type {AssetId}*/ (this.index)
return /** @type {AssetId}*/ (packInto64Int(this.index, this.generation))
}

clone() {
const { assets, index } = this
const { assets, index, generation } = this

return new Handle(assets, index)
return new Handle(assets, index, generation)
}

drop() {
Expand All @@ -291,20 +335,17 @@ export class AssetEntry {
/**
* @type {number}
*/
refCount
refCount = 0

/**
* @type {number}
*/
generation = 0

/**
* @param {T} asset
*/
constructor(asset) {
this.asset = asset
this.refCount = 0
}
}

/**
* @template T
* @callback HandleProvider
* @param {number} id
* @returns {Handle<T>}
*/
}
1 change: 0 additions & 1 deletion src/asset/plugins/asset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @import { Constructor } from '../../reflect/index.js' */
/** @import { HandleProvider } from '../core/index.js' */

import { App, AppSchedule, Plugin } from '../../app/index.js'
import { EventPlugin } from '../../event/index.js'
Expand Down
14 changes: 6 additions & 8 deletions src/asset/tests/asset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("Testing `Assets`", () => {
test('`Assets.get` returns undefined on invalid handle', () => {
const assets = new Assets(String)

const handle = new Handle(assets,0)
const handle = new Handle(assets,0, 1)
const actual = assets.get(handle)

strictEqual(actual, undefined)
Expand Down Expand Up @@ -182,7 +182,7 @@ describe("Testing `Assets`", () => {
test('`Assets.getByAssetId` returns undefined on invalid assetid', () => {
const assets = new Assets(String)

const handle = new Handle(assets,0).id()
const handle = new Handle(assets,0, 1).id()
const actual = assets.getByAssetId(handle)

strictEqual(actual, undefined)
Expand Down Expand Up @@ -252,13 +252,11 @@ describe("Testing `Assets`", () => {
test('Assets provides the right events when asset is added/modified using uuid', () => {
const assets = new Assets(String)
const asset = "Wima engine"
const uuid1 = "124-ufy7f-yrffu-67ftcgh"
const uuid2 = "1243f-ufy7f-eggfu-6cghjeha46"

const handle1 = assets.setWithUUID(uuid1,asset)
const handle2 = assets.setWithUUID(uuid2,asset)
assets.setWithUUID(uuid1,asset)
assets.setWithUUID(uuid2,asset)
const handle1 = assets.add(asset)
const handle2 = assets.add(asset)
assets.set(handle1,asset)
assets.set(handle2,asset)
const events = assets.flushEvents()

deepStrictEqual(events[0], new AssetAdded(String, handle1.id()))
Expand Down
38 changes: 38 additions & 0 deletions src/asset/tests/handle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,42 @@ describe('Testing `Handle`',()=>{

deepStrictEqual(actual, asset)
})

test('`Handle` generation starts at one (case 1).', () => {
const assets = new Assets(String)
const asset = "Wima engine"

const handle = assets.add(asset)

deepStrictEqual(handle.generation, 1)
})

test('`Handle` generation starts at one (case 2).', () => {
const assets = new Assets(String)
const asset = "Wima engine"

const handle1 = assets.add(asset)
const handle2 = assets.add(asset)

deepStrictEqual(handle1.generation, 1)
deepStrictEqual(handle2.generation, 1)
})

test('Recycled `Handle`s have incremented generation.', () => {
const assets = new Assets(String)
const asset = "Wima engine"

const handle1 = assets.add(asset)
handle1.drop()
const handle2 = assets.add(asset)
handle2.drop()
const handle3 = assets.add(asset)

deepStrictEqual(handle1.index, 0)
deepStrictEqual(handle1.generation, 1)
deepStrictEqual(handle2.index, 0)
deepStrictEqual(handle2.generation, 2)
deepStrictEqual(handle3.index, 0)
deepStrictEqual(handle3.generation, 3)
})
})