1
1
//! everything to do with dynamically added script systems
2
2
3
3
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 ,
5
10
} ;
6
11
use crate :: {
7
12
bindings:: pretty_print:: DisplayWithWorld ,
@@ -28,8 +33,8 @@ use bevy::{
28
33
reflect:: { OffsetAccess , ParsedPath , Reflect } ,
29
34
utils:: hashbrown:: HashSet ,
30
35
} ;
31
- use std:: { any:: TypeId , borrow:: Cow , hash:: Hash , marker:: PhantomData , ops:: Deref } ;
32
36
use bevy_system_reflection:: { ReflectSchedule , ReflectSystem } ;
37
+ use std:: { any:: TypeId , borrow:: Cow , hash:: Hash , marker:: PhantomData , ops:: Deref } ;
33
38
#[ derive( Clone , Hash , PartialEq , Eq ) ]
34
39
/// a system set for script systems.
35
40
pub struct ScriptSystemSet ( Cow < ' static , str > ) ;
@@ -390,8 +395,6 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
390
395
fn has_deferred ( & self ) -> bool {
391
396
false
392
397
}
393
-
394
-
395
398
396
399
unsafe fn run_unsafe (
397
400
& mut self ,
@@ -410,20 +413,20 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
410
413
} ;
411
414
412
415
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 ( ) } ;
417
420
WorldAccessGuard :: new_exclusive ( world)
418
- } else {
421
+ } else {
419
422
WorldAccessGuard :: new_non_exclusive (
420
423
world,
421
424
state. subset . clone ( ) ,
422
425
state. type_registry . clone ( ) ,
423
426
state. allocator . clone ( ) ,
424
427
state. function_registry . clone ( ) ,
425
428
state. schedule_registry . clone ( ) ,
426
- )
429
+ )
427
430
} ;
428
431
429
432
// TODO: cache references which don't change once we have benchmarks
@@ -450,12 +453,19 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
450
453
. map ( |entity| {
451
454
Val ( ScriptQueryResult {
452
455
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
+ } ,
456
466
reflect_path : Vec :: < OffsetAccess > :: default ( ) . into ( ) ,
457
- }
458
- } ) . collect ( ) ,
467
+ } )
468
+ . collect ( ) ,
459
469
} )
460
470
} )
461
471
. collect :: < Vec < _ > > ( ) ;
@@ -526,9 +536,11 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
526
536
subset. insert ( raid) ;
527
537
}
528
538
ScriptSystemParamDescriptor :: EntityQuery ( query) => {
529
- let components: Vec < _ > = query. components
539
+ let components: Vec < _ > = query
540
+ . components
530
541
. 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 ( ) ;
532
544
let query = query. as_query_state :: < Entity > ( world) ;
533
545
534
546
// Safety: we are not removing
@@ -550,7 +562,7 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
550
562
551
563
system_params. push ( ScriptSystemParam :: EntityQuery {
552
564
query : query. into ( ) ,
553
- components
565
+ components,
554
566
} ) ;
555
567
subset. extend ( new_raids) ;
556
568
}
@@ -628,12 +640,11 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
628
640
fn default_system_sets ( & self ) -> Vec < bevy:: ecs:: schedule:: InternedSystemSet > {
629
641
vec ! [ ScriptSystemSet :: new( self . name. clone( ) ) . intern( ) ]
630
642
}
631
-
643
+
632
644
fn type_id ( & self ) -> TypeId {
633
645
TypeId :: of :: < Self > ( )
634
646
}
635
647
636
-
637
648
fn validate_param ( & mut self , world : & World ) -> bool {
638
649
let world_cell = world. as_unsafe_world_cell_readonly ( ) ;
639
650
self . update_archetype_component_access ( world_cell) ;
@@ -644,15 +655,18 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
644
655
}
645
656
}
646
657
647
-
648
658
#[ cfg( test) ]
649
659
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
+ } ;
651
666
use test_utils:: make_test_plugin;
652
667
653
668
use super :: * ;
654
669
655
-
656
670
make_test_plugin ! ( crate ) ;
657
671
658
672
fn test_system_rust ( _world : & mut World ) { }
@@ -663,41 +677,49 @@ mod test {
663
677
664
678
#[ derive( ScheduleLabel , Clone , Debug , Hash , PartialEq , Eq ) ]
665
679
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
+ ) ) ;
668
686
app. init_schedule ( TestSchedule ) ;
669
687
let mut main_schedule_order = app. world_mut ( ) . resource_mut :: < MainScheduleOrder > ( ) ;
670
688
main_schedule_order. insert_after ( Update , TestSchedule ) ;
671
689
app. add_systems ( TestSchedule , test_system_rust) ;
672
-
673
-
690
+
674
691
// run the app once
675
692
app. finish ( ) ;
676
693
app. cleanup ( ) ;
677
694
app. update ( ) ;
678
695
679
696
// 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
+
694
711
// now dynamically add script system via builder
695
712
let mut builder = ScriptSystemBuilder :: new ( "test" . into ( ) , "empty_script" . into ( ) ) ;
696
713
builder. before_system ( test_system) ;
697
714
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
+
700
722
// now re-run app, expect no panicks
701
723
app. update ( ) ;
702
724
}
703
- }
725
+ }
0 commit comments