Crates.io | wasset |
lib.rs | wasset |
version | 0.1.2 |
source | src |
created_at | 2024-08-07 14:05:53.200323 |
updated_at | 2024-08-18 15:17:38.578781 |
description | Embed external assets into WASM modules. |
homepage | |
repository | https://github.com/DouglasDwyer/wasset |
max_upload_size | |
id | 1328451 |
size | 34,370 |
This crate allows for embedding external asset files (text, images, models) into WASM plugins. The assets are stored in the WASM module's custom data section. This allows for reading assets on the host using a WassetParser
. The WASM module itself can reference its assets by macro-generated ID.
wasset
is meant to be a foundation for game asset systems. It differs from using include_bytes!()
to include assets in the following ways:
WassetId
.wasset
does not define an asset format - rather, it provides the means to load and store a user-specified asset type to WASM. Therefore, setting up wasset
requires:
AssetSchema
type that can be serialized and deserialized with serde
AssetEncoder
to determine how files are turned into assetswasset::include_assets::<A: AssetEncoder>(path)
macro with the appropriate asset encoder typeA complete example is available here. Once the asset type and macro have been defined, they may be used from within WASM as follows:
use wasset::*;
use wasset_example_macro::*;
// Load all assets from the given folder. This is the
// macro defined for a specific `AssetEncoder`.
include_assets!("wasset_example/wasset_example_module/assets");
/// Gets a list of all assets from this module.
pub fn list_all_assets() -> &'static [WassetId] {
&[
// The macro has defined these for us.
assets::SOME_BINARY,
assets::SOME_TEXT,
assets::submodule::MORE_TEXT
]
}
Then, the asset data for this WASM plugin may be examined from the host:
use wasset::*;
use wasset_example_schema::*;
fn main() {
// Create a parser from the bytes of the WASM module.
let parser = WassetParser::<ExampleAsset>::parse(EXAMPLE_PLUGIN_WASM).unwrap();
for (id, asset) in &parser {
println!("{id:?} | {asset:?}");
// Prints something like:
// WassetId(9ee64711-8e7e-4e40-a1bc-f13a1b4e5bdb) | Ok(Binary([97, 115, 106, 100]))
// WassetId(b230fb86-8bf6-49a0-94f9-624386204129) | Ok(Text("Even more!"))
// WassetId(ae189ff9-b0d4-48fc-b0e1-3093d53bff85) | Ok(Text("Hello world!"))
}
}
Pod
and Zeroable
attributes on relevant types.encode_macro
use relative paths rather than paths from the project root.