Hazel

Asset Packing (rpak)

An exported game carries its content in one .rpak archive: an indexed file with per-entry compression, so the game can read one asset without decompressing the rest. The format, the packer and the reader are crates/renzora_rpak. Which files go in is decided by a crawl from the project's entry points, or by the Export window's Files tab.

Format (version 2)

[ Header, 32 bytes ]
  magic              "RPAK"
  version            u32    = 2
  flags              u32    bit 0: the index is zstd-compressed
  index_offset       u64    from the start of the rpak
  index_compressed   u32    index size as stored
  index_uncompressed u32    index size after decompression
  reserved           4 bytes

[ Data, from offset 32 ]
  Entry payloads back to back, each Stored or Zstd on its own.

[ Index, at index_offset ]
  count u32, then per entry:
    path_len u32, path (UTF-8), offset u64, compressed_size u64,
    uncompressed_size u64, compression u8 (0 Stored, 1 Zstd),
    entry_flags u8, padding u16, crc32 u32 (0: not computed)

[ Footer, 16 bytes, only when appended to a binary ]
  rpak_total_size u64   header through end of index
  magic           "RPAK"
  reserved        u32

All integers are little-endian. Offsets are relative to the start of the rpak, not of the file, so the same archive reads correctly standalone or appended to a binary.

Detection

A reader checks, in order:

  1. The last 16 bytes. If bytes 8..12 are RPAK, the archive is appended to a binary and starts at file_size - 16 - rpak_total_size.
  2. The first 8 bytes. RPAK followed by version 2 is a standalone archive starting at 0.
  3. Anything else is not an rpak.

The footer is what makes the single-binary modes work: the executable is unchanged at the front, the OS loads it as usual, and the game finds its own archive by reading its own tail. detect_appended_footer and FOOTER_LEN are exported for anything else that needs the same check.

Compression

Each entry is Stored or Zstd, chosen per entry:

  • Formats that are already compressed are stored without trying: png, jpg, jpeg, webp, gif, ogg, mp3, flac, rmip, ktx2, basis, dds, svgz, zst, zip. Compressing them again costs CPU (a lot at high levels) for no gain.
  • Everything else is compressed, and stored instead if zstd did not make it smaller.
  • The index is compressed the same way: only if that is smaller, which header flag bit 0 records.

Entries are compressed in parallel with rayon. The level is the Export window's compression setting (default 3).

On the web, decompression uses zrip, a pure-Rust zstd decoder: the zstd crate builds C code that has no libc to compile against for wasm32-unknown-unknown. The web runtime only reads archives; packing is desktop-only.

Writing: RpakPacker

use renzora_rpak::RpakPacker;

let mut packer = RpakPacker::new();
packer.add_file("scenes/main.bsn", bytes);
packer.add_from_disk(project_dir, &project_dir.join("textures/wall.png"))?;

packer.write_to_file(Path::new("game.rpak"), 3)?;      // standalone
// or
packer.append_to_binary(runtime_exe, output_exe, 3)?;   // runtime + rpak + footer
MethodDoes
add_file(path, data)Adds an entry under a normalized key
add_from_disk(base, file)Reads a file and keys it relative to base
get, len, is_emptyInspect what has been added
finish(level)Returns the archive bytes, without a footer
write_to_file(path, level)Writes a standalone archive
append_to_binary(bin, out, level)Writes bin followed by the archive and the footer
optimize_meshes[_with_progress]Runs a mesh optimizer over every model entry
generate_mesh_lods[_with_progress]Adds generated LODs for every model entry
strip_for_runtime, strip_for_serverSee Stripping

Entries live in a BTreeMap, so the archive is ordered by path, and adding a path twice keeps the last.

Keys are normalized: backslashes become /, and empty and . segments are removed. This matters because the crawl reads paths out of scene text. A path written on Windows as scripts\foo is escaped in RON as scripts\\foo, which the scan turns into scripts//foo, while the runtime looks up scripts/foo. Both sides normalizing to the same key keeps such assets loadable.

What goes in: the crawl

pack_project_with_progress(project_dir, allowed_extensions, on_progress) packs only files that are reachable from the project's entry points. It never packs a folder wholesale, so a source .blend, a reference image or a leftover test scene does not ship.

Always packed:

  • project.toml (with its [editor] section removed, so editor preferences do not ship) and project.ron if present.

