@@ -12,15 +12,15 @@ use asset::{
12
12
Language , ScriptAsset , ScriptAssetLoader , configure_asset_systems,
13
13
configure_asset_systems_for_plugin,
14
14
} ;
15
- use bevy_app:: { App , Plugin , PostStartup , PostUpdate } ;
15
+ use bevy_app:: { App , Plugin , PostUpdate } ;
16
16
use bevy_asset:: { AssetApp , Handle } ;
17
+ use bevy_ecs:: schedule:: IntoScheduleConfigs ;
17
18
use bevy_ecs:: {
18
19
reflect:: { AppTypeRegistry , ReflectComponent } ,
19
20
resource:: Resource ,
20
21
schedule:: SystemSet ,
21
- system:: { Command , In } ,
22
+ system:: Command ,
22
23
} ;
23
- use bevy_ecs:: { schedule:: IntoScheduleConfigs , system:: IntoSystem } ;
24
24
use bevy_log:: error;
25
25
use bevy_platform:: collections:: HashMap ;
26
26
use bindings:: {
@@ -30,10 +30,9 @@ use bindings::{
30
30
} ;
31
31
use commands:: { AddStaticScript , RemoveStaticScript } ;
32
32
use context:: { Context , ContextInitializer , ContextPreHandlingInitializer } ;
33
- use error:: ScriptError ;
34
33
use event:: { ScriptCallbackEvent , ScriptCallbackResponseEvent , ScriptEvent } ;
35
34
use handler:: HandlerFn ;
36
- use runtime:: { Runtime , RuntimeContainer , RuntimeInitializer , RuntimeSettings , initialize_runtime } ;
35
+ use runtime:: { Runtime , RuntimeInitializer } ;
37
36
use script:: { ContextPolicy , ScriptComponent , ScriptContext , StaticScripts } ;
38
37
use std:: ops:: { Deref , DerefMut } ;
39
38
@@ -97,8 +96,8 @@ pub trait IntoScriptPluginParams: 'static + GetPluginThreadConfig<Self> {
97
96
98
97
/// Bevy plugin enabling scripting within the bevy mod scripting framework
99
98
pub struct ScriptingPlugin < P : IntoScriptPluginParams > {
100
- /// Settings for the runtime
101
- pub runtime_settings : RuntimeSettings < P > ,
99
+ /// Functions configuring the runtime after it is created
100
+ pub runtime_initializers : Vec < RuntimeInitializer < P > > ,
102
101
103
102
/// The strategy used to assign contexts to scripts
104
103
pub context_policy : ContextPolicy ,
@@ -137,7 +136,7 @@ where
137
136
impl < P : IntoScriptPluginParams > Default for ScriptingPlugin < P > {
138
137
fn default ( ) -> Self {
139
138
Self {
140
- runtime_settings : Default :: default ( ) ,
139
+ runtime_initializers : Default :: default ( ) ,
141
140
context_policy : ContextPolicy :: default ( ) ,
142
141
language : Default :: default ( ) ,
143
142
context_initializers : Default :: default ( ) ,
@@ -150,16 +149,20 @@ impl<P: IntoScriptPluginParams> Default for ScriptingPlugin<P> {
150
149
#[ profiling:: all_functions]
151
150
impl < P : IntoScriptPluginParams > Plugin for ScriptingPlugin < P > {
152
151
fn build ( & self , app : & mut App ) {
153
- app. insert_resource ( self . runtime_settings . clone ( ) )
154
- . insert_resource :: < RuntimeContainer < P > > ( RuntimeContainer {
155
- runtime : P :: build_runtime ( ) ,
156
- } ) ;
157
-
158
152
// initialize thread local configs
153
+
154
+ let runtime = P :: build_runtime ( ) ;
155
+ for initializer in & self . runtime_initializers {
156
+ if let Err ( e) = initializer ( & runtime) {
157
+ error ! ( "Error initializing runtime: {:?}. Continuing." , e) ;
158
+ }
159
+ }
160
+
159
161
let config = ScriptingPluginConfiguration :: < P > {
160
162
pre_handling_callbacks : Vec :: leak ( self . context_pre_handling_initializers . clone ( ) ) ,
161
163
context_initialization_callbacks : Vec :: leak ( self . context_initializers . clone ( ) ) ,
162
164
emit_responses : self . emit_responses ,
165
+ runtime : Box :: leak ( Box :: new ( runtime) ) ,
163
166
} ;
164
167
165
168
P :: set_thread_config ( app. world ( ) . id ( ) , config) ;
@@ -196,7 +199,7 @@ impl<P: IntoScriptPluginParams> ScriptingPlugin<P> {
196
199
///
197
200
/// Initializers will be run after the runtime is created, but before any contexts are loaded.
198
201
pub fn add_runtime_initializer ( & mut self , initializer : RuntimeInitializer < P > ) -> & mut Self {
199
- self . runtime_settings . initializers . push ( initializer) ;
202
+ self . runtime_initializers . push ( initializer) ;
200
203
self
201
204
}
202
205
}
@@ -325,16 +328,6 @@ impl Plugin for BMSScriptingInfrastructurePlugin {
325
328
326
329
/// Systems registered per-language
327
330
fn register_script_plugin_systems < P : IntoScriptPluginParams > ( app : & mut App ) {
328
- app. add_systems (
329
- PostStartup ,
330
- ( initialize_runtime :: < P > . pipe ( |e : In < Result < ( ) , ScriptError > > | {
331
- if let Err ( e) = e. 0 {
332
- error ! ( "Error initializing runtime: {:?}" , e) ;
333
- }
334
- } ) )
335
- . in_set ( ScriptingSystemSet :: RuntimeInitialization ) ,
336
- ) ;
337
-
338
331
app. add_plugins ( configure_asset_systems_for_plugin :: < P > ) ;
339
332
}
340
333
@@ -346,32 +339,6 @@ fn register_types(app: &mut App) {
346
339
app. register_type :: < ScriptComponent > ( ) ;
347
340
}
348
341
349
- /// Trait for adding a runtime initializer to an app
350
- pub trait AddRuntimeInitializer {
351
- /// Adds a runtime initializer to the app
352
- fn add_runtime_initializer < P : IntoScriptPluginParams > (
353
- & mut self ,
354
- initializer : RuntimeInitializer < P > ,
355
- ) -> & mut Self ;
356
- }
357
-
358
- impl AddRuntimeInitializer for App {
359
- fn add_runtime_initializer < P : IntoScriptPluginParams > (
360
- & mut self ,
361
- initializer : RuntimeInitializer < P > ,
362
- ) -> & mut Self {
363
- if !self . world_mut ( ) . contains_resource :: < RuntimeSettings < P > > ( ) {
364
- self . world_mut ( ) . init_resource :: < RuntimeSettings < P > > ( ) ;
365
- }
366
- self . world_mut ( )
367
- . resource_mut :: < RuntimeSettings < P > > ( )
368
- . as_mut ( )
369
- . initializers
370
- . push ( initializer) ;
371
- self
372
- }
373
- }
374
-
375
342
/// Trait for adding static scripts to an app
376
343
pub trait ManageStaticScripts {
377
344
/// Registers a script id as a static script.
0 commit comments