Skip to content

Commit d72e239

Browse files
committed
change asset path mappers to use actual asset path
1 parent a03ec73 commit d72e239

File tree

3 files changed

+25
-24
lines changed
  • crates
    • bevy_mod_scripting_core/src
    • languages
      • bevy_mod_scripting_lua/src
      • bevy_mod_scripting_rhai/src

3 files changed

+25
-24
lines changed

crates/bevy_mod_scripting_core/src/asset.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
};
77
use bevy::{
88
app::{App, PreUpdate},
9-
asset::{Asset, AssetEvent, AssetId, AssetLoader, Assets},
9+
asset::{Asset, AssetEvent, AssetId, AssetLoader, AssetPath, Assets},
1010
ecs::system::Resource,
1111
log::{debug, error, info, trace},
1212
prelude::{
@@ -16,10 +16,7 @@ use bevy::{
1616
reflect::TypePath,
1717
utils::HashMap,
1818
};
19-
use std::{
20-
borrow::Cow,
21-
path::{Path, PathBuf},
22-
};
19+
use std::borrow::Cow;
2320

2421
/// Represents a scripting language. Languages which compile into another language should use the target language as their language.
2522
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
@@ -49,7 +46,7 @@ impl std::fmt::Display for Language {
4946
pub struct ScriptAsset {
5047
pub content: Box<[u8]>,
5148
/// 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>,
5350
}
5451

5552
#[derive(Event, Debug, Clone)]
@@ -90,7 +87,7 @@ impl AssetLoader for ScriptAssetLoader {
9087
}
9188
let asset = ScriptAsset {
9289
content: content.into_boxed_slice(),
93-
asset_path: load_context.path().to_owned(),
90+
asset_path: load_context.asset_path().to_owned(),
9491
};
9592
Ok(asset)
9693
}
@@ -107,7 +104,7 @@ pub struct ScriptAssetSettings {
107104
}
108105

109106
impl ScriptAssetSettings {
110-
pub fn select_script_language(&self, path: &Path) -> Language {
107+
pub fn select_script_language(&self, path: &AssetPath) -> Language {
111108
for mapper in &self.script_language_mappers {
112109
let language = (mapper.map)(path);
113110
match language {
@@ -124,7 +121,7 @@ impl Default for ScriptAssetSettings {
124121
fn default() -> Self {
125122
Self {
126123
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()),
128125
},
129126
script_language_mappers: vec![],
130127
}
@@ -134,12 +131,12 @@ impl Default for ScriptAssetSettings {
134131
/// Strategy for mapping asset paths to script ids, by default this is the identity function
135132
#[derive(Clone, Copy)]
136133
pub struct AssetPathToScriptIdMapper {
137-
pub map: fn(&Path) -> ScriptId,
134+
pub map: fn(&AssetPath) -> ScriptId,
138135
}
139136

140137
#[derive(Clone, Copy)]
141138
pub struct AssetPathToLanguageMapper {
142-
pub map: fn(&Path) -> Language,
139+
pub map: fn(&AssetPath) -> Language,
143140
}
144141

145142
/// 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>(
333330

334331
#[cfg(test)]
335332
mod tests {
333+
use std::path::{Path, PathBuf};
334+
336335
use bevy::{
337336
app::{App, Update},
338337
asset::{AssetApp, AssetPlugin, AssetServer, Assets, Handle, LoadState},
@@ -352,12 +351,12 @@ mod tests {
352351
fn make_test_settings() -> ScriptAssetSettings {
353352
ScriptAssetSettings {
354353
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(),
356355
},
357356
script_language_mappers: vec![
358357
AssetPathToLanguageMapper {
359358
map: |path| {
360-
if path.extension().unwrap() == "lua" {
359+
if path.path().extension().unwrap() == "lua" {
361360
Language::Lua
362361
} else {
363362
Language::Unknown
@@ -366,7 +365,7 @@ mod tests {
366365
},
367366
AssetPathToLanguageMapper {
368367
map: |path| {
369-
if path.extension().unwrap() == "rhai" {
368+
if path.path().extension().unwrap() == "rhai" {
370369
Language::Rhai
371370
} else {
372371
Language::Unknown
@@ -427,7 +426,7 @@ mod tests {
427426

428427
assert_eq!(
429428
asset.asset_path,
430-
PathBuf::from("test_assets/test_script.script")
429+
AssetPath::from_path(&PathBuf::from("test_assets/test_script.script"))
431430
);
432431

433432
assert_eq!(
@@ -457,7 +456,7 @@ mod tests {
457456

458457
assert_eq!(
459458
asset.asset_path,
460-
PathBuf::from("test_assets/test_script.script")
459+
AssetPath::from(PathBuf::from("test_assets/test_script.script"))
461460
);
462461
assert_eq!(
463462
String::from_utf8(asset.content.clone().to_vec()).unwrap(),
@@ -485,14 +484,14 @@ mod tests {
485484
fn test_script_asset_settings_select_language() {
486485
let settings = make_test_settings();
487486

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);
490489
assert_eq!(
491-
settings.select_script_language(Path::new("test.rhai")),
490+
settings.select_script_language(&AssetPath::from(Path::new("test.rhai"))),
492491
Language::Rhai
493492
);
494493
assert_eq!(
495-
settings.select_script_language(Path::new("test.blob")),
494+
settings.select_script_language(&AssetPath::from(Path::new("test.blob"))),
496495
Language::Unknown
497496
);
498497
}

crates/languages/bevy_mod_scripting_lua/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bevy::{
22
app::Plugin,
3+
asset::AssetPath,
34
ecs::{entity::Entity, world::World},
45
};
56
use bevy_mod_scripting_core::{
@@ -133,8 +134,8 @@ impl Default for LuaScriptingPlugin {
133134
}
134135
}
135136
#[profiling::function]
136-
fn lua_language_mapper(path: &std::path::Path) -> Language {
137-
match path.extension().and_then(|ext| ext.to_str()) {
137+
fn lua_language_mapper(path: &AssetPath) -> Language {
138+
match path.path().extension().and_then(|ext| ext.to_str()) {
138139
Some("lua") => Language::Lua,
139140
_ => Language::Unknown,
140141
}

crates/languages/bevy_mod_scripting_rhai/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bevy::{
22
app::Plugin,
3+
asset::AssetPath,
34
ecs::{entity::Entity, world::World},
45
};
56
use bevy_mod_scripting_core::{
@@ -150,8 +151,8 @@ impl Default for RhaiScriptingPlugin {
150151
}
151152
}
152153

153-
fn rhai_language_mapper(path: &std::path::Path) -> Language {
154-
match path.extension().and_then(|ext| ext.to_str()) {
154+
fn rhai_language_mapper(path: &AssetPath) -> Language {
155+
match path.path().extension().and_then(|ext| ext.to_str()) {
155156
Some("rhai") => Language::Rhai,
156157
_ => Language::Unknown,
157158
}

0 commit comments

Comments
 (0)