#json #gamedev #serde #serde-json #game-assets #behavior

mini_asset_loader

A composable game asset-loading system

7 releases

0.2.1 May 13, 2023
0.2.0 May 13, 2023
0.1.4 May 13, 2023

#24 in #game-assets

Download history 30/week @ 2024-03-28 15/week @ 2024-04-04

66 downloads per month

MIT license

19KB
224 lines

mini_asset_loader

Crates.io docs.rs GitHub Workflow Status Crates.io

mini_asset_loader provides an extensible asset loading system, intended to load assets from various sources for use in a game.

The asset types, loaded sources, caching behaviour, etc. is entirely customizable. Assets are reference-counted and thread-safe.

A simple asset type based on serde_json and type tags is provided in the asset module.

Example

This example makes use of the asset feature. This feature provides a simple Tagged JSON asset type, which only works on nightly rust. However, substituting this for a custom asset type will work in stable rust.

use serde::{Serialize, Deserialize};
use mini_asset_loader::asset::{TaggedJsonAsset, TaggedJsonAssetCreationHandler};
use mini_asset_loader::loaders::ToCached;
use mini_asset_loader::{TypedAssetLoader, asset_loader_vec};
use std::path::PathBuf;


// Creating an asset type is as easy as making a Serializable/Deserializable struct...
#[derive(Serialize, Deserialize)]
struct StringAsset {
    value: String
}

// ...and then tagging it with these two lines:
#[typetag::serde]
impl TaggedJsonAsset for StringAsset {}



// ...Then, when we want to *load* assets...
fn main() {
    // We create our loader setup as usual...
    let mut loader = asset_loader_vec![
        PathBuf::from("assets/"),
        PathBuf::from("/global_assets/")
    ].to_cached();

    // Make a TaggedJsonAssetCreationHandler...
    let mut handler = TaggedJsonAssetCreationHandler;
    
    // And we can load our assets!
    if let Some(my_string_asset) = loader.load_typed_asset::<StringAsset>(&mut handler, "my_string_asset.json") {
        println!("String asset loaded: {}", my_string_asset.read().value);
    }
}

Dependencies

~0–1.1MB
~20K SLoC