Skip to main content

AssetLoader

Trait AssetLoader 

Source
pub trait AssetLoader {
    // Required methods
    fn reset_base(&mut self);
    fn set_base(&mut self, src: &str);
    async fn read(&mut self, paths: &[String]) -> Vec<Option<Vec<u8>>>;
    async fn supply_texture(&mut self, key: u32, encoded: &[u8]);
    fn has_texture(&self, key: u32) -> bool;
    fn warn(&self, message: &str);

    // Provided methods
    async fn read_pack(&mut self, _name: &str) -> Option<Vec<u8>> { ... }
    fn texture_key(&self, _path: &str, cellmap_hash: u32) -> u32 { ... }
}
Expand description

The three things the loading walk cannot do for itself: reach a file, decode an image, and know what the renderer already holds.

This is the whole of the platform-specific part of loading. Everything else — reading both texture tables, deduplicating by file, walking instance references transitively — is the same everywhere and lives in SSPlayer::load_with, so a face cannot quietly implement half of it.

Two implementations ship: the filesystem behind SSPlayer::load, and the web face’s (fetch + createImageBitmap, whose pixels reach the GPU without passing through wasm memory at all). An engine integration writes a third and gets the walk for free — which it must, because an exported Godot project keeps its resources in a .pck and a Unity one in an APK, where no File::open will reach them.

async because a browser leaves no choice; a native implementation simply does not await anything, and its caller blocks on the future.

Required Methods§

Source

fn reset_base(&mut self)

Go back to the root you were built with, discarding whatever base a previous load left behind.

SSPlayer::load_with calls this first, before it reads anything. It is what lets one loader serve more than one load: without it the base set by the last load is still in force when the next src is read, and a second load_with(loader, "overall/Other.ssab") looks in overall/overall. An engine adapter holding a single loader over its AssetServer / ResourceLoader / .pck handle is the shape that needs this; nothing else about the walk changes.

Keep the root the caller gave you, and restore it here — do not try to undo AssetLoader::set_base by walking back up, which cannot be right for a URL and is not always right for a path.

Source

fn set_base(&mut self, src: &str)

Re-root yourself at the directory src turned out to live in: every path the walk asks for after this is named relative to that.

src is the pack’s own path — not its directory. Resolving it, and deciding what “its directory” means, is the whole reason this method exists: only the implementation knows whether it is holding a filesystem path, a URL, or an archive key, and each of those cuts a directory differently. Path::parent and a URL base already know their own vocabulary; the walk, serving all three at once, did not.

Resolve src against the base you were constructed with, then take its directory, so a loader that starts out rooted somewhere (a Godot res:// prefix, an archive subtree) stays inside it.

Derived from the root you were built with, not folded into the base a previous call left behind, so that this is idempotent and pairs with AssetLoader::reset_base: two loads through one loader land on the same base the two would have reached separately.

SSPlayer::load_with calls this exactly once per load, after reading the pack and before asking for anything else, so src is read while the base is still whatever the caller set up and can never be resolved against itself. SSPlayer::load_assets does not call it at all — a caller reaching for that one is expected to have rooted the loader itself.

Source

async fn read(&mut self, paths: &[String]) -> Vec<Option<Vec<u8>>>

Read every path — all of them at once, so an implementation that can overlap the waiting does (the web one issues one fetch per path and settles them together). Results are positional; None is “this one did not arrive”, which is never fatal to the rest.

Every path is relative to the base — bare, never joined onto anything by the caller. The one exception is the first call of a SSPlayer::load_with, which is src itself, verbatim, before AssetLoader::set_base has been called.

Source

async fn supply_texture(&mut self, key: u32, encoded: &[u8])

Decode encoded (a PNG, as the exporter wrote it) and register it under key — what AssetLoader::texture_key answered for it, and what the player will translate a draw batch’s cellmap hash into.

Source

fn has_texture(&self, key: u32) -> bool

Whether an atlas is already registered for key — a hint, used to skip reading a file nobody will need. key is AssetLoader::texture_key’s answer, not a cellmap hash.

Best effort: a loader that cannot see the renderer answers false and lets the flush make the real decision. The texture map is device-scoped, so the authoritative check is the one that keeps N players of one pack to one decode and one upload between them.

Source

fn warn(&self, message: &str)

Report a file that could not be loaded. One missing atlas or sub-pack costs you that one thing, not the character, so this is a diagnostic rather than an error.

Provided Methods§

Source

async fn read_pack(&mut self, _name: &str) -> Option<Vec<u8>>

Bytes for an instance-referenced pack, by the name it is referenced under rather than by a path.

Tried before <the .ssab's directory>/<name>.ssab, and returning None — the default — falls back to exactly that. Implement it where a pack is addressable by name but not by path: an archive keyed by pack name, an engine’s asset server, bytes a drop handler is already holding.

The alternative is to register the pack with SSPlayer::add_ssab before loading — the walk skips a pack it already has — but that means knowing which packs a .ssab references before reading it. This is for letting the walk ask instead.

Called once per name, not batched: a provider is normally a lookup in something already in memory, and the batching exists for the reads that actually wait.

Source

fn texture_key(&self, _path: &str, cellmap_hash: u32) -> u32

Which renderer key path’s image belongs under.

A cellmap hash names a slot inside one .ssab, not an image. Some tools name the atlas spritemap1.png in every project they export, so two packs from two projects ask for one hash and mean two different atlases; keyed by the hash alone, the second registration is skipped as “already held” and that pack silently draws the first one’s atlas. Resolving path the way AssetLoader::read would and hashing that is what tells them apart, and it is the implementation that knows how — a path, a URL and an archive key each resolve differently, which is the same reason AssetLoader::set_base exists.

Equal keys must mean the same pixels: that is what keeps the sibling packs of one project — all naming the one atlas beside them — to a single upload. Answering differently for two names of one file costs an upload, which is the harmless direction; answering alike for two files draws the wrong picture.

The default answers cellmap_hash, which is enough for a loader that never serves two projects to one renderer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§