Giter VIP home page Giter VIP logo

bevy_common_assets's Introduction

Bevy common assets

crates.io docs license crates.io

Collection of Bevy plugins offering generic asset loaders for common file formats.

Supported formats:

format feature example
json json json.rs
msgpack msgpack msgpack.rs
postcard postcard postcard.rs
ron ron ron.rs
toml toml toml.rs
xml xml xml.rs
yaml yaml yaml.rs
csv csv csv.rs

Usage

Enable the feature(s) for the format(s) that you want to use.

Define the types that you would like to load from files and derive serde::Deserialize, bevy::reflect::TypePath, and bevy::asset::Asset for them.

#[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
struct Level {
    positions: Vec<[f32;3]>,
}

With the types ready, you can start adding asset plugins. Every plugin gets the asset type that it is supposed to load as a generic parameter. You can also configure custom file endings for each plugin:

use bevy::prelude::*;
use bevy_common_assets::json::JsonAssetPlugin;
use bevy_common_assets::msgpack::MsgPackAssetPlugin;
use bevy_common_assets::postcard::PostcardAssetPlugin;
use bevy_common_assets::ron::RonAssetPlugin;
use bevy_common_assets::toml::TomlAssetPlugin;
use bevy_common_assets::xml::XmlAssetPlugin;
use bevy_common_assets::yaml::YamlAssetPlugin;

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins,
            JsonAssetPlugin::<Level>::new(&["level.json", "custom.json"]),
            RonAssetPlugin::<Level>::new(&["level.ron"]),
            MsgPackAssetPlugin::<Level>::new(&["level.msgpack"]),
            PostcardAssetPlugin::<Level>::new(&["level.postcard"]),
            TomlAssetPlugin::<Level>::new(&["level.toml"]),
            XmlAssetPlugin::<Level>::new(&["level.xml"]),
            YamlAssetPlugin::<Level>::new(&["level.yaml"])
        ))
        // ...
        .run();
}

#[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
struct Level {
    positions: Vec<[f32; 3]>,
}

The example above will load Level structs from json files ending on .level.json or .custom.json, from ron files ending on .level.ron and so on...

See the examples for working Bevy apps using the different formats.

Compatible Bevy versions

The main branch is compatible with the latest Bevy release.

Compatibility of bevy_common_assets versions:

bevy_common_assets bevy
0.10 0.13
0.8 - 0.9 0.12
0.7 0.11
0.5 - 0.6 0.10
0.4 0.9
0.3 0.8
0.1 - 0.2 0.7
main 0.13
bevy_main main

License

Dual-licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

bevy_common_assets's People

Contributors

boylede avatar eerii avatar jredow avatar niklasei avatar sardap avatar seldom-se avatar striezel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bevy_common_assets's Issues

Support CSV

It would be great if there was added support for CSV files, the csv crate already has support for serde!

Make loaders public

I'm trying to app.register_asset_processor::<LoadTransformAndSave<RonAssetLoader<MyAsset>, _, _>>(LoadTransformAndSave::new(..., ...), but I cannot, bc RonAssetLoader is private

bevy_sprite should be should be behind a feature flag, not a hard dependency

I'm trying to optimize my cargo build times for my project, and a major slow down I've noticed for recompiles is bevy_sprite. I'm making a 3d project, and I'd like to be able to remove bevy_sprite, since I don't need it, but this crate makes it a hard dependency.

A '2d' feature flag would let people who use this crate and bevy_asset_loader to remove bevy_sprite, and speed up re-compiles for 3d projects.

Support XML

This would allow for all xml file types (custom, html, etc.) to be used as assets.

I would be happy to code it up myself with a little guidance if needed!

Could not find `json`.

Just trying to use the JsonAssetPlugin:

use bevy_common_assets::json::JsonAssetPlugin;

But this won't compile. Here's the error I get:

image

Am I doing something wrong? Btw I'm fairly new to Rust.

Create AssetSaver implementations for each format.

Bevy now has AssetSaver. This allows a cross-platform way of saving assets by:

  1. Getting an AssetWriter from an AssetSource (from AssetServer)
  2. Calling AssetWriter::write to get an AsyncWrite object.
  3. Calling AssetSaver::save with that AsyncWrite object.

