Skip to content

Commit

Permalink
Merge pull request #21 from NiklasEi/bevy_main
Browse files Browse the repository at this point in the history
Update to Bevy 0.12
  • Loading branch information
NiklasEi authored Nov 4, 2023
2 parents 678dfef + e542f86 commit 868fbbf
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 103 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.8.0 - 04.11.2023
- Update to Bevy 0.12

## v0.7.0 - 10.07.2023
- Update to Bevy 0.11

Expand Down
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_common_assets"
version = "0.7.0"
version = "0.8.0"
authors = ["Niklas Eicker <git@nikl.me>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -21,18 +21,19 @@ msgpack = ["dep:rmp-serde"]
xml = ["dep:quick-xml"]

[dependencies]
bevy = { version = "0.11", default-features = false, features = ["bevy_asset"] }
serde_toml = { version = "0.7", package = "toml", optional = true }
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] }
serde_toml = { version = "0.8", package = "toml", optional = true }
serde_ron = { version = "0.8", package = "ron", optional = true }
serde_yaml = { version = "0.9", optional = true }
serde_json = { version = "1", optional = true }
rmp-serde = { version = "1", optional = true }
quick-xml = { version = "0.29", features = [ "serialize" ], optional = true }
thiserror = "1.0"
quick-xml = { version = "0.31", features = [ "serialize" ], optional = true }
serde = { version = "1" }
anyhow = { version = "1" }

[dev-dependencies]
bevy = { version = "0.11" }
bevy = { version = "0.12" }
serde = { version = "1" }

[package.metadata.docs.rs]
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ Supported formats:

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::reflect::TypeUuid` for them. The last derive requires a unique uuid as an attribute:
Define the types that you would like to load from files and derive `serde::Deserialize`, `bevy::reflect::TypePath`, and `bevy::asset::Asset` for them. The last derive requires a unique uuid as an attribute:
```rust
#[derive(serde::Deserialize, bevy::reflect::TypeUuid, bevy::reflect::TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"] // <-- keep me unique
#[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
struct Level {
positions: Vec<[f32;3]>,
}
Expand Down Expand Up @@ -57,8 +56,7 @@ fn main() {
.run();
}

#[derive(serde::Deserialize, bevy::reflect::TypeUuid, bevy::reflect::TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
Expand All @@ -77,12 +75,13 @@ Compatibility of `bevy_common_assets` versions:

| `bevy_common_assets` | `bevy` |
|:---------------------|:-------|
| `0.8` | `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.10` |
| `main` | `0.12` |
| `bevy_main` | `main` |

## License
Expand Down
5 changes: 2 additions & 3 deletions examples/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;
use bevy_common_assets::json::JsonAssetPlugin;

