game/src/assets.rs#
Source: lib/game/src/assets.rs
assets -- (G7b part 2) files through the VFS, decoded in the background, behind handles with states, reference
counts and unloading (docs/GAMECORE.md "Assets")
impl Asset for T decode(bytes, files) -> Result<T, String>: a file's bytes into the value; files
reads the files beside it by relative name (a .gltf's buffers and images). It
runs on a worker thread (Send); lib/game_render3d has glTF models and images
Assets::<T>::new(Loading) a store of one kind: Background (plat::task::spawn_blocking: threads where the
target has them), Frames { budget_us } (decoded in update(), one after the other
until the budget is spent, at least one per update: the browser without
threads, deterministic tests)
assets.load(path) -> Handle<T> the asset of path (app://, data://, mem://, a file ...): the same entry for the
same path while it lives (another handle), else a new one, Loading
assets.insert(name, value) an asset made in code, Ready at once
assets.state(&h) / get(&h) / error(&h) / path(&h) / refs(&h) / id()
assets.update() -> Vec<Change> finished decodes become Ready or Failed; assets nobody holds a handle of (for
`linger` updates) are unloaded: their value goes, their path is forgotten;
what happened, for those who keep something per asset (the GPU's copies)
Handle<T> a reference: Clone counts one more, Drop one less; AssetId (index, generation)
names the entry, also after it is gone (a stale id finds nothing)
The simulation does not depend on assets (they are what a frame shows and plays): with threads the moment an
asset becomes Ready depends on the machine. Everything else in lib/game stays deterministic; a test that needs
the same frames on every target loads with Loading::Frames.
trait Asset: Sized + Send + 'statica kind of asset: how a file's bytes become its value (on a worker thread)fn decode(bytes: &[u8], files: &mut dyn FnMut(&str) -> Option<Vec<u8>>) -> Result<Self, String>decodebytes(the file at the handle's path);filesreads a file beside it by a relative name (None: none)
struct Bytes(pub Vec<u8>)the raw bytes of a file (an Asset for anything a game decodes itself)enum Loadinghow decoding runsBackground-- on the blocking pool of plat::task (threads; where none can start, the executor runs the jobs between frames, one per run: lib/ui does)Frames { budget_us: u64 }-- in update(), one asset after the other until budget_us is spent (at least one per update)
impl Loading
fn default_for_target() -> LoadingBackground on targets with threads, Frames { 4 ms } in the browser (a bundle without shared memory has none)enum Statewhere an asset isLoading-- being read and decodedReady-- decoded: get() gives itFailed-- not read or not decoded: error() says why
struct AssetIdan asset's entry: its index and generation (a reused index has a new one)index: u32generation: u32
enum Changewhat update() did to an assetReady(AssetId)Failed(AssetId)Unloaded(AssetId)-- gone: nobody held a handle (its id finds nothing from now on)
struct Handle<T>a reference to an asset of kind T: while one lives the asset stays (Clone: one more, Drop: one less)
impl<T> Handle<T>
fn id(&self) -> AssetIdthe entry it refers tostruct AssetStatswhat a store holds nowloading: u32ready: u32failed: u32decoded: u64-- decodes finished, unloads so farunloaded: u64
struct Assets<T: Asset>the assets of one kind (see the module header)fn sibling(path: &str, rel: &str) -> Option<String>path's folder (with its slash) joined with a relative name; None for one that leaves the folder or names a scheme
impl<T: Asset> Assets<T>
fn new(mode: Loading) -> Assets<T>an empty store that decodes asmodesaysfn mode(&self) -> Loadinghow it decodesfn set_linger(&mut self, updates: u32)an asset nobody holds stays this many updates before it is unloaded (0, the default: the next update unloads it; a load of its path before then takes it again, without reading it anew)fn load(&mut self, path: &str) -> Handle<T>the asset ofpath: another handle of its entry while it lives, else a new entry that starts loadingfn insert(&mut self, name: &str, value: T) -> Handle<T>an asset made in code (no file): Ready at once;nameis its path for path() (it is not looked up by load)fn state(&self, h: &Handle<T>) -> Statewhere the asset is (None: a handle of another store, which cannot happen through the API)fn state_of(&self, id: AssetId) -> Option<State>the state of an entry by its id (None: gone)fn get(&self, h: &Handle<T>) -> Option<&T>the value once Ready (while it is read again by reload: the one so far)fn get_id(&self, id: AssetId) -> Option<&T>the value of an entry by its id (None: not ready, failed or gone)fn error(&self, h: &Handle<T>) -> Option<&str>why it failedfn path(&self, h: &Handle<T>) -> &strthe path it was loaded from (an inserted one: its name)fn refs(&self, h: &Handle<T>) -> u32handles of the asset that live nowfn refs_of(&self, id: AssetId) -> u32handles of an entry that live now (0: gone, or held by nobody)fn len(&self) -> usizeentries (every state)fn is_empty(&self) -> boolfn stats(&self) -> AssetStatswhat the store holdsfn ids(&self) -> Vec<AssetId>the ids of every entry that lives, in index orderfn reload(&mut self, h: &Handle<T>)read and decode the asset again (a file that changed): Loading until then; its value stays until the new one has come: get() gives it meanwhile (a failed reload keeps the old value, without a change, and reports Failed only when there was none)fn update(&mut self) -> Vec<Change>once a frame: decodes that finished (Frames: decoded now, within the budget) become Ready or Failed; assets nobody holds are unloaded -> what happenedfn wait_idle(&mut self, timeout_us: u64, changes: &mut Vec<Change>) -> boolBackground: wait until nothing is loading (at most timeout_us; tests, a loading screen) -> nothing is