Skip to content

Commit d6ea7a7

Browse files
committed
feature: Add ScriptSetttings to loader.
This introduces a serde dependency though. :(
1 parent 2636295 commit d6ea7a7

File tree

2 files changed

+24
-66
lines changed

2 files changed

+24
-66
lines changed

crates/bevy_mod_scripting_core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fixedbitset = "0.5"
4242
petgraph = "0.6"
4343
bevy_mod_debugdump = "0.12"
4444
bevy_system_reflection = { path = "../bevy_system_reflection", version = "0.1.1" }
45+
serde = { version = "1.0", features = ["derive"] }
4546

4647
[dev-dependencies]
4748
test_utils = { workspace = true }

crates/bevy_mod_scripting_core/src/asset.rs

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ use bevy::{
2121
utils::hashbrown::HashMap,
2222
};
2323
use std::{borrow::Cow, collections::VecDeque};
24+
use serde::{Deserialize, Serialize};
2425

2526
/// 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)]
2728
pub enum Language {
2829
/// The Rhai scripting language
2930
Rhai,
@@ -66,6 +67,13 @@ pub(crate) enum ScriptAssetEvent {
6667
Modified(ScriptMetadata),
6768
}
6869

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+
6977
#[derive(Default)]
7078
/// A loader for script assets
7179
pub struct ScriptAssetLoader {
@@ -79,14 +87,14 @@ pub struct ScriptAssetLoader {
7987
impl AssetLoader for ScriptAssetLoader {
8088
type Asset = ScriptAsset;
8189

82-
type Settings = ();
90+
type Settings = ScriptSettings;
8391

8492
type Error = ScriptError;
8593

8694
async fn load(
8795
&self,
8896
reader: &mut dyn bevy::asset::io::Reader,
89-
_settings: &Self::Settings,
97+
settings: &Self::Settings,
9098
load_context: &mut bevy::asset::LoadContext<'_>,
9199
) -> Result<Self::Asset, Self::Error> {
92100
let mut content = Vec::new();
@@ -97,14 +105,18 @@ impl AssetLoader for ScriptAssetLoader {
97105
if let Some(processor) = &self.preprocessor {
98106
processor(&mut content)?;
99107
}
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+
});
108120
let asset = ScriptAsset {
109121
content: content.into_boxed_slice(),
110122
language,
@@ -117,61 +129,6 @@ impl AssetLoader for ScriptAssetLoader {
117129
}
118130
}
119131

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-
// }
175132

176133
/// 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.
177134
#[derive(Default, Debug, Resource)]

0 commit comments

Comments
 (0)