4
4
5
5
use crate :: event:: ScriptErrorEvent ;
6
6
use asset:: {
7
- configure_asset_systems, configure_asset_systems_for_plugin, AssetPathToLanguageMapper ,
8
- Language , ScriptAsset , ScriptAssetLoader , ScriptAssetSettings ,
7
+ configure_asset_systems, configure_asset_systems_for_plugin, Language , ScriptAsset ,
8
+ ScriptAssetLoader , ScriptAssetSettings ,
9
9
} ;
10
10
use bevy:: prelude:: * ;
11
11
use bindings:: {
@@ -87,16 +87,16 @@ pub struct ScriptingPlugin<P: IntoScriptPluginParams> {
87
87
/// The strategy for assigning contexts to scripts
88
88
pub context_assignment_strategy : ContextAssignmentStrategy ,
89
89
90
- /// The asset path to language mapper for the plugin
91
- pub language_mapper : AssetPathToLanguageMapper ,
90
+ /// The language this plugin declares
91
+ pub language : Language ,
92
+ /// Supported extensions to be added to the asset settings without the dot
93
+ /// By default BMS populates a set of extensions for the languages it supports.
94
+ pub additional_supported_extensions : & ' static [ & ' static str ] ,
92
95
93
96
/// initializers for the contexts, run when loading the script
94
97
pub context_initializers : Vec < ContextInitializer < P > > ,
95
98
/// initializers for the contexts run every time before handling events
96
99
pub context_pre_handling_initializers : Vec < ContextPreHandlingInitializer < P > > ,
97
-
98
- /// Supported extensions to be added to the asset settings without the dot
99
- pub supported_extensions : & ' static [ & ' static str ] ,
100
100
}
101
101
102
102
impl < P : IntoScriptPluginParams > Default for ScriptingPlugin < P > {
@@ -106,10 +106,10 @@ impl<P: IntoScriptPluginParams> Default for ScriptingPlugin<P> {
106
106
callback_handler : CallbackSettings :: < P > :: default ( ) . callback_handler ,
107
107
context_builder : Default :: default ( ) ,
108
108
context_assignment_strategy : Default :: default ( ) ,
109
- language_mapper : Default :: default ( ) ,
109
+ language : Default :: default ( ) ,
110
110
context_initializers : Default :: default ( ) ,
111
111
context_pre_handling_initializers : Default :: default ( ) ,
112
- supported_extensions : Default :: default ( ) ,
112
+ additional_supported_extensions : Default :: default ( ) ,
113
113
}
114
114
}
115
115
}
@@ -136,13 +136,12 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
136
136
// add extension for the language to the asset loader
137
137
once_per_app_init ( app) ;
138
138
139
- app. add_supported_script_extensions ( self . supported_extensions ) ;
140
-
141
- app. world_mut ( )
142
- . resource_mut :: < ScriptAssetSettings > ( )
143
- . as_mut ( )
144
- . script_language_mappers
145
- . push ( self . language_mapper ) ;
139
+ if !self . additional_supported_extensions . is_empty ( ) {
140
+ app. add_supported_script_extensions (
141
+ self . additional_supported_extensions ,
142
+ self . language . clone ( ) ,
143
+ ) ;
144
+ }
146
145
147
146
register_types ( app) ;
148
147
}
@@ -203,6 +202,11 @@ pub trait ConfigureScriptPlugin {
203
202
/// This means that all scripts will share the same context. This is useful for when you want to share data between scripts easilly.
204
203
/// Be careful however as this also means that scripts can interfere with each other in unexpected ways! Including overwriting each other's handlers.
205
204
fn enable_context_sharing ( self ) -> Self ;
205
+
206
+ /// Set the set of extensions to be added for the plugin's language.
207
+ ///
208
+ /// This is useful for adding extensions that are not supported by default by BMS.
209
+ fn set_additional_supported_extensions ( self , extensions : & ' static [ & ' static str ] ) -> Self ;
206
210
}
207
211
208
212
impl < P : IntoScriptPluginParams + AsMut < ScriptingPlugin < P > > > ConfigureScriptPlugin for P {
@@ -231,6 +235,11 @@ impl<P: IntoScriptPluginParams + AsMut<ScriptingPlugin<P>>> ConfigureScriptPlugi
231
235
self . as_mut ( ) . context_assignment_strategy = ContextAssignmentStrategy :: Global ;
232
236
self
233
237
}
238
+
239
+ fn set_additional_supported_extensions ( mut self , extensions : & ' static [ & ' static str ] ) -> Self {
240
+ self . as_mut ( ) . additional_supported_extensions = extensions;
241
+ self
242
+ }
234
243
}
235
244
236
245
fn once_per_app_finalize ( app : & mut App ) {
@@ -386,11 +395,21 @@ impl ManageStaticScripts for App {
386
395
/// Any changes to the asset settings after that will not be reflected in the asset loader.
387
396
pub trait ConfigureScriptAssetSettings {
388
397
/// Adds a supported extension to the asset settings
389
- fn add_supported_script_extensions ( & mut self , extensions : & [ & ' static str ] ) -> & mut Self ;
398
+ ///
399
+ /// This is only valid to call in the plugin building phase, as the asset loader will be created in the `finalize` phase.
400
+ fn add_supported_script_extensions (
401
+ & mut self ,
402
+ extensions : & [ & ' static str ] ,
403
+ language : Language ,
404
+ ) -> & mut Self ;
390
405
}
391
406
392
407
impl ConfigureScriptAssetSettings for App {
393
- fn add_supported_script_extensions ( & mut self , extensions : & [ & ' static str ] ) -> & mut Self {
408
+ fn add_supported_script_extensions (
409
+ & mut self ,
410
+ extensions : & [ & ' static str ] ,
411
+ language : Language ,
412
+ ) -> & mut Self {
394
413
let mut asset_settings = self
395
414
. world_mut ( )
396
415
. get_resource_or_init :: < ScriptAssetSettings > ( ) ;
@@ -402,6 +421,11 @@ impl ConfigureScriptAssetSettings for App {
402
421
let new_arr_static = Vec :: leak ( new_arr) ;
403
422
404
423
asset_settings. supported_extensions = new_arr_static;
424
+ for extension in extensions {
425
+ asset_settings
426
+ . extension_to_language_map
427
+ . insert ( * extension, language. clone ( ) ) ;
428
+ }
405
429
406
430
self
407
431
}
0 commit comments