@@ -21,9 +21,10 @@ use bevy::{
21
21
utils:: hashbrown:: HashMap ,
22
22
} ;
23
23
use std:: { borrow:: Cow , collections:: VecDeque } ;
24
+ use serde:: { Deserialize , Serialize } ;
24
25
25
26
/// Represents a scripting language. Languages which compile into another language should use the target language as their language.
26
- #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Default ) ]
27
+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Default , Serialize , Deserialize ) ]
27
28
pub enum Language {
28
29
/// The Rhai scripting language
29
30
Rhai ,
@@ -66,6 +67,13 @@ pub(crate) enum ScriptAssetEvent {
66
67
Modified ( ScriptMetadata ) ,
67
68
}
68
69
70
+ /// Script settings
71
+ #[ derive( Debug , Clone , Serialize , Deserialize , Default ) ]
72
+ pub struct ScriptSettings {
73
+ /// Define the language for a script or use the extension if None.
74
+ pub language : Option < Language > ,
75
+ }
76
+
69
77
#[ derive( Default ) ]
70
78
/// A loader for script assets
71
79
pub struct ScriptAssetLoader {
@@ -79,14 +87,14 @@ pub struct ScriptAssetLoader {
79
87
impl AssetLoader for ScriptAssetLoader {
80
88
type Asset = ScriptAsset ;
81
89
82
- type Settings = ( ) ;
90
+ type Settings = ScriptSettings ;
83
91
84
92
type Error = ScriptError ;
85
93
86
94
async fn load (
87
95
& self ,
88
96
reader : & mut dyn bevy:: asset:: io:: Reader ,
89
- _settings : & Self :: Settings ,
97
+ settings : & Self :: Settings ,
90
98
load_context : & mut bevy:: asset:: LoadContext < ' _ > ,
91
99
) -> Result < Self :: Asset , Self :: Error > {
92
100
let mut content = Vec :: new ( ) ;
@@ -97,14 +105,18 @@ impl AssetLoader for ScriptAssetLoader {
97
105
if let Some ( processor) = & self . preprocessor {
98
106
processor ( & mut content) ?;
99
107
}
100
- let language = match load_context. path ( ) . extension ( ) . and_then ( |e| e. to_str ( ) ) . unwrap_or_default ( ) {
101
- "lua" => Language :: Lua ,
102
- "rhai" => Language :: Rhai ,
103
- x => {
104
- warn ! ( "Unknown language for {:?}" , load_context. path( ) . display( ) ) ;
105
- Language :: Unknown
106
- }
107
- } ;
108
+ let language = settings
109
+ . language
110
+ . clone ( )
111
+ . unwrap_or_else ( ||
112
+ match load_context. path ( ) . extension ( ) . and_then ( |e| e. to_str ( ) ) . unwrap_or_default ( ) {
113
+ "lua" => Language :: Lua ,
114
+ "rhai" => Language :: Rhai ,
115
+ x => {
116
+ warn ! ( "Unknown language for {:?}" , load_context. path( ) . display( ) ) ;
117
+ Language :: Unknown
118
+ }
119
+ } ) ;
108
120
let asset = ScriptAsset {
109
121
content : content. into_boxed_slice ( ) ,
110
122
language,
@@ -117,61 +129,6 @@ impl AssetLoader for ScriptAssetLoader {
117
129
}
118
130
}
119
131
120
- // #[derive(Clone, Resource)]
121
- // /// Settings to do with script assets and how they are handled
122
- // pub struct ScriptAssetSettings {
123
- // /// Strategy for mapping asset paths to script ids, by default this is the identity function
124
- // pub script_id_mapper: AssetPathToScriptIdMapper,
125
- // /// Mapping from extension to script language
126
- // pub extension_to_language_map: HashMap<&'static str, Language>,
127
-
128
- // /// The currently supported asset extensions
129
- // /// Should be updated by each scripting plugin to include the extensions it supports.
130
- // ///
131
- // /// Will be used to populate the script asset loader with the supported extensions
132
- // pub supported_extensions: &'static [&'static str],
133
- // }
134
-
135
- // #[profiling::all_functions]
136
- // impl ScriptAssetSettings {
137
- // /// Selects the language for a given asset path
138
- // pub fn select_script_language(&self, path: &AssetPath) -> Language {
139
- // let extension = path
140
- // .path()
141
- // .extension()
142
- // .and_then(|ext| ext.to_str())
143
- // .unwrap_or_default();
144
- // self.extension_to_language_map
145
- // .get(extension)
146
- // .cloned()
147
- // .unwrap_or_default()
148
- // }
149
- // }
150
-
151
- // impl Default for ScriptAssetSettings {
152
- // fn default() -> Self {
153
- // Self {
154
- // script_id_mapper: AssetPathToScriptIdMapper {
155
- // // map: (|path: &AssetPath| path.path().to_string_lossy().into_owned().into()),
156
- // map: (|path: &AssetPath| path.id()),
157
- // },
158
- // extension_to_language_map: HashMap::from_iter(vec![
159
- // ("lua", Language::Lua),
160
- // ("luau", Language::Lua),
161
- // ("rhai", Language::Rhai),
162
- // ("rn", Language::Rune),
163
- // ]),
164
- // supported_extensions: &["lua", "luau", "rhai", "rn"],
165
- // }
166
- // }
167
- // }
168
-
169
- // /// Strategy for mapping asset paths to script ids, by default this is the identity function
170
- // #[derive(Clone, Copy)]
171
- // pub struct AssetPathToScriptIdMapper {
172
- // /// The mapping function
173
- // pub map: fn(&AssetPath) -> ScriptId,
174
- // }
175
132
176
133
/// A cache of asset id's to their script id's. Necessary since when we drop an asset we won't have the ability to get the path from the asset.
177
134
#[ derive( Default , Debug , Resource ) ]
0 commit comments