fn main() {
Expand Down Expand Up @@ -44,8 +44,7 @@ fn spawn_level(
}
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
Expand Down
5 changes: 2 additions & 3 deletions examples/msgpack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;
use bevy_common_assets::msgpack::MsgPackAssetPlugin;

fn main() {
Expand Down Expand Up @@ -44,8 +44,7 @@ fn spawn_level(
}
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
Expand Down
14 changes: 7 additions & 7 deletions examples/multiple_formats.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::asset::LoadState;
use bevy::prelude::*;
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;
use bevy_common_assets::json::JsonAssetPlugin;
use bevy_common_assets::ron::RonAssetPlugin;

Expand All @@ -21,8 +21,7 @@ fn main() {
.run();
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
Expand Down Expand Up @@ -60,11 +59,12 @@ fn check_loading(
handles: Res<Levels>,
mut state: ResMut<NextState<AppState>>,
) {
if asset_server.get_group_load_state(handles.0.iter().map(|handle| handle.id()))
== LoadState::Loaded
{
state.set(AppState::Level);
for handle in &handles.0 {
if asset_server.get_load_state(handle) != Some(LoadState::Loaded) {
return;
}
}
state.set(AppState::Level);
}

#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
Expand Down
5 changes: 2 additions & 3 deletions examples/ron.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;
use bevy_common_assets::ron::RonAssetPlugin;

fn main() {
Expand Down Expand Up @@ -41,8 +41,7 @@ fn spawn_level(
}
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
Expand Down
5 changes: 2 additions & 3 deletions examples/toml.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;
use bevy_common_assets::toml::TomlAssetPlugin;

fn main() {
Expand Down Expand Up @@ -44,8 +44,7 @@ fn spawn_level(
}
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
Expand Down
5 changes: 2 additions & 3 deletions examples/xml.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::math::f32::Vec3;
use bevy::prelude::*;
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;
use bevy_common_assets::xml::XmlAssetPlugin;

fn main() {
Expand Down Expand Up @@ -44,8 +44,7 @@ fn spawn_level(
}
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
#[serde(rename = "Position")]
positions: Vec<Position>,
Expand Down
5 changes: 2 additions & 3 deletions examples/yaml.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;
use bevy_common_assets::yaml::YamlAssetPlugin;

fn main() {
Expand Down Expand Up @@ -44,8 +44,7 @@ fn spawn_level(
}
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
Expand Down
43 changes: 32 additions & 11 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use bevy::app::{App, Plugin};
use bevy::asset::{AddAsset, Asset, AssetLoader, BoxedFuture, LoadContext, LoadedAsset};
use bevy::asset::io::Reader;
use bevy::asset::{Asset, AssetApp, AssetLoader, AsyncReadExt, BoxedFuture, LoadContext};
use serde_json::from_slice;
use std::marker::PhantomData;
use thiserror::Error;

/// Plugin to load your asset type `A` from json files.
pub struct JsonAssetPlugin<A> {
Expand All @@ -14,10 +16,11 @@ where
for<'de> A: serde::Deserialize<'de> + Asset,
{
fn build(&self, app: &mut App) {
app.add_asset::<A>().add_asset_loader(JsonAssetLoader::<A> {
extensions: self.extensions.clone(),
_marker: PhantomData,
});
app.init_asset::<A>()
.register_asset_loader(JsonAssetLoader::<A> {
extensions: self.extensions.clone(),
_marker: PhantomData,
});
}
}

Expand All @@ -39,19 +42,37 @@ struct JsonAssetLoader<A> {
_marker: PhantomData<A>,
}

/// Possible errors that can be produced by [`JsonAssetLoader`]
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum JsonLoaderError {
/// An [IO Error](std::io::Error)
#[error("Could not read the file: {0}")]
Io(#[from] std::io::Error),
/// A [JSON Error](serde_json::error::Error)
#[error("Could not parse the JSON: {0}")]
JsonError(#[from] serde_json::error::Error),
}

impl<A> AssetLoader for JsonAssetLoader<A>
where
for<'de> A: serde::Deserialize<'de> + Asset,
{
type Asset = A;
type Settings = ();
type Error = JsonLoaderError;

fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
reader: &'a mut Reader,
_settings: &'a (),
_load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let asset = from_slice::<A>(bytes)?;
load_context.set_default_asset(LoadedAsset::new(asset));
Ok(())
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let asset = from_slice::<A>(&bytes)?;
Ok(asset)
})
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! The following example requires the `json` feature and loads a custom asset from a json file.
//! ```
//! use bevy::prelude::*;
//! use bevy::reflect::{TypeUuid, TypePath};
//! use bevy::reflect::TypePath;
//! # /*
//! use bevy_common_assets::json::JsonAssetPlugin;
//! # */
Expand All @@ -20,6 +20,7 @@
//! .add_plugins((DefaultPlugins, JsonAssetPlugin::<Level>::new(&["level.json"])))
//! # */
//! # .add_plugins((MinimalPlugins, AssetPlugin::default()))
//! # .init_asset::<Level>()
//! .add_systems(Startup, load_level)
//! # .add_systems(Update, stop)
//! .run()
Expand All @@ -30,8 +31,7 @@
//! commands.insert_resource(handle);
//! }
//!
//! #[derive(serde::Deserialize, TypeUuid, TypePath)]
//! #[uuid = "413be529-bfeb-41b3-9db0-4b8b380a2c46"]
//! #[derive(serde::Deserialize, Asset, TypePath)]
//! struct Level {
//! positions: Vec<[f32; 3]>,
//! }
Expand Down
38 changes: 29 additions & 9 deletions src/msgpack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use bevy::app::{App, Plugin};
use bevy::asset::{AddAsset, Asset, AssetLoader, BoxedFuture, LoadContext, LoadedAsset};
use bevy::asset::io::Reader;
use bevy::asset::{Asset, AssetApp, AssetLoader, AsyncReadExt, BoxedFuture, LoadContext};
use rmp_serde::from_slice;
use std::marker::PhantomData;
use thiserror::Error;

/// Plugin to load your asset type `A` from `MessagePack` files.
pub struct MsgPackAssetPlugin<A> {
Expand All @@ -14,8 +16,8 @@ where
for<'de> A: serde::Deserialize<'de> + Asset,
{
fn build(&self, app: &mut App) {
app.add_asset::<A>()
.add_asset_loader(MsgPackAssetLoader::<A> {
app.init_asset::<A>()
.register_asset_loader(MsgPackAssetLoader::<A> {
extensions: self.extensions.clone(),
_marker: PhantomData,
});
Expand All @@ -40,19 +42,37 @@ struct MsgPackAssetLoader<A> {
_marker: PhantomData<A>,
}

/// Possible errors that can be produced by [`MsgPackAssetLoader`]
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum MsgPackLoaderError {
/// An [IO Error](std::io::Error)
#[error("Could not read the file: {0}")]
Io(#[from] std::io::Error),
/// A [MessagePack Error](rmp_serde::decode::Error)
#[error("Could not parse MessagePack: {0}")]
MsgPackError(#[from] rmp_serde::decode::Error),
}

impl<A> AssetLoader for MsgPackAssetLoader<A>
where
for<'de> A: serde::Deserialize<'de> + Asset,
{
type Asset = A;
type Settings = ();
type Error = MsgPackLoaderError;

fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
reader: &'a mut Reader,
_settings: &'a (),
_load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let asset = from_slice::<A>(bytes)?;
load_context.set_default_asset(LoadedAsset::new(asset));
Ok(())
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let asset = from_slice::<A>(&bytes)?;
Ok(asset)
})
}

Expand Down
Loading

0 comments on commit 868fbbf

Please sign in to comment.