From 4a5513973e8ebfc11e5a1ddedb3c974e2c1c7c38 Mon Sep 17 00:00:00 2001 From: ConnorBP Date: Sun, 4 Feb 2024 00:08:56 -0500 Subject: [PATCH 1/8] Initial refactor. Converting script load system to an exclusive system --- bevy_mod_scripting_core/src/hosts.rs | 63 ++++++- bevy_mod_scripting_core/src/systems.rs | 178 +++++++++++++------ languages/bevy_mod_scripting_lua/src/lib.rs | 9 + languages/bevy_mod_scripting_rhai/src/lib.rs | 4 + languages/bevy_mod_scripting_rune/src/lib.rs | 1 + 5 files changed, 195 insertions(+), 60 deletions(-) diff --git a/bevy_mod_scripting_core/src/hosts.rs b/bevy_mod_scripting_core/src/hosts.rs index 9e8ff585..c45977ce 100644 --- a/bevy_mod_scripting_core/src/hosts.rs +++ b/bevy_mod_scripting_core/src/hosts.rs @@ -1,11 +1,13 @@ //! All script host related stuff use bevy::{asset::Asset, ecs::schedule::ScheduleLabel, prelude::*}; +use std::ops::DerefMut; use std::{ collections::HashMap, iter::once, sync::atomic::{AtomicU32, Ordering}, }; +use crate::world::WorldPointerGuard; use crate::{ asset::CodeAsset, docs::DocFragment, @@ -73,6 +75,7 @@ pub trait ScriptHost: Send + Sync + 'static + Default + Resource { /// to send useful errors. fn load_script( &mut self, + world_ptr: WorldPointer, script: &[u8], script_data: &ScriptData, providers: &mut APIProviders, @@ -113,11 +116,24 @@ pub trait ScriptHost: Send + Sync + 'static + Default + Resource { }; let mut providers: APIProviders = world.remove_resource().unwrap(); - let mut ctx = self.load_script(script, &fd, &mut providers).unwrap(); + // safety: + // - we have &mut World access + // - we do not use the original reference again anywhere in this function after this was created + let world = unsafe { WorldPointerGuard::new(world) }; + let mut ctx = self + .load_script(world.clone(), script, &fd, &mut providers) + .unwrap(); self.setup_script(&fd, &mut ctx, &mut providers)?; let events = [event; 1]; - self.handle_events(world, &events, once((fd, &mut ctx)), &mut providers); + let mut world = world.write(); + + self.handle_events( + world.deref_mut(), + &events, + once((fd, &mut ctx)), + &mut providers, + ); world.insert_resource(providers); @@ -352,12 +368,12 @@ impl Script { /// reloads the script by deleting the old context and inserting a new one /// if the script context never existed, it will after this call. pub(crate) fn reload_script( + world: &mut World, host: &mut H, script: &Script, script_assets: &Assets, providers: &mut APIProviders, contexts: &mut ScriptContexts, - event_writer: &mut EventWriter, ) { debug!("reloading script {}", script.id); @@ -367,13 +383,13 @@ impl Script { contexts.remove_context(script.id()); // insert new re-loaded context Self::insert_new_script_context::( + world, host, script, entity, script_assets, providers, contexts, - event_writer, ); } else { // remove old context @@ -385,14 +401,19 @@ impl Script { /// sets up (`ScriptHost::setup_script`) and inserts its new context into the contexts resource /// otherwise inserts None. Sends ScriptLoaded event if the script was loaded pub(crate) fn insert_new_script_context( + world: &mut World, host: &mut H, new_script: &Script, entity: Entity, script_assets: &Assets, providers: &mut APIProviders, contexts: &mut ScriptContexts, - event_writer: &mut EventWriter, ) { + // safety: + // - we have &mut World access + // - we do not use the original reference again anywhere in this function + let world = unsafe { WorldPointerGuard::new(world) }; + let fd = ScriptData { sid: new_script.id(), entity, @@ -410,14 +431,19 @@ impl Script { }; debug!("Inserted script {:?}", fd); - match host.load_script(script.bytes(), &fd, providers) { + match host.load_script(world.clone(), script.bytes(), &fd, providers) { Ok(mut ctx) => { host.setup_script(&fd, &mut ctx, providers) .expect("Failed to setup script"); contexts.insert_context(fd, Some(ctx)); - event_writer.send(ScriptLoaded { - sid: new_script.id(), - }); + { + let mut world = world.write(); + world.resource_scope(|_, mut event_writer: Mut>| { + event_writer.send(ScriptLoaded { + sid: new_script.id(), + }); + }) + } } Err(e) => { warn! {"Error in loading script {}:\n{}", &new_script.name,e} @@ -429,6 +455,17 @@ impl Script { } } +/// Allows the script handles to be cloned along with the explicit bevy asset handle clone +impl Clone for Script { + fn clone(&self) -> Self { + Self { + handle: self.handle.clone(), + name: self.name.clone(), + id: self.id, + } + } +} + #[derive(Component, Debug, Reflect)] #[reflect(Component, Default)] /// The component storing many scripts. @@ -439,6 +476,14 @@ pub struct ScriptCollection { pub scripts: Vec>, } +impl Clone for ScriptCollection { + fn clone(&self) -> Self { + Self { + scripts: self.scripts.clone(), + } + } +} + impl Default for ScriptCollection { fn default() -> Self { Self { diff --git a/bevy_mod_scripting_core/src/systems.rs b/bevy_mod_scripting_core/src/systems.rs index 000f9ecd..519175f1 100644 --- a/bevy_mod_scripting_core/src/systems.rs +++ b/bevy_mod_scripting_core/src/systems.rs @@ -19,36 +19,47 @@ pub enum ScriptSystemSet { /// Handles creating contexts for new/modified scripts /// Scripts are likely not loaded instantly at this point, so most of the time /// this system simply inserts an empty context -pub fn script_add_synchronizer( - query: Query< - ( - Entity, - &ScriptCollection, - Ref>, - ), - Changed>, - >, - mut host: ResMut, - mut providers: ResMut>, - script_assets: Res>, - mut contexts: ResMut>, - mut event_writer: EventWriter, -) { +pub fn script_add_synchronizer(world: &mut World) { debug!("Handling addition/modification of scripts"); - query.for_each(|(entity, new_scripts, tracker)| { - if tracker.is_added() { - new_scripts.scripts.iter().for_each(|new_script| { + let mut state: CachedScriptLoadState = world.remove_resource().unwrap(); + + // Entity, + // &'static ScriptCollection, + // Ref<'static, ScriptCollection>, + + let script_assets: Assets = world.remove_resource().unwrap(); + let mut contexts: ScriptContexts = world.remove_resource().unwrap(); + let mut host: H = world.remove_resource().unwrap(); + let mut providers: APIProviders = world.remove_resource().unwrap(); + + let query: Vec<_> = { + let mut q = vec![]; + let changed = state.scripts_changed_query.get(world); + for (entity, new_scripts, tracker) in changed.iter() { + q.push(( + entity.clone(), + new_scripts.scripts.iter().cloned().collect::>(), + tracker.is_added(), + )) + } + q + }; + world.insert_resource(state); + + for (entity, new_scripts, tracker) in query.iter() { + if *tracker { + for new_script in new_scripts { Script::::insert_new_script_context::( + world, &mut host, - new_script, - entity, + &new_script, + *entity, &script_assets, &mut providers, &mut contexts, - &mut event_writer, ) - }) + } } else { // changed but structure already exists in contexts // find out what's changed @@ -58,14 +69,10 @@ pub fn script_add_synchronizer( let context_ids = contexts .context_entities .iter() - .filter_map(|(sid, (e, _, _))| if *e == entity { Some(sid) } else { None }) + .filter_map(|(sid, (e, _, _))| if e == entity { Some(sid) } else { None }) .cloned() .collect::>(); - let script_ids = new_scripts - .scripts - .iter() - .map(|s| s.id()) - .collect::>(); + let script_ids = new_scripts.iter().map(|s| s.id()).collect::>(); let removed_scripts = context_ids.difference(&script_ids); let added_scripts = script_ids.difference(&context_ids); @@ -75,19 +82,25 @@ pub fn script_add_synchronizer( } for a in added_scripts { - let script = new_scripts.scripts.iter().find(|e| &e.id() == a).unwrap(); + let script = new_scripts.iter().find(|e| &e.id() == a).unwrap(); Script::::insert_new_script_context::( + world, &mut host, script, - entity, + *entity, &script_assets, &mut providers, &mut contexts, - &mut event_writer, ) } } - }) + } + + // return ownership + world.insert_resource(script_assets); + world.insert_resource(contexts); + world.insert_resource(host); + world.insert_resource(providers); } /// Handles the removal of script components and their contexts @@ -112,44 +125,66 @@ pub fn script_remove_synchronizer( } /// Reloads hot-reloaded scripts, or loads missing contexts for scripts which were added but not loaded -pub fn script_hot_reload_handler( - mut events: EventReader>, - mut host: ResMut, - scripts: Query<&ScriptCollection>, - script_assets: Res>, - mut providers: ResMut>, - mut contexts: ResMut>, - mut event_writer: EventWriter, -) { - for e in events.iter() { - let (handle, created) = match e { - AssetEvent::Modified { handle } => (handle, false), - AssetEvent::Created { handle } => (handle, true), - _ => continue, - }; +pub fn script_hot_reload_handler(world: &mut World) { + let mut state: CachedScriptLoadState = world.remove_resource().unwrap(); + let events = { + state + .event_state + .get_mut(world) + .1 + .iter() + .filter_map(|e| match e { + AssetEvent::Modified { handle } => Some((handle.clone(), false)), + AssetEvent::Created { handle } => Some((handle.clone(), true)), + _ => None, + }) + .collect::>() + }; + // collect all asset events up front + // let events = events.iter().collect::>(); + // collect all scripts from query upfront + let scripts = state + .scripts_query + .get(world) + .iter() + .cloned() + .collect::>(); + + world.insert_resource(state); + + let script_assets: Assets = world.remove_resource().unwrap(); + let mut contexts: ScriptContexts = world.remove_resource().unwrap(); + let mut host: H = world.remove_resource().unwrap(); + let mut providers: APIProviders = world.remove_resource().unwrap(); + + for (handle, created) in events { // find script using this handle by handle id // whether this script was modified or created // if a script exists with this handle, we should reload it to load in a new context // which at this point will be either None or Some(outdated context) // both ways are fine for scripts in scripts.iter() { - for script in &scripts.scripts { + for script in scripts.scripts.iter() { // the script could have well loaded in the same frame that it was added // in that case it will have a context attached and we do not want to reload it - if script.handle() == handle && !(contexts.has_context(script.id()) && created) { + if script.handle() == &handle && !(contexts.has_context(script.id()) && created) { Script::::reload_script::( + world, &mut host, - script, + &script, &script_assets, &mut providers, &mut contexts, - &mut event_writer, ); } } } } + world.insert_resource(script_assets); + world.insert_resource(contexts); + world.insert_resource(host); + world.insert_resource(providers); } /// Lets the script host handle all script events @@ -224,3 +259,44 @@ impl FromWorld for CachedScriptState { } } } + +#[derive(Resource)] +/// system state for exclusive systems dealing with script load events +pub struct CachedScriptLoadState { + pub event_state: SystemState<( + EventWriter<'static, ScriptLoaded>, + EventReader<'static, 'static, AssetEvent>, + // Query<'static, 'static, + // ( + // Entity, + // &'static ScriptCollection, + // Ref<'static, ScriptCollection>, + // ), + // Changed>, + // > + )>, + pub scripts_query: + SystemState>>, + pub scripts_changed_query: SystemState< + Query< + 'static, + 'static, + ( + Entity, + &'static ScriptCollection, + Ref<'static, ScriptCollection>, + ), + Changed>, + >, + >, +} + +impl FromWorld for crate::systems::CachedScriptLoadState { + fn from_world(world: &mut World) -> Self { + Self { + event_state: SystemState::new(world), + scripts_query: SystemState::new(world), + scripts_changed_query: SystemState::new(world), + } + } +} diff --git a/languages/bevy_mod_scripting_lua/src/lib.rs b/languages/bevy_mod_scripting_lua/src/lib.rs index 8af061d5..1df39f58 100644 --- a/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/languages/bevy_mod_scripting_lua/src/lib.rs @@ -13,7 +13,9 @@ use tealr::mlu::mlua::{prelude::*, Function}; pub mod assets; pub mod docs; pub mod util; +use bevy_mod_scripting_core::world::WorldPointer; pub use tealr; + pub mod prelude { pub use crate::{ assets::{LuaFile, LuaLoader}, @@ -83,6 +85,7 @@ impl ScriptHost for LuaScriptHost { .add_asset::() .init_asset_loader::() .init_resource::>() + .init_resource::>() .init_resource::>() .init_resource::>() .register_type::>() @@ -104,6 +107,7 @@ impl ScriptHost for LuaScriptHost { fn load_script( &mut self, + world: WorldPointer, script: &[u8], script_data: &ScriptData, providers: &mut APIProviders, @@ -115,6 +119,11 @@ impl ScriptHost for LuaScriptHost { // init lua api before loading script let mut lua = Mutex::new(lua); + + providers + .setup_runtime_all(world.clone(), &script_data, &mut lua) + .expect("Could not setup script runtime"); + providers.attach_all(&mut lua)?; lua.get_mut() diff --git a/languages/bevy_mod_scripting_rhai/src/lib.rs b/languages/bevy_mod_scripting_rhai/src/lib.rs index 4b8751e2..195e3d8f 100644 --- a/languages/bevy_mod_scripting_rhai/src/lib.rs +++ b/languages/bevy_mod_scripting_rhai/src/lib.rs @@ -9,7 +9,9 @@ use std::marker::PhantomData; pub mod assets; pub mod docs; +use bevy_mod_scripting_core::world::WorldPointer; pub use rhai; + pub mod prelude { pub use crate::{ assets::{RhaiFile, RhaiLoader}, @@ -78,6 +80,7 @@ impl ScriptHost for RhaiScriptHost< .add_asset::() .init_asset_loader::() .init_resource::>() + .init_resource::>() .init_resource::>() .init_resource::>() .register_type::>() @@ -115,6 +118,7 @@ impl ScriptHost for RhaiScriptHost< fn load_script( &mut self, + world_pointer: WorldPointer, script: &[u8], script_data: &ScriptData, _: &mut APIProviders, diff --git a/languages/bevy_mod_scripting_rune/src/lib.rs b/languages/bevy_mod_scripting_rune/src/lib.rs index 5683bde5..c225ee96 100644 --- a/languages/bevy_mod_scripting_rune/src/lib.rs +++ b/languages/bevy_mod_scripting_rune/src/lib.rs @@ -146,6 +146,7 @@ impl ScriptHost for RuneScriptHost { fn load_script( &mut self, + world_ptr: &mut World, script: &[u8], script_data: &ScriptData, providers: &mut APIProviders, From f9636c08b0aa0c700731093be5b9a86536f6797a Mon Sep 17 00:00:00 2001 From: ConnorBP Date: Sun, 4 Feb 2024 00:53:22 -0500 Subject: [PATCH 2/8] fixed world_ptr type to match in rune lib --- languages/bevy_mod_scripting_rune/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/languages/bevy_mod_scripting_rune/src/lib.rs b/languages/bevy_mod_scripting_rune/src/lib.rs index c225ee96..7bc045cb 100644 --- a/languages/bevy_mod_scripting_rune/src/lib.rs +++ b/languages/bevy_mod_scripting_rune/src/lib.rs @@ -146,7 +146,7 @@ impl ScriptHost for RuneScriptHost { fn load_script( &mut self, - world_ptr: &mut World, + world_ptr: WorldPointer, script: &[u8], script_data: &ScriptData, providers: &mut APIProviders, From 4b5ad5ae289a5b0d6d2b486348a6ca4738476c4b Mon Sep 17 00:00:00 2001 From: ConnorBP Date: Sun, 4 Feb 2024 01:32:01 -0500 Subject: [PATCH 3/8] fixed clippy warnings --- bevy_mod_scripting_core/src/systems.rs | 8 ++++---- languages/bevy_mod_scripting_lua/src/lib.rs | 2 +- languages/bevy_mod_scripting_rhai/src/lib.rs | 2 +- languages/bevy_mod_scripting_rune/src/lib.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bevy_mod_scripting_core/src/systems.rs b/bevy_mod_scripting_core/src/systems.rs index 519175f1..c52b481b 100644 --- a/bevy_mod_scripting_core/src/systems.rs +++ b/bevy_mod_scripting_core/src/systems.rs @@ -38,8 +38,8 @@ pub fn script_add_synchronizer(world: &mut World) { let changed = state.scripts_changed_query.get(world); for (entity, new_scripts, tracker) in changed.iter() { q.push(( - entity.clone(), - new_scripts.scripts.iter().cloned().collect::>(), + entity, + new_scripts.scripts.to_vec(), tracker.is_added(), )) } @@ -53,7 +53,7 @@ pub fn script_add_synchronizer(world: &mut World) { Script::::insert_new_script_context::( world, &mut host, - &new_script, + new_script, *entity, &script_assets, &mut providers, @@ -172,7 +172,7 @@ pub fn script_hot_reload_handler(world: &mut World) { Script::::reload_script::( world, &mut host, - &script, + script, &script_assets, &mut providers, &mut contexts, diff --git a/languages/bevy_mod_scripting_lua/src/lib.rs b/languages/bevy_mod_scripting_lua/src/lib.rs index 1df39f58..ea99d40d 100644 --- a/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/languages/bevy_mod_scripting_lua/src/lib.rs @@ -121,7 +121,7 @@ impl ScriptHost for LuaScriptHost { let mut lua = Mutex::new(lua); providers - .setup_runtime_all(world.clone(), &script_data, &mut lua) + .setup_runtime_all(world.clone(), script_data, &mut lua) .expect("Could not setup script runtime"); providers.attach_all(&mut lua)?; diff --git a/languages/bevy_mod_scripting_rhai/src/lib.rs b/languages/bevy_mod_scripting_rhai/src/lib.rs index 195e3d8f..c6606dae 100644 --- a/languages/bevy_mod_scripting_rhai/src/lib.rs +++ b/languages/bevy_mod_scripting_rhai/src/lib.rs @@ -118,7 +118,7 @@ impl ScriptHost for RhaiScriptHost< fn load_script( &mut self, - world_pointer: WorldPointer, + _world_pointer: WorldPointer, script: &[u8], script_data: &ScriptData, _: &mut APIProviders, diff --git a/languages/bevy_mod_scripting_rune/src/lib.rs b/languages/bevy_mod_scripting_rune/src/lib.rs index 7bc045cb..4eeae02e 100644 --- a/languages/bevy_mod_scripting_rune/src/lib.rs +++ b/languages/bevy_mod_scripting_rune/src/lib.rs @@ -146,7 +146,7 @@ impl ScriptHost for RuneScriptHost { fn load_script( &mut self, - world_ptr: WorldPointer, + _world_ptr: WorldPointer, script: &[u8], script_data: &ScriptData, providers: &mut APIProviders, From 74ffa33fcb2a649695f75cf42d5a5799ff007e8a Mon Sep 17 00:00:00 2001 From: ConnorBP Date: Mon, 5 Feb 2024 14:25:03 -0500 Subject: [PATCH 4/8] added error event forwarding to load_script --- bevy_mod_scripting_core/src/event.rs | 20 +++++++++++ languages/bevy_mod_scripting_lua/src/lib.rs | 37 ++++++++++----------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/bevy_mod_scripting_core/src/event.rs b/bevy_mod_scripting_core/src/event.rs index 7ba34707..a69723f0 100644 --- a/bevy_mod_scripting_core/src/event.rs +++ b/bevy_mod_scripting_core/src/event.rs @@ -1,6 +1,10 @@ +use bevy::log::error; use bevy::prelude::Event; use crate::{error::ScriptError, hosts::Recipients}; +use crate::hosts::ScriptHost; +use crate::systems::CachedScriptState; +use crate::world::WorldPointer; /// An error coming from a script #[derive(Debug, Event)] @@ -20,3 +24,19 @@ pub trait ScriptEvent: Send + Sync + Clone + Event + 'static { /// Retrieves the recipient scripts for this event fn recipients(&self) -> &Recipients; } + +pub fn write_error_event_with_world(world: WorldPointer, script_name: String, error_text: String) { + let mut world = world.write(); + let mut state: CachedScriptState = world.remove_resource().unwrap(); + + let (_, mut error_wrt, _) = state.event_state.get_mut(&mut world); + + let error = ScriptError::RuntimeError { + script: script_name, + msg: error_text, + }; + + error!("{}", error); + error_wrt.send(ScriptErrorEvent { error }); + world.insert_resource(state); +} \ No newline at end of file diff --git a/languages/bevy_mod_scripting_lua/src/lib.rs b/languages/bevy_mod_scripting_lua/src/lib.rs index ea99d40d..38a130e3 100644 --- a/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/languages/bevy_mod_scripting_lua/src/lib.rs @@ -15,6 +15,7 @@ pub mod docs; pub mod util; use bevy_mod_scripting_core::world::WorldPointer; pub use tealr; +use bevy_mod_scripting_core::event::write_error_event_with_world; pub mod prelude { pub use crate::{ @@ -124,19 +125,27 @@ impl ScriptHost for LuaScriptHost { .setup_runtime_all(world.clone(), script_data, &mut lua) .expect("Could not setup script runtime"); - providers.attach_all(&mut lua)?; + { + providers.attach_all(&mut lua)?; + } lua.get_mut() - .map_err(|e| ScriptError::FailedToLoad { - script: script_data.name.to_owned(), - msg: e.to_string(), + .map_err(|e| { + write_error_event_with_world::(world.clone(), script_data.name.to_owned(),e.to_string()); + ScriptError::FailedToLoad { + script: script_data.name.to_owned(), + msg: e.to_string(), + } })? .load(script) .set_name(script_data.name) .exec() - .map_err(|e| ScriptError::FailedToLoad { - script: script_data.name.to_owned(), - msg: e.to_string(), + .map_err(|e| { + write_error_event_with_world::(world.clone(), script_data.name.to_owned(),e.to_string()); + ScriptError::FailedToLoad { + script: script_data.name.to_owned(), + msg: e.to_string(), + } })?; Ok(lua) @@ -187,19 +196,7 @@ impl ScriptHost for LuaScriptHost { }; if let Err(error) = f.call::<_, ()>(event.args.clone()) { - let mut world = world.write(); - let mut state: CachedScriptState = world.remove_resource().unwrap(); - - let (_, mut error_wrt, _) = state.event_state.get_mut(&mut world); - - let error = ScriptError::RuntimeError { - script: script_data.name.to_owned(), - msg: error.to_string(), - }; - - error!("{}", error); - error_wrt.send(ScriptErrorEvent { error }); - world.insert_resource(state); + write_error_event_with_world::(world.clone(), script_data.name.to_owned(),error.to_string()); } } }); From 51d4d2ea118aa75a9dede0fc22318a1cc7ecc4d0 Mon Sep 17 00:00:00 2001 From: makspll Date: Tue, 6 Feb 2024 14:33:02 +0000 Subject: [PATCH 5/8] fix world being overridden on load --- languages/bevy_mod_scripting_lua/src/lib.rs | 26 +++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/languages/bevy_mod_scripting_lua/src/lib.rs b/languages/bevy_mod_scripting_lua/src/lib.rs index 38a130e3..012a26ca 100644 --- a/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/languages/bevy_mod_scripting_lua/src/lib.rs @@ -13,9 +13,9 @@ use tealr::mlu::mlua::{prelude::*, Function}; pub mod assets; pub mod docs; pub mod util; +use bevy_mod_scripting_core::event::write_error_event_with_world; use bevy_mod_scripting_core::world::WorldPointer; pub use tealr; -use bevy_mod_scripting_core::event::write_error_event_with_world; pub mod prelude { pub use crate::{ @@ -129,9 +129,19 @@ impl ScriptHost for LuaScriptHost { providers.attach_all(&mut lua)?; } + // We do this twice to get around the issue of attach_all overriding values here for the sake of + // documenting, TODO: this is messy, shouldn't be a problem but it's messy + providers + .setup_runtime_all(world.clone(), script_data, &mut lua) + .expect("Could not setup script runtime"); + lua.get_mut() .map_err(|e| { - write_error_event_with_world::(world.clone(), script_data.name.to_owned(),e.to_string()); + write_error_event_with_world::( + world.clone(), + script_data.name.to_owned(), + e.to_string(), + ); ScriptError::FailedToLoad { script: script_data.name.to_owned(), msg: e.to_string(), @@ -141,7 +151,11 @@ impl ScriptHost for LuaScriptHost { .set_name(script_data.name) .exec() .map_err(|e| { - write_error_event_with_world::(world.clone(), script_data.name.to_owned(),e.to_string()); + write_error_event_with_world::( + world.clone(), + script_data.name.to_owned(), + e.to_string(), + ); ScriptError::FailedToLoad { script: script_data.name.to_owned(), msg: e.to_string(), @@ -196,7 +210,11 @@ impl ScriptHost for LuaScriptHost { }; if let Err(error) = f.call::<_, ()>(event.args.clone()) { - write_error_event_with_world::(world.clone(), script_data.name.to_owned(),error.to_string()); + write_error_event_with_world::( + world.clone(), + script_data.name.to_owned(), + error.to_string(), + ); } } }); From 9343da63603eec25076c4b1ef11b4ee5b783325d Mon Sep 17 00:00:00 2001 From: ConnorBP Date: Tue, 6 Feb 2024 16:29:03 -0500 Subject: [PATCH 6/8] formatting cleanup --- bevy_mod_scripting_core/src/event.rs | 10 +++++++--- bevy_mod_scripting_core/src/systems.rs | 6 +----- bevy_script_api/src/lua/std.rs | 2 +- languages/bevy_mod_scripting_lua/src/lib.rs | 4 +--- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/bevy_mod_scripting_core/src/event.rs b/bevy_mod_scripting_core/src/event.rs index a69723f0..5f11c189 100644 --- a/bevy_mod_scripting_core/src/event.rs +++ b/bevy_mod_scripting_core/src/event.rs @@ -1,10 +1,10 @@ use bevy::log::error; use bevy::prelude::Event; -use crate::{error::ScriptError, hosts::Recipients}; use crate::hosts::ScriptHost; use crate::systems::CachedScriptState; use crate::world::WorldPointer; +use crate::{error::ScriptError, hosts::Recipients}; /// An error coming from a script #[derive(Debug, Event)] @@ -25,7 +25,11 @@ pub trait ScriptEvent: Send + Sync + Clone + Event + 'static { fn recipients(&self) -> &Recipients; } -pub fn write_error_event_with_world(world: WorldPointer, script_name: String, error_text: String) { +pub fn write_error_event_with_world( + world: WorldPointer, + script_name: String, + error_text: String, +) { let mut world = world.write(); let mut state: CachedScriptState = world.remove_resource().unwrap(); @@ -39,4 +43,4 @@ pub fn write_error_event_with_world(world: WorldPointer, script_n error!("{}", error); error_wrt.send(ScriptErrorEvent { error }); world.insert_resource(state); -} \ No newline at end of file +} diff --git a/bevy_mod_scripting_core/src/systems.rs b/bevy_mod_scripting_core/src/systems.rs index c52b481b..eacbf058 100644 --- a/bevy_mod_scripting_core/src/systems.rs +++ b/bevy_mod_scripting_core/src/systems.rs @@ -37,11 +37,7 @@ pub fn script_add_synchronizer(world: &mut World) { let mut q = vec![]; let changed = state.scripts_changed_query.get(world); for (entity, new_scripts, tracker) in changed.iter() { - q.push(( - entity, - new_scripts.scripts.to_vec(), - tracker.is_added(), - )) + q.push((entity, new_scripts.scripts.to_vec(), tracker.is_added())) } q }; diff --git a/bevy_script_api/src/lua/std.rs b/bevy_script_api/src/lua/std.rs index 9d6dbd76..1cb5d4d2 100644 --- a/bevy_script_api/src/lua/std.rs +++ b/bevy_script_api/src/lua/std.rs @@ -6,7 +6,7 @@ use bevy_mod_scripting_lua::tealr; use bevy_mod_scripting_lua::tealr::ToTypename; use tealr::mlu::mlua::MetaMethod; -use tealr::mlu::TypedFunction; + use tealr::mlu::{ mlua::{self, FromLua, IntoLua, Lua, UserData, Value}, TealData, TealDataMethods, diff --git a/languages/bevy_mod_scripting_lua/src/lib.rs b/languages/bevy_mod_scripting_lua/src/lib.rs index 012a26ca..c5641073 100644 --- a/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/languages/bevy_mod_scripting_lua/src/lib.rs @@ -125,9 +125,7 @@ impl ScriptHost for LuaScriptHost { .setup_runtime_all(world.clone(), script_data, &mut lua) .expect("Could not setup script runtime"); - { - providers.attach_all(&mut lua)?; - } + providers.attach_all(&mut lua)?; // We do this twice to get around the issue of attach_all overriding values here for the sake of // documenting, TODO: this is messy, shouldn't be a problem but it's messy From d6c724727539440742fb4e5ed64faf4ec225fc21 Mon Sep 17 00:00:00 2001 From: ConnorBP Date: Tue, 6 Feb 2024 16:43:50 -0500 Subject: [PATCH 7/8] removed commented out query --- bevy_mod_scripting_core/src/systems.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/bevy_mod_scripting_core/src/systems.rs b/bevy_mod_scripting_core/src/systems.rs index eacbf058..de4d60cf 100644 --- a/bevy_mod_scripting_core/src/systems.rs +++ b/bevy_mod_scripting_core/src/systems.rs @@ -262,14 +262,6 @@ pub struct CachedScriptLoadState { pub event_state: SystemState<( EventWriter<'static, ScriptLoaded>, EventReader<'static, 'static, AssetEvent>, - // Query<'static, 'static, - // ( - // Entity, - // &'static ScriptCollection, - // Ref<'static, ScriptCollection>, - // ), - // Changed>, - // > )>, pub scripts_query: SystemState>>, From 89f7c6dd459583c0fe62da3289f3f82248ab93b6 Mon Sep 17 00:00:00 2001 From: ConnorBP Date: Tue, 6 Feb 2024 16:50:58 -0500 Subject: [PATCH 8/8] oops --- bevy_script_api/src/lua/std.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bevy_script_api/src/lua/std.rs b/bevy_script_api/src/lua/std.rs index 1cb5d4d2..9d6dbd76 100644 --- a/bevy_script_api/src/lua/std.rs +++ b/bevy_script_api/src/lua/std.rs @@ -6,7 +6,7 @@ use bevy_mod_scripting_lua::tealr; use bevy_mod_scripting_lua::tealr::ToTypename; use tealr::mlu::mlua::MetaMethod; - +use tealr::mlu::TypedFunction; use tealr::mlu::{ mlua::{self, FromLua, IntoLua, Lua, UserData, Value}, TealData, TealDataMethods,