Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pr-titles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
docs
refactor
perf
changed

scopes: |
ladfile
Expand Down
14 changes: 2 additions & 12 deletions crates/bevy_mod_scripting_core/src/bindings/script_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
error::{InteropError, ScriptError},
event::CallbackLabel,
extractors::get_all_access_ids,
handler::CallbackSettings,
handler::ScriptingHandler,
runtime::RuntimeContainer,
script::{ScriptAttachment, ScriptContext},
IntoScriptPluginParams,
Expand Down Expand Up @@ -200,7 +200,6 @@ impl ScriptSystemBuilder {

struct DynamicHandlerContext<'w, P: IntoScriptPluginParams> {
script_context: &'w ScriptContext<P>,
callback_settings: &'w CallbackSettings<P>,
context_loading_settings: &'w ContextLoadingSettings<P>,
runtime_container: &'w RuntimeContainer<P>,
}
Expand All @@ -215,17 +214,13 @@ impl<'w, P: IntoScriptPluginParams> DynamicHandlerContext<'w, P> {
let mut access = FilteredAccess::<ComponentId>::matches_nothing();
// let scripts_res_id = world
// .query::<&Script<P>>();
let callback_settings_res_id = world
.resource_id::<CallbackSettings<P>>()
.expect("CallbackSettings resource not found");
let context_loading_settings_res_id = world
.resource_id::<ContextLoadingSettings<P>>()
.expect("ContextLoadingSettings resource not found");
let runtime_container_res_id = world
.resource_id::<RuntimeContainer<P>>()
.expect("RuntimeContainer resource not found");

access.add_resource_read(callback_settings_res_id);
access.add_resource_read(context_loading_settings_res_id);
access.add_resource_read(runtime_container_res_id);

Expand All @@ -240,9 +235,6 @@ impl<'w, P: IntoScriptPluginParams> DynamicHandlerContext<'w, P> {
unsafe {
Self {
script_context: system.get_resource().expect("Scripts resource not found"),
callback_settings: system
.get_resource()
.expect("CallbackSettings resource not found"),
context_loading_settings: system
.get_resource()
.expect("ContextLoadingSettings resource not found"),
Expand All @@ -268,16 +260,14 @@ impl<'w, P: IntoScriptPluginParams> DynamicHandlerContext<'w, P> {
};

// call the script
let handler = self.callback_settings.callback_handler;
let pre_handling_initializers = &self
.context_loading_settings
.context_pre_handling_initializers;
let runtime = &self.runtime_container.runtime;

let mut context = context.lock();

CallbackSettings::<P>::call(
handler,
P::handle(
payload,
context_key,
label,
Expand Down
17 changes: 2 additions & 15 deletions crates/bevy_mod_scripting_core/src/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! These are designed to be used to pipe inputs into other systems which require them, while handling any configuration erorrs nicely.
#![allow(deprecated)]
use crate::bindings::pretty_print::DisplayWithWorld;
use crate::handler::ScriptingHandler;
use crate::{
bindings::{
access_map::ReflectAccessId, script_value::ScriptValue, WorldAccessGuard, WorldGuard,
},
context::ContextLoadingSettings,
error::{InteropError, ScriptError},
event::{CallbackLabel, IntoCallbackLabel},
handler::CallbackSettings,
runtime::RuntimeContainer,
script::{ScriptAttachment, ScriptContext, StaticScripts},
IntoScriptPluginParams,
Expand Down Expand Up @@ -122,8 +122,6 @@ unsafe impl<T: Resource + Default> SystemParam for ResScope<'_, T> {

/// Context for systems which handle events for scripts
pub struct HandlerContext<P: IntoScriptPluginParams> {
/// Settings for callbacks
pub(crate) callback_settings: CallbackSettings<P>,
/// Settings for loading contexts
pub(crate) context_loading_settings: ContextLoadingSettings<P>,
/// The runtime container
Expand All @@ -139,7 +137,6 @@ impl<P: IntoScriptPluginParams> HandlerContext<P> {
/// Every call to this function must be paired with a call to [`Self::release`].
pub fn yoink(world: &mut World) -> Self {
Self {
callback_settings: world.remove_resource().unwrap_or_default(),
context_loading_settings: world.remove_resource().unwrap_or_default(),
runtime_container: world.remove_resource().unwrap_or_default(),
static_scripts: world.remove_resource().unwrap_or_default(),
Expand All @@ -151,7 +148,6 @@ impl<P: IntoScriptPluginParams> HandlerContext<P> {
/// Only call this if you have previously yoinked the handler context from the world.
pub fn release(self, world: &mut World) {
// insert the handler context back into the world
world.insert_resource(self.callback_settings);
world.insert_resource(self.context_loading_settings);
world.insert_resource(self.runtime_container);
world.insert_resource(self.static_scripts);
Expand All @@ -165,24 +161,17 @@ impl<P: IntoScriptPluginParams> HandlerContext<P> {
pub fn destructure(
&mut self,
) -> (
&mut CallbackSettings<P>,
&mut ContextLoadingSettings<P>,
&mut RuntimeContainer<P>,
&mut StaticScripts,
) {
(
&mut self.callback_settings,
&mut self.context_loading_settings,
&mut self.runtime_container,
&mut self.static_scripts,
)
}

/// Get the callback settings
pub fn callback_settings(&mut self) -> &mut CallbackSettings<P> {
&mut self.callback_settings
}

/// Get the context loading settings
pub fn context_loading_settings(&mut self) -> &mut ContextLoadingSettings<P> {
&mut self.context_loading_settings
Expand Down Expand Up @@ -226,16 +215,14 @@ impl<P: IntoScriptPluginParams> HandlerContext<P> {
};

// call the script
let handler = self.callback_settings.callback_handler;
let pre_handling_initializers = &self
.context_loading_settings
.context_pre_handling_initializers;
let runtime = &self.runtime_container.runtime;

let mut context = context.lock();

CallbackSettings::<P>::call(
handler,
P::handle(
payload,
context_key,
label,
Expand Down
50 changes: 18 additions & 32 deletions crates/bevy_mod_scripting_core/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
use bevy::{
ecs::{
event::EventCursor,
resource::Resource,
system::{Local, SystemState},
world::{Mut, World},
},
Expand All @@ -35,39 +34,26 @@ pub type HandlerFn<P> = fn(
runtime: &<P as IntoScriptPluginParams>::R,
) -> Result<ScriptValue, ScriptError>;

/// A resource that holds the settings for the callback handler for a specific combination of type parameters
#[derive(Resource)]
pub struct CallbackSettings<P: IntoScriptPluginParams> {
/// The callback handler function
pub callback_handler: HandlerFn<P>,
}

impl<P: IntoScriptPluginParams> Default for CallbackSettings<P> {
fn default() -> Self {
Self {
callback_handler: |_, _, _, _, _, _| Ok(ScriptValue::Unit),
}
}
}

impl<P: IntoScriptPluginParams> Clone for CallbackSettings<P> {
fn clone(&self) -> Self {
Self {
callback_handler: self.callback_handler,
}
}
/// A utility trait, implemented for all types implementing `IntoScriptPluginParams`.
///
/// Calls the underlying handler function with the provided arguments and context.
/// Implementations will handle the necessary thread local context emplacement and retrieval.
pub trait ScriptingHandler<P: IntoScriptPluginParams> {
/// Calls the handler function with the given arguments and context
fn handle(
args: Vec<ScriptValue>,
context_key: &ScriptAttachment,
callback: &CallbackLabel,
script_ctxt: &mut P::C,
pre_handling_initializers: &[ContextPreHandlingInitializer<P>],
runtime: &P::R,
world: WorldGuard,
) -> Result<ScriptValue, ScriptError>;
}

#[profiling::all_functions]
impl<P: IntoScriptPluginParams> CallbackSettings<P> {
/// Creates a new callback settings resource with the given handler function
pub fn new(callback_handler: HandlerFn<P>) -> Self {
Self { callback_handler }
}

impl<P: IntoScriptPluginParams> ScriptingHandler<P> for P {
/// Calls the handler function while providing the necessary thread local context
pub fn call(
handler: HandlerFn<P>,
fn handle(
args: Vec<ScriptValue>,
context_key: &ScriptAttachment,
callback: &CallbackLabel,
Expand All @@ -78,7 +64,7 @@ impl<P: IntoScriptPluginParams> CallbackSettings<P> {
) -> Result<ScriptValue, ScriptError> {
WorldGuard::with_existing_static_guard(world.clone(), |world| {
ThreadWorldContainer.set_world(world)?;
(handler)(
Self::handler()(
args,
context_key,
callback,
Expand Down
13 changes: 5 additions & 8 deletions crates/bevy_mod_scripting_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use context::{
};
use error::ScriptError;
use event::{ScriptCallbackEvent, ScriptCallbackResponseEvent, ScriptEvent};
use handler::{CallbackSettings, HandlerFn};
use handler::HandlerFn;
use runtime::{initialize_runtime, Runtime, RuntimeContainer, RuntimeInitializer, RuntimeSettings};
use script::{ContextPolicy, ScriptComponent, ScriptContext, StaticScripts};

Expand Down Expand Up @@ -70,14 +70,16 @@ pub trait IntoScriptPluginParams: 'static {

/// Build the runtime
fn build_runtime() -> Self::R;

/// Returns the handler function for the plugin
fn handler() -> HandlerFn<Self>;
}

/// Bevy plugin enabling scripting within the bevy mod scripting framework
pub struct ScriptingPlugin<P: IntoScriptPluginParams> {
/// Settings for the runtime
pub runtime_settings: RuntimeSettings<P>,
/// The handler used for executing callbacks in scripts
pub callback_handler: HandlerFn<P>,

/// The context builder for loading contexts
pub context_builder: ContextBuilder<P>,

Expand All @@ -103,7 +105,6 @@ where
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ScriptingPlugin")
.field("callback_handler", &self.callback_handler)
.field("context_policy", &self.context_policy)
.field("language", &self.language)
.field("context_initializers", &self.context_initializers)
Expand All @@ -120,7 +121,6 @@ impl<P: IntoScriptPluginParams> Default for ScriptingPlugin<P> {
fn default() -> Self {
Self {
runtime_settings: Default::default(),
callback_handler: CallbackSettings::<P>::default().callback_handler,
context_builder: Default::default(),
context_policy: ContextPolicy::default(),
language: Default::default(),
Expand All @@ -138,9 +138,6 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
.insert_resource::<RuntimeContainer<P>>(RuntimeContainer {
runtime: P::build_runtime(),
})
.insert_resource::<CallbackSettings<P>>(CallbackSettings {
callback_handler: self.callback_handler,
})
.insert_resource::<ContextLoadingSettings<P>>(ContextLoadingSettings {
loader: self.context_builder.clone(),
context_initializers: self.context_initializers.clone(),
Expand Down
5 changes: 4 additions & 1 deletion crates/languages/bevy_mod_scripting_lua/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl IntoScriptPluginParams for LuaScriptingPlugin {
const LANGUAGE: Language = Language::Lua;

fn build_runtime() -> Self::R {}

fn handler() -> bevy_mod_scripting_core::handler::HandlerFn<Self> {
lua_handler
}
}

// necessary for automatic config goodies
Expand All @@ -54,7 +58,6 @@ impl Default for LuaScriptingPlugin {
LuaScriptingPlugin {
scripting_plugin: ScriptingPlugin {
runtime_settings: RuntimeSettings::default(),
callback_handler: lua_handler,
context_builder: ContextBuilder::<LuaScriptingPlugin> {
load: lua_context_load,
reload: lua_context_reload,
Expand Down
5 changes: 4 additions & 1 deletion crates/languages/bevy_mod_scripting_rhai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ impl IntoScriptPluginParams for RhaiScriptingPlugin {
fn build_runtime() -> Self::R {
Engine::new().into()
}

fn handler() -> bevy_mod_scripting_core::handler::HandlerFn<Self> {
rhai_callback_handler
}
}

/// The rhai scripting plugin. Used to add rhai scripting to a bevy app within the context of the BMS framework.
Expand Down Expand Up @@ -79,7 +83,6 @@ impl Default for RhaiScriptingPlugin {
Ok(())
}],
},
callback_handler: rhai_callback_handler,
context_builder: ContextBuilder {
load: rhai_context_load,
reload: rhai_context_reload,
Expand Down
10 changes: 10 additions & 0 deletions crates/testing_crates/test_utils/src/test_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ macro_rules! make_test_plugin {
invocations: vec![].into(),
}
}

fn handler() -> $ident::HandlerFn<Self> {
(|args, context_key, callback, script_ctxt, pre_handling_initializers, runtime| {
runtime
.invocations
.lock()
.push((context_key.entity(), Some(context_key.script().id())));
Ok($ident::bindings::script_value::ScriptValue::Unit)
}) as $ident::HandlerFn<Self>
}
}

#[derive(Default, std::fmt::Debug)]
Expand Down
3 changes: 2 additions & 1 deletion release-plz.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ commit_parsers = [
{ message = "^chore.*", skip = true },
{ message = "^test.*", skip = true },
{ message = "^docs.*", skip = true },
{ message = "^.*SKIP_CHANGELOG.*$", skip = true},
{ message = "^.*SKIP_CHANGELOG.*$", skip = true },
{ message = "^feat", group = "added" },
{ message = "^changed", group = "changed" },
{ message = "^deprecated", group = "deprecated" },
{ message = "^refactor", group = "refactored" },
{ message = "^fix", group = "fixed" },
{ message = "^security", group = "security" },
{ message = "^.*", group = "other" },
Expand Down
Loading