Skip to content

Commit d26080b

Browse files
committed
formatting
1 parent 61c3321 commit d26080b

File tree

7 files changed

+208
-74
lines changed

7 files changed

+208
-74
lines changed

crates/bevy_mod_scripting_core/src/bindings/access_map.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ impl DynamicSystemMeta for AccessMap {
350350

351351
#[track_caller]
352352
fn claim_read_access<K: AccessMapKey>(&self, key: K) -> bool {
353-
354353
let mut inner = self.0.lock();
355354

356355
if !inner.global_lock.can_write() {
@@ -360,10 +359,9 @@ impl DynamicSystemMeta for AccessMap {
360359
let key = key.as_index();
361360
if key == GLOBAL_KEY {
362361
bevy::log::error!("Trying to claim read access to global key, this is not allowed");
363-
return false
362+
return false;
364363
}
365364

366-
367365
let entry = inner.individual_accesses.entry(key).or_default();
368366
if entry.can_read() {
369367
entry.read_by.push(ClaimOwner {
@@ -387,7 +385,7 @@ impl DynamicSystemMeta for AccessMap {
387385
let key = key.as_index();
388386
if key == GLOBAL_KEY {
389387
bevy::log::error!("Trying to claim write access to global key, this is not allowed");
390-
return false
388+
return false;
391389
}
392390

393391
let entry = inner.individual_accesses.entry(key).or_default();
@@ -511,13 +509,17 @@ pub struct SubsetAccessMap {
511509

512510
impl SubsetAccessMap {
513511
/// Creates a new subset access map with the provided subset of ID's as well as a exception function.
514-
pub fn new(subset: impl IntoIterator<Item = impl AccessMapKey>, exception: impl Fn(u64) -> bool + Send + Sync + 'static) -> Self {
515-
let set = subset.into_iter().map(|k| k.as_index()).collect::<HashSet<_>>();
512+
pub fn new(
513+
subset: impl IntoIterator<Item = impl AccessMapKey>,
514+
exception: impl Fn(u64) -> bool + Send + Sync + 'static,
515+
) -> Self {
516+
let set = subset
517+
.into_iter()
518+
.map(|k| k.as_index())
519+
.collect::<HashSet<_>>();
516520
Self {
517521
inner: Default::default(),
518-
subset: Box::new(move |id| {
519-
set.contains(&id) || exception(id)
520-
}),
522+
subset: Box::new(move |id| set.contains(&id) || exception(id)),
521523
}
522524
}
523525

@@ -942,7 +944,7 @@ mod test {
942944
let access_map = AccessMap::default();
943945
let subset_access_map = SubsetAccessMap {
944946
inner: access_map,
945-
subset: Box::new(|id| id == 1),
947+
subset: Box::new(|id| id == 1),
946948
};
947949

948950
subset_access_map.claim_read_access(1);
@@ -972,7 +974,7 @@ mod test {
972974
let access_map = AccessMap::default();
973975
let subset_access_map = SubsetAccessMap {
974976
inner: access_map,
975-
subset: Box::new(|id| id == 1),
977+
subset: Box::new(|id| id == 1),
976978
};
977979

978980
subset_access_map.claim_write_access(1);
@@ -1050,7 +1052,7 @@ mod test {
10501052
let access_map = AccessMap::default();
10511053
let subset_access_map = SubsetAccessMap {
10521054
inner: access_map,
1053-
subset: Box::new(|id| id == 1 || id == 2 || id == 3),
1055+
subset: Box::new(|id| id == 1 || id == 2 || id == 3),
10541056
};
10551057

10561058
// Claim a read access outside the scope
@@ -1094,7 +1096,7 @@ mod test {
10941096
let access_map = AccessMap::default();
10951097
let subset_access_map = SubsetAccessMap {
10961098
inner: access_map,
1097-
subset: Box::new(|id| id == 0 || id == 1),
1099+
subset: Box::new(|id| id == 0 || id == 1),
10981100
};
10991101

11001102
subset_access_map.with_scope(|| {
@@ -1185,7 +1187,7 @@ mod test {
11851187
let access_map = AccessMap::default();
11861188
let subset_access_map = SubsetAccessMap {
11871189
inner: access_map,
1188-
subset: Box::new(|id| id == 0 || id == 1 || id == 2),
1190+
subset: Box::new(|id| id == 0 || id == 1 || id == 2),
11891191
};
11901192

11911193
assert!(subset_access_map.claim_global_access());
@@ -1210,7 +1212,7 @@ mod test {
12101212
let access_map = AccessMap::default();
12111213
let subset_access_map = SubsetAccessMap {
12121214
inner: access_map,
1213-
subset: Box::new(|id| id == 1),
1215+
subset: Box::new(|id| id == 1),
12141216
};
12151217

12161218
assert!(!subset_access_map.claim_read_access(2));

crates/bevy_mod_scripting_core/src/bindings/script_system.rs

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
//! everything to do with dynamically added script systems
22
33
use super::{
4-
access_map::ReflectAccessId, function::{from::Val, into::IntoScript, script_function::AppScriptFunctionRegistry}, schedule::AppScheduleRegistry, script_value::ScriptValue, AppReflectAllocator, ReflectBaseType, ReflectReference, ScriptQueryBuilder, ScriptQueryResult, ScriptResourceRegistration, WorldAccessGuard, WorldGuard
4+
access_map::ReflectAccessId,
5+
function::{from::Val, into::IntoScript, script_function::AppScriptFunctionRegistry},
6+
schedule::AppScheduleRegistry,
7+
script_value::ScriptValue,
8+
AppReflectAllocator, ReflectBaseType, ReflectReference, ScriptQueryBuilder, ScriptQueryResult,
9+
ScriptResourceRegistration, WorldAccessGuard, WorldGuard,
510
};
611
use crate::{
712
bindings::pretty_print::DisplayWithWorld,
@@ -28,8 +33,8 @@ use bevy::{
2833
reflect::{OffsetAccess, ParsedPath, Reflect},
2934
utils::hashbrown::HashSet,
3035
};
31-
use std::{any::TypeId, borrow::Cow, hash::Hash, marker::PhantomData, ops::Deref};
3236
use bevy_system_reflection::{ReflectSchedule, ReflectSystem};
37+
use std::{any::TypeId, borrow::Cow, hash::Hash, marker::PhantomData, ops::Deref};
3338
#[derive(Clone, Hash, PartialEq, Eq)]
3439
/// a system set for script systems.
3540
pub struct ScriptSystemSet(Cow<'static, str>);
@@ -390,8 +395,6 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
390395
fn has_deferred(&self) -> bool {
391396
false
392397
}
393-
394-
395398

396399
unsafe fn run_unsafe(
397400
&mut self,
@@ -410,20 +413,20 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
410413
};
411414

412415
let mut payload = Vec::with_capacity(state.system_params.len());
413-
414-
let guard = if self.exclusive {
415-
// safety: we are an exclusive system, therefore the cell allows us to do this
416-
let world = unsafe {world.world_mut()};
416+
417+
let guard = if self.exclusive {
418+
// safety: we are an exclusive system, therefore the cell allows us to do this
419+
let world = unsafe { world.world_mut() };
417420
WorldAccessGuard::new_exclusive(world)
418-
} else {
421+
} else {
419422
WorldAccessGuard::new_non_exclusive(
420423
world,
421424
state.subset.clone(),
422425
state.type_registry.clone(),
423426
state.allocator.clone(),
424427
state.function_registry.clone(),
425428
state.schedule_registry.clone(),
426-
)
429+
)
427430
};
428431

429432
// TODO: cache references which don't change once we have benchmarks
@@ -450,12 +453,19 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
450453
.map(|entity| {
451454
Val(ScriptQueryResult {
452455
entity,
453-
components: components.iter().map(|(component_id, type_id)| {
454-
ReflectReference {
455-
base: ReflectBaseType { type_id: *type_id, base_id: super::ReflectBase::Component(entity, *component_id) },
456+
components: components
457+
.iter()
458+
.map(|(component_id, type_id)| ReflectReference {
459+
base: ReflectBaseType {
460+
type_id: *type_id,
461+
base_id: super::ReflectBase::Component(
462+
entity,
463+
*component_id,
464+
),
465+
},
456466
reflect_path: Vec::<OffsetAccess>::default().into(),
457-
}
458-
}).collect(),
467+
})
468+
.collect(),
459469
})
460470
})
461471
.collect::<Vec<_>>();
@@ -526,9 +536,11 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
526536
subset.insert(raid);
527537
}
528538
ScriptSystemParamDescriptor::EntityQuery(query) => {
529-
let components: Vec<_> = query.components
539+
let components: Vec<_> = query
540+
.components
530541
.iter()
531-
.map(|c| (c.component_id, c.type_registration().type_id())).collect();
542+
.map(|c| (c.component_id, c.type_registration().type_id()))
543+
.collect();
532544
let query = query.as_query_state::<Entity>(world);
533545

534546
// Safety: we are not removing
@@ -550,7 +562,7 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
550562

551563
system_params.push(ScriptSystemParam::EntityQuery {
552564
query: query.into(),
553-
components
565+
components,
554566
});
555567
subset.extend(new_raids);
556568
}
@@ -628,12 +640,11 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
628640
fn default_system_sets(&self) -> Vec<bevy::ecs::schedule::InternedSystemSet> {
629641
vec![ScriptSystemSet::new(self.name.clone()).intern()]
630642
}
631-
643+
632644
fn type_id(&self) -> TypeId {
633645
TypeId::of::<Self>()
634646
}
635647

636-
637648
fn validate_param(&mut self, world: &World) -> bool {
638649
let world_cell = world.as_unsafe_world_cell_readonly();
639650
self.update_archetype_component_access(world_cell);
@@ -644,15 +655,18 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
644655
}
645656
}
646657

647-
648658
#[cfg(test)]
649659
mod test {
650-
use bevy::{app::{App, MainScheduleOrder, Update}, asset::AssetPlugin, diagnostic::DiagnosticsPlugin, ecs::schedule::{ScheduleLabel, Schedules}};
660+
use bevy::{
661+
app::{App, MainScheduleOrder, Update},
662+
asset::AssetPlugin,
663+
diagnostic::DiagnosticsPlugin,
664+
ecs::schedule::{ScheduleLabel, Schedules},
665+
};
651666
use test_utils::make_test_plugin;
652667

653668
use super::*;
654669

655-
656670
make_test_plugin!(crate);
657671

658672
fn test_system_rust(_world: &mut World) {}
@@ -663,41 +677,49 @@ mod test {
663677

664678
#[derive(ScheduleLabel, Clone, Debug, Hash, PartialEq, Eq)]
665679
struct TestSchedule;
666-
667-
app.add_plugins((AssetPlugin::default(), DiagnosticsPlugin,TestPlugin::default()));
680+
681+
app.add_plugins((
682+
AssetPlugin::default(),
683+
DiagnosticsPlugin,
684+
TestPlugin::default(),
685+
));
668686
app.init_schedule(TestSchedule);
669687
let mut main_schedule_order = app.world_mut().resource_mut::<MainScheduleOrder>();
670688
main_schedule_order.insert_after(Update, TestSchedule);
671689
app.add_systems(TestSchedule, test_system_rust);
672-
673-
690+
674691
// run the app once
675692
app.finish();
676693
app.cleanup();
677694
app.update();
678695

679696
// find existing rust system
680-
let test_system = app.world_mut().resource_scope::<Schedules,_>(|_, schedules| {
681-
let (node_id, system) = schedules.get(TestSchedule)
682-
.unwrap()
683-
.systems()
684-
.unwrap()
685-
.find(|(_, system)| {
686-
system.name().contains("test_system_rust")
687-
})
688-
.unwrap();
689-
690-
ReflectSystem::from_system(system.as_ref(), node_id)
691-
});
692-
693-
697+
let test_system = app
698+
.world_mut()
699+
.resource_scope::<Schedules, _>(|_, schedules| {
700+
let (node_id, system) = schedules
701+
.get(TestSchedule)
702+
.unwrap()
703+
.systems()
704+
.unwrap()
705+
.find(|(_, system)| system.name().contains("test_system_rust"))
706+
.unwrap();
707+
708+
ReflectSystem::from_system(system.as_ref(), node_id)
709+
});
710+
694711
// now dynamically add script system via builder
695712
let mut builder = ScriptSystemBuilder::new("test".into(), "empty_script".into());
696713
builder.before_system(test_system);
697714

698-
let _ = builder.build::<TestPlugin>(WorldAccessGuard::new_exclusive(app.world_mut()), &ReflectSchedule::from_label(TestSchedule)).unwrap();
699-
715+
let _ = builder
716+
.build::<TestPlugin>(
717+
WorldAccessGuard::new_exclusive(app.world_mut()),
718+
&ReflectSchedule::from_label(TestSchedule),
719+
)
720+
.unwrap();
721+
700722
// now re-run app, expect no panicks
701723
app.update();
702724
}
703-
}
725+
}

crates/bevy_mod_scripting_core/src/bindings/world.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
//! we need wrapper types which have owned and ref variants.
77
88
use super::{
9-
access_map::{AccessCount, AccessMapKey, AnyAccessMap, DynamicSystemMeta, ReflectAccessId, ReflectAccessKind, SubsetAccessMap},
9+
access_map::{
10+
AccessCount, AccessMapKey, AnyAccessMap, DynamicSystemMeta, ReflectAccessId,
11+
ReflectAccessKind, SubsetAccessMap,
12+
},
1013
function::{
1114
namespace::Namespace,
1215
script_function::{AppScriptFunctionRegistry, DynamicScriptFunction, FunctionCallContext},
@@ -168,10 +171,10 @@ impl<'w> WorldAccessGuard<'w> {
168171
inner: Rc::new(WorldAccessGuardInner {
169172
cell: world,
170173
accesses: AnyAccessMap::SubsetAccessMap(SubsetAccessMap::new(
171-
subset ,
172-
// allocations live beyond the world, and can be safely accessed
173-
|id| ReflectAccessId::from_index(id).kind == ReflectAccessKind::Allocation)
174-
),
174+
subset,
175+
// allocations live beyond the world, and can be safely accessed
176+
|id| ReflectAccessId::from_index(id).kind == ReflectAccessKind::Allocation,
177+
)),
175178
type_registry: type_registry.0,
176179
allocator,
177180
function_registry,

0 commit comments

Comments
 (0)