Update bevy 0.15 (#36)

* Update bevy 0.15

* Removed unused import "AsyncReadExt"

* Update README.md

* Update lib.rs

* Update README.md

* Fix cargo test by not running examples that never end in lib.rs

* get rid of warnings

* update version references

* dont run examples

* deny warnings

* remove obsolete runners

---------

Co-authored-by: Jarosław Konik <konikjar@gmail.com>
This commit is contained in:
GolDNenex 2025-04-15 21:27:08 +02:00 committed by GitHub
parent a718aa7ce6
commit f9e872e111
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 120 additions and 66 deletions

View file

@ -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

View file

@ -1,7 +1,7 @@
[package]
name = "bevy_scriptum"
authors = ["Jaroslaw Konik <konikjar@gmail.com>"]
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"

View file

@ -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::<LuaRuntime>(|runtime| {
runtime.add_function(String::from("hello_from_my_plugin"), || {
info!("Hello from MyPlugin");
});
});
}
}
App::new()
.add_plugins(DefaultPlugins)
.add_scripting::<LuaRuntime>(|_| {
// 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 |

View file

@ -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

View file

@ -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 |

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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<A: Asset + From<String> + GetExtensions> AssetLoader for ScriptLoader<A> {
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<Output = Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();

View file

@ -6,7 +6,7 @@ use crate::{promise::Promise, Runtime};
/// A system that can be used to call a script function.
pub struct CallbackSystem<R: Runtime> {
pub(crate) system: Box<dyn System<In = Vec<R::Value>, Out = R::Value>>,
pub(crate) system: Box<dyn System<In = In<Vec<R::Value>>, Out = R::Value>>,
pub(crate) arg_types: Vec<TypeId>,
}
@ -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<R: Runtime, In, Out, Marker>: IntoSystem<In, Out, Marker> {
pub trait IntoCallbackSystem<R: Runtime, In, Out, Marker>: IntoSystem<In, Out, Marker>
where
In: SystemInput,
{
/// Convert this function into a [CallbackSystem].
#[must_use]
fn into_callback_system(self, world: &mut World) -> CallbackSystem<R>;
@ -87,10 +90,10 @@ where
macro_rules! impl_tuple {
($($idx:tt $t:tt),+) => {
impl<RN: Runtime, $($t,)+ Out, FN, Marker> IntoCallbackSystem<RN, ($($t,)+), Out, Marker>
impl<RN: Runtime, $($t,)+ Out, FN, Marker> IntoCallbackSystem<RN, In<($($t,)+)>, Out, Marker>
for FN
where
FN: IntoSystem<($($t,)+), Out, Marker>,
FN: IntoSystem<In<($($t,)+)>, Out, Marker>,
Out: for<'a> IntoRuntimeValueWithEngine<'a, Out, RN>,
$($t: 'static + for<'a> FromRuntimeValueWithEngine<'a, RN>,)+
{

View file

@ -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::*;
@ -116,14 +116,14 @@
//!
//! ```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<R, In, Out, Marker>,
) -> Self {
) -> Self
where
In: SystemInput,
{
let system = fun.into_callback_system(self.world);
let mut callbacks_resource = self.world.resource_mut::<Callbacks<R>>();

View file

@ -58,6 +58,7 @@ impl<C: Clone + Send + 'static, V: Send + Clone> Promise<C, V> {
}
/// 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

View file

@ -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<R: Runtime, Out, Marker>(
app: &mut App,
path: String,
@ -30,12 +36,13 @@ fn run_script<R: Runtime, Out, Marker>(
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<R: Runtime>(
mut scripted_entities: Query<(Entity, &mut R::ScriptData)>,
scripting_runtime: ResMut<R>,
@ -48,6 +55,7 @@ fn call_script_on_update_from_rust<R: Runtime>(
.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>| {
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;