@@ -6,7 +6,7 @@ use crate::{
6
6
} ;
7
7
use bevy:: {
8
8
app:: { App , PreUpdate } ,
9
- asset:: { Asset , AssetEvent , AssetId , AssetLoader , Assets } ,
9
+ asset:: { Asset , AssetEvent , AssetId , AssetLoader , AssetPath , Assets } ,
10
10
ecs:: system:: Resource ,
11
11
log:: { debug, error, info, trace} ,
12
12
prelude:: {
@@ -16,10 +16,7 @@ use bevy::{
16
16
reflect:: TypePath ,
17
17
utils:: HashMap ,
18
18
} ;
19
- use std:: {
20
- borrow:: Cow ,
21
- path:: { Path , PathBuf } ,
22
- } ;
19
+ use std:: borrow:: Cow ;
23
20
24
21
/// Represents a scripting language. Languages which compile into another language should use the target language as their language.
25
22
#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
@@ -49,7 +46,7 @@ impl std::fmt::Display for Language {
49
46
pub struct ScriptAsset {
50
47
pub content : Box < [ u8 ] > ,
51
48
/// The virtual filesystem path of the asset, used to map to the script Id for asset backed scripts
52
- pub asset_path : PathBuf ,
49
+ pub asset_path : AssetPath < ' static > ,
53
50
}
54
51
55
52
#[ derive( Event , Debug , Clone ) ]
@@ -90,7 +87,7 @@ impl AssetLoader for ScriptAssetLoader {
90
87
}
91
88
let asset = ScriptAsset {
92
89
content : content. into_boxed_slice ( ) ,
93
- asset_path : load_context. path ( ) . to_owned ( ) ,
90
+ asset_path : load_context. asset_path ( ) . to_owned ( ) ,
94
91
} ;
95
92
Ok ( asset)
96
93
}
@@ -107,7 +104,7 @@ pub struct ScriptAssetSettings {
107
104
}
108
105
109
106
impl ScriptAssetSettings {
110
- pub fn select_script_language ( & self , path : & Path ) -> Language {
107
+ pub fn select_script_language ( & self , path : & AssetPath ) -> Language {
111
108
for mapper in & self . script_language_mappers {
112
109
let language = ( mapper. map ) ( path) ;
113
110
match language {
@@ -124,7 +121,7 @@ impl Default for ScriptAssetSettings {
124
121
fn default ( ) -> Self {
125
122
Self {
126
123
script_id_mapper : AssetPathToScriptIdMapper {
127
- map : ( |path : & Path | path. to_string_lossy ( ) . into_owned ( ) . into ( ) ) ,
124
+ map : ( |path : & AssetPath | path. path ( ) . to_string_lossy ( ) . into_owned ( ) . into ( ) ) ,
128
125
} ,
129
126
script_language_mappers : vec ! [ ] ,
130
127
}
@@ -134,12 +131,12 @@ impl Default for ScriptAssetSettings {
134
131
/// Strategy for mapping asset paths to script ids, by default this is the identity function
135
132
#[ derive( Clone , Copy ) ]
136
133
pub struct AssetPathToScriptIdMapper {
137
- pub map : fn ( & Path ) -> ScriptId ,
134
+ pub map : fn ( & AssetPath ) -> ScriptId ,
138
135
}
139
136
140
137
#[ derive( Clone , Copy ) ]
141
138
pub struct AssetPathToLanguageMapper {
142
- pub map : fn ( & Path ) -> Language ,
139
+ pub map : fn ( & AssetPath ) -> Language ,
143
140
}
144
141
145
142
/// 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.
@@ -333,6 +330,8 @@ pub(crate) fn configure_asset_systems_for_plugin<P: IntoScriptPluginParams>(
333
330
334
331
#[ cfg( test) ]
335
332
mod tests {
333
+ use std:: path:: { Path , PathBuf } ;
334
+
336
335
use bevy:: {
337
336
app:: { App , Update } ,
338
337
asset:: { AssetApp , AssetPlugin , AssetServer , Assets , Handle , LoadState } ,
@@ -352,12 +351,12 @@ mod tests {
352
351
fn make_test_settings ( ) -> ScriptAssetSettings {
353
352
ScriptAssetSettings {
354
353
script_id_mapper : AssetPathToScriptIdMapper {
355
- map : |path| path. to_string_lossy ( ) . into_owned ( ) . into ( ) ,
354
+ map : |path| path. path ( ) . to_string_lossy ( ) . into_owned ( ) . into ( ) ,
356
355
} ,
357
356
script_language_mappers : vec ! [
358
357
AssetPathToLanguageMapper {
359
358
map: |path| {
360
- if path. extension( ) . unwrap( ) == "lua" {
359
+ if path. path ( ) . extension( ) . unwrap( ) == "lua" {
361
360
Language :: Lua
362
361
} else {
363
362
Language :: Unknown
@@ -366,7 +365,7 @@ mod tests {
366
365
} ,
367
366
AssetPathToLanguageMapper {
368
367
map: |path| {
369
- if path. extension( ) . unwrap( ) == "rhai" {
368
+ if path. path ( ) . extension( ) . unwrap( ) == "rhai" {
370
369
Language :: Rhai
371
370
} else {
372
371
Language :: Unknown
@@ -427,7 +426,7 @@ mod tests {
427
426
428
427
assert_eq ! (
429
428
asset. asset_path,
430
- PathBuf :: from( "test_assets/test_script.script" )
429
+ AssetPath :: from_path ( & PathBuf :: from( "test_assets/test_script.script" ) )
431
430
) ;
432
431
433
432
assert_eq ! (
@@ -457,7 +456,7 @@ mod tests {
457
456
458
457
assert_eq ! (
459
458
asset. asset_path,
460
- PathBuf :: from( "test_assets/test_script.script" )
459
+ AssetPath :: from ( PathBuf :: from( "test_assets/test_script.script" ) )
461
460
) ;
462
461
assert_eq ! (
463
462
String :: from_utf8( asset. content. clone( ) . to_vec( ) ) . unwrap( ) ,
@@ -485,14 +484,14 @@ mod tests {
485
484
fn test_script_asset_settings_select_language ( ) {
486
485
let settings = make_test_settings ( ) ;
487
486
488
- let path = Path :: new ( "test.lua" ) ;
489
- assert_eq ! ( settings. select_script_language( path) , Language :: Lua ) ;
487
+ let path = AssetPath :: from ( Path :: new ( "test.lua" ) ) ;
488
+ assert_eq ! ( settings. select_script_language( & path) , Language :: Lua ) ;
490
489
assert_eq ! (
491
- settings. select_script_language( Path :: new( "test.rhai" ) ) ,
490
+ settings. select_script_language( & AssetPath :: from ( Path :: new( "test.rhai" ) ) ) ,
492
491
Language :: Rhai
493
492
) ;
494
493
assert_eq ! (
495
- settings. select_script_language( Path :: new( "test.blob" ) ) ,
494
+ settings. select_script_language( & AssetPath :: from ( Path :: new( "test.blob" ) ) ) ,
496
495
Language :: Unknown
497
496
) ;
498
497
}
0 commit comments