1+ //! Systems and resources for handling script assets and events
2+
13use crate :: {
24 commands:: { CreateOrUpdateScript , DeleteScript } ,
35 error:: ScriptError ,
@@ -6,7 +8,7 @@ use crate::{
68} ;
79use bevy:: {
810 app:: { App , PreUpdate } ,
9- asset:: { Asset , AssetEvent , AssetId , AssetLoader , Assets } ,
11+ asset:: { Asset , AssetEvent , AssetId , AssetLoader , AssetPath , Assets } ,
1012 ecs:: system:: Resource ,
1113 log:: { debug, error, info, trace} ,
1214 prelude:: {
@@ -16,17 +18,18 @@ use bevy::{
1618 reflect:: TypePath ,
1719 utils:: HashMap ,
1820} ;
19- use std:: {
20- borrow:: Cow ,
21- path:: { Path , PathBuf } ,
22- } ;
21+ use std:: borrow:: Cow ;
2322
2423/// Represents a scripting language. Languages which compile into another language should use the target language as their language.
2524#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
2625pub enum Language {
26+ /// The Rhai scripting language
2727 Rhai ,
28+ /// The Lua scripting language
2829 Lua ,
30+ /// The Rune scripting language
2931 Rune ,
32+ /// An external scripting language
3033 External ( Cow < ' static , str > ) ,
3134 /// Set if none of the asset path to language mappers match
3235 Unknown ,
@@ -47,9 +50,10 @@ impl std::fmt::Display for Language {
4750/// Represents a script loaded into memory as an asset
4851#[ derive( Asset , TypePath , Clone ) ]
4952pub struct ScriptAsset {
53+ /// The body of the script
5054 pub content : Box < [ u8 ] > ,
5155 /// 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 > ,
5357}
5458
5559#[ derive( Event , Debug , Clone ) ]
@@ -60,6 +64,7 @@ pub(crate) enum ScriptAssetEvent {
6064}
6165
6266#[ derive( Default ) ]
67+ /// A loader for script assets
6368pub struct ScriptAssetLoader {
6469 /// The file extensions this loader should handle
6570 pub extensions : & ' static [ & ' static str ] ,
@@ -90,7 +95,7 @@ impl AssetLoader for ScriptAssetLoader {
9095 }
9196 let asset = ScriptAsset {
9297 content : content. into_boxed_slice ( ) ,
93- asset_path : load_context. path ( ) . to_owned ( ) ,
98+ asset_path : load_context. asset_path ( ) . to_owned ( ) ,
9499 } ;
95100 Ok ( asset)
96101 }
@@ -101,13 +106,17 @@ impl AssetLoader for ScriptAssetLoader {
101106}
102107
103108#[ derive( Clone , Resource ) ]
109+ /// Settings to do with script assets and how they are handled
104110pub struct ScriptAssetSettings {
111+ /// Strategy for mapping asset paths to script ids, by default this is the identity function
105112 pub script_id_mapper : AssetPathToScriptIdMapper ,
113+ /// Strategies for mapping asset paths to languages
106114 pub script_language_mappers : Vec < AssetPathToLanguageMapper > ,
107115}
108116
109117impl 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 {
111120 for mapper in & self . script_language_mappers {
112121 let language = ( mapper. map ) ( path) ;
113122 match language {
@@ -124,7 +133,7 @@ impl Default for ScriptAssetSettings {
124133 fn default ( ) -> Self {
125134 Self {
126135 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 ( ) ) ,
128137 } ,
129138 script_language_mappers : vec ! [ ] ,
130139 }
@@ -134,41 +143,53 @@ impl Default for ScriptAssetSettings {
134143/// Strategy for mapping asset paths to script ids, by default this is the identity function
135144#[ derive( Clone , Copy ) ]
136145pub struct AssetPathToScriptIdMapper {
137- pub map : fn ( & Path ) -> ScriptId ,
146+ /// The mapping function
147+ pub map : fn ( & AssetPath ) -> ScriptId ,
138148}
139149
140150#[ derive( Clone , Copy ) ]
151+ /// Strategy for mapping asset paths to languages
141152pub struct AssetPathToLanguageMapper {
142- pub map : fn ( & Path ) -> Language ,
153+ /// The mapping function
154+ pub map : fn ( & AssetPath ) -> Language ,
143155}
144156
145157/// 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.
146158#[ derive( Default , Debug , Resource ) ]
147159pub struct ScriptMetadataStore {
160+ /// The map of asset id's to their metadata
148161 pub map : HashMap < AssetId < ScriptAsset > , ScriptMetadata > ,
149162}
150163
151164#[ derive( Debug , Clone , PartialEq , Eq ) ]
165+ /// Metadata for a script asset
152166pub struct ScriptMetadata {
167+ /// The asset id of the script
153168 pub asset_id : AssetId < ScriptAsset > ,
169+ /// The script id of the script
154170 pub script_id : ScriptId ,
171+ /// The language of the script
155172 pub language : Language ,
156173}
157174
158175impl ScriptMetadataStore {
176+ /// Inserts a new metadata entry
159177 pub fn insert ( & mut self , id : AssetId < ScriptAsset > , meta : ScriptMetadata ) {
160178 // TODO: new generations of assets are not going to have the same ID as the old one
161179 self . map . insert ( id, meta) ;
162180 }
163181
182+ /// Gets a metadata entry
164183 pub fn get ( & self , id : AssetId < ScriptAsset > ) -> Option < & ScriptMetadata > {
165184 self . map . get ( & id)
166185 }
167186
187+ /// Removes a metadata entry
168188 pub fn remove ( & mut self , id : AssetId < ScriptAsset > ) -> Option < ScriptMetadata > {
169189 self . map . remove ( & id)
170190 }
171191
192+ /// Checks if the store contains a metadata entry
172193 pub fn contains ( & self , id : AssetId < ScriptAsset > ) -> bool {
173194 self . map . contains_key ( & id)
174195 }
@@ -333,6 +354,8 @@ pub(crate) fn configure_asset_systems_for_plugin<P: IntoScriptPluginParams>(
333354
334355#[ cfg( test) ]
335356mod tests {
357+ use std:: path:: { Path , PathBuf } ;
358+
336359 use bevy:: {
337360 app:: { App , Update } ,
338361 asset:: { AssetApp , AssetPlugin , AssetServer , Assets , Handle , LoadState } ,
@@ -352,12 +375,12 @@ mod tests {
352375 fn make_test_settings ( ) -> ScriptAssetSettings {
353376 ScriptAssetSettings {
354377 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 ( ) ,
356379 } ,
357380 script_language_mappers : vec ! [
358381 AssetPathToLanguageMapper {
359382 map: |path| {
360- if path. extension( ) . unwrap( ) == "lua" {
383+ if path. path ( ) . extension( ) . unwrap( ) == "lua" {
361384 Language :: Lua
362385 } else {
363386 Language :: Unknown
@@ -366,7 +389,7 @@ mod tests {
366389 } ,
367390 AssetPathToLanguageMapper {
368391 map: |path| {
369- if path. extension( ) . unwrap( ) == "rhai" {
392+ if path. path ( ) . extension( ) . unwrap( ) == "rhai" {
370393 Language :: Rhai
371394 } else {
372395 Language :: Unknown
@@ -427,7 +450,7 @@ mod tests {
427450
428451 assert_eq ! (
429452 asset. asset_path,
430- PathBuf :: from( "test_assets/test_script.script" )
453+ AssetPath :: from_path ( & PathBuf :: from( "test_assets/test_script.script" ) )
431454 ) ;
432455
433456 assert_eq ! (
@@ -457,7 +480,7 @@ mod tests {
457480
458481 assert_eq ! (
459482 asset. asset_path,
460- PathBuf :: from( "test_assets/test_script.script" )
483+ AssetPath :: from ( PathBuf :: from( "test_assets/test_script.script" ) )
461484 ) ;
462485 assert_eq ! (
463486 String :: from_utf8( asset. content. clone( ) . to_vec( ) ) . unwrap( ) ,
@@ -485,14 +508,14 @@ mod tests {
485508 fn test_script_asset_settings_select_language ( ) {
486509 let settings = make_test_settings ( ) ;
487510
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 ) ;
490513 assert_eq ! (
491- settings. select_script_language( Path :: new( "test.rhai" ) ) ,
514+ settings. select_script_language( & AssetPath :: from ( Path :: new( "test.rhai" ) ) ) ,
492515 Language :: Rhai
493516 ) ;
494517 assert_eq ! (
495- settings. select_script_language( Path :: new( "test.blob" ) ) ,
518+ settings. select_script_language( & AssetPath :: from ( Path :: new( "test.blob" ) ) ) ,
496519 Language :: Unknown
497520 ) ;
498521 }
0 commit comments