In the future this will likely be streamlined but for now we should support AssetSaver to support this "canonical" workflow.

no `AssetLoader` found for the following extension: yaml

bevy 1.12.1
Currently figuring out how to use this crate, i added the plugin like so:

        .add_plugins((
            DefaultPlugins, 
            YamlAssetPlugin::<World>::new(&["world.yml"])
        ))
#[derive(Deserialize, Asset, TypePath, Debug)]
struct World {
    name: String,
    width: u8,
    height: u8,
}


fn load_world(
    server: Res<AssetServer>,
    mut images: ResMut<Assets<Image>>,
    mut worlds: ResMut<Assets<World>>,
    mut commands: Commands
) {
    let handle: Handle<World> = server.load("world.yaml");
    
    if let Some(x) = worlds.get_mut(handle.id()) {
        println!("{:?}", x);
    } else {
        println!("Did not find!")
    }
}

This comes up with "did not find!" and the error found in the title: ERROR bevy_asset::server: no 'AssetLoader' found for the following extension: yaml This error also occurs when changing everything to 'yml'.
The yaml file is very simple and as far as i know, this should work?

name: Cloob
width: 64
height: 32

What is going wrong?
Thanks in advance!

Edit: The example contains wrong code, worlds.get_mut(handle.id()) should be in a Update script. It still gives the error regardless.

Example using `Map`

Could you please add an example of how to read a struct in addition to the vector?

Getting `AssetServer does not exist` when using this with bevy_asset_loader

I could not get it to work with bevy_asset_loader

Is there some example code I can look at to be sure I'm using it correctly?
error:

thread 'main' panicked at 'Requested resource bevy_asset::asset_server::AssetServer does not exist in the `World`. 
                Did you forget to add it using `app.insert_resource` / `app.init_resource`? 
                Resources are also implicitly added via `app.add_event`,
                and can be added by plugins.', /home/pinkponk/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_asset-0.8.1/src/assets.rs:324:43

Usage:

...
.with_collection::<WorldAssets>()
...
.add_plugin(JsonAssetPlugin::<WorldConfigJson>::new(&["json.world"]))
#[derive(serde::Deserialize, bevy::reflect::TypeUuid)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b38022c46"]
pub struct WorldConfigJson {
    plants: Vec<String>,
    spawns: Vec<SpawnsJson>,
}

#[derive(serde::Deserialize)]
pub struct SpawnsJson {
    species: String,
    nr_creatures: u32,
    radius: f32,
    age_factor: f32,
    world_pos_factor: [f32; 2],
}
pub fn configure_world(
    mut commands: Commands,
    world_assets: Res<WorldAssets>,
    mut world_config_jsons: ResMut<Assets<WorldConfigJson>>,
) {
...}

file:

{
    "plants": [
        "mushroom",
        "grass",
        "tree",
        "bush",
        "carrot"
    ],
    "spawns": [
        {
            "species": "fox",
            "nr_creatures": 3,
            "radius": 10.0,
            "age_factor": 0.5,
            "world_pos_factor": [
                0.1,
                0.1
            ]
        }
    ]
}

Asset type for arbitrary json/toml/etc documents

TL;DR

Could this repository include a common asset type and loader for loading generic serde_json::Value, toml_edit::Document, etc. types from files with the generic format extension like .json/.toml rather than a specific extension?

Background

I built something closely related to this for loading plugin settings from specific configuration files: https://github.com/devnev/bevy_settings_loader

But in my plugin, the settings are resource singletons and not identified by file extensions but by the full path to the file. So I add a generic .json / .toml loader and then deserialise from the loaded value into the destination type for the asset matching the settings path.

For that I've added generic JsonAsset and TomlAsset types that wrap serde_json::Value and toml_edit::Document loaded from arbitrary files with the corresponding extension. However, my repository/package/plugin is too specific for something that generic, whereas this repository seems more suitable and has most of the code for it already.

The main missing pieces is a wrapper implementing Asset for serde_json::Value etc, which would allow other plugins to avoid duplicate registration of loaders for the corresponding extension.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.