Skip to content

Commit 367e38b

Browse files
committed
various fixes and tests
1 parent 359b682 commit 367e38b

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

assets/tests/lifecycle/default/entity_script/callback/scenario.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SetCurrentLanguage language="@this_script_language"
2-
InstallPlugin
2+
InstallPlugin miliseconds_budget=999999
33
SetupHandler OnTest=null, Update=null
44
SetupHandler OnTestPostUpdate=null, PostUpdate=null
55
SetupHandler Last=null, OnTestLast=null

assets/tests/lifecycle/default/static_script/callback/scenario.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SetCurrentLanguage language="@this_script_language"
2-
InstallPlugin
2+
InstallPlugin miliseconds_budget=999999
33
SetupHandler OnTest=null, Update=null
44
SetupHandler OnTestPostUpdate=null, PostUpdate=null
55
SetupHandler Last=null, OnTestLast=null

assets/tests/scenario.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SetCurrentLanguage language="@this_script_language"
2-
InstallPlugin
2+
InstallPlugin miliseconds_budget=999999
33
SetupHandler OnTest=null, Update=null
44
SetupHandler OnTestPostUpdate=null, PostUpdate=null
55
SetupHandler Last=null, OnTestLast=null

crates/bevy_mod_scripting_core/src/commands.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Commands for creating, updating and deleting scripts
22
3-
use std::sync::Arc;
3+
use std::{sync::Arc, time::Duration};
44

55
use crate::{
66
IntoScriptPluginParams, ScriptContext,
@@ -170,13 +170,13 @@ impl<P: IntoScriptPluginParams> DetachScript<P> {
170170
impl<P: IntoScriptPluginParams> Command for AttachScript<P> {
171171
fn apply(self, world: &mut World) {
172172
world.send_event(self.0);
173-
RunProcessingPipelineOnce::<P>::new().apply(world)
173+
RunProcessingPipelineOnce::<P>::new(Some(Duration::from_secs(9999))).apply(world)
174174
}
175175
}
176176

177177
impl<P: IntoScriptPluginParams> Command for DetachScript<P> {
178178
fn apply(self, world: &mut World) {
179179
world.send_event(self.0);
180-
RunProcessingPipelineOnce::<P>::new().apply(world)
180+
RunProcessingPipelineOnce::<P>::new(Some(Duration::from_secs(9999))).apply(world)
181181
}
182182
}

crates/bevy_mod_scripting_core/src/pipeline/mod.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,24 +267,41 @@ pub fn machine_ticker<P: IntoScriptPluginParams>(world: &mut World) {
267267
}
268268
/// A command which triggers the script processing pipeline to run once,
269269
/// causing outstanding attachment events to be processed
270-
pub struct RunProcessingPipelineOnce<P>(PhantomData<fn(P)>);
270+
pub struct RunProcessingPipelineOnce<P> {
271+
override_budget: Option<Duration>,
272+
_ph: PhantomData<fn(P)>,
273+
}
271274

272275
impl<P> RunProcessingPipelineOnce<P> {
273-
/// Creates a new [`RunProcessingPipelineOnce`] command for the given plugin
274-
pub fn new() -> Self {
275-
Self(Default::default())
276+
/// Creates a new [`RunProcessingPipelineOnce`] command for the given plugin.
277+
/// The budget override can be used to make sure all scripts in the pipeline get fully processed in one go
278+
pub fn new(override_budget: Option<Duration>) -> Self {
279+
Self {
280+
override_budget,
281+
_ph: PhantomData,
282+
}
276283
}
277284
}
278285

279286
impl<P> Default for RunProcessingPipelineOnce<P> {
280287
fn default() -> Self {
281-
Self::new()
288+
Self::new(None)
282289
}
283290
}
284291

285292
impl<P: IntoScriptPluginParams> Command for RunProcessingPipelineOnce<P> {
286293
fn apply(self, world: &mut World) {
294+
let mut last_setting = None;
295+
if let Some(override_budget) = self.override_budget {
296+
let mut machines = world.get_resource_or_init::<ActiveMachines<P>>();
297+
last_setting = Some(machines.budget);
298+
machines.budget = Some(override_budget)
299+
}
287300
world.run_schedule(ScriptProcessingSchedule::<P>::default());
301+
if let Some(last_setting) = last_setting {
302+
let mut machines = world.get_resource_or_init::<ActiveMachines<P>>();
303+
machines.budget = last_setting;
304+
}
288305
}
289306
}
290307

@@ -326,7 +343,7 @@ pub fn automatic_pipeline_runner<P: IntoScriptPluginParams>(world: &mut World) {
326343
.get_resource::<ActiveMachines<P>>()
327344
.is_some_and(|machines| machines.active_machines() > 0)
328345
{
329-
RunProcessingPipelineOnce::<P>::new().apply(world);
346+
RunProcessingPipelineOnce::<P>::new(None).apply(world);
330347
}
331348
}
332349

@@ -353,8 +370,12 @@ impl PipelineRun for App {
353370
#[cfg(test)]
354371
mod test {
355372
use bevy_asset::{AssetApp, AssetId, AssetPlugin};
356-
use bevy_ecs::{system::SystemState, world::FromWorld};
373+
use bevy_ecs::{entity::Entity, system::SystemState, world::FromWorld};
357374
use bevy_mod_scripting_asset::Language;
375+
use bevy_mod_scripting_bindings::ScriptValue;
376+
use test_utils::make_test_plugin;
377+
378+
use crate::config::{GetPluginThreadConfig, ScriptingPluginConfiguration};
358379

359380
use super::*;
360381
#[test]
@@ -404,4 +425,25 @@ mod test {
404425
assert!(loaded.is_empty())
405426
}
406427
}
428+
429+
make_test_plugin!(crate);
430+
431+
#[test]
432+
fn test_run_override_is_undid() {
433+
let mut app = App::default();
434+
app.add_event::<ScriptAttachedEvent>();
435+
436+
app.add_plugins((AssetPlugin::default(), TestPlugin::default()));
437+
app.init_asset::<ScriptAsset>();
438+
let mut machines = ActiveMachines::<TestPlugin>::default();
439+
machines.budget = None;
440+
app.insert_resource(machines);
441+
app.finish();
442+
443+
let world = app.world_mut();
444+
RunProcessingPipelineOnce::<TestPlugin>::new(Some(Duration::from_secs(1))).apply(world);
445+
446+
let machines = world.get_resource::<ActiveMachines<TestPlugin>>().unwrap();
447+
assert_eq!(machines.budget, None);
448+
}
407449
}

0 commit comments

Comments
 (0)