1
+ //! Systems and resources for handling script assets and events
2
+
1
3
use crate :: {
2
4
commands:: { CreateOrUpdateScript , DeleteScript } ,
3
5
error:: ScriptError ,
@@ -6,7 +8,7 @@ use crate::{
6
8
} ;
7
9
use bevy:: {
8
10
app:: { App , PreUpdate } ,
9
- asset:: { Asset , AssetEvent , AssetId , AssetLoader , Assets } ,
11
+ asset:: { Asset , AssetEvent , AssetId , AssetLoader , AssetPath , Assets } ,
10
12
ecs:: system:: Resource ,
11
13
log:: { debug, error, info, trace} ,
12
14
prelude:: {
@@ -16,17 +18,18 @@ use bevy::{
16
18
reflect:: TypePath ,
17
19
utils:: HashMap ,
18
20
} ;
19
- use std:: {
20
- borrow:: Cow ,
21
- path:: { Path , PathBuf } ,
22
- } ;
21
+ use std:: borrow:: Cow ;
23
22
24
23
/// Represents a scripting language. Languages which compile into another language should use the target language as their language.
25
24
#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
26
25
pub enum Language {
26
+ /// The Rhai scripting language
27
27
Rhai ,
28
+ /// The Lua scripting language
28
29
Lua ,
30
+ /// The Rune scripting language
29
31
Rune ,
32
+ /// An external scripting language
30
33
External ( Cow < ' static , str > ) ,
31
34
/// Set if none of the asset path to language mappers match
32
35
Unknown ,
@@ -47,9 +50,10 @@ impl std::fmt::Display for Language {
47
50
/// Represents a script loaded into memory as an asset
48
51
#[ derive( Asset , TypePath , Clone ) ]
49
52
pub struct ScriptAsset {
53
+ /// The body of the script
50
54
pub content : Box < [ u8 ] > ,
51
55
/// The virtual filesystem path of the asset, used to map to the script Id for asset backed scripts
52
- pub asset_path : PathBuf ,
56
+ pub asset_path : AssetPath < ' static > ,
53
57
}
54
58
55
59
#[ derive( Event , Debug , Clone ) ]
@@ -60,6 +64,7 @@ pub(crate) enum ScriptAssetEvent {
60
64
}
61
65
62
66
#[ derive( Default ) ]
67
+ /// A loader for script assets
63
68
pub struct ScriptAssetLoader {
64
69
/// The file extensions this loader should handle
65
70
pub extensions : & ' static [ & ' static str ] ,
@@ -90,7 +95,7 @@ impl AssetLoader for ScriptAssetLoader {
90
95
}
91
96
let asset = ScriptAsset {
92
97
content : content. into_boxed_slice ( ) ,
93
- asset_path : load_context. path ( ) . to_owned ( ) ,
98
+ asset_path : load_context. asset_path ( ) . to_owned ( ) ,
94
99
} ;
95
100
Ok ( asset)
96
101
}
@@ -101,13 +106,17 @@ impl AssetLoader for ScriptAssetLoader {
101
106
}
102
107
103
108
#[ derive( Clone , Resource ) ]
109
+ /// Settings to do with script assets and how they are handled
104
110
pub struct ScriptAssetSettings {
111
+ /// Strategy for mapping asset paths to script ids, by default this is the identity function
105
112
pub script_id_mapper : AssetPathToScriptIdMapper ,
113
+ /// Strategies for mapping asset paths to languages
106
114
pub script_language_mappers : Vec < AssetPathToLanguageMapper > ,
107
115
}
108
116
109
117
impl ScriptAssetSettings {
110
- pub fn select_script_language ( & self , path : & Path ) -> Language {
118
+ /// Selects the language for a given asset path
119
+ pub fn select_script_language ( & self , path : & AssetPath ) -> Language {
111
120
for mapper in & self . script_language_mappers {
112
121
let language = ( mapper. map ) ( path) ;
113
122
match language {
@@ -124,7 +133,7 @@ impl Default for ScriptAssetSettings {
124
133
fn default ( ) -> Self {
125
134
Self {
126
135
script_id_mapper : AssetPathToScriptIdMapper {
127
- map : ( |path : & Path | path. to_string_lossy ( ) . into_owned ( ) . into ( ) ) ,
136
+ map : ( |path : & AssetPath | path. path ( ) . to_string_lossy ( ) . into_owned ( ) . into ( ) ) ,
128
137
} ,
129
138
script_language_mappers : vec ! [ ] ,
130
139
}
@@ -134,41 +143,53 @@ impl Default for ScriptAssetSettings {
134
143
/// Strategy for mapping asset paths to script ids, by default this is the identity function
135
144
#[ derive( Clone , Copy ) ]
136
145
pub struct AssetPathToScriptIdMapper {
137
- pub map : fn ( & Path ) -> ScriptId ,
146
+ /// The mapping function
147
+ pub map : fn ( & AssetPath ) -> ScriptId ,
138
148
}
139
149
140
150
#[ derive( Clone , Copy ) ]
151
+ /// Strategy for mapping asset paths to languages
141
152
pub struct AssetPathToLanguageMapper {
142
- pub map : fn ( & Path ) -> Language ,
153
+ /// The mapping function
154
+ pub map : fn ( & AssetPath ) -> Language ,
143
155
}
144
156
145
157
/// 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.
146
158
#[ derive( Default , Debug , Resource ) ]
147
159
pub struct ScriptMetadataStore {
160
+ /// The map of asset id's to their metadata
148
161
pub map : HashMap < AssetId < ScriptAsset > , ScriptMetadata > ,
149
162
}
150
163
151
164
#[ derive( Debug , Clone , PartialEq , Eq ) ]
165
+ /// Metadata for a script asset
152
166
pub struct ScriptMetadata {
167
+ /// The asset id of the script
153
168
pub asset_id : AssetId < ScriptAsset > ,
169
+ /// The script id of the script
154
170
pub script_id : ScriptId ,
171
+ /// The language of the script
155
172
pub language : Language ,
156
173
}
157
174
158
175
impl ScriptMetadataStore {
176
+ /// Inserts a new metadata entry
159
177
pub fn insert ( & mut self , id : AssetId < ScriptAsset > , meta : ScriptMetadata ) {
160
178
// TODO: new generations of assets are not going to have the same ID as the old one
161
179
self . map . insert ( id, meta) ;
162
180
}
163
181
182
+ /// Gets a metadata entry
164
183
pub fn get ( & self , id : AssetId < ScriptAsset > ) -> Option < & ScriptMetadata > {
165
184
self . map . get ( & id)
166
185
}
167
186
187
+ /// Removes a metadata entry
168
188
pub fn remove ( & mut self , id : AssetId < ScriptAsset > ) -> Option < ScriptMetadata > {
169
189
self . map . remove ( & id)
170
190
}
171
191
192
+ /// Checks if the store contains a metadata entry
172
193
pub fn contains ( & self , id : AssetId < ScriptAsset > ) -> bool {
173
194
self . map . contains_key ( & id)
174
195
}
@@ -333,6 +354,8 @@ pub(crate) fn configure_asset_systems_for_plugin<P: IntoScriptPluginParams>(
333
354
334
355
#[ cfg( test) ]
335
356
mod tests {
357
+ use std:: path:: { Path , PathBuf } ;
358
+
336
359
use bevy:: {
337
360
app:: { App , Update } ,
338
361
asset:: { AssetApp , AssetPlugin , AssetServer , Assets , Handle , LoadState } ,
@@ -352,12 +375,12 @@ mod tests {
352
375
fn make_test_settings ( ) -> ScriptAssetSettings {
353
376
ScriptAssetSettings {
354
377
script_id_mapper : AssetPathToScriptIdMapper {
355
- map : |path| path. to_string_lossy ( ) . into_owned ( ) . into ( ) ,
378
+ map : |path| path. path ( ) . to_string_lossy ( ) . into_owned ( ) . into ( ) ,
356
379
} ,
357
380
script_language_mappers : vec ! [
358
381
AssetPathToLanguageMapper {
359
382
map: |path| {
360
- if path. extension( ) . unwrap( ) == "lua" {
383
+ if path. path ( ) . extension( ) . unwrap( ) == "lua" {
361
384
Language :: Lua
362
385
} else {
363
386
Language :: Unknown
@@ -366,7 +389,7 @@ mod tests {
366
389
} ,
367
390
AssetPathToLanguageMapper {
368
391
map: |path| {
369
- if path. extension( ) . unwrap( ) == "rhai" {
392
+ if path. path ( ) . extension( ) . unwrap( ) == "rhai" {
370
393
Language :: Rhai
371
394
} else {
372
395
Language :: Unknown
@@ -427,7 +450,7 @@ mod tests {
427
450
428
451
assert_eq ! (
429
452
asset. asset_path,
430
- PathBuf :: from( "test_assets/test_script.script" )
453
+ AssetPath :: from_path ( & PathBuf :: from( "test_assets/test_script.script" ) )
431
454
) ;
432
455
433
456
assert_eq ! (
@@ -457,7 +480,7 @@ mod tests {
457
480
458
481
assert_eq ! (
459
482
asset. asset_path,
460
- PathBuf :: from( "test_assets/test_script.script" )
483
+ AssetPath :: from ( PathBuf :: from( "test_assets/test_script.script" ) )
461
484
) ;
462
485
assert_eq ! (
463
486
String :: from_utf8( asset. content. clone( ) . to_vec( ) ) . unwrap( ) ,
@@ -485,14 +508,14 @@ mod tests {
485
508
fn test_script_asset_settings_select_language ( ) {
486
509
let settings = make_test_settings ( ) ;
487
510
488
- let path = Path :: new ( "test.lua" ) ;
489
- assert_eq ! ( settings. select_script_language( path) , Language :: Lua ) ;
511
+ let path = AssetPath :: from ( Path :: new ( "test.lua" ) ) ;
512
+ assert_eq ! ( settings. select_script_language( & path) , Language :: Lua ) ;
490
513
assert_eq ! (
491
- settings. select_script_language( Path :: new( "test.rhai" ) ) ,
514
+ settings. select_script_language( & AssetPath :: from ( Path :: new( "test.rhai" ) ) ) ,
492
515
Language :: Rhai
493
516
) ;
494
517
assert_eq ! (
495
- settings. select_script_language( Path :: new( "test.blob" ) ) ,
518
+ settings. select_script_language( & AssetPath :: from ( Path :: new( "test.blob" ) ) ) ,
496
519
Language :: Unknown
497
520
) ;
498
521
}
0 commit comments