From 60984220a0e64bc16c079c2f144d9e963b946abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Konik?= Date: Wed, 10 Apr 2024 18:36:11 +0200 Subject: [PATCH] Bevy 0.12 (#9) * Update to bevy 0.12 --- Cargo.toml | 3 ++- README.md | 1 + examples/call_function_from_rust.rs | 4 ++-- examples/current_entity.rs | 2 +- examples/custom_type.rs | 2 +- examples/ecs.rs | 2 +- examples/entity_variable.rs | 2 +- examples/function_params.rs | 2 +- examples/hello_world.rs | 2 +- examples/non_closure_system.rs | 2 +- examples/promises.rs | 2 +- examples/side_effects.rs | 4 ++-- src/assets.rs | 21 ++++++++++++++------- src/lib.rs | 7 +++---- src/systems.rs | 6 +++--- 15 files changed, 35 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5edbf2d..c416298 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,10 @@ keywords = ["bevy", "rhai", "scripting", "game", "gamedev"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bevy = { default-features = false, version = "0.11.0", features = [ +bevy = { default-features = false, version = "0.12.0", features = [ "bevy_asset", ] } serde = "1.0.162" rhai = { version = "1.14.0", features = ["sync", "internals", "unchecked"] } thiserror = "1.0.40" +anyhow = "1.0.82" diff --git a/README.md b/README.md index b803bc6..64c3392 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ The examples live in `examples` directory and their corresponding scripts live i | bevy version | bevy_scriptum version | |--------------|----------------------| +| 0.12 | 0.3 | | 0.11 | 0.2 | | 0.10 | 0.1 | diff --git a/examples/call_function_from_rust.rs b/examples/call_function_from_rust.rs index cefcdc2..797136a 100644 --- a/examples/call_function_from_rust.rs +++ b/examples/call_function_from_rust.rs @@ -10,7 +10,7 @@ fn main() { loop { if let Some(app_exit_events) = app.world.get_resource_mut::>() { if app_exit_event_reader - .iter(&app_exit_events) + .read(&app_exit_events) .last() .is_some() { @@ -21,7 +21,7 @@ fn main() { } }) .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_systems(Startup, startup) .add_systems(Update, call_rhai_on_update_from_rust) .add_script_function(String::from("quit"), |mut exit: EventWriter| { diff --git a/examples/current_entity.rs b/examples/current_entity.rs index 1b4a398..ff6dd5b 100644 --- a/examples/current_entity.rs +++ b/examples/current_entity.rs @@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script}; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_script_function( String::from("get_name"), |In((entity,)): In<(Entity,)>, names: Query<&Name>| { diff --git a/examples/custom_type.rs b/examples/custom_type.rs index 45b73f3..b918dd1 100644 --- a/examples/custom_type.rs +++ b/examples/custom_type.rs @@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script, ScriptingRuntime}; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_script_function(String::from("hello_bevy"), || { println!("hello bevy, called from script"); }) diff --git a/examples/ecs.rs b/examples/ecs.rs index 2888056..37cb2fb 100644 --- a/examples/ecs.rs +++ b/examples/ecs.rs @@ -7,7 +7,7 @@ struct Player; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_script_function( String::from("print_player_names"), |players: Query<&Name, With>| { diff --git a/examples/entity_variable.rs b/examples/entity_variable.rs index 295da10..7b64f8c 100644 --- a/examples/entity_variable.rs +++ b/examples/entity_variable.rs @@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script}; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_systems(Startup, startup) .run(); } diff --git a/examples/function_params.rs b/examples/function_params.rs index 817c56a..251a875 100644 --- a/examples/function_params.rs +++ b/examples/function_params.rs @@ -5,7 +5,7 @@ use rhai::ImmutableString; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_script_function(String::from("fun_without_params"), || { println!("called without params"); }) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 021b8e5..66da4f6 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script}; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_script_function(String::from("hello_bevy"), || { println!("hello bevy, called from script"); }) diff --git a/examples/non_closure_system.rs b/examples/non_closure_system.rs index 958cefe..903b713 100644 --- a/examples/non_closure_system.rs +++ b/examples/non_closure_system.rs @@ -4,7 +4,7 @@ use bevy_scriptum::{prelude::*, Script}; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_script_function(String::from("hello_bevy"), hello_bevy_callback_system) .add_systems(Startup, startup) .run(); diff --git a/examples/promises.rs b/examples/promises.rs index 6014aa2..b1ac3c7 100644 --- a/examples/promises.rs +++ b/examples/promises.rs @@ -7,7 +7,7 @@ struct Player; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_script_function( String::from("get_player_name"), |player_names: Query<&Name, With>| player_names.single().to_string(), diff --git a/examples/side_effects.rs b/examples/side_effects.rs index 772fc47..08525a5 100644 --- a/examples/side_effects.rs +++ b/examples/side_effects.rs @@ -13,7 +13,7 @@ fn main() { loop { if let Some(app_exit_events) = app.world.get_resource_mut::>() { if app_exit_event_reader - .iter(&app_exit_events) + .read(&app_exit_events) .last() .is_some() { @@ -24,7 +24,7 @@ fn main() { } }) .add_plugins(DefaultPlugins) - .add_plugins(ScriptingPlugin::default()) + .add_plugins(ScriptingPlugin) .add_systems(Startup, startup) .add_systems(Update, print_entity_names_and_quit) .add_script_function(String::from("spawn_entity"), spawn_entity) diff --git a/src/assets.rs b/src/assets.rs index d4486bb..11f7573 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -1,12 +1,12 @@ use bevy::{ - asset::{AssetLoader, LoadContext, LoadedAsset}, + asset::{io::Reader, Asset, AssetLoader, AsyncReadExt as _, LoadContext}, reflect::{TypePath, TypeUuid}, utils::BoxedFuture, }; use serde::Deserialize; /// A script that can be loaded by the [crate::ScriptingPlugin]. -#[derive(Debug, Deserialize, TypeUuid, TypePath)] +#[derive(Asset, Debug, Deserialize, TypeUuid, TypePath)] #[uuid = "3ed4b68b-4f5d-4d82-96f6-5194e358921a"] pub struct RhaiScript(pub String); @@ -15,15 +15,22 @@ pub struct RhaiScript(pub String); pub struct RhaiScriptLoader; impl AssetLoader for RhaiScriptLoader { + type Asset = RhaiScript; + type Settings = (); + type Error = anyhow::Error; + fn load<'a>( &'a self, - bytes: &'a [u8], - load_context: &'a mut LoadContext, - ) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> { + reader: &'a mut Reader, + _settings: &'a Self::Settings, + _load_context: &'a mut LoadContext, + ) -> BoxedFuture<'a, anyhow::Result> { Box::pin(async move { + let mut bytes = Vec::new(); + reader.read_to_end(&mut bytes).await?; + let rhai_script = RhaiScript(String::from_utf8(bytes.to_vec())?); - load_context.set_default_asset(LoadedAsset::new(rhai_script)); - Ok(()) + Ok(rhai_script) }) } diff --git a/src/lib.rs b/src/lib.rs index cbe9bff..804d306 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -//! ⚠️ **Pre-release, alpha version**: API is bound to change, bugs are to be expected. -//! //! bevy_scriptum is a a plugin for [Bevy](https://bevyengine.org/) that allows you to write some of your game logic in a scripting language. //! Currently, only [Rhai](https://rhai.rs/) is supported, but more languages may be added in the future. //! @@ -144,6 +142,7 @@ //! //! | bevy version | bevy_scriptum version | //! |--------------|----------------------| +//! | 0.12 | 0.3 | //! | 0.11 | 0.2 | //! | 0.10 | 0.1 | //! @@ -218,8 +217,8 @@ pub struct ScriptingPlugin; impl Plugin for ScriptingPlugin { fn build(&self, app: &mut App) { - app.add_asset::() - .init_asset_loader::() + app.register_asset_loader(RhaiScriptLoader) + .init_asset::() .init_resource::() .insert_resource(ScriptingRuntime::default()) .add_systems(Startup, init_engine.pipe(log_errors)) diff --git a/src/systems.rs b/src/systems.rs index 9666c6a..dae433b 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -48,10 +48,10 @@ pub(crate) fn reload_scripts( mut ev_asset: EventReader>, mut scripts: Query<(Entity, &mut Script)>, ) { - for ev in ev_asset.iter() { - if let AssetEvent::Modified { handle } = ev { + for ev in ev_asset.read() { + if let AssetEvent::Modified { id } = ev { for (entity, script) in &mut scripts { - if script.script == *handle { + if script.script.id() == *id { commands.entity(entity).remove::(); } }