Entry points:

  • The main_scene and icon named in project.toml.
  • Every languages/*.toml: the runtime loads all of them by folder, and no file names one by path.
  • The project's UI themes: a theme is chosen by name, and a player may switch to any of them.
  • Every .rs script, wherever it lives (dot-directories skipped). A Rust script is compiled into the game, not packed, so it is scanned in place: the assets it names are packed, the source is not. Without this, a script that loads a texture by name would ship and the texture would not.

From each packed text file, a breadth-first walk follows every quoted string that looks like an asset path (it ends in a known asset extension). Several file kinds need help to be read:

FileWhat is followed
.glbThe JSON chunk is extracted, so external textures and .bin buffers are found
.rvgThe model's scene JSON inside the compressed file, and the .material files it binds
.glb / .gltfThe sibling .rvg (virtual geometry), which nothing names: the renderer swaps the extension
Scenesterrain_clusters/<id> folders: every chunk .rvg inside is packed
.svgSingle-quoted attributes too (href='a.png'), since vector editors write them
.svgzGunzipped first (capped at 64 MiB), then read as SVG

Sidecars that nothing names are carried with their file: <shader>.wgsl.meta beside an old compiled material, and <image>.svg.toml with an SVG's rasterization settings.

A reference is tried as written from the project root, and relative to the folder of the file that contains it (a model at models/foo/scene.glb naming textures/a.png). .. is collapsed before the key is formed, and a reference that climbs out of the project yields nothing, so the walk can never pack a file from outside it.

Other entry points

FunctionUse
pack_project(dir, allowed)The crawl without a progress callback
referenced_keys(dir)The keys the crawl would pack, without keeping bytes. The Files tab shows this as its default selection; it runs the real crawl, so the two cannot disagree
pack_selected_with_progress(dir, keys, cb)Exactly the given keys: what the export uses once the Files tab has been edited
pack_project_filtered(dir, extensions)The crawl restricted to some extensions: the server archive

Stripping

strip_for_runtime runs on every export. It drops .camera.ron files (the editor's saved viewport cameras), and removes components whose type path starts with an editor crate (renzora_camera::, renzora_editor_framework::, renzora_gizmo::, renzora_grid::, renzora_hierarchy::, renzora_inspector::, renzora_console::, renzora_viewport::, renzora_keybindings::, renzora_settings::) from .ron and .bsn scenes. A game does not link those crates, so their components have no meaning there.

strip_for_server runs on the dedicated-server archive. It keeps only components from the crates a headless server uses (bevy_ecs::, bevy_transform::, renzora::, renzora_scripting::, renzora_physics::, renzora_terrain::, renzora_network::, renzora_engine::) and also removes the mesh-description components (renzora::MeshInstanceData, MeshColor, MeshPrimitive) that only matter to something that draws.

Reading: RpakArchive

use renzora_rpak::RpakArchive;

let pak = RpakArchive::from_file(Path::new("game.rpak"))?;
if let Some(bytes) = pak.get("scenes/main.bsn") {
    // decompressed on this call
}
ConstructorSource
from_file(path)A standalone or appended archive, memory-mapped, falling back to positional file reads where mmap fails
from_current_exe(), from_binary(path)An archive appended to a binary; Ok(None) when there is none
from_bytes(bytes)An archive in memory (the web)
from_backend(backend)Any PakBackend

The backends are MmapBackend (desktop and iOS: the OS page cache holds the working set, not the heap), BytesBackend (web and tests) and FileBackend (network shares and sandboxes where mmap fails). A backend hands back a borrowed slice when it can, so a stored entry from a mapped file is read without a copy.

The rest of the API: get, entry, contains, paths, len, is_empty, total_compressed_bytes, total_uncompressed_bytes, and extract_to(dir) for unpacking an archive to inspect it.

How the game finds the archive

Vfs::detect in crates/renzora_engine/src/vfs.rs chooses the source once at startup:

PlatformSource
Desktop--rpak <path>, else an archive appended to the executable, else <exe-stem>.rpak beside it, else the file system
Androidgame.rpak from the APK's assets
iOSgame.rpak in the app bundle
WebThe bytes JavaScript passed to set_rpak before start()

The asset reader then resolves each request against an absolute path, the archive, the project's assets/, an assets/ folder beside the executable, and finally one in the working directory. Falling through to the file system is what lets a game run unpacked from a project folder during development with the same code.

Inspecting an archive

crates/renzora_rpak/examples/inspect_pack.rs packs a project the way an export does and prints every key, which is the fastest way to see why a file did or did not ship:

cargo run --profile dist -p renzora_rpak --example inspect_pack -- path/to/project

It also prints the quoted strings found in scenes/main.ron, for older projects whose scenes are still RON.