Skip to content

Commit d28d4aa

Browse files
committed
refactor
1 parent 9d3c3b8 commit d28d4aa

File tree

19 files changed

+759
-748
lines changed

19 files changed

+759
-748
lines changed

assets/tests/add_system/added_systems_run_in_parallel.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ digraph {
3939
node_14 [label="custom_system_b"];
4040
node_15 [label="SystemSet AssetEvents"];
4141
node_16 [label="SystemSet GarbageCollection"];
42-
node_17 [label="SystemSet StartPhase"];
42+
node_17 [label="SystemSet InitializePhase"];
4343
node_18 [label="SystemSet ScriptSystem(custom_system_a)"];
4444
node_19 [label="SystemSet ScriptSystem(custom_system_b)"];
4545
node_0 -> node_15 [color=red, label="child of", arrowhead=diamond];

assets/tests/add_system/added_systems_run_in_parallel.rhai

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ digraph {
3838
node_14 [label="custom_system_b"];
3939
node_15 [label="SystemSet AssetEvents"];
4040
node_16 [label="SystemSet GarbageCollection"];
41-
node_17 [label="SystemSet StartPhase"];
41+
node_17 [label="SystemSet InitializePhase"];
4242
node_18 [label="SystemSet ScriptSystem(custom_system_a)"];
4343
node_19 [label="SystemSet ScriptSystem(custom_system_b)"];
4444
node_0 -> node_15 [color=red, label="child of", arrowhead=diamond];

crates/bevy_mod_scripting_core/src/commands.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ use std::sync::Arc;
55
use crate::{
66
IntoScriptPluginParams, ScriptContext,
77
error::ScriptError,
8-
event::{CallbackLabel, ScriptCallbackResponseEvent},
8+
event::{
9+
CallbackLabel, ForPlugin, ScriptAttachedEvent, ScriptCallbackResponseEvent,
10+
ScriptDetachedEvent,
11+
},
912
extractors::CallContext,
1013
handler::{send_callback_response, send_script_errors},
14+
pipeline::RunProcessingPipelineOnce,
1115
script::{DisplayProxy, ScriptAttachment},
1216
};
1317
use bevy_ecs::{system::Command, world::World};
@@ -140,3 +144,39 @@ impl<P: IntoScriptPluginParams> Command for RunScriptCallback<P> {
140144
let _ = self.run(world);
141145
}
142146
}
147+
148+
/// Command which emits a [`ScriptAttachedEvent`] and then runs the processing pipeline to immediately process it.
149+
/// The end result is equivalent to attaching a script component or adding a static script and waiting for the normal pipeline to process it.
150+
pub struct AttachScript<P: IntoScriptPluginParams>(pub ForPlugin<ScriptAttachedEvent, P>);
151+
152+
impl<P: IntoScriptPluginParams> AttachScript<P> {
153+
/// Creates a new [`AttachScript`] command, which will create the given attachment, run expected callbacks, and
154+
pub fn new(attachment: ScriptAttachment) -> Self {
155+
Self(ForPlugin::new(ScriptAttachedEvent(attachment)))
156+
}
157+
}
158+
159+
/// Command which emits a [`ScriptDetachedEvent`] and then runs the processing pipeline to immediately process it.
160+
/// The end result is equivalent to detaching a script component or removing a static script and waiting for the normal pipeline to process it.
161+
pub struct DetachScript<P: IntoScriptPluginParams>(pub ForPlugin<ScriptDetachedEvent, P>);
162+
163+
impl<P: IntoScriptPluginParams> DetachScript<P> {
164+
/// Creates a new [`DetachScript`] command, which will create the given attachment, run all expected callbacks, and delete contexts if necessary.
165+
pub fn new(attachment: ScriptAttachment) -> Self {
166+
Self(ForPlugin::new(ScriptDetachedEvent(attachment)))
167+
}
168+
}
169+
170+
impl<P: IntoScriptPluginParams> Command for AttachScript<P> {
171+
fn apply(self, world: &mut World) {
172+
world.send_event(self.0);
173+
RunProcessingPipelineOnce::<P>::new().apply(world)
174+
}
175+
}
176+
177+
impl<P: IntoScriptPluginParams> Command for DetachScript<P> {
178+
fn apply(self, world: &mut World) {
179+
world.send_event(self.0);
180+
RunProcessingPipelineOnce::<P>::new().apply(world)
181+
}
182+
}

crates/bevy_mod_scripting_core/src/event.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Event handlers and event types for scripting.
22
3-
use std::sync::Arc;
3+
use std::{marker::PhantomData, sync::Arc};
44

55
use ::{bevy_asset::Handle, bevy_ecs::entity::Entity, bevy_reflect::Reflect};
66
use bevy_ecs::event::Event;
@@ -28,6 +28,62 @@ impl ScriptErrorEvent {
2828
}
2929
}
3030

31+
/// Emitted when a script is attached.
32+
#[derive(Event, Clone, Debug)]
33+
pub struct ScriptAttachedEvent(pub ScriptAttachment);
34+
35+
/// Emitted when a script is detached.
36+
#[derive(Event, Clone, Debug)]
37+
pub struct ScriptDetachedEvent(pub ScriptAttachment);
38+
39+
/// Emitted when a script asset is modified and all its attachments require re-loading
40+
#[derive(Event, Clone, Debug)]
41+
pub struct ScriptAssetModifiedEvent(pub ScriptId);
42+
43+
#[derive(Event)]
44+
/// Wrapper around a script event making it available to read by a specific plugin only
45+
pub struct ForPlugin<T, P: IntoScriptPluginParams>(T, PhantomData<fn(P)>);
46+
47+
impl<T: std::fmt::Debug, P: IntoScriptPluginParams> std::fmt::Debug for ForPlugin<T, P> {
48+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49+
f.debug_tuple("ForPlugin").field(&self.0).finish()
50+
}
51+
}
52+
53+
impl<T, P: IntoScriptPluginParams> From<T> for ForPlugin<T, P> {
54+
fn from(value: T) -> Self {
55+
Self::new(value)
56+
}
57+
}
58+
59+
impl<T: Clone, P: IntoScriptPluginParams> Clone for ForPlugin<T, P> {
60+
fn clone(&self) -> Self {
61+
Self(self.0.clone(), self.1)
62+
}
63+
}
64+
65+
impl<T, P: IntoScriptPluginParams> ForPlugin<T, P> {
66+
/// Creates a new wrapper for the specific plugin
67+
pub fn new(event: T) -> Self {
68+
Self(event, Default::default())
69+
}
70+
71+
/// Retrieves the inner event
72+
pub fn event(&self) -> &T {
73+
&self.0
74+
}
75+
76+
/// Retrieves the inner event mutably
77+
pub fn event_mut(&mut self) -> &mut T {
78+
&mut self.0
79+
}
80+
81+
/// Unpacks the inner event
82+
pub fn inner(self) -> T {
83+
self.0
84+
}
85+
}
86+
3187
/// A string which disallows common invalid characters in callback labels,
3288
/// particularly at the start of the string
3389
///

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
173173
app.insert_resource(ScriptContext::<P>::new(self.context_policy.clone()));
174174
app.register_asset_loader(ScriptAssetLoader::new(config.language_extensions));
175175

176-
app.add_plugins(ScriptLoadingPipeline::<P>::default());
176+
app.add_plugins(self.processing_pipeline_plugin.clone());
177177

178178
register_types(app);
179179
}

crates/bevy_mod_scripting_core/src/pipeline/finish.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)