@@ -9,11 +9,8 @@ use asset::{
9
9
} ;
10
10
use bevy:: prelude:: * ;
11
11
use bindings:: {
12
- function:: script_function:: AppScriptFunctionRegistry ,
13
- garbage_collector,
14
- globals:: { core:: CoreScriptGlobalsPlugin , AppScriptGlobalsRegistry } ,
15
- schedule:: AppScheduleRegistry ,
16
- script_value:: ScriptValue ,
12
+ function:: script_function:: AppScriptFunctionRegistry , garbage_collector,
13
+ globals:: AppScriptGlobalsRegistry , schedule:: AppScheduleRegistry , script_value:: ScriptValue ,
17
14
AppReflectAllocator , DynamicScriptComponentPlugin , ReflectAllocator , ReflectReference ,
18
15
ScriptTypeRegistration ,
19
16
} ;
@@ -137,9 +134,6 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
137
134
138
135
register_script_plugin_systems :: < P > ( app) ;
139
136
140
- // add extension for the language to the asset loader
141
- once_per_app_init ( app) ;
142
-
143
137
if !self . additional_supported_extensions . is_empty ( ) {
144
138
app. add_supported_script_extensions (
145
139
self . additional_supported_extensions ,
@@ -149,10 +143,6 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
149
143
150
144
register_types ( app) ;
151
145
}
152
-
153
- fn finish ( & self , app : & mut App ) {
154
- once_per_app_finalize ( app) ;
155
- }
156
146
}
157
147
158
148
impl < P : IntoScriptPluginParams > ScriptingPlugin < P > {
@@ -246,36 +236,6 @@ impl<P: IntoScriptPluginParams + AsMut<ScriptingPlugin<P>>> ConfigureScriptPlugi
246
236
}
247
237
}
248
238
249
- fn once_per_app_finalize ( app : & mut App ) {
250
- #[ derive( Resource ) ]
251
- struct BMSFinalized ;
252
-
253
- if app. world ( ) . contains_resource :: < BMSFinalized > ( ) {
254
- return ;
255
- }
256
- app. insert_resource ( BMSFinalized ) ;
257
-
258
- // read extensions from asset settings
259
- let asset_settings_extensions = app
260
- . world_mut ( )
261
- . get_resource_or_init :: < ScriptAssetSettings > ( )
262
- . supported_extensions ;
263
-
264
- // convert extensions to static array
265
- bevy:: log:: info!(
266
- "Initializing BMS with Supported extensions: {:?}" ,
267
- asset_settings_extensions
268
- ) ;
269
-
270
- app. register_asset_loader ( ScriptAssetLoader {
271
- extensions : asset_settings_extensions,
272
- preprocessor : None ,
273
- } ) ;
274
-
275
- // pre-register component id's
276
- pre_register_componnents ( app) ;
277
- }
278
-
279
239
/// Ensures all types with `ReflectComponent` type data are pre-registered with component ID's
280
240
fn pre_register_componnents ( app : & mut App ) {
281
241
let type_registry = app
@@ -290,34 +250,54 @@ fn pre_register_componnents(app: &mut App) {
290
250
}
291
251
}
292
252
293
- // One of registration of things that need to be done only once per app
294
- fn once_per_app_init ( app : & mut App ) {
295
- #[ derive( Resource ) ]
296
- struct BMSInitialized ;
297
-
298
- if app. world ( ) . contains_resource :: < BMSInitialized > ( ) {
299
- return ;
253
+ /// A plugin defining shared settings between various scripting plugins
254
+ /// It is necessary to register this plugin for any of them to work
255
+ pub struct BMSScriptingInfrastructurePlugin ;
256
+
257
+ impl Plugin for BMSScriptingInfrastructurePlugin {
258
+ fn build ( & self , app : & mut App ) {
259
+ app. add_event :: < ScriptErrorEvent > ( )
260
+ . add_event :: < ScriptCallbackEvent > ( )
261
+ . add_event :: < ScriptCallbackResponseEvent > ( )
262
+ . init_resource :: < AppReflectAllocator > ( )
263
+ . init_resource :: < StaticScripts > ( )
264
+ . init_asset :: < ScriptAsset > ( )
265
+ . init_resource :: < AppScriptFunctionRegistry > ( )
266
+ . init_resource :: < AppScriptGlobalsRegistry > ( )
267
+ . insert_resource ( AppScheduleRegistry :: new ( ) ) ;
268
+
269
+ app. add_systems (
270
+ PostUpdate ,
271
+ ( ( garbage_collector) . in_set ( ScriptingSystemSet :: GarbageCollection ) , ) ,
272
+ ) ;
273
+
274
+ configure_asset_systems ( app) ;
275
+
276
+ DynamicScriptComponentPlugin . build ( app) ;
300
277
}
301
- app. insert_resource ( BMSInitialized ) ;
302
-
303
- app. add_event :: < ScriptErrorEvent > ( )
304
- . add_event :: < ScriptCallbackEvent > ( )
305
- . add_event :: < ScriptCallbackResponseEvent > ( )
306
- . init_resource :: < AppReflectAllocator > ( )
307
- . init_resource :: < StaticScripts > ( )
308
- . init_asset :: < ScriptAsset > ( )
309
- . init_resource :: < AppScriptFunctionRegistry > ( )
310
- . init_resource :: < AppScriptGlobalsRegistry > ( )
311
- . insert_resource ( AppScheduleRegistry :: new ( ) ) ;
312
278
313
- app. add_systems (
314
- PostUpdate ,
315
- ( ( garbage_collector) . in_set ( ScriptingSystemSet :: GarbageCollection ) , ) ,
316
- ) ;
317
-
318
- app. add_plugins ( ( CoreScriptGlobalsPlugin , DynamicScriptComponentPlugin ) ) ;
319
-
320
- configure_asset_systems ( app) ;
279
+ fn finish ( & self , app : & mut App ) {
280
+ // read extensions from asset settings
281
+ let asset_settings_extensions = app
282
+ . world_mut ( )
283
+ . get_resource_or_init :: < ScriptAssetSettings > ( )
284
+ . supported_extensions ;
285
+
286
+ // convert extensions to static array
287
+ bevy:: log:: info!(
288
+ "Initializing BMS with Supported extensions: {:?}" ,
289
+ asset_settings_extensions
290
+ ) ;
291
+
292
+ app. register_asset_loader ( ScriptAssetLoader {
293
+ extensions : asset_settings_extensions,
294
+ preprocessor : None ,
295
+ } ) ;
296
+
297
+ // pre-register component id's
298
+ pre_register_componnents ( app) ;
299
+ DynamicScriptComponentPlugin . finish ( app) ;
300
+ }
321
301
}
322
302
323
303
/// Systems registered per-language
@@ -450,7 +430,7 @@ mod test {
450
430
. resource_mut :: < ScriptAssetSettings > ( )
451
431
. supported_extensions = & [ "lua" , "rhai" ] ;
452
432
453
- once_per_app_finalize ( & mut app) ;
433
+ BMSScriptingInfrastructurePlugin . finish ( & mut app) ;
454
434
455
435
let asset_loader = app
456
436
. world ( )
@@ -482,7 +462,7 @@ mod test {
482
462
483
463
assert ! ( app. world_mut( ) . component_id:: <Comp >( ) . is_none( ) ) ;
484
464
485
- once_per_app_finalize ( & mut app) ;
465
+ BMSScriptingInfrastructurePlugin . finish ( & mut app) ;
486
466
487
467
assert ! ( app. world_mut( ) . component_id:: <Comp >( ) . is_some( ) ) ;
488
468
}
0 commit comments