diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5110b5c..b7bd998 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,9 +11,9 @@ env: jobs: build: - runs-on: ubuntu-latest - + env: + RUSTFLAGS: -D warnings steps: - uses: actions/checkout@v3 - name: Clippy @@ -22,7 +22,3 @@ jobs: run: cargo build --all-features --verbose - name: Run tests run: cargo test --all-features --verbose - - name: Install cargo-examples - run: cargo install cargo-examples - - name: Run all examples - run: cargo examples --features=lua,rhai diff --git a/Cargo.toml b/Cargo.toml index 9ab19bd..d14c547 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "bevy_scriptum" authors = ["Jaroslaw Konik "] -version = "0.6.0" +version = "0.7.0" edition = "2021" license = "MIT OR Apache-2.0" readme = "README.md" @@ -15,103 +15,131 @@ lua = ["mlua/luajit"] rhai = ["dep:rhai"] [dependencies] -bevy = { default-features = false, version = "0.14", features = [ - "bevy_asset", -] } +bevy = { default-features = false, version = "0.15", features = ["bevy_asset"] } serde = "1.0.162" -rhai = { version = "1.14.0", features = ["sync", "internals", "unchecked"], optional = true } +rhai = { version = "1.14.0", features = [ + "sync", + "internals", + "unchecked", +], optional = true } thiserror = "1.0.40" anyhow = "1.0.82" tracing = "0.1.40" -mlua = { version = "0.9.8", features = ["luajit", "vendored", "send"], optional = true } +mlua = { version = "0.9.8", features = [ + "luajit", + "vendored", + "send", +], optional = true } [[example]] name = "call_function_from_rust_rhai" path = "examples/rhai/call_function_from_rust.rs" +required-features = ["rhai"] [[example]] name = "current_entity_rhai" path = "examples/rhai/current_entity.rs" +required-features = ["rhai"] [[example]] name = "custom_type_rhai" path = "examples/rhai/custom_type.rs" +required-features = ["rhai"] [[example]] name = "ecs_rhai" path = "examples/rhai/ecs.rs" +required-features = ["rhai"] [[example]] name = "entity_variable_rhai" path = "examples/rhai/entity_variable.rs" +required-features = ["rhai"] [[example]] name = "function_params_rhai" path = "examples/rhai/function_params.rs" +required-features = ["rhai"] [[example]] name = "hello_world_rhai" path = "examples/rhai/hello_world.rs" +required-features = ["rhai"] [[example]] name = "multiple_plugins_rhai" path = "examples/rhai/multiple_plugins.rs" +required-features = ["rhai"] [[example]] name = "non_closure_system_rhai" path = "examples/rhai/non_closure_system.rs" +required-features = ["rhai"] [[example]] name = "promises_rhai" path = "examples/rhai/promises.rs" +required-features = ["rhai"] [[example]] name = "side_effects_rhai" path = "examples/rhai/side_effects.rs" +required-features = ["rhai"] [[example]] name = "call_function_from_rust_lua" path = "examples/lua/call_function_from_rust.rs" +required-features = ["lua"] [[example]] name = "current_entity_lua" path = "examples/lua/current_entity.rs" +required-features = ["lua"] [[example]] name = "custom_type_lua" path = "examples/lua/custom_type.rs" +required-features = ["lua"] [[example]] name = "ecs_lua" path = "examples/lua/ecs.rs" +required-features = ["lua"] [[example]] name = "entity_variable_lua" path = "examples/lua/entity_variable.rs" +required-features = ["lua"] [[example]] name = "function_params_lua" path = "examples/lua/function_params.rs" +required-features = ["lua"] [[example]] name = "hello_world_lua" path = "examples/lua/hello_world.rs" +required-features = ["lua"] [[example]] name = "multiple_plugins_lua" path = "examples/lua/multiple_plugins.rs" +required-features = ["lua"] [[example]] name = "non_closure_system_lua" path = "examples/lua/non_closure_system.rs" +required-features = ["lua"] [[example]] name = "promises_lua" path = "examples/lua/promises.rs" +required-features = ["lua"] [[example]] name = "side_effects_lua" path = "examples/lua/side_effects.rs" +required-features = ["lua"] [dev-dependencies] tracing-subscriber = "0.3.18" diff --git a/README.md b/README.md index 4dc9477..9bf46b6 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,33 @@ which you can then call in your script like this: ```lua fun_with_string_param("Hello world!") ``` +It is also possible to split the definition of your callback functions up over multiple plugins. This enables you to split up your code by subject and keep the main initialization light and clean. +This can be accomplished by using `add_scripting_api`. Be careful though, `add_scripting` has to be called before adding plugins. +```rust +use bevy::prelude::*; +use bevy_scriptum::prelude::*; +use bevy_scriptum::runtimes::lua::prelude::*; + +struct MyPlugin; +impl Plugin for MyPlugin { + fn build(&self, app: &mut App) { + app.add_scripting_api::(|runtime| { + runtime.add_function(String::from("hello_from_my_plugin"), || { + info!("Hello from MyPlugin"); + }); + }); + } +} + +App::new() + .add_plugins(DefaultPlugins) + .add_scripting::(|_| { + // nice and clean + }) + .add_plugins(MyPlugin) + .run(); +``` + ### Usage @@ -91,7 +118,7 @@ Add the following to your `Cargo.toml`: ```toml [dependencies] -bevy_scriptum = { version = "0.6", features = ["lua"] } +bevy_scriptum = { version = "0.7", features = ["lua"] } ``` or execute `cargo add bevy_scriptum --features lua` from your project directory. @@ -164,6 +191,7 @@ The examples live in `examples` directory and their corresponding scripts live i | bevy version | bevy_scriptum version | |--------------|-----------------------| +| 0.15 | 0.7 | | 0.14 | 0.6 | | 0.13 | 0.4-0.5 | | 0.12 | 0.3 | diff --git a/SECURITY.md b/SECURITY.md index f8f7e4d..d124bd4 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -7,7 +7,7 @@ currently being supported with security updates. | Version | Supported | | ------- | ------------------ | -| 0.6 | :white_check_mark: | +| 0.7 | :white_check_mark: | ## Reporting a Vulnerability diff --git a/book/src/bevy_support_matrix.md b/book/src/bevy_support_matrix.md index 9ff5654..d67b8d8 100644 --- a/book/src/bevy_support_matrix.md +++ b/book/src/bevy_support_matrix.md @@ -2,6 +2,7 @@ | bevy version | bevy_scriptum version | | ------------ | --------------------- | +| 0.15 | 0.7 | | 0.14 | 0.6 | | 0.13 | 0.4-0.5 | | 0.12 | 0.3 | diff --git a/book/src/introduction.md b/book/src/introduction.md index eade7af..a643a37 100644 --- a/book/src/introduction.md +++ b/book/src/introduction.md @@ -90,7 +90,7 @@ fun_with_string_param("Hello world!") It is also possible to split the definition of your callback functions up over multiple plugins. This enables you to split up your code by subject and keep the main initialization light and clean. This can be accomplished by using `add_scripting_api`. Be careful though, `add_scripting` has to be called before adding plugins. -```rust +```rust use bevy::prelude::*; use bevy_scriptum::prelude::*; use bevy_scriptum::runtimes::lua::prelude::*; @@ -124,7 +124,7 @@ Add the following to your `Cargo.toml`: ```toml [dependencies] -bevy_scriptum = { version = "0.6", features = ["lua"] } +bevy_scriptum = { version = "0.7", features = ["lua"] } ``` or execute `cargo add bevy_scriptum --features lua` from your project directory. diff --git a/book/src/lua/installation.md b/book/src/lua/installation.md index 8dfdc57..1c4beee 100644 --- a/book/src/lua/installation.md +++ b/book/src/lua/installation.md @@ -4,8 +4,8 @@ Add the following to your `Cargo.toml`: ```toml [dependencies] -bevy = "0.13" -bevy_scriptum = { version = "0.6", features = ["lua"] } +bevy = "0.15" +bevy_scriptum = { version = "0.7", features = ["lua"] } ``` If you need a different version of bevy you need to use a matching bevy_scriptum diff --git a/book/src/rhai/installation.md b/book/src/rhai/installation.md index 24e2010..7991425 100644 --- a/book/src/rhai/installation.md +++ b/book/src/rhai/installation.md @@ -4,8 +4,8 @@ Add the following to your `Cargo.toml`: ```toml [dependencies] -bevy = "0.13" -bevy_scriptum = { version = "0.6", features = ["rhai"] } +bevy = "0.15" +bevy_scriptum = { version = "0.7", features = ["rhai"] } ``` If you need a different version of bevy you need to use a matching bevy_scriptum diff --git a/book/src/workflow/live_reload.md b/book/src/workflow/live_reload.md index a57a701..3c37c23 100644 --- a/book/src/workflow/live_reload.md +++ b/book/src/workflow/live_reload.md @@ -6,7 +6,7 @@ To enable live reload it should be enough to enable `file-watcher` feature within bevy dependency in `Cargo.toml` ``` -bevy = { version = "0.13", features = ["file_watcher"] } +bevy = { version = "0.15", features = ["file_watcher"] } ``` ## Init-teardown pattern for game development diff --git a/examples/lua/call_function_from_rust.rs b/examples/lua/call_function_from_rust.rs index 24d5787..49df2c8 100644 --- a/examples/lua/call_function_from_rust.rs +++ b/examples/lua/call_function_from_rust.rs @@ -4,16 +4,6 @@ use bevy_scriptum::runtimes::lua::prelude::*; fn main() { App::new() - // This is just needed for headless console app, not needed for a regular bevy game - // that uses a winit window - .set_runner(move |mut app: App| { - loop { - app.update(); - if let Some(exit) = app.should_exit() { - return exit; - } - } - }) .add_plugins(DefaultPlugins) .add_systems(Startup, startup) .add_systems(Update, call_lua_on_update_from_rust) diff --git a/examples/rhai/call_function_from_rust.rs b/examples/rhai/call_function_from_rust.rs index 9c44149..f169590 100644 --- a/examples/rhai/call_function_from_rust.rs +++ b/examples/rhai/call_function_from_rust.rs @@ -4,16 +4,6 @@ use bevy_scriptum::runtimes::rhai::prelude::*; fn main() { App::new() - // This is just needed for headless console app, not needed for a regular bevy game - // that uses a winit window - .set_runner(move |mut app: App| { - loop { - app.update(); - if let Some(exit) = app.should_exit() { - return exit; - } - } - }) .add_plugins(DefaultPlugins) .add_systems(Startup, startup) .add_systems(Update, call_rhai_on_update_from_rust) diff --git a/src/assets.rs b/src/assets.rs index 5c7592d..471a3ee 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -1,7 +1,7 @@ use std::marker::PhantomData; use bevy::{ - asset::{io::Reader, Asset, AssetLoader, AsyncReadExt as _, LoadContext}, + asset::{io::Reader, Asset, AssetLoader, LoadContext}, utils::ConditionalSendFuture, }; @@ -29,11 +29,11 @@ impl + GetExtensions> AssetLoader for ScriptLoader { type Settings = (); type Error = anyhow::Error; - fn load<'a>( - &'a self, - reader: &'a mut Reader, - _settings: &'a Self::Settings, - _load_context: &'a mut LoadContext, + fn load( + &self, + reader: &mut dyn Reader, + _settings: &Self::Settings, + _load_context: &mut LoadContext, ) -> impl ConditionalSendFuture> { Box::pin(async move { let mut bytes = Vec::new(); diff --git a/src/callback.rs b/src/callback.rs index 19ea2d0..0174950 100644 --- a/src/callback.rs +++ b/src/callback.rs @@ -6,7 +6,7 @@ use crate::{promise::Promise, Runtime}; /// A system that can be used to call a script function. pub struct CallbackSystem { - pub(crate) system: Box, Out = R::Value>>, + pub(crate) system: Box>, Out = R::Value>>, pub(crate) arg_types: Vec, } @@ -56,7 +56,10 @@ pub(crate) trait FromRuntimeValueWithEngine<'a, R: Runtime> { } /// Trait that alllows to convert a script callback function into a Bevy [`System`]. -pub trait IntoCallbackSystem: IntoSystem { +pub trait IntoCallbackSystem: IntoSystem +where + In: SystemInput, +{ /// Convert this function into a [CallbackSystem]. #[must_use] fn into_callback_system(self, world: &mut World) -> CallbackSystem; @@ -87,10 +90,10 @@ where macro_rules! impl_tuple { ($($idx:tt $t:tt),+) => { - impl IntoCallbackSystem + impl IntoCallbackSystem, Out, Marker> for FN where - FN: IntoSystem<($($t,)+), Out, Marker>, + FN: IntoSystem, Out, Marker>, Out: for<'a> IntoRuntimeValueWithEngine<'a, Out, RN>, $($t: 'static + for<'a> FromRuntimeValueWithEngine<'a, RN>,)+ { diff --git a/src/lib.rs b/src/lib.rs index 259b7ba..730e5c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ //! Scripts are separate files that can be hot-reloaded at runtime. This allows you to quickly iterate on your game logic without having to recompile your game. //! //! All you need to do is register callbacks on your Bevy app like this: -//! ```rust +//! ```no_run //! use bevy::prelude::*; //! use bevy_scriptum::prelude::*; //! use bevy_scriptum::runtimes::lua::prelude::*; @@ -37,7 +37,7 @@ //! //! Every callback function that you expose to the scripting language is also a Bevy system, so you can easily query and mutate ECS components and resources just like you would in a regular Bevy system: //! -//! ```rust +//! ```no_run //! use bevy::prelude::*; //! use bevy_scriptum::prelude::*; //! use bevy_scriptum::runtimes::lua::prelude::*; @@ -61,7 +61,7 @@ //! ``` //! //! You can also pass arguments to your callback functions, just like you would in a regular Bevy system - using `In` structs with tuples: -//! ```rust +//! ```no_run //! use bevy::prelude::*; //! use bevy_scriptum::prelude::*; //! use bevy_scriptum::runtimes::lua::prelude::*; @@ -84,7 +84,7 @@ //! ``` //! It is also possible to split the definition of your callback functions up over multiple plugins. This enables you to split up your code by subject and keep the main initialization light and clean. //! This can be accomplished by using `add_scripting_api`. Be careful though, `add_scripting` has to be called before adding plugins. -//! ```rust +//! ```no_run //! use bevy::prelude::*; //! use bevy_scriptum::prelude::*; //! use bevy_scriptum::runtimes::lua::prelude::*; @@ -108,22 +108,22 @@ //! .add_plugins(MyPlugin) //! .run(); //! ``` -//! -//! +//! +//! //! ## Usage //! //! Add the following to your `Cargo.toml`: //! //! ```toml //! [dependencies] -//! bevy_scriptum = { version = "0.6", features = ["lua"] } +//! bevy_scriptum = { version = "0.7", features = ["lua"] } //! ``` //! //! or execute `cargo add bevy_scriptum --features lua` from your project directory. //! //! You can now start exposing functions to the scripting language. For example, you can expose a function that prints a message to the console: //! -//! ```rust +//! ```no_run //! use bevy::prelude::*; //! use bevy_scriptum::prelude::*; //! use bevy_scriptum::runtimes::lua::prelude::*; @@ -149,7 +149,7 @@ //! //! And spawn an entity with attached `Script` component with a handle to a script source file: //! -//! ```rust +//! ```no_run //! use bevy::prelude::*; //! use bevy_scriptum::prelude::*; //! use bevy_scriptum::runtimes::lua::prelude::*; @@ -189,6 +189,7 @@ //! //! | bevy version | bevy_scriptum version | //! |--------------|-----------------------| +//! | 0.15 | 0.7 | //! | 0.14 | 0.6 | //! | 0.13 | 0.4-0.5 | //! | 0.12 | 0.3 | @@ -269,6 +270,7 @@ use self::{ systems::{process_new_scripts, reload_scripts}, }; +#[cfg(any(feature = "rhai", feature = "lua"))] const ENTITY_VAR_NAME: &str = "entity"; /// An error that can occur when internal [ScriptingPlugin] systems are being executed @@ -387,7 +389,10 @@ impl<'a, R: Runtime> ScriptingRuntimeBuilder<'a, R> { self, name: String, fun: impl IntoCallbackSystem, - ) -> Self { + ) -> Self + where + In: SystemInput, + { let system = fun.into_callback_system(self.world); let mut callbacks_resource = self.world.resource_mut::>(); diff --git a/src/promise.rs b/src/promise.rs index d7bacc9..6b84a63 100644 --- a/src/promise.rs +++ b/src/promise.rs @@ -58,6 +58,7 @@ impl Promise { } /// Register a callback that will be called when the [Promise] is resolved. + #[cfg(any(feature = "rhai", feature = "lua"))] pub(crate) fn then(&mut self, callback: V) -> Self { let mut inner = self .inner diff --git a/tests/tests.rs b/tests/tests.rs index 1320204..6d5375c 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,12 +1,17 @@ +#[cfg(any(feature = "rhai", feature = "lua"))] use std::sync::OnceLock; +#[cfg(any(feature = "rhai", feature = "lua"))] use bevy::ecs::system::RunSystemOnce as _; +#[cfg(any(feature = "rhai", feature = "lua"))] use bevy::prelude::*; +#[cfg(any(feature = "rhai", feature = "lua"))] use bevy_scriptum::{prelude::*, FuncArgs, Runtime}; -use mlua::Table; +#[cfg(any(feature = "rhai", feature = "lua"))] static TRACING_SUBSCRIBER: OnceLock<()> = OnceLock::new(); +#[cfg(any(feature = "rhai", feature = "lua"))] fn build_test_app() -> App { let mut app = App::new(); @@ -20,6 +25,7 @@ fn build_test_app() -> App { app } +#[cfg(any(feature = "rhai", feature = "lua"))] fn run_script( app: &mut App, path: String, @@ -30,12 +36,13 @@ fn run_script( let entity_id = app.world_mut().spawn(Script::new(asset)).id(); app.update(); // let `ScriptData` resources be added to entities - app.world_mut().run_system_once(system); + app.world_mut().run_system_once(system).unwrap(); app.update(); // let callbacks be executed entity_id } +#[cfg(any(feature = "rhai", feature = "lua"))] fn call_script_on_update_from_rust( mut scripted_entities: Query<(Entity, &mut R::ScriptData)>, scripting_runtime: ResMut, @@ -48,6 +55,7 @@ fn call_script_on_update_from_rust( .unwrap(); } +#[cfg(any(feature = "rhai", feature = "lua"))] trait AssertStateKeyValue { type ScriptData; fn assert_state_key_value_i64(world: &World, entity_id: Entity, key: &str, value: i64); @@ -55,6 +63,7 @@ trait AssertStateKeyValue { fn assert_state_key_value_string(world: &World, entity_id: Entity, key: &str, value: &str); } +#[cfg(any(feature = "rhai", feature = "lua"))] macro_rules! scripting_tests { ($runtime:ty, $script:literal, $extension:literal) => { use super::*; @@ -357,9 +366,11 @@ macro_rules! scripting_tests { call_script_on_update_from_rust::<$runtime>, ); - app.world_mut().run_system_once(|tagged: Query<&MyTag>| { - tagged.single(); - }); + app.world_mut() + .run_system_once(|tagged: Query<&MyTag>| { + tagged.single(); + }) + .unwrap(); } #[test] @@ -434,6 +445,7 @@ mod rhai_tests { mod lua_tests { use bevy::prelude::*; use bevy_scriptum::runtimes::lua::prelude::*; + use mlua::Table; impl AssertStateKeyValue for LuaRuntime { type ScriptData = LuaScriptData;