JFLib

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 + 'static a 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> decode bytes (the file at the handle's path); files reads 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 Loading how decoding runs

    • Background -- 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() -> Loading Background on targets with threads, Frames { 4 ms } in the browser (a bundle without shared memory has none)

  • enum State where an asset is

    • Loading -- being read and decoded
    • Ready -- decoded: get() gives it
    • Failed -- not read or not decoded: error() says why
  • struct AssetId an asset's entry: its index and generation (a reused index has a new one)

    • index: u32
    • generation: u32
  • enum Change what update() did to an asset

    • Ready(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) -> AssetId the entry it refers to

  • struct AssetStats what a store holds now

    • loading: u32
    • ready: u32
    • failed: u32
    • decoded: u64 -- decodes finished, unloads so far
    • unloaded: 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 as mode says
  • fn mode(&self) -> Loading how it decodes
  • fn 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 of path: another handle of its entry while it lives, else a new entry that starts loading
  • fn insert(&mut self, name: &str, value: T) -> Handle<T> an asset made in code (no file): Ready at once; name is its path for path() (it is not looked up by load)
  • fn state(&self, h: &Handle<T>) -> State where 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 failed
  • fn path(&self, h: &Handle<T>) -> &str the path it was loaded from (an inserted one: its name)
  • fn refs(&self, h: &Handle<T>) -> u32 handles of the asset that live now
  • fn refs_of(&self, id: AssetId) -> u32 handles of an entry that live now (0: gone, or held by nobody)
  • fn len(&self) -> usize entries (every state)
  • fn is_empty(&self) -> bool
  • fn stats(&self) -> AssetStats what the store holds
  • fn ids(&self) -> Vec<AssetId> the ids of every entry that lives, in index order
  • fn 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 happened
  • fn wait_idle(&mut self, timeout_us: u64, changes: &mut Vec<Change>) -> bool Background: wait until nothing is loading (at most timeout_us; tests, a loading screen) -> nothing is

Source: docs/API.md in the JFLib repository.